1 // Copyright 2011 The Chromium Authors
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 "base/path_service.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 #if BUILDFLAG(IS_WIN)
15 #include <shlobj.h>
16 #endif
17
18 namespace base {
19
TEST(ScopedTempDir,FullPath)20 TEST(ScopedTempDir, FullPath) {
21 FilePath test_path;
22 CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"), &test_path);
23
24 // Against an existing dir, it should get destroyed when leaving scope.
25 EXPECT_TRUE(DirectoryExists(test_path));
26 {
27 ScopedTempDir dir;
28 EXPECT_TRUE(dir.Set(test_path));
29 EXPECT_TRUE(dir.IsValid());
30 }
31 EXPECT_FALSE(DirectoryExists(test_path));
32
33 {
34 ScopedTempDir dir;
35 EXPECT_TRUE(dir.Set(test_path));
36 // Now the dir doesn't exist, so ensure that it gets created.
37 EXPECT_TRUE(DirectoryExists(test_path));
38 // When we call Release(), it shouldn't get destroyed when leaving scope.
39 FilePath path = dir.Take();
40 EXPECT_EQ(path.value(), test_path.value());
41 EXPECT_FALSE(dir.IsValid());
42 }
43 EXPECT_TRUE(DirectoryExists(test_path));
44
45 // Clean up.
46 {
47 ScopedTempDir dir;
48 EXPECT_TRUE(dir.Set(test_path));
49 }
50 EXPECT_FALSE(DirectoryExists(test_path));
51 }
52
TEST(ScopedTempDir,TempDir)53 TEST(ScopedTempDir, TempDir) {
54 // In this case, just verify that a directory was created and that it's a
55 // child of TempDir.
56 FilePath test_path;
57 {
58 ScopedTempDir dir;
59 EXPECT_TRUE(dir.CreateUniqueTempDir());
60 test_path = dir.GetPath();
61 EXPECT_TRUE(DirectoryExists(test_path));
62
63 #if BUILDFLAG(IS_WIN)
64 FilePath expected_parent_dir;
65 if (!GetSecureSystemTemp(&expected_parent_dir)) {
66 EXPECT_TRUE(PathService::Get(DIR_TEMP, &expected_parent_dir));
67 }
68 EXPECT_TRUE(expected_parent_dir.IsParent(test_path));
69 #else // BUILDFLAG(IS_WIN)
70 FilePath tmp_dir;
71 EXPECT_TRUE(GetTempDir(&tmp_dir));
72 EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
73 #endif // BUILDFLAG(IS_WIN)
74 }
75 EXPECT_FALSE(DirectoryExists(test_path));
76 }
77
TEST(ScopedTempDir,UniqueTempDirUnderPath)78 TEST(ScopedTempDir, UniqueTempDirUnderPath) {
79 // Create a path which will contain a unique temp path.
80 FilePath base_path;
81 ASSERT_TRUE(
82 CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"), &base_path));
83
84 FilePath test_path;
85 {
86 ScopedTempDir dir;
87 EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
88 test_path = dir.GetPath();
89 EXPECT_TRUE(DirectoryExists(test_path));
90 EXPECT_TRUE(base_path.IsParent(test_path));
91 EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos);
92 }
93 EXPECT_FALSE(DirectoryExists(test_path));
94 DeletePathRecursively(base_path);
95 }
96
TEST(ScopedTempDir,MultipleInvocations)97 TEST(ScopedTempDir, MultipleInvocations) {
98 ScopedTempDir dir;
99 EXPECT_TRUE(dir.CreateUniqueTempDir());
100 EXPECT_FALSE(dir.CreateUniqueTempDir());
101 EXPECT_TRUE(dir.Delete());
102 EXPECT_TRUE(dir.CreateUniqueTempDir());
103 EXPECT_FALSE(dir.CreateUniqueTempDir());
104 ScopedTempDir other_dir;
105 EXPECT_TRUE(other_dir.Set(dir.Take()));
106 EXPECT_TRUE(dir.CreateUniqueTempDir());
107 EXPECT_FALSE(dir.CreateUniqueTempDir());
108 EXPECT_FALSE(other_dir.CreateUniqueTempDir());
109 }
110
TEST(ScopedTempDir,Move)111 TEST(ScopedTempDir, Move) {
112 ScopedTempDir dir;
113 EXPECT_TRUE(dir.CreateUniqueTempDir());
114 FilePath dir_path = dir.GetPath();
115 EXPECT_TRUE(DirectoryExists(dir_path));
116 {
117 ScopedTempDir other_dir(std::move(dir));
118 EXPECT_EQ(dir_path, other_dir.GetPath());
119 EXPECT_TRUE(DirectoryExists(dir_path));
120 EXPECT_FALSE(dir.IsValid());
121 }
122 EXPECT_FALSE(DirectoryExists(dir_path));
123 }
124
125 #if BUILDFLAG(IS_WIN)
TEST(ScopedTempDir,LockedTempDir)126 TEST(ScopedTempDir, LockedTempDir) {
127 ScopedTempDir dir;
128 EXPECT_TRUE(dir.CreateUniqueTempDir());
129 File file(dir.GetPath().Append(FILE_PATH_LITERAL("temp")),
130 File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE);
131 EXPECT_TRUE(file.IsValid());
132 EXPECT_EQ(File::FILE_OK, file.error_details());
133 EXPECT_FALSE(dir.Delete()); // We should not be able to delete.
134 EXPECT_FALSE(dir.GetPath().empty()); // We should still have a valid path.
135 file.Close();
136 // Now, we should be able to delete.
137 EXPECT_TRUE(dir.Delete());
138 }
139 #endif // BUILDFLAG(IS_WIN)
140
141 } // namespace base
142