summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/policy_domain_descriptor_unittest.cc
blob: 0f64cc975a843abe87efe252d93a22c68697a33a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/policy_domain_descriptor.h"

#include "base/values.h"
#include "chrome/browser/policy/policy_bundle.h"
#include "chrome/browser/policy/policy_map.h"
#include "chrome/browser/policy/policy_schema.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

TEST(PolicyDomainDescriptor, FilterBundle) {
  scoped_refptr<PolicyDomainDescriptor> descriptor =
      new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS);
  EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain());
  EXPECT_TRUE(descriptor->components().empty());

  std::string error;
  scoped_ptr<PolicySchema> schema = PolicySchema::Parse(
      "{"
      "  \"$schema\":\"http://json-schema.org/draft-03/schema#\","
      "  \"type\":\"object\","
      "  \"properties\": {"
      "    \"Array\": {"
      "      \"type\": \"array\","
      "      \"items\": { \"type\": \"string\" }"
      "    },"
      "    \"Boolean\": { \"type\": \"boolean\" },"
      "    \"Integer\": { \"type\": \"integer\" },"
      "    \"Null\": { \"type\": \"null\" },"
      "    \"Number\": { \"type\": \"number\" },"
      "    \"Object\": {"
      "      \"type\": \"object\","
      "      \"properties\": {"
      "        \"a\": { \"type\": \"string\" },"
      "        \"b\": { \"type\": \"integer\" }"
      "      }"
      "    },"
      "    \"String\": { \"type\": \"string\" }"
      "  }"
      "}", &error);
  ASSERT_TRUE(schema) << error;

  descriptor->RegisterComponent("abc", schema.Pass());

  EXPECT_EQ(1u, descriptor->components().size());
  EXPECT_NE(descriptor->components().end(),
            descriptor->components().find("abc"));

  PolicyBundle bundle;
  descriptor->FilterBundle(&bundle);
  const PolicyBundle empty_bundle;
  EXPECT_TRUE(bundle.Equals(empty_bundle));

  // Other namespaces aren't filtered.
  PolicyBundle expected_bundle;
  PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
  expected_bundle.Get(chrome_ns).Set("ChromePolicy",
                                     POLICY_LEVEL_MANDATORY,
                                     POLICY_SCOPE_USER,
                                     base::Value::CreateStringValue("value"));
  bundle.CopyFrom(expected_bundle);
  // Unknown components of the domain are filtered out.
  PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz");
  bundle.Get(another_extension_ns).Set(
      "AnotherExtensionPolicy",
      POLICY_LEVEL_MANDATORY,
      POLICY_SCOPE_USER,
      base::Value::CreateStringValue("value"));
  descriptor->FilterBundle(&bundle);
  EXPECT_TRUE(bundle.Equals(expected_bundle));

  PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc");
  PolicyMap& map = expected_bundle.Get(extension_ns);
  base::ListValue list;
  list.AppendString("a");
  list.AppendString("b");
  map.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
          list.DeepCopy());
  map.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
          base::Value::CreateBooleanValue(true));
  map.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
          base::Value::CreateIntegerValue(1));
  map.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
          base::Value::CreateNullValue());
  map.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
          base::Value::CreateDoubleValue(1.2));
  base::DictionaryValue dict;
  dict.SetString("a", "b");
  dict.SetInteger("b", 2);
  map.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, dict.DeepCopy());
  map.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
          base::Value::CreateStringValue("value"));

  bundle.MergeFrom(expected_bundle);
  bundle.Get(extension_ns).Set("Unexpected",
                               POLICY_LEVEL_MANDATORY,
                               POLICY_SCOPE_USER,
                               base::Value::CreateStringValue("to-be-removed"));

  descriptor->FilterBundle(&bundle);
  EXPECT_TRUE(bundle.Equals(expected_bundle));

  // Mismatched types are also removed.
  bundle.Clear();
  PolicyMap& badmap = bundle.Get(extension_ns);
  badmap.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateBooleanValue(false));
  badmap.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateIntegerValue(0));
  badmap.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateBooleanValue(false));
  badmap.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateBooleanValue(false));
  badmap.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateBooleanValue(false));
  badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateBooleanValue(false));
  badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             base::Value::CreateBooleanValue(false));

  descriptor->FilterBundle(&bundle);
  EXPECT_TRUE(bundle.Equals(empty_bundle));
}

}  // namespace policy