File: profile_policies_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (587 lines) | stat: -rw-r--r-- 24,187 bytes parent folder | download | duplicates (8)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/profile_policies.h"

#include <string_view>

#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chromeos/ash/components/network/client_cert_util.h"
#include "components/onc/onc_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::Optional;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;

// Creates a list containing clones of elements passed in the initializer list.
base::Value::List NetworkConfigsList(
    std::initializer_list<const base::Value::Dict*> network_configs) {
  base::Value::List result;
  for (const auto* network_config : network_configs) {
    result.Append(network_config->Clone());
  }
  return result;
}

// Creates a very basic for-testing NetworkConfig, essentially
// {
//   "guid": <passed_guid>
// }
base::Value::Dict NetworkConfig(std::string_view guid) {
  return base::Value::Dict().Set(::onc::network_config::kGUID, guid);
}

bool FalseShillPropertiesMatcher(
    const base::Value::Dict& onc_network_configuration,
    const base::Value::Dict& shill_properties) {
  return false;
}

bool EqShillPropertiesMatcher(
    const base::Value::Dict& onc_network_configuration,
    const base::Value::Dict& shill_properties) {
  return onc_network_configuration == shill_properties;
}

// A runtime values setter which doesn't change its input ONC dictionary.
// Useful for emulating that setting runtime values resulted in no change from
// the "original" ONC dictionary.
base::Value::Dict NoOp(
    const base::Value::Dict& onc_network_configuration,
    const base::flat_map<std::string, std::string>& profile_wide_expansions,
    const client_cert::ResolvedCert& resolved_cert) {
  return onc_network_configuration.Clone();
}

// A RuntimeValuesSetter which injects its inputs as
// dictionary keys. Useful for checking behavior when variable
// setting runtime values actually changes the ONC dictionary, and for easy
// verification of the values that have been passed to the RuntimeValuesSetter.
base::Value::Dict Inject(
    const base::Value::Dict& onc_network_configuration,
    const base::flat_map<std::string, std::string>& profile_wide_expansions,
    const client_cert::ResolvedCert& resolved_cert) {
  base::Value::Dict result = onc_network_configuration.Clone();

  base::Value::Dict profile_wide_expansions_dict;
  for (const auto& pair : profile_wide_expansions) {
    profile_wide_expansions_dict.Set(pair.first, pair.second);
  }
  result.Set("profile_wide_expansions",
             std::move(profile_wide_expansions_dict));

  if (resolved_cert.status() ==
      client_cert::ResolvedCert::Status::kNothingMatched) {
    result.Set("cert_info", base::Value::Dict().Set("status", "no cert"));
  } else if (resolved_cert.status() ==
             client_cert::ResolvedCert::Status::kCertMatched) {
    auto cert_dict = base::Value::Dict()
                         .Set("slot_id", resolved_cert.slot_id())
                         .Set("pkcs11_id", resolved_cert.pkcs11_id());
    for (const auto& pair : resolved_cert.variable_expansions()) {
      cert_dict.Set(pair.first, pair.second);
    }

    result.Set("cert_info", std::move(cert_dict));
  }

  return result;
}

}  // namespace

using ProfilePoliciesTest = ::testing::Test;

// Calls GetGlobalNetworkConfig when no GlobalNetworkConfig has been set yet.
TEST(ProfilePoliciesTest, GlobalNetworkConfigIsEmpty) {
  ProfilePolicies profile_policies;
  ASSERT_TRUE(profile_policies.GetGlobalNetworkConfig());
  EXPECT_TRUE(profile_policies.GetGlobalNetworkConfig()->empty());
}

// Sets / retrieves GlobalNetworkConfig.
TEST(ProfilePoliciesTest, SetAndOverwriteGlobalNetworkConfig) {
  auto global_network_config_1 = base::Value::Dict().Set("key1", "value1");
  auto global_network_config_2 = base::Value::Dict().Set("key2", "value2");

  ProfilePolicies profile_policies;
  profile_policies.SetGlobalNetworkConfig(global_network_config_1);
  ASSERT_TRUE(profile_policies.GetGlobalNetworkConfig());
  EXPECT_EQ(*profile_policies.GetGlobalNetworkConfig(),
            global_network_config_1);

  profile_policies.SetGlobalNetworkConfig(global_network_config_2);
  ASSERT_TRUE(profile_policies.GetGlobalNetworkConfig());
  EXPECT_EQ(*profile_policies.GetGlobalNetworkConfig(),
            global_network_config_2);
}

// Tests accessors to per-network policies when no policy has been set.
TEST(ProfilePoliciesTest, NoNetworkPolicy) {
  ProfilePolicies profile_policies;
  EXPECT_EQ(profile_policies.GetPolicyByGuid("guid"), nullptr);
  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), IsEmpty());
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(), IsEmpty());
}

// Applies a network policy and checks accessors to per-network policies.
// The original policy (none) had no network configs.
// The new policy has no network configs.
TEST(ProfilePoliciesTest, ApplyOncNetworkConfigurationListZeroToZero) {
  base::Value::List network_configs;

  ProfilePolicies profile_policies;
  base::flat_set<std::string> new_or_modified_guids =
      profile_policies.ApplyOncNetworkConfigurationList(network_configs);
  EXPECT_THAT(new_or_modified_guids, IsEmpty());

  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), IsEmpty());
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(), IsEmpty());
}

// Applies a network policy and checks accessors to per-network policies.
// The original policy (none) had no network configs.
// The new policy has one network config.
TEST(ProfilePoliciesTest, ApplyOncNetworkConfigurationListZeroToOne) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::List network_configs = NetworkConfigsList({&network_config_1});

  ProfilePolicies profile_policies;
  base::flat_set<std::string> new_or_modified_guids =
      profile_policies.ApplyOncNetworkConfigurationList(network_configs);
  EXPECT_THAT(new_or_modified_guids, ElementsAre("guid1"));

  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), ElementsAre("guid1"));

  ASSERT_TRUE(profile_policies.GetPolicyByGuid("guid1"));
  EXPECT_EQ(*profile_policies.GetPolicyByGuid("guid1"), network_config_1);
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              ElementsAre(Pair("guid1", base::test::IsJson(network_config_1))));
}

// Applies a network policy and checks accessors to per-network policies.
// Goes from no policy (0 networks) -> policy with 1 network -> policy with 0
// networks.
TEST(ProfilePoliciesTest, ApplyOncNetworkConfigurationListOneToZero) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::List network_configs_orig =
      NetworkConfigsList({&network_config_1});

  ProfilePolicies profile_policies;
  profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);
  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), ElementsAre("guid1"));

  base::Value::List network_configs_new = NetworkConfigsList({});
  base::flat_set<std::string> new_or_modified_guids =
      profile_policies.ApplyOncNetworkConfigurationList(network_configs_new);
  EXPECT_THAT(new_or_modified_guids, IsEmpty());

  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), IsEmpty());
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(), IsEmpty());
}

// Applies a network policy and checks accessors to per-network policies.
// Tests re-application of exactly the same policy (no effective change).
TEST(ProfilePoliciesTest, ApplyOncNetworkConfigurationListNoChange) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::List network_configs_orig =
      NetworkConfigsList({&network_config_1});

  ProfilePolicies profile_policies;
  profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);
  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), ElementsAre("guid1"));

  base::flat_set<std::string> new_or_modified_guids =
      profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);
  EXPECT_THAT(new_or_modified_guids, IsEmpty());

  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), ElementsAre("guid1"));
}

// Applies a network policy and checks accessors to per-network policies.
// Applies a policy with a network config "guid1".
// Applies another policy where "guid1" has changed contents.
// Tests that "guid1" is reported as changed and has the new contents.
TEST(ProfilePoliciesTest, ApplyOncNetworkConfigurationListChange) {
  base::Value::Dict network_config_orig = NetworkConfig("guid1");
  base::Value::List network_configs_orig =
      NetworkConfigsList({&network_config_orig});

  ProfilePolicies profile_policies;
  profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);
  ASSERT_TRUE(profile_policies.GetPolicyByGuid("guid1"));
  EXPECT_EQ(*profile_policies.GetPolicyByGuid("guid1"), network_config_orig);

  base::Value::Dict network_config_changed = network_config_orig.Clone();
  network_config_changed.Set("changed", "changed");
  base::Value::List network_configs_changed =
      NetworkConfigsList({&network_config_changed});

  base::flat_set<std::string> new_or_modified_guids =
      profile_policies.ApplyOncNetworkConfigurationList(
          network_configs_changed);
  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), ElementsAre("guid1"));

  EXPECT_THAT(profile_policies.GetAllPolicyGuids(), ElementsAre("guid1"));
  ASSERT_TRUE(profile_policies.GetPolicyByGuid("guid1"));
  EXPECT_NE(*profile_policies.GetPolicyByGuid("guid1"), network_config_orig);
  EXPECT_EQ(*profile_policies.GetPolicyByGuid("guid1"), network_config_changed);
}

// Applies a network policy with multiple NetworkConfiguration elements and
// checks accessors to per-network policies.
TEST(ProfilePoliciesTest, MultipleElements) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  network_config_1.Set("test1", "value1");
  base::Value::Dict network_config_2 = NetworkConfig("guid2");
  network_config_2.Set("test2", "value2");
  base::Value::List network_configs_orig =
      NetworkConfigsList({&network_config_1, &network_config_2});

  ProfilePolicies profile_policies;
  profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);
  EXPECT_THAT(profile_policies.GetAllPolicyGuids(),
              UnorderedElementsAre("guid1", "guid2"));
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(
                  Pair("guid1", base::test::IsJson(network_config_1)),
                  Pair("guid2", base::test::IsJson(network_config_2))));
}

// Tests HasPolicyMatchingShillProperties for the case that no policy matches.
TEST(ProfilePoliciesTest, HasPolicyMatchingShillPropertiesNoMatch) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::List network_configs_orig =
      NetworkConfigsList({&network_config_1});

  ProfilePolicies profile_policies;
  profile_policies.SetShillPropertiesMatcherForTesting(
      base::BindRepeating(&FalseShillPropertiesMatcher));
  profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);

  EXPECT_FALSE(
      profile_policies.HasPolicyMatchingShillProperties(base::Value::Dict()));
}

// Tests HasPolicyMatchingShillProperties for the case that a policy matches.
TEST(ProfilePoliciesTest, HasPolicyMatchingShillPropertiesMatch) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::Dict network_config_2 = NetworkConfig("guid2");
  network_config_2.Set("marker", "value");
  base::Value::List network_configs_orig =
      NetworkConfigsList({&network_config_1, &network_config_2});

  ProfilePolicies profile_policies;
  profile_policies.SetShillPropertiesMatcherForTesting(
      base::BindRepeating(&EqShillPropertiesMatcher));
  profile_policies.ApplyOncNetworkConfigurationList(network_configs_orig);

  EXPECT_TRUE(
      profile_policies.HasPolicyMatchingShillProperties(network_config_2));
}

// Tests that profile-wide expansions apply if they were configured before the
// NetworkConfiguration was applied.
TEST(ProfilePoliciesTest, ProfileWideExpansionsAlreadyExist) {
  base::Value::Dict network_config = NetworkConfig("guid1");
  base::Value::List network_configs = NetworkConfigsList({&network_config});

  ProfilePolicies profile_policies;

  // Replace the variable expander with one which is simply injecting all
  // expansions as top-level dictionary keys instead.
  profile_policies.SetRuntimeValuesSetterForTesting(
      base::BindRepeating(&Inject));

  {
    base::flat_set<std::string> modified_guids =
        profile_policies.SetProfileWideExpansions(
            {{"profileWideVar", "profileWideValue"}});
    EXPECT_THAT(modified_guids, IsEmpty());
  }

  profile_policies.ApplyOncNetworkConfigurationList(network_configs);

  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              ElementsAre(Pair("guid1", base::test::IsJson(R"(
                      {
                        "GUID": "guid1",
                        "profile_wide_expansions": {
                          "profileWideVar": "profileWideValue"
                        }
                      })"))));
}

// Tests that nothing happens when the variable expansions change if no
// NetworkConfiguration depends on them. This is emulated by using the NoOp
// expander (in reality it would happen if no network configuration contains a
// known expansion).
TEST(ProfilePoliciesTest, ChangeNoEffect) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::List network_configs = NetworkConfigsList({&network_config_1});

  ProfilePolicies profile_policies;
  profile_policies.SetRuntimeValuesSetterForTesting(base::BindRepeating(&NoOp));
  profile_policies.ApplyOncNetworkConfigurationList(network_configs);

  base::flat_set<std::string> modified_guids =
      profile_policies.SetProfileWideExpansions(
          {{"profileWideVar", "profileWideValue"}});
  EXPECT_THAT(modified_guids, IsEmpty());
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(
                  Pair("guid1", base::test::IsJson(network_config_1))));
}

// Tests that when variable expansions (both profile-wide and network-specific)
// change and a NetworkConfiguration depends on them, the policy value with
// expansions is updated accordingly and the setter for the expansions returns
// back the set of affected NetworkConfiguration GUIDs to its caller.
TEST(ProfilePoliciesTest, ExpansionsChangeAffectsNetworkConfiguration) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::Dict network_config_2 = NetworkConfig("guid2");
  base::Value::List network_configs =
      NetworkConfigsList({&network_config_1, &network_config_2});

  ProfilePolicies profile_policies;

  // Replace the variable expander with one which is simply injecting all
  // expansions as top-level dictionary keys instead.
  profile_policies.SetRuntimeValuesSetterForTesting(
      base::BindRepeating(&Inject));

  profile_policies.ApplyOncNetworkConfigurationList(network_configs);

  {
    base::flat_set<std::string> modified_guids =
        profile_policies.SetProfileWideExpansions(
            {{"profileWideVar", "profileWideValue"}});
    EXPECT_THAT(modified_guids, UnorderedElementsAre("guid1", "guid2"));
  }

  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(Pair("guid1", base::test::IsJson(R"(
                      {
                        "GUID": "guid1",
                        "profile_wide_expansions": {
                          "profileWideVar": "profileWideValue"
                        }
                      })")),
                                   Pair("guid2", base::test::IsJson(R"(
                      {
                        "GUID": "guid2",
                        "profile_wide_expansions": {
                          "profileWideVar": "profileWideValue"
                        }
                      })"))));

  {
    bool change_had_effect = profile_policies.SetResolvedClientCertificate(
        "guid1", client_cert::ResolvedCert::CertMatched(
                     1, "pkcs11_id", {{"certVar", "certVarValue"}}));
    EXPECT_TRUE(change_had_effect);
  }

  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(Pair("guid1", base::test::IsJson(R"(
                        {
                          "GUID": "guid1",
                          "profile_wide_expansions": {
                            "profileWideVar": "profileWideValue"
                          },
                          "cert_info": {
                            "slot_id": 1,
                            "pkcs11_id": "pkcs11_id",
                            "certVar": "certVarValue"
                          }
                        })")),
                                   Pair("guid2", base::test::IsJson(R"(
                        {
                          "GUID": "guid2",
                          "profile_wide_expansions": {
                            "profileWideVar": "profileWideValue"
                          }
                        })"))));

  {
    bool change_had_effect = profile_policies.SetResolvedClientCertificate(
        "guid1", client_cert::ResolvedCert::NothingMatched());
    EXPECT_TRUE(change_had_effect);
  }

  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(Pair("guid1", base::test::IsJson(R"(
                        {
                          "GUID": "guid1",
                          "profile_wide_expansions": {
                            "profileWideVar": "profileWideValue"
                          },
                          "cert_info": {
                            "status": "no cert"
                          }
                        })")),
                                   Pair("guid2", base::test::IsJson(R"(
                        {
                          "GUID": "guid2",
                          "profile_wide_expansions": {
                            "profileWideVar": "profileWideValue"
                          }
                        })"))));

  {
    base::flat_set<std::string> modified_guids =
        profile_policies.SetProfileWideExpansions({});
    EXPECT_THAT(modified_guids, UnorderedElementsAre("guid1", "guid2"));
  }

  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(Pair("guid1", base::test::IsJson(R"(
                        {
                          "GUID": "guid1",
                          "profile_wide_expansions": {},
                          "cert_info": {
                            "status": "no cert"
                          }
                        })")),
                                   Pair("guid2", base::test::IsJson(R"(
                        {
                          "GUID": "guid2",
                          "profile_wide_expansions": {}
                        })"))));
}

// Tests that variable expansions (both profile-wide and per-network) are
// applied when a NetworkConfiguration changes.
TEST(ProfilePoliciesTest, NetworkConfigurationChangeWithExistingExpansions) {
  ProfilePolicies profile_policies;

  // Replace the variable expander with one which is simply injecting all
  // expansions as top-level dictionary keys instead.
  profile_policies.SetRuntimeValuesSetterForTesting(
      base::BindRepeating(&Inject));

  {
    base::flat_set<std::string> modified_guids =
        profile_policies.SetProfileWideExpansions(
            {{"profileWideVar", "profileWideValue"}});
    EXPECT_THAT(modified_guids, IsEmpty());
  }

  {
    base::Value::Dict network_config_1 = NetworkConfig("guid1");
    base::Value::List network_configs = NetworkConfigsList({&network_config_1});
    base::flat_set<std::string> modified_guids =
        profile_policies.ApplyOncNetworkConfigurationList(network_configs);
    EXPECT_THAT(modified_guids, UnorderedElementsAre("guid1"));
  }

  {
    bool change_had_effect = profile_policies.SetResolvedClientCertificate(
        "guid1", client_cert::ResolvedCert::CertMatched(
                     1, "pkcs11_id", {{"certVar", "certVarValue"}}));
    EXPECT_TRUE(change_had_effect);
  }
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(Pair("guid1", base::test::IsJson(R"(
                      {
                        "GUID": "guid1",
                        "profile_wide_expansions": {
                          "profileWideVar": "profileWideValue"
                        },
                        "cert_info": {
                          "slot_id": 1,
                          "pkcs11_id": "pkcs11_id",
                          "certVar": "certVarValue"
                        }
                      })"))));
  {
    base::Value::Dict network_config_1 = NetworkConfig("guid1");
    network_config_1.Set("modified", "yes");
    base::Value::List network_configs = NetworkConfigsList({&network_config_1});
    base::flat_set<std::string> modified_guids =
        profile_policies.ApplyOncNetworkConfigurationList(network_configs);
    EXPECT_THAT(modified_guids, UnorderedElementsAre("guid1"));
  }
  EXPECT_THAT(profile_policies.GetGuidToPolicyMap(),
              UnorderedElementsAre(Pair("guid1", base::test::IsJson(R"(
                      {
                        "GUID": "guid1",
                        "modified": "yes",
                        "profile_wide_expansions": {
                          "profileWideVar": "profileWideValue"
                        },
                        "cert_info": {
                          "slot_id": 1,
                          "pkcs11_id": "pkcs11_id",
                          "certVar": "certVarValue"
                        }
                      })"))));
}

// Tests that GetOriginalPolicyByGuid returns the policy without
// variable expansions.
TEST(ProfilePoliciesTest, GetOriginalPolicyByGuid) {
  base::Value::Dict network_config_1 = NetworkConfig("guid1");
  base::Value::List network_configs = NetworkConfigsList({&network_config_1});

  ProfilePolicies profile_policies;

  // Replace the variable expander with one which is simply injecting all
  // expansions as top-level dictionary keys instead.
  profile_policies.SetRuntimeValuesSetterForTesting(
      base::BindRepeating(&Inject));

  profile_policies.ApplyOncNetworkConfigurationList(network_configs);

  profile_policies.SetProfileWideExpansions(
      {{"profileWideVar", "profileWideValue"}});
  const base::Value::Dict* policy_with_expansions =
      profile_policies.GetPolicyByGuid("guid1");
  const base::Value::Dict* policy_without_expansions =
      profile_policies.GetOriginalPolicyByGuid("guid1");
  ASSERT_TRUE(policy_with_expansions);
  ASSERT_TRUE(policy_without_expansions);

  EXPECT_THAT(*policy_with_expansions, base::test::IsJson(R"(
                    {
                      "GUID": "guid1",
                      "profile_wide_expansions": {
                        "profileWideVar": "profileWideValue"
                      }
                    })"));
  EXPECT_THAT(*policy_without_expansions, base::test::IsJson(R"(
                    {
                      "GUID": "guid1"
                    })"));
}

// Tests that setting per-network variable expansions doesn't do anything (and
// doesn't crash) if the GUID is unknown.
TEST(ProfilePoliciesTest, SetResolvedClientCertificateUnknownGuid) {
  ProfilePolicies profile_policies;

  // Replace the variable expander with one which is simply injecting all
  // expansions as top-level dictionary keys instead.
  profile_policies.SetRuntimeValuesSetterForTesting(
      base::BindRepeating(&Inject));

  bool change_had_effect = profile_policies.SetResolvedClientCertificate(
      "unknown_guid", client_cert::ResolvedCert::CertMatched(
                          1, "pkcs11_id", {{"var1", "value1"}}));
  EXPECT_FALSE(change_had_effect);
}

}  // namespace ash