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
|
// 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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "chrome/browser/upgrade_detector/get_installed_version.h"
#include <stdio.h>
#include <string>
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "components/version_info/version_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
using testing::_;
namespace {
constexpr char kChildModeSwitch[] =
"get-installed-version-linux-unittest-child-mode";
enum class ChildMode {
kNoVersion = 0,
kProcessError = 1,
kWithMonkey = 2,
kWithVersion = 3,
};
ChildMode GetChildMode() {
const auto switch_value =
base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
kChildModeSwitch);
int mode_int = 0;
base::StringToInt(switch_value, &mode_int);
return static_cast<ChildMode>(mode_int);
}
} // namespace
// A function run in a child process to print the desired version to stdout,
// just like Chrome does.
MULTIPROCESS_TEST_MAIN(GetProductVersionInChildProc) {
switch (GetChildMode()) {
case ChildMode::kNoVersion:
// Return successful process exit without printing anything.
return 0;
case ChildMode::kProcessError:
// Return a bad exit code without printing anything.
break;
case ChildMode::kWithMonkey:
// Print something unexpected and report success.
printf("monkey, monkey, monkey");
return 0;
case ChildMode::kWithVersion:
// Print the current version and report success.
printf("%s\n", version_info::GetVersionNumber().data());
return 0;
}
return 1;
}
// A multi process test that exercises the Linux implementation of
// GetInstalledVersion.
class GetInstalledVersionLinuxTest : public ::testing::Test {
protected:
GetInstalledVersionLinuxTest()
: original_command_line_(*base::CommandLine::ForCurrentProcess()) {}
~GetInstalledVersionLinuxTest() override {
*base::CommandLine::ForCurrentProcess() = original_command_line_;
}
// Adds switches to the current process command line so that when
// GetInstalledVersion relaunches the current process, it has the proper
// child proc switch to lead to GetProductVersionInChildProc above and the
// mode switch to tell the child how to behave.
void AddChildCommandLineSwitches(ChildMode child_mode) {
auto* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII(switches::kTestChildProcess,
"GetProductVersionInChildProc");
command_line->AppendSwitchASCII(
kChildModeSwitch, base::NumberToString(static_cast<int>(child_mode)));
}
private:
base::test::TaskEnvironment task_environment_;
// The original process command line; saved during construction and restored
// during destruction.
const base::CommandLine original_command_line_;
};
// Tests that an empty instance is returned when the child process reports
// nothing.
TEST_F(GetInstalledVersionLinuxTest, NoVersion) {
AddChildCommandLineSwitches(ChildMode::kNoVersion);
base::RunLoop run_loop;
base::MockCallback<InstalledVersionCallback> callback;
EXPECT_CALL(callback, Run(_))
.WillOnce([&run_loop](InstalledAndCriticalVersion versions) {
EXPECT_FALSE(versions.installed_version.IsValid());
EXPECT_FALSE(versions.critical_version.has_value());
run_loop.Quit();
});
GetInstalledVersion(callback.Get());
run_loop.Run();
}
// Tests that an empty instance is returned when the child process exits with an
// error.
TEST_F(GetInstalledVersionLinuxTest, ProcessError) {
AddChildCommandLineSwitches(ChildMode::kProcessError);
base::RunLoop run_loop;
base::MockCallback<InstalledVersionCallback> callback;
EXPECT_CALL(callback, Run(_))
.WillOnce([&run_loop](InstalledAndCriticalVersion versions) {
ASSERT_FALSE(versions.installed_version.IsValid());
EXPECT_FALSE(versions.critical_version.has_value());
run_loop.Quit();
});
GetInstalledVersion(callback.Get());
run_loop.Run();
}
// Tests that an empty instance is returned when the child process reports a
// monkey.
TEST_F(GetInstalledVersionLinuxTest, WithMonkey) {
AddChildCommandLineSwitches(ChildMode::kWithMonkey);
base::RunLoop run_loop;
base::MockCallback<InstalledVersionCallback> callback;
EXPECT_CALL(callback, Run(_))
.WillOnce([&run_loop](InstalledAndCriticalVersion versions) {
ASSERT_FALSE(versions.installed_version.IsValid());
EXPECT_FALSE(versions.critical_version.has_value());
run_loop.Quit();
});
GetInstalledVersion(callback.Get());
run_loop.Run();
}
// Tests that the expected instance is returned when the child process reports a
// valid version.
// b/344455232: Disable as the test is failing on dbg build.
#if defined(NDEBUG)
#define MAYBE_WithVersion WithVersion
#else
#define MAYBE_WithVersion DISABLED_WithVersion
#endif
TEST_F(GetInstalledVersionLinuxTest, MAYBE_WithVersion) {
AddChildCommandLineSwitches(ChildMode::kWithVersion);
base::RunLoop run_loop;
base::MockCallback<InstalledVersionCallback> callback;
EXPECT_CALL(callback, Run(_))
.WillOnce([&run_loop](InstalledAndCriticalVersion versions) {
ASSERT_TRUE(versions.installed_version.IsValid());
EXPECT_EQ(versions.installed_version, version_info::GetVersion());
EXPECT_FALSE(versions.critical_version.has_value());
run_loop.Quit();
});
GetInstalledVersion(callback.Get());
run_loop.Run();
}
|