policy_handlers_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/prefs/pref_value_map.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/external_policy_loader.h"
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/policy_handlers.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/pref_names.h"
9a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/browser/policy_error_map.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_map.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/pref_names.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "policy/policy_constants.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace extensions {
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const char kTestPref[] = "unit_test.test_pref";
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionListPolicyHandlerTest, CheckPolicySettings) {
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyErrorMap errors;
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionListPolicyHandler handler(
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallBlacklist, kTestPref, true);
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("*"));
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("invalid"));
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.empty());
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      errors.GetErrors(policy::key::kExtensionInstallBlacklist).empty());
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionListPolicyHandlerTest, ApplyPolicySettings) {
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue policy;
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue expected;
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefValueMap prefs;
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Value* value = NULL;
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionListPolicyHandler handler(
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallBlacklist, kTestPref, false);
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy.Append(
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  expected.Append(
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy.Append(base::Value::CreateStringValue("invalid"));
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionInstallForcelistPolicyHandlerTest, CheckPolicySettings) {
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyErrorMap errors;
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionInstallForcelistPolicyHandler handler;
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Add an erroneous entry. This should generate an error, but the good
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // entry should still be translated successfully.
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("adfasdf;http://example.com");
1318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1U, errors.size());
1398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Add an entry with bad URL, which should generate another error.
1418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("abcdefghijklmnopabcdefghijklmnop;nourl");
1428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(2U, errors.size());
1508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Just an extension ID should also generate an error.
1528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("abcdefghijklmnopabcdefghijklmnop");
1538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(3U, errors.size());
1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionInstallForcelistPolicyHandlerTest, ApplyPolicySettings) {
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue policy;
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::DictionaryValue expected;
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefValueMap prefs;
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Value* value = NULL;
1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionInstallForcelistPolicyHandler handler;
1708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(prefs.GetValue(pref_names::kInstallForceList, &value));
1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(value);
1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  extensions::ExternalPolicyLoader::AddExtension(
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &expected, "abcdefghijklmnopabcdefghijklmnop", "http://example.com");
1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
1918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
1948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy.AppendString("invalid");
1978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
2048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
2058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionURLPatternListPolicyHandlerTest, CheckPolicySettings) {
2088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
2098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
2108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyErrorMap errors;
2118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionURLPatternListPolicyHandler handler(
2128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallSources, kTestPref);
2138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
2218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
2228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("http://*.google.com/*"));
2248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
2318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
2328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("<all_urls>"));
2348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
2418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
2428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("invalid"));
2448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
2518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.empty());
2528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.GetErrors(policy::key::kExtensionInstallSources).empty());
2538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // URLPattern syntax has a different way to express 'all urls'. Though '*'
2558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // would be compatible today, it would be brittle, so we disallow.
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("*"));
2578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
2648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.empty());
2658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.GetErrors(policy::key::kExtensionInstallSources).empty());
2668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionURLPatternListPolicyHandlerTest, ApplyPolicySettings) {
2698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
2718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefValueMap prefs;
2728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Value* value = NULL;
2738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionURLPatternListPolicyHandler handler(
2748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallSources, kTestPref);
2758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list.Append(base::Value::CreateStringValue("https://corp.monkey.net/*"));
2778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
2838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(prefs.GetValue(kTestPref, &value));
2848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&list, value));
2858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace extensions
288