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
|
// Copyright 2011 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/move_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/memory_mapped_file.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/installer/util/work_item.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class MoveTreeWorkItemTest : public testing::Test {
protected:
void SetUp() override {
ASSERT_TRUE(temp_from_dir_.CreateUniqueTempDir());
ASSERT_TRUE(temp_to_dir_.CreateUniqueTempDir());
}
base::ScopedTempDir temp_from_dir_;
base::ScopedTempDir temp_to_dir_;
};
// Simple function to dump some text into a new file.
void CreateTextFile(const std::wstring& filename,
const std::wstring& contents) {
std::wofstream file;
file.open(base::WideToASCII(filename).c_str());
ASSERT_TRUE(file.is_open());
file << contents;
file.close();
}
// Simple function to read text from a file.
std::wstring ReadTextFile(const base::FilePath& path) {
WCHAR contents[64];
std::wifstream file;
file.open(base::WideToASCII(path.value()).c_str());
EXPECT_TRUE(file.is_open());
file.getline(contents, std::size(contents));
file.close();
return std::wstring(contents);
}
const wchar_t kTextContent1[] = L"Gooooooooooooooooooooogle";
const wchar_t kTextContent2[] = L"Overwrite Me";
} // namespace
// Move one directory from source to destination when destination does not
// exist.
TEST_F(MoveTreeWorkItemTest, MoveDirectory) {
// Create two level deep source dir
base::FilePath from_dir1(temp_from_dir_.GetPath());
from_dir1 = from_dir1.AppendASCII("From_Dir1");
base::CreateDirectory(from_dir1);
ASSERT_TRUE(base::PathExists(from_dir1));
base::FilePath from_dir2(from_dir1);
from_dir2 = from_dir2.AppendASCII("From_Dir2");
base::CreateDirectory(from_dir2);
ASSERT_TRUE(base::PathExists(from_dir2));
base::FilePath from_file(from_dir2);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
// Generate destination path
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
ASSERT_FALSE(base::PathExists(to_dir));
base::FilePath to_file(to_dir);
to_file = to_file.AppendASCII("From_Dir2");
to_file = to_file.AppendASCII("From_File");
ASSERT_FALSE(base::PathExists(to_file));
// test Do()
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_TRUE(work_item->Do());
EXPECT_FALSE(base::PathExists(from_dir1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::PathExists(to_file));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(from_dir1));
EXPECT_TRUE(base::PathExists(from_file));
EXPECT_FALSE(base::PathExists(to_dir));
}
// Move one directory from source to destination when destination already
// exists.
TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) {
// Create two level deep source dir
base::FilePath from_dir1(temp_from_dir_.GetPath());
from_dir1 = from_dir1.AppendASCII("From_Dir1");
base::CreateDirectory(from_dir1);
ASSERT_TRUE(base::PathExists(from_dir1));
base::FilePath from_dir2(from_dir1);
from_dir2 = from_dir2.AppendASCII("From_Dir2");
base::CreateDirectory(from_dir2);
ASSERT_TRUE(base::PathExists(from_dir2));
base::FilePath from_file(from_dir2);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
// Create destination path
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
base::CreateDirectory(to_dir);
ASSERT_TRUE(base::PathExists(to_dir));
base::FilePath orig_to_file(to_dir);
orig_to_file = orig_to_file.AppendASCII("To_File");
CreateTextFile(orig_to_file.value(), kTextContent2);
ASSERT_TRUE(base::PathExists(orig_to_file));
base::FilePath new_to_file(to_dir);
new_to_file = new_to_file.AppendASCII("From_Dir2");
new_to_file = new_to_file.AppendASCII("From_File");
ASSERT_FALSE(base::PathExists(new_to_file));
// test Do(), don't check for duplicates.
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_TRUE(work_item->Do());
EXPECT_FALSE(base::PathExists(from_dir1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::PathExists(new_to_file));
EXPECT_FALSE(base::PathExists(orig_to_file));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(from_dir1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_FALSE(base::PathExists(new_to_file));
EXPECT_TRUE(base::PathExists(orig_to_file));
EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent2));
EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
}
// Move one file from source to destination when destination does not
// exist.
TEST_F(MoveTreeWorkItemTest, MoveAFile) {
// Create a file inside source dir
base::FilePath from_dir(temp_from_dir_.GetPath());
from_dir = from_dir.AppendASCII("From_Dir");
base::CreateDirectory(from_dir);
ASSERT_TRUE(base::PathExists(from_dir));
base::FilePath from_file(from_dir);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
// Generate destination file name
base::FilePath to_file(temp_from_dir_.GetPath());
to_file = to_file.AppendASCII("To_File");
ASSERT_FALSE(base::PathExists(to_file));
// test Do()
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_file, to_file, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_FALSE(base::PathExists(from_file));
EXPECT_TRUE(base::PathExists(to_file));
EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_TRUE(base::PathExists(from_file));
EXPECT_FALSE(base::PathExists(to_file));
EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
}
// Move one file from source to destination when destination already
// exists.
TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) {
// Create a file inside source dir
base::FilePath from_dir(temp_from_dir_.GetPath());
from_dir = from_dir.AppendASCII("From_Dir");
base::CreateDirectory(from_dir);
ASSERT_TRUE(base::PathExists(from_dir));
base::FilePath from_file(from_dir);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
// Create destination path
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
base::CreateDirectory(to_dir);
ASSERT_TRUE(base::PathExists(to_dir));
base::FilePath to_file(to_dir);
to_file = to_file.AppendASCII("To_File");
CreateTextFile(to_file.value(), kTextContent2);
ASSERT_TRUE(base::PathExists(to_file));
// test Do()
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_file, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_FALSE(base::PathExists(from_file));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_FALSE(base::PathExists(to_file));
EXPECT_EQ(0, ReadTextFile(to_dir).compare(kTextContent1));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent2));
}
// Move one file from source to destination when destination already
// exists and is in use.
TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) {
// Create a file inside source dir
base::FilePath from_dir(temp_from_dir_.GetPath());
from_dir = from_dir.AppendASCII("From_Dir");
base::CreateDirectory(from_dir);
ASSERT_TRUE(base::PathExists(from_dir));
base::FilePath from_file(from_dir);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
// Create an executable in destination path by copying ourself to it.
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
base::CreateDirectory(to_dir);
ASSERT_TRUE(base::PathExists(to_dir));
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 to_file(to_dir);
to_file = to_file.AppendASCII("To_File");
base::CopyFile(exe_full_path, to_file);
ASSERT_TRUE(base::PathExists(to_file));
// Run the executable in destination path
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi = {0};
ASSERT_TRUE(::CreateProcess(
nullptr, const_cast<wchar_t*>(to_file.value().c_str()), nullptr, nullptr,
FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, nullptr, nullptr, &si, &pi));
// test Do()
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_file, to_file, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_FALSE(base::PathExists(from_file));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file));
TerminateProcess(pi.hProcess, 0);
EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
// Move one file that is in use to destination.
TEST_F(MoveTreeWorkItemTest, MoveFileInUse) {
// Create an executable for source by copying ourself to a new source dir.
base::FilePath from_dir(temp_from_dir_.GetPath());
from_dir = from_dir.AppendASCII("From_Dir");
base::CreateDirectory(from_dir);
ASSERT_TRUE(base::PathExists(from_dir));
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 from_file(from_dir);
from_file = from_file.AppendASCII("From_File");
base::CopyFile(exe_full_path, from_file);
ASSERT_TRUE(base::PathExists(from_file));
// Create a destination source dir and generate destination file name.
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
base::CreateDirectory(to_dir);
ASSERT_TRUE(base::PathExists(to_dir));
base::FilePath to_file(to_dir);
to_file = to_file.AppendASCII("To_File");
CreateTextFile(to_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(to_file));
// Run the executable in source path
STARTUPINFOW si = {sizeof(si)};
PROCESS_INFORMATION pi = {0};
ASSERT_TRUE(::CreateProcess(
nullptr, const_cast<wchar_t*>(from_file.value().c_str()), nullptr,
nullptr, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED, nullptr, nullptr,
&si, &pi));
// test Do()
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_file, to_file, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_TRUE(work_item->Do());
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_FALSE(base::PathExists(from_file));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file));
// Close the process and make sure all the conditions after Do() are
// still true.
TerminateProcess(pi.hProcess, 0);
EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_FALSE(base::PathExists(from_file));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, to_file));
// test rollback()
work_item->Rollback();
EXPECT_TRUE(base::PathExists(from_dir));
EXPECT_TRUE(base::ContentsEqual(exe_full_path, from_file));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
}
// Move one directory from source to destination when destination already
// exists.
TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExistsCheckForDuplicatesFull) {
// Create two level deep source dir
base::FilePath from_dir1(temp_from_dir_.GetPath());
from_dir1 = from_dir1.AppendASCII("From_Dir1");
base::CreateDirectory(from_dir1);
ASSERT_TRUE(base::PathExists(from_dir1));
base::FilePath from_dir2(from_dir1);
from_dir2 = from_dir2.AppendASCII("From_Dir2");
base::CreateDirectory(from_dir2);
ASSERT_TRUE(base::PathExists(from_dir2));
base::FilePath from_file(from_dir2);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
// Create a file hierarchy identical to the one in the source directory.
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
ASSERT_TRUE(base::CopyDirectory(from_dir1, to_dir, true));
// Lock one of the files in the to destination directory to prevent moves.
base::FilePath orig_to_file(
to_dir.AppendASCII("From_Dir2").AppendASCII("From_File"));
base::MemoryMappedFile mapped_file;
EXPECT_TRUE(mapped_file.Initialize(orig_to_file));
// First check that we can't do the regular Move().
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::ALWAYS_MOVE));
EXPECT_FALSE(work_item->Do());
work_item->Rollback();
// Now test Do() with the check for duplicates. This should pass.
work_item.reset(WorkItem::CreateMoveTreeWorkItem(
from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::CHECK_DUPLICATES));
EXPECT_TRUE(work_item->Do());
// Make sure that we "moved" the files, i.e. that the source directory isn't
// there anymore,
EXPECT_FALSE(base::PathExists(from_dir1));
// Make sure that the original directory structure and file are still present.
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::PathExists(orig_to_file));
// Make sure that the backup path is not empty.
EXPECT_FALSE(base::IsDirectoryEmpty(temp_to_dir_.GetPath()));
// Check that the work item believes the source to have been moved.
EXPECT_TRUE(work_item->source_moved_to_backup_);
EXPECT_FALSE(work_item->moved_to_dest_path_);
EXPECT_FALSE(work_item->moved_to_backup_);
// test rollback()
work_item->Rollback();
// Once we rollback all the original files should still be there, as should
// the source files.
EXPECT_TRUE(base::PathExists(from_dir1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::PathExists(orig_to_file));
EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1));
EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
}
// Move one directory from source to destination when destination already
// exists but contains only a subset of the files in source.
TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExistsCheckForDuplicatesPartial) {
// Create two level deep source dir
base::FilePath from_dir1(temp_from_dir_.GetPath());
from_dir1 = from_dir1.AppendASCII("From_Dir1");
base::CreateDirectory(from_dir1);
ASSERT_TRUE(base::PathExists(from_dir1));
base::FilePath from_dir2(from_dir1);
from_dir2 = from_dir2.AppendASCII("From_Dir2");
base::CreateDirectory(from_dir2);
ASSERT_TRUE(base::PathExists(from_dir2));
base::FilePath from_file(from_dir2);
from_file = from_file.AppendASCII("From_File");
CreateTextFile(from_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(from_file));
base::FilePath from_file2(from_dir2);
from_file2 = from_file2.AppendASCII("From_File2");
CreateTextFile(from_file2.value(), kTextContent2);
ASSERT_TRUE(base::PathExists(from_file2));
// Create destination path
base::FilePath to_dir(temp_from_dir_.GetPath());
to_dir = to_dir.AppendASCII("To_Dir");
base::CreateDirectory(to_dir);
ASSERT_TRUE(base::PathExists(to_dir));
// Create a sub-directory of the same name as in the source directory.
base::FilePath to_dir2(to_dir);
to_dir2 = to_dir2.AppendASCII("From_Dir2");
base::CreateDirectory(to_dir2);
ASSERT_TRUE(base::PathExists(to_dir2));
// Create one of the files in the to sub-directory, but not the other.
base::FilePath orig_to_file(to_dir2);
orig_to_file = orig_to_file.AppendASCII("From_File");
CreateTextFile(orig_to_file.value(), kTextContent1);
ASSERT_TRUE(base::PathExists(orig_to_file));
// test Do(), check for duplicates.
std::unique_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem(
from_dir1, to_dir, temp_to_dir_.GetPath(), WorkItem::CHECK_DUPLICATES));
EXPECT_TRUE(work_item->Do());
// Make sure that we "moved" the files, i.e. that the source directory isn't
// there anymore,
EXPECT_FALSE(base::PathExists(from_dir1));
// Make sure that the original directory structure and file are still present.
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::PathExists(orig_to_file));
// Make sure that the backup path is not empty.
EXPECT_FALSE(base::IsDirectoryEmpty(temp_to_dir_.GetPath()));
// Make sure that the "new" file is also present.
base::FilePath new_to_file2(to_dir2);
new_to_file2 = new_to_file2.AppendASCII("From_File2");
EXPECT_TRUE(base::PathExists(new_to_file2));
// Check that the work item believes that this was a regular move.
EXPECT_FALSE(work_item->source_moved_to_backup_);
EXPECT_TRUE(work_item->moved_to_dest_path_);
EXPECT_TRUE(work_item->moved_to_backup_);
// test rollback()
work_item->Rollback();
// Once we rollback all the original files should still be there, as should
// the source files.
EXPECT_TRUE(base::PathExists(from_dir1));
EXPECT_TRUE(base::PathExists(to_dir));
EXPECT_TRUE(base::PathExists(orig_to_file));
EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1));
EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
// Also, after rollback the new "to" file should be gone.
EXPECT_FALSE(base::PathExists(new_to_file2));
}
|