| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <windows.h> |
| 6 #include <shlwapi.h> // NOLINT |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/win/registry.h" |
| 11 #include "chrome/installer/util/copy_reg_key_work_item.h" |
| 12 #include "chrome/installer/util/registry_test_data.h" |
| 13 #include "chrome/installer/util/work_item.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 using base::win::RegKey; |
| 17 |
| 18 class CopyRegKeyWorkItemTest : public testing::Test { |
| 19 protected: |
| 20 static void TearDownTestCase() { |
| 21 logging::CloseLogFile(); |
| 22 } |
| 23 |
| 24 virtual void SetUp() { |
| 25 ASSERT_TRUE(test_data_.Initialize(HKEY_CURRENT_USER, L"SOFTWARE\\TmpTmp")); |
| 26 destination_path_.assign(test_data_.base_path()).append(L"\\Destination"); |
| 27 } |
| 28 |
| 29 RegistryTestData test_data_; |
| 30 std::wstring destination_path_; |
| 31 }; |
| 32 |
| 33 // Test that copying a key that doesn't exist succeeds, and that rollback does |
| 34 // nothing. |
| 35 TEST_F(CopyRegKeyWorkItemTest, TestNoKey) { |
| 36 const std::wstring key_paths[] = { |
| 37 std::wstring(test_data_.base_path() + L"\\NoKeyHere"), |
| 38 std::wstring(test_data_.base_path() + L"\\NoKeyHere\\OrHere") |
| 39 }; |
| 40 RegKey key; |
| 41 for (size_t i = 0; i < arraysize(key_paths); ++i) { |
| 42 const std::wstring& key_path = key_paths[i]; |
| 43 scoped_ptr<CopyRegKeyWorkItem> item( |
| 44 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), key_path, |
| 45 destination_path_)); |
| 46 EXPECT_TRUE(item->Do()); |
| 47 EXPECT_NE(ERROR_SUCCESS, |
| 48 key.Open(test_data_.root_key(), destination_path_.c_str(), |
| 49 KEY_READ)); |
| 50 item->Rollback(); |
| 51 item.reset(); |
| 52 EXPECT_NE(ERROR_SUCCESS, |
| 53 key.Open(test_data_.root_key(), destination_path_.c_str(), |
| 54 KEY_READ)); |
| 55 } |
| 56 } |
| 57 |
| 58 // Test that copying an empty key succeeds, and that rollback removes the copy. |
| 59 TEST_F(CopyRegKeyWorkItemTest, TestEmptyKey) { |
| 60 RegKey key; |
| 61 scoped_ptr<CopyRegKeyWorkItem> item( |
| 62 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), |
| 63 test_data_.empty_key_path(), |
| 64 destination_path_)); |
| 65 EXPECT_TRUE(item->Do()); |
| 66 RegistryTestData::ExpectEmptyKey(test_data_.root_key(), |
| 67 destination_path_.c_str()); |
| 68 item->Rollback(); |
| 69 item.reset(); |
| 70 EXPECT_NE(ERROR_SUCCESS, |
| 71 key.Open(test_data_.root_key(), destination_path_.c_str(), |
| 72 KEY_READ)); |
| 73 } |
| 74 |
| 75 // Test that copying a key with subkeys and values succeeds, and that rollback |
| 76 // removes the copy. |
| 77 TEST_F(CopyRegKeyWorkItemTest, TestNonEmptyKey) { |
| 78 RegKey key; |
| 79 scoped_ptr<CopyRegKeyWorkItem> item( |
| 80 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), |
| 81 test_data_.non_empty_key_path(), |
| 82 destination_path_)); |
| 83 EXPECT_TRUE(item->Do()); |
| 84 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(), |
| 85 destination_path_.c_str()); |
| 86 item->Rollback(); |
| 87 item.reset(); |
| 88 EXPECT_NE(ERROR_SUCCESS, |
| 89 key.Open(test_data_.root_key(), destination_path_.c_str(), |
| 90 KEY_READ)); |
| 91 } |
| 92 |
| 93 // Test that copying an empty key over a key with data succeeds, and that |
| 94 // rollback restores the original data. |
| 95 TEST_F(CopyRegKeyWorkItemTest, TestOverwriteAndRestore) { |
| 96 RegKey key; |
| 97 // First copy the data into the dest. |
| 98 EXPECT_EQ(ERROR_SUCCESS, |
| 99 key.Create(test_data_.root_key(), destination_path_.c_str(), |
| 100 KEY_WRITE)); |
| 101 EXPECT_EQ(ERROR_SUCCESS, |
| 102 SHCopyKey(test_data_.root_key(), |
| 103 test_data_.non_empty_key_path().c_str(), key.Handle(), |
| 104 0)); |
| 105 key.Close(); |
| 106 |
| 107 // Now copy the empty key into the dest. |
| 108 scoped_ptr<CopyRegKeyWorkItem> item( |
| 109 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), |
| 110 test_data_.empty_key_path(), |
| 111 destination_path_)); |
| 112 EXPECT_TRUE(item->Do()); |
| 113 |
| 114 // Make sure the dest is now empty. |
| 115 RegistryTestData::ExpectEmptyKey(test_data_.root_key(), |
| 116 destination_path_.c_str()); |
| 117 |
| 118 // Restore the data. |
| 119 item->Rollback(); |
| 120 item.reset(); |
| 121 |
| 122 // Make sure it's all there. |
| 123 test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(), |
| 124 destination_path_.c_str()); |
| 125 } |
| 126 |
| 127 // Test that Rollback does nothing when the item is configured to ignore |
| 128 // failures. |
| 129 TEST_F(CopyRegKeyWorkItemTest, TestIgnoreFailRollback) { |
| 130 // Copy the empty key onto the non-empty key, ignoring failures. |
| 131 scoped_ptr<CopyRegKeyWorkItem> item( |
| 132 WorkItem::CreateCopyRegKeyWorkItem(test_data_.root_key(), |
| 133 test_data_.empty_key_path(), |
| 134 test_data_.non_empty_key_path())); |
| 135 item->set_ignore_failure(true); |
| 136 EXPECT_TRUE(item->Do()); |
| 137 item->Rollback(); |
| 138 item.reset(); |
| 139 RegistryTestData::ExpectEmptyKey(test_data_.root_key(), |
| 140 test_data_.non_empty_key_path().c_str()); |
| 141 } |
| OLD | NEW |