OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "chrome/browser/chrome_elf_init_win.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/metrics/field_trial.h" | |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/test/test_reg_util_win.h" | |
14 #include "chrome/common/chrome_version.h" | |
15 #include "chrome_elf/chrome_elf_constants.h" | |
16 #include "components/variations/entropy_provider.h" | |
17 #include "components/variations/variations_associated_data.h" | |
18 #include "components/version_info/version_info.h" | |
19 #include "content/public/test/test_browser_thread_bundle.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace { | |
23 | |
24 const char kBrowserBlacklistTrialEnabledGroupName[] = "Enabled"; | |
25 | |
26 class ChromeBlacklistTrialTest : public testing::Test { | |
27 protected: | |
28 ChromeBlacklistTrialTest() {} | |
29 ~ChromeBlacklistTrialTest() override {} | |
30 | |
31 void SetUp() override { | |
32 testing::Test::SetUp(); | |
33 | |
34 override_manager_.OverrideRegistry(HKEY_CURRENT_USER); | |
35 | |
36 blacklist_registry_key_.reset( | |
37 new base::win::RegKey(HKEY_CURRENT_USER, | |
38 blacklist::kRegistryBeaconPath, | |
39 KEY_QUERY_VALUE | KEY_SET_VALUE)); | |
40 } | |
41 | |
42 DWORD GetBlacklistState() { | |
43 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | |
44 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, | |
45 &blacklist_state); | |
46 | |
47 return blacklist_state; | |
48 } | |
49 | |
50 base::string16 GetBlacklistVersion() { | |
51 base::string16 blacklist_version; | |
52 blacklist_registry_key_->ReadValue(blacklist::kBeaconVersion, | |
53 &blacklist_version); | |
54 | |
55 return blacklist_version; | |
56 } | |
57 | |
58 std::unique_ptr<base::win::RegKey> blacklist_registry_key_; | |
59 registry_util::RegistryOverrideManager override_manager_; | |
60 content::TestBrowserThreadBundle test_browser_thread_bundle_; | |
61 | |
62 private: | |
63 DISALLOW_COPY_AND_ASSIGN(ChromeBlacklistTrialTest); | |
64 }; | |
65 | |
66 // Ensure that the default trial sets up the blacklist beacons. | |
67 TEST_F(ChromeBlacklistTrialTest, DefaultRun) { | |
68 // Set some dummy values as beacons. | |
69 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, | |
70 blacklist::BLACKLIST_DISABLED); | |
71 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, L"Data"); | |
72 | |
73 // This setup code should result in the default group, which should have | |
74 // the blacklist set up. | |
75 InitializeChromeElf(); | |
76 | |
77 // Ensure the beacon values are now correct, indicating the | |
78 // blacklist beacon was setup. | |
79 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); | |
80 base::string16 version(base::UTF8ToUTF16(version_info::GetVersionNumber())); | |
81 ASSERT_EQ(version, GetBlacklistVersion()); | |
82 } | |
83 | |
84 // Ensure that the blacklist is disabled for any users in the | |
85 // "BlacklistDisabled" finch group. | |
86 TEST_F(ChromeBlacklistTrialTest, BlacklistDisabledRun) { | |
87 // Set the beacons to enabled values. | |
88 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, | |
89 blacklist::BLACKLIST_ENABLED); | |
90 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, L"Data"); | |
91 | |
92 // Create the field trial with the blacklist disabled group. | |
93 base::FieldTrialList field_trial_list( | |
94 new metrics::SHA1EntropyProvider("test")); | |
95 | |
96 scoped_refptr<base::FieldTrial> trial( | |
97 base::FieldTrialList::CreateFieldTrial( | |
98 kBrowserBlacklistTrialName, kBrowserBlacklistTrialDisabledGroupName)); | |
99 | |
100 // This setup code should now delete any existing blacklist beacons. | |
101 InitializeChromeElf(); | |
102 | |
103 // Ensure invalid values are returned to indicate that the beacon | |
104 // values are indeed gone. | |
105 ASSERT_EQ(blacklist::BLACKLIST_STATE_MAX, GetBlacklistState()); | |
106 ASSERT_EQ(base::string16(), GetBlacklistVersion()); | |
107 } | |
108 | |
109 TEST_F(ChromeBlacklistTrialTest, VerifyFirstRun) { | |
110 BrowserBlacklistBeaconSetup(); | |
111 | |
112 // Verify the state is properly set after the first run. | |
113 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); | |
114 | |
115 base::string16 version(base::UTF8ToUTF16(version_info::GetVersionNumber())); | |
116 ASSERT_EQ(version, GetBlacklistVersion()); | |
117 } | |
118 | |
119 TEST_F(ChromeBlacklistTrialTest, BlacklistFailed) { | |
120 // Ensure when the blacklist set up failed we set the state to disabled for | |
121 // future runs. | |
122 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, | |
123 TEXT(CHROME_VERSION_STRING)); | |
124 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, | |
125 blacklist::BLACKLIST_SETUP_FAILED); | |
126 | |
127 BrowserBlacklistBeaconSetup(); | |
128 | |
129 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); | |
130 } | |
131 | |
132 TEST_F(ChromeBlacklistTrialTest, VersionChanged) { | |
133 // Mark the blacklist as disabled for an older version, it should | |
134 // get enabled for this new version. Also record a non-zero number of | |
135 // setup failures, which should be reset to zero. | |
136 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, | |
137 L"old_version"); | |
138 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, | |
139 blacklist::BLACKLIST_DISABLED); | |
140 blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount, | |
141 blacklist::kBeaconMaxAttempts); | |
142 | |
143 BrowserBlacklistBeaconSetup(); | |
144 | |
145 // The beacon should now be marked as enabled for the current version. | |
146 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); | |
147 | |
148 base::string16 expected_version( | |
149 base::UTF8ToUTF16(version_info::GetVersionNumber())); | |
150 ASSERT_EQ(expected_version, GetBlacklistVersion()); | |
151 | |
152 // The counter should be reset. | |
153 DWORD attempt_count = blacklist::kBeaconMaxAttempts; | |
154 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount, | |
155 &attempt_count); | |
156 ASSERT_EQ(static_cast<DWORD>(0), attempt_count); | |
157 } | |
158 | |
159 TEST_F(ChromeBlacklistTrialTest, AddFinchBlacklistToRegistry) { | |
160 // Create the field trial with the blacklist enabled group. | |
161 base::FieldTrialList field_trial_list( | |
162 new metrics::SHA1EntropyProvider("test")); | |
163 | |
164 scoped_refptr<base::FieldTrial> trial(base::FieldTrialList::CreateFieldTrial( | |
165 kBrowserBlacklistTrialName, kBrowserBlacklistTrialEnabledGroupName)); | |
166 | |
167 // Set up the trial with the desired parameters. | |
168 std::map<std::string, std::string> desired_params; | |
169 desired_params["TestDllName1"] = "TestDll1.dll"; | |
170 desired_params["TestDllName2"] = "TestDll2.dll"; | |
171 | |
172 variations::AssociateVariationParams( | |
173 kBrowserBlacklistTrialName, | |
174 kBrowserBlacklistTrialEnabledGroupName, | |
175 desired_params); | |
176 | |
177 // This should add the dlls in those parameters to the registry. | |
178 AddFinchBlacklistToRegistry(); | |
179 | |
180 // Check that all the values in desired_params were added to the registry. | |
181 base::win::RegKey finch_blacklist_registry_key( | |
182 HKEY_CURRENT_USER, | |
183 blacklist::kRegistryFinchListPath, | |
184 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
185 | |
186 ASSERT_EQ(desired_params.size(), | |
187 finch_blacklist_registry_key.GetValueCount()); | |
188 | |
189 for (std::map<std::string, std::string>::iterator it = desired_params.begin(); | |
190 it != desired_params.end(); | |
191 ++it) { | |
192 std::wstring name = base::UTF8ToWide(it->first); | |
193 ASSERT_TRUE(finch_blacklist_registry_key.HasValue(name.c_str())); | |
194 } | |
195 } | |
196 | |
197 } // namespace | |
OLD | NEW |