Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / default_search_policy_handler_unittest.cc
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.
4
5 #include "components/search_engines/default_search_policy_handler.h"
6
7 #include <memory>
8
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"
16
17 namespace policy {
18
19 class DefaultSearchPolicyHandlerTest
20     : public ConfigurationPolicyPrefStoreTest {
21  public:
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}");
26   }
27
28   void SetUp() override {
29     handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>(
30         new DefaultSearchPolicyHandler));
31   }
32
33  protected:
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[];
45
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
48   // method.
49   void BuildDefaultSearchPolicy(PolicyMap* policy);
50
51   base::Value::List default_alternate_urls_;
52 };
53
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[] =
61     "MyName";
62 const char DefaultSearchPolicyHandlerTest::kKeyword[] =
63     "MyKeyword";
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";
73
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),
81               nullptr);
82   policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
83               POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSearchURL),
84               nullptr);
85   policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY,
86               POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kName),
87               nullptr);
88   policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY,
89               POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kKeyword),
90               nullptr);
91   policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY,
92               POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSuggestURL),
93               nullptr);
94   policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY,
95               POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kIconURL),
96               nullptr);
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),
105               nullptr);
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),
111               nullptr);
112 }
113
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) {
117   PolicyMap policy;
118   BuildDefaultSearchPolicy(&policy);
119   policy.Erase(key::kDefaultSearchProviderSearchURL);
120   UpdateProviderPolicy(policy);
121
122   const base::Value* temp = nullptr;
123   EXPECT_FALSE(store_->GetValue(
124       DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
125 }
126
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) {
130   PolicyMap policy;
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);
137
138   const base::Value* temp = nullptr;
139   EXPECT_FALSE(store_->GetValue(
140       DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
141 }
142
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};
159
160   PolicyMap policy;
161   BuildDefaultSearchPolicy(&policy);
162
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));
169
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
174     // invalid.
175     policy.Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
176                POLICY_SOURCE_CLOUD, base::Value(base::Value::Type::BINARY),
177                nullptr);
178     UpdateProviderPolicy(policy);
179
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);
186   }
187 }
188
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) {
192   PolicyMap policy;
193   BuildDefaultSearchPolicy(&policy);
194   UpdateProviderPolicy(policy);
195
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);
201
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);
209
210   ASSERT_TRUE(
211       value = dictionary->FindString(DefaultSearchManager::kSuggestionsURL));
212   EXPECT_EQ(kSuggestURL, *value);
213   EXPECT_TRUE(value =
214                   dictionary->FindString(DefaultSearchManager::kFaviconURL));
215   EXPECT_EQ(kIconURL, *value);
216
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);
224
225   ASSERT_TRUE(list_value =
226                   dictionary->FindList(DefaultSearchManager::kAlternateURLs));
227   EXPECT_EQ(default_alternate_urls_, *list_value);
228
229   ASSERT_TRUE(value = dictionary->FindString(DefaultSearchManager::kImageURL));
230   EXPECT_EQ(kImageURL, *value);
231
232   ASSERT_TRUE(value = dictionary->FindString(
233                   DefaultSearchManager::kImageURLPostParams));
234   EXPECT_EQ(kImageParams, *value);
235
236   ASSERT_TRUE(value = dictionary->FindString(
237                   DefaultSearchManager::kSearchURLPostParams));
238   EXPECT_EQ(std::string(), *value);
239
240   EXPECT_TRUE(value = dictionary->FindString(
241                   DefaultSearchManager::kSuggestionsURLPostParams));
242   EXPECT_EQ(std::string(), *value);
243 }
244
245 // Checks that disabling default search is properly reflected the dictionary
246 // pref.
247 TEST_F(DefaultSearchPolicyHandlerTest, DisabledByPolicy) {
248   PolicyMap policy;
249   policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
250              POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
251              nullptr);
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));
259
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());
268 }
269
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) {
273   PolicyMap policy;
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));
282 }
283
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) {
287   PolicyMap policy;
288   policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
289              POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
290              nullptr);
291   policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
292              POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSearchURL),
293              nullptr);
294   UpdateProviderPolicy(policy);
295
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);
301
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);
310
311   // Everything else should be set to the default value.
312   ASSERT_TRUE(
313       value = dictionary->FindString(DefaultSearchManager::kSuggestionsURL));
314   EXPECT_EQ(std::string(), *value);
315   ASSERT_TRUE(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);
336 }
337
338 // Checks that setting a file URL as the default search is reflected properly in
339 // the dictionary pref.
340 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) {
341   PolicyMap policy;
342   policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
343              POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
344              nullptr);
345   policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
346              POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
347              base::Value(kFileSearchURL), nullptr);
348   UpdateProviderPolicy(policy);
349
350   const base::Value* temp = nullptr;
351
352   EXPECT_TRUE(store_->GetValue(
353       DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
354   const base::Value::Dict* dictionary = temp->GetIfDict();
355   ASSERT_TRUE(dictionary);
356
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);
364 }
365
366 }  // namespace policy