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.
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"
12 TEST(ScopedTempDir
, FullPath
) {
14 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"),
17 // Against an existing dir, it should get destroyed when leaving scope.
18 EXPECT_TRUE(file_util::DirectoryExists(test_path
));
21 EXPECT_TRUE(dir
.Set(test_path
));
22 EXPECT_TRUE(dir
.IsValid());
24 EXPECT_FALSE(file_util::DirectoryExists(test_path
));
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());
36 EXPECT_TRUE(file_util::DirectoryExists(test_path
));
41 EXPECT_TRUE(dir
.Set(test_path
));
43 EXPECT_FALSE(file_util::DirectoryExists(test_path
));
46 TEST(ScopedTempDir
, TempDir
) {
47 // In this case, just verify that a directory was created and that it's a
52 EXPECT_TRUE(dir
.CreateUniqueTempDir());
53 test_path
= dir
.path();
54 EXPECT_TRUE(file_util::DirectoryExists(test_path
));
56 EXPECT_TRUE(file_util::GetTempDir(&tmp_dir
));
57 EXPECT_TRUE(test_path
.value().find(tmp_dir
.value()) != std::string::npos
);
59 EXPECT_FALSE(file_util::DirectoryExists(test_path
));
62 TEST(ScopedTempDir
, UniqueTempDirUnderPath
) {
63 // Create a path which will contain a unique temp path.
65 ASSERT_TRUE(file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("base_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
);
77 EXPECT_FALSE(file_util::DirectoryExists(test_path
));
78 file_util::Delete(base_path
, true);
81 TEST(ScopedTempDir
, MultipleInvocations
) {
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());
96 TEST(ScopedTempDir
, LockedTempDir
) {
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
,
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());
113 #endif // defined(OS_WIN)