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)
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("abcdefghijklmnopabcdefghijklmnop"));
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("*"));
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("invalid"));
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.empty());
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      errors.GetErrors(policy::key::kExtensionInstallBlacklist).empty());
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionListPolicyHandlerTest, ApplyPolicySettings) {
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue policy;
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue expected;
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefValueMap prefs;
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Value* value = NULL;
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionListPolicyHandler handler(
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallBlacklist, kTestPref, false);
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  policy.Append(new base::StringValue("abcdefghijklmnopabcdefghijklmnop"));
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  expected.Append(new base::StringValue("abcdefghijklmnopabcdefghijklmnop"));
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  policy.Append(new base::StringValue("invalid"));
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallBlacklist,
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionInstallForcelistPolicyHandlerTest, CheckPolicySettings) {
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyErrorMap errors;
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionInstallForcelistPolicyHandler handler;
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Add an erroneous entry. This should generate an error, but the good
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // entry should still be translated successfully.
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("adfasdf;http://example.com");
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1U, errors.size());
1368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Add an entry with bad URL, which should generate another error.
1388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("abcdefghijklmnopabcdefghijklmnop;nourl");
1398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(2U, errors.size());
1478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Just an extension ID should also generate an error.
1498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  list.AppendString("abcdefghijklmnopabcdefghijklmnop");
1508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(3U, errors.size());
1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionInstallForcelistPolicyHandlerTest, ApplyPolicySettings) {
1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue policy;
1628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::DictionaryValue expected;
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefValueMap prefs;
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Value* value = NULL;
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionInstallForcelistPolicyHandler handler;
1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(prefs.GetValue(pref_names::kInstallForceList, &value));
1708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(value);
1718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
1768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  extensions::ExternalPolicyLoader::AddExtension(
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &expected, "abcdefghijklmnopabcdefghijklmnop", "http://example.com");
1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
1888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
1918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy.AppendString("invalid");
1948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallForcelist,
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
1968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
1978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy.DeepCopy(),
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&expected, value));
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionURLPatternListPolicyHandlerTest, CheckPolicySettings) {
2058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
2068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
2078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyErrorMap errors;
2088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionURLPatternListPolicyHandler handler(
2098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallSources, kTestPref);
2108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
2188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
2198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("http://*.google.com/*"));
2218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
2288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
2298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("<all_urls>"));
2318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
2388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(errors.empty());
2398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("invalid"));
2418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
2488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.empty());
2498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.GetErrors(policy::key::kExtensionInstallSources).empty());
2508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // URLPattern syntax has a different way to express 'all urls'. Though '*'
2528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // would be compatible today, it would be brittle, so we disallow.
2535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("*"));
2548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  errors.Clear();
2608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
2618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.empty());
2628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_FALSE(errors.GetErrors(policy::key::kExtensionInstallSources).empty());
2638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST(ExtensionURLPatternListPolicyHandlerTest, ApplyPolicySettings) {
2668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::ListValue list;
2678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy::PolicyMap policy_map;
2688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefValueMap prefs;
2698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Value* value = NULL;
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionURLPatternListPolicyHandler handler(
2718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      policy::key::kExtensionInstallSources, kTestPref);
2728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  list.Append(new base::StringValue("https://corp.monkey.net/*"));
2748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  policy_map.Set(policy::key::kExtensionInstallSources,
2758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_LEVEL_MANDATORY,
2768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 policy::POLICY_SCOPE_USER,
2778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 list.DeepCopy(),
2788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 NULL);
2798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  handler.ApplyPolicySettings(policy_map, &prefs);
2808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(prefs.GetValue(kTestPref, &value));
2818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(base::Value::Equals(&list, value));
2828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace extensions
285