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

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: Fixed after review, round 1 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/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 DISALLOW_COPY_AND_ASSIGN(TURLServicePrefsProtectBrowsertest);
Peter Kasting 2016/11/24 19:39:48 Nit: Frequently we put a blank line above this
Alexander Yashkin 2016/11/25 09:33:34 Done
36 };
37
38 // Seed and device_id passed to hash_calculator_ constructor are empty strings
39 // in tests.
Peter Kasting 2016/11/24 19:39:48 Nit: This comment kinda restates the code. There'
Alexander Yashkin 2016/11/25 09:33:34 What I really meant is that protected prefs subsys
Peter Kasting 2016/11/26 06:12:40 Wait, where is that code that you're referring to?
40 TURLServicePrefsProtectBrowsertest::TURLServicePrefsProtectBrowsertest()
41 : hash_calculator_(std::string(), std::string()) {}
42
43 // Enable protection of DSE prefs by turning on field trial in command line.
44 void TURLServicePrefsProtectBrowsertest::SetUpCommandLine(
45 base::CommandLine* command_line) {
46 command_line->AppendSwitchASCII(
47 switches::kForceFieldTrials,
48 base::StringPrintf("%s/%s/",
49 chrome_prefs::internals::kSettingsEnforcementTrialName,
50 chrome_prefs::internals::
51 kSettingsEnforcementGroupEnforceAlwaysWithDSE));
52 }
53
54 // Prefs protection is turned off in domain, so disable domain check.
55 void TURLServicePrefsProtectBrowsertest::SetUpInProcessBrowserTestFixture() {
56 chrome_prefs::DisableDomainCheckForTesting();
57 }
58
59 Profile* TURLServicePrefsProtectBrowsertest::CreateProfileWithPrefs(
60 const std::string& prefs,
61 const std::vector<std::string>& names_to_update_hashes) {
62 // Create a directory for a new profile and get the path to the Preferences.
63 ProfileManager* profile_manager = g_browser_process->profile_manager();
64 const base::FilePath profile_path =
65 profile_manager->GenerateNextProfileDirectoryPath();
66 const base::FilePath prefs_path =
67 profile_path.Append(chrome::kPreferencesFilename);
68 bool dir_exists = base::CreateDirectory(prefs_path.DirName());
69 if (!dir_exists)
70 return nullptr;
71
72 // Convert the passed |prefs| to base::DictionaryValue.
73 std::string replaced_prefs = prefs;
74 base::ReplaceSubstringsAfterOffset(&replaced_prefs, 0, "'", "\"");
75 std::unique_ptr<base::Value> prefs_value =
76 base::JSONReader::Read(replaced_prefs);
77 base::DictionaryValue* prefs_dict = nullptr;
78 prefs_value->GetAsDictionary(&prefs_dict);
79 if (!prefs_dict)
80 return nullptr;
81
82 // Calculate hashes of the requested settings.
83 for (const std::string& name : names_to_update_hashes) {
84 const base::Value* value = nullptr;
85 prefs_dict->Get(name, &value);
86 if (!value)
87 return nullptr;
88 prefs_dict->SetString("protection.macs." + name,
89 hash_calculator_.Calculate(name, value));
90 }
91
92 // Write the result preferences to file.
93 JSONFileValueSerializer json_serializer(prefs_path);
94 if (!json_serializer.Serialize(*prefs_dict))
95 return nullptr;
96
97 // Finally create a profile.
98 Profile* profile = Profile::CreateProfile(profile_path, NULL,
99 Profile::CREATE_MODE_SYNCHRONOUS);
100 if (profile)
101 profile_manager->RegisterTestingProfile(profile, false, false);
102 return profile;
103 }
104
105 // Check that preferences that can influence default search provider choice
106 // are protected.
107 IN_PROC_BROWSER_TEST_F(TURLServicePrefsProtectBrowsertest,
108 ChangeDefaultSearchProvider) {
109 struct TestCase {
110 const std::string pref_to_protect;
111 const std::vector<std::string> names_to_update_hashes;
112 const std::vector<base::string16> expected_keywords;
113 const std::vector<base::string16> not_expected_keywords;
114 };
115
116 constexpr char* default_search_provider_data =
117 "{"
118 " 'default_search_provider_data' :"
119 " {"
120 " 'template_url_data' :"
121 " {"
122 " 'keyword' : 'my_search',"
123 " 'short_name' : 'My Search',"
124 " 'url' : '{google:baseURL}search?q=my+{searchTerms}'"
125 " }"
126 " }"
127 "}";
128 constexpr char* search_provider_overrides =
129 "{"
130 " 'search_provider_overrides' : ["
131 " {"
132 " 'keyword' : 'my_search',"
133 " 'name' : 'My Search',"
134 " 'search_url' : '{google:baseURL}search?q=my+{searchTerms}',"
135 " 'encoding' : 'utf-8',"
136 " 'favicon_url' : 'http://www.google.com/favicon.ico',"
137 " 'id' : 1"
138 " },"
139 " {"
140 " 'keyword' : 'my_search2',"
141 " 'name' : 'My Search2',"
142 " 'search_url' : '{google:baseURL}search?q=my+{searchTerms}+2',"
143 " 'encoding' : 'utf-8',"
144 " 'favicon_url' : 'http://www.google.com/favicon.ico',"
145 " 'id' : 2"
146 " }"
147 "]"
148 "}";
149 constexpr char* default_search_provider =
150 "{"
151 " 'default_search_provider' :"
152 " {"
153 " 'keyword' : 'my_search',"
154 " 'name' : 'My Search',"
155 " 'search_url' : '{google:baseURL}search?q=my+{searchTerms}'"
156 " }"
157 "}";
158
159 TemplateURLService* turl_service =
160 TemplateURLServiceFactory::GetForProfile(browser()->profile());
161 ASSERT_TRUE(turl_service);
162 search_test_utils::WaitForTemplateURLServiceToLoad(turl_service);
163 // Get keyword for default search provider. It will be set for cases where
164 // protection check fail.
Peter Kasting 2016/11/24 19:39:48 Nit: This comment probably belongs above this whol
Alexander Yashkin 2016/11/25 09:33:34 Thanks, changed to your variant.
165 const base::string16 default_keyword = turl_service->
166 GetDefaultSearchProvider()->keyword();
Peter Kasting 2016/11/24 19:39:48 Nit: Prefer to wrap after '=' where there's alread
Alexander Yashkin 2016/11/25 09:33:34 Fixed, thanks.
167
168 const base::string16 my_search = base::ASCIIToUTF16("my_search");
169 const base::string16 my_search2 = base::ASCIIToUTF16("my_search2");
170
171 const TestCase test_cases[] = {
172 {default_search_provider_data,
173 {"default_search_provider_data.template_url_data"},
174 {my_search},
175 {}},
176 {default_search_provider_data, {}, {default_keyword}, {my_search}},
177 {search_provider_overrides,
178 {"search_provider_overrides"},
179 {my_search, my_search2},
180 {}},
181 {search_provider_overrides,
182 {},
183 {default_keyword},
184 {my_search, my_search2}},
185 {default_search_provider,
186 {"default_search_provider.keyword", "default_search_provider.name",
187 "default_search_provider.search_url"},
188 {my_search},
189 {}},
190 {default_search_provider, {}, {default_keyword}, {my_search}},
191 };
192
193 for (size_t i = 0; i != arraysize(test_cases); ++i) {
194 const TestCase& test_case = test_cases[i];
195 // Create profile with pref to protect and update hashes for
196 // protected prefs.
197 Profile* profile = CreateProfileWithPrefs(test_case.pref_to_protect,
198 test_case.names_to_update_hashes);
199 ASSERT_TRUE(profile);
200
201 turl_service = TemplateURLServiceFactory::GetForProfile(profile);
202 search_test_utils::WaitForTemplateURLServiceToLoad(turl_service);
203
204 TemplateURL* dse = turl_service->GetDefaultSearchProvider();
205 ASSERT_TRUE(dse) << "Test case i=" << i;
206
207 ASSERT_FALSE(test_case.expected_keywords.empty());
208 EXPECT_EQ(test_case.expected_keywords[0], dse->keyword()) << "i=" << i;
209
210 for (const base::string16& keyword : test_case.expected_keywords) {
211 EXPECT_TRUE(turl_service->GetTemplateURLForKeyword(keyword))
212 << "keyword=" << keyword << ", i=" << i;
213 }
214
215 for (const base::string16& keyword : test_case.not_expected_keywords) {
216 EXPECT_FALSE(turl_service->GetTemplateURLForKeyword(keyword))
217 << "keyword=" << keyword << ", i=" << i;
218 }
219 }
220 }
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