1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/search_engines/default_search_policy_handler.h"
9 #include "base/memory/ptr_util.h"
10 #include "components/policy/core/browser/configuration_policy_pref_store.h"
11 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
12 #include "components/policy/core/common/policy_types.h"
13 #include "components/policy/policy_constants.h"
14 #include "components/search_engines/default_search_manager.h"
15 #include "components/search_engines/search_engines_pref_names.h"
19 class DefaultSearchPolicyHandlerTest
20 : public ConfigurationPolicyPrefStoreTest {
22 DefaultSearchPolicyHandlerTest() {
23 default_alternate_urls_.Append("http://www.google.com/#q={searchTerms}");
24 default_alternate_urls_.Append(
25 "http://www.google.com/search#q={searchTerms}");
28 void SetUp() override {
29 handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>(
30 new DefaultSearchPolicyHandler));
34 static const char kSearchURL[];
35 static const char kSuggestURL[];
36 static const char kIconURL[];
37 static const char kName[];
38 static const char kKeyword[];
39 static const char kReplacementKey[];
40 static const char kImageURL[];
41 static const char kImageParams[];
42 static const char kNewTabURL[];
43 static const char kFileSearchURL[];
44 static const char kHostName[];
46 // Build a default search policy by setting search-related keys in |policy| to
47 // reasonable values. You can update any of the keys after calling this
49 void BuildDefaultSearchPolicy(PolicyMap* policy);
51 base::Value::List default_alternate_urls_;
54 const char DefaultSearchPolicyHandlerTest::kSearchURL[] =
55 "http://test.com/search?t={searchTerms}";
56 const char DefaultSearchPolicyHandlerTest::kSuggestURL[] =
57 "http://test.com/sugg?={searchTerms}";
58 const char DefaultSearchPolicyHandlerTest::kIconURL[] =
59 "http://test.com/icon.jpg";
60 const char DefaultSearchPolicyHandlerTest::kName[] =
62 const char DefaultSearchPolicyHandlerTest::kKeyword[] =
64 const char DefaultSearchPolicyHandlerTest::kImageURL[] =
65 "http://test.com/searchbyimage/upload";
66 const char DefaultSearchPolicyHandlerTest::kImageParams[] =
67 "image_content=content,image_url=http://test.com/test.png";
68 const char DefaultSearchPolicyHandlerTest::kNewTabURL[] =
69 "http://test.com/newtab";
70 const char DefaultSearchPolicyHandlerTest::kFileSearchURL[] =
71 "file:///c:/path/to/search?t={searchTerms}";
72 const char DefaultSearchPolicyHandlerTest::kHostName[] = "test.com";
74 void DefaultSearchPolicyHandlerTest::
75 BuildDefaultSearchPolicy(PolicyMap* policy) {
76 base::Value::List encodings;
77 encodings.Append("UTF-16");
78 encodings.Append("UTF-8");
79 policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
80 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
82 policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
83 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSearchURL),
85 policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY,
86 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kName),
88 policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY,
89 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kKeyword),
91 policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY,
92 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSuggestURL),
94 policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY,
95 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kIconURL),
97 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY,
98 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
99 base::Value(std::move(encodings)), nullptr);
100 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY,
101 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
102 base::Value(default_alternate_urls_.Clone()), nullptr);
103 policy->Set(key::kDefaultSearchProviderImageURL, POLICY_LEVEL_MANDATORY,
104 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kImageURL),
106 policy->Set(key::kDefaultSearchProviderImageURLPostParams,
107 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
108 base::Value(kImageParams), nullptr);
109 policy->Set(key::kDefaultSearchProviderNewTabURL, POLICY_LEVEL_MANDATORY,
110 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kNewTabURL),
114 // Checks that if the default search policy is missing, that no elements of the
115 // default search policy will be present.
116 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) {
118 BuildDefaultSearchPolicy(&policy);
119 policy.Erase(key::kDefaultSearchProviderSearchURL);
120 UpdateProviderPolicy(policy);
122 const base::Value* temp = nullptr;
123 EXPECT_FALSE(store_->GetValue(
124 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
127 // Checks that if the default search policy is invalid, that no elements of the
128 // default search policy will be present.
129 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) {
131 BuildDefaultSearchPolicy(&policy);
132 const char bad_search_url[] = "http://test.com/noSearchTerms";
133 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
134 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
135 base::Value(bad_search_url), nullptr);
136 UpdateProviderPolicy(policy);
138 const base::Value* temp = nullptr;
139 EXPECT_FALSE(store_->GetValue(
140 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
143 // Checks that if the default search policy has invalid type for elements,
144 // that no elements of the default search policy will be present in prefs.
145 TEST_F(DefaultSearchPolicyHandlerTest, InvalidType) {
146 // List of policies defined in test policy.
147 const char* kPolicyNamesToCheck[] = {
148 key::kDefaultSearchProviderEnabled,
149 key::kDefaultSearchProviderName,
150 key::kDefaultSearchProviderKeyword,
151 key::kDefaultSearchProviderSearchURL,
152 key::kDefaultSearchProviderSuggestURL,
153 key::kDefaultSearchProviderIconURL,
154 key::kDefaultSearchProviderEncodings,
155 key::kDefaultSearchProviderAlternateURLs,
156 key::kDefaultSearchProviderImageURL,
157 key::kDefaultSearchProviderNewTabURL,
158 key::kDefaultSearchProviderImageURLPostParams};
161 BuildDefaultSearchPolicy(&policy);
163 for (auto* policy_name : kPolicyNamesToCheck) {
164 // Check that policy can be successfully applied first.
165 UpdateProviderPolicy(policy);
166 const base::Value* temp = nullptr;
167 EXPECT_TRUE(store_->GetValue(
168 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
170 // It's safe to use `GetValueUnsafe()` as multiple policy types are handled.
171 auto old_value = policy.GetValueUnsafe(policy_name)->Clone();
172 // BinaryValue is not supported in any current default search policy params.
173 // Try changing policy param to BinaryValue and check that policy becomes
175 policy.Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
176 POLICY_SOURCE_CLOUD, base::Value(base::Value::Type::BINARY),
178 UpdateProviderPolicy(policy);
180 EXPECT_FALSE(store_->GetValue(
181 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp))
182 << "Policy type check failed " << policy_name;
183 // Return old value to policy map.
184 policy.Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
185 POLICY_SOURCE_CLOUD, std::move(old_value), nullptr);
189 // Checks that for a fully defined search policy, all elements have been
190 // read properly into the dictionary pref.
191 TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) {
193 BuildDefaultSearchPolicy(&policy);
194 UpdateProviderPolicy(policy);
196 const base::Value* temp = nullptr;
197 EXPECT_TRUE(store_->GetValue(
198 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
199 const base::Value::Dict* dictionary = temp->GetIfDict();
200 ASSERT_TRUE(dictionary);
202 const std::string* value = nullptr;
203 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kURL));
204 EXPECT_EQ(kSearchURL, *value);
205 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kShortName));
206 EXPECT_EQ(kName, *value);
207 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kKeyword));
208 EXPECT_EQ(kKeyword, *value);
211 value = dictionary->FindString(DefaultSearchManager::kSuggestionsURL));
212 EXPECT_EQ(kSuggestURL, *value);
214 dictionary->FindString(DefaultSearchManager::kFaviconURL));
215 EXPECT_EQ(kIconURL, *value);
217 base::Value::List encodings;
218 encodings.Append("UTF-16");
219 encodings.Append("UTF-8");
220 const base::Value::List* list_value = nullptr;
221 ASSERT_TRUE(list_value =
222 dictionary->FindList(DefaultSearchManager::kInputEncodings));
223 EXPECT_EQ(encodings, *list_value);
225 ASSERT_TRUE(list_value =
226 dictionary->FindList(DefaultSearchManager::kAlternateURLs));
227 EXPECT_EQ(default_alternate_urls_, *list_value);
229 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kImageURL));
230 EXPECT_EQ(kImageURL, *value);
232 ASSERT_TRUE(value = dictionary->FindString(
233 DefaultSearchManager::kImageURLPostParams));
234 EXPECT_EQ(kImageParams, *value);
236 ASSERT_TRUE(value = dictionary->FindString(
237 DefaultSearchManager::kSearchURLPostParams));
238 EXPECT_EQ(std::string(), *value);
240 EXPECT_TRUE(value = dictionary->FindString(
241 DefaultSearchManager::kSuggestionsURLPostParams));
242 EXPECT_EQ(std::string(), *value);
245 // Checks that disabling default search is properly reflected the dictionary
247 TEST_F(DefaultSearchPolicyHandlerTest, DisabledByPolicy) {
249 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
250 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
252 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
253 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
254 base::Value("http://a/?{searchTerms}"), nullptr);
255 UpdateProviderPolicy(policy);
256 const base::Value* temp = nullptr;
257 // Ignore any other search provider related policy in this case.
258 EXPECT_FALSE(store_->GetValue(DefaultSearchManager::kURL, &temp));
260 EXPECT_TRUE(store_->GetValue(
261 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
262 const base::Value::Dict* dictionary = temp->GetIfDict();
263 ASSERT_TRUE(dictionary);
264 absl::optional<bool> disabled =
265 dictionary->FindBool(DefaultSearchManager::kDisabledByPolicy);
266 EXPECT_TRUE(disabled.has_value());
267 EXPECT_TRUE(disabled.value());
270 // Check that when the default search enabled policy is not set, all other
271 // default search-related policies are ignored.
272 TEST_F(DefaultSearchPolicyHandlerTest, DisabledByPolicyNotSet) {
274 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
275 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
276 base::Value("http://a/?{searchTerms}"), nullptr);
277 UpdateProviderPolicy(policy);
278 const base::Value* temp = nullptr;
279 EXPECT_FALSE(store_->GetValue(
280 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
281 EXPECT_FALSE(store_->GetValue(DefaultSearchManager::kURL, &temp));
284 // Checks that if the policy for default search is valid, i.e. there's a
285 // search URL, that all the elements have been given proper defaults.
286 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) {
288 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
289 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
291 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
292 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSearchURL),
294 UpdateProviderPolicy(policy);
296 const base::Value* temp = nullptr;
297 EXPECT_TRUE(store_->GetValue(
298 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
299 const base::Value::Dict* dictionary = temp->GetIfDict();
300 ASSERT_TRUE(dictionary);
302 // Name and keyword should be derived from host.
303 const std::string* value = nullptr;
304 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kURL));
305 EXPECT_EQ(kSearchURL, *value);
306 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kShortName));
307 EXPECT_EQ(kHostName, *value);
308 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kKeyword));
309 EXPECT_EQ(kHostName, *value);
311 // Everything else should be set to the default value.
313 value = dictionary->FindString(DefaultSearchManager::kSuggestionsURL));
314 EXPECT_EQ(std::string(), *value);
316 dictionary->FindString(DefaultSearchManager::kFaviconURL));
317 EXPECT_EQ(std::string(), *value);
318 const base::Value::List* list_value = nullptr;
319 ASSERT_TRUE(list_value =
320 dictionary->FindList(DefaultSearchManager::kInputEncodings));
321 EXPECT_EQ(base::Value::List(), *list_value);
322 ASSERT_TRUE(list_value =
323 dictionary->FindList(DefaultSearchManager::kAlternateURLs));
324 EXPECT_EQ(base::Value::List(), *list_value);
325 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kImageURL));
326 EXPECT_EQ(std::string(), *value);
327 ASSERT_TRUE(value = dictionary->FindString(
328 DefaultSearchManager::kImageURLPostParams));
329 EXPECT_EQ(std::string(), *value);
330 ASSERT_TRUE(value = dictionary->FindString(
331 DefaultSearchManager::kSearchURLPostParams));
332 EXPECT_EQ(std::string(), *value);
333 ASSERT_TRUE(value = dictionary->FindString(
334 DefaultSearchManager::kSuggestionsURLPostParams));
335 EXPECT_EQ(std::string(), *value);
338 // Checks that setting a file URL as the default search is reflected properly in
339 // the dictionary pref.
340 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) {
342 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
343 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
345 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
346 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
347 base::Value(kFileSearchURL), nullptr);
348 UpdateProviderPolicy(policy);
350 const base::Value* temp = nullptr;
352 EXPECT_TRUE(store_->GetValue(
353 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
354 const base::Value::Dict* dictionary = temp->GetIfDict();
355 ASSERT_TRUE(dictionary);
357 const std::string* value = nullptr;
358 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kURL));
359 EXPECT_EQ(kFileSearchURL, *value);
360 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kShortName));
361 EXPECT_EQ("_", *value);
362 ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kKeyword));
363 EXPECT_EQ("_", *value);
366 } // namespace policy