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.
5 #include "chrome/browser/chrome_elf_init_win.h"
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/test_reg_util_win.h"
13 #include "chrome/common/chrome_version_info.h"
14 #include "chrome_elf/chrome_elf_constants.h"
15 #include "components/variations/entropy_provider.h"
16 #include "components/variations/variations_associated_data.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "version.h" // NOLINT
23 const char kBrowserBlacklistTrialEnabledGroupName
[] = "Enabled";
25 class ChromeBlacklistTrialTest
: public testing::Test
{
27 ChromeBlacklistTrialTest() {}
28 ~ChromeBlacklistTrialTest() override
{}
30 void SetUp() override
{
31 testing::Test::SetUp();
33 override_manager_
.OverrideRegistry(HKEY_CURRENT_USER
);
35 blacklist_registry_key_
.reset(
36 new base::win::RegKey(HKEY_CURRENT_USER
,
37 blacklist::kRegistryBeaconPath
,
38 KEY_QUERY_VALUE
| KEY_SET_VALUE
));
41 DWORD
GetBlacklistState() {
42 DWORD blacklist_state
= blacklist::BLACKLIST_STATE_MAX
;
43 blacklist_registry_key_
->ReadValueDW(blacklist::kBeaconState
,
46 return blacklist_state
;
49 base::string16
GetBlacklistVersion() {
50 base::string16 blacklist_version
;
51 blacklist_registry_key_
->ReadValue(blacklist::kBeaconVersion
,
54 return blacklist_version
;
57 scoped_ptr
<base::win::RegKey
> blacklist_registry_key_
;
58 registry_util::RegistryOverrideManager override_manager_
;
59 content::TestBrowserThreadBundle test_browser_thread_bundle_
;
62 DISALLOW_COPY_AND_ASSIGN(ChromeBlacklistTrialTest
);
65 // Ensure that the default trial sets up the blacklist beacons.
66 TEST_F(ChromeBlacklistTrialTest
, DefaultRun
) {
67 // Set some dummy values as beacons.
68 blacklist_registry_key_
->WriteValue(blacklist::kBeaconState
,
69 blacklist::BLACKLIST_DISABLED
);
70 blacklist_registry_key_
->WriteValue(blacklist::kBeaconVersion
, L
"Data");
72 // This setup code should result in the default group, which should have
73 // the blacklist set up.
74 InitializeChromeElf();
76 // Ensure the beacon values are now correct, indicating the
77 // blacklist beacon was setup.
78 ASSERT_EQ(blacklist::BLACKLIST_ENABLED
, GetBlacklistState());
79 chrome::VersionInfo version_info
;
80 base::string16
version(base::UTF8ToUTF16(version_info
.Version()));
81 ASSERT_EQ(version
, GetBlacklistVersion());
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");
92 // Create the field trial with the blacklist disabled group.
93 base::FieldTrialList
field_trial_list(
94 new metrics::SHA1EntropyProvider("test"));
96 scoped_refptr
<base::FieldTrial
> trial(
97 base::FieldTrialList::CreateFieldTrial(
98 kBrowserBlacklistTrialName
, kBrowserBlacklistTrialDisabledGroupName
));
100 // This setup code should now delete any existing blacklist beacons.
101 InitializeChromeElf();
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());
109 TEST_F(ChromeBlacklistTrialTest
, VerifyFirstRun
) {
110 BrowserBlacklistBeaconSetup();
112 // Verify the state is properly set after the first run.
113 ASSERT_EQ(blacklist::BLACKLIST_ENABLED
, GetBlacklistState());
115 chrome::VersionInfo version_info
;
116 base::string16
version(base::UTF8ToUTF16(version_info
.Version()));
117 ASSERT_EQ(version
, GetBlacklistVersion());
120 TEST_F(ChromeBlacklistTrialTest
, BlacklistFailed
) {
121 // Ensure when the blacklist set up failed we set the state to disabled for
123 blacklist_registry_key_
->WriteValue(blacklist::kBeaconVersion
,
124 TEXT(CHROME_VERSION_STRING
));
125 blacklist_registry_key_
->WriteValue(blacklist::kBeaconState
,
126 blacklist::BLACKLIST_SETUP_FAILED
);
128 BrowserBlacklistBeaconSetup();
130 ASSERT_EQ(blacklist::BLACKLIST_DISABLED
, GetBlacklistState());
133 TEST_F(ChromeBlacklistTrialTest
, VersionChanged
) {
134 // Mark the blacklist as disabled for an older version, it should
135 // get enabled for this new version. Also record a non-zero number of
136 // setup failures, which should be reset to zero.
137 blacklist_registry_key_
->WriteValue(blacklist::kBeaconVersion
,
139 blacklist_registry_key_
->WriteValue(blacklist::kBeaconState
,
140 blacklist::BLACKLIST_DISABLED
);
141 blacklist_registry_key_
->WriteValue(blacklist::kBeaconAttemptCount
,
142 blacklist::kBeaconMaxAttempts
);
144 BrowserBlacklistBeaconSetup();
146 // The beacon should now be marked as enabled for the current version.
147 ASSERT_EQ(blacklist::BLACKLIST_ENABLED
, GetBlacklistState());
149 chrome::VersionInfo version_info
;
150 base::string16
expected_version(base::UTF8ToUTF16(version_info
.Version()));
151 ASSERT_EQ(expected_version
, GetBlacklistVersion());
153 // The counter should be reset.
154 DWORD attempt_count
= blacklist::kBeaconMaxAttempts
;
155 blacklist_registry_key_
->ReadValueDW(blacklist::kBeaconAttemptCount
,
157 ASSERT_EQ(static_cast<DWORD
>(0), attempt_count
);
160 TEST_F(ChromeBlacklistTrialTest
, AddFinchBlacklistToRegistry
) {
161 // Create the field trial with the blacklist enabled group.
162 base::FieldTrialList
field_trial_list(
163 new metrics::SHA1EntropyProvider("test"));
165 scoped_refptr
<base::FieldTrial
> trial(base::FieldTrialList::CreateFieldTrial(
166 kBrowserBlacklistTrialName
, kBrowserBlacklistTrialEnabledGroupName
));
168 // Set up the trial with the desired parameters.
169 std::map
<std::string
, std::string
> desired_params
;
170 desired_params
["TestDllName1"] = "TestDll1.dll";
171 desired_params
["TestDllName2"] = "TestDll2.dll";
173 variations::AssociateVariationParams(
174 kBrowserBlacklistTrialName
,
175 kBrowserBlacklistTrialEnabledGroupName
,
178 // This should add the dlls in those parameters to the registry.
179 AddFinchBlacklistToRegistry();
181 // Check that all the values in desired_params were added to the registry.
182 base::win::RegKey
finch_blacklist_registry_key(
184 blacklist::kRegistryFinchListPath
,
185 KEY_QUERY_VALUE
| KEY_SET_VALUE
);
187 ASSERT_EQ(desired_params
.size(),
188 finch_blacklist_registry_key
.GetValueCount());
190 for (std::map
<std::string
, std::string
>::iterator it
= desired_params
.begin();
191 it
!= desired_params
.end();
193 std::wstring name
= base::UTF8ToWide(it
->first
);
194 ASSERT_TRUE(finch_blacklist_registry_key
.HasValue(name
.c_str()));