Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Side by Side Diff: chrome/browser/search_engines/template_url_service_prefs_protect_browsertest.cc

Issue 2521823007: Added browser test for TemplateUrlService protected prefs (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/common/chrome_constants.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/search_test_utils.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "components/user_prefs/tracked/pref_hash_calculator.h"
21
22 #if defined(OS_WIN)
23 #include "rlz/lib/machine_id.h"
24 #endif // defined(OS_WIN)
25
26 namespace {
27
28 std::string GetSeed() {
29 // Seed is empty string in tests.
Peter Kasting 2016/11/23 18:49:13 Nit: I would just pass std::string() directly in t
Alexander Yashkin 2016/11/24 14:37:27 Done
30 return std::string();
31 }
32
33 std::string GetDeviceID() {
34 std::string device_id;
35 #if defined(OS_WIN) && defined(ENABLE_RLZ)
36 rlz_lib::GetMachineId(&device_id);
Peter Kasting 2016/11/23 18:49:13 Why do we need to compute this? Can't we just pas
Alexander Yashkin 2016/11/24 14:37:27 Done
37 #endif
38 return device_id;
39 }
40
41 } // namespace
42
43 class TURLServicePrefsProtectBrowsertest : public InProcessBrowserTest {
44 protected:
45 TURLServicePrefsProtectBrowsertest();
46
47 void SetUpCommandLine(base::CommandLine* command_line) override;
48 void SetUpInProcessBrowserTestFixture() override;
49 Profile* CreateProfileWithPrefs(
50 const std::string& prefs,
51 const std::vector<std::string>& names_to_update_hashes);
52
53 private:
54 PrefHashCalculator hash_calculator_;
55 };
Peter Kasting 2016/11/23 18:49:13 Nit: DISALLOW_COPY_AND_ASSIGN
Alexander Yashkin 2016/11/24 14:37:27 Done
56
57 TURLServicePrefsProtectBrowsertest::TURLServicePrefsProtectBrowsertest()
58 : hash_calculator_(GetSeed(), GetDeviceID()) {}
59
60 // Enable protection of DSE prefs by turning on field trial in command line.
61 void TURLServicePrefsProtectBrowsertest::SetUpCommandLine(
62 base::CommandLine* command_line) {
63 command_line->AppendSwitchASCII(
64 switches::kForceFieldTrials,
65 base::StringPrintf("%s/%s/",
66 chrome_prefs::internals::kSettingsEnforcementTrialName,
67 chrome_prefs::internals::
68 kSettingsEnforcementGroupEnforceAlwaysWithDSE));
69 }
70
71 // Prefs protection is turned off in domain, so disable domain check.
72 void TURLServicePrefsProtectBrowsertest::SetUpInProcessBrowserTestFixture() {
73 chrome_prefs::DisableDomainCheckForTesting();
74 }
75
76 Profile* TURLServicePrefsProtectBrowsertest::CreateProfileWithPrefs(
77 const std::string& prefs,
78 const std::vector<std::string>& names_to_update_hashes) {
79 // Create a directory for a new profile and get the path to the Preferences.
80 ProfileManager* profile_manager = g_browser_process->profile_manager();
81 const base::FilePath profile_path =
82 profile_manager->GenerateNextProfileDirectoryPath();
83 const base::FilePath prefs_path =
84 profile_path.Append(chrome::kPreferencesFilename);
85 bool dir_exists = base::CreateDirectory(prefs_path.DirName());
86 DCHECK(dir_exists);
Peter Kasting 2016/11/23 18:49:13 Nit: Avoid [D]CHECK in test code (failed checks ca
Alexander Yashkin 2016/11/24 14:37:27 Done
87
88 // Convert the passed |prefs| to base::DictionaryValue.
89 std::string replaced_prefs = prefs;
90 base::ReplaceSubstringsAfterOffset(&replaced_prefs, 0, "'", "\"");
Peter Kasting 2016/11/23 18:49:13 Nit: Seems like you wouldn't need this if you init
91 std::unique_ptr<base::Value> prefs_value =
92 base::JSONReader::Read(replaced_prefs);
93 base::DictionaryValue* prefs_dict = nullptr;
94 prefs_value->GetAsDictionary(&prefs_dict);
95 DCHECK(prefs_dict);
96
97 // Calculate hashes of the requested settings.
98 for (const std::string& name : names_to_update_hashes) {
99 const base::Value* value = nullptr;
100 prefs_dict->Get(name, &value);
101 DCHECK(value);
102 prefs_dict->SetString("protection.macs." + name,
103 hash_calculator_.Calculate(name, value));
104 }
105
106 // Write the result preferences to file.
107 JSONFileValueSerializer json_serializer(prefs_path);
108 if (!json_serializer.Serialize(*prefs_dict))
109 return nullptr;
110
111 // Finally create a profile.
112 Profile* profile = Profile::CreateProfile(profile_path, NULL,
113 Profile::CREATE_MODE_SYNCHRONOUS);
114 if (!profile)
115 return nullptr;
116 profile_manager->RegisterTestingProfile(profile, false, false);
117 return profile;
Peter Kasting 2016/11/23 18:49:13 Nit: Simpler: if (profile) profile_manager-
Alexander Yashkin 2016/11/24 14:37:27 Done
118 }
119
120 // Check that preferences that can influence default search provider choice
121 // are protected.
122 IN_PROC_BROWSER_TEST_F(TURLServicePrefsProtectBrowsertest,
123 ChangeDefaultSearchProvider) {
124 struct TestCase {
125 const std::string pref_to_protect;
126 const std::vector<std::string> names_to_update_hashes;
127 const std::vector<base::string16> expected_keywords;
128 const std::vector<base::string16> not_expected_keywords;
129 };
130
131 constexpr const char* default_search_provider_data =
Peter Kasting 2016/11/23 18:49:13 Nit: I suspect raw string literals would make thes
Alexander Yashkin 2016/11/24 14:37:27 Could not find raw literals example in sources. Tr
Peter Kasting 2016/11/24 19:39:48 See https://codereview.chromium.org/2470643002 for
132 "{"
133 "'default_search_provider_data' : {"
134 "'template_url_data' : {"
135 "'keyword' : 'my_search',"
136 "'short_name' : 'My Search',"
137 "'url' : '{google:baseURL}search?q=my+{searchTerms}'"
138 "}"
139 "}"
140 "}";
141 constexpr const char* search_provider_overrides =
142 "{"
143 "'search_provider_overrides' : ["
144 "{"
145 "'keyword' : 'my_search',"
146 "'name' : 'My Search',"
147 "'search_url' : '{google:baseURL}search?q=my+{searchTerms}',"
148 "'encoding' : 'utf-8',"
149 "'favicon_url' : 'http://www.google.com/favicon.ico',"
150 "'id' : 1"
151 "}, {"
152 "'keyword' : 'my_search2',"
153 "'name' : 'My Search2',"
154 "'search_url' : '{google:baseURL}search?q="
155 "my+{searchTerms}+2',"
156 "'encoding' : 'utf-8',"
157 "'favicon_url' : 'http://www.google.com/favicon.ico',"
158 "'id' : 2"
159 "}"
160 "]"
161 "}";
162 constexpr const char* default_search_provider =
163 "{"
164 "'default_search_provider' : {"
165 "'keyword' : 'my_search',"
166 "'name' : 'My Search',"
167 "'search_url' : '{google:baseURL}search?q=my+{searchTerms}'"
168 "}"
169 "}";
170
171 // Google is default search, so google keywod will be set for cases where
172 // protection check fail.
Peter Kasting 2016/11/23 18:49:13 Nit: It might be nice to avoid this hardcoding by
Alexander Yashkin 2016/11/24 14:37:27 Changed to get default provider from current brows
173 const base::string16 google_keyword(base::ASCIIToUTF16("google.com"));
Peter Kasting 2016/11/23 18:49:13 Nit: Prefer = to () to init "simple" objects like
Alexander Yashkin 2016/11/24 14:37:27 Done
174 const base::string16 my_search(base::ASCIIToUTF16("my_search"));
175 const base::string16 my_search2(base::ASCIIToUTF16("my_search2"));
176
177 const TestCase test_cases[] = {
178 {default_search_provider_data,
179 {"default_search_provider_data.template_url_data"},
180 {my_search},
181 {}},
182 {default_search_provider_data, {}, {google_keyword}, {my_search}},
183 {search_provider_overrides,
184 {"search_provider_overrides"},
185 {my_search, my_search2},
186 {}},
187 {search_provider_overrides,
188 {},
189 {google_keyword},
190 {my_search, my_search2}},
191 {default_search_provider,
192 {"default_search_provider.keyword", "default_search_provider.name",
193 "default_search_provider.search_url"},
194 {my_search},
195 {}},
196 {default_search_provider, {}, {google_keyword}, {my_search}},
197 };
198
199 for (size_t i = 0; i != arraysize(test_cases); ++i) {
200 const TestCase& test_case = test_cases[i];
201 // Create profile with pref to protect and update hashes for
202 // protected prefs.
203 Profile* profile = CreateProfileWithPrefs(test_case.pref_to_protect,
204 test_case.names_to_update_hashes);
205 ASSERT_TRUE(profile);
206
207 TemplateURLService* template_url_service =
208 TemplateURLServiceFactory::GetForProfile(profile);
209 search_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
210
211 TemplateURL* dse = template_url_service->GetDefaultSearchProvider();
212 ASSERT_TRUE(dse) << "Test case i=" << i;
213
214 DCHECK(!test_case.expected_keywords.empty());
Peter Kasting 2016/11/23 18:49:13 Nit: Avoid [D]CHECK in test code, use ASSERT inste
Alexander Yashkin 2016/11/24 14:37:27 Done.
215 EXPECT_EQ(test_case.expected_keywords[0], dse->keyword()) << "i=" << i;
216
217 for (const base::string16& keyword : test_case.expected_keywords) {
218 EXPECT_TRUE(template_url_service->GetTemplateURLForKeyword(keyword))
219 << "keyword=" << keyword << ", i=" << i;
220 }
221
222 for (const base::string16& keyword : test_case.not_expected_keywords) {
223 EXPECT_FALSE(template_url_service->GetTemplateURLForKeyword(keyword))
224 << "keyword=" << keyword << ", i=" << i;
225 }
226 }
227 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698