Update broken references to image assets
[chromium-blink-merge.git] / chrome / installer / util / self_cleaning_temp_dir_unittest.cc
blob64445e697ad7522cbb29f65fce37e7092b6f7d2a
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 <windows.h>
6 #include <wincrypt.h>
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/installer/util/self_cleaning_temp_dir.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
16 // Returns a string of 8 characters consisting of the letter 'R' followed by
17 // seven random hex digits.
18 std::string GetRandomFilename() {
19 uint8 data[4];
20 HCRYPTPROV crypt_ctx = NULL;
22 // Get four bytes of randomness. Use CAPI rather than the CRT since I've
23 // seen the latter trivially repeat.
24 EXPECT_NE(FALSE, CryptAcquireContext(&crypt_ctx, NULL, NULL, PROV_RSA_FULL,
25 CRYPT_VERIFYCONTEXT));
26 EXPECT_NE(FALSE, CryptGenRandom(crypt_ctx, arraysize(data), &data[0]));
27 EXPECT_NE(FALSE, CryptReleaseContext(crypt_ctx, 0));
29 // Hexify the value.
30 std::string result(base::HexEncode(&data[0], arraysize(data)));
31 EXPECT_EQ(8, result.size());
33 // Replace the first digit with the letter 'R' (for "random", get it?).
34 result[0] = 'R';
36 return result;
39 } // namespace
41 namespace installer {
43 class SelfCleaningTempDirTest : public testing::Test {
46 // Test the implementation of GetTopDirToCreate when given the root of a
47 // volume.
48 TEST_F(SelfCleaningTempDirTest, TopLevel) {
49 base::FilePath base_dir;
50 SelfCleaningTempDir::GetTopDirToCreate(base::FilePath(L"C:\\"), &base_dir);
51 EXPECT_TRUE(base_dir.empty());
54 // Test the implementation of GetTopDirToCreate when given a non-existant dir
55 // under the root of a volume.
56 TEST_F(SelfCleaningTempDirTest, TopLevelPlusOne) {
57 base::FilePath base_dir;
58 base::FilePath parent_dir(L"C:\\");
59 parent_dir = parent_dir.AppendASCII(GetRandomFilename());
60 SelfCleaningTempDir::GetTopDirToCreate(parent_dir, &base_dir);
61 EXPECT_EQ(parent_dir, base_dir);
64 // Test that all intermediate dirs are cleaned up if they're empty when
65 // Delete() is called.
66 TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDelete) {
67 // Make a directory in which we'll work.
68 base::ScopedTempDir work_dir;
69 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
71 // Make up some path under the temp dir.
72 base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
73 SelfCleaningTempDir temp_dir;
74 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
75 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
76 EXPECT_TRUE(base::DirectoryExists(temp_dir.path()));
77 EXPECT_TRUE(temp_dir.Delete());
78 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
79 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir));
80 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName()));
81 EXPECT_TRUE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
82 EXPECT_TRUE(work_dir.Delete());
83 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
86 // Test that two clients can work in the same area.
87 TEST_F(SelfCleaningTempDirTest, TwoClients) {
88 // Make a directory in which we'll work.
89 base::ScopedTempDir work_dir;
90 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
92 // Make up some path under the temp dir.
93 base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
94 SelfCleaningTempDir temp_dir1;
95 SelfCleaningTempDir temp_dir2;
96 // First client is created.
97 EXPECT_TRUE(temp_dir1.Initialize(parent_temp_dir, L"Three"));
98 // Second client is created in the same space.
99 EXPECT_TRUE(temp_dir2.Initialize(parent_temp_dir, L"Three"));
100 // Both clients are where they are expected.
101 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir1.path());
102 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir2.path());
103 EXPECT_TRUE(base::DirectoryExists(temp_dir1.path()));
104 EXPECT_TRUE(base::DirectoryExists(temp_dir2.path()));
105 // Second client goes away.
106 EXPECT_TRUE(temp_dir2.Delete());
107 // The first is now useless.
108 EXPECT_FALSE(base::DirectoryExists(temp_dir1.path()));
109 // But the intermediate dirs are still present
110 EXPECT_TRUE(base::DirectoryExists(parent_temp_dir));
111 // Now the first goes away.
112 EXPECT_TRUE(temp_dir1.Delete());
113 // And cleans up after itself.
114 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
115 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir));
116 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName()));
117 EXPECT_TRUE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
118 EXPECT_TRUE(work_dir.Delete());
119 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
122 // Test that all intermediate dirs are cleaned up if they're empty when the
123 // destructor is called.
124 TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDestroy) {
125 // Make a directory in which we'll work.
126 base::ScopedTempDir work_dir;
127 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
129 // Make up some path under the temp dir.
130 base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
132 SelfCleaningTempDir temp_dir;
133 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
134 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
135 EXPECT_TRUE(base::DirectoryExists(temp_dir.path()));
137 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
138 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir));
139 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName()));
140 EXPECT_TRUE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
141 EXPECT_TRUE(work_dir.Delete());
142 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
145 // Test that intermediate dirs are left behind if they're not empty when the
146 // destructor is called.
147 TEST_F(SelfCleaningTempDirTest, LeaveUsedOnDestroy) {
148 static const char kHiHon[] = "hi, hon";
150 // Make a directory in which we'll work.
151 base::ScopedTempDir work_dir;
152 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
154 // Make up some path under the temp dir.
155 base::FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
157 SelfCleaningTempDir temp_dir;
158 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
159 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
160 EXPECT_TRUE(base::DirectoryExists(temp_dir.path()));
161 // Drop a file somewhere.
162 EXPECT_EQ(arraysize(kHiHon) - 1,
163 base::WriteFile(parent_temp_dir.AppendASCII(GetRandomFilename()),
164 kHiHon, arraysize(kHiHon) - 1));
166 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.Append(L"Three")));
167 EXPECT_TRUE(base::DirectoryExists(parent_temp_dir));
168 EXPECT_TRUE(work_dir.Delete());
169 EXPECT_FALSE(base::DirectoryExists(parent_temp_dir.DirName().DirName()));
172 } // namespace installer