| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/files/file.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace base { | |
| 13 | |
| 14 TEST(ScopedTempDir, FullPath) { | |
| 15 FilePath test_path; | |
| 16 base::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"), | |
| 17 &test_path); | |
| 18 | |
| 19 // Against an existing dir, it should get destroyed when leaving scope. | |
| 20 EXPECT_TRUE(DirectoryExists(test_path)); | |
| 21 { | |
| 22 ScopedTempDir dir; | |
| 23 EXPECT_TRUE(dir.Set(test_path)); | |
| 24 EXPECT_TRUE(dir.IsValid()); | |
| 25 } | |
| 26 EXPECT_FALSE(DirectoryExists(test_path)); | |
| 27 | |
| 28 { | |
| 29 ScopedTempDir dir; | |
| 30 EXPECT_TRUE(dir.Set(test_path)); | |
| 31 // Now the dir doesn't exist, so ensure that it gets created. | |
| 32 EXPECT_TRUE(DirectoryExists(test_path)); | |
| 33 // When we call Release(), it shouldn't get destroyed when leaving scope. | |
| 34 FilePath path = dir.Take(); | |
| 35 EXPECT_EQ(path.value(), test_path.value()); | |
| 36 EXPECT_FALSE(dir.IsValid()); | |
| 37 } | |
| 38 EXPECT_TRUE(DirectoryExists(test_path)); | |
| 39 | |
| 40 // Clean up. | |
| 41 { | |
| 42 ScopedTempDir dir; | |
| 43 EXPECT_TRUE(dir.Set(test_path)); | |
| 44 } | |
| 45 EXPECT_FALSE(DirectoryExists(test_path)); | |
| 46 } | |
| 47 | |
| 48 TEST(ScopedTempDir, TempDir) { | |
| 49 // In this case, just verify that a directory was created and that it's a | |
| 50 // child of TempDir. | |
| 51 FilePath test_path; | |
| 52 { | |
| 53 ScopedTempDir dir; | |
| 54 EXPECT_TRUE(dir.CreateUniqueTempDir()); | |
| 55 test_path = dir.path(); | |
| 56 EXPECT_TRUE(DirectoryExists(test_path)); | |
| 57 FilePath tmp_dir; | |
| 58 EXPECT_TRUE(base::GetTempDir(&tmp_dir)); | |
| 59 EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos); | |
| 60 } | |
| 61 EXPECT_FALSE(DirectoryExists(test_path)); | |
| 62 } | |
| 63 | |
| 64 TEST(ScopedTempDir, UniqueTempDirUnderPath) { | |
| 65 // Create a path which will contain a unique temp path. | |
| 66 FilePath base_path; | |
| 67 ASSERT_TRUE(base::CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"), | |
| 68 &base_path)); | |
| 69 | |
| 70 FilePath test_path; | |
| 71 { | |
| 72 ScopedTempDir dir; | |
| 73 EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path)); | |
| 74 test_path = dir.path(); | |
| 75 EXPECT_TRUE(DirectoryExists(test_path)); | |
| 76 EXPECT_TRUE(base_path.IsParent(test_path)); | |
| 77 EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos); | |
| 78 } | |
| 79 EXPECT_FALSE(DirectoryExists(test_path)); | |
| 80 base::DeleteFile(base_path, true); | |
| 81 } | |
| 82 | |
| 83 TEST(ScopedTempDir, MultipleInvocations) { | |
| 84 ScopedTempDir dir; | |
| 85 EXPECT_TRUE(dir.CreateUniqueTempDir()); | |
| 86 EXPECT_FALSE(dir.CreateUniqueTempDir()); | |
| 87 EXPECT_TRUE(dir.Delete()); | |
| 88 EXPECT_TRUE(dir.CreateUniqueTempDir()); | |
| 89 EXPECT_FALSE(dir.CreateUniqueTempDir()); | |
| 90 ScopedTempDir other_dir; | |
| 91 EXPECT_TRUE(other_dir.Set(dir.Take())); | |
| 92 EXPECT_TRUE(dir.CreateUniqueTempDir()); | |
| 93 EXPECT_FALSE(dir.CreateUniqueTempDir()); | |
| 94 EXPECT_FALSE(other_dir.CreateUniqueTempDir()); | |
| 95 } | |
| 96 | |
| 97 #if defined(OS_WIN) | |
| 98 TEST(ScopedTempDir, LockedTempDir) { | |
| 99 ScopedTempDir dir; | |
| 100 EXPECT_TRUE(dir.CreateUniqueTempDir()); | |
| 101 base::File file(dir.path().Append(FILE_PATH_LITERAL("temp")), | |
| 102 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | |
| 103 EXPECT_TRUE(file.IsValid()); | |
| 104 EXPECT_EQ(base::File::FILE_OK, file.error_details()); | |
| 105 EXPECT_FALSE(dir.Delete()); // We should not be able to delete. | |
| 106 EXPECT_FALSE(dir.path().empty()); // We should still have a valid path. | |
| 107 file.Close(); | |
| 108 // Now, we should be able to delete. | |
| 109 EXPECT_TRUE(dir.Delete()); | |
| 110 } | |
| 111 #endif // defined(OS_WIN) | |
| 112 | |
| 113 } // namespace base | |
| OLD | NEW |