OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/external_policy_loader.h" | 10 #include "chrome/browser/extensions/external_policy_loader.h" |
9 #include "chrome/browser/extensions/policy_handlers.h" | 11 #include "chrome/browser/extensions/policy_handlers.h" |
10 #include "components/policy/core/browser/policy_error_map.h" | 12 #include "components/policy/core/browser/policy_error_map.h" |
11 #include "components/policy/core/common/policy_map.h" | 13 #include "components/policy/core/common/policy_map.h" |
12 #include "components/policy/core/common/policy_types.h" | 14 #include "components/policy/core/common/policy_types.h" |
13 #include "components/policy/core/common/schema.h" | 15 #include "components/policy/core/common/schema.h" |
14 #include "components/policy/policy_constants.h" | 16 #include "components/policy/policy_constants.h" |
15 #include "components/prefs/pref_value_map.h" | 17 #include "components/prefs/pref_value_map.h" |
16 #include "extensions/browser/pref_names.h" | 18 #include "extensions/browser/pref_names.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 10 matching lines...) Expand all Loading... |
28 const char kTestManagementPolicy2[] = | 30 const char kTestManagementPolicy2[] = |
29 "{" | 31 "{" |
30 " \"abcdefghijklmnopabcdefghijklmnop\": {" | 32 " \"abcdefghijklmnopabcdefghijklmnop\": {" |
31 " \"installation_mode\": \"force_installed\"," | 33 " \"installation_mode\": \"force_installed\"," |
32 " \"update_url\": \"http://example.com/app\"," | 34 " \"update_url\": \"http://example.com/app\"," |
33 " }," | 35 " }," |
34 " \"*\": {" | 36 " \"*\": {" |
35 " \"installation_mode\": \"blocked\"," | 37 " \"installation_mode\": \"blocked\"," |
36 " }," | 38 " }," |
37 "}"; | 39 "}"; |
| 40 const char kTestManagementPolicy3[] = |
| 41 "{" |
| 42 " \"*\": {" |
| 43 " \"runtime_blocked_hosts\": [\"%s\"]" |
| 44 " }" |
| 45 "}"; |
38 | 46 |
39 TEST(ExtensionListPolicyHandlerTest, CheckPolicySettings) { | 47 TEST(ExtensionListPolicyHandlerTest, CheckPolicySettings) { |
40 base::ListValue list; | 48 base::ListValue list; |
41 policy::PolicyMap policy_map; | 49 policy::PolicyMap policy_map; |
42 policy::PolicyErrorMap errors; | 50 policy::PolicyErrorMap errors; |
43 ExtensionListPolicyHandler handler( | 51 ExtensionListPolicyHandler handler( |
44 policy::key::kExtensionInstallBlacklist, kTestPref, true); | 52 policy::key::kExtensionInstallBlacklist, kTestPref, true); |
45 | 53 |
46 policy_map.Set(policy::key::kExtensionInstallBlacklist, | 54 policy_map.Set(policy::key::kExtensionInstallBlacklist, |
47 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 55 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 ExtensionSettingsPolicyHandler handler(chrome_schema); | 284 ExtensionSettingsPolicyHandler handler(chrome_schema); |
277 | 285 |
278 policy_map.Set(policy::key::kExtensionSettings, | 286 policy_map.Set(policy::key::kExtensionSettings, |
279 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 287 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
280 policy::POLICY_SOURCE_CLOUD, std::move(policy_value), nullptr); | 288 policy::POLICY_SOURCE_CLOUD, std::move(policy_value), nullptr); |
281 // CheckPolicySettings() fails due to missing update URL. | 289 // CheckPolicySettings() fails due to missing update URL. |
282 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); | 290 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors)); |
283 EXPECT_FALSE(errors.empty()); | 291 EXPECT_FALSE(errors.empty()); |
284 } | 292 } |
285 | 293 |
| 294 TEST(ExtensionSettingsPolicyHandlerTest, CheckPolicySettingsURL) { |
| 295 std::vector<std::string> good_urls = { |
| 296 "*://*.example.com", "*://example.com", "http://cat.example.com", |
| 297 "https://example.*", "*://*.example.*", "<all_urls>"}; |
| 298 |
| 299 // Invalid URLPattern or with a non-standard path |
| 300 std::vector<std::string> bad_urls = { |
| 301 "://*.example.com", "*://example.com/cat*", "*://example.com/", |
| 302 "*://*.example.com/*cat", "*://example.com/cat/*", "bad", |
| 303 "*://example.com/*"}; |
| 304 |
| 305 // Crafts and parses a ExtensionSettings policy to test URL parsing. |
| 306 auto URLParsesSuccessfully = [](std::string url) { |
| 307 std::string policy = |
| 308 base::StringPrintf(kTestManagementPolicy3, url.c_str()); |
| 309 std::string error; |
| 310 std::unique_ptr<base::Value> policy_value = |
| 311 base::JSONReader::ReadAndReturnError( |
| 312 policy, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, NULL, |
| 313 &error); |
| 314 if (!policy_value.get()) |
| 315 return false; |
| 316 |
| 317 policy::Schema chrome_schema = |
| 318 policy::Schema::Wrap(policy::GetChromeSchemaData()); |
| 319 policy::PolicyMap policy_map; |
| 320 policy::PolicyErrorMap errors; |
| 321 ExtensionSettingsPolicyHandler handler(chrome_schema); |
| 322 |
| 323 policy_map.Set(policy::key::kExtensionSettings, |
| 324 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 325 policy::POLICY_SOURCE_CLOUD, std::move(policy_value), |
| 326 nullptr); |
| 327 |
| 328 return handler.CheckPolicySettings(policy_map, &errors) && errors.empty(); |
| 329 |
| 330 }; |
| 331 |
| 332 for (std::string url : good_urls) { |
| 333 EXPECT_TRUE(URLParsesSuccessfully(url)) << url; |
| 334 } |
| 335 for (std::string url : bad_urls) { |
| 336 EXPECT_FALSE(URLParsesSuccessfully(url)) << url; |
| 337 } |
| 338 } |
| 339 |
286 TEST(ExtensionSettingsPolicyHandlerTest, ApplyPolicySettings) { | 340 TEST(ExtensionSettingsPolicyHandlerTest, ApplyPolicySettings) { |
287 std::string error; | 341 std::string error; |
288 std::unique_ptr<base::Value> policy_value = | 342 std::unique_ptr<base::Value> policy_value = |
289 base::JSONReader::ReadAndReturnError( | 343 base::JSONReader::ReadAndReturnError( |
290 kTestManagementPolicy2, | 344 kTestManagementPolicy2, |
291 base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, NULL, &error); | 345 base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, NULL, &error); |
292 ASSERT_TRUE(policy_value.get()) << error; | 346 ASSERT_TRUE(policy_value.get()) << error; |
293 | 347 |
294 policy::Schema chrome_schema = | 348 policy::Schema chrome_schema = |
295 policy::Schema::Wrap(policy::GetChromeSchemaData()); | 349 policy::Schema::Wrap(policy::GetChromeSchemaData()); |
296 policy::PolicyMap policy_map; | 350 policy::PolicyMap policy_map; |
297 policy::PolicyErrorMap errors; | 351 policy::PolicyErrorMap errors; |
298 PrefValueMap prefs; | 352 PrefValueMap prefs; |
299 ExtensionSettingsPolicyHandler handler(chrome_schema); | 353 ExtensionSettingsPolicyHandler handler(chrome_schema); |
300 | 354 |
301 policy_map.Set(policy::key::kExtensionSettings, | 355 policy_map.Set(policy::key::kExtensionSettings, |
302 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 356 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
303 policy::POLICY_SOURCE_CLOUD, policy_value->CreateDeepCopy(), | 357 policy::POLICY_SOURCE_CLOUD, policy_value->CreateDeepCopy(), |
304 nullptr); | 358 nullptr); |
305 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); | 359 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors)); |
306 handler.ApplyPolicySettings(policy_map, &prefs); | 360 handler.ApplyPolicySettings(policy_map, &prefs); |
307 base::Value* value = NULL; | 361 base::Value* value = NULL; |
308 ASSERT_TRUE(prefs.GetValue(pref_names::kExtensionManagement, &value)); | 362 ASSERT_TRUE(prefs.GetValue(pref_names::kExtensionManagement, &value)); |
309 EXPECT_TRUE(base::Value::Equals(policy_value.get(), value)); | 363 EXPECT_TRUE(base::Value::Equals(policy_value.get(), value)); |
310 } | 364 } |
311 | 365 |
312 } // namespace extensions | 366 } // namespace extensions |
OLD | NEW |