Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1179)

Side by Side Diff: base/win/registry_unittest.cc

Issue 275103012: Add WOW64 support and DeleteEmptyKey to base::win::registry. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: code review changes Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« base/win/registry.cc ('K') | « base/win/registry.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 "base/win/registry.h" 5 #include "base/win/registry.h"
6 6
7 #include <cstring> 7 #include <cstring>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/win/windows_version.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace base { 15 namespace base {
15 namespace win { 16 namespace win {
16 17
17 namespace { 18 namespace {
18 19
19 const wchar_t kRootKey[] = L"Base_Registry_Unittest"; 20 const wchar_t kRootKey[] = L"Base_Registry_Unittest";
20 21
22 #if defined(_WIN64)
23 const REGSAM kNativeViewMask = KEY_WOW64_64KEY;
grt (UTC plus 2) 2014/05/17 19:31:53 make these static const protected members of the t
Will Harris 2014/05/19 21:49:03 Done.
24 const REGSAM kRedirectedViewMask = KEY_WOW64_32KEY;
25 #else
26 const REGSAM kNativeViewMask = KEY_WOW64_32KEY;
27 const REGSAM kRedirectedViewMask = KEY_WOW64_64KEY;
28 #endif // _WIN64
29
21 class RegistryTest : public testing::Test { 30 class RegistryTest : public testing::Test {
22 public: 31 public:
23 RegistryTest() {} 32 RegistryTest() {
grt (UTC plus 2) 2014/05/17 19:31:53 while you're here, please move this ctor into the
Will Harris 2014/05/19 21:49:03 Done.
33 foo_software_key_ = L"Software\\";
grt (UTC plus 2) 2014/05/17 19:31:53 may as well do this work in SetUp so that the fixt
Will Harris 2014/05/19 21:49:03 Done.
34 foo_software_key_ += kRootKey;
35 foo_software_key_ += L"\\Foo";
36 foo_software_wow64_key_ = L"Software\\Wow6432Node\\";
37 foo_software_wow64_key_ += kRootKey;
38 foo_software_wow64_key_ += L"\\Foo";
39 }
24 40
25 protected: 41 protected:
26 virtual void SetUp() OVERRIDE { 42 virtual void SetUp() OVERRIDE {
27 // Create a temporary key. 43 // Create a temporary key.
28 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS); 44 RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
29 key.DeleteKey(kRootKey); 45 key.DeleteKey(kRootKey);
30 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_READ)); 46 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_READ));
31 ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, kRootKey, KEY_READ)); 47 ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, kRootKey, KEY_READ));
32 } 48 }
33 49
34 virtual void TearDown() OVERRIDE { 50 virtual void TearDown() OVERRIDE {
35 // Clean up the temporary key. 51 // Clean up the temporary key.
36 RegKey key(HKEY_CURRENT_USER, L"", KEY_SET_VALUE); 52 RegKey key(HKEY_CURRENT_USER, L"", KEY_SET_VALUE);
37 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey)); 53 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey));
54 ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_READ));
55
56 // Delete all variations of the Software key we created.
57 ASSERT_EQ(
58 ERROR_SUCCESS,
59 key.Open(
60 HKEY_LOCAL_MACHINE, L"Software", KEY_SET_VALUE | KEY_WOW64_32KEY));
61 key.DeleteKey(kRootKey);
grt (UTC plus 2) 2014/05/17 19:31:53 ? EXPECT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootK
62
63 ASSERT_EQ(
64 ERROR_SUCCESS,
65 key.Open(
66 HKEY_LOCAL_MACHINE, L"Software", KEY_SET_VALUE | KEY_WOW64_64KEY));
67 key.DeleteKey(kRootKey);
68 }
69
70 std::wstring foo_software_key_;
grt (UTC plus 2) 2014/05/17 19:31:53 move these down to the end of the protected: secti
Will Harris 2014/05/19 21:49:03 Done.
71 std::wstring foo_software_wow64_key_;
72
73 static bool IsRedirectorPresent() {
74 #if defined(_WIN64)
75 return true;
76 #else
77 OSInfo* os_info = OSInfo::GetInstance();
78
79 return os_info->wow64_status() == OSInfo::WOW64_ENABLED;
grt (UTC plus 2) 2014/05/17 19:31:53 return OSInfo::GetInstance()->wow64_status() == OS
Will Harris 2014/05/19 21:49:03 Done.
80 #endif
38 } 81 }
39 82
40 private: 83 private:
41 DISALLOW_COPY_AND_ASSIGN(RegistryTest); 84 DISALLOW_COPY_AND_ASSIGN(RegistryTest);
42 }; 85 };
43 86
44 TEST_F(RegistryTest, ValueTest) { 87 TEST_F(RegistryTest, ValueTest) {
45 RegKey key; 88 RegKey key;
46 89
47 std::wstring foo_key(kRootKey); 90 std::wstring foo_key(kRootKey);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 ASSERT_EQ(arraysize(kData), iterator.ValueSize()); 194 ASSERT_EQ(arraysize(kData), iterator.ValueSize());
152 // Value() is NUL terminated. 195 // Value() is NUL terminated.
153 int end = (iterator.ValueSize() + sizeof(wchar_t) - 1) / sizeof(wchar_t); 196 int end = (iterator.ValueSize() + sizeof(wchar_t) - 1) / sizeof(wchar_t);
154 EXPECT_NE(L'\0', iterator.Value()[end-1]); 197 EXPECT_NE(L'\0', iterator.Value()[end-1]);
155 EXPECT_EQ(L'\0', iterator.Value()[end]); 198 EXPECT_EQ(L'\0', iterator.Value()[end]);
156 EXPECT_EQ(0, std::memcmp(kData, iterator.Value(), arraysize(kData))); 199 EXPECT_EQ(0, std::memcmp(kData, iterator.Value(), arraysize(kData)));
157 ++iterator; 200 ++iterator;
158 EXPECT_FALSE(iterator.Valid()); 201 EXPECT_FALSE(iterator.Valid());
159 } 202 }
160 203
204 TEST_F(RegistryTest, RecursiveDelete) {
205 RegKey key;
206 // Create kRootKey->Foo
207 // \->Bar
208 // \->Foo
209 // \->Moo
210 // \->Foo
211 // and delete kRootKey->Foo
212 std::wstring foo_key(kRootKey);
213 foo_key += L"\\Foo";
214 ASSERT_EQ(ERROR_SUCCESS,
215 key.Create(HKEY_CURRENT_USER, foo_key.c_str(), KEY_WRITE));
216 foo_key += L"\\Bar";
217
218 ASSERT_NE(ERROR_SUCCESS,
219 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ));
220 ASSERT_EQ(ERROR_SUCCESS, key.CreateKey(L"Bar", KEY_WRITE));
221 ASSERT_EQ(ERROR_SUCCESS, key.CreateKey(L"Moo", KEY_WRITE));
222 ASSERT_EQ(ERROR_SUCCESS, key.CreateKey(L"Foo", KEY_WRITE));
223 ASSERT_EQ(ERROR_SUCCESS,
224 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_WRITE));
225 foo_key += L"\\Foo";
226 ASSERT_EQ(ERROR_SUCCESS, key.CreateKey(L"Foo", KEY_WRITE));
227 ASSERT_EQ(ERROR_SUCCESS,
228 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ));
229
230 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_WRITE));
231 ASSERT_NE(ERROR_SUCCESS, key.DeleteKey(L"Bar"));
232 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(L"Foo"));
233 ASSERT_NE(ERROR_SUCCESS, key.DeleteKey(L"Foo"));
234 ASSERT_NE(ERROR_SUCCESS,
235 key.Open(HKEY_CURRENT_USER, foo_key.c_str(), KEY_READ));
236 }
237
238 // This test requires running as an Administrator as it tests redirected
239 // registry writes to HKLM\Software
240 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384253.aspx
241 TEST_F(RegistryTest, Wow64RedirectedFromNative) {
242 if (IsRedirectorPresent()) {
grt (UTC plus 2) 2014/05/17 19:31:53 please have these early-exit in the case where the
Will Harris 2014/05/19 21:49:03 Done.
243 RegKey key;
244
245 // Test redirected key access from non-redirected.
246 ASSERT_EQ(ERROR_SUCCESS,
247 key.Create(HKEY_LOCAL_MACHINE,
248 foo_software_key_.c_str(),
249 KEY_WRITE | kRedirectedViewMask));
250 ASSERT_NE(ERROR_SUCCESS,
251 key.Open(HKEY_LOCAL_MACHINE,
252 foo_software_key_.c_str(),
253 KEY_READ));
254 ASSERT_NE(ERROR_SUCCESS,
255 key.Open(HKEY_LOCAL_MACHINE,
256 foo_software_key_.c_str(),
257 KEY_READ | kNativeViewMask));
258
259 // Open the non-redirected view of the parent and try to delete the test
260 // key.
261 ASSERT_EQ(ERROR_SUCCESS,
262 key.Open(HKEY_LOCAL_MACHINE,
263 L"Software",
264 KEY_SET_VALUE));
265 ASSERT_NE(ERROR_SUCCESS, key.DeleteKey(kRootKey));
266 ASSERT_EQ(ERROR_SUCCESS,
267 key.Open(HKEY_LOCAL_MACHINE,
268 L"Software",
269 KEY_SET_VALUE | kNativeViewMask));
270 ASSERT_NE(ERROR_SUCCESS, key.DeleteKey(kRootKey));
271
grt (UTC plus 2) 2014/05/17 19:31:53 // Open the redirected view and delete the key cre
Will Harris 2014/05/19 21:49:03 Done.
272 ASSERT_EQ(ERROR_SUCCESS,
273 key.Open(HKEY_LOCAL_MACHINE,
274 L"Software",
275 KEY_SET_VALUE | kRedirectedViewMask));
276 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey));
277 }
278 }
279
280 TEST_F(RegistryTest, Wow64NativeFromRedirected) {
281 if (IsRedirectorPresent()) {
282 RegKey key;
283
284 // Test non-redirected key access from redirected.
285 ASSERT_EQ(ERROR_SUCCESS,
286 key.Create(HKEY_LOCAL_MACHINE,
287 foo_software_key_.c_str(),
288 KEY_WRITE | kNativeViewMask));
289 ASSERT_EQ(ERROR_SUCCESS,
290 key.Open(HKEY_LOCAL_MACHINE,
291 foo_software_key_.c_str(),
292 KEY_READ));
293 ASSERT_NE(ERROR_SUCCESS,
294 key.Open(HKEY_LOCAL_MACHINE,
295 foo_software_key_.c_str(),
296 KEY_READ | kRedirectedViewMask));
297
298 // Open the redirected view of the parent and try to delete the test key
299 // from the non-redirected view.
300 ASSERT_EQ(ERROR_SUCCESS,
301 key.Open(HKEY_LOCAL_MACHINE,
302 L"Software",
303 KEY_SET_VALUE | kRedirectedViewMask));
304 ASSERT_NE(ERROR_SUCCESS, key.DeleteKey(kRootKey));
305
306 ASSERT_EQ(ERROR_SUCCESS,
307 key.Open(HKEY_LOCAL_MACHINE,
308 L"Software",
309 KEY_SET_VALUE | kNativeViewMask));
310 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey));
311 }
312 }
313
314 TEST_F(RegistryTest, Wow6432NodeFromRedirected) {
315 if (IsRedirectorPresent()) {
316 RegKey key;
317 // Test access to 32-bit values on 64-bit via the Wow6432Node key.
grt (UTC plus 2) 2014/05/17 19:31:53 is this worth testing? i thought Wow6432Node was a
Will Harris 2014/05/19 21:49:03 earlier tests just confirm that when KEY_WOW64_32K
318 ASSERT_EQ(ERROR_SUCCESS,
319 key.Create(HKEY_LOCAL_MACHINE,
320 foo_software_key_.c_str(),
321 KEY_WRITE | KEY_WOW64_32KEY));
322 ASSERT_EQ(ERROR_SUCCESS,
323 key.Open(HKEY_LOCAL_MACHINE,
324 foo_software_wow64_key_.c_str(),
325 KEY_READ));
326 ASSERT_EQ(ERROR_SUCCESS,
327 key.Open(HKEY_LOCAL_MACHINE,
328 foo_software_wow64_key_.c_str(),
329 KEY_READ | KEY_WOW64_64KEY));
330 ASSERT_EQ(ERROR_SUCCESS,
331 key.Open(HKEY_LOCAL_MACHINE,
332 foo_software_wow64_key_.c_str(),
333 KEY_READ | KEY_WOW64_32KEY));
334 ASSERT_EQ(ERROR_SUCCESS,
335 key.Open(HKEY_LOCAL_MACHINE,
336 L"Software",
337 KEY_SET_VALUE | KEY_WOW64_32KEY));
338 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey));
339 }
340 }
341
342 TEST_F(RegistryTest, NativeNotRedirected) {
343 if (!IsRedirectorPresent()) {
344 RegKey key;
345 // 32-bit on 32-bit shouldn't even see a Wow6432Node key.
grt (UTC plus 2) 2014/05/17 19:31:53 this also seems to be testing an implementation de
Will Harris 2014/05/19 21:49:03 Happy for this to be removed.
346 ASSERT_EQ(ERROR_SUCCESS,
347 key.Create(HKEY_LOCAL_MACHINE,
348 foo_software_key_.c_str(),
349 KEY_READ));
350 ASSERT_NE(ERROR_SUCCESS,
351 key.Open(HKEY_LOCAL_MACHINE,
352 foo_software_wow64_key_.c_str(),
353 KEY_READ));
354 ASSERT_EQ(ERROR_SUCCESS,
355 key.Open(HKEY_LOCAL_MACHINE, L"Software", KEY_SET_VALUE));
356 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey));
357 }
358 }
359
161 } // namespace 360 } // namespace
162 361
163 } // namespace win 362 } // namespace win
164 } // namespace base 363 } // namespace base
OLDNEW
« base/win/registry.cc ('K') | « base/win/registry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698