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

Side by Side Diff: rlz/test/rlz_test_helpers.cc

Issue 63153009: Use RegistryOverrideManager properly in RLZ tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comment fix Created 7 years, 1 month 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
« no previous file with comments | « rlz/test/rlz_test_helpers.h ('k') | rlz/win/dll/exports.cc » ('j') | 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) 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 // Main entry point for all unit tests. 5 // Main entry point for all unit tests.
6 6
7 #include "rlz_test_helpers.h" 7 #include "rlz_test_helpers.h"
8 8
9 #include <map>
10 #include <vector>
11
12 #include "base/strings/string16.h"
9 #include "rlz/lib/rlz_lib.h" 13 #include "rlz/lib/rlz_lib.h"
10 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
11 15
12 #if defined(OS_WIN) 16 #if defined(OS_WIN)
13 #include <shlwapi.h> 17 #include <shlwapi.h>
14 #include "base/win/registry.h" 18 #include "base/win/registry.h"
15 #include "rlz/win/lib/rlz_lib.h" 19 #include "base/win/windows_version.h"
16 #elif defined(OS_POSIX) 20 #elif defined(OS_POSIX)
17 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
18 #include "rlz/lib/rlz_value_store.h" 22 #include "rlz/lib/rlz_value_store.h"
19 #endif 23 #endif
20 24
21 #if defined(OS_WIN) 25 #if defined(OS_WIN)
26
22 namespace { 27 namespace {
23 28
24 const wchar_t* kHKCUReplacement = L"Software\\Google\\RlzUtilUnittest\\HKCU"; 29 // Path to recursively copy into the replacemment hives. These are needed
25 const wchar_t* kHKLMReplacement = L"Software\\Google\\RlzUtilUnittest\\HKLM"; 30 // to make sure certain win32 APIs continue to run correctly once the real
31 // hives are replaced.
32 const wchar_t kHKLMAccessProviders[] =
33 L"System\\CurrentControlSet\\Control\\Lsa\\AccessProviders";
26 34
27 void OverrideRegistryHives() { 35 struct RegistryValue {
28 // Wipe the keys we redirect to. 36 string16 name;
29 // This gives us a stable run, even in the presence of previous 37 DWORD type;
30 // crashes or failures. 38 std::vector<uint8> data;
31 LSTATUS err = SHDeleteKey(HKEY_CURRENT_USER, kHKCUReplacement); 39 };
32 EXPECT_TRUE(err == ERROR_SUCCESS || err == ERROR_FILE_NOT_FOUND);
33 err = SHDeleteKey(HKEY_CURRENT_USER, kHKLMReplacement);
34 EXPECT_TRUE(err == ERROR_SUCCESS || err == ERROR_FILE_NOT_FOUND);
35 40
36 // Create the keys we're redirecting HKCU and HKLM to. 41 struct RegistryKeyData {
37 base::win::RegKey hkcu; 42 std::vector<RegistryValue> values;
38 base::win::RegKey hklm; 43 std::map<string16, RegistryKeyData> keys;
39 ASSERT_EQ(ERROR_SUCCESS, 44 };
40 hkcu.Create(HKEY_CURRENT_USER, kHKCUReplacement, KEY_READ));
41 ASSERT_EQ(ERROR_SUCCESS,
42 hklm.Create(HKEY_CURRENT_USER, kHKLMReplacement, KEY_READ));
43 45
44 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu); 46 void ReadRegistryTree(const base::win::RegKey& src, RegistryKeyData* data) {
47 // First read values.
48 {
49 base::win::RegistryValueIterator i(src.Handle(), L"");
50 data->values.clear();
51 data->values.reserve(i.ValueCount());
52 for (; i.Valid(); ++i) {
53 RegistryValue& value = *data->values.insert(data->values.end(),
54 RegistryValue());
55 const uint8* data = reinterpret_cast<const uint8*>(i.Value());
56 value.name.assign(i.Name());
57 value.type = i.Type();
58 value.data.assign(data, data + i.ValueSize());
59 }
60 }
45 61
46 // And do the switcharoo. 62 // Next read subkeys recursively.
47 ASSERT_EQ(ERROR_SUCCESS, 63 for (base::win::RegistryKeyIterator i(src.Handle(), L"");
48 ::RegOverridePredefKey(HKEY_CURRENT_USER, hkcu.Handle())); 64 i.Valid(); ++i) {
49 ASSERT_EQ(ERROR_SUCCESS, 65 ReadRegistryTree(base::win::RegKey(src.Handle(), i.Name(), KEY_READ),
50 ::RegOverridePredefKey(HKEY_LOCAL_MACHINE, hklm.Handle())); 66 &data->keys[string16(i.Name())]);
67 }
51 } 68 }
52 69
53 void UndoOverrideRegistryHives() { 70 void WriteRegistryTree(const RegistryKeyData& data, base::win::RegKey* dest) {
54 // Undo the redirection. 71 // First write values.
55 EXPECT_EQ(ERROR_SUCCESS, ::RegOverridePredefKey(HKEY_CURRENT_USER, NULL)); 72 for (size_t i = 0; i < data.values.size(); ++i) {
56 EXPECT_EQ(ERROR_SUCCESS, ::RegOverridePredefKey(HKEY_LOCAL_MACHINE, NULL)); 73 const RegistryValue& value = data.values[i];
74 dest->WriteValue(value.name.c_str(),
75 value.data.size() ? &value.data[0] : NULL,
76 static_cast<DWORD>(value.data.size()),
77 value.type);
78 }
79
80 // Next write values recursively.
81 for (std::map<string16, RegistryKeyData>::const_iterator iter =
82 data.keys.begin();
83 iter != data.keys.end(); ++iter) {
84 WriteRegistryTree(iter->second,
85 &base::win::RegKey(dest->Handle(), iter->first.c_str(),
86 KEY_ALL_ACCESS));
87 }
88 }
89
90 // Initialize temporary HKLM/HKCU registry hives used for testing.
91 // Testing RLZ requires reading and writing to the Windows registry. To keep
92 // the tests isolated from the machine's state, as well as to prevent the tests
93 // from causing side effects in the registry, HKCU and HKLM are overridden for
94 // the duration of the tests. RLZ tests don't expect the HKCU and KHLM hives to
95 // be empty though, and this function initializes the minimum value needed so
96 // that the test will run successfully.
97 void InitializeRegistryOverridesForTesting(
98 registry_util::RegistryOverrideManager* override_manager) {
99 // For the moment, the HKCU hive requires no initialization.
100 const bool do_copy = (base::win::GetVersion() >= base::win::VERSION_WIN7);
101 RegistryKeyData data;
102
103 if (do_copy) {
104 // Copy the following HKLM subtrees to the temporary location so that the
105 // win32 APIs used by the tests continue to work:
106 //
107 // HKLM\System\CurrentControlSet\Control\Lsa\AccessProviders
108 //
109 // This seems to be required since Win7.
110 ReadRegistryTree(base::win::RegKey(HKEY_LOCAL_MACHINE,
111 kHKLMAccessProviders,
112 KEY_READ), &data);
113 }
114
115 override_manager->OverrideRegistry(HKEY_LOCAL_MACHINE, L"rlz_temp_hklm");
116 override_manager->OverrideRegistry(HKEY_CURRENT_USER, L"rlz_temp_hkcu");
117
118 if (do_copy) {
119 WriteRegistryTree(data, &base::win::RegKey(HKEY_LOCAL_MACHINE,
120 kHKLMAccessProviders,
121 KEY_ALL_ACCESS));
122 }
57 } 123 }
58 124
59 } // namespace 125 } // namespace
126
60 #endif // defined(OS_WIN) 127 #endif // defined(OS_WIN)
61 128
62 void RlzLibTestNoMachineState::SetUp() { 129 void RlzLibTestNoMachineState::SetUp() {
63 #if defined(OS_WIN) 130 #if defined(OS_WIN)
64 OverrideRegistryHives(); 131 InitializeRegistryOverridesForTesting(&override_manager_);
65 #elif defined(OS_MACOSX) 132 #elif defined(OS_MACOSX)
66 base::mac::ScopedNSAutoreleasePool pool; 133 base::mac::ScopedNSAutoreleasePool pool;
67 #endif // defined(OS_WIN) 134 #endif // defined(OS_WIN)
68 #if defined(OS_POSIX) 135 #if defined(OS_POSIX)
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 136 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
70 rlz_lib::testing::SetRlzStoreDirectory(temp_dir_.path()); 137 rlz_lib::testing::SetRlzStoreDirectory(temp_dir_.path());
71 #endif // defined(OS_POSIX) 138 #endif // defined(OS_POSIX)
72 } 139 }
73 140
74 void RlzLibTestNoMachineState::TearDown() { 141 void RlzLibTestNoMachineState::TearDown() {
75 #if defined(OS_WIN) 142 #if defined(OS_POSIX)
76 UndoOverrideRegistryHives();
77 #elif defined(OS_POSIX)
78 rlz_lib::testing::SetRlzStoreDirectory(base::FilePath()); 143 rlz_lib::testing::SetRlzStoreDirectory(base::FilePath());
79 #endif // defined(OS_WIN) 144 #endif // defined(OS_POSIX)
80 } 145 }
81 146
82 void RlzLibTestBase::SetUp() { 147 void RlzLibTestBase::SetUp() {
83 RlzLibTestNoMachineState::SetUp(); 148 RlzLibTestNoMachineState::SetUp();
84 #if defined(OS_WIN) 149 #if defined(OS_WIN)
85 rlz_lib::CreateMachineState(); 150 rlz_lib::CreateMachineState();
86 #endif // defined(OS_WIN) 151 #endif // defined(OS_WIN)
87 } 152 }
OLDNEW
« no previous file with comments | « rlz/test/rlz_test_helpers.h ('k') | rlz/win/dll/exports.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698