OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/files/file_path.h" | |
8 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_util.h" | 8 #include "base/strings/string16.h" |
| 9 #include "base/test/test_reg_util_win.h" |
10 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
11 #include "chrome/installer/util/delete_reg_value_work_item.h" | 11 #include "chrome/installer/util/delete_reg_value_work_item.h" |
12 #include "chrome/installer/util/work_item.h" | 12 #include "chrome/installer/util/work_item.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 using base::win::RegKey; | 15 using base::win::RegKey; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 const wchar_t kTestRoot[] = L"DeleteRegValueWorkItemTest"; | 19 const wchar_t kTestKey[] = L"DeleteRegValueWorkItemTest"; |
20 const wchar_t kWriteNew[] = L"WriteNew"; | |
21 const wchar_t kNameStr[] = L"name_str"; | 20 const wchar_t kNameStr[] = L"name_str"; |
22 const wchar_t kNameDword[] = L"name_dword"; | 21 const wchar_t kNameDword[] = L"name_dword"; |
23 | 22 |
24 class DeleteRegValueWorkItemTest : public testing::Test { | 23 class DeleteRegValueWorkItemTest : public testing::Test { |
25 protected: | 24 protected: |
| 25 DeleteRegValueWorkItemTest() {} |
| 26 |
26 void SetUp() override { | 27 void SetUp() override { |
27 // Create a temporary key for testing | 28 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER); |
28 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); | |
29 key.DeleteKey(kTestRoot); | |
30 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kTestRoot, KEY_READ)); | |
31 ASSERT_EQ(ERROR_SUCCESS, | |
32 key.Create(HKEY_CURRENT_USER, kTestRoot, KEY_READ)); | |
33 } | 29 } |
34 void TearDown() override { | 30 |
35 logging::CloseLogFile(); | 31 private: |
36 // Clean up the temporary key | 32 registry_util::RegistryOverrideManager registry_override_manager_; |
37 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); | 33 |
38 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kTestRoot)); | 34 DISALLOW_COPY_AND_ASSIGN(DeleteRegValueWorkItemTest); |
39 } | |
40 }; | 35 }; |
41 | 36 |
42 } // namespace | 37 } // namespace |
43 | 38 |
44 // Delete a value. The value should get deleted after Do() and should be | 39 // Delete a value. The value should get deleted after Do() and should be |
45 // recreated after Rollback(). | 40 // recreated after Rollback(). |
46 TEST_F(DeleteRegValueWorkItemTest, DeleteExistingValue) { | 41 TEST_F(DeleteRegValueWorkItemTest, DeleteExistingValue) { |
47 RegKey key; | 42 RegKey key; |
48 std::wstring parent_key(kTestRoot); | |
49 parent_key.push_back(base::FilePath::kSeparators[0]); | |
50 parent_key.append(kWriteNew); | |
51 ASSERT_EQ(ERROR_SUCCESS, | 43 ASSERT_EQ(ERROR_SUCCESS, |
52 key.Create(HKEY_CURRENT_USER, parent_key.c_str(), KEY_READ | KEY_WRITE)); | 44 key.Create(HKEY_CURRENT_USER, kTestKey, KEY_READ | KEY_WRITE)); |
53 std::wstring name_str(kNameStr); | 45 const base::string16 data_str(L"data_111"); |
54 std::wstring data_str(L"data_111"); | 46 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kNameStr, data_str.c_str())); |
55 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(name_str.c_str(), data_str.c_str())); | 47 const DWORD data_dword = 100; |
56 std::wstring name_dword(kNameDword); | 48 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kNameDword, data_dword)); |
57 DWORD data_dword = 100; | |
58 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(name_dword.c_str(), data_dword)); | |
59 | 49 |
60 std::wstring name_empty(L"name_empty"); | 50 static const wchar_t kNameEmpty[](L"name_empty"); |
61 ASSERT_EQ(ERROR_SUCCESS, RegSetValueEx(key.Handle(), name_empty.c_str(), NULL, | 51 ASSERT_EQ(ERROR_SUCCESS, |
62 REG_SZ, NULL, 0)); | 52 RegSetValueEx(key.Handle(), kNameEmpty, NULL, REG_SZ, NULL, 0)); |
63 | 53 |
64 scoped_ptr<DeleteRegValueWorkItem> work_item1( | 54 scoped_ptr<DeleteRegValueWorkItem> work_item1( |
65 WorkItem::CreateDeleteRegValueWorkItem( | 55 WorkItem::CreateDeleteRegValueWorkItem( |
66 HKEY_CURRENT_USER, parent_key, WorkItem::kWow64Default, name_str)); | 56 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameStr)); |
67 scoped_ptr<DeleteRegValueWorkItem> work_item2( | 57 scoped_ptr<DeleteRegValueWorkItem> work_item2( |
68 WorkItem::CreateDeleteRegValueWorkItem( | 58 WorkItem::CreateDeleteRegValueWorkItem( |
69 HKEY_CURRENT_USER, parent_key, WorkItem::kWow64Default, name_dword)); | 59 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameDword)); |
70 scoped_ptr<DeleteRegValueWorkItem> work_item3( | 60 scoped_ptr<DeleteRegValueWorkItem> work_item3( |
71 WorkItem::CreateDeleteRegValueWorkItem( | 61 WorkItem::CreateDeleteRegValueWorkItem( |
72 HKEY_CURRENT_USER, parent_key, WorkItem::kWow64Default, name_empty)); | 62 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameEmpty)); |
| 63 |
| 64 EXPECT_TRUE(key.HasValue(kNameStr)); |
| 65 EXPECT_TRUE(key.HasValue(kNameDword)); |
| 66 EXPECT_TRUE(key.HasValue(kNameEmpty)); |
73 | 67 |
74 EXPECT_TRUE(work_item1->Do()); | 68 EXPECT_TRUE(work_item1->Do()); |
75 EXPECT_TRUE(work_item2->Do()); | 69 EXPECT_TRUE(work_item2->Do()); |
76 EXPECT_TRUE(work_item3->Do()); | 70 EXPECT_TRUE(work_item3->Do()); |
77 | 71 |
78 EXPECT_FALSE(key.HasValue(name_str.c_str())); | 72 EXPECT_FALSE(key.HasValue(kNameStr)); |
79 EXPECT_FALSE(key.HasValue(name_dword.c_str())); | 73 EXPECT_FALSE(key.HasValue(kNameDword)); |
80 EXPECT_FALSE(key.HasValue(name_empty.c_str())); | 74 EXPECT_FALSE(key.HasValue(kNameEmpty)); |
81 | 75 |
82 work_item1->Rollback(); | 76 work_item1->Rollback(); |
83 work_item2->Rollback(); | 77 work_item2->Rollback(); |
84 work_item3->Rollback(); | 78 work_item3->Rollback(); |
85 | 79 |
86 std::wstring read_str; | 80 EXPECT_TRUE(key.HasValue(kNameStr)); |
| 81 EXPECT_TRUE(key.HasValue(kNameDword)); |
| 82 EXPECT_TRUE(key.HasValue(kNameEmpty)); |
| 83 |
| 84 base::string16 read_str; |
87 DWORD read_dword; | 85 DWORD read_dword; |
88 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name_str.c_str(), &read_str)); | 86 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(kNameStr, &read_str)); |
89 EXPECT_EQ(ERROR_SUCCESS, key.ReadValueDW(name_dword.c_str(), &read_dword)); | 87 EXPECT_EQ(ERROR_SUCCESS, key.ReadValueDW(kNameDword, &read_dword)); |
90 EXPECT_EQ(read_str, data_str); | 88 EXPECT_EQ(read_str, data_str); |
91 EXPECT_EQ(read_dword, data_dword); | 89 EXPECT_EQ(read_dword, data_dword); |
92 | 90 |
93 // Verify empty value. | 91 // Verify empty value. |
94 DWORD type = 0; | 92 DWORD type = 0; |
95 DWORD size = 0; | 93 DWORD size = 0; |
96 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(name_empty.c_str(), NULL, &size, | 94 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(kNameEmpty, NULL, &size, &type)); |
97 &type)); | |
98 EXPECT_EQ(REG_SZ, type); | 95 EXPECT_EQ(REG_SZ, type); |
99 EXPECT_EQ(0, size); | 96 EXPECT_EQ(0, size); |
100 } | 97 } |
101 | 98 |
102 // Try deleting a value that doesn't exist. | 99 // Try deleting a value that doesn't exist. |
103 TEST_F(DeleteRegValueWorkItemTest, DeleteNonExistentValue) { | 100 TEST_F(DeleteRegValueWorkItemTest, DeleteNonExistentValue) { |
104 RegKey key; | 101 RegKey key; |
105 std::wstring parent_key(kTestRoot); | |
106 parent_key.push_back(base::FilePath::kSeparators[0]); | |
107 parent_key.append(kWriteNew); | |
108 ASSERT_EQ(ERROR_SUCCESS, | 102 ASSERT_EQ(ERROR_SUCCESS, |
109 key.Create(HKEY_CURRENT_USER, parent_key.c_str(), KEY_READ | KEY_WRITE)); | 103 key.Create(HKEY_CURRENT_USER, kTestKey, KEY_READ | KEY_WRITE)); |
110 std::wstring name_str(kNameStr); | 104 EXPECT_FALSE(key.HasValue(kNameStr)); |
111 std::wstring name_dword(kNameDword); | 105 EXPECT_FALSE(key.HasValue(kNameDword)); |
112 EXPECT_FALSE(key.HasValue(name_str.c_str())); | |
113 EXPECT_FALSE(key.HasValue(name_dword.c_str())); | |
114 | 106 |
115 scoped_ptr<DeleteRegValueWorkItem> work_item1( | 107 scoped_ptr<DeleteRegValueWorkItem> work_item1( |
116 WorkItem::CreateDeleteRegValueWorkItem( | 108 WorkItem::CreateDeleteRegValueWorkItem( |
117 HKEY_CURRENT_USER, parent_key, WorkItem::kWow64Default, name_str)); | 109 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameStr)); |
118 scoped_ptr<DeleteRegValueWorkItem> work_item2( | 110 scoped_ptr<DeleteRegValueWorkItem> work_item2( |
119 WorkItem::CreateDeleteRegValueWorkItem( | 111 WorkItem::CreateDeleteRegValueWorkItem( |
120 HKEY_CURRENT_USER, parent_key, WorkItem::kWow64Default, name_dword)); | 112 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameDword)); |
121 | 113 |
122 EXPECT_TRUE(work_item1->Do()); | 114 EXPECT_TRUE(work_item1->Do()); |
123 EXPECT_TRUE(work_item2->Do()); | 115 EXPECT_TRUE(work_item2->Do()); |
124 | 116 |
125 EXPECT_FALSE(key.HasValue(name_str.c_str())); | 117 EXPECT_FALSE(key.HasValue(kNameStr)); |
126 EXPECT_FALSE(key.HasValue(name_dword.c_str())); | 118 EXPECT_FALSE(key.HasValue(kNameDword)); |
127 | 119 |
128 work_item1->Rollback(); | 120 work_item1->Rollback(); |
129 work_item2->Rollback(); | 121 work_item2->Rollback(); |
130 | 122 |
131 EXPECT_FALSE(key.HasValue(name_str.c_str())); | 123 EXPECT_FALSE(key.HasValue(kNameStr)); |
132 EXPECT_FALSE(key.HasValue(name_dword.c_str())); | 124 EXPECT_FALSE(key.HasValue(kNameDword)); |
133 } | 125 } |
| 126 |
| 127 // Try deleting a value whose key doesn't even exist. |
| 128 TEST_F(DeleteRegValueWorkItemTest, DeleteValueInNonExistentKey) { |
| 129 RegKey key; |
| 130 // Confirm the key doesn't exist. |
| 131 ASSERT_EQ(ERROR_FILE_NOT_FOUND, |
| 132 key.Open(HKEY_CURRENT_USER, kTestKey, KEY_READ)); |
| 133 |
| 134 scoped_ptr<DeleteRegValueWorkItem> work_item1( |
| 135 WorkItem::CreateDeleteRegValueWorkItem( |
| 136 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameStr)); |
| 137 scoped_ptr<DeleteRegValueWorkItem> work_item2( |
| 138 WorkItem::CreateDeleteRegValueWorkItem( |
| 139 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameDword)); |
| 140 |
| 141 EXPECT_TRUE(work_item1->Do()); |
| 142 EXPECT_TRUE(work_item2->Do()); |
| 143 |
| 144 EXPECT_EQ(ERROR_FILE_NOT_FOUND, |
| 145 key.Open(HKEY_CURRENT_USER, kTestKey, KEY_READ)); |
| 146 |
| 147 work_item1->Rollback(); |
| 148 work_item2->Rollback(); |
| 149 |
| 150 EXPECT_EQ(ERROR_FILE_NOT_FOUND, |
| 151 key.Open(HKEY_CURRENT_USER, kTestKey, KEY_READ)); |
| 152 } |
OLD | NEW |