summaryrefslogtreecommitdiff
path: root/chromium/components/policy/core/common/schema_registry_unittest.cc
blob: 298b583035ddcfbefd13181016da2741610ca225 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 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 "components/policy/core/common/schema_registry.h"

#include <memory>

#include "base/test/gtest_util.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/schema.h"
#include "extensions/buildflags/buildflags.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Mock;
using ::testing::_;

namespace policy {

namespace {

const char kTestSchema[] =
    R"({
      "type": "object",
      "properties": {
        "string": { "type": "string" },
        "integer": { "type": "integer" },
        "boolean": { "type": "boolean" },
        "double": { "type": "number" },
        "list": {
          "type": "array",
          "items": { "type": "string" }
        },
        "object": {
          "type": "object",
          "properties": {
            "a": { "type": "string" },
            "b": { "type": "integer" }
          }
        }
      }
    })";

class MockSchemaRegistryObserver : public SchemaRegistry::Observer {
 public:
  MockSchemaRegistryObserver() {}
  ~MockSchemaRegistryObserver() override {}

  MOCK_METHOD1(OnSchemaRegistryUpdated, void(bool));
  MOCK_METHOD0(OnSchemaRegistryReady, void());
};

bool SchemaMapEquals(const scoped_refptr<SchemaMap>& schema_map1,
                     const scoped_refptr<SchemaMap>& schema_map2) {
  PolicyNamespaceList added;
  PolicyNamespaceList removed;
  schema_map1->GetChanges(schema_map2, &removed, &added);
  return added.empty() && removed.empty();
}

}  // namespace

TEST(SchemaRegistryTest, Notifications) {
  std::string error;
  Schema schema = Schema::Parse(kTestSchema, &error);
  ASSERT_TRUE(schema.valid()) << error;

  MockSchemaRegistryObserver observer;
  SchemaRegistry registry;
  registry.AddObserver(&observer);

  ASSERT_TRUE(registry.schema_map().get());
  EXPECT_FALSE(registry.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"),
                             schema);
  Mock::VerifyAndClearExpectations(&observer);

  // Re-register also triggers notifications, because the Schema might have
  // changed.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  registry.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"),
                             schema);
  Mock::VerifyAndClearExpectations(&observer);

  EXPECT_TRUE(registry.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  registry.UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"));
  Mock::VerifyAndClearExpectations(&observer);

  EXPECT_FALSE(registry.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")));

  // Registering multiple components at once issues only one notification.
  ComponentMap components;
  components["abc"] = schema;
  components["def"] = schema;
  components["xyz"] = schema;
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  registry.RegisterComponents(POLICY_DOMAIN_EXTENSIONS, components);
  Mock::VerifyAndClearExpectations(&observer);

  registry.RemoveObserver(&observer);
}

TEST(SchemaRegistryTest, IsReady) {
  SchemaRegistry registry;
  MockSchemaRegistryObserver observer;
  registry.AddObserver(&observer);

  EXPECT_FALSE(registry.IsReady());
#if BUILDFLAG(ENABLE_EXTENSIONS)
  EXPECT_CALL(observer, OnSchemaRegistryReady()).Times(0);
  registry.SetExtensionsDomainsReady();
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_FALSE(registry.IsReady());
#endif
  EXPECT_CALL(observer, OnSchemaRegistryReady());
  registry.SetDomainReady(POLICY_DOMAIN_CHROME);
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(registry.IsReady());
  EXPECT_CALL(observer, OnSchemaRegistryReady()).Times(0);
  registry.SetDomainReady(POLICY_DOMAIN_CHROME);
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(registry.IsReady());

  CombinedSchemaRegistry combined;
  EXPECT_TRUE(combined.IsReady());

  registry.RemoveObserver(&observer);
}

TEST(SchemaRegistryTest, Combined) {
  std::string error;
  Schema schema = Schema::Parse(kTestSchema, &error);
  ASSERT_TRUE(schema.valid()) << error;

  MockSchemaRegistryObserver observer;
  std::unique_ptr<SchemaRegistry> registry1(new SchemaRegistry);
  std::unique_ptr<SchemaRegistry> registry2(new SchemaRegistry);
  CombinedSchemaRegistry combined;
  combined.AddObserver(&observer);

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0);
  registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"),
                               schema);
  Mock::VerifyAndClearExpectations(&observer);

  // Starting to track a registry issues notifications when it comes with new
  // schemas.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  combined.Track(registry1.get());
  Mock::VerifyAndClearExpectations(&observer);

  // Adding a new empty registry does not trigger notifications.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0);
  combined.Track(registry2.get());
  Mock::VerifyAndClearExpectations(&observer);

  // Adding the same component to the combined registry itself triggers
  // notifications.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  combined.RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"),
                             schema);
  Mock::VerifyAndClearExpectations(&observer);

  // Adding components to the sub-registries triggers notifications.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  registry2->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"),
                               schema);
  Mock::VerifyAndClearExpectations(&observer);

  // If the same component is published in 2 sub-registries then the combined
  // registry publishes one of them.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"),
                               schema);
  Mock::VerifyAndClearExpectations(&observer);

  ASSERT_EQ(1u, combined.schema_map()->GetDomains().size());
  ASSERT_TRUE(combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS));
  ASSERT_EQ(
      2u,
      combined.schema_map()->GetComponents(POLICY_DOMAIN_EXTENSIONS)->size());
  EXPECT_TRUE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")));
  EXPECT_TRUE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")));
  EXPECT_FALSE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  registry1->UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"));
  Mock::VerifyAndClearExpectations(&observer);
  // Still registered at the combined registry.
  EXPECT_TRUE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  combined.UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"));
  Mock::VerifyAndClearExpectations(&observer);
  // Now it's gone.
  EXPECT_FALSE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  registry1->UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"));
  Mock::VerifyAndClearExpectations(&observer);
  // Still registered at registry2.
  EXPECT_TRUE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  registry2->UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def"));
  Mock::VerifyAndClearExpectations(&observer);
  // Now it's gone.
  EXPECT_FALSE(combined.schema_map()->GetSchema(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "def")));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true)).Times(2);
  registry1->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, ""),
                               schema);
  registry2->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "hij"),
                               schema);
  Mock::VerifyAndClearExpectations(&observer);

  // Untracking |registry1| doesn't trigger an update notification, because it
  // doesn't contain any components.
  EXPECT_CALL(observer, OnSchemaRegistryUpdated(_)).Times(0);
  registry1.reset();
  Mock::VerifyAndClearExpectations(&observer);

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  registry2.reset();
  Mock::VerifyAndClearExpectations(&observer);

  combined.RemoveObserver(&observer);
}

TEST(SchemaRegistryTest, ForwardingSchemaRegistry) {
  std::unique_ptr<SchemaRegistry> registry(new SchemaRegistry);
  ForwardingSchemaRegistry forwarding(registry.get());
  MockSchemaRegistryObserver observer;
  forwarding.AddObserver(&observer);

  EXPECT_FALSE(registry->IsReady());
  EXPECT_FALSE(forwarding.IsReady());
  // They always have the same SchemaMap.
  EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map()));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
  registry->RegisterComponent(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"),
                              Schema());
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map()));

  EXPECT_CALL(observer, OnSchemaRegistryUpdated(false));
  registry->UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"));
  Mock::VerifyAndClearExpectations(&observer);
  EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map()));

  // No notifications expected for these calls.
  EXPECT_FALSE(registry->IsReady());
  EXPECT_FALSE(forwarding.IsReady());

  registry->SetExtensionsDomainsReady();
  EXPECT_FALSE(registry->IsReady());
  EXPECT_FALSE(forwarding.IsReady());

  EXPECT_CALL(observer, OnSchemaRegistryReady());
  registry->SetDomainReady(POLICY_DOMAIN_CHROME);
  EXPECT_TRUE(registry->IsReady());
  EXPECT_TRUE(forwarding.IsReady());
  Mock::VerifyAndClearExpectations(&observer);

  EXPECT_TRUE(SchemaMapEquals(registry->schema_map(), forwarding.schema_map()));
  Mock::VerifyAndClearExpectations(&observer);

  forwarding.SetExtensionsDomainsReady();
  forwarding.SetDomainReady(POLICY_DOMAIN_CHROME);
  EXPECT_TRUE(forwarding.IsReady());

  // Keep the same SchemaMap when the original registry is gone.
  // No notifications are expected in this case either.
  scoped_refptr<SchemaMap> schema_map = registry->schema_map();
  registry.reset();
  EXPECT_TRUE(SchemaMapEquals(schema_map, forwarding.schema_map()));
  Mock::VerifyAndClearExpectations(&observer);

  forwarding.RemoveObserver(&observer);
}

TEST(SchemaRegistryTest, ForwardingSchemaRegistryReadiness) {
  std::unique_ptr<SchemaRegistry> registry(new SchemaRegistry);

  ForwardingSchemaRegistry forwarding_1(registry.get());
  EXPECT_FALSE(registry->IsReady());
  EXPECT_FALSE(forwarding_1.IsReady());

  // Once the wrapped registry gets ready, the forwarding schema registry
  // becomes ready too.
  registry->SetAllDomainsReady();
  EXPECT_TRUE(registry->IsReady());
  EXPECT_TRUE(forwarding_1.IsReady());

  // The wrapped registry was ready at the time when the forwarding registry was
  // constructed, so the forwarding registry is immediately ready too.
  ForwardingSchemaRegistry forwarding_2(registry.get());
  EXPECT_TRUE(forwarding_2.IsReady());

  // Destruction of the wrapped registry doesn't change the readiness of the
  // forwarding registry.
  registry.reset();
  EXPECT_TRUE(forwarding_1.IsReady());
  EXPECT_TRUE(forwarding_2.IsReady());
}

// Extension policy unregister before register shouldn't cause DCHECK failure.
// However, Chrome policy should always register first.
TEST(SchemaRegistryTest, UnregisterBeforeRegister) {
  SchemaRegistry registry;
  ASSERT_NO_FATAL_FAILURE(registry.UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "")));
  ASSERT_NO_FATAL_FAILURE(registry.UnregisterComponent(
      PolicyNamespace(POLICY_DOMAIN_SIGNIN_EXTENSIONS, "")));

  ASSERT_DCHECK_DEATH(
      registry.UnregisterComponent(PolicyNamespace(POLICY_DOMAIN_CHROME, "")));
}

}  // namespace policy