| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include <iostream> | 8 #include <iostream> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/platform_thread.h" |
| 14 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 15 #include "base/scoped_ptr.h" | 16 #include "base/scoped_ptr.h" |
| 16 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 17 #include "chrome/installer/util/work_item.h" | 18 #include "chrome/installer/util/work_item.h" |
| 18 #include "chrome/installer/util/copy_tree_work_item.h" | 19 #include "chrome/installer/util/copy_tree_work_item.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 class CopyTreeWorkItemTest : public testing::Test { | 23 class CopyTreeWorkItemTest : public testing::Test { |
| 23 protected: | 24 protected: |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 // Simple function to dump some text into a new file. | 55 // Simple function to dump some text into a new file. |
| 55 void CreateTextFile(const std::wstring& filename, | 56 void CreateTextFile(const std::wstring& filename, |
| 56 const std::wstring& contents) { | 57 const std::wstring& contents) { |
| 57 std::ofstream file; | 58 std::ofstream file; |
| 58 file.open(filename.c_str()); | 59 file.open(filename.c_str()); |
| 59 ASSERT_TRUE(file.is_open()); | 60 ASSERT_TRUE(file.is_open()); |
| 60 file << contents; | 61 file << contents; |
| 61 file.close(); | 62 file.close(); |
| 62 } | 63 } |
| 63 | 64 |
| 65 bool IsFileInUse(std::wstring path) { |
| 66 if (!file_util::PathExists(path)) |
| 67 return false; |
| 68 |
| 69 HANDLE handle = ::CreateFile(path.c_str(), FILE_ALL_ACCESS, |
| 70 NULL, NULL, OPEN_EXISTING, NULL, NULL); |
| 71 if (handle == INVALID_HANDLE_VALUE) |
| 72 return true; |
| 73 |
| 74 CloseHandle(handle); |
| 75 return false; |
| 76 } |
| 77 |
| 64 // Simple function to read text from a file. | 78 // Simple function to read text from a file. |
| 65 std::wstring ReadTextFile(const std::wstring& filename) { | 79 std::wstring ReadTextFile(const std::wstring& filename) { |
| 66 WCHAR contents[64]; | 80 WCHAR contents[64]; |
| 67 std::wifstream file; | 81 std::wifstream file; |
| 68 file.open(filename.c_str()); | 82 file.open(filename.c_str()); |
| 69 EXPECT_TRUE(file.is_open()); | 83 EXPECT_TRUE(file.is_open()); |
| 70 file.getline(contents, 64); | 84 file.getline(contents, 64); |
| 71 file.close(); | 85 file.close(); |
| 72 return std::wstring(contents); | 86 return std::wstring(contents); |
| 73 } | 87 } |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 TerminateProcess(pi.hProcess, 0); | 453 TerminateProcess(pi.hProcess, 0); |
| 440 // make sure the handle is closed. | 454 // make sure the handle is closed. |
| 441 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 455 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
| 442 CloseHandle(pi.hProcess); | 456 CloseHandle(pi.hProcess); |
| 443 CloseHandle(pi.hThread); | 457 CloseHandle(pi.hThread); |
| 444 | 458 |
| 445 // Now the process has terminated, lets try overwriting the file again | 459 // Now the process has terminated, lets try overwriting the file again |
| 446 work_item.reset(WorkItem::CreateCopyTreeWorkItem( | 460 work_item.reset(WorkItem::CreateCopyTreeWorkItem( |
| 447 file_name_from, file_name_to, temp_dir_, WorkItem::NEW_NAME_IF_IN_USE, | 461 file_name_from, file_name_to, temp_dir_, WorkItem::NEW_NAME_IF_IN_USE, |
| 448 alternate_to)); | 462 alternate_to)); |
| 463 if (IsFileInUse(file_name_to)) |
| 464 PlatformThread::Sleep(2000); |
| 465 // If file is still in use, the rest of the test will fail. |
| 466 ASSERT_FALSE(IsFileInUse(file_name_to)); |
| 449 EXPECT_TRUE(work_item->Do()); | 467 EXPECT_TRUE(work_item->Do()); |
| 450 | 468 |
| 451 EXPECT_TRUE(file_util::PathExists(file_name_from)); | 469 EXPECT_TRUE(file_util::PathExists(file_name_from)); |
| 452 EXPECT_TRUE(file_util::PathExists(file_name_to)); | 470 EXPECT_TRUE(file_util::PathExists(file_name_to)); |
| 453 EXPECT_EQ(0, ReadTextFile(file_name_from).compare(text_content_1)); | 471 EXPECT_EQ(0, ReadTextFile(file_name_from).compare(text_content_1)); |
| 454 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, file_name_to)); | 472 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, file_name_to)); |
| 455 // verify that the backup path does exist | 473 // verify that the backup path does exist |
| 456 EXPECT_TRUE(file_util::PathExists(backup_file)); | 474 EXPECT_TRUE(file_util::PathExists(backup_file)); |
| 457 EXPECT_FALSE(file_util::PathExists(alternate_to)); | 475 EXPECT_FALSE(file_util::PathExists(alternate_to)); |
| 458 | 476 |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 LOG(INFO) << "compare " << file_name_from_1 << " and " << file_name_to_1; | 678 LOG(INFO) << "compare " << file_name_from_1 << " and " << file_name_to_1; |
| 661 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_1, file_name_to_1)); | 679 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_1, file_name_to_1)); |
| 662 | 680 |
| 663 std::wstring file_name_to_2(dir_name_to); | 681 std::wstring file_name_to_2(dir_name_to); |
| 664 file_util::AppendToPath(&file_name_to_2, L"2"); | 682 file_util::AppendToPath(&file_name_to_2, L"2"); |
| 665 file_util::AppendToPath(&file_name_to_2, L"File_2.txt"); | 683 file_util::AppendToPath(&file_name_to_2, L"File_2.txt"); |
| 666 EXPECT_TRUE(file_util::PathExists(file_name_to_2)); | 684 EXPECT_TRUE(file_util::PathExists(file_name_to_2)); |
| 667 LOG(INFO) << "compare " << file_name_from_2 << " and " << file_name_to_2; | 685 LOG(INFO) << "compare " << file_name_from_2 << " and " << file_name_to_2; |
| 668 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_2, file_name_to_2)); | 686 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_2, file_name_to_2)); |
| 669 } | 687 } |
| OLD | NEW |