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

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

Issue 6538025: Temp dir cleanup:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 6
7 #include <fstream> 7 #include <fstream>
8 8
9 #include "base/base_paths.h" 9 #include "base/base_paths.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 FilePath dir_name_to(test_dir_); 210 FilePath dir_name_to(test_dir_);
211 dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir"); 211 dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
212 file_util::CreateDirectory(dir_name_to); 212 file_util::CreateDirectory(dir_name_to);
213 ASSERT_TRUE(file_util::PathExists(dir_name_to)); 213 ASSERT_TRUE(file_util::PathExists(dir_name_to));
214 214
215 FilePath file_name_to(dir_name_to); 215 FilePath file_name_to(dir_name_to);
216 file_name_to = file_name_to.AppendASCII("File_To.txt"); 216 file_name_to = file_name_to.AppendASCII("File_To.txt");
217 CreateTextFile(file_name_to.value(), text_content_1); 217 CreateTextFile(file_name_to.value(), text_content_1);
218 ASSERT_TRUE(file_util::PathExists(file_name_to)); 218 ASSERT_TRUE(file_util::PathExists(file_name_to));
219 219
220 // Get the path of backup file
221 FilePath backup_file(temp_dir_);
222 backup_file = backup_file.AppendASCII("File_To.txt");
223
224 // test Do() with always_overwrite being true. 220 // test Do() with always_overwrite being true.
225 scoped_ptr<CopyTreeWorkItem> work_item( 221 scoped_ptr<CopyTreeWorkItem> work_item(
226 WorkItem::CreateCopyTreeWorkItem(file_name_from, 222 WorkItem::CreateCopyTreeWorkItem(file_name_from,
227 file_name_to, 223 file_name_to,
228 temp_dir_, 224 temp_dir_,
229 WorkItem::ALWAYS, 225 WorkItem::ALWAYS,
230 FilePath())); 226 FilePath()));
231 227
232 EXPECT_TRUE(work_item->Do()); 228 EXPECT_TRUE(work_item->Do());
233 229
230 // Get the path of backup file
231 FilePath backup_file(work_item->backup_path_.path());
232 EXPECT_FALSE(backup_file.empty());
233 backup_file = backup_file.AppendASCII("File_To.txt");
234
234 EXPECT_TRUE(file_util::PathExists(file_name_from)); 235 EXPECT_TRUE(file_util::PathExists(file_name_from));
235 EXPECT_TRUE(file_util::PathExists(file_name_to)); 236 EXPECT_TRUE(file_util::PathExists(file_name_to));
236 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 237 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
237 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); 238 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
238 // we verify the file is overwritten by checking the existence of backup 239 // we verify the file is overwritten by checking the existence of backup
239 // file. 240 // file.
240 EXPECT_TRUE(file_util::PathExists(backup_file)); 241 EXPECT_TRUE(file_util::PathExists(backup_file));
241 EXPECT_EQ(0, ReadTextFile(backup_file.value()).compare(text_content_1)); 242 EXPECT_EQ(0, ReadTextFile(backup_file.value()).compare(text_content_1));
242 243
243 // test rollback() 244 // test rollback()
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 FilePath dir_name_to(test_dir_); 291 FilePath dir_name_to(test_dir_);
291 dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir"); 292 dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
292 file_util::CreateDirectory(dir_name_to); 293 file_util::CreateDirectory(dir_name_to);
293 ASSERT_TRUE(file_util::PathExists(dir_name_to)); 294 ASSERT_TRUE(file_util::PathExists(dir_name_to));
294 295
295 FilePath file_name_to(dir_name_to); 296 FilePath file_name_to(dir_name_to);
296 file_name_to = file_name_to.AppendASCII("File_To.txt"); 297 file_name_to = file_name_to.AppendASCII("File_To.txt");
297 CreateTextFile(file_name_to.value(), text_content_2); 298 CreateTextFile(file_name_to.value(), text_content_2);
298 ASSERT_TRUE(file_util::PathExists(file_name_to)); 299 ASSERT_TRUE(file_util::PathExists(file_name_to));
299 300
300 // Get the path of backup file 301 FilePath backup_file;
301 FilePath backup_file(temp_dir_);
302 backup_file = backup_file.AppendASCII("File_To.txt");
303 302
304 { 303 {
305 // test Do(). 304 // test Do().
306 scoped_ptr<CopyTreeWorkItem> work_item( 305 scoped_ptr<CopyTreeWorkItem> work_item(
307 WorkItem::CreateCopyTreeWorkItem(file_name_from, 306 WorkItem::CreateCopyTreeWorkItem(file_name_from,
308 file_name_to, 307 file_name_to,
309 temp_dir_, 308 temp_dir_,
310 WorkItem::IF_DIFFERENT, 309 WorkItem::IF_DIFFERENT,
311 FilePath())); 310 FilePath()));
312 311
313 EXPECT_TRUE(work_item->Do()); 312 EXPECT_TRUE(work_item->Do());
314 313
314 // Get the path of backup file
315 backup_file = work_item->backup_path_.path();
316 EXPECT_FALSE(backup_file.empty());
317 backup_file = backup_file.AppendASCII("File_To.txt");
318
315 EXPECT_TRUE(file_util::PathExists(file_name_from)); 319 EXPECT_TRUE(file_util::PathExists(file_name_from));
316 EXPECT_TRUE(file_util::PathExists(file_name_to)); 320 EXPECT_TRUE(file_util::PathExists(file_name_to));
317 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 321 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
318 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); 322 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
319 // verify the file is moved to backup place. 323 // verify the file is moved to backup place.
320 EXPECT_TRUE(file_util::PathExists(backup_file)); 324 EXPECT_TRUE(file_util::PathExists(backup_file));
321 EXPECT_EQ(0, ReadTextFile(backup_file.value()).compare(text_content_2)); 325 EXPECT_EQ(0, ReadTextFile(backup_file.value()).compare(text_content_2));
322 } 326 }
323 327
324 // verify the backup file is cleaned up as well. 328 // verify the backup file is cleaned up as well.
(...skipping 29 matching lines...) Expand all
354 << " to " << file_name_to.value(); 358 << " to " << file_name_to.value();
355 359
356 // Run the executable in destination path 360 // Run the executable in destination path
357 STARTUPINFOW si = {sizeof(si)}; 361 STARTUPINFOW si = {sizeof(si)};
358 PROCESS_INFORMATION pi = {0}; 362 PROCESS_INFORMATION pi = {0};
359 ASSERT_TRUE( 363 ASSERT_TRUE(
360 ::CreateProcess(NULL, const_cast<wchar_t*>(file_name_to.value().c_str()), 364 ::CreateProcess(NULL, const_cast<wchar_t*>(file_name_to.value().c_str()),
361 NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, 365 NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED,
362 NULL, NULL, &si, &pi)); 366 NULL, NULL, &si, &pi));
363 367
364 // Get the path of backup file
365 FilePath backup_file(temp_dir_);
366 backup_file = backup_file.AppendASCII("File_To");
367
368 // test Do(). 368 // test Do().
369 scoped_ptr<CopyTreeWorkItem> work_item( 369 scoped_ptr<CopyTreeWorkItem> work_item(
370 WorkItem::CreateCopyTreeWorkItem(file_name_from, 370 WorkItem::CreateCopyTreeWorkItem(file_name_from,
371 file_name_to, 371 file_name_to,
372 temp_dir_, 372 temp_dir_,
373 WorkItem::IF_DIFFERENT, 373 WorkItem::IF_DIFFERENT,
374 FilePath())); 374 FilePath()));
375 375
376 EXPECT_TRUE(work_item->Do()); 376 EXPECT_TRUE(work_item->Do());
377 377
378 // Get the path of backup file
379 FilePath backup_file(work_item->backup_path_.path());
380 EXPECT_FALSE(backup_file.empty());
381 backup_file = backup_file.AppendASCII("File_To");
382
378 EXPECT_TRUE(file_util::PathExists(file_name_from)); 383 EXPECT_TRUE(file_util::PathExists(file_name_from));
379 EXPECT_TRUE(file_util::PathExists(file_name_to)); 384 EXPECT_TRUE(file_util::PathExists(file_name_to));
380 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 385 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
381 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); 386 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
382 // verify the file in used is moved to backup place. 387 // verify the file in used is moved to backup place.
383 EXPECT_TRUE(file_util::PathExists(backup_file)); 388 EXPECT_TRUE(file_util::PathExists(backup_file));
384 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file)); 389 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file));
385 390
386 // test rollback() 391 // test rollback()
387 work_item->Rollback(); 392 work_item->Rollback();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 << " to " << file_name_to.value(); 438 << " to " << file_name_to.value();
434 439
435 // Run the executable in destination path 440 // Run the executable in destination path
436 STARTUPINFOW si = {sizeof(si)}; 441 STARTUPINFOW si = {sizeof(si)};
437 PROCESS_INFORMATION pi = {0}; 442 PROCESS_INFORMATION pi = {0};
438 ASSERT_TRUE( 443 ASSERT_TRUE(
439 ::CreateProcess(NULL, const_cast<wchar_t*>(file_name_to.value().c_str()), 444 ::CreateProcess(NULL, const_cast<wchar_t*>(file_name_to.value().c_str()),
440 NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, 445 NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED,
441 NULL, NULL, &si, &pi)); 446 NULL, NULL, &si, &pi));
442 447
443 // Get the path of backup file
444 FilePath backup_file(temp_dir_);
445 backup_file = backup_file.AppendASCII("File_To");
446
447 // test Do(). 448 // test Do().
448 scoped_ptr<CopyTreeWorkItem> work_item( 449 scoped_ptr<CopyTreeWorkItem> work_item(
449 WorkItem::CreateCopyTreeWorkItem(file_name_from, 450 WorkItem::CreateCopyTreeWorkItem(file_name_from,
450 file_name_to, 451 file_name_to,
451 temp_dir_, 452 temp_dir_,
452 WorkItem::NEW_NAME_IF_IN_USE, 453 WorkItem::NEW_NAME_IF_IN_USE,
453 alternate_to)); 454 alternate_to));
454 455
455 EXPECT_TRUE(work_item->Do()); 456 EXPECT_TRUE(work_item->Do());
456 457
457 EXPECT_TRUE(file_util::PathExists(file_name_from)); 458 EXPECT_TRUE(file_util::PathExists(file_name_from));
458 EXPECT_TRUE(file_util::PathExists(file_name_to)); 459 EXPECT_TRUE(file_util::PathExists(file_name_to));
459 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 460 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
460 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); 461 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to));
461 // verify that the backup path does not exist 462 // verify that the backup path does not exist
462 EXPECT_FALSE(file_util::PathExists(backup_file)); 463 EXPECT_TRUE(work_item->backup_path_.path().empty());
463 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, alternate_to)); 464 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, alternate_to));
464 465
465 // test rollback() 466 // test rollback()
466 work_item->Rollback(); 467 work_item->Rollback();
467 468
468 EXPECT_TRUE(file_util::PathExists(file_name_from)); 469 EXPECT_TRUE(file_util::PathExists(file_name_from));
469 EXPECT_TRUE(file_util::PathExists(file_name_to)); 470 EXPECT_TRUE(file_util::PathExists(file_name_to));
470 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 471 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
471 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); 472 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to));
472 EXPECT_FALSE(file_util::PathExists(backup_file)); 473 EXPECT_TRUE(work_item->backup_path_.path().empty());
473 // the alternate file should be gone after rollback 474 // the alternate file should be gone after rollback
474 EXPECT_FALSE(file_util::PathExists(alternate_to)); 475 EXPECT_FALSE(file_util::PathExists(alternate_to));
475 476
476 TerminateProcess(pi.hProcess, 0); 477 TerminateProcess(pi.hProcess, 0);
477 // make sure the handle is closed. 478 // make sure the handle is closed.
478 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); 479 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
479 CloseHandle(pi.hProcess); 480 CloseHandle(pi.hProcess);
480 CloseHandle(pi.hThread); 481 CloseHandle(pi.hThread);
481 482
482 // Now the process has terminated, lets try overwriting the file again 483 // Now the process has terminated, lets try overwriting the file again
483 work_item.reset(WorkItem::CreateCopyTreeWorkItem( 484 work_item.reset(WorkItem::CreateCopyTreeWorkItem(
484 file_name_from, file_name_to, 485 file_name_from, file_name_to,
485 temp_dir_, WorkItem::NEW_NAME_IF_IN_USE, 486 temp_dir_, WorkItem::NEW_NAME_IF_IN_USE,
486 alternate_to)); 487 alternate_to));
487 if (IsFileInUse(file_name_to)) 488 if (IsFileInUse(file_name_to))
488 base::PlatformThread::Sleep(2000); 489 base::PlatformThread::Sleep(2000);
489 // If file is still in use, the rest of the test will fail. 490 // If file is still in use, the rest of the test will fail.
490 ASSERT_FALSE(IsFileInUse(file_name_to)); 491 ASSERT_FALSE(IsFileInUse(file_name_to));
491 EXPECT_TRUE(work_item->Do()); 492 EXPECT_TRUE(work_item->Do());
492 493
494 // Get the path of backup file
495 FilePath backup_file(work_item->backup_path_.path());
496 EXPECT_FALSE(backup_file.empty());
497 backup_file = backup_file.AppendASCII("File_To");
498
493 EXPECT_TRUE(file_util::PathExists(file_name_from)); 499 EXPECT_TRUE(file_util::PathExists(file_name_from));
494 EXPECT_TRUE(file_util::PathExists(file_name_to)); 500 EXPECT_TRUE(file_util::PathExists(file_name_to));
495 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 501 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
496 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, file_name_to)); 502 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, file_name_to));
497 // verify that the backup path does exist 503 // verify that the backup path does exist
498 EXPECT_TRUE(file_util::PathExists(backup_file)); 504 EXPECT_TRUE(file_util::PathExists(backup_file));
499 EXPECT_FALSE(file_util::PathExists(alternate_to)); 505 EXPECT_FALSE(file_util::PathExists(alternate_to));
500 506
501 // test rollback() 507 // test rollback()
502 work_item->Rollback(); 508 work_item->Rollback();
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 << " to " << file_name_to.value(); 630 << " to " << file_name_to.value();
625 631
626 // Run the executable in destination path 632 // Run the executable in destination path
627 STARTUPINFOW si = {sizeof(si)}; 633 STARTUPINFOW si = {sizeof(si)};
628 PROCESS_INFORMATION pi = {0}; 634 PROCESS_INFORMATION pi = {0};
629 ASSERT_TRUE( 635 ASSERT_TRUE(
630 ::CreateProcess(NULL, const_cast<wchar_t*>(file_name_to.value().c_str()), 636 ::CreateProcess(NULL, const_cast<wchar_t*>(file_name_to.value().c_str()),
631 NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, 637 NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED,
632 NULL, NULL, &si, &pi)); 638 NULL, NULL, &si, &pi));
633 639
634 // Get the path of backup file 640 FilePath backup_file;
635 FilePath backup_file(temp_dir_);
636 backup_file = backup_file.AppendASCII("File_To");
637 641
638 // test Do(). 642 // test Do().
639 { 643 {
640 scoped_ptr<CopyTreeWorkItem> work_item( 644 scoped_ptr<CopyTreeWorkItem> work_item(
641 WorkItem::CreateCopyTreeWorkItem(file_name_from, 645 WorkItem::CreateCopyTreeWorkItem(file_name_from,
642 file_name_to, 646 file_name_to,
643 temp_dir_, 647 temp_dir_,
644 WorkItem::IF_DIFFERENT, 648 WorkItem::IF_DIFFERENT,
645 FilePath())); 649 FilePath()));
646 650
647 EXPECT_TRUE(work_item->Do()); 651 EXPECT_TRUE(work_item->Do());
648 652
653 // Get the path of backup file
654 backup_file = work_item->backup_path_.path();
655 EXPECT_FALSE(backup_file.empty());
656 backup_file = backup_file.AppendASCII("File_To");
657
649 EXPECT_TRUE(file_util::PathExists(file_name_from)); 658 EXPECT_TRUE(file_util::PathExists(file_name_from));
650 EXPECT_TRUE(file_util::PathExists(file_name_to)); 659 EXPECT_TRUE(file_util::PathExists(file_name_to));
651 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); 660 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
652 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); 661 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
653 // verify the file in used is moved to backup place. 662 // verify the file in used is moved to backup place.
654 EXPECT_TRUE(file_util::PathExists(backup_file)); 663 EXPECT_TRUE(file_util::PathExists(backup_file));
655 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file)); 664 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file));
656 } 665 }
657 666
658 // verify the file in used should be still at the backup place. 667 // verify the file in used should be still at the backup place.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_1, file_name_to_1)); 728 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_1, file_name_to_1));
720 729
721 FilePath file_name_to_2(dir_name_to); 730 FilePath file_name_to_2(dir_name_to);
722 file_name_to_2 = file_name_to_2.AppendASCII("2"); 731 file_name_to_2 = file_name_to_2.AppendASCII("2");
723 file_name_to_2 = file_name_to_2.AppendASCII("File_2.txt"); 732 file_name_to_2 = file_name_to_2.AppendASCII("File_2.txt");
724 EXPECT_TRUE(file_util::PathExists(file_name_to_2)); 733 EXPECT_TRUE(file_util::PathExists(file_name_to_2));
725 VLOG(1) << "compare " << file_name_from_2.value() 734 VLOG(1) << "compare " << file_name_from_2.value()
726 << " and " << file_name_to_2.value(); 735 << " and " << file_name_to_2.value();
727 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_2, file_name_to_2)); 736 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_2, file_name_to_2));
728 } 737 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698