summaryrefslogtreecommitdiffstats
path: root/toolkit/xre/test/win/TestLauncherRegistryInfo.cpp
blob: ab5edcf1ca23b294d7b7affd02ef0e37c198c69a (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#define MOZ_USE_LAUNCHER_ERROR

#include "mozilla/LauncherRegistryInfo.h"
#include "mozilla/NativeNt.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Unused.h"
#include "nsWindowsHelpers.h"

#include "LauncherRegistryInfo.cpp"

#include <string>

static const char kMsgStart[] = "TEST-FAILED | LauncherRegistryInfo | ";

static const wchar_t kRegKeyPath[] = L"SOFTWARE\\" EXPAND_STRING_MACRO(
    MOZ_APP_VENDOR) L"\\" EXPAND_STRING_MACRO(MOZ_APP_BASENAME) L"\\Launcher";
static const wchar_t kBrowserSuffix[] = L"|Browser";
static const wchar_t kLauncherSuffix[] = L"|Launcher";
static const wchar_t kImageSuffix[] = L"|Image";
static const wchar_t kTelemetrySuffix[] = L"|Telemetry";

static std::wstring gBrowserValue;
static std::wstring gLauncherValue;
static std::wstring gImageValue;
static std::wstring gTelemetryValue;

static DWORD gMyImageTimestamp;

#define RUN_TEST(result, fn)                                                 \
  if ((result = fn()).isErr()) {                                             \
    const mozilla::LauncherError& err = result.inspectErr();                 \
    printf("%s%s | %08lx (%s:%d)\n", kMsgStart, #fn, err.mError.AsHResult(), \
           err.mFile, err.mLine);                                            \
    return 1;                                                                \
  }

#define EXPECT_COMMIT_IS_OK()                        \
  do {                                               \
    mozilla::LauncherVoidResult vr2 = info.Commit(); \
    if (vr2.isErr()) {                               \
      return vr2;                                    \
    }                                                \
  } while (0)

#define EXPECT_CHECK_RESULT_IS(desired, expected)                       \
  do {                                                                  \
    mozilla::LauncherResult<mozilla::LauncherRegistryInfo::ProcessType> \
        result = info.Check(mozilla::LauncherRegistryInfo::desired);    \
    if (result.isErr()) {                                               \
      return result.propagateErr();                                     \
    }                                                                   \
    if (result.unwrap() != mozilla::LauncherRegistryInfo::expected) {   \
      return LAUNCHER_ERROR_FROM_HRESULT(E_FAIL);                       \
    }                                                                   \
  } while (0)

#define EXPECT_ENABLED_STATE_IS(expected)                                \
  do {                                                                   \
    mozilla::LauncherResult<mozilla::LauncherRegistryInfo::EnabledState> \
        enabled = info.IsEnabled();                                      \
    if (enabled.isErr()) {                                               \
      return enabled.propagateErr();                                     \
    }                                                                    \
    if (enabled.unwrap() != mozilla::LauncherRegistryInfo::expected) {   \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);                  \
    }                                                                    \
  } while (0)

#define EXPECT_TELEMETRY_IS_ENABLED(expected)                          \
  do {                                                                 \
    mozilla::LauncherResult<bool> enabled = info.IsTelemetryEnabled(); \
    if (enabled.isErr()) {                                             \
      return enabled.propagateErr();                                   \
    }                                                                  \
    if (enabled.unwrap() != expected) {                                \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);                \
    }                                                                  \
  } while (0)

#define EXPECT_REG_DWORD_EXISTS_AND_EQ(name, expected)      \
  do {                                                      \
    mozilla::LauncherResult<mozilla::Maybe<DWORD>> result = \
        ReadRegistryValueData<DWORD>(name, REG_DWORD);      \
    if (result.isErr()) {                                   \
      return result.propagateErr();                         \
    }                                                       \
    if (result.inspect().isNothing() ||                     \
        result.inspect().value() != expected) {             \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);     \
    }                                                       \
  } while (0)

#define EXPECT_REG_QWORD_EXISTS(name)                          \
  do {                                                         \
    mozilla::LauncherResult<mozilla::Maybe<uint64_t>> result = \
        ReadRegistryValueData<uint64_t>(name, REG_QWORD);      \
    if (result.isErr()) {                                      \
      return result.propagateErr();                            \
    }                                                          \
    if (result.inspect().isNothing()) {                        \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);        \
    }                                                          \
  } while (0)

#define EXPECT_REG_QWORD_EXISTS_AND_EQ(name, expected)         \
  do {                                                         \
    mozilla::LauncherResult<mozilla::Maybe<uint64_t>> result = \
        ReadRegistryValueData<uint64_t>(name, REG_QWORD);      \
    if (result.isErr()) {                                      \
      return result.propagateErr();                            \
    }                                                          \
    if (result.inspect().isNothing() ||                        \
        result.inspect().value() != expected) {                \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);        \
    }                                                          \
  } while (0)

#define EXPECT_REG_DWORD_DOES_NOT_EXIST(name)               \
  do {                                                      \
    mozilla::LauncherResult<mozilla::Maybe<DWORD>> result = \
        ReadRegistryValueData<DWORD>(name, REG_DWORD);      \
    if (result.isErr()) {                                   \
      return result.propagateErr();                         \
    }                                                       \
    if (result.inspect().isSome()) {                        \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);     \
    }                                                       \
  } while (0)

#define EXPECT_REG_QWORD_DOES_NOT_EXIST(name)                  \
  do {                                                         \
    mozilla::LauncherResult<mozilla::Maybe<uint64_t>> result = \
        ReadRegistryValueData<uint64_t>(name, REG_QWORD);      \
    if (result.isErr()) {                                      \
      return result.propagateErr();                            \
    }                                                          \
    if (result.inspect().isSome()) {                           \
      return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);        \
    }                                                          \
  } while (0)

template <typename T>
static mozilla::LauncherResult<mozilla::Maybe<T>> ReadRegistryValueData(
    const std::wstring& name, DWORD expectedType) {
  T data;
  DWORD dataLen = sizeof(data);
  DWORD type;
  LSTATUS status = ::RegGetValueW(HKEY_CURRENT_USER, kRegKeyPath, name.c_str(),
                                  RRF_RT_ANY, &type, &data, &dataLen);
  if (status == ERROR_FILE_NOT_FOUND) {
    return mozilla::Maybe<T>();
  }

  if (status != ERROR_SUCCESS) {
    return LAUNCHER_ERROR_FROM_WIN32(status);
  }

  if (type != expectedType) {
    return LAUNCHER_ERROR_FROM_WIN32(ERROR_DATATYPE_MISMATCH);
  }

  return mozilla::Some(data);
}

template <typename T>
static mozilla::LauncherVoidResult WriteRegistryValueData(
    const std::wstring& name, DWORD type, T data) {
  LSTATUS status = ::RegSetKeyValueW(HKEY_CURRENT_USER, kRegKeyPath,
                                     name.c_str(), type, &data, sizeof(T));
  if (status != ERROR_SUCCESS) {
    return LAUNCHER_ERROR_FROM_WIN32(status);
  }

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult DeleteRegistryValueData(
    const std::wstring& name) {
  LSTATUS status =
      ::RegDeleteKeyValueW(HKEY_CURRENT_USER, kRegKeyPath, name.c_str());
  if (status == ERROR_SUCCESS || status == ERROR_FILE_NOT_FOUND) {
    return mozilla::Ok();
  }

  return LAUNCHER_ERROR_FROM_WIN32(status);
}

static mozilla::LauncherVoidResult DeleteAllRegstryValues() {
  // Unblock commit via ReflectPrefToRegistry
  // (We need to set false, and then true to bypass the early return)
  mozilla::LauncherRegistryInfo info;
  mozilla::LauncherVoidResult vr = info.ReflectPrefToRegistry(false);
  vr = info.ReflectPrefToRegistry(true);
  if (vr.isErr()) {
    return vr;
  }

  vr = DeleteRegistryValueData(gImageValue);
  if (vr.isErr()) {
    return vr;
  }

  vr = DeleteRegistryValueData(gLauncherValue);
  if (vr.isErr()) {
    return vr;
  }

  vr = DeleteRegistryValueData(gBrowserValue);
  if (vr.isErr()) {
    return vr;
  }

  return DeleteRegistryValueData(gTelemetryValue);
}

bool GetInstallHash(const char16_t*, mozilla::UniquePtr<NS_tchar[]>& result) {
  return true;
}

static mozilla::LauncherVoidResult SetupEnabledScenario() {
  // Reset the registry state to an enabled state. First, we delete all existing
  // registry values (if any).
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }

  // Now we run Check(Launcher)...
  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
  EXPECT_COMMIT_IS_OK();
  // ...and Check(Browser)
  EXPECT_CHECK_RESULT_IS(ProcessType::Browser, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();

  // By this point we are considered to be fully enabled.
  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestEmptyRegistry() {
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
  EXPECT_COMMIT_IS_OK();

  // LauncherRegistryInfo should have created Launcher and Image values
  EXPECT_REG_DWORD_EXISTS_AND_EQ(gImageValue, gMyImageTimestamp);
  EXPECT_REG_QWORD_EXISTS(gLauncherValue);
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gBrowserValue);

  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestNormal() {
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, gMyImageTimestamp);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD, QPCNowRaw());
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Browser, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();

  // Make sure the browser timestamp is newer than the launcher's
  mozilla::LauncherResult<mozilla::Maybe<uint64_t>> launcherTs =
      ReadRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD);
  if (launcherTs.isErr()) {
    return launcherTs.propagateErr();
  }
  mozilla::LauncherResult<mozilla::Maybe<uint64_t>> browserTs =
      ReadRegistryValueData<uint64_t>(gBrowserValue, REG_QWORD);
  if (browserTs.isErr()) {
    return browserTs.propagateErr();
  }
  if (launcherTs.inspect().isNothing() || browserTs.inspect().isNothing() ||
      browserTs.inspect().value() <= launcherTs.inspect().value()) {
    return LAUNCHER_ERROR_FROM_HRESULT(E_FAIL);
  }

  EXPECT_ENABLED_STATE_IS(EnabledState::Enabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestBrowserNoLauncher() {
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }
  vr = DeleteRegistryValueData(gLauncherValue);
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();

  // Verify that we still don't have a launcher timestamp
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gLauncherValue);
  // Verify that the browser timestamp is now zero
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, 0ULL);

  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestLauncherNoBrowser() {
  constexpr uint64_t launcherTs = 0x77777777;
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, gMyImageTimestamp);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD, launcherTs);
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();

  // Launcher's timestamps is kept intact while browser's is set to 0.
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gLauncherValue, launcherTs);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, 0ULL);

  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestBrowserLessThanLauncher() {
  constexpr uint64_t launcherTs = 0x77777777, browserTs = 0x66666666;
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, gMyImageTimestamp);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD, launcherTs);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gBrowserValue, REG_QWORD, browserTs);
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();

  // Launcher's timestamps is kept intact while browser's is set to 0.
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gLauncherValue, launcherTs);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, 0ULL);

  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestImageTimestampChange() {
  // This should reset the timestamps and then essentially run like
  // TestEmptyRegistry
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, 0x12345678);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD, 1ULL);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gBrowserValue, REG_QWORD, 2ULL);
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
  EXPECT_COMMIT_IS_OK();

  EXPECT_REG_DWORD_EXISTS_AND_EQ(gImageValue, gMyImageTimestamp);
  EXPECT_REG_QWORD_EXISTS(gLauncherValue);
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gBrowserValue);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestImageTimestampChangeWhenDisabled() {
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, 0x12345678);
  if (vr.isErr()) {
    return vr;
  }
  vr = WriteRegistryValueData<uint64_t>(gBrowserValue, REG_QWORD, 0ULL);
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();

  EXPECT_REG_DWORD_EXISTS_AND_EQ(gImageValue, gMyImageTimestamp);
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gLauncherValue);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, 0);

  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestDisableDueToFailure() {
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }

  // Check that we are indeed enabled.
  mozilla::LauncherRegistryInfo info;
  EXPECT_ENABLED_STATE_IS(EnabledState::Enabled);

  // Now call DisableDueToFailure
  mozilla::LauncherVoidResult lvr = info.DisableDueToFailure();
  if (lvr.isErr()) {
    return lvr.propagateErr();
  }

  // We should now be FailDisabled
  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  // If we delete the launcher timestamp, IsEnabled should then return
  // ForceDisabled.
  vr = DeleteRegistryValueData(gLauncherValue);
  if (vr.isErr()) {
    return vr;
  }
  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestPrefReflection() {
  // Reset the registry to a known good state.
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }

  // Let's see what happens when we flip the pref to OFF.
  mozilla::LauncherRegistryInfo info;
  mozilla::LauncherVoidResult reflectOk = info.ReflectPrefToRegistry(false);
  if (reflectOk.isErr()) {
    return reflectOk.propagateErr();
  }

  // Launcher timestamp should be non-existent.
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gLauncherValue);
  // Browser timestamp should be zero
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, 0ULL);
  // IsEnabled should give us ForceDisabled
  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  // Now test to see what happens when the pref is set to ON.
  reflectOk = info.ReflectPrefToRegistry(true);
  if (reflectOk.isErr()) {
    return reflectOk.propagateErr();
  }

  // Launcher and browser timestamps should be non-existent.
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gLauncherValue);
  EXPECT_REG_QWORD_DOES_NOT_EXIST(gBrowserValue);

  // IsEnabled should give us Enabled.
  EXPECT_ENABLED_STATE_IS(EnabledState::Enabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestTelemetryConfig() {
  mozilla::LauncherVoidResult vr = DeleteAllRegstryValues();
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_TELEMETRY_IS_ENABLED(false);

  mozilla::LauncherVoidResult reflectOk =
      info.ReflectTelemetryPrefToRegistry(false);
  if (reflectOk.isErr()) {
    return reflectOk.propagateErr();
  }
  EXPECT_TELEMETRY_IS_ENABLED(false);

  reflectOk = info.ReflectTelemetryPrefToRegistry(true);
  if (reflectOk.isErr()) {
    return reflectOk.propagateErr();
  }
  EXPECT_TELEMETRY_IS_ENABLED(true);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestCommitAbort() {
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }

  // Retrieve the current timestamps to compare later
  mozilla::LauncherResult<mozilla::Maybe<uint64_t>> launcherValue =
      ReadRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD);
  if (launcherValue.isErr() || launcherValue.inspect().isNothing()) {
    return launcherValue.propagateErr();
  }
  mozilla::LauncherResult<mozilla::Maybe<uint64_t>> browserValue =
      ReadRegistryValueData<uint64_t>(gBrowserValue, REG_QWORD);
  if (browserValue.isErr() || browserValue.inspect().isNothing()) {
    return browserValue.propagateErr();
  }
  uint64_t launcherTs = launcherValue.inspect().value();
  uint64_t browserTs = browserValue.inspect().value();

  vr = []() -> mozilla::LauncherVoidResult {
    mozilla::LauncherRegistryInfo info;
    EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
    // No commit
    return mozilla::Ok();
  }();
  if (vr.isErr()) {
    return vr;
  }

  // Exiting the scope discards the change.
  mozilla::LauncherRegistryInfo info;
  EXPECT_REG_DWORD_EXISTS_AND_EQ(gImageValue, gMyImageTimestamp);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gLauncherValue, launcherTs);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, browserTs);

  // Commit -> Check -> Abort -> Commit
  EXPECT_COMMIT_IS_OK();
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
  info.Abort();
  EXPECT_COMMIT_IS_OK();

  // Nothing is changed.
  EXPECT_REG_DWORD_EXISTS_AND_EQ(gImageValue, gMyImageTimestamp);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gLauncherValue, launcherTs);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gBrowserValue, browserTs);
  EXPECT_ENABLED_STATE_IS(EnabledState::Enabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestDisableDuringLauncherLaunch() {
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherResult<mozilla::Maybe<uint64_t>> launcherTs =
      ReadRegistryValueData<uint64_t>(gLauncherValue, REG_QWORD);
  if (launcherTs.isErr()) {
    return launcherTs.propagateErr();
  }
  if (launcherTs.inspect().isNothing()) {
    return LAUNCHER_ERROR_FROM_HRESULT(E_UNEXPECTED);
  }

  vr = []() -> mozilla::LauncherVoidResult {
    mozilla::LauncherRegistryInfo info;
    EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);

    // Call DisableDueToFailure with a different instance
    mozilla::LauncherVoidResult vr = []() -> mozilla::LauncherVoidResult {
      mozilla::LauncherRegistryInfo info;
      mozilla::LauncherVoidResult vr = info.DisableDueToFailure();
      if (vr.isErr()) {
        return vr.propagateErr();
      }
      return mozilla::Ok();
    }();
    if (vr.isErr()) {
      return vr;
    }

    // Commit after disable.
    EXPECT_COMMIT_IS_OK();

    return mozilla::Ok();
  }();
  if (vr.isErr()) {
    return vr;
  }

  // Make sure we're still FailDisabled and the launcher's timestamp is not
  // updated
  mozilla::LauncherRegistryInfo info;
  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);
  EXPECT_REG_QWORD_EXISTS_AND_EQ(gLauncherValue, launcherTs.inspect().value());

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestDisableDuringBrowserLaunch() {
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }

  mozilla::LauncherRegistryInfo info;
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
  EXPECT_COMMIT_IS_OK();

  vr = []() -> mozilla::LauncherVoidResult {
    mozilla::LauncherRegistryInfo info;
    EXPECT_CHECK_RESULT_IS(ProcessType::Browser, ProcessType::Browser);

    // Call DisableDueToFailure with a different instance
    mozilla::LauncherVoidResult vr = []() -> mozilla::LauncherVoidResult {
      mozilla::LauncherRegistryInfo info;
      mozilla::LauncherVoidResult vr = info.DisableDueToFailure();
      if (vr.isErr()) {
        return vr.propagateErr();
      }
      return mozilla::Ok();
    }();
    if (vr.isErr()) {
      return vr;
    }

    // Commit after disable.
    EXPECT_COMMIT_IS_OK();

    return mozilla::Ok();
  }();
  if (vr.isErr()) {
    return vr;
  }

  // Make sure we're still FailDisabled
  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  return mozilla::Ok();
}

static mozilla::LauncherVoidResult TestReEnable() {
  mozilla::LauncherVoidResult vr = SetupEnabledScenario();
  if (vr.isErr()) {
    return vr;
  }

  // Make FailDisabled
  mozilla::LauncherRegistryInfo info;
  vr = info.DisableDueToFailure();
  if (vr.isErr()) {
    return vr.propagateErr();
  }
  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  // Attempt to launch when FailDisabled: Still be FailDisabled
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();
  EXPECT_ENABLED_STATE_IS(EnabledState::FailDisabled);

  // Change the timestamp
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, 0x12345678);
  if (vr.isErr()) {
    return vr;
  }

  // Attempt to launch again: Launcher comes back
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Launcher);
  EXPECT_COMMIT_IS_OK();

  // Make ForceDisabled
  vr = info.ReflectPrefToRegistry(false);
  if (vr.isErr()) {
    return vr.propagateErr();
  }
  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  // Attempt to launch when ForceDisabled: Still be ForceDisabled
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();
  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  // Change the timestamp
  vr = WriteRegistryValueData<DWORD>(gImageValue, REG_DWORD, 0x12345678);
  if (vr.isErr()) {
    return vr;
  }

  // Attempt to launch again: Still be ForceDisabled
  EXPECT_CHECK_RESULT_IS(ProcessType::Launcher, ProcessType::Browser);
  EXPECT_COMMIT_IS_OK();
  EXPECT_ENABLED_STATE_IS(EnabledState::ForceDisabled);

  return mozilla::Ok();
}

int main(int argc, char* argv[]) {
  auto fullPath = mozilla::GetFullBinaryPath();
  if (!fullPath) {
    return 1;
  }

  // Global setup for all tests
  gBrowserValue = fullPath.get();
  gBrowserValue += kBrowserSuffix;

  gLauncherValue = fullPath.get();
  gLauncherValue += kLauncherSuffix;

  gImageValue = fullPath.get();
  gImageValue += kImageSuffix;

  gTelemetryValue = fullPath.get();
  gTelemetryValue += kTelemetrySuffix;

  mozilla::LauncherResult<DWORD> timestamp = 0;
  RUN_TEST(timestamp, GetCurrentImageTimestamp);
  gMyImageTimestamp = timestamp.unwrap();

  auto onExit = mozilla::MakeScopeExit(
      []() { mozilla::Unused << DeleteAllRegstryValues(); });

  mozilla::LauncherVoidResult vr = mozilla::Ok();

  // All testcases should call SetupEnabledScenario() or
  // DeleteAllRegstryValues() to be order-independent
  RUN_TEST(vr, TestEmptyRegistry);
  RUN_TEST(vr, TestNormal);
  RUN_TEST(vr, TestBrowserNoLauncher);
  RUN_TEST(vr, TestLauncherNoBrowser);
  RUN_TEST(vr, TestBrowserLessThanLauncher);
  RUN_TEST(vr, TestImageTimestampChange);
  RUN_TEST(vr, TestImageTimestampChangeWhenDisabled);
  RUN_TEST(vr, TestDisableDueToFailure);
  RUN_TEST(vr, TestPrefReflection);
  RUN_TEST(vr, TestTelemetryConfig);
  RUN_TEST(vr, TestCommitAbort);
  RUN_TEST(vr, TestDisableDuringLauncherLaunch);
  RUN_TEST(vr, TestDisableDuringBrowserLaunch);
  RUN_TEST(vr, TestReEnable);

  return 0;
}