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

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

Issue 2321573002: //chrome misc: Change ScopedTempDir::path() to GetPath() (Closed)
Patch Set: Fix Win compilation Created 4 years, 3 months 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
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 "chrome/installer/util/move_tree_work_item.h" 5 #include "chrome/installer/util/move_tree_work_item.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include <fstream> 9 #include <fstream>
10 #include <memory> 10 #include <memory>
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 } 53 }
54 54
55 const wchar_t kTextContent1[] = L"Gooooooooooooooooooooogle"; 55 const wchar_t kTextContent1[] = L"Gooooooooooooooooooooogle";
56 const wchar_t kTextContent2[] = L"Overwrite Me"; 56 const wchar_t kTextContent2[] = L"Overwrite Me";
57 }; // namespace 57 }; // namespace
58 58
59 // Move one directory from source to destination when destination does not 59 // Move one directory from source to destination when destination does not
60 // exist. 60 // exist.
61 TEST_F(MoveTreeWorkItemTest, MoveDirectory) { 61 TEST_F(MoveTreeWorkItemTest, MoveDirectory) {
62 // Create two level deep source dir 62 // Create two level deep source dir
63 base::FilePath from_dir1(temp_from_dir_.path()); 63 base::FilePath from_dir1(temp_from_dir_.GetPath());
64 from_dir1 = from_dir1.AppendASCII("From_Dir1"); 64 from_dir1 = from_dir1.AppendASCII("From_Dir1");
65 base::CreateDirectory(from_dir1); 65 base::CreateDirectory(from_dir1);
66 ASSERT_TRUE(base::PathExists(from_dir1)); 66 ASSERT_TRUE(base::PathExists(from_dir1));
67 67
68 base::FilePath from_dir2(from_dir1); 68 base::FilePath from_dir2(from_dir1);
69 from_dir2 = from_dir2.AppendASCII("From_Dir2"); 69 from_dir2 = from_dir2.AppendASCII("From_Dir2");
70 base::CreateDirectory(from_dir2); 70 base::CreateDirectory(from_dir2);
71 ASSERT_TRUE(base::PathExists(from_dir2)); 71 ASSERT_TRUE(base::PathExists(from_dir2));
72 72
73 base::FilePath from_file(from_dir2); 73 base::FilePath from_file(from_dir2);
74 from_file = from_file.AppendASCII("From_File"); 74 from_file = from_file.AppendASCII("From_File");
75 CreateTextFile(from_file.value(), kTextContent1); 75 CreateTextFile(from_file.value(), kTextContent1);
76 ASSERT_TRUE(base::PathExists(from_file)); 76 ASSERT_TRUE(base::PathExists(from_file));
77 77
78 // Generate destination path 78 // Generate destination path
79 base::FilePath to_dir(temp_from_dir_.path()); 79 base::FilePath to_dir(temp_from_dir_.GetPath());
80 to_dir = to_dir.AppendASCII("To_Dir"); 80 to_dir = to_dir.AppendASCII("To_Dir");
81 ASSERT_FALSE(base::PathExists(to_dir)); 81 ASSERT_FALSE(base::PathExists(to_dir));
82 82
83 base::FilePath to_file(to_dir); 83 base::FilePath to_file(to_dir);
84 to_file = to_file.AppendASCII("From_Dir2"); 84 to_file = to_file.AppendASCII("From_Dir2");
85 to_file = to_file.AppendASCII("From_File"); 85 to_file = to_file.AppendASCII("From_File");
86 ASSERT_FALSE(base::PathExists(to_file)); 86 ASSERT_FALSE(base::PathExists(to_file));
87 87
88 // test Do() 88 // test Do()
89 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 89 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
90 from_dir1, to_dir, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 90 from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
91 EXPECT_TRUE(work_item->Do()); 91 EXPECT_TRUE(work_item->Do());
92 92
93 EXPECT_FALSE(base::PathExists(from_dir1)); 93 EXPECT_FALSE(base::PathExists(from_dir1));
94 EXPECT_TRUE(base::PathExists(to_dir)); 94 EXPECT_TRUE(base::PathExists(to_dir));
95 EXPECT_TRUE(base::PathExists(to_file)); 95 EXPECT_TRUE(base::PathExists(to_file));
96 96
97 // test rollback() 97 // test rollback()
98 work_item->Rollback(); 98 work_item->Rollback();
99 99
100 EXPECT_TRUE(base::PathExists(from_dir1)); 100 EXPECT_TRUE(base::PathExists(from_dir1));
101 EXPECT_TRUE(base::PathExists(from_file)); 101 EXPECT_TRUE(base::PathExists(from_file));
102 EXPECT_FALSE(base::PathExists(to_dir)); 102 EXPECT_FALSE(base::PathExists(to_dir));
103 } 103 }
104 104
105 // Move one directory from source to destination when destination already 105 // Move one directory from source to destination when destination already
106 // exists. 106 // exists.
107 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) { 107 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) {
108 // Create two level deep source dir 108 // Create two level deep source dir
109 base::FilePath from_dir1(temp_from_dir_.path()); 109 base::FilePath from_dir1(temp_from_dir_.GetPath());
110 from_dir1 = from_dir1.AppendASCII("From_Dir1"); 110 from_dir1 = from_dir1.AppendASCII("From_Dir1");
111 base::CreateDirectory(from_dir1); 111 base::CreateDirectory(from_dir1);
112 ASSERT_TRUE(base::PathExists(from_dir1)); 112 ASSERT_TRUE(base::PathExists(from_dir1));
113 113
114 base::FilePath from_dir2(from_dir1); 114 base::FilePath from_dir2(from_dir1);
115 from_dir2 = from_dir2.AppendASCII("From_Dir2"); 115 from_dir2 = from_dir2.AppendASCII("From_Dir2");
116 base::CreateDirectory(from_dir2); 116 base::CreateDirectory(from_dir2);
117 ASSERT_TRUE(base::PathExists(from_dir2)); 117 ASSERT_TRUE(base::PathExists(from_dir2));
118 118
119 base::FilePath from_file(from_dir2); 119 base::FilePath from_file(from_dir2);
120 from_file = from_file.AppendASCII("From_File"); 120 from_file = from_file.AppendASCII("From_File");
121 CreateTextFile(from_file.value(), kTextContent1); 121 CreateTextFile(from_file.value(), kTextContent1);
122 ASSERT_TRUE(base::PathExists(from_file)); 122 ASSERT_TRUE(base::PathExists(from_file));
123 123
124 // Create destination path 124 // Create destination path
125 base::FilePath to_dir(temp_from_dir_.path()); 125 base::FilePath to_dir(temp_from_dir_.GetPath());
126 to_dir = to_dir.AppendASCII("To_Dir"); 126 to_dir = to_dir.AppendASCII("To_Dir");
127 base::CreateDirectory(to_dir); 127 base::CreateDirectory(to_dir);
128 ASSERT_TRUE(base::PathExists(to_dir)); 128 ASSERT_TRUE(base::PathExists(to_dir));
129 129
130 base::FilePath orig_to_file(to_dir); 130 base::FilePath orig_to_file(to_dir);
131 orig_to_file = orig_to_file.AppendASCII("To_File"); 131 orig_to_file = orig_to_file.AppendASCII("To_File");
132 CreateTextFile(orig_to_file.value(), kTextContent2); 132 CreateTextFile(orig_to_file.value(), kTextContent2);
133 ASSERT_TRUE(base::PathExists(orig_to_file)); 133 ASSERT_TRUE(base::PathExists(orig_to_file));
134 134
135 base::FilePath new_to_file(to_dir); 135 base::FilePath new_to_file(to_dir);
136 new_to_file = new_to_file.AppendASCII("From_Dir2"); 136 new_to_file = new_to_file.AppendASCII("From_Dir2");
137 new_to_file = new_to_file.AppendASCII("From_File"); 137 new_to_file = new_to_file.AppendASCII("From_File");
138 ASSERT_FALSE(base::PathExists(new_to_file)); 138 ASSERT_FALSE(base::PathExists(new_to_file));
139 139
140 // test Do(), don't check for duplicates. 140 // test Do(), don't check for duplicates.
141 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 141 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
142 from_dir1, to_dir, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 142 from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
143 EXPECT_TRUE(work_item->Do()); 143 EXPECT_TRUE(work_item->Do());
144 144
145 EXPECT_FALSE(base::PathExists(from_dir1)); 145 EXPECT_FALSE(base::PathExists(from_dir1));
146 EXPECT_TRUE(base::PathExists(to_dir)); 146 EXPECT_TRUE(base::PathExists(to_dir));
147 EXPECT_TRUE(base::PathExists(new_to_file)); 147 EXPECT_TRUE(base::PathExists(new_to_file));
148 EXPECT_FALSE(base::PathExists(orig_to_file)); 148 EXPECT_FALSE(base::PathExists(orig_to_file));
149 149
150 // test rollback() 150 // test rollback()
151 work_item->Rollback(); 151 work_item->Rollback();
152 152
153 EXPECT_TRUE(base::PathExists(from_dir1)); 153 EXPECT_TRUE(base::PathExists(from_dir1));
154 EXPECT_TRUE(base::PathExists(to_dir)); 154 EXPECT_TRUE(base::PathExists(to_dir));
155 EXPECT_FALSE(base::PathExists(new_to_file)); 155 EXPECT_FALSE(base::PathExists(new_to_file));
156 EXPECT_TRUE(base::PathExists(orig_to_file)); 156 EXPECT_TRUE(base::PathExists(orig_to_file));
157 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent2)); 157 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent2));
158 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 158 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
159 } 159 }
160 160
161 // Move one file from source to destination when destination does not 161 // Move one file from source to destination when destination does not
162 // exist. 162 // exist.
163 TEST_F(MoveTreeWorkItemTest, MoveAFile) { 163 TEST_F(MoveTreeWorkItemTest, MoveAFile) {
164 // Create a file inside source dir 164 // Create a file inside source dir
165 base::FilePath from_dir(temp_from_dir_.path()); 165 base::FilePath from_dir(temp_from_dir_.GetPath());
166 from_dir = from_dir.AppendASCII("From_Dir"); 166 from_dir = from_dir.AppendASCII("From_Dir");
167 base::CreateDirectory(from_dir); 167 base::CreateDirectory(from_dir);
168 ASSERT_TRUE(base::PathExists(from_dir)); 168 ASSERT_TRUE(base::PathExists(from_dir));
169 169
170 base::FilePath from_file(from_dir); 170 base::FilePath from_file(from_dir);
171 from_file = from_file.AppendASCII("From_File"); 171 from_file = from_file.AppendASCII("From_File");
172 CreateTextFile(from_file.value(), kTextContent1); 172 CreateTextFile(from_file.value(), kTextContent1);
173 ASSERT_TRUE(base::PathExists(from_file)); 173 ASSERT_TRUE(base::PathExists(from_file));
174 174
175 // Generate destination file name 175 // Generate destination file name
176 base::FilePath to_file(temp_from_dir_.path()); 176 base::FilePath to_file(temp_from_dir_.GetPath());
177 to_file = to_file.AppendASCII("To_File"); 177 to_file = to_file.AppendASCII("To_File");
178 ASSERT_FALSE(base::PathExists(to_file)); 178 ASSERT_FALSE(base::PathExists(to_file));
179 179
180 // test Do() 180 // test Do()
181 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 181 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
182 from_file, to_file, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 182 from_file, to_file, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
183 EXPECT_TRUE(work_item->Do()); 183 EXPECT_TRUE(work_item->Do());
184 184
185 EXPECT_TRUE(base::PathExists(from_dir)); 185 EXPECT_TRUE(base::PathExists(from_dir));
186 EXPECT_FALSE(base::PathExists(from_file)); 186 EXPECT_FALSE(base::PathExists(from_file));
187 EXPECT_TRUE(base::PathExists(to_file)); 187 EXPECT_TRUE(base::PathExists(to_file));
188 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1)); 188 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
189 189
190 // test rollback() 190 // test rollback()
191 work_item->Rollback(); 191 work_item->Rollback();
192 192
193 EXPECT_TRUE(base::PathExists(from_dir)); 193 EXPECT_TRUE(base::PathExists(from_dir));
194 EXPECT_TRUE(base::PathExists(from_file)); 194 EXPECT_TRUE(base::PathExists(from_file));
195 EXPECT_FALSE(base::PathExists(to_file)); 195 EXPECT_FALSE(base::PathExists(to_file));
196 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 196 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
197 } 197 }
198 198
199 // Move one file from source to destination when destination already 199 // Move one file from source to destination when destination already
200 // exists. 200 // exists.
201 TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) { 201 TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) {
202 // Create a file inside source dir 202 // Create a file inside source dir
203 base::FilePath from_dir(temp_from_dir_.path()); 203 base::FilePath from_dir(temp_from_dir_.GetPath());
204 from_dir = from_dir.AppendASCII("From_Dir"); 204 from_dir = from_dir.AppendASCII("From_Dir");
205 base::CreateDirectory(from_dir); 205 base::CreateDirectory(from_dir);
206 ASSERT_TRUE(base::PathExists(from_dir)); 206 ASSERT_TRUE(base::PathExists(from_dir));
207 207
208 base::FilePath from_file(from_dir); 208 base::FilePath from_file(from_dir);
209 from_file = from_file.AppendASCII("From_File"); 209 from_file = from_file.AppendASCII("From_File");
210 CreateTextFile(from_file.value(), kTextContent1); 210 CreateTextFile(from_file.value(), kTextContent1);
211 ASSERT_TRUE(base::PathExists(from_file)); 211 ASSERT_TRUE(base::PathExists(from_file));
212 212
213 // Create destination path 213 // Create destination path
214 base::FilePath to_dir(temp_from_dir_.path()); 214 base::FilePath to_dir(temp_from_dir_.GetPath());
215 to_dir = to_dir.AppendASCII("To_Dir"); 215 to_dir = to_dir.AppendASCII("To_Dir");
216 base::CreateDirectory(to_dir); 216 base::CreateDirectory(to_dir);
217 ASSERT_TRUE(base::PathExists(to_dir)); 217 ASSERT_TRUE(base::PathExists(to_dir));
218 218
219 base::FilePath to_file(to_dir); 219 base::FilePath to_file(to_dir);
220 to_file = to_file.AppendASCII("To_File"); 220 to_file = to_file.AppendASCII("To_File");
221 CreateTextFile(to_file.value(), kTextContent2); 221 CreateTextFile(to_file.value(), kTextContent2);
222 ASSERT_TRUE(base::PathExists(to_file)); 222 ASSERT_TRUE(base::PathExists(to_file));
223 223
224 // test Do() 224 // test Do()
225 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 225 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
226 from_file, to_dir, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 226 from_file, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
227 EXPECT_TRUE(work_item->Do()); 227 EXPECT_TRUE(work_item->Do());
228 228
229 EXPECT_TRUE(base::PathExists(from_dir)); 229 EXPECT_TRUE(base::PathExists(from_dir));
230 EXPECT_FALSE(base::PathExists(from_file)); 230 EXPECT_FALSE(base::PathExists(from_file));
231 EXPECT_TRUE(base::PathExists(to_dir)); 231 EXPECT_TRUE(base::PathExists(to_dir));
232 EXPECT_FALSE(base::PathExists(to_file)); 232 EXPECT_FALSE(base::PathExists(to_file));
233 EXPECT_EQ(0, ReadTextFile(to_dir).compare(kTextContent1)); 233 EXPECT_EQ(0, ReadTextFile(to_dir).compare(kTextContent1));
234 234
235 // test rollback() 235 // test rollback()
236 work_item->Rollback(); 236 work_item->Rollback();
237 237
238 EXPECT_TRUE(base::PathExists(from_dir)); 238 EXPECT_TRUE(base::PathExists(from_dir));
239 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 239 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
240 EXPECT_TRUE(base::PathExists(to_dir)); 240 EXPECT_TRUE(base::PathExists(to_dir));
241 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent2)); 241 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent2));
242 } 242 }
243 243
244 // Move one file from source to destination when destination already 244 // Move one file from source to destination when destination already
245 // exists and is in use. 245 // exists and is in use.
246 TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) { 246 TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) {
247 // Create a file inside source dir 247 // Create a file inside source dir
248 base::FilePath from_dir(temp_from_dir_.path()); 248 base::FilePath from_dir(temp_from_dir_.GetPath());
249 from_dir = from_dir.AppendASCII("From_Dir"); 249 from_dir = from_dir.AppendASCII("From_Dir");
250 base::CreateDirectory(from_dir); 250 base::CreateDirectory(from_dir);
251 ASSERT_TRUE(base::PathExists(from_dir)); 251 ASSERT_TRUE(base::PathExists(from_dir));
252 252
253 base::FilePath from_file(from_dir); 253 base::FilePath from_file(from_dir);
254 from_file = from_file.AppendASCII("From_File"); 254 from_file = from_file.AppendASCII("From_File");
255 CreateTextFile(from_file.value(), kTextContent1); 255 CreateTextFile(from_file.value(), kTextContent1);
256 ASSERT_TRUE(base::PathExists(from_file)); 256 ASSERT_TRUE(base::PathExists(from_file));
257 257
258 // Create an executable in destination path by copying ourself to it. 258 // Create an executable in destination path by copying ourself to it.
259 base::FilePath to_dir(temp_from_dir_.path()); 259 base::FilePath to_dir(temp_from_dir_.GetPath());
260 to_dir = to_dir.AppendASCII("To_Dir"); 260 to_dir = to_dir.AppendASCII("To_Dir");
261 base::CreateDirectory(to_dir); 261 base::CreateDirectory(to_dir);
262 ASSERT_TRUE(base::PathExists(to_dir)); 262 ASSERT_TRUE(base::PathExists(to_dir));
263 263
264 wchar_t exe_full_path_str[MAX_PATH]; 264 wchar_t exe_full_path_str[MAX_PATH];
265 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); 265 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH);
266 base::FilePath exe_full_path(exe_full_path_str); 266 base::FilePath exe_full_path(exe_full_path_str);
267 base::FilePath to_file(to_dir); 267 base::FilePath to_file(to_dir);
268 to_file = to_file.AppendASCII("To_File"); 268 to_file = to_file.AppendASCII("To_File");
269 base::CopyFile(exe_full_path, to_file); 269 base::CopyFile(exe_full_path, to_file);
270 ASSERT_TRUE(base::PathExists(to_file)); 270 ASSERT_TRUE(base::PathExists(to_file));
271 271
272 // Run the executable in destination path 272 // Run the executable in destination path
273 STARTUPINFOW si = {sizeof(si)}; 273 STARTUPINFOW si = {sizeof(si)};
274 PROCESS_INFORMATION pi = {0}; 274 PROCESS_INFORMATION pi = {0};
275 ASSERT_TRUE(::CreateProcess(NULL, 275 ASSERT_TRUE(::CreateProcess(NULL,
276 const_cast<wchar_t*>(to_file.value().c_str()), 276 const_cast<wchar_t*>(to_file.value().c_str()),
277 NULL, NULL, FALSE, 277 NULL, NULL, FALSE,
278 CREATE_NO_WINDOW | CREATE_SUSPENDED, 278 CREATE_NO_WINDOW | CREATE_SUSPENDED,
279 NULL, NULL, &si, &pi)); 279 NULL, NULL, &si, &pi));
280 280
281 // test Do() 281 // test Do()
282 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 282 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
283 from_file, to_file, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 283 from_file, to_file, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
284 EXPECT_TRUE(work_item->Do()); 284 EXPECT_TRUE(work_item->Do());
285 285
286 EXPECT_TRUE(base::PathExists(from_dir)); 286 EXPECT_TRUE(base::PathExists(from_dir));
287 EXPECT_FALSE(base::PathExists(from_file)); 287 EXPECT_FALSE(base::PathExists(from_file));
288 EXPECT_TRUE(base::PathExists(to_dir)); 288 EXPECT_TRUE(base::PathExists(to_dir));
289 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1)); 289 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
290 290
291 // test rollback() 291 // test rollback()
292 work_item->Rollback(); 292 work_item->Rollback();
293 293
294 EXPECT_TRUE(base::PathExists(from_dir)); 294 EXPECT_TRUE(base::PathExists(from_dir));
295 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 295 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
296 EXPECT_TRUE(base::PathExists(to_dir)); 296 EXPECT_TRUE(base::PathExists(to_dir));
297 EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file)); 297 EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file));
298 298
299 TerminateProcess(pi.hProcess, 0); 299 TerminateProcess(pi.hProcess, 0);
300 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); 300 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
301 CloseHandle(pi.hProcess); 301 CloseHandle(pi.hProcess);
302 CloseHandle(pi.hThread); 302 CloseHandle(pi.hThread);
303 } 303 }
304 304
305 // Move one file that is in use to destination. 305 // Move one file that is in use to destination.
306 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) { 306 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) {
307 // Create an executable for source by copying ourself to a new source dir. 307 // Create an executable for source by copying ourself to a new source dir.
308 base::FilePath from_dir(temp_from_dir_.path()); 308 base::FilePath from_dir(temp_from_dir_.GetPath());
309 from_dir = from_dir.AppendASCII("From_Dir"); 309 from_dir = from_dir.AppendASCII("From_Dir");
310 base::CreateDirectory(from_dir); 310 base::CreateDirectory(from_dir);
311 ASSERT_TRUE(base::PathExists(from_dir)); 311 ASSERT_TRUE(base::PathExists(from_dir));
312 312
313 wchar_t exe_full_path_str[MAX_PATH]; 313 wchar_t exe_full_path_str[MAX_PATH];
314 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); 314 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH);
315 base::FilePath exe_full_path(exe_full_path_str); 315 base::FilePath exe_full_path(exe_full_path_str);
316 base::FilePath from_file(from_dir); 316 base::FilePath from_file(from_dir);
317 from_file = from_file.AppendASCII("From_File"); 317 from_file = from_file.AppendASCII("From_File");
318 base::CopyFile(exe_full_path, from_file); 318 base::CopyFile(exe_full_path, from_file);
319 ASSERT_TRUE(base::PathExists(from_file)); 319 ASSERT_TRUE(base::PathExists(from_file));
320 320
321 // Create a destination source dir and generate destination file name. 321 // Create a destination source dir and generate destination file name.
322 base::FilePath to_dir(temp_from_dir_.path()); 322 base::FilePath to_dir(temp_from_dir_.GetPath());
323 to_dir = to_dir.AppendASCII("To_Dir"); 323 to_dir = to_dir.AppendASCII("To_Dir");
324 base::CreateDirectory(to_dir); 324 base::CreateDirectory(to_dir);
325 ASSERT_TRUE(base::PathExists(to_dir)); 325 ASSERT_TRUE(base::PathExists(to_dir));
326 326
327 base::FilePath to_file(to_dir); 327 base::FilePath to_file(to_dir);
328 to_file = to_file.AppendASCII("To_File"); 328 to_file = to_file.AppendASCII("To_File");
329 CreateTextFile(to_file.value(), kTextContent1); 329 CreateTextFile(to_file.value(), kTextContent1);
330 ASSERT_TRUE(base::PathExists(to_file)); 330 ASSERT_TRUE(base::PathExists(to_file));
331 331
332 // Run the executable in source path 332 // Run the executable in source path
333 STARTUPINFOW si = {sizeof(si)}; 333 STARTUPINFOW si = {sizeof(si)};
334 PROCESS_INFORMATION pi = {0}; 334 PROCESS_INFORMATION pi = {0};
335 ASSERT_TRUE(::CreateProcess(NULL, 335 ASSERT_TRUE(::CreateProcess(NULL,
336 const_cast<wchar_t*>(from_file.value().c_str()), 336 const_cast<wchar_t*>(from_file.value().c_str()),
337 NULL, NULL, FALSE, 337 NULL, NULL, FALSE,
338 CREATE_NO_WINDOW | CREATE_SUSPENDED, 338 CREATE_NO_WINDOW | CREATE_SUSPENDED,
339 NULL, NULL, &si, &pi)); 339 NULL, NULL, &si, &pi));
340 340
341 // test Do() 341 // test Do()
342 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 342 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
343 from_file, to_file, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 343 from_file, to_file, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
344 EXPECT_TRUE(work_item->Do()); 344 EXPECT_TRUE(work_item->Do());
345 345
346 EXPECT_TRUE(base::PathExists(from_dir)); 346 EXPECT_TRUE(base::PathExists(from_dir));
347 EXPECT_FALSE(base::PathExists(from_file)); 347 EXPECT_FALSE(base::PathExists(from_file));
348 EXPECT_TRUE(base::PathExists(to_dir)); 348 EXPECT_TRUE(base::PathExists(to_dir));
349 EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file)); 349 EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file));
350 350
351 // Close the process and make sure all the conditions after Do() are 351 // Close the process and make sure all the conditions after Do() are
352 // still true. 352 // still true.
353 TerminateProcess(pi.hProcess, 0); 353 TerminateProcess(pi.hProcess, 0);
(...skipping 12 matching lines...) Expand all
366 EXPECT_TRUE(base::PathExists(from_dir)); 366 EXPECT_TRUE(base::PathExists(from_dir));
367 EXPECT_TRUE(base::ContentsEqual(exe_full_path, from_file)); 367 EXPECT_TRUE(base::ContentsEqual(exe_full_path, from_file));
368 EXPECT_TRUE(base::PathExists(to_dir)); 368 EXPECT_TRUE(base::PathExists(to_dir));
369 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1)); 369 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
370 } 370 }
371 371
372 // Move one directory from source to destination when destination already 372 // Move one directory from source to destination when destination already
373 // exists. 373 // exists.
374 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExistsCheckForDuplicatesFull) { 374 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExistsCheckForDuplicatesFull) {
375 // Create two level deep source dir 375 // Create two level deep source dir
376 base::FilePath from_dir1(temp_from_dir_.path()); 376 base::FilePath from_dir1(temp_from_dir_.GetPath());
377 from_dir1 = from_dir1.AppendASCII("From_Dir1"); 377 from_dir1 = from_dir1.AppendASCII("From_Dir1");
378 base::CreateDirectory(from_dir1); 378 base::CreateDirectory(from_dir1);
379 ASSERT_TRUE(base::PathExists(from_dir1)); 379 ASSERT_TRUE(base::PathExists(from_dir1));
380 380
381 base::FilePath from_dir2(from_dir1); 381 base::FilePath from_dir2(from_dir1);
382 from_dir2 = from_dir2.AppendASCII("From_Dir2"); 382 from_dir2 = from_dir2.AppendASCII("From_Dir2");
383 base::CreateDirectory(from_dir2); 383 base::CreateDirectory(from_dir2);
384 ASSERT_TRUE(base::PathExists(from_dir2)); 384 ASSERT_TRUE(base::PathExists(from_dir2));
385 385
386 base::FilePath from_file(from_dir2); 386 base::FilePath from_file(from_dir2);
387 from_file = from_file.AppendASCII("From_File"); 387 from_file = from_file.AppendASCII("From_File");
388 CreateTextFile(from_file.value(), kTextContent1); 388 CreateTextFile(from_file.value(), kTextContent1);
389 ASSERT_TRUE(base::PathExists(from_file)); 389 ASSERT_TRUE(base::PathExists(from_file));
390 390
391 // // Create a file hierarchy identical to the one in the source directory. 391 // // Create a file hierarchy identical to the one in the source directory.
392 base::FilePath to_dir(temp_from_dir_.path()); 392 base::FilePath to_dir(temp_from_dir_.GetPath());
393 to_dir = to_dir.AppendASCII("To_Dir"); 393 to_dir = to_dir.AppendASCII("To_Dir");
394 ASSERT_TRUE(installer::test::CopyFileHierarchy(from_dir1, to_dir)); 394 ASSERT_TRUE(installer::test::CopyFileHierarchy(from_dir1, to_dir));
395 395
396 // Lock one of the files in the to destination directory to prevent moves. 396 // Lock one of the files in the to destination directory to prevent moves.
397 base::FilePath orig_to_file( 397 base::FilePath orig_to_file(
398 to_dir.AppendASCII("From_Dir2").AppendASCII("From_File")); 398 to_dir.AppendASCII("From_Dir2").AppendASCII("From_File"));
399 base::MemoryMappedFile mapped_file; 399 base::MemoryMappedFile mapped_file;
400 EXPECT_TRUE(mapped_file.Initialize(orig_to_file)); 400 EXPECT_TRUE(mapped_file.Initialize(orig_to_file));
401 401
402 // First check that we can't do the regular Move(). 402 // First check that we can't do the regular Move().
403 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 403 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
404 from_dir1, to_dir, temp_to_dir_.path(), WorkItem::ALWAYS_MOVE)); 404 from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
405 EXPECT_FALSE(work_item->Do()); 405 EXPECT_FALSE(work_item->Do());
406 work_item->Rollback(); 406 work_item->Rollback();
407 407
408 // Now test Do() with the check for duplicates. This should pass. 408 // Now test Do() with the check for duplicates. This should pass.
409 work_item.reset( 409 work_item.reset(WorkItem::CreateMoveTreeWorkItem(
410 WorkItem::CreateMoveTreeWorkItem(from_dir1, 410 from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::CHECK_DUPLICATES));
411 to_dir,
412 temp_to_dir_.path(),
413 WorkItem::CHECK_DUPLICATES));
414 EXPECT_TRUE(work_item->Do()); 411 EXPECT_TRUE(work_item->Do());
415 412
416 // Make sure that we "moved" the files, i.e. that the source directory isn't 413 // Make sure that we "moved" the files, i.e. that the source directory isn't
417 // there anymore, 414 // there anymore,
418 EXPECT_FALSE(base::PathExists(from_dir1)); 415 EXPECT_FALSE(base::PathExists(from_dir1));
419 // Make sure that the original directory structure and file are still present. 416 // Make sure that the original directory structure and file are still present.
420 EXPECT_TRUE(base::PathExists(to_dir)); 417 EXPECT_TRUE(base::PathExists(to_dir));
421 EXPECT_TRUE(base::PathExists(orig_to_file)); 418 EXPECT_TRUE(base::PathExists(orig_to_file));
422 // Make sure that the backup path is not empty. 419 // Make sure that the backup path is not empty.
423 EXPECT_FALSE(base::IsDirectoryEmpty(temp_to_dir_.path())); 420 EXPECT_FALSE(base::IsDirectoryEmpty(temp_to_dir_.GetPath()));
424 421
425 // Check that the work item believes the source to have been moved. 422 // Check that the work item believes the source to have been moved.
426 EXPECT_TRUE(work_item->source_moved_to_backup_); 423 EXPECT_TRUE(work_item->source_moved_to_backup_);
427 EXPECT_FALSE(work_item->moved_to_dest_path_); 424 EXPECT_FALSE(work_item->moved_to_dest_path_);
428 EXPECT_FALSE(work_item->moved_to_backup_); 425 EXPECT_FALSE(work_item->moved_to_backup_);
429 426
430 // test rollback() 427 // test rollback()
431 work_item->Rollback(); 428 work_item->Rollback();
432 429
433 // Once we rollback all the original files should still be there, as should 430 // Once we rollback all the original files should still be there, as should
434 // the source files. 431 // the source files.
435 EXPECT_TRUE(base::PathExists(from_dir1)); 432 EXPECT_TRUE(base::PathExists(from_dir1));
436 EXPECT_TRUE(base::PathExists(to_dir)); 433 EXPECT_TRUE(base::PathExists(to_dir));
437 EXPECT_TRUE(base::PathExists(orig_to_file)); 434 EXPECT_TRUE(base::PathExists(orig_to_file));
438 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1)); 435 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1));
439 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 436 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
440 } 437 }
441 438
442 // Move one directory from source to destination when destination already 439 // Move one directory from source to destination when destination already
443 // exists but contains only a subset of the files in source. 440 // exists but contains only a subset of the files in source.
444 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExistsCheckForDuplicatesPartial) { 441 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExistsCheckForDuplicatesPartial) {
445 // Create two level deep source dir 442 // Create two level deep source dir
446 base::FilePath from_dir1(temp_from_dir_.path()); 443 base::FilePath from_dir1(temp_from_dir_.GetPath());
447 from_dir1 = from_dir1.AppendASCII("From_Dir1"); 444 from_dir1 = from_dir1.AppendASCII("From_Dir1");
448 base::CreateDirectory(from_dir1); 445 base::CreateDirectory(from_dir1);
449 ASSERT_TRUE(base::PathExists(from_dir1)); 446 ASSERT_TRUE(base::PathExists(from_dir1));
450 447
451 base::FilePath from_dir2(from_dir1); 448 base::FilePath from_dir2(from_dir1);
452 from_dir2 = from_dir2.AppendASCII("From_Dir2"); 449 from_dir2 = from_dir2.AppendASCII("From_Dir2");
453 base::CreateDirectory(from_dir2); 450 base::CreateDirectory(from_dir2);
454 ASSERT_TRUE(base::PathExists(from_dir2)); 451 ASSERT_TRUE(base::PathExists(from_dir2));
455 452
456 base::FilePath from_file(from_dir2); 453 base::FilePath from_file(from_dir2);
457 from_file = from_file.AppendASCII("From_File"); 454 from_file = from_file.AppendASCII("From_File");
458 CreateTextFile(from_file.value(), kTextContent1); 455 CreateTextFile(from_file.value(), kTextContent1);
459 ASSERT_TRUE(base::PathExists(from_file)); 456 ASSERT_TRUE(base::PathExists(from_file));
460 457
461 base::FilePath from_file2(from_dir2); 458 base::FilePath from_file2(from_dir2);
462 from_file2 = from_file2.AppendASCII("From_File2"); 459 from_file2 = from_file2.AppendASCII("From_File2");
463 CreateTextFile(from_file2.value(), kTextContent2); 460 CreateTextFile(from_file2.value(), kTextContent2);
464 ASSERT_TRUE(base::PathExists(from_file2)); 461 ASSERT_TRUE(base::PathExists(from_file2));
465 462
466 // Create destination path 463 // Create destination path
467 base::FilePath to_dir(temp_from_dir_.path()); 464 base::FilePath to_dir(temp_from_dir_.GetPath());
468 to_dir = to_dir.AppendASCII("To_Dir"); 465 to_dir = to_dir.AppendASCII("To_Dir");
469 base::CreateDirectory(to_dir); 466 base::CreateDirectory(to_dir);
470 ASSERT_TRUE(base::PathExists(to_dir)); 467 ASSERT_TRUE(base::PathExists(to_dir));
471 468
472 // Create a sub-directory of the same name as in the source directory. 469 // Create a sub-directory of the same name as in the source directory.
473 base::FilePath to_dir2(to_dir); 470 base::FilePath to_dir2(to_dir);
474 to_dir2 = to_dir2.AppendASCII("From_Dir2"); 471 to_dir2 = to_dir2.AppendASCII("From_Dir2");
475 base::CreateDirectory(to_dir2); 472 base::CreateDirectory(to_dir2);
476 ASSERT_TRUE(base::PathExists(to_dir2)); 473 ASSERT_TRUE(base::PathExists(to_dir2));
477 474
478 // Create one of the files in the to sub-directory, but not the other. 475 // Create one of the files in the to sub-directory, but not the other.
479 base::FilePath orig_to_file(to_dir2); 476 base::FilePath orig_to_file(to_dir2);
480 orig_to_file = orig_to_file.AppendASCII("From_File"); 477 orig_to_file = orig_to_file.AppendASCII("From_File");
481 CreateTextFile(orig_to_file.value(), kTextContent1); 478 CreateTextFile(orig_to_file.value(), kTextContent1);
482 ASSERT_TRUE(base::PathExists(orig_to_file)); 479 ASSERT_TRUE(base::PathExists(orig_to_file));
483 480
484 // test Do(), check for duplicates. 481 // test Do(), check for duplicates.
485 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( 482 std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
486 from_dir1, to_dir, temp_to_dir_.path(), WorkItem::CHECK_DUPLICATES)); 483 from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::CHECK_DUPLICATES));
487 EXPECT_TRUE(work_item->Do()); 484 EXPECT_TRUE(work_item->Do());
488 485
489 // Make sure that we "moved" the files, i.e. that the source directory isn't 486 // Make sure that we "moved" the files, i.e. that the source directory isn't
490 // there anymore, 487 // there anymore,
491 EXPECT_FALSE(base::PathExists(from_dir1)); 488 EXPECT_FALSE(base::PathExists(from_dir1));
492 // Make sure that the original directory structure and file are still present. 489 // Make sure that the original directory structure and file are still present.
493 EXPECT_TRUE(base::PathExists(to_dir)); 490 EXPECT_TRUE(base::PathExists(to_dir));
494 EXPECT_TRUE(base::PathExists(orig_to_file)); 491 EXPECT_TRUE(base::PathExists(orig_to_file));
495 // Make sure that the backup path is not empty. 492 // Make sure that the backup path is not empty.
496 EXPECT_FALSE(base::IsDirectoryEmpty(temp_to_dir_.path())); 493 EXPECT_FALSE(base::IsDirectoryEmpty(temp_to_dir_.GetPath()));
497 // Make sure that the "new" file is also present. 494 // Make sure that the "new" file is also present.
498 base::FilePath new_to_file2(to_dir2); 495 base::FilePath new_to_file2(to_dir2);
499 new_to_file2 = new_to_file2.AppendASCII("From_File2"); 496 new_to_file2 = new_to_file2.AppendASCII("From_File2");
500 EXPECT_TRUE(base::PathExists(new_to_file2)); 497 EXPECT_TRUE(base::PathExists(new_to_file2));
501 498
502 // Check that the work item believes that this was a regular move. 499 // Check that the work item believes that this was a regular move.
503 EXPECT_FALSE(work_item->source_moved_to_backup_); 500 EXPECT_FALSE(work_item->source_moved_to_backup_);
504 EXPECT_TRUE(work_item->moved_to_dest_path_); 501 EXPECT_TRUE(work_item->moved_to_dest_path_);
505 EXPECT_TRUE(work_item->moved_to_backup_); 502 EXPECT_TRUE(work_item->moved_to_backup_);
506 503
507 // test rollback() 504 // test rollback()
508 work_item->Rollback(); 505 work_item->Rollback();
509 506
510 // Once we rollback all the original files should still be there, as should 507 // Once we rollback all the original files should still be there, as should
511 // the source files. 508 // the source files.
512 EXPECT_TRUE(base::PathExists(from_dir1)); 509 EXPECT_TRUE(base::PathExists(from_dir1));
513 EXPECT_TRUE(base::PathExists(to_dir)); 510 EXPECT_TRUE(base::PathExists(to_dir));
514 EXPECT_TRUE(base::PathExists(orig_to_file)); 511 EXPECT_TRUE(base::PathExists(orig_to_file));
515 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1)); 512 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1));
516 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 513 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
517 514
518 // Also, after rollback the new "to" file should be gone. 515 // Also, after rollback the new "to" file should be gone.
519 EXPECT_FALSE(base::PathExists(new_to_file2)); 516 EXPECT_FALSE(base::PathExists(new_to_file2));
520 } 517 }
OLDNEW
« no previous file with comments | « chrome/installer/util/lzma_util_unittest.cc ('k') | chrome/installer/util/self_cleaning_temp_dir_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698