File: euicc_status_uploader_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (728 lines) | stat: -rw-r--r-- 26,590 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
// Copyright 2021 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/policy/networking/euicc_status_uploader.h"

#include <algorithm>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/json/json_string_value_serializer.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/values_test_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/managed_cellular_pref_handler.h"
#include "chromeos/ash/components/network/managed_network_configuration_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_ui_data.h"
#include "chromeos/ash/components/network/policy_util.h"
#include "chromeos/components/onc/onc_utils.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace policy {

namespace {

class FakeCloudPolicyClient : public testing::NiceMock<MockCloudPolicyClient> {
 public:
  using UploadEuiccInfoCallbackHandler =
      base::RepeatingCallback<void(base::OnceCallback<void(bool)>)>;

  void SetHandler(UploadEuiccInfoCallbackHandler handler) {
    handler_ = handler;
  }
  void SetStatus(bool status) { status_ = status; }

  enterprise_management::UploadEuiccInfoRequest* GetLastRequest() {
    if (requests_.empty())
      return nullptr;
    return requests_.back().get();
  }

  int num_requests() const { return requests_.size(); }

 private:
  void UploadEuiccInfo(
      std::unique_ptr<enterprise_management::UploadEuiccInfoRequest> request,
      base::OnceCallback<void(bool)> callback) override {
    requests_.push_back(std::move(request));
    if (handler_) {
      handler_.Run(std::move(callback));
      return;
    }
    std::move(callback).Run(status_);
  }

  std::vector<std::unique_ptr<enterprise_management::UploadEuiccInfoRequest>>
      requests_;
  bool status_ = false;
  // This member is used to control how the callback is executed when
  // UploadEuiccInfo() is called.
  UploadEuiccInfoCallbackHandler handler_;
};

bool RequestsAreEqual(
    const enterprise_management::UploadEuiccInfoRequest& lhs,
    const enterprise_management::UploadEuiccInfoRequest& rhs) {
  const auto proj = [](const auto& profile) {
    return std::tie(profile.iccid(), profile.smdp_address());
  };
  return lhs.euicc_count() == rhs.euicc_count() &&
         std::ranges::equal(lhs.esim_profiles(), rhs.esim_profiles(),
                            std::equal_to<>(), proj, proj) &&
         lhs.clear_profile_list() == rhs.clear_profile_list();
}

const char kDmToken[] = "token";
const char kFakeObjectPath[] = "object-path";
const char kFakeEid[] = "12";
const char kEuiccStatusUploadResultHistogram[] =
    "Network.Cellular.ESim.Policy.EuiccStatusUploadResult";

const char kEuiccStatus_Empty[] =
    R"({
        "esim_profiles": [],
        "euicc_count": 0
       })";
const char kEuiccStatus_OneProfileWithMissingName[] =
    R"({
        "esim_profiles": [
          {
            "iccid": "iccid-1",
            "smdp_activation_code": "smdp-1"
          }
        ],
        "euicc_count": 1
       })";
const char kEuiccStatus_OneProfileWithEmptyActivationCode[] =
    R"({
        "esim_profiles": [],
        "euicc_count": 1
       })";
const char kEuiccStatus_OneProfile[] =
    R"({
        "esim_profiles": [
          {
            "iccid": "iccid-1",
            "network_name": "name-1",
            "smdp_activation_code": "smdp-1"
          }
        ],
        "euicc_count": 1
       })";
const char kEuiccStatus_TwoProfiles[] =
    R"({
        "esim_profiles": [
          {
            "iccid": "iccid-1",
            "network_name": "name-1",
            "smdp_activation_code": "smdp-1"
          },
          {
            "iccid": "iccid-2",
            "network_name": "name-2",
            "smdp_activation_code": "smdp-2"
          }
        ],
        "euicc_count": 2
       })";
const char kEuiccStatus_FourProfiles[] =
    R"({
        "esim_profiles": [
          {
            "iccid": "iccid-1",
            "network_name": "name-1",
            "smdp_activation_code": "smdp-1"
          },
          {
            "iccid": "iccid-2",
            "network_name": "name-2",
            "smdp_activation_code": "smdp-2"
          },
          {
            "iccid": "iccid-3",
            "network_name": "name-3",
            "smds_activation_code": "smds-3"
          },
          {
            "iccid": "iccid-4",
            "network_name": "name-4",
            "smds_activation_code": "smds-4"
          }
        ],
        "euicc_count": 3
       })";
const char kEuiccStatus_AfterReset[] =
    R"({
        "esim_profiles": [],
        "euicc_count": 4
      })";

const char kCellularServicePath0[] = "/service/cellular0";
const char kCellularServicePath1[] = "/service/cellular1";
const char kCellularServicePath2[] = "/service/cellular2";
const char kCellularServicePath3[] = "/service/cellular3";
const char kCellularProfilePath0[] = "/org/chromium/Hermes/Profile/0";
const char kCellularProfilePath1[] = "/org/chromium/Hermes/Profile/1";
const char kCellularProfilePath2[] = "/org/chromium/Hermes/Profile/2";
const char kCellularProfilePath3[] = "/org/chromium/Hermes/Profile/3";

struct FakeESimProfile {
  std::string profile_path;
  std::string service_path;
  std::string guid;
  std::string iccid;
  ash::policy_util::SmdxActivationCode::Type activation_code_type;
  std::string activation_code_value;
  std::string network_name;
  hermes::profile::State state;
  bool managed = true;
};
struct EuiccTestData {
  int euicc_count = 0;
  std::vector<FakeESimProfile> profiles;
};

const EuiccTestData kEuiccTestData_OneProfile = {
    1,
    {
        {kCellularProfilePath0, kCellularServicePath0, "guid-1", "iccid-1",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "smdp-1", "name-1",
         hermes::profile::State::kActive, true},
    },
};
const EuiccTestData kEuiccTestData_OneProfileWithMissingName = {
    1,
    {
        {kCellularProfilePath0, kCellularServicePath0, "guid-1", "iccid-1",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "smdp-1", "",
         hermes::profile::State::kActive, true},
    },
};
const EuiccTestData kEuiccTestData_OneProfileWithEmptyActivationCode = {
    1,
    {
        {kCellularProfilePath0, kCellularServicePath0, "guid-1", "iccid-1",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "", "name-1",
         hermes::profile::State::kActive, true},
    },
};
const EuiccTestData kEuiccTestData_TwoProfiles = {
    2,
    {
        {kCellularProfilePath0, kCellularServicePath0, "guid-1", "iccid-1",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "smdp-1", "name-1",
         hermes::profile::State::kActive, true},
        {kCellularProfilePath1, kCellularServicePath1, "guid-2", "iccid-2",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "smdp-2", "name-2",
         hermes::profile::State::kInactive, true},
    },
};
const EuiccTestData kEuiccTestData_FourProfiles = {
    3,
    {
        {kCellularProfilePath0, kCellularServicePath0, "guid-1", "iccid-1",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "smdp-1", "name-1",
         hermes::profile::State::kActive, true},
        {kCellularProfilePath1, kCellularServicePath1, "guid-2", "iccid-2",
         ash::policy_util::SmdxActivationCode::Type::SMDP, "smdp-2", "name-2",
         hermes::profile::State::kInactive, true},
        {kCellularProfilePath2, kCellularServicePath2, "guid-3", "iccid-3",
         ash::policy_util::SmdxActivationCode::Type::SMDS, "smds-3", "name-3",
         hermes::profile::State::kActive, true},
        {kCellularProfilePath3, kCellularServicePath3, "guid-4", "iccid-4",
         ash::policy_util::SmdxActivationCode::Type::SMDS, "smds-4", "name-4",
         hermes::profile::State::kInactive, true},
    },
};
const EuiccTestData kEuiccTestData_AfterReset = {4, {}};

std::string GetEid(int euicc_id) {
  return base::StringPrintf("%s%d", kFakeObjectPath, euicc_id);
}

std::string GetEuiccPath(int euicc_id) {
  return base::StringPrintf("%s%d", kFakeEid, euicc_id);
}

}  // namespace

class EuiccStatusUploaderTest : public testing::Test {
 public:

  void SetUp() override {
    helper_ = std::make_unique<ash::NetworkHandlerTestHelper>();

    EuiccStatusUploader::RegisterLocalStatePrefs(local_state_.registry());
    helper_->RegisterPrefs(nullptr, local_state_.registry());
    helper_->InitializePrefs(nullptr, &local_state_);
    SetPolicyClientIsRegistered(/*is_registered=*/true);
  }

  std::unique_ptr<EuiccStatusUploader> CreateStatusUploader(
      bool is_policy_fetched = true) {
    auto status_uploader = base::WrapUnique(new EuiccStatusUploader(
        &cloud_policy_client_, &local_state_,
        base::BindRepeating(&EuiccStatusUploaderTest::is_device_active,
                            base::Unretained(this))));
    if (is_policy_fetched) {
      SetPolicyFetched(status_uploader.get());
    }
    return status_uploader;
  }

  void SetPolicyClientIsRegistered(bool is_registered) {
    cloud_policy_client_.dm_token_ = is_registered ? kDmToken : std::string();
  }

  void SetPolicyFetched(EuiccStatusUploader* status_uploader) {
    status_uploader->OnPolicyFetched(&cloud_policy_client_);
  }

  void SetServerSuccessStatus(bool success) {
    cloud_policy_client_.SetStatus(success);
  }

  void SetPolicyClientHandler(
      FakeCloudPolicyClient::UploadEuiccInfoCallbackHandler handler) {
    cloud_policy_client_.SetHandler(std::move(handler));
  }

  const base::Value& GetStoredPref() {
    return local_state_.GetValue(
        EuiccStatusUploader::kLastUploadedEuiccStatusPref);
  }

  std::string GetStoredPrefString() {
    const base::Value& last_uploaded_pref = GetStoredPref();
    std::string result;
    JSONStringValueSerializer sz(&result);
    sz.Serialize(last_uploaded_pref);
    return result;
  }

  void UpdateUploader(EuiccStatusUploader* status_uploader) {
    (static_cast<ash::NetworkPolicyObserver*>(status_uploader))
        ->PoliciesApplied(/*userhash=*/std::string());
    status_uploader->FireRetryTimerIfExistsForTesting();
  }

  void SetupEuicc(int euicc_id = 0) {
    ash::HermesManagerClient::Get()->GetTestInterface()->AddEuicc(
        dbus::ObjectPath(GetEuiccPath(euicc_id)), GetEid(euicc_id),
        /*is_active=*/true, euicc_id);
  }

  void SetUpDeviceProfiles(const EuiccTestData& data) {

    // Create |data.euicc_count| fake EUICCs.
    ash::HermesManagerClient::Get()->GetTestInterface()->ClearEuiccs();
    for (int euicc_id = 0; euicc_id < data.euicc_count; euicc_id++) {
      SetupEuicc(euicc_id);
    }

    for (const auto& test_profile : data.profiles) {
      ash::HermesEuiccClient::Get()->GetTestInterface()->AddCarrierProfile(
          dbus::ObjectPath(test_profile.profile_path),
          dbus::ObjectPath(GetEuiccPath(/*euicc_id=*/0)), test_profile.iccid,
          test_profile.guid, "nickname", "service_provider",
          test_profile.activation_code_value, test_profile.service_path,
          test_profile.state, hermes::profile::ProfileClass::kOperational,
          ash::HermesEuiccClient::TestInterface::AddCarrierProfileBehavior::
              kAddProfileWithService);

      if (test_profile.managed) {
        // We set the prefs directly so that we can force situations that
        // otherwise would not be acceptable by the API of
        // ManagedCellularPrefHandler, e.g. an empty name or activation code
        // value, that we want to protect against uploading.
        base::Value::Dict esim_metadata;
        esim_metadata.Set(::onc::network_config::kName,
                          test_profile.network_name);
        esim_metadata.Set(
            test_profile.activation_code_type ==
                    ash::policy_util::SmdxActivationCode::Type::SMDP
                ? ::onc::cellular::kSMDPAddress
                : ::onc::cellular::kSMDSAddress,
            test_profile.activation_code_value);

        base::Value::Dict existing_prefs =
            local_state_.GetDict(ash::prefs::kManagedCellularESimMetadata)
                .Clone();
        existing_prefs.Set(test_profile.iccid, std::move(esim_metadata));
        local_state_.Set(ash::prefs::kManagedCellularESimMetadata,
                         base::Value(std::move(existing_prefs)));
      }
    }

    // Wait for Shill device and service change notifications to propagate.
    base::RunLoop().RunUntilIdle();
  }

  void ValidateUploadedStatus(const std::string& expected_status_str,
                              bool clear_profile_list) {
    base::Value expected_status = base::test::ParseJson(expected_status_str);
    EXPECT_EQ(expected_status, GetStoredPref());
    EXPECT_TRUE(cloud_policy_client_.GetLastRequest());
    EXPECT_TRUE(
        RequestsAreEqual(*EuiccStatusUploader::ConstructRequestFromStatus(
                             expected_status.GetDict(), clear_profile_list),
                         *cloud_policy_client_.GetLastRequest()));
  }

  void SetLastUploadedValue(const std::string& last_value) {
    local_state_.Set(EuiccStatusUploader::kLastUploadedEuiccStatusPref,
                     base::test::ParseJson(last_value));
  }

  void TriggerManagedCellularPrefChanged(EuiccStatusUploader* status_uploader) {
    static_cast<ash::ManagedCellularPrefHandler::Observer*>(status_uploader)
        ->OnManagedCellularPrefChanged();
  }

  void ExecuteResetCommand(EuiccStatusUploader* status_uploader) {
    SetUpDeviceProfiles(kEuiccTestData_AfterReset);

    // TODO(crbug.com/40205133): Make FakeHermesEuiccClient trigger OnEuiccReset
    // directly.
    static_cast<ash::HermesEuiccClient::Observer*>(status_uploader)
        ->OnEuiccReset(dbus::ObjectPath());
  }

  int GetRequestCount() { return cloud_policy_client_.num_requests(); }

  enterprise_management::UploadEuiccInfoRequest* GetLastRequest() {
    return cloud_policy_client_.GetLastRequest();
  }

  void CheckHistogram(int total_count, int success_count, int failed_count) {
    histogram_tester_.ExpectTotalCount(kEuiccStatusUploadResultHistogram,
                                       total_count);
    histogram_tester_.ExpectBucketCount(kEuiccStatusUploadResultHistogram, true,
                                        /*expected_count=*/success_count);
    histogram_tester_.ExpectBucketCount(kEuiccStatusUploadResultHistogram,
                                        false,
                                        /*expected_count=*/failed_count);
  }

  void SetIsDeviceActive(bool value) { is_device_active_ = value; }

 private:
  bool is_device_active() { return is_device_active_; }

  bool is_device_active_ = true;
  content::BrowserTaskEnvironment task_environment_;
  FakeCloudPolicyClient cloud_policy_client_;
  TestingPrefServiceSimple local_state_;
  std::unique_ptr<ash::NetworkHandlerTestHelper> helper_;
  base::HistogramTester histogram_tester_;
};

TEST_F(EuiccStatusUploaderTest, EmptySetup) {
  auto status_uploader = CreateStatusUploader();
  EXPECT_EQ(GetRequestCount(), 0);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  // Verify that no status is uploaded if there is no EUICC.
  EXPECT_EQ(GetRequestCount(), 0);
  CheckHistogram(/*total_count=*/0, /*success_count=*/0, /*failed_count=*/0);
}

TEST_F(EuiccStatusUploaderTest, InactiveDevice) {
  SetIsDeviceActive(false);
  auto status_uploader = CreateStatusUploader();
  EXPECT_EQ(GetRequestCount(), 0);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  // Verify that no status is uploaded if the device is inactive.
  EXPECT_EQ(GetRequestCount(), 0);
  CheckHistogram(/*total_count=*/0, /*success_count=*/0, /*failed_count=*/0);
}

TEST_F(EuiccStatusUploaderTest, ClientNotRegistered) {
  SetupEuicc();
  base::RunLoop().RunUntilIdle();
  SetPolicyClientIsRegistered(/*is_registered=*/false);

  auto status_uploader = CreateStatusUploader();
  EXPECT_EQ(GetRequestCount(), 0);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());

  UpdateUploader(status_uploader.get());
  // Verify that no requests are made if client is not registered.
  EXPECT_EQ(GetRequestCount(), 0);
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/0, /*success_count=*/0, /*failed_count=*/0);
}

TEST_F(EuiccStatusUploaderTest, ServerError) {
  SetupEuicc();
  base::RunLoop().RunUntilIdle();
  auto status_uploader = CreateStatusUploader();
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);
  // Nothing is stored when requests fail.
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/2, /*success_count=*/0, /*failed_count=*/2);
}

TEST_F(EuiccStatusUploaderTest, WaitForPolicyFetch) {
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);

  auto status_uploader = CreateStatusUploader(/*is_policy_fetched=*/false);
  EXPECT_EQ(GetRequestCount(), 0);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());

  // Verify that no requests are made when policy has not been fetched.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 0);

  // Verify that status is uploaded correctly when policy is fetched.
  SetPolicyFetched(status_uploader.get());
  ValidateUploadedStatus(kEuiccStatus_OneProfile,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/1, /*success_count=*/1, /*failed_count=*/0);
}

TEST_F(EuiccStatusUploaderTest, Basic) {
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);

  auto status_uploader = CreateStatusUploader();
  // Initial upload request.
  EXPECT_EQ(GetRequestCount(), 1);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/1, /*success_count=*/0, /*failed_count=*/1);

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);
  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_OneProfile,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/2, /*success_count=*/1, /*failed_count=*/1);
}

TEST_F(EuiccStatusUploaderTest, BasicWithMissingName) {
  SetUpDeviceProfiles(kEuiccTestData_OneProfileWithMissingName);

  auto status_uploader = CreateStatusUploader();
  // Initial upload request.
  EXPECT_EQ(GetRequestCount(), 1);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/1, /*success_count=*/0, /*failed_count=*/1);

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);
  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_OneProfileWithMissingName,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/2, /*success_count=*/1, /*failed_count=*/1);
}

TEST_F(EuiccStatusUploaderTest, BasicWithEmptyActivationCode) {
  SetUpDeviceProfiles(kEuiccTestData_OneProfileWithEmptyActivationCode);

  auto status_uploader = CreateStatusUploader();
  // Initial upload request.
  EXPECT_EQ(GetRequestCount(), 1);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/1, /*success_count=*/0, /*failed_count=*/1);

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);
  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_OneProfileWithEmptyActivationCode,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/2, /*success_count=*/1, /*failed_count=*/1);
}

TEST_F(EuiccStatusUploaderTest, TwoProfiles) {
  SetUpDeviceProfiles(kEuiccTestData_TwoProfiles);

  auto status_uploader = CreateStatusUploader();
  // Initial upload request.
  EXPECT_EQ(GetRequestCount(), 1);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/1, /*success_count=*/0, /*failed_count=*/1);

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);

  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_TwoProfiles,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/2, /*success_count=*/1, /*failed_count=*/1);
}

TEST_F(EuiccStatusUploaderTest, FourProfilesWithSmds) {
  SetUpDeviceProfiles(kEuiccTestData_FourProfiles);

  auto status_uploader = CreateStatusUploader();
  // Initial upload request.
  EXPECT_EQ(GetRequestCount(), 1);
  // No value is uploaded yet.
  EXPECT_EQ("{}", GetStoredPrefString());
  CheckHistogram(/*total_count=*/1, /*success_count=*/0, /*failed_count=*/1);

  // Make server accept requests.
  SetServerSuccessStatus(true);
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);

  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_FourProfiles,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/2, /*success_count=*/1, /*failed_count=*/1);
}

TEST_F(EuiccStatusUploaderTest, SameValueAsBefore) {
  // Make server accept requests.
  SetServerSuccessStatus(true);
  // Mark the current state as already uploaded.
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);
  SetLastUploadedValue(kEuiccStatus_OneProfile);

  auto status_uploader = CreateStatusUploader();
  // No value is uploaded since it has been previously sent.
  EXPECT_EQ(GetRequestCount(), 0);
  CheckHistogram(/*total_count=*/0, /*success_count=*/0, /*failed_count=*/0);
}

TEST_F(EuiccStatusUploaderTest, NewValue) {
  // Make server accept requests.
  SetServerSuccessStatus(true);
  // Set up a value different from one that was previously uploaded.
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);
  SetLastUploadedValue(kEuiccStatus_Empty);

  auto status_uploader = CreateStatusUploader();
  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_OneProfile,
                         /*clear_profile_list=*/false);
  CheckHistogram(/*total_count=*/1, /*success_count=*/1, /*failed_count=*/0);
}

TEST_F(EuiccStatusUploaderTest, ResetRequest) {
  // Make server accept requests.
  SetServerSuccessStatus(true);
  // Set up a value different from one that was previously uploaded.
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);
  SetLastUploadedValue(kEuiccStatus_Empty);

  auto status_uploader = CreateStatusUploader();
  // Verify that last uploaded configuration is stored.
  ValidateUploadedStatus(kEuiccStatus_OneProfile,
                         /*clear_profile_list=*/false);

  // Reset remote command was received and executed.
  ExecuteResetCommand(status_uploader.get());
  // Request has been sent.
  EXPECT_EQ(GetRequestCount(), 3);

  ValidateUploadedStatus(kEuiccStatus_AfterReset,
                         /*clear_profile_list=*/true);

  // Send the reset command again.
  ExecuteResetCommand(status_uploader.get());
  // Request will be force-sent again because we've received a reset command..
  EXPECT_EQ(GetRequestCount(), 4);

  ValidateUploadedStatus(kEuiccStatus_AfterReset,
                         /*clear_profile_list=*/true);
}

TEST_F(EuiccStatusUploaderTest, ClearProfileListRaceCondition) {
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);

  // Create the status uploader but do not trigger an upload via the policies
  // being fetched.
  auto status_uploader = CreateStatusUploader(/*is_policy_fetched=*/false);
  EXPECT_EQ(GetRequestCount(), 0);

  // Capture the callback that the status uploader provides to the cloud policy
  // client to allow us to block the completion of the first upload.
  base::OnceCallback<void(bool)> callback;
  SetPolicyClientHandler(base::BindRepeating(
      [](base::OnceCallback<void(bool)>* callback_out,
         base::OnceCallback<void(bool)> callback_in) {
        *callback_out = std::move(callback_in);
      },
      &callback));

  // Trigger the first upload.
  SetPolicyFetched(status_uploader.get());

  EXPECT_FALSE(GetLastRequest()->clear_profile_list());
  EXPECT_EQ(GetRequestCount(), 1);

  // Simulate a race condition where an upload is in progress and an EUICC reset
  // causes the "clear profile list" pref to be set to |true|. This pref should
  // not be cleared when the first upload finishes. This won't affect the value
  // of |callback| since an ongoing upload is blocking.
  ExecuteResetCommand(status_uploader.get());

  // Complete the first status upload. When this completes the status uploader
  // class will check if it should perform another upload; this class will
  // identify that the EUICC was reset and will trigger a second upload.
  ASSERT_TRUE(callback);
  std::move(callback).Run(true);

  EXPECT_TRUE(GetLastRequest()->clear_profile_list());
  EXPECT_EQ(GetRequestCount(), 2);
}

TEST_F(EuiccStatusUploaderTest, UnexpectedNetworkHandlerShutdown) {
  SetUpDeviceProfiles(kEuiccTestData_OneProfile);
  // NetworkHandler has not been initialized.
  auto status_uploader = CreateStatusUploader();

  // Initial Request
  EXPECT_EQ(GetRequestCount(), 1);

  // Requests made normally.
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);

  // NetworkHandler::Shutdown() has already been called before
  // EuiccStatusUploader is deleted
  ash::NetworkHandler::Shutdown();

  // No requests made as NetworkHandler is not available.
  UpdateUploader(status_uploader.get());
  EXPECT_EQ(GetRequestCount(), 2);

  // Need to reinitialize before exiting test.
  ash::NetworkHandler::Initialize();
}

}  // namespace policy