File: network_device_handler_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 (822 lines) | stat: -rw-r--r-- 37,503 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
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <optional>

#include "ash/constants/ash_features.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h"
#include "chromeos/ash/components/dbus/shill/fake_shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/cellular_metrics_logger.h"
#include "chromeos/ash/components/network/network_device_handler_impl.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_callbacks.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

namespace {

const char kDefaultCellularDevicePath[] = "stub_cellular_device";
const char kUnknownCellularDevicePath[] = "unknown_cellular_device";
const char kDefaultWifiDevicePath[] = "stub_wifi_device";
const char kResultFailure[] = "failure";
const char kResultSuccess[] = "success";
const char kDefaultPin[] = "1111";

}  // namespace

class NetworkDeviceHandlerTest : public testing::Test {
 public:
  NetworkDeviceHandlerTest()
      : task_environment_(
            base::test::SingleThreadTaskEnvironment::MainThreadType::UI) {}

  NetworkDeviceHandlerTest(const NetworkDeviceHandlerTest&) = delete;
  NetworkDeviceHandlerTest& operator=(const NetworkDeviceHandlerTest&) = delete;

  ~NetworkDeviceHandlerTest() override = default;

  void SetUp() override {
    shill_clients::InitializeFakes();
    fake_device_client_ = ShillDeviceClient::Get();
    fake_device_client_->GetTestInterface()->ClearDevices();

    network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();

    network_state_handler_ = NetworkStateHandler::InitializeForTest();
    NetworkDeviceHandlerImpl* device_handler = new NetworkDeviceHandlerImpl;
    device_handler->Init(network_state_handler_.get());
    network_device_handler_.reset(device_handler);

    // Add devices after handlers have been initialized.
    ShillDeviceClient::TestInterface* device_test =
        fake_device_client_->GetTestInterface();
    device_test->AddDevice(kDefaultCellularDevicePath, shill::kTypeCellular,
                           "cellular1");
    device_test->AddDevice(kDefaultWifiDevicePath, shill::kTypeWifi, "wifi1");

    base::Value::List test_ip_configs;
    test_ip_configs.Append("ip_config1");
    device_test->SetDeviceProperty(kDefaultWifiDevicePath,
                                   shill::kIPConfigsProperty,
                                   base::Value(std::move(test_ip_configs)),
                                   /*notify_changed=*/true);

    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    network_state_handler_->Shutdown();
    network_handler_test_helper_.reset();
    network_device_handler_.reset();
    network_state_handler_.reset();
    shill_clients::Shutdown();
  }

  base::OnceClosure GetSuccessCallback() {
    return base::BindOnce(&NetworkDeviceHandlerTest::SuccessCallback,
                          base::Unretained(this));
  }

  network_handler::ErrorCallback GetErrorCallback() {
    return base::BindOnce(&NetworkDeviceHandlerTest::ErrorCallback,
                          base::Unretained(this));
  }

  void ErrorCallback(const std::string& error_name) {
    VLOG(1) << "ErrorCallback: " << error_name;
    result_ = error_name;
  }

  void SuccessCallback() { result_ = kResultSuccess; }

  void GetPropertiesCallback(const std::string& device_path,
                             std::optional<base::Value::Dict> properties) {
    if (!properties) {
      result_ = kResultFailure;
      return;
    }
    result_ = kResultSuccess;
    properties_ = std::move(properties.value());
  }

  void StringSuccessCallback(const std::string& result) {
    VLOG(1) << "StringSuccessCallback: " << result;
    result_ = result;
  }

  void GetDeviceProperties(const std::string& device_path,
                           const std::string& expected_result) {
    network_device_handler_->GetDeviceProperties(
        device_path,
        base::BindOnce(&NetworkDeviceHandlerTest::GetPropertiesCallback,
                       base::Unretained(this)));
    base::RunLoop().RunUntilIdle();
    ASSERT_EQ(expected_result, result_);
  }

  void ExpectDeviceProperty(const std::string& device_path,
                            const std::string& property_name,
                            const std::string& expected_value) {
    GetDeviceProperties(device_path, kResultSuccess);
    std::string* value = properties_.FindString(property_name);
    ASSERT_NE(value, nullptr);
    ASSERT_EQ(*value, expected_value);
  }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  std::string result_;
  raw_ptr<ShillDeviceClient, DanglingUntriaged> fake_device_client_ = nullptr;
  std::unique_ptr<NetworkDeviceHandler> network_device_handler_;
  std::unique_ptr<NetworkStateHandler> network_state_handler_;
  std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
  base::Value::Dict properties_;
};

TEST_F(NetworkDeviceHandlerTest, GetDeviceProperties) {
  GetDeviceProperties(kDefaultWifiDevicePath, kResultSuccess);
  std::string* type = properties_.FindString(shill::kTypeProperty);
  ASSERT_TRUE(type);
  EXPECT_EQ(shill::kTypeWifi, *type);
}

TEST_F(NetworkDeviceHandlerTest, SetDeviceProperty) {
  // Set the shill::kScanIntervalProperty to true. The call
  // should succeed and the value should be set.
  network_device_handler_->SetDeviceProperty(
      kDefaultCellularDevicePath, shill::kScanIntervalProperty, base::Value(1),
      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);

  // GetDeviceProperties should return the value set by SetDeviceProperty.
  GetDeviceProperties(kDefaultCellularDevicePath, kResultSuccess);

  std::optional<int> interval =
      properties_.FindInt(shill::kScanIntervalProperty);
  EXPECT_TRUE(interval.has_value());
  EXPECT_EQ(1, interval.value());

  // Repeat the same with value false.
  network_device_handler_->SetDeviceProperty(
      kDefaultCellularDevicePath, shill::kScanIntervalProperty, base::Value(2),
      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);

  GetDeviceProperties(kDefaultCellularDevicePath, kResultSuccess);

  interval = properties_.FindInt(shill::kScanIntervalProperty);
  EXPECT_TRUE(interval.has_value());
  EXPECT_EQ(2, interval.value());

  // Set property on an invalid path.
  network_device_handler_->SetDeviceProperty(
      kUnknownCellularDevicePath, shill::kScanIntervalProperty, base::Value(1),
      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);

  // Setting a owner-protected device property through SetDeviceProperty must
  // fail.
  network_device_handler_->SetDeviceProperty(
      kDefaultCellularDevicePath, shill::kCellularPolicyAllowRoamingProperty,
      base::Value(true), GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_NE(kResultSuccess, result_);
}

TEST_F(NetworkDeviceHandlerTest, CellularAllowRoaming) {
  ShillDeviceClient::TestInterface* device_test =
      fake_device_client_->GetTestInterface();
  device_test->SetDeviceProperty(kDefaultCellularDevicePath,
                                 shill::kCellularPolicyAllowRoamingProperty,
                                 base::Value(false), /*notify_changed=*/true);

  network_device_handler_->SetCellularPolicyAllowRoaming(true);
  base::RunLoop().RunUntilIdle();

  GetDeviceProperties(kDefaultCellularDevicePath, kResultSuccess);

  std::optional<bool> policy_allow_roaming =
      properties_.FindBool(shill::kCellularPolicyAllowRoamingProperty);
  EXPECT_TRUE(policy_allow_roaming.has_value());
  EXPECT_TRUE(policy_allow_roaming.value());

  network_device_handler_->SetCellularPolicyAllowRoaming(false);
  base::RunLoop().RunUntilIdle();

  GetDeviceProperties(kDefaultCellularDevicePath, kResultSuccess);

  policy_allow_roaming =
      properties_.FindBool(shill::kCellularPolicyAllowRoamingProperty);
  EXPECT_TRUE(policy_allow_roaming.has_value());
  EXPECT_FALSE(policy_allow_roaming.value());
}

TEST_F(NetworkDeviceHandlerTest,
       ResetUsbEthernetMacAddressSourceForSecondaryUsbDevices) {
  ShillDeviceClient::TestInterface* device_test =
      fake_device_client_->GetTestInterface();

  constexpr char kSource[] = "some_source1";

  constexpr char kUsbEthernetDevicePath1[] = "usb_ethernet_device1";
  device_test->AddDevice(kUsbEthernetDevicePath1, shill::kTypeEthernet, "eth1");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath1, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath1,
                                 shill::kLinkUpProperty, base::Value(true),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath1,
                                 shill::kUsbEthernetMacAddressSourceProperty,
                                 base::Value("source_to_override1"),
                                 /*notify_changed=*/true);
  constexpr char kUsbEthernetDevicePath2[] = "usb_ethernet_device2";
  device_test->AddDevice(kUsbEthernetDevicePath2, shill::kTypeEthernet, "eth2");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath2, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath2,
                                 shill::kLinkUpProperty, base::Value(true),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath2,
                                 shill::kUsbEthernetMacAddressSourceProperty,
                                 base::Value(kSource),
                                 /*notify_changed=*/true);
  constexpr char kUsbEthernetDevicePath3[] = "usb_ethernet_device3";
  device_test->AddDevice(kUsbEthernetDevicePath3, shill::kTypeEthernet, "eth3");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath3, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath3,
                                 shill::kLinkUpProperty, base::Value(true),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath3,
                                 shill::kUsbEthernetMacAddressSourceProperty,
                                 base::Value("source_to_override2"),
                                 /*notify_changed=*/true);

  network_device_handler_->SetUsbEthernetMacAddressSource(kSource);
  base::RunLoop().RunUntilIdle();

  // Expect to reset source property for eth1 and eth3 since eth2 already has
  // needed source value.
  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath1, shill::kUsbEthernetMacAddressSourceProperty,
      "usb_adapter_mac"));
  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath2, shill::kUsbEthernetMacAddressSourceProperty,
      kSource));
  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath3, shill::kUsbEthernetMacAddressSourceProperty,
      "usb_adapter_mac"));
}

TEST_F(NetworkDeviceHandlerTest, UsbEthernetMacAddressSourceNotSupported) {
  ShillDeviceClient::TestInterface* device_test =
      fake_device_client_->GetTestInterface();

  constexpr char kSourceToOverride[] = "source_to_override";
  constexpr char kUsbEthernetDevicePath[] = "usb_ethernet_device1";
  device_test->AddDevice(kUsbEthernetDevicePath, shill::kTypeEthernet, "eth1");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath, shill::kLinkUpProperty,
                                 base::Value(true),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath,
                                 shill::kUsbEthernetMacAddressSourceProperty,
                                 base::Value(kSourceToOverride),
                                 /*notify_changed=*/true);
  device_test->SetUsbEthernetMacAddressSourceError(
      kUsbEthernetDevicePath, shill::kErrorResultNotSupported);

  network_device_handler_->SetUsbEthernetMacAddressSource("some_source1");
  base::RunLoop().RunUntilIdle();

  // Expect to do not change MAC address source property, because eth1 does not
  // support |some_source1|.
  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath, shill::kUsbEthernetMacAddressSourceProperty,
      kSourceToOverride));

  constexpr char kSource2[] = "some_source2";
  device_test->SetUsbEthernetMacAddressSourceError(kUsbEthernetDevicePath, "");
  network_device_handler_->SetUsbEthernetMacAddressSource(kSource2);
  base::RunLoop().RunUntilIdle();

  // Expect to change MAC address source property, because eth1 supports
  // |some_source2|.
  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath, shill::kUsbEthernetMacAddressSourceProperty,
      kSource2));
}

TEST_F(NetworkDeviceHandlerTest, UsbEthernetMacAddressSource) {
  ShillDeviceClient::TestInterface* device_test =
      fake_device_client_->GetTestInterface();

  constexpr char kUsbEthernetDevicePath1[] = "ubs_ethernet_device1";
  device_test->AddDevice(kUsbEthernetDevicePath1, shill::kTypeEthernet, "eth1");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath1, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);

  constexpr char kUsbEthernetDevicePath2[] = "usb_ethernet_device2";
  device_test->AddDevice(kUsbEthernetDevicePath2, shill::kTypeEthernet, "eth2");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath2, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath2,
                                 shill::kAddressProperty,
                                 base::Value("abcdef123456"),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath2,
                                 shill::kLinkUpProperty, base::Value(true),
                                 /*notify_changed=*/true);
  device_test->SetUsbEthernetMacAddressSourceError(
      kUsbEthernetDevicePath2, shill::kErrorResultNotSupported);

  constexpr char kUsbEthernetDevicePath3[] = "usb_ethernet_device3";
  device_test->AddDevice(kUsbEthernetDevicePath3, shill::kTypeEthernet, "eth3");
  device_test->SetDeviceProperty(
      kUsbEthernetDevicePath3, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypeUsb), /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath3,
                                 shill::kAddressProperty,
                                 base::Value("123456abcdef"),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kUsbEthernetDevicePath3,
                                 shill::kLinkUpProperty, base::Value(true),
                                 /*notify_changed=*/true);

  constexpr char kPciEthernetDevicePath[] = "pci_ethernet_device";
  device_test->AddDevice(kPciEthernetDevicePath, shill::kTypeEthernet, "eth4");
  device_test->SetDeviceProperty(
      kPciEthernetDevicePath, shill::kDeviceBusTypeProperty,
      base::Value(shill::kDeviceBusTypePci), /*notify_changed=*/true);

  // Expect property change on eth3, because:
  //   1) eth2 device is connected to the internet, but does not support MAC
  //      address change;
  //   2) eth3 device is connected to the internet and supports MAC address
  //      change.
  constexpr char kSource1[] = "some_source1";
  network_device_handler_->SetUsbEthernetMacAddressSource(kSource1);
  base::RunLoop().RunUntilIdle();

  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath3, shill::kUsbEthernetMacAddressSourceProperty,
      kSource1));

  // Expect property change on eth3, because device is connected to the
  // internet.
  const char* kSource2 = shill::kUsbEthernetMacAddressSourceBuiltinAdapterMac;
  network_device_handler_->SetUsbEthernetMacAddressSource(kSource2);
  base::RunLoop().RunUntilIdle();

  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath3, shill::kUsbEthernetMacAddressSourceProperty,
      kSource2));

  // Expect property change back to "usb_adapter_mac" on eth3, because device
  // is not connected to the internet.
  device_test->SetDeviceProperty(kUsbEthernetDevicePath3,
                                 shill::kLinkUpProperty, base::Value(false),
                                 /*notify_changed=*/true);
  base::RunLoop().RunUntilIdle();

  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath3, shill::kUsbEthernetMacAddressSourceProperty,
      "usb_adapter_mac"));

  // Expect property change back to "usb_adapter_mac" on eth1, because both
  // builtin PCI eth4 and eth1 have the same MAC address and connected to the
  // internet.
  device_test->SetDeviceProperty(kUsbEthernetDevicePath1,
                                 shill::kLinkUpProperty, base::Value(true),
                                 /*notify_changed=*/true);
  device_test->SetDeviceProperty(kPciEthernetDevicePath, shill::kLinkUpProperty,
                                 base::Value(true),
                                 /*notify_changed=*/true);
  base::RunLoop().RunUntilIdle();

  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath1, shill::kUsbEthernetMacAddressSourceProperty,
      "usb_adapter_mac"));

  // Expect property change on eth1, because device is connected to the
  // internet and builtin PCI eth4 and eth1 have different MAC addresses.
  constexpr char kSource3[] = "some_source3";
  network_device_handler_->SetUsbEthernetMacAddressSource(kSource3);
  base::RunLoop().RunUntilIdle();

  ASSERT_NO_FATAL_FAILURE(ExpectDeviceProperty(
      kUsbEthernetDevicePath1, shill::kUsbEthernetMacAddressSourceProperty,
      kSource3));
}

TEST_F(NetworkDeviceHandlerTest, RequirePin) {
  base::HistogramTester histogram_tester;

  // Test that the success callback gets called.
  network_device_handler_->RequirePin(kDefaultCellularDevicePath, true,
                                      kDefaultPin, GetSuccessCallback(),
                                      GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRequireLockSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kSimPinRequireLockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kRequirePinSuccessSimPinLockPolicyHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRequirePinSuccessSimPinLockPolicyHistogram, true,
      1);

  // Test that the shill error propagates to the error callback.
  network_device_handler_->RequirePin(kUnknownCellularDevicePath, true,
                                      kDefaultPin, GetSuccessCallback(),
                                      GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);

  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRequireLockSuccessHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kSimPinRequireLockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kRequirePinSuccessSimPinLockPolicyHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRequirePinSuccessSimPinLockPolicyHistogram, true,
      2);
}

TEST_F(NetworkDeviceHandlerTest, EnterPinOnManagedDevice) {
  base::HistogramTester histogram_tester;

  NetworkHandler::Get()->SetIsEnterpriseManaged(true);
  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/true);

  // Test that the success metrics get emitted for managed devices.
  network_device_handler_->EnterPin(kDefaultCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kManagedSimPinUnlockSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kManagedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 0);

  // Test that the shill error propagates to the error callback.
  network_device_handler_->EnterPin(kUnknownCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kManagedSimPinUnlockSuccessHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kManagedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 0);

  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/false);

  network_device_handler_->EnterPin(kDefaultCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);

  network_device_handler_->EnterPin(kUnknownCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
}

TEST_F(NetworkDeviceHandlerTest, EnterPinOnUnmanagedDevice) {
  base::HistogramTester histogram_tester;

  NetworkHandler::Get()->SetIsEnterpriseManaged(false);

  // Test that the success callback gets called.
  network_device_handler_->EnterPin(kDefaultCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kUnmanagedSimPinUnlockSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnmanagedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 0);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 0);

  // Test that the shill error propagates to the error callback.
  network_device_handler_->EnterPin(kUnknownCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kUnmanagedSimPinUnlockSuccessHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnmanagedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 0);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnlockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 0);
}

TEST_F(NetworkDeviceHandlerTest, UnblockPinOnManagedDevice) {
  base::HistogramTester histogram_tester;

  const char kPuk[] = "12345678";
  const char kPin[] = "1234";

  NetworkHandler::Get()->SetIsEnterpriseManaged(true);
  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/true);

  // Test that the success metrics get emitted for managed devices.
  network_device_handler_->UnblockPin(kDefaultCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kManagedSimPinUnblockSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kManagedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 0);

  // Test that the error metrics get emitted for managed devices.
  network_device_handler_->UnblockPin(kUnknownCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kManagedSimPinUnblockSuccessHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kManagedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 0);

  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/false);
  network_device_handler_->UnblockPin(kDefaultCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);

  network_device_handler_->UnblockPin(kUnknownCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
}

TEST_F(NetworkDeviceHandlerTest, UnblockPinOnUnmanagedDevice) {
  base::HistogramTester histogram_tester;

  const char kPuk[] = "12345678";
  const char kPin[] = "1234";

  NetworkHandler::Get()->SetIsEnterpriseManaged(false);

  // Test that the success callback gets called.
  network_device_handler_->UnblockPin(kDefaultCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kUnmanagedSimPinUnblockSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnmanagedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 0);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 0);

  // Test that the shill error propagates to the error callback.
  network_device_handler_->UnblockPin(kUnknownCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorDeviceMissing, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kUnmanagedSimPinUnblockSuccessHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnmanagedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kUnrestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 0);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRestrictedSimPinUnblockSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorDeviceMissing, 0);

  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRemoveLockSuccessHistogram, 0);

  // Test that if SIM PIN locking is prohibited, PUK unblocking a SIM will
  // also disable the SIM PIN lock setting.
  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/false);
  network_device_handler_->UnblockPin(kDefaultCellularDevicePath, kPin, kPuk,
                                      GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRemoveLockSuccessHistogram, 1);
}

TEST_F(NetworkDeviceHandlerTest, ChangePin) {
  base::HistogramTester histogram_tester;
  const char kNewPin[] = "1234";
  const char kIncorrectPin[] = "9999";

  fake_device_client_->GetTestInterface()->SetSimLocked(
      kDefaultCellularDevicePath, true);

  // Test that the success callback gets called.
  network_device_handler_->ChangePin(
      kDefaultCellularDevicePath, FakeShillDeviceClient::kDefaultSimPin,
      kNewPin, GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinChangeSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kSimPinChangeSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kSuccess, 1);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kChangePinSuccessSimPinLockPolicyHistogram, 1);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kChangePinSuccessSimPinLockPolicyHistogram, true,
      1);

  // Test that the shill error propagates to the error callback.
  network_device_handler_->ChangePin(kDefaultCellularDevicePath, kIncorrectPin,
                                     kNewPin, GetSuccessCallback(),
                                     GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorIncorrectPin, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinChangeSuccessHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kSimPinChangeSuccessHistogram,
      CellularMetricsLogger::SimPinOperationResult::kErrorIncorrectPin, 1);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kChangePinSuccessSimPinLockPolicyHistogram, 2);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kChangePinSuccessSimPinLockPolicyHistogram, true,
      2);
}

TEST_F(NetworkDeviceHandlerTest, RequirePinBlockedByPolicy) {
  base::HistogramTester histogram_tester;

  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/false);

  // Test that the error callback gets called when attempting to require a PIN
  // lock.
  network_device_handler_->RequirePin(kDefaultCellularDevicePath, true,
                                      kDefaultPin, GetSuccessCallback(),
                                      GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorBlockedByPolicy, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRequireLockSuccessHistogram, 0);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kRequirePinSuccessSimPinLockPolicyHistogram, 0);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kRequirePinSuccessSimPinLockPolicyHistogram, true,
      0);

  // Test that the success callback gets called when removing a PIN lock.
  network_device_handler_->RequirePin(kDefaultCellularDevicePath, false,
                                      kDefaultPin, GetSuccessCallback(),
                                      GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kResultSuccess, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRemoveLockSuccessHistogram, 1);
}

TEST_F(NetworkDeviceHandlerTest, ChangePinBlockedByPolicy) {
  base::HistogramTester histogram_tester;
  const char kNewPin[] = "1234";

  fake_device_client_->GetTestInterface()->SetSimLocked(
      kDefaultCellularDevicePath, true);

  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/false);

  // Test that the error callback gets called.
  network_device_handler_->ChangePin(
      kDefaultCellularDevicePath, FakeShillDeviceClient::kDefaultSimPin,
      kNewPin, GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(NetworkDeviceHandler::kErrorBlockedByPolicy, result_);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinChangeSuccessHistogram, 0);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kChangePinSuccessSimPinLockPolicyHistogram, 0);
  histogram_tester.ExpectBucketCount(
      CellularMetricsLogger::kChangePinSuccessSimPinLockPolicyHistogram, true,
      0);
}

TEST_F(NetworkDeviceHandlerTest, EnterPinWhenSimPinLockPolicyRestricted) {
  base::HistogramTester histogram_tester;

  fake_device_client_->GetTestInterface()->SetSimLocked(
      kDefaultCellularDevicePath, true);

  // Test that removing the PIN Lock requirement does not occur when the policy
  // is not applied.
  network_device_handler_->EnterPin(kDefaultCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();

  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRemoveLockSuccessHistogram, 0);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kUnmanagedSimPinUnlockSuccessHistogram, 1);

  network_device_handler_->SetAllowCellularSimLock(
      /*allow_cellular_sim_lock=*/false);

  // Test that removing the PIN Lock requirement does occur when the policy is
  // applied.
  network_device_handler_->EnterPin(kDefaultCellularDevicePath, kDefaultPin,
                                    GetSuccessCallback(), GetErrorCallback());
  base::RunLoop().RunUntilIdle();

  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kSimPinRemoveLockSuccessHistogram, 1);
  histogram_tester.ExpectTotalCount(
      CellularMetricsLogger::kUnmanagedSimPinUnlockSuccessHistogram, 2);
}
}  // namespace ash