OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chrome_elf_init_win.h" | 5 #include "chrome/browser/chrome_elf_init_win.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
10 #include "base/test/test_reg_util_win.h" | 11 #include "base/test/test_reg_util_win.h" |
11 #include "chrome/common/chrome_version_info.h" | 12 #include "chrome/common/chrome_version_info.h" |
12 #include "chrome_elf/chrome_elf_constants.h" | 13 #include "chrome_elf/chrome_elf_constants.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "version.h" // NOLINT |
14 | 16 |
15 class ChromeBlacklistTrialTest : public testing::Test { | 17 class ChromeBlacklistTrialTest : public testing::Test { |
16 protected: | 18 protected: |
17 ChromeBlacklistTrialTest() : | 19 ChromeBlacklistTrialTest() {} |
18 blacklist_registry_key_(HKEY_CURRENT_USER, | 20 virtual ~ChromeBlacklistTrialTest() {} |
19 blacklist::kRegistryBeaconPath, | |
20 KEY_QUERY_VALUE | KEY_SET_VALUE) {} | |
21 | 21 |
22 virtual void SetUp() OVERRIDE { | 22 virtual void SetUp() OVERRIDE { |
23 testing::Test::SetUp(); | 23 testing::Test::SetUp(); |
24 | 24 |
25 registry_util::RegistryOverrideManager override_manager; | 25 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, |
26 override_manager.OverrideRegistry(HKEY_CURRENT_USER, | 26 L"browser_blacklist_test"); |
27 L"browser_blacklist_test"); | |
28 | 27 |
29 | 28 blacklist_registry_key_.reset( |
| 29 new base::win::RegKey(HKEY_CURRENT_USER, |
| 30 blacklist::kRegistryBeaconPath, |
| 31 KEY_QUERY_VALUE | KEY_SET_VALUE)); |
30 } | 32 } |
31 | 33 |
32 DWORD GetBlacklistState() { | 34 DWORD GetBlacklistState() { |
33 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; | 35 DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX; |
34 blacklist_registry_key_.ReadValueDW(blacklist::kBeaconState, | 36 blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState, |
35 &blacklist_state); | 37 &blacklist_state); |
36 | 38 |
37 return blacklist_state; | 39 return blacklist_state; |
38 } | 40 } |
39 | 41 |
40 base::string16 GetBlacklistVersion() { | 42 base::string16 GetBlacklistVersion() { |
41 base::string16 blacklist_version; | 43 base::string16 blacklist_version; |
42 blacklist_registry_key_.ReadValue(blacklist::kBeaconVersion, | 44 blacklist_registry_key_->ReadValue(blacklist::kBeaconVersion, |
43 &blacklist_version); | 45 &blacklist_version); |
44 | 46 |
45 return blacklist_version; | 47 return blacklist_version; |
46 } | 48 } |
47 | 49 |
48 base::win::RegKey blacklist_registry_key_; | 50 scoped_ptr<base::win::RegKey> blacklist_registry_key_; |
| 51 registry_util::RegistryOverrideManager override_manager_; |
49 | 52 |
50 private: | 53 private: |
51 DISALLOW_COPY_AND_ASSIGN(ChromeBlacklistTrialTest); | 54 DISALLOW_COPY_AND_ASSIGN(ChromeBlacklistTrialTest); |
52 }; | 55 }; |
53 | 56 |
54 | 57 |
55 // Ensure that the default trial deletes any existing blacklist beacons. | 58 // Ensure that the default trial deletes any existing blacklist beacons. |
56 TEST_F(ChromeBlacklistTrialTest, DefaultRun) { | 59 TEST_F(ChromeBlacklistTrialTest, DefaultRun) { |
57 // Set some dummy values as beacons. | 60 // Set some dummy values as beacons. |
58 blacklist_registry_key_.WriteValue(blacklist::kBeaconState, | 61 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, |
59 blacklist::BLACKLIST_ENABLED); | 62 blacklist::BLACKLIST_ENABLED); |
60 blacklist_registry_key_.WriteValue(blacklist::kBeaconVersion, L"Data"); | 63 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, L"Data"); |
61 | 64 |
62 // This setup code should result in the default group, which should remove | 65 // This setup code should result in the default group, which should remove |
63 // all the beacon values. | 66 // all the beacon values. |
64 InitializeChromeElf(); | 67 InitializeChromeElf(); |
65 | 68 |
66 // Ensure that invalid values are returned to indicate that the | 69 // Ensure that invalid values are returned to indicate that the |
67 // beacon values are gone. | 70 // beacon values are gone. |
68 ASSERT_EQ(blacklist::BLACKLIST_STATE_MAX, GetBlacklistState()); | 71 ASSERT_EQ(blacklist::BLACKLIST_STATE_MAX, GetBlacklistState()); |
69 ASSERT_EQ(base::string16(), GetBlacklistVersion()); | 72 ASSERT_EQ(base::string16(), GetBlacklistVersion()); |
70 } | 73 } |
71 | 74 |
72 TEST_F(ChromeBlacklistTrialTest, VerifyFirstRun) { | 75 TEST_F(ChromeBlacklistTrialTest, VerifyFirstRun) { |
73 BrowserBlacklistBeaconSetup(); | 76 BrowserBlacklistBeaconSetup(); |
74 | 77 |
75 // Verify the state is properly set after the first run. | 78 // Verify the state is properly set after the first run. |
76 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); | 79 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); |
77 | 80 |
78 chrome::VersionInfo version_info; | 81 chrome::VersionInfo version_info; |
79 base::string16 version(base::UTF8ToUTF16(version_info.Version())); | 82 base::string16 version(base::UTF8ToUTF16(version_info.Version())); |
80 ASSERT_EQ(version, GetBlacklistVersion()); | 83 ASSERT_EQ(version, GetBlacklistVersion()); |
81 } | 84 } |
82 | 85 |
83 TEST_F(ChromeBlacklistTrialTest, SetupFailed) { | 86 TEST_F(ChromeBlacklistTrialTest, SetupFailed) { |
84 // Set the registry to indicate that the blacklist setup is running, | 87 // Set the registry to indicate that the blacklist setup is running, |
85 // which means it failed to run correctly last time. | 88 // which means it failed to run correctly last time for this version. |
86 blacklist_registry_key_.WriteValue(blacklist::kBeaconState, | 89 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
87 blacklist::BLACKLIST_SETUP_RUNNING); | 90 TEXT(CHROME_VERSION_STRING)); |
| 91 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, |
| 92 blacklist::BLACKLIST_SETUP_RUNNING); |
88 | 93 |
89 BrowserBlacklistBeaconSetup(); | 94 BrowserBlacklistBeaconSetup(); |
90 | 95 |
91 // Since the blacklist setup failed, it should now be disabled. | 96 // Since the blacklist setup failed, it should now be disabled. |
92 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); | 97 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); |
93 } | 98 } |
94 | 99 |
95 TEST_F(ChromeBlacklistTrialTest, ThunkSetupFailed) { | 100 TEST_F(ChromeBlacklistTrialTest, ThunkSetupFailed) { |
96 // Set the registry to indicate that the blacklist thunk setup is running, | 101 // Set the registry to indicate that the blacklist thunk setup is running, |
97 // which means it failed to run correctly last time. | 102 // which means it failed to run correctly last time for this version. |
98 blacklist_registry_key_.WriteValue(blacklist::kBeaconState, | 103 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
99 blacklist::BLACKLIST_THUNK_SETUP); | 104 TEXT(CHROME_VERSION_STRING)); |
| 105 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, |
| 106 blacklist::BLACKLIST_THUNK_SETUP); |
100 | 107 |
101 BrowserBlacklistBeaconSetup(); | 108 BrowserBlacklistBeaconSetup(); |
102 | 109 |
103 // Since the blacklist thunk setup failed, it should now be disabled. | 110 // Since the blacklist thunk setup failed, it should now be disabled. |
104 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); | 111 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); |
105 } | 112 } |
106 | 113 |
107 TEST_F(ChromeBlacklistTrialTest, InterceptionFailed) { | 114 TEST_F(ChromeBlacklistTrialTest, InterceptionFailed) { |
108 // Set the registry to indicate that an interception is running, | 115 // Set the registry to indicate that an interception is running, |
109 // which means it failed to run correctly last time. | 116 // which means it failed to run correctly last time for this version. |
110 blacklist_registry_key_.WriteValue(blacklist::kBeaconState, | 117 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
111 blacklist::BLACKLIST_INTERCEPTING); | 118 TEXT(CHROME_VERSION_STRING)); |
| 119 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, |
| 120 blacklist::BLACKLIST_INTERCEPTING); |
112 | 121 |
113 BrowserBlacklistBeaconSetup(); | 122 BrowserBlacklistBeaconSetup(); |
114 | 123 |
115 // Since an interception failed, the blacklist should now be disabled. | 124 // Since an interception failed, the blacklist should now be disabled. |
116 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); | 125 ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState()); |
117 } | 126 } |
118 | 127 |
119 TEST_F(ChromeBlacklistTrialTest, VersionChanged) { | 128 TEST_F(ChromeBlacklistTrialTest, VersionChanged) { |
120 // Mark the blacklist as disabled for an older version, so it should | 129 // Mark the blacklist as disabled for an older version, so it should |
121 // get enabled for this new version. | 130 // get enabled for this new version. |
122 blacklist_registry_key_.WriteValue(blacklist::kBeaconVersion, | 131 blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, |
123 L"old_version"); | 132 L"old_version"); |
124 blacklist_registry_key_.WriteValue(blacklist::kBeaconState, | 133 blacklist_registry_key_->WriteValue(blacklist::kBeaconState, |
125 blacklist::BLACKLIST_DISABLED); | 134 blacklist::BLACKLIST_DISABLED); |
126 | 135 |
127 BrowserBlacklistBeaconSetup(); | 136 BrowserBlacklistBeaconSetup(); |
128 | 137 |
129 // The beacon should now be marked as enabled for the current version. | 138 // The beacon should now be marked as enabled for the current version. |
130 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); | 139 ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState()); |
131 | 140 |
132 chrome::VersionInfo version_info; | 141 chrome::VersionInfo version_info; |
133 base::string16 expected_version(base::UTF8ToUTF16(version_info.Version())); | 142 base::string16 expected_version(base::UTF8ToUTF16(version_info.Version())); |
134 ASSERT_EQ(expected_version, GetBlacklistVersion()); | 143 ASSERT_EQ(expected_version, GetBlacklistVersion()); |
135 } | 144 } |
OLD | NEW |