1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/util/copy_tree_work_item.h"
#include <windows.h>
#include <fstream>
#include <memory>
#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/threading/platform_thread.h"
#include "chrome/installer/util/work_item.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class CopyTreeWorkItemTest : public testing::Test {
protected:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
}
void TearDown() override { logging::CloseLogFile(); }
// the path to temporary directory used to contain the test operations
base::ScopedTempDir test_dir_;
base::ScopedTempDir temp_dir_;
};
// Simple function to dump some text into a new file.
void CreateTextFile(const std::wstring& filename,
const std::wstring& contents) {
std::ofstream file;
file.open(filename.c_str());
ASSERT_TRUE(file.is_open());
file << contents;
file.close();
}
// Simple function to read text from a file.
std::wstring ReadTextFile(const std::wstring& filename) {
WCHAR contents[64];
std::wifstream file;
file.open(filename.c_str());
EXPECT_TRUE(file.is_open());
file.getline(contents, 64);
file.close();
return std::wstring(contents);
}
const wchar_t text_content_1[] = L"Gooooooooooooooooooooogle";
const wchar_t text_content_2[] = L"Overwrite Me";
} // namespace
// Copy one file from source to destination.
TEST_F(CopyTreeWorkItemTest, CopyFile) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From.txt");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create destination path
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To.txt");
// test Do()
std::unique_ptr<CopyTreeWorkItem> work_item(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(), WorkItem::ALWAYS,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_TRUE(base::ContentsEqual(file_name_from, file_name_to));
// test rollback()
work_item->Rollback();
EXPECT_FALSE(base::PathExists(file_name_to));
EXPECT_TRUE(base::PathExists(file_name_from));
}
// Copy one file, overwriting the existing one in destination.
// Test with always_overwrite being true or false. The file is overwritten
// regardless since the content at destination file is different from source.
TEST_F(CopyTreeWorkItemTest, CopyFileOverwrite) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From.txt");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create destination file
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To.txt");
CreateTextFile(file_name_to.value(), text_content_2);
ASSERT_TRUE(base::PathExists(file_name_to));
// test Do() with always_overwrite being true.
std::unique_ptr<CopyTreeWorkItem> work_item(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(), WorkItem::ALWAYS,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_2));
// test Do() with always_overwrite being false.
// the file is still overwritten since the content is different.
work_item.reset(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(), WorkItem::IF_DIFFERENT,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_2));
}
// Copy one file, with the existing one in destination having the same
// content.
// If always_overwrite being true, the file is overwritten.
// If always_overwrite being false, the file is unchanged.
TEST_F(CopyTreeWorkItemTest, CopyFileSameContent) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From.txt");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create destination file
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To.txt");
CreateTextFile(file_name_to.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_to));
// test Do() with always_overwrite being true.
std::unique_ptr<CopyTreeWorkItem> work_item(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(), WorkItem::ALWAYS,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
// Get the path of backup file
base::FilePath backup_file(work_item->backup_path_.GetPath());
EXPECT_FALSE(backup_file.empty());
backup_file = backup_file.AppendASCII("File_To.txt");
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// we verify the file is overwritten by checking the existence of backup
// file.
EXPECT_TRUE(base::PathExists(backup_file));
EXPECT_EQ(0, ReadTextFile(backup_file.value()).compare(text_content_1));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// the backup file should be gone after rollback
EXPECT_FALSE(base::PathExists(backup_file));
// test Do() with always_overwrite being false. nothing should change.
work_item.reset(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(), WorkItem::IF_DIFFERENT,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// we verify the file is not overwritten by checking that the backup
// file does not exist.
EXPECT_FALSE(base::PathExists(backup_file));
// test rollback(). nothing should happen here.
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
EXPECT_FALSE(base::PathExists(backup_file));
}
// Copy one file and without rollback. Verify all temporary files are deleted.
TEST_F(CopyTreeWorkItemTest, CopyFileAndCleanup) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From.txt");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create destination file
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To.txt");
CreateTextFile(file_name_to.value(), text_content_2);
ASSERT_TRUE(base::PathExists(file_name_to));
base::FilePath backup_file;
{
// test Do().
std::unique_ptr<CopyTreeWorkItem> work_item(
WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(),
WorkItem::IF_DIFFERENT, base::FilePath()));
EXPECT_TRUE(work_item->Do());
// Get the path of backup file
backup_file = work_item->backup_path_.GetPath();
EXPECT_FALSE(backup_file.empty());
backup_file = backup_file.AppendASCII("File_To.txt");
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// verify the file is moved to backup place.
EXPECT_TRUE(base::PathExists(backup_file));
EXPECT_EQ(0, ReadTextFile(backup_file.value()).compare(text_content_2));
}
// verify the backup file is cleaned up as well.
EXPECT_FALSE(base::PathExists(backup_file));
}
// Copy one file, with the existing one in destination being used with
// overwrite option as IF_DIFFERENT. This destination-file-in-use should
// be moved to backup location after Do() and moved back after Rollback().
TEST_F(CopyTreeWorkItemTest, CopyFileInUse) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create an executable in destination path by copying ourself to it.
wchar_t exe_full_path_str[MAX_PATH];
::GetModuleFileName(nullptr, exe_full_path_str, MAX_PATH);
base::FilePath exe_full_path(exe_full_path_str);
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To");
base::CopyFile(exe_full_path, file_name_to);
ASSERT_TRUE(base::PathExists(file_name_to));
VLOG(1) << "copy ourself from " << exe_full_path.value() << " to "
<< file_name_to.value();
// Run the executable in destination path
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi = {0};
ASSERT_TRUE(::CreateProcess(
nullptr, const_cast<wchar_t*>(file_name_to.value().c_str()), nullptr,
nullptr, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, nullptr, nullptr,
&si, &pi));
// test Do().
std::unique_ptr<CopyTreeWorkItem> work_item(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(), WorkItem::IF_DIFFERENT,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
// Get the path of backup file
base::FilePath backup_file(work_item->backup_path_.GetPath());
EXPECT_FALSE(backup_file.empty());
backup_file = backup_file.AppendASCII("File_To");
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// verify the file in used is moved to backup place.
EXPECT_TRUE(base::PathExists(backup_file));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, backup_file));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to));
// the backup file should be gone after rollback
EXPECT_FALSE(base::PathExists(backup_file));
TerminateProcess(pi.hProcess, 0);
// make sure the handle is closed.
EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
// Test overwrite option NEW_NAME_IF_IN_USE:
// 1. If destination file is in use, the source should be copied with the
// new name after Do() and this new name file should be deleted
// after rollback.
// 2. If destination file is not in use, the source should be copied in the
// destination folder after Do() and should be rolled back after Rollback().
TEST_F(CopyTreeWorkItemTest, NewNameAndCopyTest) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create an executable in destination path by copying ourself to it.
wchar_t exe_full_path_str[MAX_PATH];
::GetModuleFileName(nullptr, exe_full_path_str, MAX_PATH);
base::FilePath exe_full_path(exe_full_path_str);
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to), alternate_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To");
alternate_to = alternate_to.AppendASCII("Alternate_To");
base::CopyFile(exe_full_path, file_name_to);
ASSERT_TRUE(base::PathExists(file_name_to));
ASSERT_FALSE(CopyTreeWorkItem::IsFileInUse(file_name_to));
VLOG(1) << "copy ourself from " << exe_full_path.value() << " to "
<< file_name_to.value();
// Run the executable in destination path
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi = {0};
ASSERT_TRUE(::CreateProcess(
nullptr, const_cast<wchar_t*>(file_name_to.value().c_str()), nullptr,
nullptr, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, nullptr, nullptr,
&si, &pi));
// test Do().
std::unique_ptr<CopyTreeWorkItem> work_item(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(),
WorkItem::NEW_NAME_IF_IN_USE, alternate_to));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to));
// verify that the backup path does not exist
EXPECT_FALSE(work_item->backup_path_created_);
EXPECT_TRUE(base::ContentsEqual(file_name_from, alternate_to));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to));
EXPECT_FALSE(work_item->backup_path_created_);
// the alternate file should be gone after rollback
EXPECT_FALSE(base::PathExists(alternate_to));
TerminateProcess(pi.hProcess, 0);
// make sure the handle is closed.
EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
// Now the process has terminated, lets try overwriting the file again
work_item.reset(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(),
WorkItem::NEW_NAME_IF_IN_USE, alternate_to));
if (CopyTreeWorkItem::IsFileInUse(file_name_to))
base::PlatformThread::Sleep(base::Seconds(2));
// If file is still in use, the rest of the test will fail.
ASSERT_FALSE(CopyTreeWorkItem::IsFileInUse(file_name_to));
EXPECT_TRUE(work_item->Do());
// Get the path of backup file
base::FilePath backup_file(work_item->backup_path_.GetPath());
EXPECT_FALSE(backup_file.empty());
backup_file = backup_file.AppendASCII("File_To");
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(file_name_from, file_name_to));
// verify that the backup path does exist
EXPECT_TRUE(base::PathExists(backup_file));
EXPECT_FALSE(base::PathExists(alternate_to));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to));
// the backup file should be gone after rollback
EXPECT_FALSE(base::PathExists(backup_file));
EXPECT_FALSE(base::PathExists(alternate_to));
}
// Test overwrite option IF_NOT_PRESENT:
// 1. If destination file/directory exist, the source should not be copied
// 2. If destination file/directory do not exist, the source should be copied
// in the destination folder after Do() and should be rolled back after
// Rollback().
// Flaky, http://crbug.com/59785.
TEST_F(CopyTreeWorkItemTest, DISABLED_IfNotPresentTest) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create an executable in destination path by copying ourself to it.
wchar_t exe_full_path_str[MAX_PATH];
::GetModuleFileName(nullptr, exe_full_path_str, MAX_PATH);
base::FilePath exe_full_path(exe_full_path_str);
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To");
base::CopyFile(exe_full_path, file_name_to);
ASSERT_TRUE(base::PathExists(file_name_to));
// Get the path of backup file
base::FilePath backup_file(temp_dir_.GetPath());
backup_file = backup_file.AppendASCII("File_To");
// test Do().
std::unique_ptr<CopyTreeWorkItem> work_item(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(),
WorkItem::IF_NOT_PRESENT, base::FilePath()));
EXPECT_TRUE(work_item->Do());
// verify that the source, destination have not changed and backup path
// does not exist
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to));
EXPECT_FALSE(base::PathExists(backup_file));
// test rollback()
work_item->Rollback();
// verify that the source, destination have not changed and backup path
// does not exist after rollback also
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to));
EXPECT_FALSE(base::PathExists(backup_file));
// Now delete the destination and try copying the file again.
base::DeleteFile(file_name_to);
work_item.reset(WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(),
WorkItem::IF_NOT_PRESENT, base::FilePath()));
EXPECT_TRUE(work_item->Do());
// verify that the source, destination are the same and backup path
// does not exist
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
EXPECT_FALSE(base::PathExists(backup_file));
// test rollback()
work_item->Rollback();
// verify that the destination does not exist anymore
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_FALSE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_FALSE(base::PathExists(backup_file));
}
// Copy one file without rollback. The existing one in destination is in use.
// Verify it is moved to backup location and stays there.
// Flaky, http://crbug.com/59783.
TEST_F(CopyTreeWorkItemTest, DISABLED_CopyFileInUseAndCleanup) {
// Create source file
base::FilePath file_name_from(test_dir_.GetPath());
file_name_from = file_name_from.AppendASCII("File_From");
CreateTextFile(file_name_from.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from));
// Create an executable in destination path by copying ourself to it.
wchar_t exe_full_path_str[MAX_PATH];
::GetModuleFileName(nullptr, exe_full_path_str, MAX_PATH);
base::FilePath exe_full_path(exe_full_path_str);
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("Copy_To_Subdir");
base::CreateDirectory(dir_name_to);
ASSERT_TRUE(base::PathExists(dir_name_to));
base::FilePath file_name_to(dir_name_to);
file_name_to = file_name_to.AppendASCII("File_To");
base::CopyFile(exe_full_path, file_name_to);
ASSERT_TRUE(base::PathExists(file_name_to));
VLOG(1) << "copy ourself from " << exe_full_path.value() << " to "
<< file_name_to.value();
// Run the executable in destination path
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi = {0};
ASSERT_TRUE(::CreateProcess(
nullptr, const_cast<wchar_t*>(file_name_to.value().c_str()), nullptr,
nullptr, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, nullptr, nullptr,
&si, &pi));
base::FilePath backup_file;
// test Do().
{
std::unique_ptr<CopyTreeWorkItem> work_item(
WorkItem::CreateCopyTreeWorkItem(
file_name_from, file_name_to, temp_dir_.GetPath(),
WorkItem::IF_DIFFERENT, base::FilePath()));
EXPECT_TRUE(work_item->Do());
// Get the path of backup file
backup_file = work_item->backup_path_.GetPath();
EXPECT_FALSE(backup_file.empty());
backup_file = backup_file.AppendASCII("File_To");
EXPECT_TRUE(base::PathExists(file_name_from));
EXPECT_TRUE(base::PathExists(file_name_to));
EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1));
EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1));
// verify the file in used is moved to backup place.
EXPECT_TRUE(base::PathExists(backup_file));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, backup_file));
}
// verify the file in used should be still at the backup place.
EXPECT_TRUE(base::PathExists(backup_file));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, backup_file));
TerminateProcess(pi.hProcess, 0);
// make sure the handle is closed.
EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
// Copy a tree from source to destination.
// Flaky, http://crbug.com/59784.
TEST_F(CopyTreeWorkItemTest, DISABLED_CopyTree) {
// Create source tree
base::FilePath dir_name_from(test_dir_.GetPath());
dir_name_from = dir_name_from.AppendASCII("from");
base::CreateDirectory(dir_name_from);
ASSERT_TRUE(base::PathExists(dir_name_from));
base::FilePath dir_name_from_1(dir_name_from);
dir_name_from_1 = dir_name_from_1.AppendASCII("1");
base::CreateDirectory(dir_name_from_1);
ASSERT_TRUE(base::PathExists(dir_name_from_1));
base::FilePath dir_name_from_2(dir_name_from);
dir_name_from_2 = dir_name_from_2.AppendASCII("2");
base::CreateDirectory(dir_name_from_2);
ASSERT_TRUE(base::PathExists(dir_name_from_2));
base::FilePath file_name_from_1(dir_name_from_1);
file_name_from_1 = file_name_from_1.AppendASCII("File_1.txt");
CreateTextFile(file_name_from_1.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from_1));
base::FilePath file_name_from_2(dir_name_from_2);
file_name_from_2 = file_name_from_2.AppendASCII("File_2.txt");
CreateTextFile(file_name_from_2.value(), text_content_1);
ASSERT_TRUE(base::PathExists(file_name_from_2));
base::FilePath dir_name_to(test_dir_.GetPath());
dir_name_to = dir_name_to.AppendASCII("to");
// test Do()
{
std::unique_ptr<CopyTreeWorkItem> work_item(
WorkItem::CreateCopyTreeWorkItem(dir_name_from, dir_name_to,
temp_dir_.GetPath(), WorkItem::ALWAYS,
base::FilePath()));
EXPECT_TRUE(work_item->Do());
}
base::FilePath file_name_to_1(dir_name_to);
file_name_to_1 = file_name_to_1.AppendASCII("1");
file_name_to_1 = file_name_to_1.AppendASCII("File_1.txt");
EXPECT_TRUE(base::PathExists(file_name_to_1));
VLOG(1) << "compare " << file_name_from_1.value() << " and "
<< file_name_to_1.value();
EXPECT_TRUE(base::ContentsEqual(file_name_from_1, file_name_to_1));
base::FilePath file_name_to_2(dir_name_to);
file_name_to_2 = file_name_to_2.AppendASCII("2");
file_name_to_2 = file_name_to_2.AppendASCII("File_2.txt");
EXPECT_TRUE(base::PathExists(file_name_to_2));
VLOG(1) << "compare " << file_name_from_2.value() << " and "
<< file_name_to_2.value();
EXPECT_TRUE(base::ContentsEqual(file_name_from_2, file_name_to_2));
}
|