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/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/test_reg_util_win.h"
11 #include "chrome/common/chrome_version_info.h"
12 #include "chrome_elf/blacklist/blacklist.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 class ChromeBlacklistTrialTest
: public testing::Test
{
17 ChromeBlacklistTrialTest() :
18 blacklist_registry_key_(HKEY_CURRENT_USER
,
19 blacklist::kRegistryBeaconPath
,
20 KEY_QUERY_VALUE
| KEY_SET_VALUE
) {}
22 virtual void SetUp() OVERRIDE
{
23 testing::Test::SetUp();
25 registry_util::RegistryOverrideManager override_manager
;
26 override_manager
.OverrideRegistry(HKEY_CURRENT_USER
,
27 L
"browser_blacklist_test");
32 DWORD
GetBlacklistState() {
33 DWORD blacklist_state
= blacklist::BLACKLIST_STATE_MAX
;
34 blacklist_registry_key_
.ReadValueDW(blacklist::kBeaconState
,
37 return blacklist_state
;
40 base::string16
GetBlacklistVersion() {
41 base::string16 blacklist_version
;
42 blacklist_registry_key_
.ReadValue(blacklist::kBeaconVersion
,
45 return blacklist_version
;
48 base::win::RegKey blacklist_registry_key_
;
51 DISALLOW_COPY_AND_ASSIGN(ChromeBlacklistTrialTest
);
55 // Ensure that the default trial deletes any existing blacklist beacons.
56 TEST_F(ChromeBlacklistTrialTest
, DefaultRun
) {
57 // Set some dummy values as beacons.
58 blacklist_registry_key_
.WriteValue(blacklist::kBeaconState
,
59 blacklist::BLACKLIST_ENABLED
);
60 blacklist_registry_key_
.WriteValue(blacklist::kBeaconVersion
, L
"Data");
62 // This setup code should result in the default group, which should remove
63 // all the beacon values.
64 InitializeChromeElf();
66 // Ensure that invalid values are returned to indicate that the
67 // beacon values are gone.
68 ASSERT_EQ(blacklist::BLACKLIST_STATE_MAX
, GetBlacklistState());
69 ASSERT_EQ(base::string16(), GetBlacklistVersion());
72 TEST_F(ChromeBlacklistTrialTest
, VerifyFirstRun
) {
73 BrowserBlacklistBeaconSetup();
75 // Verify the state is properly set after the first run.
76 ASSERT_EQ(blacklist::BLACKLIST_ENABLED
, GetBlacklistState());
78 chrome::VersionInfo version_info
;
79 base::string16
version(base::UTF8ToUTF16(version_info
.Version()));
80 ASSERT_EQ(version
, GetBlacklistVersion());
83 TEST_F(ChromeBlacklistTrialTest
, SetupFailed
) {
84 // Set the registry to indicate that the blacklist setup is running,
85 // which means it failed to run correctly last time.
86 blacklist_registry_key_
.WriteValue(blacklist::kBeaconState
,
87 blacklist::BLACKLIST_SETUP_RUNNING
);
89 BrowserBlacklistBeaconSetup();
91 // Since the blacklist setup failed, it should now be disabled.
92 ASSERT_EQ(blacklist::BLACKLIST_DISABLED
, GetBlacklistState());
95 TEST_F(ChromeBlacklistTrialTest
, ThunkSetupFailed
) {
96 // Set the registry to indicate that the blacklist thunk setup is running,
97 // which means it failed to run correctly last time.
98 blacklist_registry_key_
.WriteValue(blacklist::kBeaconState
,
99 blacklist::BLACKLIST_THUNK_SETUP
);
101 BrowserBlacklistBeaconSetup();
103 // Since the blacklist thunk setup failed, it should now be disabled.
104 ASSERT_EQ(blacklist::BLACKLIST_DISABLED
, GetBlacklistState());
107 TEST_F(ChromeBlacklistTrialTest
, InterceptionFailed
) {
108 // Set the registry to indicate that an interception is running,
109 // which means it failed to run correctly last time.
110 blacklist_registry_key_
.WriteValue(blacklist::kBeaconState
,
111 blacklist::BLACKLIST_INTERCEPTING
);
113 BrowserBlacklistBeaconSetup();
115 // Since an interception failed, the blacklist should now be disabled.
116 ASSERT_EQ(blacklist::BLACKLIST_DISABLED
, GetBlacklistState());
119 TEST_F(ChromeBlacklistTrialTest
, VersionChanged
) {
120 // Mark the blacklist as disabled for an older version, so it should
121 // get enabled for this new version.
122 blacklist_registry_key_
.WriteValue(blacklist::kBeaconVersion
,
124 blacklist_registry_key_
.WriteValue(blacklist::kBeaconState
,
125 blacklist::BLACKLIST_DISABLED
);
127 BrowserBlacklistBeaconSetup();
129 // The beacon should now be marked as enabled for the current version.
130 ASSERT_EQ(blacklist::BLACKLIST_ENABLED
, GetBlacklistState());
132 chrome::VersionInfo version_info
;
133 base::string16
expected_version(base::UTF8ToUTF16(version_info
.Version()));
134 ASSERT_EQ(expected_version
, GetBlacklistVersion());