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 (!::IsUserAnAdmin() ||
66 !PathService::Get(DIR_SYSTEM_TEMP, &expected_parent_dir)) {
67 EXPECT_TRUE(PathService::Get(DIR_TEMP, &expected_parent_dir));
68 }
69 EXPECT_TRUE(expected_parent_dir.IsParent(test_path));
70 #else // BUILDFLAG(IS_WIN)
71 FilePath tmp_dir;
72 EXPECT_TRUE(GetTempDir(&tmp_dir));
73 EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
74 #endif // BUILDFLAG(IS_WIN)
75 }
76 EXPECT_FALSE(DirectoryExists(test_path));
77 }
78
TEST(ScopedTempDir,UniqueTempDirUnderPath)79 TEST(ScopedTempDir, UniqueTempDirUnderPath) {
80 // Create a path which will contain a unique temp path.
81 FilePath base_path;
82 ASSERT_TRUE(
83 CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"), &base_path));
84
85 FilePath test_path;
86 {
87 ScopedTempDir dir;
88 EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
89 test_path = dir.GetPath();
90 EXPECT_TRUE(DirectoryExists(test_path));
91 EXPECT_TRUE(base_path.IsParent(test_path));
92 EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos);
93 }
94 EXPECT_FALSE(DirectoryExists(test_path));
95 DeletePathRecursively(base_path);
96 }
97
TEST(ScopedTempDir,MultipleInvocations)98 TEST(ScopedTempDir, MultipleInvocations) {
99 ScopedTempDir dir;
100 EXPECT_TRUE(dir.CreateUniqueTempDir());
101 EXPECT_FALSE(dir.CreateUniqueTempDir());
102 EXPECT_TRUE(dir.Delete());
103 EXPECT_TRUE(dir.CreateUniqueTempDir());
104 EXPECT_FALSE(dir.CreateUniqueTempDir());
105 ScopedTempDir other_dir;
106 EXPECT_TRUE(other_dir.Set(dir.Take()));
107 EXPECT_TRUE(dir.CreateUniqueTempDir());
108 EXPECT_FALSE(dir.CreateUniqueTempDir());
109 EXPECT_FALSE(other_dir.CreateUniqueTempDir());
110 }
111
TEST(ScopedTempDir,Move)112 TEST(ScopedTempDir, Move) {
113 ScopedTempDir dir;
114 EXPECT_TRUE(dir.CreateUniqueTempDir());
115 FilePath dir_path = dir.GetPath();
116 EXPECT_TRUE(DirectoryExists(dir_path));
117 {
118 ScopedTempDir other_dir(std::move(dir));
119 EXPECT_EQ(dir_path, other_dir.GetPath());
120 EXPECT_TRUE(DirectoryExists(dir_path));
121 EXPECT_FALSE(dir.IsValid());
122 }
123 EXPECT_FALSE(DirectoryExists(dir_path));
124 }
125
126 #if BUILDFLAG(IS_WIN)
TEST(ScopedTempDir,LockedTempDir)127 TEST(ScopedTempDir, LockedTempDir) {
128 ScopedTempDir dir;
129 EXPECT_TRUE(dir.CreateUniqueTempDir());
130 File file(dir.GetPath().Append(FILE_PATH_LITERAL("temp")),
131 File::FLAG_CREATE_ALWAYS | File::FLAG_WRITE);
132 EXPECT_TRUE(file.IsValid());
133 EXPECT_EQ(File::FILE_OK, file.error_details());
134 EXPECT_FALSE(dir.Delete()); // We should not be able to delete.
135 EXPECT_FALSE(dir.GetPath().empty()); // We should still have a valid path.
136 file.Close();
137 // Now, we should be able to delete.
138 EXPECT_TRUE(dir.Delete());
139 }
140 #endif // BUILDFLAG(IS_WIN)
141
142 } // namespace base
143