Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chrome_elf_init_unittest_win.cc
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 "base/basictypes.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/path_service.h"
12 #include "base/scoped_native_library.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/test_reg_util_win.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome_elf/blacklist/blacklist.h"
19 #include "chrome_elf/chrome_elf_constants.h"
20 #include "components/variations/entropy_provider.h"
21 #include "components/variations/variations_associated_data.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "version.h"  // NOLINT
24
25 namespace {
26
27 const char kBrowserBlacklistTrialEnabledGroupName[] = "Enabled";
28 const wchar_t kTestDllName[] = L"blacklist_test_dll_1.dll";
29
30 class ChromeBlacklistTrialTest : public testing::Test {
31  protected:
32   ChromeBlacklistTrialTest() {}
33   virtual ~ChromeBlacklistTrialTest() {}
34
35   virtual void SetUp() OVERRIDE {
36     testing::Test::SetUp();
37
38     override_manager_.OverrideRegistry(HKEY_CURRENT_USER,
39                                        L"browser_blacklist_test");
40
41     blacklist_registry_key_.reset(
42         new base::win::RegKey(HKEY_CURRENT_USER,
43                               blacklist::kRegistryBeaconPath,
44                               KEY_QUERY_VALUE | KEY_SET_VALUE));
45   }
46
47   DWORD GetBlacklistState() {
48     DWORD blacklist_state = blacklist::BLACKLIST_STATE_MAX;
49     blacklist_registry_key_->ReadValueDW(blacklist::kBeaconState,
50                                          &blacklist_state);
51
52     return blacklist_state;
53   }
54
55   base::string16 GetBlacklistVersion() {
56     base::string16 blacklist_version;
57     blacklist_registry_key_->ReadValue(blacklist::kBeaconVersion,
58                                        &blacklist_version);
59
60     return blacklist_version;
61   }
62
63   scoped_ptr<base::win::RegKey> blacklist_registry_key_;
64   registry_util::RegistryOverrideManager override_manager_;
65
66  private:
67   DISALLOW_COPY_AND_ASSIGN(ChromeBlacklistTrialTest);
68 };
69
70 // Ensure that the default trial sets up the blacklist beacons.
71 TEST_F(ChromeBlacklistTrialTest, DefaultRun) {
72   // Set some dummy values as beacons.
73   blacklist_registry_key_->WriteValue(blacklist::kBeaconState,
74                                       blacklist::BLACKLIST_DISABLED);
75   blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, L"Data");
76
77   // This setup code should result in the default group, which should have
78   // the blacklist set up.
79   InitializeChromeElf();
80
81   // Ensure the beacon values are now correct, indicating the
82   // blacklist beacon was setup.
83   ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState());
84   chrome::VersionInfo version_info;
85   base::string16 version(base::UTF8ToUTF16(version_info.Version()));
86   ASSERT_EQ(version, GetBlacklistVersion());
87 }
88
89 // Ensure that the blacklist is disabled for any users in the
90 // "BlacklistDisabled" finch group.
91 TEST_F(ChromeBlacklistTrialTest, BlacklistDisabledRun) {
92   // Set the beacons to enabled values.
93   blacklist_registry_key_->WriteValue(blacklist::kBeaconState,
94                                       blacklist::BLACKLIST_ENABLED);
95   blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion, L"Data");
96
97   // Create the field trial with the blacklist disabled group.
98   base::FieldTrialList field_trial_list(
99     new metrics::SHA1EntropyProvider("test"));
100
101   scoped_refptr<base::FieldTrial> trial(
102     base::FieldTrialList::CreateFieldTrial(
103       kBrowserBlacklistTrialName, kBrowserBlacklistTrialDisabledGroupName));
104
105   // This setup code should now delete any existing blacklist beacons.
106   InitializeChromeElf();
107
108   // Ensure invalid values are returned to indicate that the beacon
109   // values are indeed gone.
110   ASSERT_EQ(blacklist::BLACKLIST_STATE_MAX, GetBlacklistState());
111   ASSERT_EQ(base::string16(), GetBlacklistVersion());
112 }
113
114 TEST_F(ChromeBlacklistTrialTest, VerifyFirstRun) {
115   BrowserBlacklistBeaconSetup();
116
117   // Verify the state is properly set after the first run.
118   ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState());
119
120   chrome::VersionInfo version_info;
121   base::string16 version(base::UTF8ToUTF16(version_info.Version()));
122   ASSERT_EQ(version, GetBlacklistVersion());
123 }
124
125 TEST_F(ChromeBlacklistTrialTest, BlacklistFailed) {
126   // Ensure when the blacklist set up failed we set the state to disabled for
127   // future runs.
128   blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion,
129                                       TEXT(CHROME_VERSION_STRING));
130   blacklist_registry_key_->WriteValue(blacklist::kBeaconState,
131                                       blacklist::BLACKLIST_SETUP_FAILED);
132
133   BrowserBlacklistBeaconSetup();
134
135   ASSERT_EQ(blacklist::BLACKLIST_DISABLED, GetBlacklistState());
136 }
137
138 TEST_F(ChromeBlacklistTrialTest, VersionChanged) {
139   // Mark the blacklist as disabled for an older version, it should
140   // get enabled for this new version.  Also record a non-zero number of
141   // setup failures, which should be reset to zero.
142   blacklist_registry_key_->WriteValue(blacklist::kBeaconVersion,
143                                       L"old_version");
144   blacklist_registry_key_->WriteValue(blacklist::kBeaconState,
145                                       blacklist::BLACKLIST_DISABLED);
146   blacklist_registry_key_->WriteValue(blacklist::kBeaconAttemptCount,
147                                       blacklist::kBeaconMaxAttempts);
148
149   BrowserBlacklistBeaconSetup();
150
151   // The beacon should now be marked as enabled for the current version.
152   ASSERT_EQ(blacklist::BLACKLIST_ENABLED, GetBlacklistState());
153
154   chrome::VersionInfo version_info;
155   base::string16 expected_version(base::UTF8ToUTF16(version_info.Version()));
156   ASSERT_EQ(expected_version, GetBlacklistVersion());
157
158   // The counter should be reset.
159   DWORD attempt_count = blacklist::kBeaconMaxAttempts;
160   blacklist_registry_key_->ReadValueDW(blacklist::kBeaconAttemptCount,
161                                        &attempt_count);
162   ASSERT_EQ(static_cast<DWORD>(0), attempt_count);
163 }
164
165 TEST_F(ChromeBlacklistTrialTest, AddFinchBlacklistToRegistry) {
166   // Create the field trial with the blacklist enabled group.
167   base::FieldTrialList field_trial_list(
168       new metrics::SHA1EntropyProvider("test"));
169
170   scoped_refptr<base::FieldTrial> trial(base::FieldTrialList::CreateFieldTrial(
171       kBrowserBlacklistTrialName, kBrowserBlacklistTrialEnabledGroupName));
172
173   // Set up the trial with the desired parameters.
174   std::map<std::string, std::string> desired_params;
175   desired_params["TestDllName1"] = "TestDll1.dll";
176   desired_params["TestDllName2"] = "TestDll2.dll";
177
178   variations::AssociateVariationParams(
179       kBrowserBlacklistTrialName,
180       kBrowserBlacklistTrialEnabledGroupName,
181       desired_params);
182
183   // This should add the dlls in those parameters to the registry.
184   AddFinchBlacklistToRegistry();
185
186   // Check that all the values in desired_params were added to the registry.
187   base::win::RegKey finch_blacklist_registry_key(
188       HKEY_CURRENT_USER,
189       blacklist::kRegistryFinchListPath,
190       KEY_QUERY_VALUE | KEY_SET_VALUE);
191
192   ASSERT_EQ(desired_params.size(),
193             finch_blacklist_registry_key.GetValueCount());
194
195   for (std::map<std::string, std::string>::iterator it = desired_params.begin();
196        it != desired_params.end();
197        ++it) {
198     std::wstring name = base::UTF8ToWide(it->first);
199     ASSERT_TRUE(finch_blacklist_registry_key.HasValue(name.c_str()));
200   }
201 }
202
203 TEST_F(ChromeBlacklistTrialTest, TestBlacklistBypass) {
204   base::FilePath current_dir;
205   ASSERT_TRUE(PathService::Get(base::DIR_EXE, &current_dir));
206
207   // Load test dll.
208   base::ScopedNativeLibrary dll1(current_dir.Append(kTestDllName));
209
210   // No blacklisted dll should be found.
211   std::vector<base::string16> module_names;
212   EXPECT_TRUE(GetLoadedBlacklistedModules(&module_names));
213   EXPECT_TRUE(module_names.empty());
214   // For posterity, print any that are.
215   std::vector<base::string16>::const_iterator module_iter(module_names.begin());
216   for (; module_iter != module_names.end(); ++module_iter) {
217     LOG(ERROR) << "Found blacklisted module: " << *module_iter;
218   }
219
220   // Add test dll to blacklist
221   blacklist::AddDllToBlacklist(kTestDllName);
222
223   // Check that the test dll appears in list.
224   module_names.clear();
225   EXPECT_TRUE(GetLoadedBlacklistedModules(&module_names));
226   ASSERT_EQ(1, module_names.size());
227   EXPECT_STREQ(kTestDllName,
228                base::StringToLowerASCII(
229                    base::FilePath(module_names[0]).BaseName().value()).c_str());
230 }
231
232 }  // namespace