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