Commit ba563dc0 authored by Olivier Li's avatar Olivier Li Committed by Commit Bot

Add the settings directory

Bug: 830892
Change-Id: I665849d42d4af63171a972eaa10d9670b23f8899
Reviewed-on: https://chromium-review.googlesource.com/1134220
Commit-Queue: Chris Sharp <csharp@chromium.org>
Reviewed-by: default avatarChris Sharp <csharp@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574914}
parent b1fc7250
...@@ -6,13 +6,17 @@ import("//testing/test.gni") ...@@ -6,13 +6,17 @@ import("//testing/test.gni")
test("chrome_cleaner_unittests") { test("chrome_cleaner_unittests") {
sources = [ sources = [
"//chrome/chrome_cleaner/engines/dummy_engine_resources.cc",
"test/test_main.cc", "test/test_main.cc",
] ]
deps = [ deps = [
"//chrome/chrome_cleaner/engines:resources",
# Tests from sub-directories. # Tests from sub-directories.
"//chrome/chrome_cleaner/http:unittest_sources", "//chrome/chrome_cleaner/http:unittest_sources",
"//chrome/chrome_cleaner/os:unittest_sources", "//chrome/chrome_cleaner/os:unittest_sources",
"//chrome/chrome_cleaner/settings:unittest_sources",
"//chrome/chrome_cleaner/strings:unittest_sources", "//chrome/chrome_cleaner/strings:unittest_sources",
# Dependencies of the test harness. # Dependencies of the test harness.
......
...@@ -117,6 +117,11 @@ const char kSandboxMojoPipeTokenSwitch[] = "sandbox-mojo-pipe-token"; ...@@ -117,6 +117,11 @@ const char kSandboxMojoPipeTokenSwitch[] = "sandbox-mojo-pipe-token";
// to be spawned. // to be spawned.
const char kSandboxedProcessIdSwitch[] = "sandboxed-process-id"; const char kSandboxedProcessIdSwitch[] = "sandboxed-process-id";
// Used to limit trace locations that will be scanned. Trace locations should be
// specified as integers, separated by commas. For example:
// --scan-locations=1,2,3,5
const char kScanLocationsSwitch[] = "scan-locations";
// Set the timeout for the scanning phase, in minutes. 0 disables the timeout // Set the timeout for the scanning phase, in minutes. 0 disables the timeout
// entirely. // entirely.
const char kScanningTimeoutMinutesSwitch[] = "scanning-timeout"; const char kScanningTimeoutMinutesSwitch[] = "scanning-timeout";
......
...@@ -34,6 +34,7 @@ extern const char kRemoveScanOnlyUwS[]; ...@@ -34,6 +34,7 @@ extern const char kRemoveScanOnlyUwS[];
extern const char kReportExtensionsSwitch[]; extern const char kReportExtensionsSwitch[];
extern const char kSandboxMojoPipeTokenSwitch[]; extern const char kSandboxMojoPipeTokenSwitch[];
extern const char kSandboxedProcessIdSwitch[]; extern const char kSandboxedProcessIdSwitch[];
extern const char kScanLocationsSwitch[];
extern const char kScanningTimeoutMinutesSwitch[]; extern const char kScanningTimeoutMinutesSwitch[];
extern const char kTestLoggingURLSwitch[]; extern const char kTestLoggingURLSwitch[];
extern const char kTestingSwitch[]; extern const char kTestingSwitch[];
......
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
source_set("resources") {
sources = [
"engine_resources.h",
]
public_deps = [
"//chrome/chrome_cleaner/logging/proto:shared_data_proto",
]
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/engines/engine_resources.h"
namespace chrome_cleaner {
std::string GetEngineVersion(Engine::Name engine) {
return std::string();
}
int GetProtectedFilesDigestResourceId() {
return 0;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_ENGINES_ENGINE_RESOURCES_H_
#define CHROME_CHROME_CLEANER_ENGINES_ENGINE_RESOURCES_H_
#include <string>
#include "chrome/chrome_cleaner/logging/proto/shared_data.pb.h"
namespace chrome_cleaner {
// Returns string representation of the engine's version or an empty string if
// not available.
std::string GetEngineVersion(Engine::Name engine);
// Returns ID of the "TEXT" resource that contains serialized FileDigests
// message (see file_digest.proto) of protected files. Returns zero if not
// available.
int GetProtectedFilesDigestResourceId();
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_ENGINES_ENGINE_RESOURCES_H_
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
source_set("settings_types") {
sources = [
"settings_types.h",
]
}
static_library("settings") {
sources = [
"settings.cc",
"settings.h",
]
deps = [
":settings_types",
"//base:base",
"//chrome/chrome_cleaner/constants:common_strings",
"//chrome/chrome_cleaner/engines:resources",
"//components/chrome_cleaner/public/constants:constants",
]
public_deps = [
"//chrome/chrome_cleaner/logging/proto:shared_data_proto",
"//mojo/public/cpp/platform",
]
}
static_library("matching_options") {
sources = [
"matching_options.cc",
"matching_options.h",
]
}
static_library("default_matching_options") {
sources = [
"default_matching_options.cc",
"default_matching_options.h",
]
deps = [
":settings",
]
public_deps = [
":matching_options",
]
}
source_set("unittest_sources") {
testonly = true
sources = [
"cleaner_settings_unittest.cc",
"other_settings_definitions.cc",
"reporter_settings_unittest.cc",
"settings_unittest.cc",
]
deps = [
":settings",
"//base:base",
"//base/test:test_support",
"//chrome/chrome_cleaner/constants:common_strings",
"//chrome/chrome_cleaner/test:test_util",
"//components/chrome_cleaner/public/constants:constants",
"//testing/gtest",
]
}
include_rules = [
"+components/chrome_cleaner",
"+mojo/public",
]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Provides the target binary to be used by the cleaner executable.
#include "chrome/chrome_cleaner/settings/settings_definitions.h"
namespace chrome_cleaner {
TargetBinary GetTargetBinary() {
return TargetBinary::kCleaner;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/settings/settings.h"
#include <tuple>
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/chrome_cleaner/constants/chrome_cleaner_switches.h"
#include "chrome/chrome_cleaner/test/test_name_helper.h"
#include "components/chrome_cleaner/public/constants/constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_cleaner {
// Test params:
// - execution_mode (legacy, scanning, or cleanup mode)
// - with_scanning_mode_logs: whether switch --with-scanning-mode-logs is
// present on the command line.
// - with_cleanup_mode_logs: whether switch --with-cleanup-mode-logs is
// present on the command line.
// - uploading_blocked: whether switch --no-report-upload is present on the
// command line.
class CleanerSettingsTest : public testing::TestWithParam<
std::tuple<ExecutionMode, bool, bool, bool>> {
protected:
Settings* ReinitializeSettings(const base::CommandLine& command_line) {
Settings* settings = Settings::GetInstance();
settings->Initialize(command_line, TargetBinary::kCleaner);
return settings;
}
// Test params.
ExecutionMode execution_mode_;
bool with_scanning_mode_logs_;
bool with_cleanup_mode_logs_;
bool uploading_blocked_;
};
TEST_P(CleanerSettingsTest, CleanerLogsPermissions) {
std::tie(execution_mode_, with_scanning_mode_logs_, with_cleanup_mode_logs_,
uploading_blocked_) = GetParam();
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
if (execution_mode_ != ExecutionMode::kNone) {
command_line.AppendSwitchASCII(
kExecutionModeSwitch,
base::IntToString(static_cast<int>(execution_mode_)));
}
if (with_scanning_mode_logs_)
command_line.AppendSwitch(kWithScanningModeLogsSwitch);
if (with_cleanup_mode_logs_)
command_line.AppendSwitch(kWithCleanupModeLogsSwitch);
if (uploading_blocked_)
command_line.AppendSwitch(kNoReportUploadSwitch);
Settings* settings = ReinitializeSettings(command_line);
bool expect_logs_upload_enabled =
(execution_mode_ == ExecutionMode::kScanning &&
with_scanning_mode_logs_) ||
(execution_mode_ == ExecutionMode::kCleanup && with_cleanup_mode_logs_);
EXPECT_EQ(expect_logs_upload_enabled, settings->logs_collection_enabled());
const bool logs_upload_allowed =
expect_logs_upload_enabled && !uploading_blocked_;
EXPECT_EQ(logs_upload_allowed, settings->logs_upload_allowed());
}
INSTANTIATE_TEST_CASE_P(
All,
CleanerSettingsTest,
::testing::Combine(
/*execution_mode=*/::testing::Values(ExecutionMode::kNone,
ExecutionMode::kScanning,
ExecutionMode::kCleanup),
/*with_scanning_mode_logs=*/::testing::Bool(),
/*with_cleanup_mode_logs=*/::testing::Bool(),
/*uploading_blocked=*/::testing::Bool()),
GetParamNameForTest());
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/settings/default_matching_options.h"
#include "chrome/chrome_cleaner/settings/settings.h"
namespace chrome_cleaner {
// For the cleaner we should always perform full scan so we know exactly what is
// found, even for report only UwS, and we should always look for incomplete
// matches.
MatchingOptions DefaultCleanerMatchingOptions() {
MatchingOptions options;
options.set_only_one_footprint(false);
options.set_find_incomplete_matches(true);
return options;
}
// For the reporter, we should only perform a full scan if we report back to
// Google what files and registry entries were matched. Otherwise, we should
// stop looking as soon as any piece of the UwS is found, since we just care if
// it is present. In addition, we should never look for incomplete matches,
// since they will not be available anyway in logs.
MatchingOptions DefaultReporterMatchingOptions() {
MatchingOptions options;
options.set_only_one_footprint(
!Settings::GetInstance()->logs_collection_enabled());
options.set_find_incomplete_matches(false);
return options;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_SETTINGS_DEFAULT_MATCHING_OPTIONS_H_
#define CHROME_CHROME_CLEANER_SETTINGS_DEFAULT_MATCHING_OPTIONS_H_
#include "chrome/chrome_cleaner/settings/matching_options.h"
namespace chrome_cleaner {
// Return the default MatchingOptions for the cleaner and the reporter.
MatchingOptions DefaultCleanerMatchingOptions();
MatchingOptions DefaultReporterMatchingOptions();
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_SETTINGS_DEFAULT_MATCHING_OPTIONS_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/settings/matching_options.h"
namespace chrome_cleaner {
bool MatchingOptions::only_one_footprint() const {
return only_one_footprint_;
}
void MatchingOptions::set_only_one_footprint(bool only_one_footprint) {
only_one_footprint_ = only_one_footprint;
}
bool MatchingOptions::find_incomplete_matches() const {
return find_incomplete_matches_;
}
void MatchingOptions::set_find_incomplete_matches(
bool find_incomplete_matches) {
find_incomplete_matches_ = find_incomplete_matches;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_SETTINGS_MATCHING_OPTIONS_H_
#define CHROME_CHROME_CLEANER_SETTINGS_MATCHING_OPTIONS_H_
namespace chrome_cleaner {
class MatchingOptions {
public:
bool only_one_footprint() const;
void set_only_one_footprint(bool only_one_footprint);
bool find_incomplete_matches() const;
void set_find_incomplete_matches(bool find_incomplete_matches);
private:
bool only_one_footprint_ = false;
bool find_incomplete_matches_ = false;
};
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_SETTINGS_MATCHING_OPTIONS_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Provides the target binary to be used by executables other than the reporter
// and cleaner.
#include "chrome/chrome_cleaner/settings/settings_definitions.h"
namespace chrome_cleaner {
TargetBinary GetTargetBinary() {
return TargetBinary::kOther;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Provides the target binary to be used by the reporter executable.
#include "chrome/chrome_cleaner/settings/settings_definitions.h"
namespace chrome_cleaner {
TargetBinary GetTargetBinary() {
return TargetBinary::kReporter;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/settings/settings.h"
#include "base/command_line.h"
#include "chrome/chrome_cleaner/constants/chrome_cleaner_switches.h"
#include "components/chrome_cleaner/public/constants/constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_cleaner {
class ReporterSettingsTest : public testing::Test {
protected:
Settings* ReinitializeSettings(const base::CommandLine& command_line) {
Settings* settings = Settings::GetInstance();
settings->Initialize(command_line, TargetBinary::kReporter);
return settings;
}
};
TEST_F(ReporterSettingsTest, ReporterLogsPermissions) {
for (bool sber : {false, true}) {
for (bool uploading_enabled : {false, true}) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
if (sber)
command_line.AppendSwitch(kExtendedSafeBrowsingEnabledSwitch);
if (!uploading_enabled)
command_line.AppendSwitch(kNoReportUploadSwitch);
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(sber, settings->logs_collection_enabled());
EXPECT_EQ(sber && uploading_enabled, settings->logs_upload_allowed());
}
}
}
} // namespace chrome_cleaner
This diff is collapsed.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_H_
#define CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_H_
#include <windows.h>
#include <string>
#include <unordered_map>
#include "base/command_line.h"
#include "base/memory/singleton.h"
#include "base/strings/string16.h"
#include "chrome/chrome_cleaner/logging/proto/shared_data.pb.h"
#include "chrome/chrome_cleaner/settings/settings_definitions.h"
#include "components/chrome_cleaner/public/constants/constants.h"
namespace chrome_cleaner {
// Read the given switch as a time in minutes. If it's missing or invalid,
// return false to use the default timeout. Otherwise return true and put the
// time in |timeout|.
bool GetTimeoutOverride(const base::CommandLine& command_line,
const char* switch_name,
base::TimeDelta* timeout);
std::vector<UwS::TraceLocation> GetValidTraceLocations();
class Settings {
public:
static Settings* GetInstance();
static void SetInstanceForTesting(Settings* instance_for_testing);
// Returns true if uploading crash reports is allowed. Crash reports are
// always saved on disk regardless of this setting.
virtual bool allow_crash_report_upload() const;
// Returns the session id for this run as passed by Chrome to the reporter.
virtual base::string16 session_id() const;
virtual std::string cleanup_id() const;
// Returns the engine to use as passed by Chrome.
virtual Engine::Name engine() const;
virtual std::string engine_version() const;
// Returns true if logs uploading is allowed for this runs (can be used by
// both the Cleaner and the Reporter).
virtual bool logs_upload_allowed() const;
// Returns true if logs collection is enabled for this run. When true, data
// will be saved to a proto that can be saved to disk or sent to Google if
// logs upload is allowed. Integration tests and automated evaluations are
// expected to run the reporter with logs collection enabled and logs upload
// disallowed.
virtual bool logs_collection_enabled() const;
// Returns true if the cleaner running in kCleanup mode will be allowed to
// upload logs to Google. This setting is only used by the cleaner in
// kScanning mode, which will set according to the user's response in the
// Chrome prompt and will be used to propagate the flag that enables logging
// to the process in kCleanup mode.
virtual bool logs_allowed_in_cleanup_mode() const;
virtual void set_logs_allowed_in_cleanup_mode(bool new_value);
// Returns true if metrics reporting is enabled for the user.
virtual bool metrics_enabled() const;
// Returns true if Safe Browsing extended reporting is enabled for the user.
virtual bool sber_enabled() const;
virtual const std::string& chrome_mojo_pipe_token() const;
virtual bool has_parent_pipe_handle() const;
// Returns the execution mode sent by Chrome if valid, or kNone if
// kExecutionModeSwitch is not present or the corresponding value is invalid.
virtual ExecutionMode execution_mode() const;
// Returns true if experimental engine should remove report-only UwS.
virtual bool remove_report_only_uws() const;
// Returns true if the timeout for the cleaning phase has been overridden on
// the command-line.
virtual bool cleaning_timeout_overridden() const;
// If the timeout for the cleaning phase has been overridden, return the new
// timeout. Otherwise returns TimeDelta(0). Note that this function cannot be
// used to determine if the timout value has been overridden since overriding
// with a value of zero is indistinguishable from not having an override; use
// cleaning_timeout_overridden() for that purpose.
virtual base::TimeDelta cleaning_timeout() const;
// Returns true if the timeout for the scanning phase has been overridden on
// the command-line.
virtual bool scanning_timeout_overridden() const;
// If the timeout for the scanning phase has been overridden, return the new
// timeout. Otherwise returns TimeDelta(0). Note that this function cannot be
// used to determine if the timout value has been overridden since overriding
// with a value of zero is indistinguishable from not having an override; use
// scanning_timeout_overridden() for that purpose.
virtual base::TimeDelta scanning_timeout() const;
// Returns true if the timeout for how long the scanner waits for user
// response from Chrome has been overridden on the command-line.
virtual bool user_response_timeout_overridden() const;
// If the timeout for how long the scanner waits for user response from Chrome
// has been overridden, return the new timeout. Otherwise returns
// TimeDelta(0). Note that this function cannot be used to determine if the
// timout value has been overridden since overriding with a value of zero is
// indistinguishable from not having an override; use
// user_response_timeout_overridden() for that purpose.
virtual base::TimeDelta user_response_timeout() const;
// Returns list of trace locations, to which scanning should be limited.
virtual const std::vector<UwS::TraceLocation>& locations_to_scan() const;
// Returns true if no invalid values were provided on the command line to
// scanning configuration switches. If there were invalid arguments, returns
// false, but other settings properties will be set to known safe defaults.
virtual bool scan_switches_correct() const;
protected:
Settings();
virtual ~Settings();
private:
friend struct base::DefaultSingletonTraits<Settings>;
friend class CleanerSettingsTest;
friend class ReporterSettingsTest;
friend class SettingsTest;
// This method is separate from the constructor, so it can be called by tests.
void Initialize(const base::CommandLine& command_line,
TargetBinary target_binary);
// Crash related settings.
bool allow_crash_report_upload_ = false;
// Logging related settings.
bool logs_collection_enabled_ = false;
bool logs_upload_allowed_ = false;
bool metrics_enabled_ = false;
bool sber_enabled_ = false;
bool logs_allowed_in_cleanup_mode_ = false;
// Statistics about the current run.
std::string cleanup_id_;
base::string16 session_id_;
// Execution parameters.
ExecutionMode execution_mode_ = ExecutionMode::kNone;
bool remove_report_only_uws_ = false;
bool cleaning_timeout_overridden_ = false;
base::TimeDelta cleaning_timeout_;
bool scanning_timeout_overridden_ = false;
base::TimeDelta scanning_timeout_;
bool user_response_timeout_overridden_ = false;
base::TimeDelta user_response_timeout_;
std::vector<UwS::TraceLocation> locations_to_scan_;
bool scan_switches_correct_ = false;
// Mojo related settings.
std::string chrome_mojo_pipe_token_;
bool has_parent_pipe_handle_ = false;
// Engine selection settings.
Engine::Name engine_ = Engine::URZA;
static Settings* instance_for_testing_;
};
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_DEFINITIONS_H_
#define CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_DEFINITIONS_H_
// All executable targets build in this project should contain one
// *_settings_definitions.cc file to ensure that this functions are defined.
namespace chrome_cleaner {
// Indicates what type of Chrome Cleaner binary is running.
enum class TargetBinary {
kReporter,
kCleaner,
kOther,
};
TargetBinary GetTargetBinary();
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_DEFINITIONS_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_TYPES_H_
#define CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_TYPES_H_
namespace chrome_cleaner {
// Values to be passed to kSandboxType and that will be used to determine the
// type of sandbox that should be created on a certain process.
enum class SandboxType {
// Signal no sandbox.
kNonSandboxed = 0,
kTest,
kEset,
kNumValues,
};
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_SETTINGS_SETTINGS_TYPES_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/settings/settings.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/chrome_cleaner/constants/chrome_cleaner_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_cleaner {
// Number high enough that it will never be a valid engine value.
constexpr int kInvalidEngineValue = 3141592;
static_assert(kInvalidEngineValue < Engine::Name_MIN ||
kInvalidEngineValue > Engine::Name_MAX,
"kInvalidEngineValue is not an invalid value");
constexpr char kNonNumericValue[] = "foo";
class SettingsTest : public testing::Test {
protected:
Settings* ReinitializeSettings(const base::CommandLine& command_line) {
Settings* settings = Settings::GetInstance();
settings->Initialize(command_line, GetTargetBinary());
return settings;
}
void TestExecutionMode(const std::string& switch_value,
ExecutionMode expected_execution_mode) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
if (!switch_value.empty())
command_line.AppendSwitchASCII(kExecutionModeSwitch, switch_value);
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(expected_execution_mode, settings->execution_mode());
}
};
TEST_F(SettingsTest, EngineDefaultValue) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(Engine::URZA, settings->engine());
}
TEST_F(SettingsTest, ValidEngines) {
for (int index = Engine::Name_MIN; index < Engine::Name_MAX; ++index) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(chrome_cleaner::kEngineSwitch,
base::IntToString(index));
Settings* settings = ReinitializeSettings(command_line);
if (index != Engine::UNKNOWN)
EXPECT_EQ(static_cast<Engine::Name>(index), settings->engine());
else
EXPECT_EQ(Engine::URZA, settings->engine()); // Fallback to default.
}
}
TEST_F(SettingsTest, EngineInvalidNumericValue) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(chrome_cleaner::kEngineSwitch,
base::IntToString(kInvalidEngineValue));
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(Engine::URZA, settings->engine());
}
TEST_F(SettingsTest, EngineNonNumericValue) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(chrome_cleaner::kEngineSwitch,
kNonNumericValue);
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(Engine::URZA, settings->engine());
}
TEST_F(SettingsTest, CleanerRunId_Generated) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
Settings* settings = ReinitializeSettings(command_line);
// A non-empty cleanup id should be generated if switch --cleanup-id is not
// present.
EXPECT_FALSE(settings->cleanup_id().empty());
command_line.AppendSwitchASCII(chrome_cleaner::kCleanupIdSwitch,
std::string());
settings = ReinitializeSettings(command_line);
// A non-empty cleanup id should be generated if switch --cleanup-id is
// present, but the corresponding value is empty.
EXPECT_FALSE(settings->cleanup_id().empty());
}
TEST_F(SettingsTest, CleanerRunId_Propagated) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
const std::string propagated_cleanup_id = "propagated-run-id";
command_line.AppendSwitchASCII(chrome_cleaner::kCleanupIdSwitch,
propagated_cleanup_id);
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(propagated_cleanup_id, settings->cleanup_id());
}
TEST_F(SettingsTest, SBEREnabled) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
EXPECT_FALSE(ReinitializeSettings(command_line)->sber_enabled());
command_line.AppendSwitch(chrome_cleaner::kExtendedSafeBrowsingEnabledSwitch);
EXPECT_TRUE(ReinitializeSettings(command_line)->sber_enabled());
}
TEST_F(SettingsTest, ExecutionMode) {
const struct TestCase {
std::string switch_value;
ExecutionMode expected_execution_mode;
} test_cases[] = {
// Switch not present.
{"", ExecutionMode::kNone},
// Valid values for execution mode.
{base::IntToString(static_cast<int>(ExecutionMode::kScanning)),
ExecutionMode::kScanning},
{base::IntToString(static_cast<int>(ExecutionMode::kCleanup)),
ExecutionMode::kCleanup},
// Unknown values for execution mode.
{base::IntToString(static_cast<int>(ExecutionMode::kNumValues)),
ExecutionMode::kNone},
{base::IntToString(static_cast<int>(ExecutionMode::kNumValues) + 1),
ExecutionMode::kNone},
{base::IntToString(static_cast<int>(ExecutionMode::kNone) - 1),
ExecutionMode::kNone},
{"invalid-value", ExecutionMode::kNone},
};
for (auto t : test_cases) {
TestExecutionMode(t.switch_value, t.expected_execution_mode);
}
}
TEST_F(SettingsTest, Timeouts) {
using OverrideAccessor = bool (Settings::*)() const;
using TimeoutAccessor = base::TimeDelta (Settings::*)() const;
const struct TestCase {
std::string switch_value;
bool expected_timeout_overridden;
base::TimeDelta expected_timeout;
} test_cases[] = {
// Switch not present.
{"", false, base::TimeDelta()},
// Valid number of minutes.
{"100", true, base::TimeDelta::FromMinutes(100)},
// Disabled.
{"0", true, base::TimeDelta()},
// Invalid values.
{"ten", false, base::TimeDelta()},
{"1.5", false, base::TimeDelta()},
{"-3", false, base::TimeDelta()},
};
const struct SwitchesToTest {
std::string switch_name;
OverrideAccessor override_accessor;
TimeoutAccessor timeout_accessor;
} switches_to_test[] = {
{kCleaningTimeoutMinutesSwitch, &Settings::cleaning_timeout_overridden,
&Settings::cleaning_timeout},
{kScanningTimeoutMinutesSwitch, &Settings::scanning_timeout_overridden,
&Settings::scanning_timeout},
};
for (auto s : switches_to_test) {
SCOPED_TRACE(s.switch_name);
for (auto t : test_cases) {
SCOPED_TRACE(t.switch_value);
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
if (!t.switch_value.empty())
command_line.AppendSwitchASCII(s.switch_name, t.switch_value);
Settings* settings = ReinitializeSettings(command_line);
bool timeout_overridden = (settings->*(s.override_accessor))();
base::TimeDelta timeout = (settings->*(s.timeout_accessor))();
EXPECT_EQ(t.expected_timeout_overridden, timeout_overridden);
EXPECT_EQ(t.expected_timeout, timeout);
}
}
}
TEST_F(SettingsTest, NoLocationsToScanSpecified) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
Settings* settings = ReinitializeSettings(command_line);
// All valid scan locations should be specified, but let's not check the exact
// contents and size. It's bigger than 1 location for sure.
EXPECT_LT(1u, settings->locations_to_scan().size());
EXPECT_TRUE(settings->scan_switches_correct());
}
TEST_F(SettingsTest, LimitsToSpecifiedLocations) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(kScanLocationsSwitch, "1,2,3");
std::vector<UwS::TraceLocation> expected_locations = {
UwS::FOUND_IN_STARTUP, UwS::FOUND_IN_MEMORY,
UwS::FOUND_IN_UNINSTALLSTRING,
};
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(expected_locations, settings->locations_to_scan());
EXPECT_TRUE(settings->scan_switches_correct());
}
TEST_F(SettingsTest, UsesOnlyValidSpecifiedLocations) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(kScanLocationsSwitch, "1,100,yada,-3");
std::vector<UwS::TraceLocation> expected_locations = {UwS::FOUND_IN_STARTUP};
Settings* settings = ReinitializeSettings(command_line);
EXPECT_EQ(expected_locations, settings->locations_to_scan());
EXPECT_FALSE(settings->scan_switches_correct());
}
TEST_F(SettingsTest, OnlyInvalidLocationsSpecified) {
const std::string test_cases[] = {
"", // empty flag value
"100,102,-3", // only invalid values specified
};
for (const std::string& switch_value : test_cases) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitchASCII(kScanLocationsSwitch, switch_value);
Settings* settings = ReinitializeSettings(command_line);
// All valid scan locations should be specified.
EXPECT_LT(1u, settings->locations_to_scan().size());
EXPECT_FALSE(settings->scan_switches_correct());
}
}
} // namespace chrome_cleaner
...@@ -2,6 +2,18 @@ ...@@ -2,6 +2,18 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
static_library("test_util") {
testonly = true
sources = [
"test_name_helper.cc",
]
deps = [
"//testing/gtest",
]
}
source_set("test_branding_header") { source_set("test_branding_header") {
# This is NOT testonly because it's used by RegistryOverrideManager, which is # This is NOT testonly because it's used by RegistryOverrideManager, which is
# shipped on official builds. # shipped on official builds.
......
include_rules = [
"+components/chrome_cleaner",
]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/test/test_name_helper.h"
#include <ostream>
namespace chrome_cleaner {
std::ostream& operator<<(std::ostream& stream, ExecutionMode mode) {
switch (mode) {
case ExecutionMode::kNone:
stream << "kNone";
break;
case ExecutionMode::kScanning:
stream << "kScanning";
break;
case ExecutionMode::kCleanup:
stream << "kCleanup";
break;
default:
stream << mode;
}
return stream;
}
} // namespace chrome_cleaner
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_CHROME_CLEANER_TEST_TEST_NAME_HELPER_H_
#define CHROME_CHROME_CLEANER_TEST_TEST_NAME_HELPER_H_
#include <stdint.h>
#include <string>
#include "base/strings/string_util.h"
#include "components/chrome_cleaner/public/constants/constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_cleaner {
// A functor that formats test parameters for use in a test name. Use this
// instead of PrintToStringParamName, which sometimes returns characters that
// aren't valid in test names.
//
// Known limitations:
//
// * A char* initialized with "string value" is formatted as
// ADDR_pointer_to_string_value. To avoid this, declare the parameter with type
// std::string. Example: instead of "TestWithParam<std::tuple<const char*,
// bool>>" use "TestWithParam<std::tuple<std::string, bool>>".
//
// * A string initialized with the literal L"wide string" is formatted as
// Lwide_string. No known workaround.
struct GetParamNameForTest {
template <typename ParamType>
std::string operator()(
const ::testing::TestParamInfo<ParamType>& info) const {
std::string param_name = ::testing::PrintToString(info.param);
// Remove or convert invalid characters that are inserted by PrintToString:
//
// * Strings formatted as "string value" (including quotes) -> string_value
// * Tuples formatted as (value1, value2) -> value1_value2
// * Mojo enums formatted as Enum::VALUE -> EnumVALUE
base::RemoveChars(param_name, "\"(),:", &param_name);
base::ReplaceChars(param_name, " ", "_", &param_name);
return param_name;
}
};
std::ostream& operator<<(std::ostream& stream, ExecutionMode mode);
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_TEST_TEST_NAME_HELPER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment