Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / installer / util / delete_reg_value_work_item_unittest.cc
blob9a508aec4b23e00e6eb2745134b6b141e4295448
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
3 // found in the LICENSE file.
5 #include <windows.h>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/test/test_reg_util_win.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/util/delete_reg_value_work_item.h"
12 #include "chrome/installer/util/work_item.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::win::RegKey;
17 namespace {
19 const wchar_t kTestKey[] = L"DeleteRegValueWorkItemTest";
20 const wchar_t kNameStr[] = L"name_str";
21 const wchar_t kNameDword[] = L"name_dword";
23 class DeleteRegValueWorkItemTest : public testing::Test {
24 protected:
25 DeleteRegValueWorkItemTest() {}
27 void SetUp() override {
28 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER);
31 private:
32 registry_util::RegistryOverrideManager registry_override_manager_;
34 DISALLOW_COPY_AND_ASSIGN(DeleteRegValueWorkItemTest);
37 } // namespace
39 // Delete a value. The value should get deleted after Do() and should be
40 // recreated after Rollback().
41 TEST_F(DeleteRegValueWorkItemTest, DeleteExistingValue) {
42 RegKey key;
43 ASSERT_EQ(ERROR_SUCCESS,
44 key.Create(HKEY_CURRENT_USER, kTestKey, KEY_READ | KEY_WRITE));
45 const base::string16 data_str(L"data_111");
46 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kNameStr, data_str.c_str()));
47 const DWORD data_dword = 100;
48 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kNameDword, data_dword));
50 static const wchar_t kNameEmpty[](L"name_empty");
51 ASSERT_EQ(ERROR_SUCCESS,
52 RegSetValueEx(key.Handle(), kNameEmpty, NULL, REG_SZ, NULL, 0));
54 scoped_ptr<DeleteRegValueWorkItem> work_item1(
55 WorkItem::CreateDeleteRegValueWorkItem(
56 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameStr));
57 scoped_ptr<DeleteRegValueWorkItem> work_item2(
58 WorkItem::CreateDeleteRegValueWorkItem(
59 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameDword));
60 scoped_ptr<DeleteRegValueWorkItem> work_item3(
61 WorkItem::CreateDeleteRegValueWorkItem(
62 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameEmpty));
64 EXPECT_TRUE(key.HasValue(kNameStr));
65 EXPECT_TRUE(key.HasValue(kNameDword));
66 EXPECT_TRUE(key.HasValue(kNameEmpty));
68 EXPECT_TRUE(work_item1->Do());
69 EXPECT_TRUE(work_item2->Do());
70 EXPECT_TRUE(work_item3->Do());
72 EXPECT_FALSE(key.HasValue(kNameStr));
73 EXPECT_FALSE(key.HasValue(kNameDword));
74 EXPECT_FALSE(key.HasValue(kNameEmpty));
76 work_item1->Rollback();
77 work_item2->Rollback();
78 work_item3->Rollback();
80 EXPECT_TRUE(key.HasValue(kNameStr));
81 EXPECT_TRUE(key.HasValue(kNameDword));
82 EXPECT_TRUE(key.HasValue(kNameEmpty));
84 base::string16 read_str;
85 DWORD read_dword;
86 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(kNameStr, &read_str));
87 EXPECT_EQ(ERROR_SUCCESS, key.ReadValueDW(kNameDword, &read_dword));
88 EXPECT_EQ(read_str, data_str);
89 EXPECT_EQ(read_dword, data_dword);
91 // Verify empty value.
92 DWORD type = 0;
93 DWORD size = 0;
94 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(kNameEmpty, NULL, &size, &type));
95 EXPECT_EQ(REG_SZ, type);
96 EXPECT_EQ(0, size);
99 // Try deleting a value that doesn't exist.
100 TEST_F(DeleteRegValueWorkItemTest, DeleteNonExistentValue) {
101 RegKey key;
102 ASSERT_EQ(ERROR_SUCCESS,
103 key.Create(HKEY_CURRENT_USER, kTestKey, KEY_READ | KEY_WRITE));
104 EXPECT_FALSE(key.HasValue(kNameStr));
105 EXPECT_FALSE(key.HasValue(kNameDword));
107 scoped_ptr<DeleteRegValueWorkItem> work_item1(
108 WorkItem::CreateDeleteRegValueWorkItem(
109 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameStr));
110 scoped_ptr<DeleteRegValueWorkItem> work_item2(
111 WorkItem::CreateDeleteRegValueWorkItem(
112 HKEY_CURRENT_USER, kTestKey, WorkItem::kWow64Default, kNameDword));
114 EXPECT_TRUE(work_item1->Do());
115 EXPECT_TRUE(work_item2->Do());
117 EXPECT_FALSE(key.HasValue(kNameStr));
118 EXPECT_FALSE(key.HasValue(kNameDword));
120 work_item1->Rollback();
121 work_item2->Rollback();
123 EXPECT_FALSE(key.HasValue(kNameStr));
124 EXPECT_FALSE(key.HasValue(kNameDword));
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));
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));
141 EXPECT_TRUE(work_item1->Do());
142 EXPECT_TRUE(work_item2->Do());
144 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
145 key.Open(HKEY_CURRENT_USER, kTestKey, KEY_READ));
147 work_item1->Rollback();
148 work_item2->Rollback();
150 EXPECT_EQ(ERROR_FILE_NOT_FOUND,
151 key.Open(HKEY_CURRENT_USER, kTestKey, KEY_READ));