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