File: configuration_policy_provider_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (430 lines) | stat: -rw-r--r-- 16,204 bytes parent folder | download | duplicates (6)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2013 The Chromium Authors
// 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/configuration_policy_provider_test.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/types/expected_macros.h"
#include "base/values.h"
#include "components/policy/core/common/chrome_schema.h"
#include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_migrator.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/policy_types.h"
#include "testing/gmock/include/gmock/gmock.h"

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

namespace policy {

const char kTestChromeSchema[] =
    "{"
    "  \"type\": \"object\","
    "  \"properties\": {"
    "    \"StringPolicy\": { \"type\": \"string\" },"
    "    \"BooleanPolicy\": { \"type\": \"boolean\" },"
    "    \"IntegerPolicy\": { \"type\": \"integer\" },"
    "    \"StringListPolicy\": {"
    "      \"type\": \"array\","
    "      \"items\": { \"type\": \"string\" }"
    "    },"
    "    \"DictionaryPolicy\": {"
    "      \"type\": \"object\","
    "      \"properties\": {"
    "        \"bool\": { \"type\": \"boolean\" },"
    "        \"double\": { \"type\": \"number\" },"
    "        \"int\": { \"type\": \"integer\" },"
    "        \"string\": { \"type\": \"string\" },"
    "        \"array\": {"
    "          \"type\": \"array\","
    "          \"items\": { \"type\": \"string\" }"
    "        },"
    "        \"dictionary\": {"
    "          \"type\": \"object\","
    "          \"properties\": {"
    "            \"sub\": { \"type\": \"string\" },"
    "            \"sublist\": {"
    "              \"type\": \"array\","
    "              \"items\": {"
    "                \"type\": \"object\","
    "                \"properties\": {"
    "                  \"aaa\": { \"type\": \"integer\" },"
    "                  \"bbb\": { \"type\": \"integer\" },"
    "                  \"ccc\": { \"type\": \"string\" },"
    "                  \"ddd\": { \"type\": \"string\" }"
    "                }"
    "              }"
    "            }"
    "          }"
    "        },"
    "        \"list\": {"
    "          \"type\": \"array\","
    "          \"items\": {"
    "            \"type\": \"object\","
    "            \"properties\": {"
    "              \"subdictindex\": { \"type\": \"integer\" },"
    "              \"subdict\": {"
    "                \"type\": \"object\","
    "                \"properties\": {"
    "                  \"bool\": { \"type\": \"boolean\" },"
    "                  \"double\": { \"type\": \"number\" },"
    "                  \"int\": { \"type\": \"integer\" },"
    "                  \"string\": { \"type\": \"string\" }"
    "                }"
    "              }"
    "            }"
    "          }"
    "        },"
    "        \"dict\": {"
    "          \"type\": \"object\","
    "          \"properties\": {"
    "            \"bool\": { \"type\": \"boolean\" },"
    "            \"double\": { \"type\": \"number\" },"
    "            \"int\": { \"type\": \"integer\" },"
    "            \"string\": { \"type\": \"string\" },"
    "            \"list\": {"
    "              \"type\": \"array\","
    "              \"items\": {"
    "                \"type\": \"object\","
    "                \"properties\": {"
    "                  \"subdictindex\": { \"type\": \"integer\" },"
    "                  \"subdict\": {"
    "                    \"type\": \"object\","
    "                    \"properties\": {"
    "                      \"bool\": { \"type\": \"boolean\" },"
    "                      \"double\": { \"type\": \"number\" },"
    "                      \"int\": { \"type\": \"integer\" },"
    "                      \"string\": { \"type\": \"string\" }"
    "                    }"
    "                  }"
    "                }"
    "              }"
    "            }"
    "          }"
    "        }"
    "      }"
    "    }"
    "  }"
    "}";

namespace test_keys {

const char kKeyString[] = "StringPolicy";
const char kKeyBoolean[] = "BooleanPolicy";
const char kKeyInteger[] = "IntegerPolicy";
const char kKeyStringList[] = "StringListPolicy";
const char kKeyDictionary[] = "DictionaryPolicy";

}  // namespace test_keys

PolicyTestBase::PolicyTestBase() = default;

PolicyTestBase::~PolicyTestBase() = default;

void PolicyTestBase::SetUp() {
  const PolicyNamespace ns(POLICY_DOMAIN_CHROME, "");
  ASSERT_TRUE(RegisterSchema(ns, kTestChromeSchema));
}

void PolicyTestBase::TearDown() {
  task_environment_.RunUntilIdle();
}

bool PolicyTestBase::RegisterSchema(const PolicyNamespace& ns,
                                    const std::string& schema_string) {
  ASSIGN_OR_RETURN(const auto schema, Schema::Parse(schema_string),
                   [](const auto& e) {
                     ADD_FAILURE() << e;
                     return false;
                   });
  schema_registry_.RegisterComponent(ns, schema);
  return true;
}

void PolicyTestBase::RegisterChromeSchema(const PolicyNamespace& ns) {
  schema_registry_.RegisterComponent(ns, policy::GetChromeSchema());
}

PolicyProviderTestHarness::PolicyProviderTestHarness(PolicyLevel level,
                                                     PolicyScope scope,
                                                     PolicySource source)
    : level_(level), scope_(scope), source_(source) {}

PolicyProviderTestHarness::~PolicyProviderTestHarness() = default;

PolicyLevel PolicyProviderTestHarness::policy_level() const {
  return level_;
}

PolicyScope PolicyProviderTestHarness::policy_scope() const {
  return scope_;
}

PolicySource PolicyProviderTestHarness::policy_source() const {
  return source_;
}

void PolicyProviderTestHarness::Install3rdPartyPolicy(
    const base::Value::Dict& policies) {
  FAIL();
}

ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() = default;

ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() = default;

void ConfigurationPolicyProviderTest::SetUp() {
  PolicyTestBase::SetUp();

  test_harness_.reset((*GetParam())());
  ASSERT_NO_FATAL_FAILURE(test_harness_->SetUp());

  const PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
  Schema chrome_schema = *schema_registry_.schema_map()->GetSchema(chrome_ns);
  Schema extension_schema =
      chrome_schema.GetKnownProperty(test_keys::kKeyDictionary);
  ASSERT_TRUE(extension_schema.valid());
  schema_registry_.RegisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
      extension_schema);
  schema_registry_.RegisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
                      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
      extension_schema);
  schema_registry_.RegisterComponent(
      PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
                      "cccccccccccccccccccccccccccccccc"),
      extension_schema);

  provider_.reset(test_harness_->CreateProvider(
      &schema_registry_,
      base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})));
  provider_->Init(&schema_registry_);
  // Some providers do a reload on init. Make sure any notifications generated
  // are fired now.
  task_environment_.RunUntilIdle();

  const PolicyBundle kEmptyBundle;
  EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle));
}

void ConfigurationPolicyProviderTest::TearDown() {
  test_harness_->TearDown();

  // Give providers the chance to clean up after themselves on the file thread.
  provider_->Shutdown();
  provider_.reset();

  PolicyTestBase::TearDown();
}

void ConfigurationPolicyProviderTest::CheckValue(
    const char* policy_name,
    const base::Value& expected_value,
    base::OnceClosure install_value) {
  // Install the value, reload policy and check the provider for the value.
  std::move(install_value).Run();
  provider_->RefreshPolicies(PolicyFetchReason::kTest);
  task_environment_.RunUntilIdle();
  PolicyBundle expected_bundle;
  expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
      .Set(policy_name, test_harness_->policy_level(),
           test_harness_->policy_scope(), test_harness_->policy_source(),
           expected_value.Clone(), nullptr);
  EXPECT_TRUE(provider_->policies().Equals(expected_bundle));
}

TEST_P(ConfigurationPolicyProviderTest, Empty) {
  provider_->RefreshPolicies(PolicyFetchReason::kTest);
  task_environment_.RunUntilIdle();
  const PolicyBundle kEmptyBundle;
  EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle));
}

TEST_P(ConfigurationPolicyProviderTest, StringValue) {
  const char kTestString[] = "string_value";
  base::Value expected_value(kTestString);
  CheckValue(test_keys::kKeyString, expected_value,
             base::BindOnce(&PolicyProviderTestHarness::InstallStringPolicy,
                            base::Unretained(test_harness_.get()),
                            test_keys::kKeyString, kTestString));
}

TEST_P(ConfigurationPolicyProviderTest, BooleanValue) {
  base::Value expected_value(true);
  CheckValue(test_keys::kKeyBoolean, expected_value,
             base::BindOnce(&PolicyProviderTestHarness::InstallBooleanPolicy,
                            base::Unretained(test_harness_.get()),
                            test_keys::kKeyBoolean, true));
}

TEST_P(ConfigurationPolicyProviderTest, IntegerValue) {
  base::Value expected_value(42);
  CheckValue(test_keys::kKeyInteger, expected_value,
             base::BindOnce(&PolicyProviderTestHarness::InstallIntegerPolicy,
                            base::Unretained(test_harness_.get()),
                            test_keys::kKeyInteger, 42));
}

TEST_P(ConfigurationPolicyProviderTest, StringListValue) {
  base::Value::List expected_value;
  expected_value.Append("first");
  expected_value.Append("second");
  CheckValue(test_keys::kKeyStringList, base::Value(expected_value.Clone()),
             base::BindOnce(&PolicyProviderTestHarness::InstallStringListPolicy,
                            base::Unretained(test_harness_.get()),
                            test_keys::kKeyStringList, expected_value.Clone()));
}

TEST_P(ConfigurationPolicyProviderTest, DictionaryValue) {
  base::Value::Dict expected_value;
  expected_value.Set("bool", true);
  expected_value.Set("double", 123.456);
  expected_value.Set("int", 123);
  expected_value.Set("string", "omg");

  {
    base::Value::List list;
    list.Append("first");
    list.Append("second");
    expected_value.Set("array", std::move(list));
  }

  base::Value::List sublist;
  {
    base::Value::Dict sub;
    sub.Set("aaa", 111);
    sub.Set("bbb", 222);
    sublist.Append(std::move(sub));
  }

  {
    base::Value::Dict sub;
    sub.Set("ccc", "333");
    sub.Set("ddd", "444");
    sublist.Append(std::move(sub));
  }

  base::Value::Dict dict;
  dict.Set("sub", "value");
  dict.Set("sublist", std::move(sublist));
  expected_value.Set("dictionary", std::move(dict));

  CheckValue(test_keys::kKeyDictionary, base::Value(expected_value.Clone()),
             base::BindOnce(&PolicyProviderTestHarness::InstallDictionaryPolicy,
                            base::Unretained(test_harness_.get()),
                            test_keys::kKeyDictionary, expected_value.Clone()));
}

TEST_P(ConfigurationPolicyProviderTest, RefreshPolicies) {
  PolicyBundle bundle;
  EXPECT_TRUE(provider_->policies().Equals(bundle));

  // OnUpdatePolicy is called even when there are no changes.
  MockConfigurationPolicyObserver observer;
  provider_->AddObserver(&observer);
  EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
  provider_->RefreshPolicies(PolicyFetchReason::kTest);
  task_environment_.RunUntilIdle();
  Mock::VerifyAndClearExpectations(&observer);

  EXPECT_TRUE(provider_->policies().Equals(bundle));

  // OnUpdatePolicy is called when there are changes.
  test_harness_->InstallStringPolicy(test_keys::kKeyString, "value");
  EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
  provider_->RefreshPolicies(PolicyFetchReason::kTest);
  task_environment_.RunUntilIdle();
  Mock::VerifyAndClearExpectations(&observer);

  bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
      .Set(test_keys::kKeyString, test_harness_->policy_level(),
           test_harness_->policy_scope(), test_harness_->policy_source(),
           base::Value("value"), nullptr);
  EXPECT_TRUE(provider_->policies().Equals(bundle));
  provider_->RemoveObserver(&observer);
}

Configuration3rdPartyPolicyProviderTest::
    Configuration3rdPartyPolicyProviderTest() = default;

Configuration3rdPartyPolicyProviderTest::
    ~Configuration3rdPartyPolicyProviderTest() = default;

TEST_P(Configuration3rdPartyPolicyProviderTest, Load3rdParty) {
  base::Value::Dict policy_dict;
  policy_dict.Set("bool", true);
  policy_dict.Set("double", 123.456);
  policy_dict.Set("int", 789);
  policy_dict.Set("string", "string value");

  base::Value::List list;
  for (int i = 0; i < 2; ++i) {
    base::Value::Dict dict;
    dict.Set("subdictindex", i);
    dict.Set("subdict", policy_dict.Clone());
    list.Append(std::move(dict));
  }
  policy_dict.Set("list", std::move(list));
  policy_dict.Set("dict", policy_dict.Clone());

  // Install these policies as a Chrome policy.
  test_harness_->InstallDictionaryPolicy(test_keys::kKeyDictionary,
                                         policy_dict.Clone());
  // Install them as 3rd party policies too.
  base::Value::Dict policy_3rdparty;
  policy_3rdparty.SetByDottedPath("extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                                  base::Value(policy_dict.Clone()));
  policy_3rdparty.SetByDottedPath("extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
                                  base::Value(policy_dict.Clone()));
  // Install invalid 3rd party policies that shouldn't be loaded. These also
  // help detecting memory leaks in the code paths that detect invalid input.
  policy_3rdparty.SetByDottedPath("invalid-domain.component",
                                  base::Value(policy_dict.Clone()));
  policy_3rdparty.SetByDottedPath("extensions.cccccccccccccccccccccccccccccccc",
                                  "invalid-value");
  test_harness_->Install3rdPartyPolicy(policy_3rdparty);

  provider_->RefreshPolicies(PolicyFetchReason::kTest);
  task_environment_.RunUntilIdle();

  PolicyMap expected_policy;
  expected_policy.Set(test_keys::kKeyDictionary, test_harness_->policy_level(),
                      test_harness_->policy_scope(),
                      test_harness_->policy_source(),
                      base::Value(policy_dict.Clone()), nullptr);
  PolicyBundle expected_bundle;
  expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) =
      expected_policy.Clone();
  expected_policy.Clear();
  expected_policy.LoadFrom(policy_dict, test_harness_->policy_level(),
                           test_harness_->policy_scope(),
                           test_harness_->policy_source());
  expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
                                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) =
      expected_policy.Clone();
  expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS,
                                      "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) =
      expected_policy.Clone();
  EXPECT_TRUE(provider_->policies().Equals(expected_bundle));
}

// These are abstract policy provider tests, which are instantiated for each
// policy provider implementation.
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ConfigurationPolicyProviderTest);
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(
    Configuration3rdPartyPolicyProviderTest);

}  // namespace policy