Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: chrome/installer/util/self_cleaning_temp_dir_unittest.cc

Issue 11359217: Move scoped_temp_dir from base to base/files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <windows.h> 5 #include <windows.h>
6 #include <wincrypt.h> 6 #include <wincrypt.h>
7 7
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/installer/util/self_cleaning_temp_dir.h" 12 #include "chrome/installer/util/self_cleaning_temp_dir.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace { 15 namespace {
16 16
17 // Returns a string of 8 characters consisting of the letter 'R' followed by 17 // Returns a string of 8 characters consisting of the letter 'R' followed by
18 // seven random hex digits. 18 // seven random hex digits.
19 std::wstring GetRandomFilename() { 19 std::wstring GetRandomFilename() {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 FilePath parent_dir(L"C:\\"); 59 FilePath parent_dir(L"C:\\");
60 parent_dir = parent_dir.Append(GetRandomFilename()); 60 parent_dir = parent_dir.Append(GetRandomFilename());
61 SelfCleaningTempDir::GetTopDirToCreate(parent_dir, &base_dir); 61 SelfCleaningTempDir::GetTopDirToCreate(parent_dir, &base_dir);
62 EXPECT_EQ(parent_dir, base_dir); 62 EXPECT_EQ(parent_dir, base_dir);
63 } 63 }
64 64
65 // Test that all intermediate dirs are cleaned up if they're empty when 65 // Test that all intermediate dirs are cleaned up if they're empty when
66 // Delete() is called. 66 // Delete() is called.
67 TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDelete) { 67 TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDelete) {
68 // Make a directory in which we'll work. 68 // Make a directory in which we'll work.
69 ScopedTempDir work_dir; 69 base::ScopedTempDir work_dir;
70 EXPECT_TRUE(work_dir.CreateUniqueTempDir()); 70 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
71 71
72 // Make up some path under the temp dir. 72 // Make up some path under the temp dir.
73 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two")); 73 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
74 SelfCleaningTempDir temp_dir; 74 SelfCleaningTempDir temp_dir;
75 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three")); 75 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
76 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path()); 76 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
77 EXPECT_TRUE(file_util::DirectoryExists(temp_dir.path())); 77 EXPECT_TRUE(file_util::DirectoryExists(temp_dir.path()));
78 EXPECT_TRUE(temp_dir.Delete()); 78 EXPECT_TRUE(temp_dir.Delete());
79 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.Append(L"Three"))); 79 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.Append(L"Three")));
80 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir)); 80 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir));
81 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName())); 81 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName()));
82 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 82 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
83 EXPECT_TRUE(work_dir.Delete()); 83 EXPECT_TRUE(work_dir.Delete());
84 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 84 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
85 } 85 }
86 86
87 // Test that two clients can work in the same area. 87 // Test that two clients can work in the same area.
88 TEST_F(SelfCleaningTempDirTest, TwoClients) { 88 TEST_F(SelfCleaningTempDirTest, TwoClients) {
89 // Make a directory in which we'll work. 89 // Make a directory in which we'll work.
90 ScopedTempDir work_dir; 90 base::ScopedTempDir work_dir;
91 EXPECT_TRUE(work_dir.CreateUniqueTempDir()); 91 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
92 92
93 // Make up some path under the temp dir. 93 // Make up some path under the temp dir.
94 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two")); 94 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
95 SelfCleaningTempDir temp_dir1; 95 SelfCleaningTempDir temp_dir1;
96 SelfCleaningTempDir temp_dir2; 96 SelfCleaningTempDir temp_dir2;
97 // First client is created. 97 // First client is created.
98 EXPECT_TRUE(temp_dir1.Initialize(parent_temp_dir, L"Three")); 98 EXPECT_TRUE(temp_dir1.Initialize(parent_temp_dir, L"Three"));
99 // Second client is created in the same space. 99 // Second client is created in the same space.
100 EXPECT_TRUE(temp_dir2.Initialize(parent_temp_dir, L"Three")); 100 EXPECT_TRUE(temp_dir2.Initialize(parent_temp_dir, L"Three"));
(...skipping 16 matching lines...) Expand all
117 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName())); 117 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName()));
118 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 118 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
119 EXPECT_TRUE(work_dir.Delete()); 119 EXPECT_TRUE(work_dir.Delete());
120 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 120 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
121 } 121 }
122 122
123 // Test that all intermediate dirs are cleaned up if they're empty when the 123 // Test that all intermediate dirs are cleaned up if they're empty when the
124 // destructor is called. 124 // destructor is called.
125 TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDestroy) { 125 TEST_F(SelfCleaningTempDirTest, RemoveUnusedOnDestroy) {
126 // Make a directory in which we'll work. 126 // Make a directory in which we'll work.
127 ScopedTempDir work_dir; 127 base::ScopedTempDir work_dir;
128 EXPECT_TRUE(work_dir.CreateUniqueTempDir()); 128 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
129 129
130 // Make up some path under the temp dir. 130 // Make up some path under the temp dir.
131 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two")); 131 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
132 { 132 {
133 SelfCleaningTempDir temp_dir; 133 SelfCleaningTempDir temp_dir;
134 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three")); 134 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
135 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path()); 135 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
136 EXPECT_TRUE(file_util::DirectoryExists(temp_dir.path())); 136 EXPECT_TRUE(file_util::DirectoryExists(temp_dir.path()));
137 } 137 }
138 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.Append(L"Three"))); 138 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.Append(L"Three")));
139 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir)); 139 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir));
140 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName())); 140 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName()));
141 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 141 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
142 EXPECT_TRUE(work_dir.Delete()); 142 EXPECT_TRUE(work_dir.Delete());
143 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 143 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
144 } 144 }
145 145
146 // Test that intermediate dirs are left behind if they're not empty when the 146 // Test that intermediate dirs are left behind if they're not empty when the
147 // destructor is called. 147 // destructor is called.
148 TEST_F(SelfCleaningTempDirTest, LeaveUsedOnDestroy) { 148 TEST_F(SelfCleaningTempDirTest, LeaveUsedOnDestroy) {
149 static const char kHiHon[] = "hi, hon"; 149 static const char kHiHon[] = "hi, hon";
150 150
151 // Make a directory in which we'll work. 151 // Make a directory in which we'll work.
152 ScopedTempDir work_dir; 152 base::ScopedTempDir work_dir;
153 EXPECT_TRUE(work_dir.CreateUniqueTempDir()); 153 EXPECT_TRUE(work_dir.CreateUniqueTempDir());
154 154
155 // Make up some path under the temp dir. 155 // Make up some path under the temp dir.
156 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two")); 156 FilePath parent_temp_dir(work_dir.path().Append(L"One").Append(L"Two"));
157 { 157 {
158 SelfCleaningTempDir temp_dir; 158 SelfCleaningTempDir temp_dir;
159 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three")); 159 EXPECT_TRUE(temp_dir.Initialize(parent_temp_dir, L"Three"));
160 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path()); 160 EXPECT_EQ(parent_temp_dir.Append(L"Three"), temp_dir.path());
161 EXPECT_TRUE(file_util::DirectoryExists(temp_dir.path())); 161 EXPECT_TRUE(file_util::DirectoryExists(temp_dir.path()));
162 // Drop a file somewhere. 162 // Drop a file somewhere.
163 EXPECT_EQ(arraysize(kHiHon) - 1, 163 EXPECT_EQ(arraysize(kHiHon) - 1,
164 file_util::WriteFile(parent_temp_dir.Append(GetRandomFilename()), 164 file_util::WriteFile(parent_temp_dir.Append(GetRandomFilename()),
165 kHiHon, arraysize(kHiHon) - 1)); 165 kHiHon, arraysize(kHiHon) - 1));
166 } 166 }
167 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.Append(L"Three"))); 167 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.Append(L"Three")));
168 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir)); 168 EXPECT_TRUE(file_util::DirectoryExists(parent_temp_dir));
169 EXPECT_TRUE(work_dir.Delete()); 169 EXPECT_TRUE(work_dir.Delete());
170 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName())); 170 EXPECT_FALSE(file_util::DirectoryExists(parent_temp_dir.DirName().DirName()));
171 } 171 }
172 172
173 } // namespace installer 173 } // namespace installer
OLDNEW
« no previous file with comments | « chrome/installer/util/self_cleaning_temp_dir.cc ('k') | chrome/installer/util/shell_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698