File: auto_run_on_os_upgrade_task_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,862 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
// 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/updater/auto_run_on_os_upgrade_task.h"

#include <memory>
#include <optional>
#include <string>

#include "base/base64.h"
#include "base/containers/span.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/bind.h"
#include "base/test/test_timeouts.h"
#include "chrome/updater/activity.h"
#include "chrome/updater/persisted_data.h"
#include "chrome/updater/test/test_scope.h"
#include "chrome/updater/test/unit_test_util.h"
#include "chrome/updater/test/unit_test_util_win.h"
#include "chrome/updater/util/win_util.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/update_client/update_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace updater {

namespace {

constexpr wchar_t kAppId[] = L"{3B1A3CCA-0525-4418-93E6-A0DB3398EC9B}";
constexpr wchar_t kCmdId1[] = L"CreateOSVersionsFileOnOSUpgrade";
constexpr wchar_t kCmdLineCreateOSVersionsFile[] =
    L"/c \"echo %1 > %1 && exit 0\"";
constexpr wchar_t kCmdId2[] = L"CreateHardcodedFileOnOSUpgrade";
constexpr wchar_t kCmdLineCreateHardcodedFile[] =
    L"/c \"echo HardcodedFile > HardcodedFile && exit 0\"";
constexpr char kLastOSVersion[] = "last_os_version";

}  // namespace

class AutoRunOnOsUpgradeTaskTest : public testing::Test {
 protected:
  void SetUp() override {
    pref_service_ = std::make_unique<TestingPrefServiceSimple>();
    update_client::RegisterPrefs(pref_service_->registry());
    RegisterPersistedDataPrefs(pref_service_->registry());
    persisted_data_ = base::MakeRefCounted<PersistedData>(
        GetUpdaterScopeForTesting(), pref_service_.get(), nullptr);
    test::SetupCmdExe(GetUpdaterScopeForTesting(), cmd_exe_command_line_,
                      temp_programfiles_dir_);
  }

  void TearDown() override {
    test::DeleteAppClientKey(GetUpdaterScopeForTesting(), kAppId);
  }

  void SetLastOSVersion(const OSVERSIONINFOEX& os_version) {
    EXPECT_TRUE(pref_service_);

    std::string encoded_os_version =
        base::Base64Encode(base::byte_span_from_ref(os_version));

    pref_service_->SetString(kLastOSVersion, encoded_os_version);
  }

  scoped_refptr<PersistedData> persisted_data_;
  std::unique_ptr<TestingPrefServiceSimple> pref_service_;
  base::CommandLine cmd_exe_command_line_{base::CommandLine::NO_PROGRAM};
  base::ScopedTempDir temp_programfiles_dir_;
};

TEST_F(AutoRunOnOsUpgradeTaskTest, RunOnOsUpgradeForApp) {
  const std::optional<OSVERSIONINFOEX> current_os_version = GetOSVersion();
  ASSERT_NE(current_os_version, std::nullopt);
  OSVERSIONINFOEX last_os_version = current_os_version.value();
  --last_os_version.dwMajorVersion;

  // Sets a lower major version in the persisted data prefs to look like an OS
  // upgrade.
  SetLastOSVersion(last_os_version);

  auto os_upgrade_task = base::MakeRefCounted<AutoRunOnOsUpgradeTask>(
      GetUpdaterScopeForTesting(), persisted_data_);
  ASSERT_TRUE(os_upgrade_task->HasOSUpgraded());

  test::CreateAppCommandOSUpgradeRegistry(
      GetUpdaterScopeForTesting(), kAppId, kCmdId1,
      base::StrCat({cmd_exe_command_line_.GetCommandLineString(), L" ",
                    kCmdLineCreateOSVersionsFile}));
  test::CreateAppCommandOSUpgradeRegistry(
      GetUpdaterScopeForTesting(), kAppId, kCmdId2,
      base::StrCat({cmd_exe_command_line_.GetCommandLineString(), L" ",
                    kCmdLineCreateHardcodedFile}));

  ASSERT_EQ(os_upgrade_task->RunOnOsUpgradeForApp(base::WideToUTF8(kAppId)),
            2U);

  const std::wstring os_upgrade_string = [&] {
    std::string versions;
    for (const auto& version : {last_os_version, current_os_version.value()}) {
      versions += base::StringPrintf(
          "%lu.%lu.%lu.%u.%u%s", version.dwMajorVersion, version.dwMinorVersion,
          version.dwBuildNumber, version.wServicePackMajor,
          version.wServicePackMinor, versions.empty() ? "-" : "");
    }
    return base::UTF8ToWide(versions);
  }();

  base::FilePath current_directory;
  base::PathService::Get(base::DIR_CURRENT, &current_directory);
  base::FilePath os_upgrade_file = current_directory.Append(os_upgrade_string);
  base::FilePath hardcoded_file = current_directory.Append(L"HardcodedFile");

  EXPECT_TRUE(test::WaitFor([&] {
    return base::PathExists(os_upgrade_file) &&
           base::PathExists(hardcoded_file);
  }));
  EXPECT_TRUE(base::DeleteFile(os_upgrade_file));
  EXPECT_TRUE(base::DeleteFile(hardcoded_file));
}

}  // namespace updater