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

#include "chrome/browser/ash/bruschetta/bruschetta_installer_impl.h"

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "base/system/sys_info.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_amount_of_physical_memory_override.h"
#include "base/values.h"
#include "chrome/browser/ash/bruschetta/bruschetta_download.h"
#include "chrome/browser/ash/bruschetta/bruschetta_installer.h"
#include "chrome/browser/ash/bruschetta/bruschetta_pref_names.h"
#include "chrome/browser/ash/bruschetta/bruschetta_service.h"
#include "chrome/browser/ash/bruschetta/bruschetta_service_factory.h"
#include "chrome/browser/ash/guest_os/dbus_test_helper.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/dbus/attestation/attestation_client.h"
#include "chromeos/ash/components/dbus/concierge/fake_concierge_client.h"
#include "chromeos/ash/components/dbus/dlcservice/dlcservice.pb.h"
#include "chromeos/ash/components/dbus/dlcservice/fake_dlcservice_client.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/disks/mock_disk_mount_manager.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace bruschetta {

// Defined in bruschetta_installer_impl.cc
extern const char kInstallResultMetric[];

namespace {

using testing::_;
using testing::AnyNumber;
using testing::DoAll;
using testing::InvokeWithoutArgs;
using testing::Sequence;

// Total number of stopping points in ::ExpectStopOnStepN
constexpr int kMaxSteps = 26;

// Total number of stopping points in ::ExpectStopOnStepN when we don't install
// a pflash file.
constexpr int kMaxStepsNoPflash = kMaxSteps - 7;

const char kVmName[] = "vm-name";
const char kVmConfigId[] = "test-config-id";
const char kVmConfigName[] = "test vm config";
const char kVmConfigUrl[] = "https://example.com/";
const char kVmConfigHash[] =
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
const char kBadHash[] =
    "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210";

class MockObserver : public BruschettaInstaller::Observer {
 public:
  MOCK_METHOD(void,
              StateChanged,
              (BruschettaInstaller::State state),
              (override));
  MOCK_METHOD(void, Error, (BruschettaInstallResult), (override));
};

class StubDownload : public BruschettaDownload {
 public:
  StubDownload(base::FilePath path, std::string hash)
      : path_(std::move(path)), hash_(std::move(hash)) {}
  ~StubDownload() override = default;
  void StartDownload(
      Profile* profile,
      GURL url,
      base::OnceCallback<void(base::FilePath, std::string)> callback) override {
    std::move(callback).Run(path_, hash_);
  }
  base::FilePath path_;
  std::string hash_;
};

class BruschettaInstallerTest : public testing::TestWithParam<int>,
                                protected guest_os::FakeVmServicesHelper {
 public:
  BruschettaInstallerTest() : fake_20gb_memory(20ULL * 1024 * 1024) {}
  BruschettaInstallerTest(const BruschettaInstallerTest&) = delete;
  BruschettaInstallerTest& operator=(const BruschettaInstallerTest&) = delete;
  ~BruschettaInstallerTest() override = default;

 protected:
  void BuildPrefValues() {
    base::Value::Dict vtpm;
    vtpm.Set(prefs::kPolicyVTPMEnabledKey, true);
    vtpm.Set(prefs::kPolicyVTPMUpdateActionKey,
             static_cast<int>(
                 prefs::PolicyUpdateAction::FORCE_SHUTDOWN_IF_MORE_RESTRICTED));
    base::Value::Dict image;
    image.Set(prefs::kPolicyURLKey, kVmConfigUrl);
    image.Set(prefs::kPolicyHashKey, kVmConfigHash);
    base::Value::List oem_strings;
    oem_strings.Append("OEM string");

    base::Value::Dict config;

    config.Set(prefs::kPolicyEnabledKey,
               static_cast<int>(prefs::PolicyEnabledState::RUN_ALLOWED));
    config.Set(prefs::kPolicyNameKey, kVmConfigName);
    config.Set(prefs::kPolicyVTPMKey, vtpm.Clone());
    config.Set(prefs::kPolicyOEMStringsKey, oem_strings.Clone());
    prefs_not_installable_.Set(kVmConfigId, config.Clone());

    config.Set(prefs::kPolicyEnabledKey,
               static_cast<int>(prefs::PolicyEnabledState::INSTALL_ALLOWED));
    config.Set(prefs::kPolicyImageKey, image.Clone());
    prefs_installable_no_pflash_.Set(kVmConfigId, config.Clone());

    config.Set(prefs::kPolicyPflashKey, image.Clone());
    prefs_installable_.Set(kVmConfigId, config.Clone());
  }

  void SetUp() override {
    ash::AttestationClient::InitializeFake();
    ash::system::StatisticsProvider::SetTestProvider(
        &fake_statistics_provider_);
    fake_statistics_provider_.SetMachineStatistic(
        ash::system::kAttestedDeviceIdKey, "my:cool:ADID");

    BuildPrefValues();

    ASSERT_TRUE(base::CreateDirectory(
        profile_.GetPath().Append("MyFiles").Append("Downloads")));

    ash::disks::DiskMountManager::InitializeForTesting(&*disk_mount_manager_);

    installer_ = std::make_unique<BruschettaInstallerImpl>(
        &profile_, *local_state_.Get(),
        base::BindOnce(&BruschettaInstallerTest::CloseCallback,
                       base::Unretained(this)));

    installer_->AddObserver(&observer_);
    ConfigureDownloadFactory(base::FilePath(), "");
  }

  // Configures the Bruschetta installer to use a fake downloader which
  // immediately completes returning |path| and |hash|.
  void ConfigureDownloadFactory(base::FilePath path, std::string hash) {
    installer_->SetDownloadFactoryForTesting(base::BindLambdaForTesting(
        [path = std::move(path), hash = std::move(hash)]() {
          std::unique_ptr<BruschettaDownload> d =
              std::make_unique<StubDownload>(std::move(path), std::move(hash));
          return d;
        }));
  }

  void TearDown() override {
    CheckVmRegistration();
    ash::disks::DiskMountManager::Shutdown();
    ash::AttestationClient::Shutdown();
  }

  void CheckVmRegistration() {
    const auto& running_vms = BruschettaServiceFactory::GetForProfile(&profile_)
                                  ->GetRunningVmsForTesting();
    if (expect_vm_registered_) {
      auto it = running_vms.find(kVmName);
      EXPECT_NE(it, running_vms.end());
      EXPECT_TRUE(it->second.vtpm_enabled);
    } else {
      EXPECT_FALSE(running_vms.contains(kVmName));
    }
  }

  // All these methods return anonymous lambdas because gmock doesn't accept
  // base::OnceCallbacks as callable objects in expectations.
  auto CancelCallback() {
    return [this]() { installer_->Cancel(); };
  }

  auto StopCallback() {
    return [this]() { run_loop_.Quit(); };
  }

  auto PrefsCallback(const base::Value::Dict& value) {
    return [this, &value]() {
      profile_.GetPrefs()->SetDict(prefs::kBruschettaVMConfiguration,
                                   value.Clone());
    };
  }

  auto DlcCallback(std::string error) {
    return
        [this, error]() { FakeDlcserviceClient()->set_install_error(error); };
  }

  auto DownloadErrorCallback(bool fail_at_start) {
    return [this]() { ConfigureDownloadFactory(base::FilePath(), ""); };
  }

  auto DownloadBadHashCallback() {
    return [this]() {
      base::FilePath path;
      base::CreateTemporaryFile(&path);
      ConfigureDownloadFactory(path, kBadHash);
    };
  }

  auto DownloadSuccessCallback() {
    return [this]() {
      base::FilePath path;
      base::CreateTemporaryFile(&path);
      ConfigureDownloadFactory(path, kVmConfigHash);
    };
  }

  auto DiskImageCallback(
      std::optional<vm_tools::concierge::DiskImageStatus> value) {
    return [this, value]() {
      if (value.has_value()) {
        vm_tools::concierge::CreateDiskImageResponse response;
        response.set_status(*value);
        FakeConciergeClient()->set_create_disk_image_response(
            std::move(response));
      } else {
        FakeConciergeClient()->set_create_disk_image_response(std::nullopt);
      }
    };
  }

  auto InstallPflashCallback(std::optional<bool> success) {
    return [this, success]() {
      if (success.has_value()) {
        vm_tools::concierge::SuccessFailureResponse response;
        response.set_success(*success);
        FakeConciergeClient()->set_install_pflash_response(std::move(response));
      } else {
        FakeConciergeClient()->set_install_pflash_response(std::nullopt);
      }
    };
  }

  auto ClearVekCallback(bool success) {
    return [success]() {
      ash::AttestationClient::Get()->GetTestInterface()->set_delete_keys_status(
          success ? attestation::STATUS_SUCCESS
                  : attestation::STATUS_INVALID_PARAMETER);
    };
  }

  auto StartVmCallback(std::optional<bool> success) {
    return [this, success]() {
      if (success.has_value()) {
        vm_tools::concierge::StartVmResponse response;
        response.set_success(*success);
        FakeConciergeClient()->set_start_vm_response(std::move(response));
        this->expect_vm_registered_ = *success;
      } else {
        FakeConciergeClient()->set_start_vm_response(std::nullopt);
      }
    };
  }

  void ErrorExpectation(Sequence seq) {
    EXPECT_CALL(observer_, Error)
        .Times(1)
        .InSequence(seq)
        .WillOnce(StopCallback());
  }

  template <typename T, typename F>
  void MakeErrorPoint(T& expectation, Sequence seq, F callback) {
    expectation.WillOnce(InvokeWithoutArgs(callback));
    ErrorExpectation(seq);
  }

  template <typename T, typename F, typename G>
  void MakeErrorPoint(T& expectation, Sequence seq, F callback1, G callback2) {
    expectation.WillOnce(
        DoAll(InvokeWithoutArgs(callback1), InvokeWithoutArgs(callback2)));
    ErrorExpectation(seq);
  }

  // Generate expectations and actions for a test that runs the install and
  // stops at the nth point where stopping is possible, returning true if the
  // stop is due to an error and false if the stop is a cancel. Passing in
  // kMaxSteps means letting the install run to completion. If out_result is
  // passed in, will set it to the expected result (as reported to the observer
  // + metrics).
  //
  // Takes an optional Sequence to order these expectations before/after other
  // things.
  bool ExpectStopOnStepN(int n,
                         Sequence seq = {},
                         BruschettaInstallResult* out_result = nullptr,
                         bool use_pflash = true) {
    // Policy check step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kInstallationProhibited;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kInstallStarted))
              .Times(1)
              .InSequence(seq);
      if (!n--) {
        MakeErrorPoint(expectation, seq, PrefsCallback(prefs_not_installable_));
        return true;
      }

      if (use_pflash) {
        expectation.WillOnce(
            InvokeWithoutArgs(PrefsCallback(prefs_installable_)));
      } else {
        expectation.WillOnce(
            InvokeWithoutArgs(PrefsCallback(prefs_installable_no_pflash_)));
      }
    }

    // Tools DLC install step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kToolsDlcUnknownError;
      }
      auto& expectation =
          EXPECT_CALL(
              observer_,
              StateChanged(BruschettaInstaller::State::kToolsDlcInstall))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, DlcCallback("Install Error"));
        return true;
      }

      expectation.WillOnce(
          InvokeWithoutArgs(DlcCallback(dlcservice::kErrorNone)));
    }

    // UEFI DLC install step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kFirmwareDlcUnknownError;
      }
      auto& expectation =
          EXPECT_CALL(
              observer_,
              StateChanged(BruschettaInstaller::State::kFirmwareDlcInstall))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, DlcCallback("Install Error"));
        return true;
      }

      expectation.WillOnce(
          InvokeWithoutArgs(DlcCallback(dlcservice::kErrorNone)));
    }

    // Boot disk download step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kDownloadError;
      }
      auto& expectation =
          EXPECT_CALL(
              observer_,
              StateChanged(BruschettaInstaller::State::kBootDiskDownload))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, DownloadErrorCallback(true));
        return true;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, DownloadErrorCallback(false));
        return true;
      }
      if (out_result) {
        *out_result = BruschettaInstallResult::kInvalidBootDisk;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, DownloadBadHashCallback());
        return true;
      }

      expectation.WillOnce(InvokeWithoutArgs(DownloadSuccessCallback()));
    }

    // pflash download step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kDownloadError;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kPflashDownload))
              .Times(1)
              .InSequence(seq);

      if (use_pflash) {
        if (!n--) {
          expectation.WillOnce(CancelCallback());
          return false;
        }
        if (!n--) {
          MakeErrorPoint(expectation, seq, DownloadErrorCallback(true));
          return true;
        }
        if (!n--) {
          MakeErrorPoint(expectation, seq, DownloadErrorCallback(false));
          return true;
        }
        if (out_result) {
          *out_result = BruschettaInstallResult::kInvalidPflash;
        }
        if (!n--) {
          MakeErrorPoint(expectation, seq, DownloadBadHashCallback());
          return true;
        }

        expectation.WillOnce(InvokeWithoutArgs(DownloadSuccessCallback()));
      }
    }

    // Open files step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kUnableToOpenImages;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kOpenFiles))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
    }

    // Create VM disk step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kCreateDiskError;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kCreateVmDisk))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, DiskImageCallback(std::nullopt));
        return true;
      }
      if (!n--) {
        MakeErrorPoint(
            expectation, seq,
            DiskImageCallback(
                vm_tools::concierge::DiskImageStatus::DISK_STATUS_FAILED));
        return true;
      }

      expectation.WillOnce(InvokeWithoutArgs(DiskImageCallback(
          vm_tools::concierge::DiskImageStatus::DISK_STATUS_CREATED)));
    }

    // Install pflash file step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kInstallPflashError;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kInstallPflash))
              .Times(1)
              .InSequence(seq);

      if (use_pflash) {
        if (!n--) {
          expectation.WillOnce(CancelCallback());
          return false;
        }
        if (!n--) {
          MakeErrorPoint(expectation, seq, InstallPflashCallback(std::nullopt));
          return true;
        }
        if (!n--) {
          MakeErrorPoint(expectation, seq, InstallPflashCallback(false));
          return true;
        }

        expectation.WillOnce(InvokeWithoutArgs(InstallPflashCallback(true)));
      }
    }

    // Clear vEK step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kClearVekFailed;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kClearVek))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, ClearVekCallback(false));
        return true;
      }

      expectation.WillOnce(InvokeWithoutArgs(ClearVekCallback(true)));
    }

    // Start VM step
    {
      if (out_result) {
        *out_result = BruschettaInstallResult::kInstallationProhibited;
      }
      auto& expectation =
          EXPECT_CALL(observer_,
                      StateChanged(BruschettaInstaller::State::kStartVm))
              .Times(1)
              .InSequence(seq);

      if (!n--) {
        expectation.WillOnce(CancelCallback());
        return false;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, PrefsCallback(prefs_not_installable_));
        return true;
      }
      if (out_result) {
        *out_result = BruschettaInstallResult::kStartVmFailed;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, StartVmCallback(std::nullopt));
        return true;
      }
      if (!n--) {
        MakeErrorPoint(expectation, seq, StartVmCallback(false));
        return true;
      }

      expectation.WillOnce(InvokeWithoutArgs(StartVmCallback(true)));
    }

    // Open terminal step
    EXPECT_CALL(observer_,
                StateChanged(BruschettaInstaller::State::kLaunchTerminal))
        .Times(1)
        .InSequence(seq);
    // Dialog closes after this without further action from us

    // Make sure all input steps other then kMaxSteps got handled earlier.
    if (n != 0) {
      ADD_FAILURE() << "Steps input is too high, try setting kMaxSteps to "
                    << kMaxSteps - n;
    }

    // Like a cancellation, finishing the install closes the installer.
    return false;
  }

  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  ScopedTestingLocalState local_state_{TestingBrowserProcess::GetGlobal()};

  base::RunLoop run_loop_, run_loop_2_;

  base::Value::Dict prefs_installable_no_pflash_, prefs_installable_,
      prefs_not_installable_;

  TestingProfile profile_;
  std::unique_ptr<BruschettaInstallerImpl> installer_;

  MockObserver observer_;
  // Pointer owned by DiskMountManager
  const raw_ref<ash::disks::MockDiskMountManager, DanglingUntriaged>
      disk_mount_manager_{*new ash::disks::MockDiskMountManager};

  bool destroy_installer_on_completion_ = true;
  base::HistogramTester histogram_tester_;

  bool expect_vm_registered_ = false;
  ash::system::FakeStatisticsProvider fake_statistics_provider_;
  base::test::ScopedAmountOfPhysicalMemoryOverride fake_20gb_memory;

 private:
  // Called when the installer exists, suitable for base::BindOnce.
  void CloseCallback() {
    if (destroy_installer_on_completion_) {
      // Delete the installer object after it requests closure so we can tell if
      // it does anything afterwards that assumes it hasn't been deleted yet.
      installer_.reset();
    }
    run_loop_.Quit();
    run_loop_2_.Quit();
  }
};

TEST_F(BruschettaInstallerTest, CloseOnPrompt) {
  installer_->Cancel();
  run_loop_.Run();

  EXPECT_FALSE(installer_);
}

TEST_F(BruschettaInstallerTest, InstallSuccess) {
  ExpectStopOnStepN(kMaxSteps);

  installer_->Install(kVmName, kVmConfigId);
  run_loop_.Run();

  histogram_tester_.ExpectBucketCount(kInstallResultMetric,
                                      BruschettaInstallResult::kSuccess, 1);
  EXPECT_FALSE(installer_);
}

TEST_F(BruschettaInstallerTest, InstallSuccessNoPflash) {
  ExpectStopOnStepN(kMaxStepsNoPflash, {}, nullptr, false);

  installer_->Install(kVmName, kVmConfigId);
  run_loop_.Run();

  histogram_tester_.ExpectBucketCount(kInstallResultMetric,
                                      BruschettaInstallResult::kSuccess, 1);
  EXPECT_FALSE(installer_);
}

TEST_F(BruschettaInstallerTest, TwoInstalls) {
  ExpectStopOnStepN(kMaxSteps);

  installer_->Install(kVmName, kVmConfigId);
  installer_->Install(kVmName, kVmConfigId);
  run_loop_.Run();

  EXPECT_FALSE(installer_);
}

TEST_F(BruschettaInstallerTest, MultipleCancelsNoOp) {
  destroy_installer_on_completion_ = false;
  // Should be safe to call cancel multiple times.
  installer_->Cancel();
  installer_->Cancel();
  run_loop_.Run();
  installer_->Cancel();
  installer_->Cancel();
  run_loop_2_.Run();
}

TEST_P(BruschettaInstallerTest, StopDuringInstall) {
  BruschettaInstallResult expected_result;
  bool is_error = ExpectStopOnStepN(GetParam(), {}, &expected_result);

  installer_->Install(kVmName, kVmConfigId);
  run_loop_.Run();

  if (is_error) {
    // Installer should remain open in error state, tell it to close.
    EXPECT_TRUE(installer_);
    installer_->Cancel();
    run_loop_2_.Run();

    histogram_tester_.ExpectBucketCount(kInstallResultMetric, expected_result,
                                        1);
  }
  EXPECT_FALSE(installer_);
}

TEST_P(BruschettaInstallerTest, StopDuringInstallNoPflash) {
  if (GetParam() > kMaxStepsNoPflash) {
    GTEST_SKIP();
  }

  BruschettaInstallResult expected_result;
  bool is_error = ExpectStopOnStepN(GetParam(), {}, &expected_result, false);

  installer_->Install(kVmName, kVmConfigId);
  run_loop_.Run();

  if (is_error) {
    // Installer should remain open in error state, tell it to close.
    EXPECT_TRUE(installer_);
    installer_->Cancel();
    run_loop_2_.Run();

    histogram_tester_.ExpectBucketCount(kInstallResultMetric, expected_result,
                                        1);
  }
  EXPECT_FALSE(installer_);
}

TEST_P(BruschettaInstallerTest, ErrorAndRetry) {
  Sequence seq;

  bool is_error = ExpectStopOnStepN(GetParam(), seq);
  if (is_error) {
    ExpectStopOnStepN(kMaxSteps, seq);
  }

  installer_->Install(kVmName, kVmConfigId);
  run_loop_.Run();

  if (!is_error) {
    return;
  }

  // Installer should remain open in error state, retry the install.
  EXPECT_TRUE(installer_);

  installer_->Install(kVmName, kVmConfigId);
  run_loop_2_.Run();

  EXPECT_FALSE(installer_);
}

INSTANTIATE_TEST_SUITE_P(All,
                         BruschettaInstallerTest,
                         testing::Range(0, kMaxSteps));

TEST_F(BruschettaInstallerTest, AllStepsTested) {
  // Meta-test to check that kMaxSteps is set correctly.

  // We generate a lot of gmock expectations just to ignore them, and taking
  // stack traces for all of them is really slow, so disable stack traces for
  // this test.
  GTEST_FLAG_SET(stack_trace_depth, 0);

  std::optional<int> new_max_steps;

  for (int i = 0; i < 1000; i++) {
    testing::TestPartResultArray failures;
    testing::ScopedFakeTestPartResultReporter test_reporter{
        testing::ScopedFakeTestPartResultReporter::
            INTERCEPT_ONLY_CURRENT_THREAD,
        &failures};

    ExpectStopOnStepN(i);

    if (failures.size() > 0) {
      new_max_steps = i - 1;
      break;
    }
  }
  if (new_max_steps.has_value()) {
    if (*new_max_steps != kMaxSteps) {
      ADD_FAILURE() << "kMaxSteps needs to be updated. Try setting it to "
                    << *new_max_steps;
    }
  } else {
    ADD_FAILURE() << "Was unable to find the correct value for kMaxSteps";
  }

  // Force the expectations we just set to be checked here so we can ignore all
  // the failures.
  {
    testing::TestPartResultArray failures;
    testing::ScopedFakeTestPartResultReporter test_reporter{
        testing::ScopedFakeTestPartResultReporter::
            INTERCEPT_ONLY_CURRENT_THREAD,
        &failures};

    testing::Mock::VerifyAndClearExpectations(&observer_);
    testing::Mock::VerifyAndClearExpectations(&*disk_mount_manager_);
  }
}

}  // namespace

}  // namespace bruschetta