File: integration_tests_mac.mm

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 (826 lines) | stat: -rw-r--r-- 31,256 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// Copyright 2020 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/updater/test/integration_tests_mac.h"

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/apple/bridging.h"
#include "base/apple/foundation_util.h"
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/file_version_info.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "base/version.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/updater/activity.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/external_constants_builder.h"
#import "chrome/updater/mac/client_lib/CRURegistration-Private.h"
#import "chrome/updater/mac/client_lib/CRURegistration.h"
#include "chrome/updater/mac/privileged_helper/service.h"
#include "chrome/updater/persisted_data.h"
#include "chrome/updater/prefs.h"
#include "chrome/updater/registration_data.h"
#include "chrome/updater/test/integration_tests_impl.h"
#include "chrome/updater/test/unit_test_util.h"
#include "chrome/updater/updater_branding.h"
#include "chrome/updater/updater_scope.h"
#import "chrome/updater/util/mac_util.h"
#include "chrome/updater/util/posix_util.h"
#include "chrome/updater/util/util.h"
#include "components/crx_file/crx_verifier.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "third_party/crashpad/crashpad/client/crash_report_database.h"
#include "third_party/crashpad/crashpad/client/settings.h"
#include "url/gurl.h"

namespace updater::test {
namespace {

base::FilePath GetExecutablePath() {
  base::FilePath out_dir;
  if (!base::PathService::Get(base::DIR_EXE, &out_dir)) {
    return base::FilePath();
  }
  return out_dir.Append(GetExecutableRelativePath());
}

std::optional<base::FilePath> GetActiveFile(UpdaterScope /*scope*/,
                                            const std::string& id) {
  // The active user is always managed in the updater scope for the user.
  const std::optional<base::FilePath> path =
      GetLibraryFolderPath(UpdaterScope::kUser);
  if (!path) {
    return std::nullopt;
  }

  return path->Append(COMPANY_SHORTNAME_STRING)
      .Append(COMPANY_SHORTNAME_STRING "SoftwareUpdate")
      .Append("Actives")
      .Append(id);
}

}  // namespace

base::FilePath GetSetupExecutablePath() {
  // There is no metainstaller on mac, use the main executable for setup.
  return GetExecutablePath();
}

base::TimeDelta GetOverinstallTimeoutForEnterTestMode() {
  return base::Seconds(5);
}

void Clean(UpdaterScope scope) {
  CleanProcesses();

  std::optional<base::FilePath> path = GetInstallDirectory(scope);
  EXPECT_TRUE(path);
  if (path) {
    EXPECT_TRUE(base::DeletePathRecursively(*path));
  }
  EXPECT_TRUE(base::DeleteFile(*GetWakeTaskPlistPath(scope)));

  path = GetInstallDirectory(scope);
  EXPECT_TRUE(path);
  if (path) {
    EXPECT_TRUE(base::DeletePathRecursively(*path));
  }

  std::optional<base::FilePath> keystone_path = GetKeystoneFolderPath(scope);
  EXPECT_TRUE(keystone_path);
  if (keystone_path) {
    EXPECT_TRUE(base::DeletePathRecursively(*keystone_path));
  }

  // TODO(crbug.com/394302692): Delete after CIPD updater versions are M136+.
  EXPECT_TRUE(base::DeletePathRecursively(
      base::FilePath("/Library/Caches/").Append(MAC_BUNDLE_IDENTIFIER_STRING)));

  EXPECT_TRUE(RemoveWakeJobFromLaunchd(scope));

  // Also clean up any other versions of the updater that are around.
  base::CommandLine launchctl(base::FilePath("/bin/launchctl"));
  launchctl.AppendArg("list");
  std::string out;
  ASSERT_TRUE(base::GetAppOutput(launchctl, &out));
  for (const auto& token : base::SplitStringPiece(out, base::kWhitespaceASCII,
                                                  base::TRIM_WHITESPACE,
                                                  base::SPLIT_WANT_NONEMPTY)) {
    if (base::StartsWith(token, MAC_BUNDLE_IDENTIFIER_STRING)) {
      std::string out_rm;
      base::CommandLine launchctl_rm(base::FilePath("/bin/launchctl"));
      launchctl_rm.AppendArg("remove");
      launchctl_rm.AppendArg(token);
      ASSERT_TRUE(base::GetAppOutput(launchctl_rm, &out_rm));
    }
  }

  if (IsSystemInstall(scope)) {
    ASSERT_NO_FATAL_FAILURE(UninstallEnterpriseCompanionApp());
  }
}

void DeleteLegacyUpdater(UpdaterScope scope) {
  std::optional<base::FilePath> keystone = GetKeystoneFolderPath(scope);
  ASSERT_TRUE(keystone);
  ASSERT_TRUE(base::DeletePathRecursively(*keystone));
}

void ExpectClean(UpdaterScope scope) {
  ExpectCleanProcesses();

  // Files must not exist on the file system.
  EXPECT_FALSE(base::PathExists(*GetWakeTaskPlistPath(scope)));

  std::optional<base::FilePath> path = GetInstallDirectory(scope);
  EXPECT_TRUE(path);
  if (path && base::PathExists(*path)) {
    // If the path exists, then expect only the log and json files to be
    // present.
    int count = CountDirectoryFiles(*path);
    for (const auto& file_name : {"updater.log", "prefs.json"}) {
      if (base::PathExists(path->Append(file_name))) {
        count -= 1;
      }
    }
    EXPECT_EQ(count, 0) << base::JoinString(
        [](const base::FilePath& dir) {
          std::vector<base::FilePath::StringType> files;
          base::FileEnumerator(dir, false, base::FileEnumerator::FILES)
              .ForEach([&files](const base::FilePath& name) {
                files.push_back(name.value());
              });
          return files;
        }(*path),
        FILE_PATH_LITERAL(","));
  }
  // Keystone must not exist on the file system.
  std::optional<base::FilePath> keystone_path = GetKeystoneFolderPath(scope);
  EXPECT_TRUE(keystone_path);
  if (keystone_path) {
    EXPECT_FALSE(
        base::PathExists(keystone_path->Append(KEYSTONE_NAME ".bundle")));
  }
  ASSERT_NO_FATAL_FAILURE(ExpectEnterpriseCompanionAppNotInstalled());
}

void ExpectInstalled(UpdaterScope scope) {
  std::optional<base::FilePath> keystone_path = GetKeystoneFolderPath(scope);
  ASSERT_TRUE(keystone_path);

  // Files must exist on the file system.
  for (const auto& path :
       {GetInstallDirectory(scope), keystone_path, GetKSAdminPath(scope)}) {
    ASSERT_TRUE(path) << path;
    EXPECT_TRUE(base::PathExists(*path)) << path;
  }

  EXPECT_TRUE(base::PathExists(*GetWakeTaskPlistPath(scope)));
}

std::optional<base::FilePath> GetInstalledExecutablePath(UpdaterScope scope) {
  return GetUpdaterExecutablePath(scope);
}

void ExpectCandidateUninstalled(UpdaterScope scope) {
  std::optional<base::FilePath> versioned_folder_path =
      GetVersionedInstallDirectory(scope);
  ASSERT_TRUE(versioned_folder_path);
  EXPECT_FALSE(base::PathExists(*versioned_folder_path));
}

void Uninstall(UpdaterScope scope) {
  std::optional<base::FilePath> path = GetExecutablePath();
  ASSERT_TRUE(path);
  base::CommandLine command_line(*path);
  command_line.AppendSwitch(kUninstallSwitch);
  int exit_code = -1;
  Run(scope, command_line, &exit_code);
  ASSERT_EQ(exit_code, 0);
}

void SetActive(UpdaterScope scope, const std::string& app_id) {
  const std::optional<base::FilePath> path = GetActiveFile(scope, app_id);
  ASSERT_TRUE(path);
  VLOG(0) << "Actives file: " << *path;
  base::File::Error err = base::File::FILE_OK;
  EXPECT_TRUE(base::CreateDirectoryAndGetError(path->DirName(), &err))
      << "Error: " << err;
  EXPECT_TRUE(base::WriteFile(*path, ""));
}

void ExpectActive(UpdaterScope scope, const std::string& app_id) {
  const std::optional<base::FilePath> path = GetActiveFile(scope, app_id);
  ASSERT_TRUE(path);
  EXPECT_TRUE(base::PathExists(*path));
  EXPECT_TRUE(base::PathIsWritable(*path));
}

void ExpectNotActive(UpdaterScope scope, const std::string& app_id) {
  const std::optional<base::FilePath> path = GetActiveFile(scope, app_id);
  ASSERT_TRUE(path);
  EXPECT_FALSE(base::PathExists(*path));
  EXPECT_FALSE(base::PathIsWritable(*path));
}

bool WaitForUpdaterExit() {
  std::string last_found;
  return WaitFor(
      [&] {
        std::string ps_stdout;
        EXPECT_TRUE(
            base::GetAppOutput({"ps", "ax", "-o", "command"}, &ps_stdout));
        std::vector<std::string_view> commands = base::SplitStringPiece(
            ps_stdout, "\n", base::WhitespaceHandling::TRIM_WHITESPACE,
            base::SplitResult::SPLIT_WANT_NONEMPTY);
        for (const auto& command : commands) {
          // Skip command lines referencing the symbol files, other processes
          // can safely have those open (and often do on ASAN bots).
          if (command.find(GetExecutablePath().BaseName().AsUTF8Unsafe()) !=
                  std::string::npos &&
              command.find(".dSYM") == std::string::npos) {
            last_found = command;
            return false;
          }
        }
        return true;
      },
      [&] { VLOG(0) << "Still waiting for updater to exit: " << last_found; });
}

std::vector<TestUpdaterVersion> GetRealUpdaterLowerVersions(
    const std::string& arch_suffix) {
  base::FilePath exe_path;
  EXPECT_TRUE(base::PathService::Get(base::DIR_EXE, &exe_path));
  base::FilePath old_updater_path =
      exe_path.Append(FILE_PATH_LITERAL("old_updater"));

  std::string arch;
#if BUILDFLAG(CHROMIUM_BRANDING)
#if defined(ARCH_CPU_ARM64)
  arch = "chromium_mac_arm64";
#elif defined(ARCH_CPU_X86_64)
  arch = "chromium_mac_amd64";
#endif
#elif BUILDFLAG(GOOGLE_CHROME_BRANDING)
  arch = "chrome_mac_universal";
#endif

  old_updater_path = old_updater_path.Append(base::StrCat({arch, arch_suffix}));

#if BUILDFLAG(CHROMIUM_BRANDING) || BUILDFLAG(GOOGLE_CHROME_BRANDING)
  old_updater_path = old_updater_path.Append("cipd");
#endif

  const base::FilePath updater_setup_path =
      old_updater_path.Append(PRODUCT_FULLNAME_STRING "_test.app")
          .Append("Contents")
          .Append("MacOS")
          .Append(PRODUCT_FULLNAME_STRING "_test");
  return {{updater_setup_path,
           base::Version(base::UTF16ToUTF8(
               FileVersionInfo::CreateFileVersionInfo(updater_setup_path)
                   ->file_version()))}};
}

void SetupFakeLegacyUpdater(UpdaterScope scope) {
  base::FilePath updater_test_data_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_TEST_DATA, &updater_test_data_path));
  updater_test_data_path =
      updater_test_data_path.Append(FILE_PATH_LITERAL("updater"));

  base::FilePath keystone_path = GetKeystoneFolderPath(scope).value();
  base::FilePath keystone_ticket_store_path =
      keystone_path.Append(FILE_PATH_LITERAL("TicketStore"));
  ASSERT_TRUE(base::CreateDirectory(keystone_ticket_store_path));
  ASSERT_TRUE(base::CopyFile(
      updater_test_data_path.Append("Keystone.legacy.ticketstore"),
      keystone_ticket_store_path.Append("Keystone.ticketstore")));
  ASSERT_TRUE(
      base::CopyFile(updater_test_data_path.Append("CountingMetrics.plist"),
                     keystone_path.Append("CountingMetrics.plist")));
}

void ExpectLegacyUpdaterMigrated(UpdaterScope scope) {
  scoped_refptr<GlobalPrefs> global_prefs = CreateGlobalPrefs(scope);
  auto persisted_data = base::MakeRefCounted<PersistedData>(
      scope, global_prefs->GetPrefService(), nullptr);

  // Keystone should not be migrated.
  EXPECT_FALSE(
      persisted_data->GetProductVersion(LEGACY_GOOGLE_UPDATE_APPID).IsValid());

  // Uninstalled app should be migrated.
  EXPECT_TRUE(
      persisted_data->GetProductVersion("com.chromium.NonExistApp").IsValid());

  // App Kipple.
  const std::string kKippleApp = "com.chromium.kipple";
  EXPECT_EQ(persisted_data->GetProductVersion(kKippleApp),
            base::Version("1.2.3.4"));
  EXPECT_EQ(persisted_data->GetExistenceCheckerPath(kKippleApp),
            base::FilePath("/"));
  EXPECT_TRUE(persisted_data->GetAP(kKippleApp).empty());
  EXPECT_TRUE(persisted_data->GetBrandCode(kKippleApp).empty());
  EXPECT_TRUE(persisted_data->GetBrandPath(kKippleApp).empty());
  EXPECT_TRUE(persisted_data->GetFingerprint(kKippleApp).empty());
  EXPECT_EQ(persisted_data->GetDateLastActive(kKippleApp), -1);
  EXPECT_EQ(persisted_data->GetDateLastRollCall(kKippleApp), -1);

  // App PopularApp.
  const std::string kPopularApp = "com.chromium.PopularApp";
  EXPECT_EQ(persisted_data->GetProductVersion(kPopularApp),
            base::Version("101.100.1000.9999"));
  EXPECT_EQ(persisted_data->GetExistenceCheckerPath(kPopularApp),
            base::FilePath("/"));
  EXPECT_EQ(persisted_data->GetAP(kPopularApp), "GOOG");
  EXPECT_TRUE(persisted_data->GetBrandCode(kPopularApp).empty());
  EXPECT_EQ(persisted_data->GetBrandPath(kPopularApp), base::FilePath("/"));
  EXPECT_TRUE(persisted_data->GetFingerprint(kPopularApp).empty());
  EXPECT_EQ(persisted_data->GetDateLastActive(kPopularApp), 5921);
  EXPECT_EQ(persisted_data->GetDateLastRollCall(kPopularApp), 5922);

  EXPECT_EQ(persisted_data->GetCohort(kPopularApp), "TestCohort");
  EXPECT_EQ(persisted_data->GetCohortName(kPopularApp), "TestCohortName");
  EXPECT_EQ(persisted_data->GetCohortHint(kPopularApp), "TestCohortHint");

  // App CorruptedApp (client-regulated counting data is corrupted).
  const std::string kCorruptedApp = "com.chromium.CorruptedApp";
  EXPECT_EQ(persisted_data->GetProductVersion(kCorruptedApp),
            base::Version("1.2.1"));
  EXPECT_EQ(persisted_data->GetExistenceCheckerPath(kCorruptedApp),
            base::FilePath("/"));
  EXPECT_EQ(persisted_data->GetAP(kCorruptedApp), "canary");
  EXPECT_EQ(persisted_data->GetDateLastActive(kCorruptedApp), -1);
  EXPECT_EQ(persisted_data->GetDateLastRollCall(kCorruptedApp), -1);
}

void InstallApp(UpdaterScope scope,
                const std::string& app_id,
                const base::Version& version) {
  RegistrationRequest registration;
  registration.app_id = app_id;
  registration.version = version;
  RegisterApp(scope, registration);
}

void UninstallApp(UpdaterScope scope, const std::string& app_id) {
  const base::FilePath& install_path =
      base::MakeRefCounted<PersistedData>(
          scope, CreateGlobalPrefs(scope)->GetPrefService(), nullptr)
          ->GetExistenceCheckerPath(app_id);
  VLOG(1) << "Deleting app install path: " << install_path;
  base::DeletePathRecursively(install_path);
  SetExistenceCheckerPath(scope, app_id,
                          base::FilePath(FILE_PATH_LITERAL("NONE")));
}

base::CommandLine MakeElevated(base::CommandLine command_line) {
  command_line.PrependWrapper("/usr/bin/sudo");
  return command_line;
}

void SetPlatformPolicies(const base::Value::Dict& values) {
  const CFStringRef domain = CFSTR(LEGACY_GOOGLE_UPDATE_APPID);

  // Synchronize just to be safe. Ignore spurious errors if the domain
  // does not yet exist.
  CFPreferencesSynchronize(domain, kCFPreferencesAnyUser,
                           kCFPreferencesCurrentHost);

  NSMutableDictionary* all_policies = [NSMutableDictionary dictionary];
  for (const auto [app_id, policies] : values) {
    ASSERT_TRUE(policies.is_dict());
    NSMutableDictionary* app_policies = [NSMutableDictionary dictionary];
    for (const auto [name, value] : policies.GetDict()) {
      NSString* key = base::SysUTF8ToNSString(name);
      if (value.is_string()) {
        app_policies[key] = base::SysUTF8ToNSString(value.GetString());
      } else if (value.is_int()) {
        app_policies[key] = [NSNumber numberWithInt:value.GetInt()];
      } else if (value.is_bool()) {
        app_policies[key] = [NSNumber numberWithInt:value.GetBool()];
      }
    }
    all_policies[base::SysUTF8ToNSString(app_id)] = app_policies;

    NSURL* const managed_preferences_url = base::apple::FilePathToNSURL(
        GetLibraryFolderPath(UpdaterScope::kSystem)
            ->Append("Managed Preferences")
            .Append(LEGACY_GOOGLE_UPDATE_APPID ".plist"));
    ASSERT_TRUE([[NSDictionary dictionaryWithObject:all_policies
                                             forKey:@"updatePolicies"]
        writeToURL:managed_preferences_url
             error:nil])
        << "Failed to write " << managed_preferences_url;
  }
  ASSERT_TRUE(CFPreferencesSynchronize(domain, kCFPreferencesAnyUser,
                                       kCFPreferencesCurrentHost));

  // Force flushing preferences cache by killing the defaults server.
  base::Process process = base::LaunchProcess({"killall", "cfprefsd"}, {});
  if (!process.IsValid()) {
    VLOG(2) << "Failed to launch the process to refresh preferences.";
  }
  // Exit code is not checked because it is not 0 when `cfprefsd` is not
  // found.
  EXPECT_TRUE(
      process.WaitForExitWithTimeout(TestTimeouts::action_timeout(), nullptr));
}

void PrivilegedHelperInstall(UpdaterScope scope) {
  ASSERT_EQ(scope, UpdaterScope::kSystem)
      << "The privileged helper only works at system scope.";
  base::FilePath src_dir;
  ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &src_dir));
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  base::FilePath helpers_dir = temp_dir.GetPath().Append(
      "Contents/Frameworks/" BROWSER_PRODUCT_NAME_STRING
      " Framework.framework/Helpers/");
  ASSERT_TRUE(base::CreateDirectory(helpers_dir));
  ASSERT_TRUE(CopyDir(src_dir.Append("third_party")
                          .Append("updater")
                          .Append("chrome_mac_universal_prod")
                          .Append("cipd")
                          .Append(PRODUCT_FULLNAME_STRING ".app"),
                      helpers_dir, false));
  ASSERT_TRUE(
      base::WriteFile(temp_dir.GetPath().Append("Contents/Info.plist"),
                      R"(<?xml version="1.0" encoding="UTF-8"?>)"
                      R"(<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN")"
                      R"(    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">)"
                      R"(<plist version="1.0">)"
                      R"(<dict>)"
                      R"(<key>KSProductID</key>)"
                      R"(<string>test1</string>)"
                      R"(<key>KSChannelID</key>)"
                      R"(<string>tag</string>)"
                      R"(<key>KSVersion</key>)"
                      R"(<string>1.2.3.4</string>)"
                      R"(</dict>)"
                      R"(</plist>)"));
  ASSERT_TRUE(VerifyUpdaterSignature(
      helpers_dir.Append(PRODUCT_FULLNAME_STRING ".app")));
  ASSERT_EQ(InstallUpdater(temp_dir.GetPath()), 0);
}

void ExpectAppVersion(UpdaterScope scope,
                      const std::string& app_id,
                      const base::Version& version) {
  const base::Version app_version =
      base::MakeRefCounted<PersistedData>(
          scope, CreateGlobalPrefs(scope)->GetPrefService(), nullptr)
          ->GetProductVersion(app_id);
  if (version.IsValid()) {
    EXPECT_TRUE(app_version.IsValid());
    EXPECT_EQ(version, app_version);
  } else {
    EXPECT_FALSE(app_version.IsValid());
  }
}

void ExpectPrepareToRunBundleSuccess(const base::FilePath& bundle_path) {
  EXPECT_TRUE(PrepareToRunBundle(bundle_path));
}

void ExpectKSAdminResult(UpdaterScope scope,
                         bool elevate,
                         const std::map<std::string, std::string>& switches,
                         std::optional<std::string> want_stdout,
                         std::optional<int> want_exit_code) {
  const std::optional<base::FilePath> ksadmin_path = GetKSAdminPath(scope);
  ASSERT_TRUE(ksadmin_path && !ksadmin_path->empty());
  ASSERT_TRUE(base::PathExists(*ksadmin_path));

  base::CommandLine command_line(*ksadmin_path);
  for (const auto& [key, value] : switches) {
    command_line.AppendSwitchUTF8(key, value);
  }

  ExpectCliResult(command_line, elevate, std::move(want_stdout),
                  want_exit_code);
}

void ExpectKSAdminFetchTag(UpdaterScope scope,
                           bool elevate,
                           const std::string& product_id,
                           const base::FilePath& xc_path,
                           std::optional<UpdaterScope> store_flag,
                           std::optional<std::string> want_tag) {
  std::map<std::string, std::string> switches;
  switches["--print-tag"] = "";
  switches["--productid"] = product_id;
  if (!xc_path.empty()) {
    switches["--xcpath"] = xc_path.value();
  }
  if (store_flag) {
    switch (*store_flag) {
      case UpdaterScope::kSystem:
        switches["--system-store"] = "";
        break;
      case UpdaterScope::kUser:
        switches["--user-store"] = "";
        break;
    }
  }

  int want_exit = EXIT_FAILURE;
  if (want_tag) {
    *want_tag = base::StrCat({*want_tag, "\n"});
    want_exit = EXIT_SUCCESS;
  }

  ExpectKSAdminResult(scope, elevate, switches, std::move(want_tag), want_exit);
}

void ExpectKSAdminXattrBrand(UpdaterScope scope,
                             bool elevate,
                             const base::FilePath& path,
                             std::optional<std::string> want_brand) {
  int want_exit = EXIT_FAILURE;
  if (want_brand) {
    *want_brand = base::StrCat({*want_brand, "\n"});
    want_exit = EXIT_SUCCESS;
  }
  ExpectKSAdminResult(scope, elevate,
                      {{"--print-xattr-tag-brand", path.value()}},
                      std::move(want_brand), want_exit);
}

void ExpectCRURegistrationCannotFindKSAdmin() {
  @autoreleasepool {
    CRURegistration* registration = [[CRURegistration alloc]
               initWithAppId:@"org.chromium.ChromiumUpdater.CannotFindKSAdmin"
        existenceCheckerPath:@"IGNORED during this test"];
    NSURL* got_ksadmin = [registration syncFindBestKSAdmin];
    EXPECT_FALSE(got_ksadmin)
        << "unexpectedly found ksadmin: "
        << base::SysNSStringToUTF8(got_ksadmin.absoluteString);
  }
}

void ExpectCRURegistrationFindsKSAdmin(UpdaterScope scope) {
  @autoreleasepool {
    CRURegistration* registration = [[CRURegistration alloc]
               initWithAppId:@"org.chromium.ChromiumUpdater.FindsKSAdmin"
        existenceCheckerPath:@"IGNORED during this test"];
    NSURL* got_ksadmin = [registration syncFindBestKSAdmin];
    EXPECT_TRUE(got_ksadmin);
    EXPECT_TRUE([got_ksadmin isFileURL]);
    base::FilePath got_ksadmin_path(base::apple::NSURLToFilePath(got_ksadmin));
    EXPECT_FALSE(got_ksadmin_path.empty());
    got_ksadmin_path = base::MakeAbsoluteFilePath(got_ksadmin_path);
    EXPECT_FALSE(got_ksadmin_path.empty());

    std::optional<base::FilePath> expected_ksadmin_path(GetKSAdminPath(scope));
    ASSERT_TRUE(expected_ksadmin_path);
    EXPECT_FALSE(expected_ksadmin_path->empty());

    EXPECT_TRUE(base::FilePath::CompareEqualIgnoreCase(
        expected_ksadmin_path->value(), got_ksadmin_path.value()))
        << "unexpected ksadmin path.\n\twant: "
        << expected_ksadmin_path->value()
        << "\n\t got: " << got_ksadmin_path.value();
  }
}

/**
 * InvokeCRURegistrationAndWait creates a CRURegistration and a semaphore, hands
 * them off to a test-specific block, and waits for the semaphore to signal
 * or times out after ten seconds. It returns whether the semaphore was
 * successfully signaled. This factors out the common logic for converting
 * CRURegistration's asynchronous operations to something functionally
 * synchronous for test purposes. The test thread blocks while waiting.
 *
 * The `impl` block is responsible for invoking the (asynchronous) method
 * under test on the provided `CRURegistration` and providing a reply block
 * to it that stores results from CRURegistration, then signals the semaphore.
 *
 * Mishandling the semaphore in `impl`, or ignoring a `false` result from this
 * function and subsequently attempting to access fields `impl` set up the
 * CRURegistration reply block to write to, will cause a data race, which is
 * undefined behavior in C.
 */
[[nodiscard]] bool InvokeCRURegistrationAndWait(
    const std::string& app_id,
    const base::FilePath& xc_path,
    void (^impl)(CRURegistration*, dispatch_semaphore_t)) {
  if (!impl) {
    ADD_FAILURE() << "test issue - no impl provided";
    return false;
  }
  NSString* ns_xc_path = @"NOT PROVIDED FOR THIS TEST";
  if (!xc_path.empty()) {
    ns_xc_path = base::apple::FilePathToNSString(xc_path);
  }
  if (!ns_xc_path) {
    ADD_FAILURE() << "test issue - xc_path could not be converted to NSString";
    return false;
  }
  CRURegistration* registration =
      [[CRURegistration alloc] initWithAppId:base::SysUTF8ToNSString(app_id)
                        existenceCheckerPath:ns_xc_path];

  dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  impl(registration, semaphore);

  dispatch_time_t limit = dispatch_time(DISPATCH_TIME_NOW, 10L * NSEC_PER_SEC);
  return !dispatch_semaphore_wait(semaphore, limit);
}

void ExpectCRURegistrationCannotFetchTag(const std::string& app_id,
                                         const base::FilePath& xc_path) {
  @autoreleasepool {
    __block NSString* got_tag = nil;
    __block NSError* got_error = nil;

    ASSERT_TRUE(InvokeCRURegistrationAndWait(
        app_id, xc_path,
        ^(CRURegistration* registration, dispatch_semaphore_t semaphore) {
          [registration fetchTagWithReply:^(NSString* tag, NSError* error) {
            got_tag = tag;
            got_error = error;
            dispatch_semaphore_signal(semaphore);
          }];
        }));

    EXPECT_FALSE(got_tag) << base::SysNSStringToUTF8(got_tag);
    EXPECT_TRUE(got_error);
  }
}

void ExpectCRURegistrationFetchesTag(const std::string& app_id,
                                     const base::FilePath& xc_path,
                                     const std::string& want_tag) {
  @autoreleasepool {
    __block NSString* got_tag = nil;
    __block NSError* got_error = nil;

    ASSERT_TRUE(InvokeCRURegistrationAndWait(
        app_id, xc_path,
        ^(CRURegistration* registration, dispatch_semaphore_t semaphore) {
          [registration fetchTagWithReply:^(NSString* tag, NSError* error) {
            got_tag = tag;
            got_error = error;
            dispatch_semaphore_signal(semaphore);
          }];
        }));

    EXPECT_FALSE(got_error) << base::SysNSStringToUTF8([got_error description]);
    ASSERT_TRUE(got_tag);
    EXPECT_EQ(want_tag, base::SysNSStringToUTF8(got_tag));
  }
}

void ExpectCRURegistrationRegisters(const std::string& app_id,
                                    const base::FilePath& xc_path,
                                    const std::string& version_str) {
  @autoreleasepool {
    __block NSError* got_error = nil;

    ASSERT_TRUE(InvokeCRURegistrationAndWait(
        app_id, xc_path,
        ^(CRURegistration* registration, dispatch_semaphore_t semaphore) {
          [registration registerVersion:base::SysUTF8ToNSString(version_str)
                                  reply:^(NSError* error) {
                                    got_error = error;
                                    dispatch_semaphore_signal(semaphore);
                                  }];
        }));

    EXPECT_FALSE(got_error) << base::SysNSStringToUTF8([got_error description]);
  }
}

void ExpectCRURegistrationCannotRegister(const std::string& app_id,
                                         const base::FilePath& xc_path,
                                         const std::string& version_str) {
  @autoreleasepool {
    __block NSError* got_error = nil;

    ASSERT_TRUE(InvokeCRURegistrationAndWait(
        app_id, xc_path,
        ^(CRURegistration* registration, dispatch_semaphore_t semaphore) {
          [registration registerVersion:base::SysUTF8ToNSString(version_str)
                                  reply:^(NSError* error) {
                                    got_error = error;
                                    dispatch_semaphore_signal(semaphore);
                                  }];
        }));

    EXPECT_TRUE(got_error);
  }
}

void ExpectCRURegistrationMarksActive(const std::string& app_id) {
  @autoreleasepool {
    __block NSError* got_error = nil;

    ASSERT_TRUE(InvokeCRURegistrationAndWait(
        app_id, {},
        ^(CRURegistration* registration, dispatch_semaphore_t semaphore) {
          [registration markActiveWithReply:^(NSError* error) {
            got_error = error;
            dispatch_semaphore_signal(semaphore);
          }];
        }));

    EXPECT_FALSE(got_error) << base::SysNSStringToUTF8([got_error description]);
  }
}

std::optional<base::FilePath> GetRegistrationTestAppPath() {
  base::FilePath exe_path;
  if (!base::PathService::Get(base::DIR_EXE, &exe_path)) {
    return std::nullopt;
  }
  const base::FilePath test_app_path =
      exe_path.Append("registration_test_app_bundle.app/Contents/MacOS/"
                      "registration_test_app_bundle");
  if (test_app_path.empty() || !base::PathExists(test_app_path)) {
    return std::nullopt;
  }
  return test_app_path;
}

void ExpectRegistrationTestAppSuccess(const std::string& arg) {
  std::optional<base::FilePath> test_app_path = GetRegistrationTestAppPath();
  ASSERT_TRUE(test_app_path);
  base::CommandLine command_line(*test_app_path);
  command_line.AppendArg(arg);

  ExpectCliResult(command_line, false, {}, 0);
}

void ExpectRegistrationTestAppUserUpdaterInstallSuccess() {
  ExpectRegistrationTestAppSuccess("--install");
}

void ExpectRegistrationTestAppRegisterSuccess() {
  ExpectRegistrationTestAppSuccess("--register");
}

void ExpectRegistrationTestAppInstallAndRegisterSuccess() {
  ExpectRegistrationTestAppSuccess("--install_and_register");
}

void SetAppAllowsUsageStats(UpdaterScope scope,
                            const std::string& identifier,
                            bool allowed) {
  std::optional<base::FilePath> application_support_dir =
      GetApplicationSupportDirectory(scope);
  ASSERT_TRUE(application_support_dir);
  base::FilePath app_dir =
      application_support_dir->Append(COMPANY_SHORTNAME_STRING)
          .Append(identifier);

  LOG(ERROR) << __func__ << " : " << app_dir;

  ASSERT_TRUE(base::CreateDirectory(app_dir));
  std::unique_ptr<crashpad::CrashReportDatabase> database =
      crashpad::CrashReportDatabase::Initialize(app_dir.Append("Crashpad"));
  ASSERT_TRUE(database && database->GetSettings()->SetUploadsEnabled(allowed));
}

void ClearAppAllowsUsageStats(UpdaterScope scope,
                              const std::string& identifier) {
  std::optional<base::FilePath> application_support_dir =
      GetApplicationSupportDirectory(scope);
  ASSERT_TRUE(application_support_dir);
  base::FilePath app_dir =
      application_support_dir->Append(COMPANY_SHORTNAME_STRING)
          .Append(identifier);

  ASSERT_TRUE(base::DeletePathRecursively(app_dir));
}

}  // namespace updater::test