File: arc_settings_service_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (915 lines) | stat: -rw-r--r-- 36,874 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
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/intent_helper/arc_settings_service.h"

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/task/current_thread.h"
#include "base/values.h"
#include "chrome/browser/ash/policy/handlers/configuration_policy_handler_ash.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/ash/components/dbus/shill/shill_ipconfig_client.h"
#include "chromeos/ash/components/dbus/shill/shill_profile_client.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/proxy/proxy_config_handler.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "chromeos/ash/experiences/arc/test/arc_util_test_support.h"
#include "chromeos/ash/experiences/arc/test/connection_holder_util.h"
#include "chromeos/ash/experiences/arc/test/fake_backup_settings_instance.h"
#include "chromeos/ash/experiences/arc/test/fake_intent_helper_instance.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/proxy_config/proxy_prefs.h"
#include "content/public/test/browser_test.h"
#include "net/proxy_resolution/proxy_bypass_rules.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

using testing::_;
using testing::Return;

namespace arc {

namespace {
constexpr char kONCPolicy[] =
    "{ \"NetworkConfigurations\": ["
    "    { \"GUID\": \"stub_ethernet_guid\","
    "      \"Type\": \"Ethernet\","
    "      \"Name\": \"My Ethernet\","
    "      \"Ethernet\": {"
    "        \"Authentication\": \"None\" },"
    "      \"ProxySettings\": {"
    "        \"PAC\": \"http://domain.com/x\","
    "        \"Type\": \"PAC\" }"
    "    }"
    "  ],"
    "  \"Type\": \"UnencryptedConfiguration\""
    "}";

constexpr char kDeviceONCPolicy[] =
    "{"
    "   \"GlobalNetworkConfiguration\": {"
    "      \"AllowOnlyPolicyNetworksToAutoconnect\": false,"
    "      \"AllowOnlyPolicyNetworksToConnect\": false"
    "   },"
    "   \"NetworkConfigurations\": [ {"
    "      \"GUID\": \"{wifi1_guid}\","
    "      \"Name\": \"wifi1\","
    "      \"ProxySettings\": {"
    "         \"Manual\": {"
    "            \"FTPProxy\": {"
    "               \"Host\": \"proxy\","
    "               \"Port\": 5000"
    "            },"
    "            \"HTTPProxy\": {"
    "               \"Host\": \"proxy\","
    "               \"Port\": 5000"
    "            },"
    "            \"SOCKS\": {"
    "               \"Host\": \"proxy\","
    "               \"Port\": 5000"
    "            },"
    "            \"SecureHTTPProxy\": {"
    "               \"Host\": \"proxy\","
    "               \"Port\": 5000"
    "            }"
    "         },"
    "         \"Type\": \"Manual\""
    "      },"
    "      \"Type\": \"WiFi\","
    "      \"WiFi\": {"
    "         \"AutoConnect\": false,"
    "         \"HiddenSSID\": false,"
    "         \"SSID\": \"wifi1\","
    "         \"Security\": \"None\""
    "      }"
    "   } ]"
    "}";

constexpr char kUserONCPolicy[] =
    "{"
    "   \"NetworkConfigurations\": [ {"
    "      \"GUID\": \"{direct_guid}\","
    "      \"Name\": \"EAP-TTLS\","
    "      \"ProxySettings\": {"
    "         \"Type\": \"Direct\""
    "      },"
    "      \"Type\": \"WiFi\","
    "      \"WiFi\": {"
    "         \"AutoConnect\": false,"
    "         \"EAP\": {"
    "            \"Identity\": \"CrOS\","
    "            \"Inner\": \"Automatic\","
    "            \"Outer\": \"EAP-TTLS\","
    "            \"Password\": \"********\","
    "            \"Recommended\": ["
    "              \"AnonymousIdentity\","
    "              \"Identity\","
    "              \"Password\""
    "            ],"
    "            \"SaveCredentials\": true,"
    "            \"UseSystemCAs\": false"
    "         },"
    "         \"HiddenSSID\": false,"
    "        \"SSID\": \"direct_ssid\","
    "        \"Security\": \"WPA-EAP\""
    "     }"
    "  }, {"
    "      \"GUID\": \"{wifi0_guid}\","
    "      \"Name\": \"wifi0\","
    "      \"ProxySettings\": {"
    "         \"Manual\": {"
    "            \"FTPProxy\": {"
    "               \"Host\": \"proxy-n300\","
    "               \"Port\": 3000"
    "            },"
    "            \"HTTPProxy\": {"
    "               \"Host\": \"proxy-n300\","
    "               \"Port\": 3000"
    "            },"
    "            \"SOCKS\": {"
    "               \"Host\": \"proxy-n300\","
    "               \"Port\": 3000"
    "            },"
    "            \"SecureHTTPProxy\": {"
    "               \"Host\": \"proxy-n300\","
    "               \"Port\": 3000"
    "            }"
    "         },"
    "         \"Type\": \"Manual\""
    "      },"
    "      \"Type\": \"WiFi\","
    "      \"WiFi\": {"
    "         \"AutoConnect\": false,"
    "         \"HiddenSSID\": false,"
    "         \"SSID\": \"wifi0\","
    "         \"Security\": \"None\""
    "      }"
    "   } ]"
    "}";

constexpr char kUserProfilePath[] = "user_profile";
constexpr char kDefaultServicePath[] = "stub_ethernet";

constexpr char kWifi0ServicePath[] = "stub_wifi0";
constexpr char kWifi0Ssid[] = "wifi0";
constexpr char kWifi0Guid[] = "{wifi0_guid}";

constexpr char kWifi1ServicePath[] = "stub_wifi1";
constexpr char kWifi1Ssid[] = "wifi1";
constexpr char kWifi1Guid[] = "{wifi1_guid}";

constexpr char kONCPacUrl[] = "http://domain.com/x";

constexpr char kSetProxyBroadcastAction[] =
    "org.chromium.arc.intent_helper.SET_PROXY";

// Returns the number of |broadcasts| having the proxy action, and checks that
// all their extras match with |extras|.
int CountProxyBroadcasts(
    const std::vector<FakeIntentHelperInstance::Broadcast>& broadcasts,
    const std::vector<base::Value::Dict*>& extras) {
  unsigned long count = 0;
  for (const FakeIntentHelperInstance::Broadcast& broadcast : broadcasts) {
    if (broadcast.action == kSetProxyBroadcastAction) {
      DCHECK(count < extras.size())
          << "The expected proxy broadcast count is smaller than "
             "the actual count.";
      EXPECT_EQ(*base::JSONReader::Read(broadcast.extras), *extras[count]);
      count++;
    }
  }
  return count;
}

void RunUntilIdle() {
  DCHECK(base::CurrentThread::Get());
  base::RunLoop loop;
  loop.RunUntilIdle();
}

}  // namespace

class ArcSettingsServiceTest : public InProcessBrowserTest {
 public:
  ArcSettingsServiceTest() {
    feature_list_.InitAndDisableFeature(ash::features::kCrosPrivacyHub);
  }
  ArcSettingsServiceTest(const ArcSettingsServiceTest&) = delete;
  ArcSettingsServiceTest& operator=(const ArcSettingsServiceTest&) = delete;

  // InProcessBrowserTest:
  ~ArcSettingsServiceTest() override = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    arc::SetArcAvailableCommandLineForTesting(command_line);
  }

  void SetUpInProcessBrowserTestFixture() override {
    provider_.SetDefaultReturns(
        /*is_initialization_complete_return=*/true,
        /*is_first_policy_load_complete_return=*/true);
    policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
  }

  void SetUpOnMainThread() override {
    SetupNetworkEnvironment();
    RunUntilIdle();

    fake_intent_helper_instance_ = std::make_unique<FakeIntentHelperInstance>();
    ArcServiceManager::Get()
        ->arc_bridge_service()
        ->intent_helper()
        ->SetInstance(fake_intent_helper_instance_.get());
    WaitForInstanceReady(
        ArcServiceManager::Get()->arc_bridge_service()->intent_helper());

    fake_backup_settings_instance_ =
        std::make_unique<FakeBackupSettingsInstance>();
    ArcServiceManager::Get()
        ->arc_bridge_service()
        ->backup_settings()
        ->SetInstance(fake_backup_settings_instance_.get());
    WaitForInstanceReady(
        ArcServiceManager::Get()->arc_bridge_service()->backup_settings());
  }

  void TearDownOnMainThread() override {
    ArcServiceManager::Get()
        ->arc_bridge_service()
        ->backup_settings()
        ->CloseInstance(fake_backup_settings_instance_.get());
    fake_backup_settings_instance_.reset();

    ArcServiceManager::Get()
        ->arc_bridge_service()
        ->intent_helper()
        ->CloseInstance(fake_intent_helper_instance_.get());
    fake_intent_helper_instance_.reset();
  }

  void UpdatePolicy(const policy::PolicyMap& policy) {
    provider_.UpdateChromePolicy(policy);
    RunUntilIdle();
  }

 protected:
  void DisconnectNetworkService(const std::string& service_path) {
    ash::ShillServiceClient::TestInterface* service_test =
        ash::ShillServiceClient::Get()->GetTestInterface();
    base::Value value(shill::kStateIdle);
    service_test->SetServiceProperty(service_path, shill::kStateProperty,
                                     value);
    RunUntilIdle();
  }

  void ConnectWifiNetworkService(const std::string& service_path,
                                 const std::string& guid,
                                 const std::string& ssid) {
    ash::ShillServiceClient::TestInterface* service_test =
        ash::ShillServiceClient::Get()->GetTestInterface();

    service_test->AddService(service_path, guid, ssid, shill::kTypeWifi,
                             shill::kStateOnline, true /* add_to_visible */);

    service_test->SetServiceProperty(service_path, shill::kProfileProperty,
                                     base::Value(kUserProfilePath));
    RunUntilIdle();
  }

  void SetProxyConfigForNetworkService(const std::string& service_path,
                                       base::Value::Dict proxy_config) {
    ProxyConfigDictionary proxy_config_dict(std::move(proxy_config));
    const ash::NetworkState* network =
        ash::NetworkHandler::Get()->network_state_handler()->GetNetworkState(
            service_path);
    ASSERT_TRUE(network);
    ash::proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
  }

  std::unique_ptr<FakeIntentHelperInstance> fake_intent_helper_instance_;
  std::unique_ptr<FakeBackupSettingsInstance> fake_backup_settings_instance_;

 private:
  void SetupNetworkEnvironment() {
    ash::ShillProfileClient::TestInterface* profile_test =
        ash::ShillProfileClient::Get()->GetTestInterface();
    ash::ShillServiceClient::TestInterface* service_test =
        ash::ShillServiceClient::Get()->GetTestInterface();

    profile_test->AddProfile(kUserProfilePath, "user");

    service_test->ClearServices();

    service_test->AddService(kDefaultServicePath, "stub_ethernet_guid", "eth0",
                             shill::kTypeEthernet, shill::kStateOnline,
                             true /* add_to_visible */);
    service_test->SetServiceProperty(kDefaultServicePath,
                                     shill::kProfileProperty,
                                     base::Value(kUserProfilePath));
  }

  testing::NiceMock<policy::MockConfigurationPolicyProvider> provider_;
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, BackupRestorePolicyTest) {
  // The policy is initially set to user control.
  policy::PolicyMap policy;
  policy.Set(policy::key::kArcBackupRestoreServiceEnabled,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD,
             base::Value(static_cast<int>(
                 policy::ArcServicePolicyValue::kUnderUserControl)),
             nullptr);
  UpdatePolicy(policy);

  PrefService* const prefs = browser()->profile()->GetPrefs();

  // Set the user pref as initially enabled.
  prefs->SetBoolean(prefs::kArcBackupRestoreEnabled, true);
  EXPECT_TRUE(prefs->GetBoolean(prefs::kArcBackupRestoreEnabled));

  fake_backup_settings_instance_->ClearCallHistory();

  // The policy is set to disabled.
  policy.Set(
      policy::key::kArcBackupRestoreServiceEnabled,
      policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
      policy::POLICY_SOURCE_CLOUD,
      base::Value(static_cast<int>(policy::ArcServicePolicyValue::kDisabled)),
      nullptr);
  UpdatePolicy(policy);

  // The pref is disabled and managed, but the corresponding sync method does
  // not reflect the pref as it is not dynamically applied.
  EXPECT_FALSE(prefs->GetBoolean(prefs::kArcBackupRestoreEnabled));
  EXPECT_TRUE(prefs->IsManagedPreference(prefs::kArcBackupRestoreEnabled));
  EXPECT_EQ(0, fake_backup_settings_instance_->set_backup_enabled_count());
  EXPECT_FALSE(fake_backup_settings_instance_->enabled());
  EXPECT_FALSE(fake_backup_settings_instance_->managed());

  fake_backup_settings_instance_->ClearCallHistory();

  // The policy is set to user control.
  policy.Set(policy::key::kArcBackupRestoreServiceEnabled,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD,
             base::Value(static_cast<int>(
                 policy::ArcServicePolicyValue::kUnderUserControl)),
             nullptr);
  UpdatePolicy(policy);

  // The pref is unmanaged, but the corresponding sync method does not reflect
  // the pref as it is not dynamically applied.
  EXPECT_TRUE(prefs->GetBoolean(prefs::kArcBackupRestoreEnabled));
  EXPECT_FALSE(prefs->IsManagedPreference(prefs::kArcBackupRestoreEnabled));
  EXPECT_EQ(0, fake_backup_settings_instance_->set_backup_enabled_count());
  EXPECT_FALSE(fake_backup_settings_instance_->enabled());
  EXPECT_FALSE(fake_backup_settings_instance_->managed());

  fake_backup_settings_instance_->ClearCallHistory();

  // The policy is set to enabled.
  policy.Set(
      policy::key::kArcBackupRestoreServiceEnabled,
      policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
      policy::POLICY_SOURCE_CLOUD,
      base::Value(static_cast<int>(policy::ArcServicePolicyValue::kEnabled)),
      nullptr);
  UpdatePolicy(policy);

  // The pref is enabled and managed, but the corresponding sync method does
  // not reflect the pref as it is not dynamically applied.
  EXPECT_TRUE(prefs->GetBoolean(prefs::kArcBackupRestoreEnabled));
  EXPECT_TRUE(prefs->IsManagedPreference(prefs::kArcBackupRestoreEnabled));
  EXPECT_EQ(0, fake_backup_settings_instance_->set_backup_enabled_count());
  EXPECT_FALSE(fake_backup_settings_instance_->enabled());
  EXPECT_FALSE(fake_backup_settings_instance_->managed());
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, LocationServicePolicyTest) {
  // The policy is initially set to user control.
  policy::PolicyMap policy;
  policy.Set(policy::key::kArcGoogleLocationServicesEnabled,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD,
             base::Value(static_cast<int>(
                 policy::ArcServicePolicyValue::kUnderUserControl)),
             nullptr);
  UpdatePolicy(policy);

  PrefService* const prefs = browser()->profile()->GetPrefs();

  // Set the user pref as initially enabled.
  prefs->SetBoolean(prefs::kArcLocationServiceEnabled, true);
  EXPECT_TRUE(prefs->GetBoolean(prefs::kArcLocationServiceEnabled));

  fake_intent_helper_instance_->clear_broadcasts();

  // The policy is set to disabled.
  policy.Set(
      policy::key::kArcGoogleLocationServicesEnabled,
      policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
      policy::POLICY_SOURCE_CLOUD,
      base::Value(static_cast<int>(policy::ArcServicePolicyValue::kDisabled)),
      nullptr);
  UpdatePolicy(policy);

  // The pref is disabled and managed, but no broadcast is sent as the setting
  // is not dynamically applied.
  EXPECT_FALSE(prefs->GetBoolean(prefs::kArcLocationServiceEnabled));
  EXPECT_TRUE(prefs->IsManagedPreference(prefs::kArcLocationServiceEnabled));
  EXPECT_EQ(0UL, fake_intent_helper_instance_->broadcasts().size());

  fake_intent_helper_instance_->clear_broadcasts();

  // The policy is set to user control.
  policy.Set(policy::key::kArcGoogleLocationServicesEnabled,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD,
             base::Value(static_cast<int>(
                 policy::ArcServicePolicyValue::kUnderUserControl)),
             nullptr);
  UpdatePolicy(policy);

  // The pref is unmanaged, but no broadcast is sent as the setting is not
  // dynamically applied.
  EXPECT_FALSE(prefs->IsManagedPreference(prefs::kArcLocationServiceEnabled));
  EXPECT_EQ(0UL, fake_intent_helper_instance_->broadcasts().size());

  // The policy is set to enabled.
  policy.Set(
      policy::key::kArcGoogleLocationServicesEnabled,
      policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
      policy::POLICY_SOURCE_CLOUD,
      base::Value(static_cast<int>(policy::ArcServicePolicyValue::kEnabled)),
      nullptr);
  UpdatePolicy(policy);

  // The pref is enabled and managed, but no broadcast is sent as the setting
  // is not dynamically applied.
  EXPECT_TRUE(prefs->GetBoolean(prefs::kArcLocationServiceEnabled));
  EXPECT_TRUE(prefs->IsManagedPreference(prefs::kArcLocationServiceEnabled));
  EXPECT_EQ(0UL, fake_intent_helper_instance_->broadcasts().size());
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyModePolicyTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  policy::PolicyMap policy;
  policy.Set(policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY,
             policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
             base::Value(ProxyPrefs::kAutoDetectProxyModeName), nullptr);
  UpdatePolicy(policy);

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set("mode",
                            base::Value(ProxyPrefs::kAutoDetectProxyModeName));
  expected_proxy_config.Set("pacUrl", base::Value("http://wpad/wpad.dat"));
  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ONCProxyPolicyTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  policy::PolicyMap policy;
  policy.Set(policy::key::kOpenNetworkConfiguration,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD, base::Value(kONCPolicy), nullptr);
  UpdatePolicy(policy);

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set("mode",
                            base::Value(ProxyPrefs::kPacScriptProxyModeName));
  expected_proxy_config.Set("pacUrl", base::Value(kONCPacUrl));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

// Test to verify that, when enabled, the local proxy address is synced instead
// of the real proxy set via policy.
IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest,
                       SystemProxyAddressForwardedTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  policy::PolicyMap policy;
  policy.Set(policy::key::kOpenNetworkConfiguration,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD, base::Value(kONCPolicy), nullptr);
  UpdatePolicy(policy);

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set("mode",
                            base::Value(ProxyPrefs::kPacScriptProxyModeName));
  expected_proxy_config.Set("pacUrl", base::Value(kONCPacUrl));

  // Set the user preference to indicate that ARC should connect to
  // System-proxy.
  browser()->profile()->GetPrefs()->Set(
      ::prefs::kSystemProxyUserTrafficHostAndPort,
      base::Value("local_proxy:3128"));
  RunUntilIdle();

  base::Value::Dict expected_proxy_config_system_proxy;
  expected_proxy_config_system_proxy.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config_system_proxy.Set("host", base::Value("local_proxy"));
  expected_proxy_config_system_proxy.Set("port", 3128);

  // Unset the System-proxy preference to verify that ARC syncs proxy configs
  // correctly when System-proxy is disabled.
  browser()->profile()->GetPrefs()->Set(
      ::prefs::kSystemProxyUserTrafficHostAndPort, base::Value(""));
  RunUntilIdle();

  EXPECT_EQ(CountProxyBroadcasts(
                fake_intent_helper_instance_->broadcasts(),
                {&expected_proxy_config, &expected_proxy_config_system_proxy,
                 &expected_proxy_config}),
            3);
}

// Test to verify that the address of the local proxy is not forwarded if
// there's no proxy set in the browser.
IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest,
                       SystemProxyAddressNotForwardedForDirectMode) {
  fake_intent_helper_instance_->clear_broadcasts();

  policy::PolicyMap policy;
  // Apply ONC policy with direct proxy.
  policy.Set(policy::key::kDeviceOpenNetworkConfiguration,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
             policy::POLICY_SOURCE_CLOUD, base::Value(kDeviceONCPolicy),
             nullptr);
  UpdatePolicy(policy);

  // Set the user preference to indicate that ARC should connect to
  // System-proxy.
  browser()->profile()->GetPrefs()->Set(
      ::prefs::kSystemProxyUserTrafficHostAndPort,
      base::Value("local_proxy:3128"));
  RunUntilIdle();

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set("mode",
                            base::Value(ProxyPrefs::kDirectProxyModeName));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

// Proxy policy has a higher priority than proxy default settings.
IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, TwoSourcesTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  policy::PolicyMap policy;
  // Proxy policy.
  policy.Set(policy::key::kProxyMode, policy::POLICY_LEVEL_MANDATORY,
             policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
             base::Value(ProxyPrefs::kFixedServersProxyModeName), nullptr);
  policy.Set(policy::key::kProxyServer, policy::POLICY_LEVEL_MANDATORY,
             policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
             base::Value("proxy:8888"), nullptr);
  UpdatePolicy(policy);

  base::Value::Dict proxy_config;
  proxy_config.Set("mode", base::Value(ProxyPrefs::kAutoDetectProxyModeName));
  ProxyConfigDictionary proxy_config_dict(std::move(proxy_config));
  const ash::NetworkState* network =
      ash::NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
  ASSERT_TRUE(network);
  ash::proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
  RunUntilIdle();

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config.Set("host", base::Value("proxy"));
  expected_proxy_config.Set("port", base::Value(8888));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxyPrefTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  base::Value::Dict proxy_config;
  proxy_config.Set("mode", base::Value(ProxyPrefs::kPacScriptProxyModeName));
  proxy_config.Set("pac_url", base::Value("http://proxy"));
  browser()->profile()->GetPrefs()->SetDict(proxy_config::prefs::kProxy,
                                            std::move(proxy_config));
  RunUntilIdle();

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set("mode",
                            base::Value(ProxyPrefs::kPacScriptProxyModeName));
  expected_proxy_config.Set("pacUrl", base::Value("http://proxy"));
  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, DefaultNetworkProxyConfigTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  base::Value::Dict proxy_config;
  proxy_config.Set("mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  proxy_config.Set("server", base::Value("proxy:8080"));
  SetProxyConfigForNetworkService(kDefaultServicePath, std::move(proxy_config));
  RunUntilIdle();

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config.Set("host", base::Value("proxy"));
  expected_proxy_config.Set("port", base::Value(8080));
  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

// Chrome and ARC use different delimiters for the string representation of the
// proxy bypass list. This test verifies that the string bypass list sent by
// Chrome to ARC is formatted in a way that Android code understands.
IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest,
                       ProxyBypassListStringRepresentationTest) {
  fake_intent_helper_instance_->clear_broadcasts();

  net::ProxyBypassRules chrome_proxy_bypass_rules;
  chrome_proxy_bypass_rules.AddRuleFromString("test1.org");
  chrome_proxy_bypass_rules.AddRuleFromString("test2.org");

  const char kArcProxyBypassList[] = "test1.org,test2.org";

  base::Value::Dict proxy_config;
  proxy_config.Set("mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  proxy_config.Set("server", base::Value("proxy:8080"));
  proxy_config.Set("bypass_list",
                   base::Value(chrome_proxy_bypass_rules.ToString()));
  SetProxyConfigForNetworkService(kDefaultServicePath, std::move(proxy_config));
  RunUntilIdle();

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config.Set("host", base::Value("proxy"));
  expected_proxy_config.Set("port", base::Value(8080));
  expected_proxy_config.Set("bypassList", base::Value(kArcProxyBypassList));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, DefaultNetworkDisconnectedTest) {
  ConnectWifiNetworkService(kWifi0ServicePath, kWifi0Guid, kWifi0Ssid);
  fake_intent_helper_instance_->clear_broadcasts();
  // Set proxy confog for default network.
  base::Value::Dict default_proxy_config;
  default_proxy_config.Set("mode",
                           base::Value(ProxyPrefs::kFixedServersProxyModeName));
  default_proxy_config.Set("server", base::Value("default.proxy.test:8080"));
  SetProxyConfigForNetworkService(kDefaultServicePath,
                                  std::move(default_proxy_config));
  RunUntilIdle();

  // Set proxy confog for WI-FI network.
  base::Value::Dict wifi_proxy_config;
  wifi_proxy_config.Set("mode",
                        base::Value(ProxyPrefs::kFixedServersProxyModeName));
  wifi_proxy_config.Set("server", base::Value("wifi.proxy.test:8080"));
  SetProxyConfigForNetworkService(kWifi0ServicePath,
                                  std::move(wifi_proxy_config));
  RunUntilIdle();

  // Observe default network proxy config broadcast.
  base::Value::Dict expected_default_proxy_config;
  expected_default_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_default_proxy_config.Set("host", base::Value("default.proxy.test"));
  expected_default_proxy_config.Set("port", base::Value(8080));
  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_default_proxy_config}),
            1);

  // Disconnect default network.
  fake_intent_helper_instance_->clear_broadcasts();
  DisconnectNetworkService(kDefaultServicePath);

  // Observe WI-FI network proxy config broadcast.
  base::Value::Dict expected_wifi_proxy_config;
  expected_wifi_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_wifi_proxy_config.Set("host", base::Value("wifi.proxy.test"));
  expected_wifi_proxy_config.Set("port", base::Value(8080));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_wifi_proxy_config}),
            1);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, NoNetworkConnectedTest) {
  // Disconnect all networks.
  fake_intent_helper_instance_->clear_broadcasts();
  DisconnectNetworkService(kDefaultServicePath);

  EXPECT_EQ(
      CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(), {}), 0);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, TwoONCProxyPolicyTest) {
  // Connect to wifi1 with appliead device ONC policy.
  ConnectWifiNetworkService(kWifi1ServicePath, kWifi1Guid, kWifi1Ssid);

  // Disconnect default network.
  DisconnectNetworkService(kDefaultServicePath);

  fake_intent_helper_instance_->clear_broadcasts();

  policy::PolicyMap policy;
  policy.Set(policy::key::kOpenNetworkConfiguration,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
             policy::POLICY_SOURCE_CLOUD, base::Value(kUserONCPolicy), nullptr);
  policy.Set(policy::key::kDeviceOpenNetworkConfiguration,
             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
             policy::POLICY_SOURCE_CLOUD, base::Value(kDeviceONCPolicy),
             nullptr);
  UpdatePolicy(policy);

  base::Value::Dict expected_proxy_config;
  expected_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config.Set("host", base::Value("proxy"));
  expected_proxy_config.Set("port", base::Value(5000));

  base::Value::Dict expected_proxy_config_direct;
  expected_proxy_config_direct.Set(
      "mode", base::Value(ProxyPrefs::kDirectProxyModeName));

  EXPECT_EQ(CountProxyBroadcasts(
                fake_intent_helper_instance_->broadcasts(),
                {&expected_proxy_config, &expected_proxy_config_direct}),
            2);

  DisconnectNetworkService(kWifi1ServicePath);
  fake_intent_helper_instance_->clear_broadcasts();

  // Connect to wifi0 with appliead user ONC policy.
  ConnectWifiNetworkService(kWifi0ServicePath, kWifi0Guid, kWifi0Ssid);

  expected_proxy_config.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config.Set("host", base::Value("proxy-n300"));
  expected_proxy_config.Set("port", base::Value(3000));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config}),
            1);
}

// Test that on consumer devices, the proxy is correctly synced when the user
// changes network configurations.
IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, ProxySyncUnmanagedDevice) {
  fake_intent_helper_instance_->clear_broadcasts();

  std::vector<base::Value::Dict*> expected_proxy_configs;
  base::Value::Dict expected_proxy_config1;
  expected_proxy_config1.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config1.Set("host", base::Value("proxy"));
  expected_proxy_config1.Set("port", base::Value(1111));

  base::Value::Dict expected_proxy_config2;
  expected_proxy_config2.Set(
      "mode", base::Value(ProxyPrefs::kFixedServersProxyModeName));
  expected_proxy_config2.Set("host", base::Value("proxy"));
  expected_proxy_config2.Set("port", base::Value(2222));

  // The number of times to sync is randomly chosen. The only constraint is that
  // it has to be larger than two, as the proxy settings will be synced once at
  // ARC boot time and once when the default network is first changed.
  int proxy_sync_count = 10;

  for (int i = 0; i < proxy_sync_count; i += 2) {
    base::Value::Dict proxy_config1;
    proxy_config1.Set("mode",
                      base::Value(ProxyPrefs::kFixedServersProxyModeName));
    proxy_config1.Set("server", base::Value("proxy:1111"));
    SetProxyConfigForNetworkService(kDefaultServicePath,
                                    std::move(proxy_config1));
    expected_proxy_configs.push_back(&expected_proxy_config1);
    RunUntilIdle();

    base::Value::Dict proxy_config2;
    proxy_config2.Set("mode",
                      base::Value(ProxyPrefs::kFixedServersProxyModeName));
    proxy_config2.Set("server", base::Value("proxy:2222"));
    SetProxyConfigForNetworkService(kDefaultServicePath,
                                    std::move(proxy_config2));
    expected_proxy_configs.push_back(&expected_proxy_config2);
    RunUntilIdle();
  }

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 expected_proxy_configs),
            proxy_sync_count);
}

IN_PROC_BROWSER_TEST_F(ArcSettingsServiceTest, WebProxyAutoDiscovery) {
  fake_intent_helper_instance_->clear_broadcasts();

  // Set the proxy config to use auto-discovery. There's no PAC URL set via DHCP
  // so the URL "http://wpad/wpad.dat" set via DNS will be propagated to ARC.
  base::Value::Dict proxy_config_wpad;
  proxy_config_wpad.Set("mode",
                        base::Value(ProxyPrefs::kAutoDetectProxyModeName));
  browser()->profile()->GetPrefs()->SetDict(proxy_config::prefs::kProxy,
                                            std::move(proxy_config_wpad));

  RunUntilIdle();
  const char kWebProxyAutodetectionUrl[] = "www.proxyurl.com:443";

  ash::ShillIPConfigClient::TestInterface* ip_config_client =
      ash::ShillIPConfigClient::Get()->GetTestInterface();

  // Set the WPAD DHCP URL. This should now have precedence over the PAC URL set
  // via DNS.
  base::Value::Dict wpad_config;
  wpad_config.Set(shill::kWebProxyAutoDiscoveryUrlProperty,
                  base::Value(kWebProxyAutodetectionUrl));
  const std::string kIPConfigPath = "test_ip_config";
  ip_config_client->AddIPConfig(kIPConfigPath, std::move(wpad_config));

  ash::ShillServiceClient::TestInterface* service_test =
      ash::ShillServiceClient::Get()->GetTestInterface();

  service_test->SetServiceProperty(kDefaultServicePath,
                                   shill::kIPConfigProperty,
                                   base::Value(kIPConfigPath));
  RunUntilIdle();

  // Remove the proxy.
  base::Value::Dict proxy_config_direct;
  proxy_config_direct.Set("mode",
                          base::Value(ProxyPrefs::kDirectProxyModeName));
  browser()->profile()->GetPrefs()->SetDict(proxy_config::prefs::kProxy,
                                            std::move(proxy_config_direct));

  RunUntilIdle();
  base::Value::Dict expected_proxy_config_wpad_dns;
  expected_proxy_config_wpad_dns.Set(
      "mode", base::Value(ProxyPrefs::kAutoDetectProxyModeName));
  expected_proxy_config_wpad_dns.Set("pacUrl",
                                     base::Value("http://wpad/wpad.dat"));

  base::Value::Dict expected_proxy_config_wpad_dhcp;
  expected_proxy_config_wpad_dhcp.Set(
      "mode", base::Value(ProxyPrefs::kAutoDetectProxyModeName));
  expected_proxy_config_wpad_dhcp.Set("pacUrl",
                                      base::Value(kWebProxyAutodetectionUrl));

  base::Value::Dict expected_proxy_config_direct;
  expected_proxy_config_direct.Set(
      "mode", base::Value(ProxyPrefs::kDirectProxyModeName));

  EXPECT_EQ(CountProxyBroadcasts(fake_intent_helper_instance_->broadcasts(),
                                 {&expected_proxy_config_wpad_dns,
                                  &expected_proxy_config_wpad_dhcp,
                                  &expected_proxy_config_direct}),
            3);
}

}  // namespace arc