Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" | 5 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" | |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/test/scoped_feature_list.h" | 15 #include "base/test/scoped_feature_list.h" |
| 16 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/version.h" | |
| 18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/lifetime/keep_alive_types.h" | |
| 21 #include "chrome/browser/lifetime/scoped_keep_alive.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/safe_browsing/srt_client_info_win.h" | 23 #include "chrome/browser/safe_browsing/srt_client_info_win.h" |
| 21 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" | 25 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
| 25 #include "components/component_updater/pref_names.h" | 28 #include "components/component_updater/pref_names.h" |
| 26 #include "components/prefs/pref_service.h" | 29 #include "components/prefs/pref_service.h" |
| 27 #include "components/safe_browsing_db/safe_browsing_prefs.h" | 30 #include "components/safe_browsing_db/safe_browsing_prefs.h" |
| 28 #include "content/public/test/test_browser_thread_bundle.h" | |
| 29 | 31 |
| 30 namespace safe_browsing { | 32 namespace safe_browsing { |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 const char* const kExpectedSwitches[] = {kExtendedSafeBrowsingEnabledSwitch, | 36 const char* const kExpectedSwitches[] = {kExtendedSafeBrowsingEnabledSwitch, |
| 35 kChromeVersionSwitch, | 37 kChromeVersionSwitch, |
| 36 kChromeChannelSwitch}; | 38 kChromeChannelSwitch}; |
| 37 | 39 |
| 38 class SRTFetcherTest : public InProcessBrowserTest, | 40 class SRTFetcherTest : public InProcessBrowserTest, |
| 39 public SwReporterTestingDelegate { | 41 public SwReporterTestingDelegate { |
| 40 public: | 42 public: |
| 41 void SetUpInProcessBrowserTestFixture() override { | 43 void SetUpInProcessBrowserTestFixture() override { |
| 42 task_runner_ = new base::TestSimpleTaskRunner; | |
| 43 | |
| 44 SetSwReporterTestingDelegate(this); | 44 SetSwReporterTestingDelegate(this); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SetUpOnMainThread() override { | 47 void SetUpOnMainThread() override { |
| 48 scoped_task_runner_ = | |
| 49 std::make_unique<base::ScopedMockTimeMessageLoopTaskRunner>(); | |
|
gab
2017/02/21 22:15:36
std::make_unique is C++14, you still need to use b
Joe Mason
2017/02/22 04:43:37
Huh, I've been using std::make_unique a lot. Surpr
| |
| 48 InProcessBrowserTest::SetUpOnMainThread(); | 50 InProcessBrowserTest::SetUpOnMainThread(); |
| 51 | |
| 52 // Move the simulated clock ahead far enough that times in the past won't | |
| 53 // underflow. | |
|
gab
2017/02/21 22:15:36
I don't understand this?
Joe Mason
2017/02/22 04:43:37
Reworded. Better?
| |
| 54 task_runner()->FastForwardBy( | |
| 55 base::TimeDelta::FromDays(kDaysBetweenSuccessfulSwReporterRuns * 2)); | |
| 56 | |
| 49 ClearLastTimeSentReport(); | 57 ClearLastTimeSentReport(); |
| 50 } | 58 } |
| 51 | 59 |
| 60 void TearDownOnMainThread() override { | |
| 61 // ScopedMockTimeMessageLoopTaskRunner's destructor re-posts pending tasks | |
| 62 // to the previous task runner. Get rid of them so they don't run after the | |
| 63 // test. | |
| 64 task_runner()->ClearPendingTasks(); | |
|
gab
2017/02/21 22:15:36
It's weird to drop tasks, can we run remaining non
Joe Mason
2017/02/22 04:43:37
The problem is this line here:
https://cs.chromiu
gab
2017/02/22 17:19:48
Hmmm, I doubt this is true? i.e. I don't think del
| |
| 65 | |
| 66 // Restore the standard task runner to perform browser cleanup, which posts | |
| 67 // some tasks with pending times but does not advance | |
| 68 // ScopedMockTimeMessageLoopTaskRunner's mock clock. | |
| 69 scoped_task_runner_.reset(); | |
| 70 } | |
| 71 | |
| 52 void TearDownInProcessBrowserTestFixture() override { | 72 void TearDownInProcessBrowserTestFixture() override { |
| 53 SetSwReporterTestingDelegate(nullptr); | 73 SetSwReporterTestingDelegate(nullptr); |
| 54 } | 74 } |
| 55 | 75 |
| 76 base::TestMockTimeTaskRunner* task_runner() const { | |
| 77 DCHECK(scoped_task_runner_); | |
| 78 return scoped_task_runner_->task_runner(); | |
|
gab
2017/02/21 22:15:36
ScopedMockTimeMessageLoopTaskRunner overrides oper
Joe Mason
2017/02/22 04:43:37
Done.
Didn't notice that. Thanks!
| |
| 79 } | |
| 80 | |
| 56 void RunReporter(const base::FilePath& exe_path = base::FilePath()) { | 81 void RunReporter(const base::FilePath& exe_path = base::FilePath()) { |
| 57 auto invocation = SwReporterInvocation::FromFilePath(exe_path); | 82 auto invocation = SwReporterInvocation::FromFilePath(exe_path); |
| 58 invocation.supported_behaviours = | 83 invocation.supported_behaviours = |
| 59 SwReporterInvocation::BEHAVIOUR_LOG_TO_RAPPOR | | 84 SwReporterInvocation::BEHAVIOUR_LOG_TO_RAPPOR | |
| 60 SwReporterInvocation::BEHAVIOUR_LOG_EXIT_CODE_TO_PREFS | | 85 SwReporterInvocation::BEHAVIOUR_LOG_EXIT_CODE_TO_PREFS | |
| 61 SwReporterInvocation::BEHAVIOUR_TRIGGER_PROMPT | | 86 SwReporterInvocation::BEHAVIOUR_TRIGGER_PROMPT | |
| 62 SwReporterInvocation::BEHAVIOUR_ALLOW_SEND_REPORTER_LOGS; | 87 SwReporterInvocation::BEHAVIOUR_ALLOW_SEND_REPORTER_LOGS; |
| 63 | 88 |
| 64 SwReporterQueue invocations; | 89 SwReporterQueue invocations; |
| 65 invocations.push(invocation); | 90 invocations.push(invocation); |
| 66 RunSwReporters(invocations, base::Version("1.2.3"), task_runner_, | 91 RunSwReporters(invocations, base::Version("1.2.3")); |
| 67 task_runner_); | |
| 68 } | 92 } |
| 69 | 93 |
| 70 void RunReporterQueue(const SwReporterQueue& invocations) { | 94 void RunReporterQueue(const SwReporterQueue& invocations) { |
| 71 RunSwReporters(invocations, base::Version("1.2.3"), task_runner_, | 95 RunSwReporters(invocations, base::Version("1.2.3")); |
| 72 task_runner_); | |
| 73 } | 96 } |
| 74 | 97 |
| 75 void TriggerPrompt(Browser* browser, const std::string& version) override { | 98 void TriggerPrompt(Browser* browser, const std::string& version) override { |
| 76 prompt_trigger_called_ = true; | 99 prompt_trigger_called_ = true; |
| 77 } | 100 } |
| 78 | 101 |
| 79 int LaunchReporter(const SwReporterInvocation& invocation) override { | 102 int LaunchReporter(const SwReporterInvocation& invocation) override { |
| 80 ++reporter_launch_count_; | 103 ++reporter_launch_count_; |
| 81 reporter_launch_parameters_ = invocation; | 104 reporter_launch_parameters_.push_back(invocation); |
| 105 if (first_launch_callback_) { | |
| 106 // This is a OnceCallback so it will be cleared after running. | |
|
gab
2017/02/21 22:15:37
Don't think you need this, it'll become natural as
Joe Mason
2017/02/22 04:43:37
Done.
| |
| 107 std::move(first_launch_callback_).Run(); | |
| 108 } | |
| 82 return exit_code_to_report_; | 109 return exit_code_to_report_; |
| 83 } | 110 } |
| 84 | 111 |
| 85 void NotifyLaunchReady() override { launch_ready_notified_ = true; } | 112 base::Time Now() const override { return task_runner()->Now(); } |
| 86 | 113 |
| 87 void NotifyReporterDone() override { reporter_done_notified_ = true; } | 114 base::TaskRunner* BlockingTaskRunner() const override { |
| 115 return task_runner(); | |
| 116 } | |
| 88 | 117 |
| 89 // Sets |path| in the local state to a date corresponding to |days| days ago. | 118 // Sets |path| in the local state to a date corresponding to |days| days ago. |
| 90 void SetDateInLocalState(const std::string& path, int days) { | 119 void SetDateInLocalState(const std::string& path, int days) { |
| 91 PrefService* local_state = g_browser_process->local_state(); | 120 PrefService* local_state = g_browser_process->local_state(); |
| 92 DCHECK_NE(local_state, nullptr); | 121 DCHECK_NE(local_state, nullptr); |
| 93 local_state->SetInt64(path, | 122 local_state->SetInt64( |
| 94 (base::Time::Now() - base::TimeDelta::FromDays(days)) | 123 path, (Now() - base::TimeDelta::FromDays(days)).ToInternalValue()); |
| 95 .ToInternalValue()); | |
| 96 } | 124 } |
| 97 | 125 |
| 98 void SetDaysSinceLastReport(int days) { | 126 void SetDaysSinceLastReport(int days) { |
| 99 SetDateInLocalState(prefs::kSwReporterLastTimeTriggered, days); | 127 SetDateInLocalState(prefs::kSwReporterLastTimeTriggered, days); |
| 100 } | 128 } |
| 101 | 129 |
| 102 void ExpectToRunAgain(int days) { | 130 void ExpectToRunAgain(int days) { |
| 103 ASSERT_TRUE(task_runner_->HasPendingTask()); | 131 ASSERT_TRUE(task_runner()->HasPendingTask()); |
| 104 EXPECT_LE(task_runner_->NextPendingTaskDelay(), | 132 EXPECT_LE(task_runner()->NextPendingTaskDelay(), |
| 105 base::TimeDelta::FromDays(days)); | 133 base::TimeDelta::FromDays(days)); |
| 106 EXPECT_GT(task_runner_->NextPendingTaskDelay(), | 134 EXPECT_GT(task_runner()->NextPendingTaskDelay(), |
| 107 base::TimeDelta::FromDays(days) - base::TimeDelta::FromHours(1)); | 135 base::TimeDelta::FromDays(days) - base::TimeDelta::FromHours(1)); |
| 108 } | 136 } |
| 109 | 137 |
| 110 // Clears local state for last time the software reporter sent logs to |days| | 138 // Clears local state for last time the software reporter sent logs to |days| |
| 111 // days ago. This prevents potential false positives that could arise from | 139 // days ago. This prevents potential false positives that could arise from |
| 112 // state not properly cleaned between successive tests. | 140 // state not properly cleaned between successive tests. |
| 113 void ClearLastTimeSentReport() { | 141 void ClearLastTimeSentReport() { |
| 114 DCHECK_NE(g_browser_process, nullptr); | 142 DCHECK_NE(g_browser_process, nullptr); |
| 115 PrefService* local_state = g_browser_process->local_state(); | 143 PrefService* local_state = g_browser_process->local_state(); |
| 116 DCHECK_NE(local_state, nullptr); | 144 DCHECK_NE(local_state, nullptr); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 133 void ExpectLastTimeSentReportNotSet() { | 161 void ExpectLastTimeSentReportNotSet() { |
| 134 PrefService* local_state = g_browser_process->local_state(); | 162 PrefService* local_state = g_browser_process->local_state(); |
| 135 DCHECK_NE(local_state, nullptr); | 163 DCHECK_NE(local_state, nullptr); |
| 136 EXPECT_FALSE( | 164 EXPECT_FALSE( |
| 137 local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport)); | 165 local_state->HasPrefPath(prefs::kSwReporterLastTimeSentReport)); |
| 138 } | 166 } |
| 139 | 167 |
| 140 void ExpectLastReportSentInTheLastHour() { | 168 void ExpectLastReportSentInTheLastHour() { |
| 141 const PrefService* local_state = g_browser_process->local_state(); | 169 const PrefService* local_state = g_browser_process->local_state(); |
| 142 DCHECK_NE(local_state, nullptr); | 170 DCHECK_NE(local_state, nullptr); |
| 143 const base::Time now = base::Time::Now(); | 171 const base::Time now = Now(); |
| 144 const base::Time last_time_sent_logs = base::Time::FromInternalValue( | 172 const base::Time last_time_sent_logs = base::Time::FromInternalValue( |
| 145 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport)); | 173 local_state->GetInt64(prefs::kSwReporterLastTimeSentReport)); |
| 146 | 174 |
| 147 // Checks if the last time sent logs is set as no more than one hour ago, | 175 // Checks if the last time sent logs is set as no more than one hour ago, |
| 148 // which should be enough time if the execution does not fail. | 176 // which should be enough time if the execution does not fail. |
| 149 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs); | 177 EXPECT_LT(now - base::TimeDelta::FromHours(1), last_time_sent_logs); |
| 150 EXPECT_LT(last_time_sent_logs, now); | 178 EXPECT_LE(last_time_sent_logs, now); |
| 151 } | 179 } |
| 152 | 180 |
| 153 // Run through the steps needed to launch the reporter, as many times as | 181 // Expects that after |days_until_launched| days, the reporter will be |
| 154 // needed to launch all the reporters given in |expected_launch_paths|. Test | 182 // launched // |expected_launch_count| times, and TriggerPrompt will be |
| 155 // that each of those launches succeeded. But do not test that ONLY those | 183 // called if and only if |expect_prompt| is true. |
| 156 // launches succeeded. | 184 void ExpectReporterLaunches(int days_until_launch, |
| 157 // | 185 int expected_launch_count, |
| 158 // After this, if more launches are expected you can call | 186 bool expect_prompt) { |
| 159 // |TestPartialLaunchCycle| again with another list of paths, to test that | 187 ASSERT_TRUE(task_runner()->HasPendingTask()); |
| 160 // the launch cycle will continue with those paths. | 188 reporter_launch_count_ = 0; |
| 161 // | 189 reporter_launch_parameters_.clear(); |
| 162 // To test that a list of paths are launched AND NO OTHERS, use | 190 prompt_trigger_called_ = false; |
| 163 // |TestReporterLaunchCycle|. | |
| 164 void TestPartialLaunchCycle( | |
| 165 const std::vector<base::FilePath>& expected_launch_paths) { | |
| 166 // This test has an unfortunate amount of knowledge of the internals of | |
| 167 // ReporterRunner, because it needs to pump the right message loops at the | |
| 168 // right time so that all its internal messages are delivered. This | |
| 169 // function might need to be updated if the internals change. | |
| 170 // | |
| 171 // The basic sequence is: | |
| 172 // | |
| 173 // 1. TryToRun kicks the whole thing off. If the reporter should not be | |
| 174 // launched now (eg. DaysSinceLastReport is too low) it posts a call to | |
| 175 // itself again. (In a regular task runner this will be scheduled with a | |
| 176 // delay, but the test task runner ignores delays so TryToRun will be | |
| 177 // called again on the next call to RunPendingTasks.) | |
| 178 // | |
| 179 // 2. When it is time to run a reporter, TryToRun calls NotifyLaunchReady | |
| 180 // and then posts a call to LaunchAndWait. | |
| 181 // | |
| 182 // 3. When the reporter returns, a call to ReporterDone is posted on the UI | |
| 183 // thread. | |
| 184 // | |
| 185 // 4. ReporterDone calls NotifyReporterDone and then posts another call to | |
| 186 // TryToRun, which starts the whole process over for the next run. | |
| 187 // | |
| 188 // Each call to RunPendingTasks only handles messages already on the queue. | |
| 189 // It doesn't handle messages posted by those messages. So, we need to call | |
| 190 // it in a loop to make sure we're past all pending TryToRun calls before | |
| 191 // LaunchAndWaitForExit will be called. | |
| 192 // | |
| 193 // Once a call to LaunchAndWaitForExit has been posted, TryToRun won't be | |
| 194 // called again until we pump the UI message loop in order to run | |
| 195 // ReporterDone. | |
| 196 | 191 |
| 197 ASSERT_TRUE(task_runner_->HasPendingTask()); | 192 task_runner()->FastForwardBy(base::TimeDelta::FromDays(days_until_launch)); |
| 198 ASSERT_FALSE(reporter_done_notified_); | |
| 199 | 193 |
| 200 reporter_launch_count_ = 0; | 194 EXPECT_EQ(reporter_launch_count_, expected_launch_count); |
| 201 reporter_launch_parameters_ = SwReporterInvocation(); | 195 EXPECT_EQ(prompt_trigger_called_, expect_prompt); |
| 196 } | |
| 202 | 197 |
| 203 int current_launch_count = reporter_launch_count_; | 198 // Expects that after |days_until_launched| days, the reporter will be |
| 204 for (const auto& expected_launch_path : expected_launch_paths) { | 199 // launched once with each path in |expected_launch_paths|, and TriggerPrompt |
| 205 // If RunReporter was called with no pending messages, and it was already | 200 // will be called if and only if |expect_prompt| is true. |
| 206 // time to launch the reporter, then |launch_ready_notified_| will | 201 void ExpectReporterLaunches( |
| 207 // already be true. Otherwise there will be a TryToRun message pending, | 202 int days_until_launch, |
| 208 // which must be processed first. | 203 const std::vector<base::FilePath>& expected_launch_paths, |
| 209 if (!launch_ready_notified_) { | 204 bool expect_prompt) { |
| 210 task_runner_->RunPendingTasks(); | 205 ExpectReporterLaunches(days_until_launch, expected_launch_paths.size(), |
| 211 // Since we're expecting a launch here, we expect it to schedule | 206 expect_prompt); |
| 212 // LaunchAndWaitForExit. So NOW |launch_ready_notified_| should be | 207 ASSERT_EQ(reporter_launch_parameters_.size(), expected_launch_paths.size()); |
| 213 // true. | 208 for (size_t i = 0; i < expected_launch_paths.size(); ++i) { |
| 214 ASSERT_TRUE(task_runner_->HasPendingTask()); | 209 EXPECT_EQ(expected_launch_paths[i], |
| 215 } | 210 reporter_launch_parameters_[i].command_line.GetProgram()); |
| 216 ASSERT_TRUE(launch_ready_notified_); | |
| 217 ASSERT_EQ(current_launch_count, reporter_launch_count_); | |
| 218 | |
| 219 // Reset |launch_ready_notified_| so that we can tell if TryToRun gets | |
| 220 // called again unexpectedly. | |
| 221 launch_ready_notified_ = false; | |
| 222 | |
| 223 // Call the pending LaunchAndWaitForExit. | |
| 224 task_runner_->RunPendingTasks(); | |
| 225 ASSERT_FALSE(launch_ready_notified_); | |
| 226 ASSERT_FALSE(reporter_done_notified_); | |
| 227 | |
| 228 // At this point LaunchAndWaitForExit has definitely been called if | |
| 229 // it's going to be called at all. (If not, TryToRun will have been | |
| 230 // scheduled again.) | |
| 231 EXPECT_EQ(current_launch_count + 1, reporter_launch_count_); | |
| 232 EXPECT_EQ(expected_launch_path, | |
| 233 reporter_launch_parameters_.command_line.GetProgram()); | |
| 234 | |
| 235 // Pump the UI message loop to process the ReporterDone call (which | |
| 236 // will schedule the next TryToRun.) If LaunchAndWaitForExit wasn't | |
| 237 // called, this does nothing. | |
| 238 base::RunLoop().RunUntilIdle(); | |
| 239 | |
| 240 // At this point there are three things that could have happened: | |
| 241 // | |
| 242 // 1. LaunchAndWaitForExit was not called. There should be a TryToRun | |
| 243 // scheduled. | |
| 244 // | |
| 245 // 2. ReporterDone was called and there was nothing left in the queue | |
| 246 // of SwReporterInvocation's. There should be a TryToRun scheduled. | |
| 247 // | |
| 248 // 3. ReporterDone was called and there were more | |
| 249 // SwReporterInvocation's in the queue to run immediately. There should | |
| 250 // be a LaunchAndWaitForExit scheduled. | |
| 251 // | |
| 252 // So in all cases there should be a pending task, and if we are expecting | |
| 253 // more launches in this loop, |launch_ready_notified_| will already be | |
| 254 // true. | |
| 255 ASSERT_TRUE(task_runner_->HasPendingTask()); | |
| 256 | |
| 257 // The test task runner does not actually advance the clock. Pretend that | |
| 258 // one day has passed. (Otherwise, when we launch the last | |
| 259 // SwReporterInvocation in the queue, the next call to TryToRun will | |
| 260 // start a whole new launch cycle.) | |
| 261 SetDaysSinceLastReport(1); | |
| 262 | |
| 263 reporter_done_notified_ = false; | |
| 264 current_launch_count = reporter_launch_count_; | |
| 265 } | 211 } |
| 266 } | 212 } |
| 267 | 213 |
| 268 // Run through the steps needed to launch the reporter, as many times as | 214 // Expects |invocation|'s command line to contain all the switches required |
| 269 // needed to launch all the reporters given in |expected_launch_paths|. Test | 215 // for reporter logging. |
| 270 // that each of those launches succeeded. Then, run through the steps needed | 216 void ExpectLoggingSwitches(const SwReporterInvocation& invocation, |
| 271 // to launch the reporter again, to test that the launch cycle is complete | 217 bool expect_switches) { |
| 272 // (no more reporters will be launched). | |
| 273 void TestReporterLaunchCycle( | |
| 274 const std::vector<base::FilePath>& expected_launch_paths) { | |
| 275 TestPartialLaunchCycle(expected_launch_paths); | |
| 276 | |
| 277 // Now that all expected launches have been tested, run the cycle once more | |
| 278 // to make sure no more launches happen. | |
| 279 ASSERT_TRUE(task_runner_->HasPendingTask()); | |
| 280 ASSERT_FALSE(reporter_done_notified_); | |
| 281 ASSERT_FALSE(launch_ready_notified_); | |
| 282 | |
| 283 int current_launch_count = reporter_launch_count_; | |
| 284 | |
| 285 // Call the pending TryToRun. | |
| 286 task_runner_->RunPendingTasks(); | |
| 287 | |
| 288 // We expect that this scheduled another TryToRun. If it scheduled | |
| 289 // LaunchAndWaitForExit an unexpected launch is about to happen. | |
| 290 ASSERT_TRUE(task_runner_->HasPendingTask()); | |
| 291 ASSERT_FALSE(launch_ready_notified_); | |
| 292 ASSERT_FALSE(reporter_done_notified_); | |
| 293 ASSERT_EQ(current_launch_count, reporter_launch_count_); | |
| 294 } | |
| 295 | |
| 296 // Expects |reporter_launch_parameters_| to contain exactly the command line | |
| 297 // switches specified in |expected_switches|. | |
| 298 void ExpectLoggingSwitches(const std::set<std::string>& expected_switches) { | |
| 299 const base::CommandLine::SwitchMap& invocation_switches = | 218 const base::CommandLine::SwitchMap& invocation_switches = |
| 300 reporter_launch_parameters_.command_line.GetSwitches(); | 219 invocation.command_line.GetSwitches(); |
| 220 std::set<std::string> expected_switches; | |
| 221 if (expect_switches) | |
| 222 expected_switches = {std::begin(kExpectedSwitches), | |
| 223 std::end(kExpectedSwitches)}; | |
| 301 EXPECT_EQ(expected_switches.size(), invocation_switches.size()); | 224 EXPECT_EQ(expected_switches.size(), invocation_switches.size()); |
| 302 // Checks if all expected switches are in the invocation switches. It's not | 225 // Checks if all expected switches are in the invocation switches. It's not |
| 303 // necessary to check if all invocation switches are expected, since we | 226 // necessary to check if all invocation switches are expected, since we |
| 304 // checked if both sets should have the same size. | 227 // checked if both sets should have the same size. |
| 305 for (const std::string& expected_switch : expected_switches) { | 228 for (const std::string& expected_switch : expected_switches) { |
| 306 EXPECT_NE(invocation_switches.find(expected_switch), | 229 EXPECT_NE(invocation_switches.find(expected_switch), |
| 307 invocation_switches.end()); | 230 invocation_switches.end()); |
| 308 } | 231 } |
| 309 } | 232 } |
| 310 | 233 |
| 311 void EnableSBExtendedReporting() { | 234 void EnableSBExtendedReporting() { |
| 312 Browser* browser = chrome::FindLastActive(); | 235 Browser* browser = chrome::FindLastActive(); |
| 313 ASSERT_NE(browser, nullptr); | 236 ASSERT_NE(browser, nullptr); |
| 314 Profile* profile = browser->profile(); | 237 Profile* profile = browser->profile(); |
| 315 ASSERT_NE(profile, nullptr); | 238 ASSERT_NE(profile, nullptr); |
| 316 SetExtendedReportingPref(profile->GetPrefs(), true); | 239 SetExtendedReportingPref(profile->GetPrefs(), true); |
| 317 } | 240 } |
| 318 | 241 |
| 319 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 242 // Replaces the main MessageLoop's TaskRunner with a TaskRunner on which time |
| 243 // is mocked to allow testing of scheduled launches. | |
| 244 std::unique_ptr<base::ScopedMockTimeMessageLoopTaskRunner> | |
| 245 scoped_task_runner_; | |
| 246 | |
| 320 bool prompt_trigger_called_ = false; | 247 bool prompt_trigger_called_ = false; |
| 321 int reporter_launch_count_ = 0; | 248 int reporter_launch_count_ = 0; |
| 322 SwReporterInvocation reporter_launch_parameters_; | 249 std::vector<SwReporterInvocation> reporter_launch_parameters_; |
| 323 int exit_code_to_report_ = kReporterFailureExitCode; | 250 int exit_code_to_report_ = kReporterFailureExitCode; |
| 324 | 251 |
| 325 // This will be set to true when a call to |LaunchAndWaitForExit| is next in | 252 // A callback to invoke when the first reporter of a queue is launched. This |
| 326 // the task queue. | 253 // can be used to perform actions in the middle of a queue of reporters which |
| 327 bool launch_ready_notified_ = false; | 254 // all launch on the same mock clock tick. |
| 328 | 255 base::OnceCallback<void()> first_launch_callback_; |
|
gab
2017/02/21 22:15:37
Use OnceClosure instead of OnceCallback<void()>
Joe Mason
2017/02/22 04:43:37
Done.
| |
| 329 bool reporter_done_notified_ = false; | |
| 330 }; | 256 }; |
| 331 | 257 |
| 332 } // namespace | 258 } // namespace |
| 333 | 259 |
| 334 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, NothingFound) { | 260 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, NothingFound) { |
| 335 exit_code_to_report_ = kSwReporterNothingFound; | 261 exit_code_to_report_ = kSwReporterNothingFound; |
| 336 RunReporter(); | 262 RunReporter(); |
| 337 task_runner_->RunPendingTasks(); | 263 ExpectReporterLaunches(0, 1, false); |
| 338 EXPECT_EQ(1, reporter_launch_count_); | |
| 339 base::RunLoop().RunUntilIdle(); | |
| 340 EXPECT_FALSE(prompt_trigger_called_); | |
| 341 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | 264 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); |
| 342 } | 265 } |
| 343 | 266 |
| 344 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, CleanupNeeded) { | 267 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, CleanupNeeded) { |
| 345 exit_code_to_report_ = kSwReporterCleanupNeeded; | 268 exit_code_to_report_ = kSwReporterCleanupNeeded; |
| 346 RunReporter(); | 269 RunReporter(); |
| 347 | 270 ExpectReporterLaunches(0, 1, true); |
| 348 task_runner_->RunPendingTasks(); | |
| 349 EXPECT_EQ(1, reporter_launch_count_); | |
| 350 // The reply task from the task posted to run the reporter is run on a | |
| 351 // specific thread, as opposed to a specific task runner, and that thread is | |
| 352 // the current message loop's thread. | |
| 353 base::RunLoop().RunUntilIdle(); | |
| 354 EXPECT_TRUE(prompt_trigger_called_); | |
| 355 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | 271 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); |
| 356 } | 272 } |
| 357 | 273 |
| 358 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, RanRecently) { | 274 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, RanRecently) { |
| 359 constexpr int kDaysLeft = 1; | 275 constexpr int kDaysLeft = 1; |
| 360 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns - kDaysLeft); | 276 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns - kDaysLeft); |
| 361 RunReporter(); | 277 RunReporter(); |
| 362 | 278 ExpectReporterLaunches(0, 0, false); |
| 363 // Here we can't run until idle since the ReporterRunner will re-post | |
| 364 // infinitely. | |
| 365 task_runner_->RunPendingTasks(); | |
| 366 EXPECT_EQ(0, reporter_launch_count_); | |
| 367 | |
| 368 ExpectToRunAgain(kDaysLeft); | 279 ExpectToRunAgain(kDaysLeft); |
| 369 task_runner_->ClearPendingTasks(); | 280 ExpectReporterLaunches(kDaysLeft, 1, false); |
| 281 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | |
| 370 } | 282 } |
| 371 | 283 |
| 372 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, WaitForBrowser) { | 284 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, WaitForBrowser) { |
| 373 Profile* profile = browser()->profile(); | 285 Profile* profile = browser()->profile(); |
| 374 CloseAllBrowsers(); | |
| 375 | 286 |
| 287 // Ensure that even though we're closing the last browser, we don't enter the | |
| 288 // "shutting down" state, which will prevent the test from starting another | |
| 289 // browser. | |
| 290 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::SESSION_RESTORE, | |
| 291 KeepAliveRestartOption::ENABLED); | |
| 292 | |
| 293 // Restore the standard task runner to perform browser cleanup, which posts | |
| 294 // some tasks with pending times but does not advance | |
| 295 // ScopedMockTimeMessageLoopTaskRunner's mock clock. | |
| 296 scoped_task_runner_.reset(); | |
| 297 CloseBrowserSynchronously(browser()); | |
| 298 scoped_task_runner_ = | |
| 299 std::make_unique<base::ScopedMockTimeMessageLoopTaskRunner>(); | |
| 300 ASSERT_EQ(0u, chrome::GetTotalBrowserCount()); | |
| 301 ASSERT_FALSE(chrome::FindLastActive()); | |
| 302 | |
| 303 // Start the reporter while the browser is closed. The prompt should not open. | |
| 376 exit_code_to_report_ = kSwReporterCleanupNeeded; | 304 exit_code_to_report_ = kSwReporterCleanupNeeded; |
| 377 RunReporter(); | 305 RunReporter(); |
| 306 ExpectReporterLaunches(0, 1, false); | |
| 378 | 307 |
| 379 task_runner_->RunPendingTasks(); | 308 // Create a Browser object directly instead of using helper functions like |
| 309 // CreateBrowser, because they all wait on timed events but do not advance the | |
| 310 // mock timer. The Browser constructor registers itself with the global | |
| 311 // BrowserList, which is cleaned up when InProcessBrowserTest exits. | |
| 312 new Browser(Browser::CreateParams(profile)); | |
| 313 ASSERT_EQ(1u, chrome::GetTotalBrowserCount()); | |
| 314 | |
| 315 // Some browser startup tasks are scheduled to run in the first few minutes | |
| 316 // after creation. Make sure they've all been processed so that the only | |
| 317 // pending task in the queue is the next reporter check. | |
| 318 task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(10)); | |
| 319 | |
| 320 // On opening the new browser, the prompt should be shown and the reporter | |
| 321 // should be scheduled to run later. | |
| 380 EXPECT_EQ(1, reporter_launch_count_); | 322 EXPECT_EQ(1, reporter_launch_count_); |
| 381 | |
| 382 CreateBrowser(profile); | |
| 383 EXPECT_TRUE(prompt_trigger_called_); | 323 EXPECT_TRUE(prompt_trigger_called_); |
| 384 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | 324 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); |
| 385 } | 325 } |
| 386 | 326 |
| 387 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, Failure) { | 327 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, Failure) { |
| 388 exit_code_to_report_ = kReporterFailureExitCode; | 328 exit_code_to_report_ = kReporterFailureExitCode; |
| 389 RunReporter(); | 329 RunReporter(); |
| 390 | 330 ExpectReporterLaunches(0, 1, false); |
| 391 task_runner_->RunPendingTasks(); | |
| 392 EXPECT_EQ(1, reporter_launch_count_); | |
| 393 | |
| 394 base::RunLoop().RunUntilIdle(); | |
| 395 EXPECT_FALSE(prompt_trigger_called_); | |
| 396 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | 331 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); |
| 397 } | 332 } |
| 398 | 333 |
| 399 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, RunDaily) { | 334 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, RunDaily) { |
| 400 exit_code_to_report_ = kSwReporterNothingFound; | 335 exit_code_to_report_ = kSwReporterNothingFound; |
| 401 PrefService* local_state = g_browser_process->local_state(); | 336 PrefService* local_state = g_browser_process->local_state(); |
| 402 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, true); | 337 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, true); |
| 403 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns - 1); | 338 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns - 1); |
| 404 DCHECK_GT(kDaysBetweenSuccessfulSwReporterRuns - 1, | 339 DCHECK_GT(kDaysBetweenSuccessfulSwReporterRuns - 1, |
| 405 kDaysBetweenSwReporterRunsForPendingPrompt); | 340 kDaysBetweenSwReporterRunsForPendingPrompt); |
| 406 RunReporter(); | 341 RunReporter(); |
| 407 | 342 |
| 408 task_runner_->RunPendingTasks(); | 343 // Expect the reporter to run immediately, since a prompt is pending and it |
| 409 EXPECT_EQ(1, reporter_launch_count_); | 344 // has been more than kDaysBetweenSwReporterRunsForPendingPrompt days. |
| 410 reporter_launch_count_ = 0; | 345 ExpectReporterLaunches(0, 1, false); |
| 411 base::RunLoop().RunUntilIdle(); | |
| 412 ExpectToRunAgain(kDaysBetweenSwReporterRunsForPendingPrompt); | 346 ExpectToRunAgain(kDaysBetweenSwReporterRunsForPendingPrompt); |
| 413 | 347 |
| 348 // Move the clock ahead kDaysBetweenSwReporterRunsForPendingPrompt days. The | |
| 349 // expected run should trigger, but not cause the reporter to launch because | |
| 350 // a prompt is no longer pending. | |
| 414 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, false); | 351 local_state->SetBoolean(prefs::kSwReporterPendingPrompt, false); |
| 415 task_runner_->RunPendingTasks(); | 352 ExpectReporterLaunches(kDaysBetweenSwReporterRunsForPendingPrompt, 0, false); |
| 416 EXPECT_EQ(0, reporter_launch_count_); | 353 |
| 417 base::RunLoop().RunUntilIdle(); | 354 // Instead it should now run kDaysBetweenSuccessfulSwReporterRuns after the |
| 355 // first prompt (of which kDaysBetweenSwReporterRunsForPendingPrompt has | |
| 356 // already passed.) | |
| 357 int days_left = kDaysBetweenSuccessfulSwReporterRuns - | |
| 358 kDaysBetweenSwReporterRunsForPendingPrompt; | |
| 359 ExpectToRunAgain(days_left); | |
| 360 ExpectReporterLaunches(days_left, 1, false); | |
| 418 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | 361 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); |
| 419 } | 362 } |
| 420 | 363 |
| 421 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ParameterChange) { | 364 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ParameterChange) { |
| 422 exit_code_to_report_ = kSwReporterNothingFound; | 365 exit_code_to_report_ = kSwReporterNothingFound; |
| 423 | 366 |
| 424 // If the reporter is run several times with different parameters, it should | 367 // If the reporter is run several times with different parameters, it should |
| 425 // only be launched once, with the last parameter set. | 368 // only be launched once, with the last parameter set. |
| 426 const base::FilePath path1(L"path1"); | 369 const base::FilePath path1(L"path1"); |
| 427 const base::FilePath path2(L"path2"); | 370 const base::FilePath path2(L"path2"); |
| 428 const base::FilePath path3(L"path3"); | 371 const base::FilePath path3(L"path3"); |
| 429 | 372 |
| 430 // Schedule path1 with a day left in the reporting period. | 373 // Schedule path1 with a day left in the reporting period. |
| 431 // The reporter should not launch. | 374 // The reporter should not launch. |
| 432 constexpr int kDaysLeft = 1; | 375 constexpr int kDaysLeft = 1; |
| 433 { | 376 { |
| 434 SCOPED_TRACE("N days left until next reporter run"); | 377 SCOPED_TRACE("N days left until next reporter run"); |
| 435 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns - kDaysLeft); | 378 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns - kDaysLeft); |
| 436 RunReporter(path1); | 379 RunReporter(path1); |
| 437 TestReporterLaunchCycle({}); | 380 ExpectReporterLaunches(0, {}, false); |
| 438 } | 381 } |
| 439 | 382 |
| 440 // Schedule path2 just as we enter the next reporting period. | 383 // Schedule path2 just as we enter the next reporting period. |
| 441 // Now the reporter should launch, just once, using path2. | 384 // Now the reporter should launch, just once, using path2. |
| 442 { | 385 { |
| 443 SCOPED_TRACE("Reporter runs now"); | 386 SCOPED_TRACE("Reporter runs now"); |
| 444 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | |
| 445 RunReporter(path2); | 387 RunReporter(path2); |
| 446 // Schedule it twice; it should only actually run once. | 388 // Schedule it twice; it should only actually run once. |
| 447 RunReporter(path2); | 389 RunReporter(path2); |
| 448 TestReporterLaunchCycle({path2}); | 390 ExpectReporterLaunches(kDaysLeft, {path2}, false); |
| 449 } | 391 } |
| 450 | 392 |
| 451 // Schedule path3 before any more time has passed. | 393 // Schedule path3 before any more time has passed. |
| 452 // The reporter should not launch. | 394 // The reporter should not launch. |
| 453 { | 395 { |
| 454 SCOPED_TRACE("No more time passed"); | 396 SCOPED_TRACE("No more time passed"); |
| 455 SetDaysSinceLastReport(0); | |
| 456 RunReporter(path3); | 397 RunReporter(path3); |
| 457 TestReporterLaunchCycle({}); | 398 ExpectReporterLaunches(0, {}, false); |
| 458 } | 399 } |
| 459 | 400 |
| 460 // Enter the next reporting period as path3 is still scheduled. | 401 // Enter the next reporting period as path3 is still scheduled. |
| 461 // Now the reporter should launch again using path3. (Tests that the | 402 // Now the reporter should launch again using path3. (Tests that the |
| 462 // parameters from the first launch aren't reused.) | 403 // parameters from the first launch aren't reused.) |
| 463 { | 404 { |
| 464 SCOPED_TRACE("Previous run still scheduled"); | 405 SCOPED_TRACE("Previous run still scheduled"); |
| 465 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 406 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path3}, |
| 466 TestReporterLaunchCycle({path3}); | 407 false); |
| 467 } | 408 } |
| 468 | 409 |
| 469 // Schedule path3 again in the next reporting period. | 410 // Schedule path3 again in the next reporting period. |
| 470 // The reporter should launch again using path3, since enough time has | 411 // The reporter should launch again using path3, since enough time has |
| 471 // passed, even though the parameters haven't changed. | 412 // passed, even though the parameters haven't changed. |
| 472 { | 413 { |
| 473 SCOPED_TRACE("Run with same parameters"); | 414 SCOPED_TRACE("Run with same parameters"); |
| 474 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | |
| 475 RunReporter(path3); | 415 RunReporter(path3); |
| 476 TestReporterLaunchCycle({path3}); | 416 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path3}, |
| 417 false); | |
| 477 } | 418 } |
| 478 } | 419 } |
| 479 | 420 |
| 480 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, MultipleLaunches) { | 421 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, MultipleLaunches) { |
| 481 exit_code_to_report_ = kSwReporterNothingFound; | 422 exit_code_to_report_ = kSwReporterNothingFound; |
| 482 | 423 |
| 483 const base::FilePath path1(L"path1"); | 424 const base::FilePath path1(L"path1"); |
| 484 const base::FilePath path2(L"path2"); | 425 const base::FilePath path2(L"path2"); |
| 485 const base::FilePath path3(L"path3"); | 426 const base::FilePath path3(L"path3"); |
| 486 | 427 |
| 487 SwReporterQueue invocations; | 428 SwReporterQueue invocations; |
| 488 invocations.push(SwReporterInvocation::FromFilePath(path1)); | 429 invocations.push(SwReporterInvocation::FromFilePath(path1)); |
| 489 invocations.push(SwReporterInvocation::FromFilePath(path2)); | 430 invocations.push(SwReporterInvocation::FromFilePath(path2)); |
| 490 | 431 |
| 491 { | 432 { |
| 492 SCOPED_TRACE("Launch 2 times"); | 433 SCOPED_TRACE("Launch 2 times"); |
| 493 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 434 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); |
| 494 RunReporterQueue(invocations); | 435 RunReporterQueue(invocations); |
| 495 TestReporterLaunchCycle({path1, path2}); | 436 ExpectReporterLaunches(0, {path1, path2}, false); |
| 437 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | |
| 496 } | 438 } |
| 497 | 439 |
| 498 // Schedule a launch with 2 elements, then another with the same 2. It should | 440 // Schedule a launch with 2 elements, then another with the same 2. It should |
| 499 // just run 2 times, not 4. | 441 // just run 2 times, not 4. |
| 500 { | 442 { |
| 501 SCOPED_TRACE("Launch 2 times with retry"); | 443 SCOPED_TRACE("Launch 2 times with retry"); |
| 502 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | |
| 503 RunReporterQueue(invocations); | 444 RunReporterQueue(invocations); |
| 504 RunReporterQueue(invocations); | 445 RunReporterQueue(invocations); |
| 505 TestReporterLaunchCycle({path1, path2}); | 446 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path1, path2}, |
| 447 false); | |
| 448 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | |
| 506 } | 449 } |
| 507 | 450 |
| 508 // Schedule a launch with 2 elements, then add a third while the queue is | 451 // Another launch with 2 elements is already scheduled. Add a third while the |
| 509 // running. | 452 // queue is running. |
| 510 { | 453 { |
| 511 SCOPED_TRACE("Add third launch while running"); | 454 SCOPED_TRACE("Add third launch while running"); |
| 512 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 455 invocations.push(SwReporterInvocation::FromFilePath(path3)); |
| 513 RunReporterQueue(invocations); | 456 first_launch_callback_ = base::BindOnce( |
| 457 &SRTFetcherTest::RunReporterQueue, base::Unretained(this), invocations); | |
| 514 | 458 |
| 515 // Only test the cycle once, to process the first element in queue. | 459 // Only the first two elements should execute since the third was added |
| 516 TestPartialLaunchCycle({path1}); | 460 // during the cycle. |
| 517 | 461 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path1, path2}, |
| 518 invocations.push(SwReporterInvocation::FromFilePath(path3)); | 462 false); |
| 519 RunReporterQueue(invocations); | 463 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); |
| 520 | |
| 521 // There is still a 2nd element on the queue - that should execute, and | |
| 522 // nothing more. | |
| 523 TestReporterLaunchCycle({path2}); | |
| 524 | 464 |
| 525 // Time passes... Now the 3-element queue should run. | 465 // Time passes... Now the 3-element queue should run. |
| 526 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 466 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, |
| 527 TestReporterLaunchCycle({path1, path2, path3}); | 467 {path1, path2, path3}, false); |
| 468 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | |
| 528 } | 469 } |
| 529 | 470 |
| 530 // Second launch should not occur after a failure. | 471 // Second launch should not occur after a failure. |
| 531 { | 472 { |
| 532 SCOPED_TRACE("Launch multiple times with failure"); | 473 SCOPED_TRACE("Launch multiple times with failure"); |
| 533 exit_code_to_report_ = kReporterFailureExitCode; | 474 exit_code_to_report_ = kReporterFailureExitCode; |
| 534 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | |
| 535 RunReporterQueue(invocations); | 475 RunReporterQueue(invocations); |
| 536 TestReporterLaunchCycle({path1}); | 476 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path1}, |
| 477 false); | |
| 478 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | |
| 537 | 479 |
| 538 // If we try again before the reporting period is up, it should not do | 480 // If we try again before the reporting period is up, it should not do |
| 539 // anything. | 481 // anything. |
| 540 TestReporterLaunchCycle({}); | 482 ExpectReporterLaunches(0, {}, false); |
| 541 | 483 |
| 542 // After enough time has passed, should try the queue again. | 484 // After enough time has passed, should try the queue again. |
| 543 SetDaysSinceLastReport(kDaysBetweenSuccessfulSwReporterRuns); | 485 ExpectReporterLaunches(kDaysBetweenSuccessfulSwReporterRuns, {path1}, |
| 544 TestReporterLaunchCycle({path1}); | 486 false); |
| 487 ExpectToRunAgain(kDaysBetweenSuccessfulSwReporterRuns); | |
| 545 } | 488 } |
| 546 } | 489 } |
| 547 | 490 |
| 548 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) { | 491 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_NoSBExtendedReporting) { |
| 549 exit_code_to_report_ = kSwReporterNothingFound; | 492 exit_code_to_report_ = kSwReporterNothingFound; |
| 550 base::test::ScopedFeatureList scoped_feature_list; | 493 base::test::ScopedFeatureList scoped_feature_list; |
| 551 RunReporter(); | 494 RunReporter(); |
| 552 TestReporterLaunchCycle({base::FilePath()}); | 495 ExpectReporterLaunches(0, 1, false); |
| 553 ExpectLoggingSwitches({/*expect no switches*/}); | 496 ExpectLoggingSwitches(reporter_launch_parameters_.front(), false); |
| 554 ExpectLastTimeSentReportNotSet(); | 497 ExpectLastTimeSentReportNotSet(); |
| 555 } | 498 } |
| 556 | 499 |
| 557 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledFirstRun) { | 500 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledFirstRun) { |
| 558 exit_code_to_report_ = kSwReporterNothingFound; | 501 exit_code_to_report_ = kSwReporterNothingFound; |
| 559 base::test::ScopedFeatureList scoped_feature_list; | 502 base::test::ScopedFeatureList scoped_feature_list; |
| 560 EnableSBExtendedReporting(); | 503 EnableSBExtendedReporting(); |
| 561 // Note: don't set last time sent logs in the local state. | 504 // Note: don't set last time sent logs in the local state. |
| 562 // SBER is enabled and there is no record in the local state of the last time | 505 // SBER is enabled and there is no record in the local state of the last time |
| 563 // logs have been sent, so we should send logs in this run. | 506 // logs have been sent, so we should send logs in this run. |
| 564 RunReporter(); | 507 RunReporter(); |
| 565 TestReporterLaunchCycle({base::FilePath()}); | 508 ExpectReporterLaunches(0, 1, false); |
| 566 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | 509 ExpectLoggingSwitches(reporter_launch_parameters_.front(), true); |
| 567 std::end(kExpectedSwitches))); | |
| 568 ExpectLastReportSentInTheLastHour(); | 510 ExpectLastReportSentInTheLastHour(); |
| 569 } | 511 } |
| 570 | 512 |
| 571 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) { | 513 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledNoRecentLogging) { |
| 572 exit_code_to_report_ = kSwReporterNothingFound; | 514 exit_code_to_report_ = kSwReporterNothingFound; |
| 573 base::test::ScopedFeatureList scoped_feature_list; | 515 base::test::ScopedFeatureList scoped_feature_list; |
| 574 // SBER is enabled and last time logs were sent was more than | 516 // SBER is enabled and last time logs were sent was more than |
| 575 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. | 517 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. |
| 576 EnableSBExtendedReporting(); | 518 EnableSBExtendedReporting(); |
| 577 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); | 519 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); |
| 578 RunReporter(); | 520 RunReporter(); |
| 579 TestReporterLaunchCycle({base::FilePath()}); | 521 ExpectReporterLaunches(0, 1, false); |
| 580 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | 522 ExpectLoggingSwitches(reporter_launch_parameters_.front(), true); |
| 581 std::end(kExpectedSwitches))); | |
| 582 ExpectLastReportSentInTheLastHour(); | 523 ExpectLastReportSentInTheLastHour(); |
| 583 } | 524 } |
| 584 | 525 |
| 585 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) { | 526 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_EnabledRecentlyLogged) { |
| 586 exit_code_to_report_ = kSwReporterNothingFound; | 527 exit_code_to_report_ = kSwReporterNothingFound; |
| 587 base::test::ScopedFeatureList scoped_feature_list; | 528 base::test::ScopedFeatureList scoped_feature_list; |
| 588 // SBER is enabled, but logs have been sent less than | 529 // SBER is enabled, but logs have been sent less than |
| 589 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in | 530 // |kDaysBetweenReporterLogsSent| day ago, so we shouldn't send any logs in |
| 590 // this run. | 531 // this run. |
| 591 EnableSBExtendedReporting(); | 532 EnableSBExtendedReporting(); |
| 592 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1); | 533 SetLastTimeSentReport(kDaysBetweenReporterLogsSent - 1); |
| 593 int64_t last_time_sent_logs = GetLastTimeSentReport(); | 534 int64_t last_time_sent_logs = GetLastTimeSentReport(); |
| 594 RunReporter(); | 535 RunReporter(); |
| 595 TestReporterLaunchCycle({base::FilePath()}); | 536 ExpectReporterLaunches(0, 1, false); |
| 596 ExpectLoggingSwitches(std::set<std::string>{/*expect no switches*/}); | 537 ExpectLoggingSwitches(reporter_launch_parameters_.front(), false); |
| 597 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport()); | 538 EXPECT_EQ(last_time_sent_logs, GetLastTimeSentReport()); |
| 598 } | 539 } |
| 599 | 540 |
| 600 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_MultipleLaunches) { | 541 IN_PROC_BROWSER_TEST_F(SRTFetcherTest, ReporterLogging_MultipleLaunches) { |
| 601 exit_code_to_report_ = kSwReporterNothingFound; | 542 exit_code_to_report_ = kSwReporterNothingFound; |
| 602 base::test::ScopedFeatureList scoped_feature_list; | 543 base::test::ScopedFeatureList scoped_feature_list; |
| 603 EnableSBExtendedReporting(); | 544 EnableSBExtendedReporting(); |
| 604 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); | 545 SetLastTimeSentReport(kDaysBetweenReporterLogsSent + 3); |
| 605 | 546 |
| 606 const base::FilePath path1(L"path1"); | 547 const base::FilePath path1(L"path1"); |
| 607 const base::FilePath path2(L"path2"); | 548 const base::FilePath path2(L"path2"); |
| 608 SwReporterQueue invocations; | 549 SwReporterQueue invocations; |
| 609 for (const auto& path : {path1, path2}) { | 550 for (const auto& path : {path1, path2}) { |
| 610 auto invocation = SwReporterInvocation::FromFilePath(path); | 551 auto invocation = SwReporterInvocation::FromFilePath(path); |
| 611 invocation.supported_behaviours = | 552 invocation.supported_behaviours = |
| 612 SwReporterInvocation::BEHAVIOUR_ALLOW_SEND_REPORTER_LOGS; | 553 SwReporterInvocation::BEHAVIOUR_ALLOW_SEND_REPORTER_LOGS; |
| 613 invocations.push(invocation); | 554 invocations.push(invocation); |
| 614 } | 555 } |
| 615 RunReporterQueue(invocations); | 556 RunReporterQueue(invocations); |
| 616 | 557 |
| 617 // SBER is enabled and last time logs were sent was more than | 558 // SBER is enabled and last time logs were sent was more than |
| 618 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. | 559 // |kDaysBetweenReporterLogsSent| day ago, so we should send logs in this run. |
| 619 { | 560 { |
| 620 SCOPED_TRACE("first launch"); | 561 SCOPED_TRACE("first launch"); |
| 621 TestPartialLaunchCycle({path1}); | 562 first_launch_callback_ = |
| 622 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | 563 base::BindOnce(&SRTFetcherTest::ExpectLastReportSentInTheLastHour, |
| 623 std::end(kExpectedSwitches))); | 564 base::Unretained(this)); |
| 624 ExpectLastReportSentInTheLastHour(); | 565 ExpectReporterLaunches(0, {path1, path2}, false); |
| 566 ExpectLoggingSwitches(reporter_launch_parameters_[0], true); | |
| 625 } | 567 } |
| 626 | 568 |
| 627 // Logs should also be sent for the next run, even though LastTimeSentReport | 569 // Logs should also be sent for the next run, even though LastTimeSentReport |
| 628 // is now recent, because the run is part of the same set of invocations. | 570 // is now recent, because the run is part of the same set of invocations. |
| 629 { | 571 { |
| 630 SCOPED_TRACE("second launch"); | 572 SCOPED_TRACE("second launch"); |
| 631 TestReporterLaunchCycle({path2}); | 573 ExpectLoggingSwitches(reporter_launch_parameters_[1], true); |
| 632 ExpectLoggingSwitches(std::set<std::string>(std::begin(kExpectedSwitches), | |
| 633 std::end(kExpectedSwitches))); | |
| 634 ExpectLastReportSentInTheLastHour(); | 574 ExpectLastReportSentInTheLastHour(); |
| 635 } | 575 } |
| 636 } | 576 } |
| 637 | 577 |
| 638 } // namespace safe_browsing | 578 } // namespace safe_browsing |
| OLD | NEW |