File: policy_pref_mapping_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 (812 lines) | stat: -rw-r--r-- 29,547 bytes parent folder | download | duplicates (5)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// Copyright 2020 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/browser/policy_pref_mapping_test.h"

#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/hash/hash.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/android_buildflags.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "printing/buildflags/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace policy {

namespace {

enum class PrefLocation {
  kUserProfile,
  kSigninProfile,
  kLocalState,
};

PrefLocation GetPrefLocation(const base::Value::Dict& settings) {
  const std::string* location = settings.FindString("location");
  if (!location || *location == "user_profile")
    return PrefLocation::kUserProfile;
  if (*location == "local_state")
    return PrefLocation::kLocalState;
  if (*location == "signin_profile")
    return PrefLocation::kSigninProfile;
  ADD_FAILURE() << "Unknown pref location: " << *location;
  return PrefLocation::kUserProfile;
}

PrefService* GetPrefServiceForLocation(PrefLocation location,
                                       PrefService* local_state,
                                       PrefService* user_prefs,
                                       PrefService* signin_profile_prefs) {
  switch (location) {
    case PrefLocation::kUserProfile:
      return user_prefs;
    case PrefLocation::kSigninProfile:
      return signin_profile_prefs;
    case PrefLocation::kLocalState:
      return local_state;
    default:
      ADD_FAILURE() << "Unhandled pref location: "
                    << static_cast<int>(location);
  }
  return nullptr;
}

void CheckPrefHasValue(const PrefService::Preference* pref,
                       const base::Value* expected_value) {
  ASSERT_TRUE(pref);

  const base::Value* pref_value = pref->GetValue();
  ASSERT_TRUE(pref->GetValue());
  ASSERT_TRUE(expected_value);
  EXPECT_EQ(*pref_value, *expected_value);
}

void CheckPrefHasDefaultValue(const PrefService::Preference* pref,
                              const base::Value* expected_value = nullptr) {
  ASSERT_TRUE(pref);
  EXPECT_TRUE(pref->IsDefaultValue());
  EXPECT_TRUE(pref->IsUserModifiable());
  EXPECT_FALSE(pref->IsUserControlled());
  EXPECT_FALSE(pref->IsManaged());
  EXPECT_FALSE(pref->IsRecommended());
  if (expected_value)
    CheckPrefHasValue(pref, expected_value);
}

void CheckPrefHasRecommendedValue(const PrefService::Preference* pref,
                                  const base::Value* expected_value) {
  ASSERT_TRUE(pref);
  ASSERT_TRUE(expected_value);
  EXPECT_FALSE(pref->IsDefaultValue());
  EXPECT_TRUE(pref->IsUserModifiable());
  EXPECT_FALSE(pref->IsUserControlled());
  EXPECT_FALSE(pref->IsManaged());
  EXPECT_TRUE(pref->IsRecommended());
  CheckPrefHasValue(pref, expected_value);
}

void CheckPrefHasMandatoryValue(const PrefService::Preference* pref,
                                const base::Value* expected_value) {
  ASSERT_TRUE(pref);
  ASSERT_TRUE(expected_value);
  EXPECT_FALSE(pref->IsDefaultValue());
  EXPECT_FALSE(pref->IsUserModifiable());
  EXPECT_FALSE(pref->IsUserControlled());
  EXPECT_TRUE(pref->IsManaged());
  EXPECT_FALSE(pref->IsRecommended());
  CheckPrefHasValue(pref, expected_value);
}

// Contains the testing details for a single pref affected by one or multiple
// policies. Each entry in `prefs` from
// components/policy/test/data/pref_mapping/[PolicyName].json is represented by
// one `PrefTestCase`.
class PrefTestCase {
 public:
  PrefTestCase(const std::string& name, const base::Value::Dict& settings) {
    const base::Value* value = settings.Find("value");
    const base::Value* default_value = settings.Find("default_value");
    location_ = GetPrefLocation(settings);
    check_for_mandatory_ =
        settings.FindBool("check_for_mandatory").value_or(true);
    check_for_recommended_ =
        settings.FindBool("check_for_recommended").value_or(true);

    pref_ = name;
    if (value)
      value_ = value->Clone();
    if (default_value)
      default_value_ = default_value->Clone();

    if (value && default_value) {
      ADD_FAILURE()
          << "only one of |value| or |default_value| should be used for pref "
          << name;
    }
    if (!value && !default_value) {
      ADD_FAILURE() << "either |value| or |default_value| must be set for pref "
                    << name;
    }
  }

  ~PrefTestCase() = default;
  PrefTestCase(const PrefTestCase& other) = delete;
  PrefTestCase& operator=(const PrefTestCase& other) = delete;

  const std::string& pref() const { return pref_; }

  const base::Value* value() const {
    if (value_.is_none())
      return nullptr;
    return &value_;
  }
  const base::Value* default_value() const {
    if (default_value_.is_none())
      return nullptr;
    return &default_value_;
  }

  PrefLocation location() const { return location_; }

  bool check_for_mandatory() const { return check_for_mandatory_; }
  bool check_for_recommended() const { return check_for_recommended_; }

 private:
  std::string pref_;
  PrefLocation location_;
  bool check_for_mandatory_;
  bool check_for_recommended_;

  // At most one of these will be set.
  base::Value value_;
  base::Value default_value_;
};

// Contains the testing details for a single policy to pref mapping. Each entry
// under `policy_pref_mapping_tests` from
// components/policy/test/data/pref_mapping/[PolicyName].json is represented by
// one `PolicyPrefMappingTest`.
class PolicyPrefMappingTest {
 public:
  explicit PolicyPrefMappingTest(const base::Value::Dict& mapping) {
    const base::Value::Dict* policies = mapping.FindDict("policies");
    const base::Value::Dict* policies_settings =
        mapping.FindDict("policies_settings");
    const base::Value::Dict* prefs = mapping.FindDict("prefs");
    if (policies)
      policies_ = policies->Clone();
    if (policies_settings)
      policies_settings_ = policies_settings->Clone();
    if (prefs) {
      for (auto [name, setting] : *prefs) {
        if (!setting.is_dict()) {
          ADD_FAILURE() << "prefs item " << name << " is not dict";
          continue;
        }
        prefs_.push_back(
            std::make_unique<PrefTestCase>(name, setting.GetDict()));
      }
    }
    if (prefs_.empty()) {
      ADD_FAILURE() << "missing |prefs|";
    }
    const base::Value::List* required_buildflags =
        mapping.FindList("required_buildflags");
    if (required_buildflags) {
      for (const auto& required_buildflag : *required_buildflags) {
        required_buildflags_.push_back(required_buildflag.GetString());
      }
    }
  }
  ~PolicyPrefMappingTest() = default;
  PolicyPrefMappingTest(const PolicyPrefMappingTest& other) = delete;
  PolicyPrefMappingTest& operator=(const PolicyPrefMappingTest& other) = delete;

  const base::Value::Dict& policies() const { return policies_; }
  const base::Value::Dict& policies_settings() const {
    return policies_settings_;
  }

  const std::vector<std::unique_ptr<PrefTestCase>>& prefs() const {
    return prefs_;
  }

  const std::vector<std::string>& required_buildflags() const {
    return required_buildflags_;
  }

 private:
  base::Value::Dict policies_;
  base::Value::Dict policies_settings_;
  std::vector<std::unique_ptr<PrefTestCase>> prefs_;
  std::vector<std::string> required_buildflags_;
};

class SimplePolicyPrefMappingTest {
 public:
  SimplePolicyPrefMappingTest(const std::string& policy_name,
                              const base::Value::Dict& test,
                              const bool is_os_supported) {
    const std::string* pref_name = test.FindString("pref_name");
    if (!pref_name) {
      ADD_FAILURE() << "Simple test for " << policy_name
                    << " is missing a 'pref_name'";
      return;
    }

    const std::string* location = test.FindString("pref_location");
    const base::Value::Dict* policy_settings = test.FindDict("policy_settings");

    const base::Value* default_value = test.Find("default_value");
    const base::Value* default_for_enterprise_users = nullptr;
#if BUILDFLAG(IS_CHROMEOS)
    default_for_enterprise_users = test.Find("default_for_enterprise_users");
    if (!default_value && !default_for_enterprise_users) {
      ADD_FAILURE()
          << "Simple test for " << policy_name
          << " is missing a 'default_value' (or alternatively a "
             "'default_for_enterprise_users' if the policy defines one)";
      return;
    }
#else   // BUILDFLAG(IS_CHROMEOS)
    if (!default_value && is_os_supported) {
      ADD_FAILURE() << "Simple test for " << policy_name
                    << " is missing a 'default_value'";
      return;
    }
#endif  // BUILDFLAG(IS_CHROMEOS)

    const base::Value::List* values_to_test = test.FindList("values_to_test");
    if (!values_to_test || values_to_test->empty()) {
      ADD_FAILURE() << "Simple test for " << policy_name
                    << " is missing a 'values_to_test' or is empty";
      return;
    }

    // Build test case for default value.
    if (default_value || default_for_enterprise_users) {
      base::Value::Dict default_value_test_dict;
      base::Value::Dict default_value_prefs_dict;
      base::Value::Dict default_value_pref_dict;
      if (default_for_enterprise_users) {
        default_value_pref_dict.Set("value",
                                    default_for_enterprise_users->Clone());
      } else {
        default_value_pref_dict.Set("default_value", default_value->Clone());
      }
      if (location) {
        default_value_pref_dict.Set("location", *location);
      }
      default_value_prefs_dict.Set(*pref_name,
                                   std::move(default_value_pref_dict));
      default_value_test_dict.Set("prefs", std::move(default_value_prefs_dict));
      policy_pref_mapping_test_dicts_.push_back(
          std::move(default_value_test_dict));
    }

    // Build test case for each `value_to_test`.
    for (const base::Value& value_to_test : *values_to_test) {
      base::Value::Dict value_test_dict;
      base::Value::Dict value_policies_dict;
      base::Value::Dict value_prefs_dict;
      base::Value::Dict value_pref_dict;
      value_policies_dict.Set(policy_name, value_to_test.Clone());
      value_test_dict.Set("policies", std::move(value_policies_dict));
      if (policy_settings) {
        base::Value::Dict value_policy_settings_dict;
        value_policy_settings_dict.Set(policy_name, policy_settings->Clone());
        value_test_dict.Set("policies_settings",
                            std::move(value_policy_settings_dict));
      }
      value_pref_dict.Set("value", value_to_test.Clone());
      if (location) {
        value_pref_dict.Set("location", *location);
      }
      value_prefs_dict.Set(*pref_name, std::move(value_pref_dict));
      value_test_dict.Set("prefs", std::move(value_prefs_dict));
      policy_pref_mapping_test_dicts_.push_back(std::move(value_test_dict));
    }
  }

  const std::vector<base::Value::Dict>& policy_pref_mapping_test_dicts() const {
    return policy_pref_mapping_test_dicts_;
  }

 private:
  std::vector<base::Value::Dict> policy_pref_mapping_test_dicts_;
};

// Populates buildflags as strings that policy pref mapping test cases
// can depend on and implements a check if such a test case should run according
// to the buildflags.
bool CheckRequiredBuildFlagsSupported(const PolicyPrefMappingTest* test) {
  static base::NoDestructor<base::flat_set<std::string>> kBuildFlags([] {
    base::flat_set<std::string> flags;
#if BUILDFLAG(USE_CUPS)
    flags.insert("USE_CUPS");
#endif
    return flags;
  }());

  for (const auto& required_buildflag : test->required_buildflags()) {
    if (!kBuildFlags->contains(required_buildflag)) {
      return false;
    }
  }

  return true;
}

// Each file like components/policy/test/data/pref_mapping/[PolicyName].json
// contains one or more `PolicyTestCase` with each of them defining the platform
// they run on. Typically, if a policy has different mappings per platform, it
// would have multiple `PolicyTestCase`s. If the mapping is the same across
// platforms, it would only have one `PolicyTestCase`.
class PolicyTestCase {
 public:
  PolicyTestCase(const std::string& policy_name,
                 const base::Value::Dict& test_case) {
    is_official_only_ = test_case.FindBool("official_only").value_or(false);
    can_be_recommended_ =
        test_case.FindBool("can_be_recommended").value_or(false);
    has_reason_for_missing_test_ =
        test_case.FindString("reason_for_missing_test") != nullptr;

    const base::Value::List* os_list = test_case.FindList("os");
    if (os_list) {
      for (const auto& os : *os_list) {
        if (os.is_string()) {
          supported_os_.push_back(os.GetString());
        }
      }
    }

    const base::Value::List* policy_pref_mapping_test_list =
        test_case.FindList("policy_pref_mapping_tests");
    if (policy_pref_mapping_test_list) {
      for (const auto& policy_pref_mapping_test_dict :
           *policy_pref_mapping_test_list) {
        if (policy_pref_mapping_test_dict.is_dict()) {
          AddPolicyPrefMappingTest(policy_pref_mapping_test_dict.GetDict());
        }
      }
    }

    const base::Value::Dict* simple_policy_pref_mapping_test_dict =
        test_case.FindDict("simple_policy_pref_mapping_test");
    if (simple_policy_pref_mapping_test_dict) {
      const SimplePolicyPrefMappingTest simple_policy_pref_mapping_test(
          policy_name, *simple_policy_pref_mapping_test_dict, IsOsSupported());
      for (const base::Value::Dict& policy_pref_mapping_test_dict :
           simple_policy_pref_mapping_test.policy_pref_mapping_test_dicts()) {
        AddPolicyPrefMappingTest(policy_pref_mapping_test_dict);
      }
    }
  }

  ~PolicyTestCase() = default;
  PolicyTestCase(const PolicyTestCase& other) = delete;
  PolicyTestCase& operator=(const PolicyTestCase& other) = delete;

  bool is_official_only() const { return is_official_only_; }

  bool can_be_recommended() const { return can_be_recommended_; }

  bool has_reason_for_missing_test() const {
    return has_reason_for_missing_test_;
  }

  bool IsOsSupported() const {
#if BUILDFLAG(IS_DESKTOP_ANDROID)
    // For prefs testing desktop Android is considered a separate OS because it
    // registers some additional prefs (e.g. extensions prefs).
    const std::string os("desktop_android");
#elif BUILDFLAG(IS_ANDROID)
    const std::string os("android");
#elif BUILDFLAG(IS_CHROMEOS)
    const std::string os("chromeos");
#elif BUILDFLAG(IS_IOS)
    const std::string os("ios");
#elif BUILDFLAG(IS_LINUX)
    const std::string os("linux");
#elif BUILDFLAG(IS_MAC)
    const std::string os("mac");
#elif BUILDFLAG(IS_WIN)
    const std::string os("win");
#elif BUILDFLAG(IS_FUCHSIA)
    const std::string os("fuchsia");
#else
#error "Unknown platform"
#endif
    return base::Contains(supported_os_, os);
  }

  bool IsOsCovered() const {
#if BUILDFLAG(IS_ANDROID)
    // Android policies that apply to desktop Android are covered as part of the
    // desktop Android build because they may invoke desktop-only code.
    return base::Contains(supported_os_, "android") ||
           base::Contains(supported_os_, "desktop_android");
#else
    return IsOsSupported();
#endif
  }

  bool IsSupported() const {
#if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
    if (is_official_only())
      return false;
#endif
    return IsOsSupported();
  }

  const std::vector<std::unique_ptr<PolicyPrefMappingTest>>&
  policy_pref_mapping_tests() const {
    return policy_pref_mapping_tests_;
  }

  bool HasSupportedOs() const { return !supported_os_.empty(); }

 private:
  bool is_official_only_;
  bool can_be_recommended_;
  bool has_reason_for_missing_test_;
  std::vector<std::string> supported_os_;
  std::vector<std::unique_ptr<PolicyPrefMappingTest>>
      policy_pref_mapping_tests_;

  void AddPolicyPrefMappingTest(const base::Value::Dict& mapping) {
    policy_pref_mapping_tests_.push_back(
        std::make_unique<PolicyPrefMappingTest>(mapping));
  }
};

// Parses all policy test cases in components/policy/test/data/pref_mapping/ and
// makes them available in a map.
class PolicyTestCases {
 public:
  typedef std::vector<std::unique_ptr<PolicyTestCase>> PolicyTestCaseVector;
  typedef std::map<std::string, PolicyTestCaseVector> PolicyTestCaseMap;
  typedef PolicyTestCaseMap::const_iterator iterator;

  explicit PolicyTestCases(const base::FilePath& test_case_dir) {
    base::ScopedAllowBlockingForTesting allow_blocking;
    std::string json;

    base::FileEnumerator iterator(
        test_case_dir, /*recursive=*/false, base::FileEnumerator::FILES,
        FILE_PATH_LITERAL("*.json"),
        base::FileEnumerator::FolderSearchPolicy::ALL,
        base::FileEnumerator::ErrorPolicy::STOP_ENUMERATION);

    for (base::FilePath path = iterator.Next(); !path.empty();
         path = iterator.Next()) {
      if (!base::ReadFileToString(path, &json)) {
        ADD_FAILURE() << "Error reading: " << path;
        return;
      }
      auto parsed_json = base::JSONReader::ReadAndReturnValueWithError(json);
      if (!parsed_json.has_value()) {
        ADD_FAILURE() << "Error parsing " << path << " : "
                      << parsed_json.error().message;
        return;
      }
      if (!parsed_json->is_list()) {
        ADD_FAILURE() << "Error parsing " << path << ": Expected list.";
        return;
      }
      std::string policy_name =
          path.BaseName().RemoveFinalExtension().MaybeAsASCII();
      for (const auto& test_case : parsed_json->GetList()) {
        auto policy_test_case =
            std::make_unique<PolicyTestCase>(policy_name, test_case.GetDict());
        policy_test_cases_[policy_name].push_back(std::move(policy_test_case));
      }
    }
  }

  ~PolicyTestCases() = default;
  PolicyTestCases(const PolicyTestCases& other) = delete;
  PolicyTestCases& operator=(const PolicyTestCases& other) = delete;

  const PolicyTestCaseVector* Get(const std::string& name) const {
    const iterator it = policy_test_cases_.find(name);
    return it == end() ? nullptr : &it->second;
  }

  const PolicyTestCaseMap& map() const { return policy_test_cases_; }
  iterator begin() const { return policy_test_cases_.begin(); }
  iterator end() const { return policy_test_cases_.end(); }

 private:
  PolicyTestCaseMap policy_test_cases_;
};

struct PolicySettings {
  PolicySource source = PolicySource::POLICY_SOURCE_CLOUD;
  PolicyScope scope = PolicyScope::POLICY_SCOPE_USER;
};

PolicySettings GetPolicySettings(const std::string& policy,
                                 const base::Value::Dict& policies_settings) {
  PolicySettings settings;
  const base::Value::Dict* settings_value =
      policies_settings.FindDictByDottedPath(policy);
  if (!settings_value)
    return settings;
  const std::string* source = settings_value->FindString("source");
  if (source) {
    if (*source == "enterprise_default")
      settings.source = POLICY_SOURCE_ENTERPRISE_DEFAULT;
    else if (*source == "command_line")
      settings.source = POLICY_SOURCE_COMMAND_LINE;
    else if (*source == "cloud")
      settings.source = POLICY_SOURCE_CLOUD;
    else if (*source == "active_directory")
      settings.source = POLICY_SOURCE_ACTIVE_DIRECTORY;
    else if (*source == "platform")
      settings.source = POLICY_SOURCE_PLATFORM;
    else if (*source == "merged")
      settings.source = POLICY_SOURCE_MERGED;
    else if (*source == "cloud_from_ash")
      settings.source = POLICY_SOURCE_CLOUD_FROM_ASH;
    else {
      NOTREACHED();
    }
  }

  const std::string* scope = settings_value->FindString("scope");
  if (scope) {
    if (*scope == "user")
      settings.scope = POLICY_SCOPE_USER;
    else if (*scope == "machine")
      settings.scope = POLICY_SCOPE_MACHINE;
  }

  return settings;
}

void SetProviderPolicy(MockConfigurationPolicyProvider* provider,
                       const base::Value::Dict& policies,
                       const base::Value::Dict& policies_settings,
                       PolicyLevel level) {
  PolicyMap policy_map;
#if BUILDFLAG(IS_CHROMEOS)
  SetEnterpriseUsersDefaults(&policy_map);
#endif  // BUILDFLAG(IS_CHROMEOS)
  for (auto it : policies) {
    const PolicyDetails* policy_details = GetChromePolicyDetails(it.first);
    const PolicySettings policy_settings =
        GetPolicySettings(it.first, policies_settings);
    ASSERT_TRUE(policy_details);
    policy_map.Set(
        it.first, level, policy_settings.scope, policy_settings.source,
        it.second.Clone(),
        policy_details->max_external_data_size
            ? std::make_unique<ExternalDataFetcher>(nullptr, it.first)
            : nullptr);
  }
  provider->UpdateChromePolicy(policy_map);
}

std::optional<base::flat_set<std::string>> GetTestFilter() {
  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
          kPolicyToPrefMappingsFilterSwitch)) {
    return std::nullopt;
  }

  std::string value =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          kPolicyToPrefMappingsFilterSwitch);
  auto list = base::SplitString(value, ":", base::TRIM_WHITESPACE,
                                base::SPLIT_WANT_NONEMPTY);
  if (list.empty())
    return std::nullopt;

  return base::flat_set<std::string>(std::move(list));
}

}  // namespace

void VerifyAllPoliciesHaveATestCase(const base::FilePath& test_case_dir) {
  // Verifies that all known policies have a pref mapping test case.
  // This test fails when a policy is added to
  // components/policy/resources/templates/policy_definitions/ but a test case
  // is not added to components/policy/test/data/pref_mapping/.
  Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
  ASSERT_TRUE(chrome_schema.valid());

  PolicyTestCases policy_test_cases(test_case_dir);
  for (Schema::Iterator it = chrome_schema.GetPropertiesIterator();
       !it.IsAtEnd(); it.Advance()) {
    const char* policy_name = it.key();
    auto policy = policy_test_cases.map().find(policy_name);
    if (policy == policy_test_cases.map().end()) {
      ADD_FAILURE() << "Missing policy test case for: " << policy_name;
      continue;
    }

    bool has_test_case_or_reason_for_this_os = false;
    bool has_reason_for_all_os = false;
    for (const auto& test_case : policy->second) {
      EXPECT_TRUE(test_case->has_reason_for_missing_test() ||
                  !test_case->policy_pref_mapping_tests().empty())
          << "A test case for " << policy_name << " has empty list of test "
          << "cases (policy_pref_mapping_tests). Add tests or use "
          << "reason_for_missing_test.";

      if (test_case->HasSupportedOs()) {
        has_test_case_or_reason_for_this_os |= test_case->IsOsCovered();
      } else {
        has_reason_for_all_os |= test_case->has_reason_for_missing_test();
      }
    }
    EXPECT_TRUE(has_test_case_or_reason_for_this_os || has_reason_for_all_os)
        << "Policy " << policy_name << " should either provide a test case for "
        << "all supported operating systems (see policy_templates.json) or "
        << "provide a reason_for_missing_test.";
  }
}

// Verifies that policies make their corresponding preferences become managed,
// and that the user can't override that setting.
void VerifyPolicyToPrefMappings(const base::FilePath& test_case_dir,
                                PrefService* local_state,
                                PrefService* user_prefs,
                                PrefService* signin_profile_prefs,
                                MockConfigurationPolicyProvider* provider,
                                PrefMappingChunkInfo* chunk_info) {
  Schema chrome_schema = Schema::Wrap(GetChromeSchemaData());
  ASSERT_TRUE(chrome_schema.valid());

  const PolicyTestCases test_cases(test_case_dir);

  auto test_filter = GetTestFilter();

  for (const auto& [policy_name, policy_test_cases] : test_cases) {
    if (chunk_info != nullptr) {
      const size_t policy_name_hash = base::PersistentHash(policy_name);
      const size_t chunk_index = policy_name_hash % chunk_info->num_chunks;
      if (chunk_index != chunk_info->current_chunk) {
        // Skip policy if test cases are chunked and the policy is not part of
        // the current chunk.
        continue;
      }
    }

    if (test_filter.has_value() &&
        !base::Contains(test_filter.value(), policy_name)) {
      // Skip policy based on the filter.
      continue;
    }

    for (size_t idx = 0; idx < policy_test_cases.size(); ++idx) {
      auto& test_case = policy_test_cases[idx];
      SCOPED_TRACE(policy_test_cases.size() <= 1
                       ? ::testing::Message() << "Policy name: " << policy_name
                       : ::testing::Message()
                             << "Policy name: " << policy_name << " - " << idx);

      if (!chrome_schema.GetKnownProperty(policy_name).valid() &&
          test_case->IsSupported()) {
        // Print warning message if a deprecated policy is still supported by
        // the test file.
        LOG(WARNING) << "Policy " << policy_name << " is marked as supported "
                     << "on this OS but does not exist in the Chrome policy "
                     << "schema.";
        continue;
      }

      if (!test_case->IsSupported() ||
          test_case->has_reason_for_missing_test()) {
        continue;
      }

      for (size_t i = 0; i < test_case->policy_pref_mapping_tests().size();
           ++i) {
        const auto& pref_mapping = test_case->policy_pref_mapping_tests()[i];
        SCOPED_TRACE(::testing::Message() << "Mapping test index " << i);

        EXPECT_FALSE(pref_mapping->prefs().empty())
            << "Test #" << i << " for " << policy_name << " is missing pref "
            << "values to check for";

        if (!CheckRequiredBuildFlagsSupported(pref_mapping.get())) {
          LOG(INFO) << "Test #" << i << " for " << policy_name << " skipped "
                    << "due to buildflags";
          continue;
        }

        for (const auto& pref_case : pref_mapping->prefs()) {
          SCOPED_TRACE(::testing::Message() << "Pref: " << pref_case->pref());
          PrefService* prefs =
              GetPrefServiceForLocation(pref_case->location(), local_state,
                                        user_prefs, signin_profile_prefs);
          // Skip preference mapping if required PrefService was not provided.
          if (!prefs)
            continue;

          const bool check_recommended = test_case->can_be_recommended() &&
                                         pref_case->check_for_recommended();
          const bool check_mandatory = pref_case->check_for_mandatory();

          EXPECT_TRUE(check_recommended || check_mandatory)
              << "pref has to be checked for recommended and/or mandatory "
                 "values";

          // The preference must have been registered.
          const PrefService::Preference* pref =
              prefs->FindPreference(pref_case->pref());
          ASSERT_TRUE(pref)
              << "Pref " << pref_case->pref() << " not registered";

          provider->UpdateChromePolicy(PolicyMap());
          prefs->ClearPref(pref_case->pref());
          CheckPrefHasDefaultValue(pref);

          const base::Value::Dict& policies = pref_mapping->policies();

          const base::Value* expected_value = pref_case->value();
          bool expect_value_to_be_default = false;
          if (!expected_value && pref_case->default_value()) {
            expected_value = pref_case->default_value();
            expect_value_to_be_default = true;
          }
          ASSERT_TRUE(expected_value);

          if (check_recommended) {
            SCOPED_TRACE(::testing::Message() << "checking recommended policy");

            ASSERT_NO_FATAL_FAILURE(SetProviderPolicy(
                provider, policies, pref_mapping->policies_settings(),
                POLICY_LEVEL_RECOMMENDED));
            if (expect_value_to_be_default) {
              CheckPrefHasDefaultValue(pref, expected_value);
            } else {
              CheckPrefHasRecommendedValue(pref, expected_value);
            }
          }

          if (check_mandatory) {
            SCOPED_TRACE(::testing::Message() << "checking mandatory policy");
            ASSERT_NO_FATAL_FAILURE(SetProviderPolicy(
                provider, policies, pref_mapping->policies_settings(),
                POLICY_LEVEL_MANDATORY));
            if (expect_value_to_be_default) {
              CheckPrefHasDefaultValue(pref, expected_value);
            } else {
              CheckPrefHasMandatoryValue(pref, expected_value);
            }
          }
        }
      }
    }
  }
}

}  // namespace policy