File: generate_policy_source_unittest.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 (333 lines) | stat: -rw-r--r-- 12,730 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
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <cstring>
#include <memory>
#include <string>

#include "base/values.h"
#include "build/build_config.h"
#include "components/policy/core/common/policy_details.h"
#include "components/policy/core/common/proxy_settings_constants.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

// This unittest tests the code generated by
// chrome/tools/build/generate_policy_source.py.

namespace policy {

namespace {

#if BUILDFLAG(IS_CHROMEOS)
// Checks if two schemas are the same or not. Note that this function doesn't
// consider restrictions on integers and strings nor pattern properties.
bool IsSameSchema(Schema a, Schema b) {
  if (a.valid() != b.valid())
    return false;
  if (!a.valid())
    return true;
  if (a.type() != b.type())
    return false;
  if (a.type() == base::Value::Type::LIST)
    return IsSameSchema(a.GetItems(), b.GetItems());
  if (a.type() != base::Value::Type::DICT) {
    return true;
  }
  Schema::Iterator a_it = a.GetPropertiesIterator();
  Schema::Iterator b_it = b.GetPropertiesIterator();
  while (!a_it.IsAtEnd()) {
    if (b_it.IsAtEnd())
      return false;
    if (strcmp(a_it.key(), b_it.key()) != 0)
      return false;
    if (!IsSameSchema(a_it.schema(), b_it.schema()))
      return false;
    a_it.Advance();
    b_it.Advance();
  }
  if (!b_it.IsAtEnd())
    return false;
  return IsSameSchema(a.GetAdditionalProperties(), b.GetAdditionalProperties());
}
#endif

}  // namespace

TEST(GeneratePolicySource, ChromeSchemaData) {
  Schema schema = Schema::Wrap(GetChromeSchemaData());
  ASSERT_TRUE(schema.valid());
  EXPECT_EQ(base::Value::Type::DICT, schema.type());

  Schema subschema = schema.GetAdditionalProperties();
  EXPECT_FALSE(subschema.valid());

  subschema = schema.GetProperty("no such policy exists");
  EXPECT_FALSE(subschema.valid());

  subschema = schema.GetProperty(key::kSearchSuggestEnabled);
  ASSERT_TRUE(subschema.valid());
  EXPECT_EQ(base::Value::Type::BOOLEAN, subschema.type());

  subschema = schema.GetProperty(key::kURLBlocklist);
  ASSERT_TRUE(subschema.valid());
  EXPECT_EQ(base::Value::Type::LIST, subschema.type());
  ASSERT_TRUE(subschema.GetItems().valid());
  EXPECT_EQ(base::Value::Type::STRING, subschema.GetItems().type());

  // Verify that all the Chrome policies are there.
  for (Schema::Iterator it = schema.GetPropertiesIterator(); !it.IsAtEnd();
       it.Advance()) {
    EXPECT_TRUE(it.key());
    EXPECT_FALSE(std::string(it.key()).empty());
    EXPECT_TRUE(GetChromePolicyDetails(it.key()));
  }

#if !BUILDFLAG(IS_IOS)
  subschema = schema.GetProperty(key::kDefaultCookiesSetting);
  ASSERT_TRUE(subschema.valid());
  EXPECT_EQ(base::Value::Type::INTEGER, subschema.type());

  subschema = schema.GetProperty(key::kProxyMode);
  ASSERT_TRUE(subschema.valid());
  EXPECT_EQ(base::Value::Type::STRING, subschema.type());

  subschema = schema.GetProperty(key::kProxySettings);
  ASSERT_TRUE(subschema.valid());
  EXPECT_EQ(base::Value::Type::DICT, subschema.type());
  EXPECT_FALSE(subschema.GetAdditionalProperties().valid());
  EXPECT_FALSE(subschema.GetProperty("no such proxy key exists").valid());
  ASSERT_TRUE(subschema.GetProperty(key::kProxyMode).valid());
  ASSERT_TRUE(subschema.GetProperty(key::kProxyServer).valid());
  ASSERT_TRUE(subschema.GetProperty(key::kProxyServerMode).valid());
  ASSERT_TRUE(subschema.GetProperty(key::kProxyPacUrl).valid());
  ASSERT_TRUE(subschema.GetProperty(kProxyPacMandatory).valid());
  ASSERT_TRUE(subschema.GetProperty(key::kProxyBypassList).valid());

  // The properties are iterated in order.
  const char* kExpectedProperties[] = {
      key::kProxyBypassList,
      key::kProxyMode,
      kProxyPacMandatory,
      key::kProxyPacUrl,
      key::kProxyServer,
      key::kProxyServerMode,
      nullptr,
  };
  const char** next = kExpectedProperties;
  for (Schema::Iterator it(subschema.GetPropertiesIterator());
       !it.IsAtEnd(); it.Advance(), ++next) {
    ASSERT_TRUE(*next != nullptr);
    EXPECT_STREQ(*next, it.key());
    ASSERT_TRUE(it.schema().valid());
    if (it.key() == key::kProxyServerMode)
      EXPECT_EQ(base::Value::Type::INTEGER, it.schema().type());
    else if (strcmp(it.key(), kProxyPacMandatory) == 0)
      EXPECT_EQ(base::Value::Type::BOOLEAN, it.schema().type());
    else
      EXPECT_EQ(base::Value::Type::STRING, it.schema().type());
  }
  EXPECT_TRUE(*next == nullptr);
#endif  // !BUILDFLAG(IS_IOS)

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
  subschema = schema.GetProperty(key::kExtensionSettings);
  ASSERT_TRUE(subschema.valid());
  ASSERT_EQ(base::Value::Type::DICT, subschema.type());
  EXPECT_FALSE(subschema.GetAdditionalProperties().valid());
  EXPECT_FALSE(subschema.GetProperty("no such extension id exists").valid());
  EXPECT_TRUE(subschema.GetPatternProperties("*").empty());
  EXPECT_TRUE(subschema.GetPatternProperties("no such extension id").empty());
  EXPECT_TRUE(subschema.GetPatternProperties("^[a-p]{32}$").empty());
  EXPECT_TRUE(subschema.GetPatternProperties("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                  .empty());
  EXPECT_TRUE(
      subschema.GetPatternProperties("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
          .empty());
  SchemaList schema_list =
      subschema.GetPatternProperties("abcdefghijklmnopabcdefghijklmnop");
  ASSERT_EQ(1u, schema_list.size());
  subschema = schema_list[0];
  ASSERT_TRUE(subschema.valid());
  ASSERT_EQ(base::Value::Type::DICT, subschema.type());
  subschema = subschema.GetProperty("installation_mode");
  ASSERT_TRUE(subschema.valid());
  ASSERT_EQ(base::Value::Type::STRING, subschema.type());

  subschema = schema.GetProperty(key::kExtensionSettings).GetProperty("*");
  ASSERT_TRUE(subschema.valid());
  ASSERT_EQ(base::Value::Type::DICT, subschema.type());
  subschema = subschema.GetProperty("installation_mode");
  ASSERT_TRUE(subschema.valid());
  ASSERT_EQ(base::Value::Type::STRING, subschema.type());
#endif

#if BUILDFLAG(IS_CHROMEOS)
  subschema = schema.GetKnownProperty(key::kPowerManagementIdleSettings);
  ASSERT_TRUE(subschema.valid());

  EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"),
                           subschema.GetKnownProperty("Battery")));

  subschema = schema.GetKnownProperty(key::kDeviceLoginScreenPowerManagement);
  ASSERT_TRUE(subschema.valid());

  EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"),
                           subschema.GetKnownProperty("Battery")));
#endif
}

TEST(GeneratePolicySource, PolicyScope) {
  const PolicyDetails* details;
#if !BUILDFLAG(IS_IOS)
  details = GetChromePolicyDetails(key::kCloudProfileReportingEnabled);
  ASSERT_TRUE(details);
  EXPECT_EQ(kSingleProfile, details->scope);
#endif

  details = GetChromePolicyDetails(key::kDefaultSearchProviderEnabled);
  ASSERT_TRUE(details);
  EXPECT_EQ(kProfile, details->scope);

  details = GetChromePolicyDetails(key::kCloudReportingEnabled);
  ASSERT_TRUE(details);
  EXPECT_EQ(kBrowser, details->scope);

#if BUILDFLAG(IS_CHROMEOS)
  details = GetChromePolicyDetails(key::kDeviceGuestModeEnabled);
  ASSERT_TRUE(details);
  EXPECT_EQ(kDevice, details->scope);
#endif
}

TEST(GeneratePolicySource, PolicyDetails) {
  EXPECT_FALSE(GetChromePolicyDetails(""));
  EXPECT_FALSE(GetChromePolicyDetails("no such policy"));
  EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable"));
  EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled"));
  EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled"));

  const PolicyDetails* details =
      GetChromePolicyDetails(key::kSearchSuggestEnabled);
  ASSERT_TRUE(details);
  EXPECT_FALSE(details->is_deprecated);
  EXPECT_EQ(kProfile, details->scope);
  EXPECT_EQ(6, details->id);
  EXPECT_EQ(0u, details->max_external_data_size);

#if !BUILDFLAG(IS_IOS)
  details = GetChromePolicyDetails(key::kJavascriptEnabled);
  ASSERT_TRUE(details);
  EXPECT_TRUE(details->is_deprecated);
  EXPECT_EQ(kProfile, details->scope);
  EXPECT_EQ(9, details->id);
  EXPECT_EQ(0u, details->max_external_data_size);
#endif

#if BUILDFLAG(IS_CHROMEOS)
  details = GetChromePolicyDetails(key::kDevicePolicyRefreshRate);
  ASSERT_TRUE(details);
  EXPECT_FALSE(details->is_deprecated);
  EXPECT_EQ(kDevice, details->scope);
  EXPECT_EQ(90, details->id);
  EXPECT_EQ(0u, details->max_external_data_size);

  // Policies of type 'external' have a greater-than-zero value for
  // |max_external_data_size|.
  details = GetChromePolicyDetails(key::kWallpaperImage);
  ASSERT_TRUE(details);
  EXPECT_FALSE(details->is_deprecated);
  EXPECT_EQ(kProfile, details->scope);
  EXPECT_EQ(262, details->id);
  EXPECT_GT(details->max_external_data_size, 0u);
#endif
}

#if BUILDFLAG(IS_CHROMEOS)
TEST(GeneratePolicySource, SetEnterpriseDefaults) {
  PolicyMap policy_map;

  // If policy not configured yet, set the enterprise default.
  SetEnterpriseUsersDefaults(&policy_map);

  const base::Value* multiprof_behavior = policy_map.GetValue(
      key::kChromeOsMultiProfileUserBehavior, base::Value::Type::STRING);
  base::Value expected("primary-only");
  EXPECT_EQ(expected, *multiprof_behavior);

  // If policy already configured, it's not changed to enterprise defaults.
  policy_map.Set(key::kChromeOsMultiProfileUserBehavior, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
                 base::Value("test_value"), nullptr);
  SetEnterpriseUsersDefaults(&policy_map);
  multiprof_behavior = policy_map.GetValue(
      key::kChromeOsMultiProfileUserBehavior, base::Value::Type::STRING);
  expected = base::Value("test_value");
  EXPECT_EQ(expected, *multiprof_behavior);
}

TEST(GeneratePolicySource, SetEnterpriseSystemWideDefaults) {
  PolicyMap policy_map;

  // If policy not configured yet, set the enterprise system-wide default.
  SetEnterpriseUsersSystemWideDefaults(&policy_map);

  const base::Value* pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  ASSERT_TRUE(pin_unlock_autosubmit_enabled);
  EXPECT_FALSE(pin_unlock_autosubmit_enabled->GetBool());
  const base::Value* allow_dinosaur_easter_egg = policy_map.GetValue(
      key::kAllowDinosaurEasterEgg, base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, allow_dinosaur_easter_egg);

  // If policy already configured, it's not changed to enterprise defaults.
  policy_map.Set(key::kPinUnlockAutosubmitEnabled, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
                 nullptr);
  SetEnterpriseUsersSystemWideDefaults(&policy_map);
  pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  ASSERT_TRUE(pin_unlock_autosubmit_enabled);
  EXPECT_TRUE(pin_unlock_autosubmit_enabled->GetBool());
  allow_dinosaur_easter_egg = policy_map.GetValue(key::kAllowDinosaurEasterEgg,
                                                  base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, allow_dinosaur_easter_egg);
}

TEST(GeneratePolicySource, SetEnterpriseProfileDefaults) {
  PolicyMap policy_map;

  // If policy not configured yet, set the enterprise profile default.
  SetEnterpriseUsersProfileDefaults(&policy_map);

  const base::Value* allow_dinosaur_easter_egg = policy_map.GetValue(
      key::kAllowDinosaurEasterEgg, base::Value::Type::BOOLEAN);
  ASSERT_TRUE(allow_dinosaur_easter_egg);
  EXPECT_FALSE(allow_dinosaur_easter_egg->GetBool());
  const base::Value* pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, pin_unlock_autosubmit_enabled);

  // If policy already configured, it's not changed to enterprise defaults.
  policy_map.Set(key::kAllowDinosaurEasterEgg, POLICY_LEVEL_MANDATORY,
                 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, base::Value(true),
                 nullptr);
  SetEnterpriseUsersProfileDefaults(&policy_map);
  allow_dinosaur_easter_egg = policy_map.GetValue(key::kAllowDinosaurEasterEgg,
                                                  base::Value::Type::BOOLEAN);
  ASSERT_TRUE(allow_dinosaur_easter_egg);
  EXPECT_TRUE(allow_dinosaur_easter_egg->GetBool());
  pin_unlock_autosubmit_enabled = policy_map.GetValue(
      key::kPinUnlockAutosubmitEnabled, base::Value::Type::BOOLEAN);
  EXPECT_EQ(nullptr, pin_unlock_autosubmit_enabled);
}
#endif

}  // namespace policy