Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/base_switches.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/json/json_file_value_serializer.h" | |
| 8 #include "base/json/json_reader.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "chrome/browser/prefs/chrome_pref_service_factory.h" | |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 16 #include "chrome/browser/ui/browser.h" | |
| 17 #include "chrome/common/chrome_constants.h" | |
| 18 #include "chrome/test/base/in_process_browser_test.h" | |
| 19 #include "chrome/test/base/search_test_utils.h" | |
| 20 #include "components/search_engines/template_url_service.h" | |
| 21 #include "components/user_prefs/tracked/pref_hash_calculator.h" | |
| 22 | |
| 23 class TURLServicePrefsProtectBrowsertest : public InProcessBrowserTest { | |
| 24 protected: | |
| 25 TURLServicePrefsProtectBrowsertest(); | |
| 26 | |
| 27 void SetUpCommandLine(base::CommandLine* command_line) override; | |
| 28 void SetUpInProcessBrowserTestFixture() override; | |
| 29 Profile* CreateProfileWithPrefs( | |
| 30 const std::string& prefs, | |
| 31 const std::vector<std::string>& names_to_update_hashes); | |
| 32 | |
| 33 private: | |
| 34 PrefHashCalculator hash_calculator_; | |
| 35 | |
| 36 DISALLOW_COPY_AND_ASSIGN(TURLServicePrefsProtectBrowsertest); | |
| 37 }; | |
| 38 | |
| 39 // Passing empty strings to the |hash_calculator_| would be insecure for real | |
| 40 // usage, but is sufficient in tests to verify the protection functionality. | |
| 41 TURLServicePrefsProtectBrowsertest::TURLServicePrefsProtectBrowsertest() | |
| 42 : hash_calculator_(std::string(), std::string()) {} | |
| 43 | |
| 44 // Enable protection of DSE prefs by turning on field trial in command line. | |
| 45 void TURLServicePrefsProtectBrowsertest::SetUpCommandLine( | |
| 46 base::CommandLine* command_line) { | |
| 47 command_line->AppendSwitchASCII( | |
| 48 switches::kForceFieldTrials, | |
| 49 base::StringPrintf("%s/%s/", | |
| 50 chrome_prefs::internals::kSettingsEnforcementTrialName, | |
| 51 chrome_prefs::internals:: | |
| 52 kSettingsEnforcementGroupEnforceAlwaysWithDSE)); | |
| 53 } | |
| 54 | |
| 55 // Prefs protection is turned off in domain, so disable domain check. | |
| 56 void TURLServicePrefsProtectBrowsertest::SetUpInProcessBrowserTestFixture() { | |
| 57 chrome_prefs::DisableDomainCheckForTesting(); | |
| 58 } | |
| 59 | |
| 60 Profile* TURLServicePrefsProtectBrowsertest::CreateProfileWithPrefs( | |
| 61 const std::string& prefs, | |
| 62 const std::vector<std::string>& names_to_update_hashes) { | |
| 63 // Create a directory for a new profile and get the path to the Preferences. | |
| 64 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 65 const base::FilePath profile_path = | |
| 66 profile_manager->GenerateNextProfileDirectoryPath(); | |
| 67 const base::FilePath prefs_path = | |
| 68 profile_path.Append(chrome::kPreferencesFilename); | |
| 69 bool dir_exists = base::CreateDirectory(prefs_path.DirName()); | |
| 70 if (!dir_exists) | |
| 71 return nullptr; | |
| 72 | |
| 73 // Convert the passed |prefs| to base::DictionaryValue. | |
| 74 std::string replaced_prefs = prefs; | |
| 75 base::ReplaceSubstringsAfterOffset(&replaced_prefs, 0, "'", "\""); | |
| 76 std::unique_ptr<base::Value> prefs_value = | |
| 77 base::JSONReader::Read(replaced_prefs); | |
| 78 base::DictionaryValue* prefs_dict = nullptr; | |
| 79 prefs_value->GetAsDictionary(&prefs_dict); | |
| 80 if (!prefs_dict) | |
| 81 return nullptr; | |
| 82 | |
| 83 // Calculate hashes of the requested settings. | |
| 84 for (const std::string& name : names_to_update_hashes) { | |
| 85 const base::Value* value = nullptr; | |
| 86 prefs_dict->Get(name, &value); | |
| 87 if (!value) | |
| 88 return nullptr; | |
| 89 prefs_dict->SetString("protection.macs." + name, | |
| 90 hash_calculator_.Calculate(name, value)); | |
| 91 } | |
| 92 | |
| 93 // Write the result preferences to file. | |
| 94 JSONFileValueSerializer json_serializer(prefs_path); | |
| 95 if (!json_serializer.Serialize(*prefs_dict)) | |
| 96 return nullptr; | |
| 97 | |
| 98 // Finally create a profile. | |
| 99 Profile* profile = Profile::CreateProfile(profile_path, NULL, | |
| 100 Profile::CREATE_MODE_SYNCHRONOUS); | |
| 101 if (profile) | |
| 102 profile_manager->RegisterTestingProfile(profile, false, false); | |
| 103 return profile; | |
| 104 } | |
| 105 | |
| 106 // Check that preferences that can influence default search provider choice | |
| 107 // are protected. | |
| 108 IN_PROC_BROWSER_TEST_F(TURLServicePrefsProtectBrowsertest, | |
| 109 ChangeDefaultSearchProvider) { | |
| 110 struct TestCase { | |
| 111 const std::string pref_to_protect; | |
| 112 const std::vector<std::string> names_to_update_hashes; | |
| 113 const std::vector<base::string16> expected_keywords; | |
| 114 const std::vector<base::string16> not_expected_keywords; | |
| 115 }; | |
| 116 | |
| 117 const char* default_search_provider_data = | |
|
gab
2016/11/25 14:24:41
nit: char[] instead of char*
nit: prefer "static"
Peter Kasting
2016/11/26 06:12:40
Please find the reference; I've been explicitly as
gab
2016/11/28 16:12:20
According to our tests from 3 years ago, static wa
Peter Kasting
2016/11/28 20:20:44
Fascinating! I forgot about this thread. So, loo
| |
| 118 R"({ | |
| 119 'default_search_provider_data' : | |
| 120 { | |
| 121 'template_url_data' : | |
| 122 { | |
| 123 'keyword' : 'my_search', | |
| 124 'short_name' : 'My Search', | |
| 125 'url' : '{google:baseURL}search?q=my+{searchTerms}' | |
| 126 } | |
| 127 } | |
| 128 })"; | |
| 129 const char* search_provider_overrides = | |
| 130 R"({ | |
| 131 'search_provider_overrides' : [ | |
| 132 { | |
| 133 'keyword' : 'my_search', | |
| 134 'name' : 'My Search', | |
| 135 'search_url' : '{google:baseURL}search?q=my+{searchTerms}', | |
| 136 'encoding' : 'utf-8', | |
| 137 'favicon_url' : 'http://www.google.com/favicon.ico', | |
| 138 'id' : 1 | |
| 139 }, | |
| 140 { | |
| 141 'keyword' : 'my_search2', | |
| 142 'name' : 'My Search2', | |
| 143 'search_url' : '{google:baseURL}search?q=my+{searchTerms}+2', | |
| 144 'encoding' : 'utf-8', | |
| 145 'favicon_url' : 'http://www.google.com/favicon.ico', | |
| 146 'id' : 2 | |
| 147 } | |
| 148 ] | |
| 149 })"; | |
| 150 const char* default_search_provider = | |
| 151 R"({ | |
| 152 'default_search_provider' : | |
| 153 { | |
| 154 'keyword' : 'my_search', | |
| 155 'name' : 'My Search', | |
| 156 'search_url' : '{google:baseURL}search?q=my+{searchTerms}' | |
| 157 } | |
| 158 })"; | |
| 159 | |
| 160 // In cases below where the protection check fails, the default search | |
| 161 // provider's keyword will be unchanged. Get that keyword from an unmodified | |
| 162 // profile. | |
| 163 TemplateURLService* turl_service = | |
| 164 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | |
| 165 ASSERT_TRUE(turl_service); | |
| 166 search_test_utils::WaitForTemplateURLServiceToLoad(turl_service); | |
| 167 const base::string16 default_keyword = | |
| 168 turl_service->GetDefaultSearchProvider()->keyword(); | |
| 169 const base::string16 my_search = base::ASCIIToUTF16("my_search"); | |
|
gab
2016/11/25 14:24:41
L"my_search" instead of ASCIIToUTF16
and below
Peter Kasting
2016/11/26 06:12:40
Doesn't work. Can't assign a wide string to a str
gab
2016/11/28 16:12:20
Duh, right, that's unfortunate.
Beyond this CL,
| |
| 170 const base::string16 my_search2 = base::ASCIIToUTF16("my_search2"); | |
| 171 | |
| 172 const TestCase test_cases[] = { | |
| 173 {default_search_provider_data, | |
|
gab
2016/11/25 14:24:41
Add a comment above each test case mentioning what
| |
| 174 {"default_search_provider_data.template_url_data"}, | |
| 175 {my_search}, | |
| 176 std::vector<base::string16>()}, | |
| 177 {default_search_provider_data, std::vector<std::string>(), | |
| 178 {default_keyword}, {my_search}}, | |
| 179 {search_provider_overrides, | |
| 180 {"search_provider_overrides"}, | |
| 181 {my_search, my_search2}, | |
| 182 std::vector<base::string16>()}, | |
| 183 {search_provider_overrides, | |
| 184 std::vector<std::string>(), | |
| 185 {default_keyword}, | |
| 186 {my_search, my_search2}}, | |
| 187 {default_search_provider, | |
| 188 {"default_search_provider.keyword", "default_search_provider.name", | |
| 189 "default_search_provider.search_url"}, | |
| 190 {my_search}, | |
| 191 std::vector<base::string16>()}, | |
| 192 {default_search_provider, std::vector<std::string>(), | |
| 193 {default_keyword}, {my_search}}, | |
| 194 }; | |
| 195 | |
| 196 for (size_t i = 0; i != arraysize(test_cases); ++i) { | |
| 197 const TestCase& test_case = test_cases[i]; | |
| 198 // Create profile with pref to protect and update hashes for | |
| 199 // protected prefs. | |
| 200 Profile* profile = CreateProfileWithPrefs(test_case.pref_to_protect, | |
| 201 test_case.names_to_update_hashes); | |
| 202 ASSERT_TRUE(profile); | |
| 203 | |
| 204 turl_service = TemplateURLServiceFactory::GetForProfile(profile); | |
| 205 search_test_utils::WaitForTemplateURLServiceToLoad(turl_service); | |
| 206 | |
| 207 TemplateURL* dse = turl_service->GetDefaultSearchProvider(); | |
| 208 ASSERT_TRUE(dse) << "Test case i=" << i; | |
|
gab
2016/11/25 14:24:41
Can replace all of these logs by SCOPED_TRACE(i) a
Peter Kasting
2016/11/26 06:12:40
Good call, I forgot about that.
| |
| 209 | |
| 210 ASSERT_FALSE(test_case.expected_keywords.empty()); | |
| 211 EXPECT_EQ(test_case.expected_keywords[0], dse->keyword()) << "i=" << i; | |
| 212 | |
| 213 for (const base::string16& keyword : test_case.expected_keywords) { | |
| 214 EXPECT_TRUE(turl_service->GetTemplateURLForKeyword(keyword)) | |
| 215 << "keyword=" << keyword << ", i=" << i; | |
| 216 } | |
| 217 | |
| 218 for (const base::string16& keyword : test_case.not_expected_keywords) { | |
| 219 EXPECT_FALSE(turl_service->GetTemplateURLForKeyword(keyword)) | |
| 220 << "keyword=" << keyword << ", i=" << i; | |
| 221 } | |
| 222 } | |
| 223 } | |
| OLD | NEW |