Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / base / scoped_temp_dir_unittest.cc
blobeddea833e533ef3eef3c32489e43f60cbe801c73
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.
5 #include <string>
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) {
13 FilePath test_path;
14 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"),
15 &test_path);
17 // Against an existing dir, it should get destroyed when leaving scope.
18 EXPECT_TRUE(file_util::DirectoryExists(test_path));
20 ScopedTempDir dir;
21 EXPECT_TRUE(dir.Set(test_path));
22 EXPECT_TRUE(dir.IsValid());
24 EXPECT_FALSE(file_util::DirectoryExists(test_path));
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());
36 EXPECT_TRUE(file_util::DirectoryExists(test_path));
38 // Clean up.
40 ScopedTempDir dir;
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
48 // child of TempDir.
49 FilePath test_path;
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);
59 EXPECT_FALSE(file_util::DirectoryExists(test_path));
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));
68 FilePath test_path;
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);
77 EXPECT_FALSE(file_util::DirectoryExists(test_path));
78 file_util::Delete(base_path, true);
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());
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());
113 #endif // defined(OS_WIN)