Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / search_engines / default_search_policy_handler_unittest.cc
blob954984be3a0fd6385bfaad42ebb39841227461f7
1 // Copyright 2014 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "components/policy/core/browser/configuration_policy_pref_store.h"
7 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
8 #include "components/search_engines/default_search_manager.h"
9 #include "components/search_engines/default_search_policy_handler.h"
10 #include "components/search_engines/search_engines_pref_names.h"
11 #include "policy/policy_constants.h"
13 namespace {
14 // TODO(caitkp): Should we find a way to route this through DefaultSearchManager
15 // to avoid hardcoding this here?
16 const char kDefaultSearchProviderData[] =
17 "default_search_provider_data.template_url_data";
18 } // namespace
20 namespace policy {
22 class DefaultSearchPolicyHandlerTest
23 : public ConfigurationPolicyPrefStoreTest {
24 public:
25 DefaultSearchPolicyHandlerTest() {
26 default_alternate_urls_.AppendString(
27 "http://www.google.com/#q={searchTerms}");
28 default_alternate_urls_.AppendString(
29 "http://www.google.com/search#q={searchTerms}");
32 void SetUp() override {
33 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
34 new DefaultSearchPolicyHandler));
37 protected:
38 static const char kSearchURL[];
39 static const char kSuggestURL[];
40 static const char kIconURL[];
41 static const char kName[];
42 static const char kKeyword[];
43 static const char kReplacementKey[];
44 static const char kImageURL[];
45 static const char kImageParams[];
46 static const char kNewTabURL[];
47 static const char kFileSearchURL[];
48 static const char kHostName[];
50 // Build a default search policy by setting search-related keys in |policy| to
51 // reasonable values. You can update any of the keys after calling this
52 // method.
53 void BuildDefaultSearchPolicy(PolicyMap* policy);
55 base::ListValue default_alternate_urls_;
58 const char DefaultSearchPolicyHandlerTest::kSearchURL[] =
59 "http://test.com/search?t={searchTerms}";
60 const char DefaultSearchPolicyHandlerTest::kSuggestURL[] =
61 "http://test.com/sugg?={searchTerms}";
62 const char DefaultSearchPolicyHandlerTest::kIconURL[] =
63 "http://test.com/icon.jpg";
64 const char DefaultSearchPolicyHandlerTest::kName[] =
65 "MyName";
66 const char DefaultSearchPolicyHandlerTest::kKeyword[] =
67 "MyKeyword";
68 const char DefaultSearchPolicyHandlerTest::kReplacementKey[] =
69 "espv";
70 const char DefaultSearchPolicyHandlerTest::kImageURL[] =
71 "http://test.com/searchbyimage/upload";
72 const char DefaultSearchPolicyHandlerTest::kImageParams[] =
73 "image_content=content,image_url=http://test.com/test.png";
74 const char DefaultSearchPolicyHandlerTest::kNewTabURL[] =
75 "http://test.com/newtab";
76 const char DefaultSearchPolicyHandlerTest::kFileSearchURL[] =
77 "file:///c:/path/to/search?t={searchTerms}";
78 const char DefaultSearchPolicyHandlerTest::kHostName[] = "test.com";
80 void DefaultSearchPolicyHandlerTest::
81 BuildDefaultSearchPolicy(PolicyMap* policy) {
82 base::ListValue* encodings = new base::ListValue();
83 encodings->AppendString("UTF-16");
84 encodings->AppendString("UTF-8");
85 policy->Set(key::kDefaultSearchProviderEnabled,
86 POLICY_LEVEL_MANDATORY,
87 POLICY_SCOPE_USER,
88 new base::FundamentalValue(true),
89 NULL);
90 policy->Set(key::kDefaultSearchProviderSearchURL,
91 POLICY_LEVEL_MANDATORY,
92 POLICY_SCOPE_USER,
93 new base::StringValue(kSearchURL),
94 NULL);
95 policy->Set(key::kDefaultSearchProviderName,
96 POLICY_LEVEL_MANDATORY,
97 POLICY_SCOPE_USER,
98 new base::StringValue(kName),
99 NULL);
100 policy->Set(key::kDefaultSearchProviderKeyword,
101 POLICY_LEVEL_MANDATORY,
102 POLICY_SCOPE_USER,
103 new base::StringValue(kKeyword),
104 NULL);
105 policy->Set(key::kDefaultSearchProviderSuggestURL,
106 POLICY_LEVEL_MANDATORY,
107 POLICY_SCOPE_USER,
108 new base::StringValue(kSuggestURL),
109 NULL);
110 policy->Set(key::kDefaultSearchProviderIconURL,
111 POLICY_LEVEL_MANDATORY,
112 POLICY_SCOPE_USER,
113 new base::StringValue(kIconURL),
114 NULL);
115 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY,
116 POLICY_SCOPE_USER, encodings, NULL);
117 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY,
118 POLICY_SCOPE_USER, default_alternate_urls_.DeepCopy(), NULL);
119 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey,
120 POLICY_LEVEL_MANDATORY,
121 POLICY_SCOPE_USER,
122 new base::StringValue(kReplacementKey),
123 NULL);
124 policy->Set(key::kDefaultSearchProviderImageURL,
125 POLICY_LEVEL_MANDATORY,
126 POLICY_SCOPE_USER,
127 new base::StringValue(kImageURL),
128 NULL);
129 policy->Set(key::kDefaultSearchProviderImageURLPostParams,
130 POLICY_LEVEL_MANDATORY,
131 POLICY_SCOPE_USER,
132 new base::StringValue(kImageParams),
133 NULL);
134 policy->Set(key::kDefaultSearchProviderNewTabURL,
135 POLICY_LEVEL_MANDATORY,
136 POLICY_SCOPE_USER,
137 new base::StringValue(kNewTabURL),
138 NULL);
141 // Checks that if the default search policy is missing, that no elements of the
142 // default search policy will be present.
143 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) {
144 PolicyMap policy;
145 BuildDefaultSearchPolicy(&policy);
146 policy.Erase(key::kDefaultSearchProviderSearchURL);
147 UpdateProviderPolicy(policy);
149 const base::Value* temp = nullptr;
150 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp));
153 // Checks that if the default search policy is invalid, that no elements of the
154 // default search policy will be present.
155 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) {
156 PolicyMap policy;
157 BuildDefaultSearchPolicy(&policy);
158 const char bad_search_url[] = "http://test.com/noSearchTerms";
159 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
160 POLICY_SCOPE_USER, new base::StringValue(bad_search_url), NULL);
161 UpdateProviderPolicy(policy);
163 const base::Value* temp = nullptr;
164 EXPECT_FALSE(store_->GetValue(kDefaultSearchProviderData, &temp));
167 // Checks that for a fully defined search policy, all elements have been
168 // read properly into the dictionary pref.
169 TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) {
170 PolicyMap policy;
171 BuildDefaultSearchPolicy(&policy);
172 UpdateProviderPolicy(policy);
174 const base::Value* temp = NULL;
175 const base::DictionaryValue* dictionary;
176 std::string value;
177 const base::ListValue* list_value;
178 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
179 temp->GetAsDictionary(&dictionary);
181 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
182 EXPECT_EQ(kSearchURL, value);
183 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
184 EXPECT_EQ(kName, value);
185 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
186 EXPECT_EQ(kKeyword, value);
188 EXPECT_TRUE(
189 dictionary->GetString(DefaultSearchManager::kSuggestionsURL, &value));
190 EXPECT_EQ(kSuggestURL, value);
191 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kFaviconURL, &value));
192 EXPECT_EQ(kIconURL, value);
194 base::ListValue encodings;
195 encodings.AppendString("UTF-16");
196 encodings.AppendString("UTF-8");
198 EXPECT_TRUE(
199 dictionary->GetList(DefaultSearchManager::kInputEncodings, &list_value));
200 EXPECT_TRUE(encodings.Equals(list_value));
202 EXPECT_TRUE(
203 dictionary->GetList(DefaultSearchManager::kAlternateURLs, &list_value));
204 EXPECT_TRUE(default_alternate_urls_.Equals(list_value));
206 EXPECT_TRUE(dictionary->GetString(
207 DefaultSearchManager::kSearchTermsReplacementKey, &value));
208 EXPECT_EQ(kReplacementKey, value);
210 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kImageURL, &value));
211 EXPECT_EQ(kImageURL, value);
213 EXPECT_TRUE(
214 dictionary->GetString(DefaultSearchManager::kImageURLPostParams, &value));
215 EXPECT_EQ(kImageParams, value);
217 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kSearchURLPostParams,
218 &value));
219 EXPECT_EQ(std::string(), value);
221 EXPECT_TRUE(dictionary->GetString(
222 DefaultSearchManager::kSuggestionsURLPostParams, &value));
223 EXPECT_EQ(std::string(), value);
225 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kInstantURLPostParams,
226 &value));
227 EXPECT_EQ(std::string(), value);
230 // Checks that disabling default search is properly reflected the dictionary
231 // pref.
232 TEST_F(DefaultSearchPolicyHandlerTest, Disabled) {
233 PolicyMap policy;
234 policy.Set(key::kDefaultSearchProviderEnabled,
235 POLICY_LEVEL_MANDATORY,
236 POLICY_SCOPE_USER,
237 new base::FundamentalValue(false),
238 NULL);
239 UpdateProviderPolicy(policy);
240 const base::Value* temp = NULL;
241 const base::DictionaryValue* dictionary;
242 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
243 temp->GetAsDictionary(&dictionary);
244 bool disabled = false;
245 EXPECT_TRUE(dictionary->GetBoolean(DefaultSearchManager::kDisabledByPolicy,
246 &disabled));
247 EXPECT_TRUE(disabled);
250 // Checks that if the policy for default search is valid, i.e. there's a
251 // search URL, that all the elements have been given proper defaults.
252 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) {
253 PolicyMap policy;
254 policy.Set(key::kDefaultSearchProviderEnabled,
255 POLICY_LEVEL_MANDATORY,
256 POLICY_SCOPE_USER,
257 new base::FundamentalValue(true),
258 NULL);
259 policy.Set(key::kDefaultSearchProviderSearchURL,
260 POLICY_LEVEL_MANDATORY,
261 POLICY_SCOPE_USER,
262 new base::StringValue(kSearchURL),
263 NULL);
264 UpdateProviderPolicy(policy);
266 const base::Value* temp = NULL;
267 const base::DictionaryValue* dictionary;
268 std::string value;
269 const base::ListValue* list_value;
270 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
271 temp->GetAsDictionary(&dictionary);
273 // Name and keyword should be derived from host.
274 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
275 EXPECT_EQ(kSearchURL, value);
276 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
277 EXPECT_EQ(kHostName, value);
278 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
279 EXPECT_EQ(kHostName, value);
281 // Everything else should be set to the default value.
282 EXPECT_TRUE(
283 dictionary->GetString(DefaultSearchManager::kSuggestionsURL, &value));
284 EXPECT_EQ(std::string(), value);
285 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kFaviconURL, &value));
286 EXPECT_EQ(std::string(), value);
287 EXPECT_TRUE(
288 dictionary->GetList(DefaultSearchManager::kInputEncodings, &list_value));
289 EXPECT_TRUE(base::ListValue().Equals(list_value));
290 EXPECT_TRUE(
291 dictionary->GetList(DefaultSearchManager::kAlternateURLs, &list_value));
292 EXPECT_TRUE(base::ListValue().Equals(list_value));
293 EXPECT_TRUE(dictionary->GetString(
294 DefaultSearchManager::kSearchTermsReplacementKey, &value));
295 EXPECT_EQ(std::string(), value);
296 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kImageURL, &value));
297 EXPECT_EQ(std::string(), value);
298 EXPECT_TRUE(
299 dictionary->GetString(DefaultSearchManager::kImageURLPostParams, &value));
300 EXPECT_EQ(std::string(), value);
301 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kSearchURLPostParams,
302 &value));
303 EXPECT_EQ(std::string(), value);
304 EXPECT_TRUE(dictionary->GetString(
305 DefaultSearchManager::kSuggestionsURLPostParams, &value));
306 EXPECT_EQ(std::string(), value);
307 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kInstantURLPostParams,
308 &value));
309 EXPECT_EQ(std::string(), value);
312 // Checks that setting a file URL as the default search is reflected properly in
313 // the dictionary pref.
314 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) {
315 PolicyMap policy;
316 policy.Set(key::kDefaultSearchProviderEnabled,
317 POLICY_LEVEL_MANDATORY,
318 POLICY_SCOPE_USER,
319 new base::FundamentalValue(true),
320 NULL);
321 policy.Set(key::kDefaultSearchProviderSearchURL,
322 POLICY_LEVEL_MANDATORY,
323 POLICY_SCOPE_USER,
324 new base::StringValue(kFileSearchURL),
325 NULL);
326 UpdateProviderPolicy(policy);
328 const base::Value* temp = NULL;
329 const base::DictionaryValue* dictionary;
330 std::string value;
332 EXPECT_TRUE(store_->GetValue(kDefaultSearchProviderData, &temp));
333 temp->GetAsDictionary(&dictionary);
335 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
336 EXPECT_EQ(kFileSearchURL, value);
337 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
338 EXPECT_EQ("_", value);
339 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
340 EXPECT_EQ("_", value);
342 } // namespace policy