1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/chrome_cleaner/settings/settings.h"
6 
7 #include <tuple>
8 
9 #include "base/command_line.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/chrome_cleaner/buildflags.h"
12 #include "chrome/chrome_cleaner/constants/chrome_cleaner_switches.h"
13 #include "components/chrome_cleaner/public/constants/constants.h"
14 #include "components/chrome_cleaner/test/test_name_helper.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 namespace chrome_cleaner {
18 
19 // Test params:
20 //  - execution_mode (legacy, scanning, or cleanup mode)
21 //  - with_scanning_mode_logs: whether switch --with-scanning-mode-logs is
22 //        present on the command line.
23 //  - with_cleanup_mode_logs: whether switch --with-cleanup-mode-logs is
24 //        present on the command line.
25 //  - with_test_logging_url: whether switch --test_logging_url is present on the
26 //        command line.
27 //  - uploading_blocked: whether switch --no-report-upload is present on the
28 //        command line.
29 class CleanerSettingsTest
30     : public testing::TestWithParam<
31           std::tuple<ExecutionMode, bool, bool, bool, bool>> {
32  protected:
ReinitializeSettings(const base::CommandLine & command_line)33   Settings* ReinitializeSettings(const base::CommandLine& command_line) {
34     Settings* settings = Settings::GetInstance();
35     settings->Initialize(command_line, TargetBinary::kCleaner);
36     return settings;
37   }
38 
39   // Test params.
40   ExecutionMode execution_mode_;
41   bool with_scanning_mode_logs_;
42   bool with_cleanup_mode_logs_;
43   bool with_test_logging_url_;
44   bool uploading_blocked_;
45 };
46 
TEST_P(CleanerSettingsTest,CleanerLogsPermissions)47 TEST_P(CleanerSettingsTest, CleanerLogsPermissions) {
48   std::tie(execution_mode_, with_scanning_mode_logs_, with_cleanup_mode_logs_,
49            with_test_logging_url_, uploading_blocked_) = GetParam();
50 
51   base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
52   if (execution_mode_ != ExecutionMode::kNone) {
53     command_line.AppendSwitchASCII(
54         kExecutionModeSwitch,
55         base::NumberToString(static_cast<int>(execution_mode_)));
56   }
57   if (with_scanning_mode_logs_)
58     command_line.AppendSwitch(kWithScanningModeLogsSwitch);
59   if (with_cleanup_mode_logs_)
60     command_line.AppendSwitch(kWithCleanupModeLogsSwitch);
61   if (with_test_logging_url_)
62     command_line.AppendSwitch(kTestLoggingURLSwitch);
63   if (uploading_blocked_)
64     command_line.AppendSwitch(kNoReportUploadSwitch);
65 
66   Settings* settings = ReinitializeSettings(command_line);
67 
68   const bool expect_logs_collection_enabled =
69       (execution_mode_ == ExecutionMode::kScanning &&
70        with_scanning_mode_logs_) ||
71       (execution_mode_ == ExecutionMode::kCleanup && with_cleanup_mode_logs_);
72   EXPECT_EQ(expect_logs_collection_enabled,
73             settings->logs_collection_enabled());
74 
75   bool logs_upload_allowed =
76       expect_logs_collection_enabled && !uploading_blocked_;
77 #if !BUILDFLAG(IS_OFFICIAL_CHROME_CLEANER_BUILD)
78   if (!with_test_logging_url_)
79     logs_upload_allowed = false;
80 #endif
81   EXPECT_EQ(logs_upload_allowed, settings->logs_upload_allowed());
82 }
83 
84 INSTANTIATE_TEST_SUITE_P(
85     All,
86     CleanerSettingsTest,
87     ::testing::Combine(
88         /*execution_mode=*/::testing::Values(ExecutionMode::kNone,
89                                              ExecutionMode::kScanning,
90                                              ExecutionMode::kCleanup),
91         /*with_scanning_mode_logs=*/::testing::Bool(),
92         /*with_cleanup_mode_logs=*/::testing::Bool(),
93         /*with_test_logging_url=*/::testing::Bool(),
94         /*uploading_blocked=*/::testing::Bool()),
95     GetParamNameForTest());
96 
97 }  // namespace chrome_cleaner
98