Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: base/file_util_unittest.cc

Issue 99923002: Move temp file functions to base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 // different short name. 524 // different short name.
525 WCHAR path_buffer[MAX_PATH]; 525 WCHAR path_buffer[MAX_PATH];
526 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(), 526 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
527 path_buffer, MAX_PATH); 527 path_buffer, MAX_PATH);
528 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH)); 528 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
529 ASSERT_NE(DWORD(0), path_buffer_length); 529 ASSERT_NE(DWORD(0), path_buffer_length);
530 FilePath short_test_dir(path_buffer); 530 FilePath short_test_dir(path_buffer);
531 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str()); 531 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
532 532
533 FilePath temp_file; 533 FilePath temp_file;
534 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file)); 534 ASSERT_TRUE(base::CreateTemporaryFileInDir(short_test_dir, &temp_file));
535 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str()); 535 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
536 EXPECT_TRUE(PathExists(temp_file)); 536 EXPECT_TRUE(PathExists(temp_file));
537 537
538 // Create a subdirectory of |long_test_dir| and make |long_test_dir| 538 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
539 // unreadable. We should still be able to create a temp file in the 539 // unreadable. We should still be able to create a temp file in the
540 // subdirectory, but we won't be able to determine the long path for it. This 540 // subdirectory, but we won't be able to determine the long path for it. This
541 // mimics the environment that some users run where their user profiles reside 541 // mimics the environment that some users run where their user profiles reside
542 // in a location where the don't have full access to the higher level 542 // in a location where the don't have full access to the higher level
543 // directories. (Note that this assumption is true for NTFS, but not for some 543 // directories. (Note that this assumption is true for NTFS, but not for some
544 // network file systems. E.g. AFS). 544 // network file systems. E.g. AFS).
545 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName); 545 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
546 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir)); 546 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir));
547 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir); 547 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
548 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir)); 548 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
549 549
550 // Use the short form of the directory to create a temporary filename. 550 // Use the short form of the directory to create a temporary filename.
551 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( 551 ASSERT_TRUE(base::CreateTemporaryFileInDir(
552 short_test_dir.Append(kTestSubDirName), &temp_file)); 552 short_test_dir.Append(kTestSubDirName), &temp_file));
553 EXPECT_TRUE(PathExists(temp_file)); 553 EXPECT_TRUE(PathExists(temp_file));
554 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName())); 554 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
555 555
556 // Check that the long path can't be determined for |temp_file|. 556 // Check that the long path can't be determined for |temp_file|.
557 path_buffer_length = GetLongPathName(temp_file.value().c_str(), 557 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
558 path_buffer, MAX_PATH); 558 path_buffer, MAX_PATH);
559 EXPECT_EQ(DWORD(0), path_buffer_length); 559 EXPECT_EQ(DWORD(0), path_buffer_length);
560 } 560 }
561 561
(...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after
1575 free(original_tmp); 1575 free(original_tmp);
1576 } else { 1576 } else {
1577 ::_tputenv_s(kTmpKey, _T("")); 1577 ::_tputenv_s(kTmpKey, _T(""));
1578 } 1578 }
1579 } 1579 }
1580 #endif // OS_WIN 1580 #endif // OS_WIN
1581 1581
1582 TEST_F(FileUtilTest, CreateTemporaryFileTest) { 1582 TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1583 FilePath temp_files[3]; 1583 FilePath temp_files[3];
1584 for (int i = 0; i < 3; i++) { 1584 for (int i = 0; i < 3; i++) {
1585 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); 1585 ASSERT_TRUE(base::CreateTemporaryFile(&(temp_files[i])));
1586 EXPECT_TRUE(PathExists(temp_files[i])); 1586 EXPECT_TRUE(PathExists(temp_files[i]));
1587 EXPECT_FALSE(DirectoryExists(temp_files[i])); 1587 EXPECT_FALSE(DirectoryExists(temp_files[i]));
1588 } 1588 }
1589 for (int i = 0; i < 3; i++) 1589 for (int i = 0; i < 3; i++)
1590 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); 1590 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1591 for (int i = 0; i < 3; i++) 1591 for (int i = 0; i < 3; i++)
1592 EXPECT_TRUE(DeleteFile(temp_files[i], false)); 1592 EXPECT_TRUE(DeleteFile(temp_files[i], false));
1593 } 1593 }
1594 1594
1595 TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { 1595 TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
1596 FilePath names[3]; 1596 FilePath names[3];
1597 FILE* fps[3]; 1597 FILE* fps[3];
1598 int i; 1598 int i;
1599 1599
1600 // Create; make sure they are open and exist. 1600 // Create; make sure they are open and exist.
1601 for (i = 0; i < 3; ++i) { 1601 for (i = 0; i < 3; ++i) {
1602 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); 1602 fps[i] = base::CreateAndOpenTemporaryFile(&(names[i]));
1603 ASSERT_TRUE(fps[i]); 1603 ASSERT_TRUE(fps[i]);
1604 EXPECT_TRUE(PathExists(names[i])); 1604 EXPECT_TRUE(PathExists(names[i]));
1605 } 1605 }
1606 1606
1607 // Make sure all names are unique. 1607 // Make sure all names are unique.
1608 for (i = 0; i < 3; ++i) { 1608 for (i = 0; i < 3; ++i) {
1609 EXPECT_FALSE(names[i] == names[(i+1)%3]); 1609 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1610 } 1610 }
1611 1611
1612 // Close and delete. 1612 // Close and delete.
1613 for (i = 0; i < 3; ++i) { 1613 for (i = 0; i < 3; ++i) {
1614 EXPECT_TRUE(file_util::CloseFile(fps[i])); 1614 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1615 EXPECT_TRUE(DeleteFile(names[i], false)); 1615 EXPECT_TRUE(DeleteFile(names[i], false));
1616 } 1616 }
1617 } 1617 }
1618 1618
1619 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { 1619 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
1620 FilePath temp_dir; 1620 FilePath temp_dir;
1621 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), 1621 ASSERT_TRUE(base::CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
1622 &temp_dir));
1623 EXPECT_TRUE(PathExists(temp_dir)); 1622 EXPECT_TRUE(PathExists(temp_dir));
1624 EXPECT_TRUE(DeleteFile(temp_dir, false)); 1623 EXPECT_TRUE(DeleteFile(temp_dir, false));
1625 } 1624 }
1626 1625
1627 TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) { 1626 TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1628 FilePath new_dir; 1627 FilePath new_dir;
1629 ASSERT_TRUE(file_util::CreateTemporaryDirInDir( 1628 ASSERT_TRUE(base::CreateTemporaryDirInDir(
1630 temp_dir_.path(), 1629 temp_dir_.path(),
1631 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"), 1630 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1632 &new_dir)); 1631 &new_dir));
1633 EXPECT_TRUE(PathExists(new_dir)); 1632 EXPECT_TRUE(PathExists(new_dir));
1634 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir)); 1633 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
1635 EXPECT_TRUE(DeleteFile(new_dir, false)); 1634 EXPECT_TRUE(DeleteFile(new_dir, false));
1636 } 1635 }
1637 1636
1638 TEST_F(FileUtilTest, GetShmemTempDirTest) { 1637 TEST_F(FileUtilTest, GetShmemTempDirTest) {
1639 FilePath dir; 1638 FilePath dir;
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
2353 int fd = OpenContentUriForRead(path); 2352 int fd = OpenContentUriForRead(path);
2354 EXPECT_EQ(-1, fd); 2353 EXPECT_EQ(-1, fd);
2355 } 2354 }
2356 #endif 2355 #endif
2357 2356
2358 #endif // defined(OS_POSIX) 2357 #endif // defined(OS_POSIX)
2359 2358
2360 } // namespace 2359 } // namespace
2361 2360
2362 } // namespace base 2361 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698