default_search_policy_handler_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.
  4. #include "components/search_engines/default_search_policy_handler.h"
  5. #include <memory>
  6. #include "base/memory/ptr_util.h"
  7. #include "components/policy/core/browser/configuration_policy_pref_store.h"
  8. #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
  9. #include "components/policy/core/common/policy_types.h"
  10. #include "components/policy/policy_constants.h"
  11. #include "components/search_engines/default_search_manager.h"
  12. #include "components/search_engines/search_engines_pref_names.h"
  13. namespace policy {
  14. class DefaultSearchPolicyHandlerTest
  15. : public ConfigurationPolicyPrefStoreTest {
  16. public:
  17. DefaultSearchPolicyHandlerTest() {
  18. default_alternate_urls_.Append("http://www.google.com/#q={searchTerms}");
  19. default_alternate_urls_.Append(
  20. "http://www.google.com/search#q={searchTerms}");
  21. }
  22. void SetUp() override {
  23. handler_list_.AddHandler(base::WrapUnique<ConfigurationPolicyHandler>(
  24. new DefaultSearchPolicyHandler));
  25. }
  26. protected:
  27. static const char kSearchURL[];
  28. static const char kSuggestURL[];
  29. static const char kIconURL[];
  30. static const char kName[];
  31. static const char kKeyword[];
  32. static const char kReplacementKey[];
  33. static const char kImageURL[];
  34. static const char kImageParams[];
  35. static const char kNewTabURL[];
  36. static const char kFileSearchURL[];
  37. static const char kHostName[];
  38. // Build a default search policy by setting search-related keys in |policy| to
  39. // reasonable values. You can update any of the keys after calling this
  40. // method.
  41. void BuildDefaultSearchPolicy(PolicyMap* policy);
  42. base::ListValue default_alternate_urls_;
  43. };
  44. const char DefaultSearchPolicyHandlerTest::kSearchURL[] =
  45. "http://test.com/search?t={searchTerms}";
  46. const char DefaultSearchPolicyHandlerTest::kSuggestURL[] =
  47. "http://test.com/sugg?={searchTerms}";
  48. const char DefaultSearchPolicyHandlerTest::kIconURL[] =
  49. "http://test.com/icon.jpg";
  50. const char DefaultSearchPolicyHandlerTest::kName[] =
  51. "MyName";
  52. const char DefaultSearchPolicyHandlerTest::kKeyword[] =
  53. "MyKeyword";
  54. const char DefaultSearchPolicyHandlerTest::kImageURL[] =
  55. "http://test.com/searchbyimage/upload";
  56. const char DefaultSearchPolicyHandlerTest::kImageParams[] =
  57. "image_content=content,image_url=http://test.com/test.png";
  58. const char DefaultSearchPolicyHandlerTest::kNewTabURL[] =
  59. "http://test.com/newtab";
  60. const char DefaultSearchPolicyHandlerTest::kFileSearchURL[] =
  61. "file:///c:/path/to/search?t={searchTerms}";
  62. const char DefaultSearchPolicyHandlerTest::kHostName[] = "test.com";
  63. void DefaultSearchPolicyHandlerTest::
  64. BuildDefaultSearchPolicy(PolicyMap* policy) {
  65. base::Value encodings(base::Value::Type::LIST);
  66. encodings.Append("UTF-16");
  67. encodings.Append("UTF-8");
  68. policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
  69. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
  70. nullptr);
  71. policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
  72. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSearchURL),
  73. nullptr);
  74. policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY,
  75. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kName),
  76. nullptr);
  77. policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY,
  78. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kKeyword),
  79. nullptr);
  80. policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY,
  81. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSuggestURL),
  82. nullptr);
  83. policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY,
  84. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kIconURL),
  85. nullptr);
  86. policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY,
  87. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, std::move(encodings),
  88. nullptr);
  89. policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY,
  90. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
  91. default_alternate_urls_.Clone(), nullptr);
  92. policy->Set(key::kDefaultSearchProviderImageURL, POLICY_LEVEL_MANDATORY,
  93. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kImageURL),
  94. nullptr);
  95. policy->Set(key::kDefaultSearchProviderImageURLPostParams,
  96. POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
  97. base::Value(kImageParams), nullptr);
  98. policy->Set(key::kDefaultSearchProviderNewTabURL, POLICY_LEVEL_MANDATORY,
  99. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kNewTabURL),
  100. nullptr);
  101. }
  102. // Checks that if the default search policy is missing, that no elements of the
  103. // default search policy will be present.
  104. TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) {
  105. PolicyMap policy;
  106. BuildDefaultSearchPolicy(&policy);
  107. policy.Erase(key::kDefaultSearchProviderSearchURL);
  108. UpdateProviderPolicy(policy);
  109. const base::Value* temp = nullptr;
  110. EXPECT_FALSE(store_->GetValue(
  111. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  112. }
  113. // Checks that if the default search policy is invalid, that no elements of the
  114. // default search policy will be present.
  115. TEST_F(DefaultSearchPolicyHandlerTest, Invalid) {
  116. PolicyMap policy;
  117. BuildDefaultSearchPolicy(&policy);
  118. const char bad_search_url[] = "http://test.com/noSearchTerms";
  119. policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
  120. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
  121. base::Value(bad_search_url), nullptr);
  122. UpdateProviderPolicy(policy);
  123. const base::Value* temp = nullptr;
  124. EXPECT_FALSE(store_->GetValue(
  125. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  126. }
  127. // Checks that if the default search policy has invalid type for elements,
  128. // that no elements of the default search policy will be present in prefs.
  129. TEST_F(DefaultSearchPolicyHandlerTest, InvalidType) {
  130. // List of policies defined in test policy.
  131. const char* kPolicyNamesToCheck[] = {
  132. key::kDefaultSearchProviderEnabled,
  133. key::kDefaultSearchProviderName,
  134. key::kDefaultSearchProviderKeyword,
  135. key::kDefaultSearchProviderSearchURL,
  136. key::kDefaultSearchProviderSuggestURL,
  137. key::kDefaultSearchProviderIconURL,
  138. key::kDefaultSearchProviderEncodings,
  139. key::kDefaultSearchProviderAlternateURLs,
  140. key::kDefaultSearchProviderImageURL,
  141. key::kDefaultSearchProviderNewTabURL,
  142. key::kDefaultSearchProviderImageURLPostParams};
  143. PolicyMap policy;
  144. BuildDefaultSearchPolicy(&policy);
  145. for (auto* policy_name : kPolicyNamesToCheck) {
  146. // Check that policy can be successfully applied first.
  147. UpdateProviderPolicy(policy);
  148. const base::Value* temp = nullptr;
  149. EXPECT_TRUE(store_->GetValue(
  150. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  151. // It's safe to use `GetValueUnsafe()` as multiple policy types are handled.
  152. auto old_value = policy.GetValueUnsafe(policy_name)->Clone();
  153. // BinaryValue is not supported in any current default search policy params.
  154. // Try changing policy param to BinaryValue and check that policy becomes
  155. // invalid.
  156. policy.Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
  157. POLICY_SOURCE_CLOUD, base::Value(base::Value::Type::BINARY),
  158. nullptr);
  159. UpdateProviderPolicy(policy);
  160. EXPECT_FALSE(store_->GetValue(
  161. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp))
  162. << "Policy type check failed " << policy_name;
  163. // Return old value to policy map.
  164. policy.Set(policy_name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
  165. POLICY_SOURCE_CLOUD, std::move(old_value), nullptr);
  166. }
  167. }
  168. // Checks that for a fully defined search policy, all elements have been
  169. // read properly into the dictionary pref.
  170. TEST_F(DefaultSearchPolicyHandlerTest, FullyDefined) {
  171. PolicyMap policy;
  172. BuildDefaultSearchPolicy(&policy);
  173. UpdateProviderPolicy(policy);
  174. const base::Value* temp = nullptr;
  175. const base::DictionaryValue* dictionary;
  176. std::string value;
  177. const base::ListValue* list_value;
  178. EXPECT_TRUE(store_->GetValue(
  179. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  180. 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);
  187. EXPECT_TRUE(
  188. dictionary->GetString(DefaultSearchManager::kSuggestionsURL, &value));
  189. EXPECT_EQ(kSuggestURL, value);
  190. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kFaviconURL, &value));
  191. EXPECT_EQ(kIconURL, value);
  192. base::ListValue encodings;
  193. encodings.Append("UTF-16");
  194. encodings.Append("UTF-8");
  195. EXPECT_TRUE(
  196. dictionary->GetList(DefaultSearchManager::kInputEncodings, &list_value));
  197. EXPECT_EQ(encodings, *list_value);
  198. EXPECT_TRUE(
  199. dictionary->GetList(DefaultSearchManager::kAlternateURLs, &list_value));
  200. EXPECT_EQ(default_alternate_urls_, *list_value);
  201. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kImageURL, &value));
  202. EXPECT_EQ(kImageURL, value);
  203. EXPECT_TRUE(
  204. dictionary->GetString(DefaultSearchManager::kImageURLPostParams, &value));
  205. EXPECT_EQ(kImageParams, value);
  206. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kSearchURLPostParams,
  207. &value));
  208. EXPECT_EQ(std::string(), value);
  209. EXPECT_TRUE(dictionary->GetString(
  210. DefaultSearchManager::kSuggestionsURLPostParams, &value));
  211. EXPECT_EQ(std::string(), value);
  212. }
  213. // Checks that disabling default search is properly reflected the dictionary
  214. // pref.
  215. TEST_F(DefaultSearchPolicyHandlerTest, DisabledByPolicy) {
  216. PolicyMap policy;
  217. policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
  218. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(false),
  219. nullptr);
  220. policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
  221. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
  222. base::Value("http://a/?{searchTerms}"), nullptr);
  223. UpdateProviderPolicy(policy);
  224. const base::Value* temp = nullptr;
  225. // Ignore any other search provider related policy in this case.
  226. EXPECT_FALSE(store_->GetValue(DefaultSearchManager::kURL, &temp));
  227. const base::DictionaryValue* dictionary;
  228. EXPECT_TRUE(store_->GetValue(
  229. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  230. temp->GetAsDictionary(&dictionary);
  231. absl::optional<bool> disabled =
  232. dictionary->FindBoolKey(DefaultSearchManager::kDisabledByPolicy);
  233. EXPECT_TRUE(disabled);
  234. EXPECT_TRUE(disabled.value());
  235. }
  236. // Check that when the default search enabled policy is not set, all other
  237. // default search-related policies are ignored.
  238. TEST_F(DefaultSearchPolicyHandlerTest, DisabledByPolicyNotSet) {
  239. PolicyMap policy;
  240. policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
  241. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
  242. base::Value("http://a/?{searchTerms}"), nullptr);
  243. UpdateProviderPolicy(policy);
  244. const base::Value* temp = nullptr;
  245. EXPECT_FALSE(store_->GetValue(
  246. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  247. EXPECT_FALSE(store_->GetValue(DefaultSearchManager::kURL, &temp));
  248. }
  249. // Checks that if the policy for default search is valid, i.e. there's a
  250. // search URL, that all the elements have been given proper defaults.
  251. TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) {
  252. PolicyMap policy;
  253. policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
  254. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
  255. nullptr);
  256. policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
  257. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(kSearchURL),
  258. nullptr);
  259. UpdateProviderPolicy(policy);
  260. const base::Value* temp = nullptr;
  261. const base::DictionaryValue* dictionary;
  262. std::string value;
  263. const base::ListValue* list_value;
  264. EXPECT_TRUE(store_->GetValue(
  265. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  266. temp->GetAsDictionary(&dictionary);
  267. // Name and keyword should be derived from host.
  268. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
  269. EXPECT_EQ(kSearchURL, value);
  270. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
  271. EXPECT_EQ(kHostName, value);
  272. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
  273. EXPECT_EQ(kHostName, value);
  274. // Everything else should be set to the default value.
  275. EXPECT_TRUE(
  276. dictionary->GetString(DefaultSearchManager::kSuggestionsURL, &value));
  277. EXPECT_EQ(std::string(), value);
  278. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kFaviconURL, &value));
  279. EXPECT_EQ(std::string(), value);
  280. EXPECT_TRUE(
  281. dictionary->GetList(DefaultSearchManager::kInputEncodings, &list_value));
  282. EXPECT_EQ(base::ListValue(), *list_value);
  283. EXPECT_TRUE(
  284. dictionary->GetList(DefaultSearchManager::kAlternateURLs, &list_value));
  285. EXPECT_EQ(base::ListValue(), *list_value);
  286. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kImageURL, &value));
  287. EXPECT_EQ(std::string(), value);
  288. EXPECT_TRUE(
  289. dictionary->GetString(DefaultSearchManager::kImageURLPostParams, &value));
  290. EXPECT_EQ(std::string(), value);
  291. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kSearchURLPostParams,
  292. &value));
  293. EXPECT_EQ(std::string(), value);
  294. EXPECT_TRUE(dictionary->GetString(
  295. DefaultSearchManager::kSuggestionsURLPostParams, &value));
  296. EXPECT_EQ(std::string(), value);
  297. }
  298. // Checks that setting a file URL as the default search is reflected properly in
  299. // the dictionary pref.
  300. TEST_F(DefaultSearchPolicyHandlerTest, FileURL) {
  301. PolicyMap policy;
  302. policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY,
  303. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
  304. nullptr);
  305. policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY,
  306. POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
  307. base::Value(kFileSearchURL), nullptr);
  308. UpdateProviderPolicy(policy);
  309. const base::Value* temp = nullptr;
  310. const base::DictionaryValue* dictionary;
  311. std::string value;
  312. EXPECT_TRUE(store_->GetValue(
  313. DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp));
  314. temp->GetAsDictionary(&dictionary);
  315. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value));
  316. EXPECT_EQ(kFileSearchURL, value);
  317. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value));
  318. EXPECT_EQ("_", value);
  319. EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value));
  320. EXPECT_EQ("_", value);
  321. }
  322. } // namespace policy