Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / system_log_uploader_unittest.cc
blob3c79b7c6a112072559dd859b2eaab0ab6d0f6be2
1 // Copyright (c) 2015 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.
5 #include "base/strings/stringprintf.h"
6 #include "base/test/test_simple_task_runner.h"
7 #include "base/time/time.h"
8 #include "chrome/browser/chromeos/policy/system_log_uploader.h"
9 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "content/public/test/test_utils.h"
12 #include "net/http/http_request_headers.h"
13 #include "testing/gmock/include/gmock/gmock.h"
15 namespace policy {
17 namespace {
19 // The list of tested system log file names.
20 const char* const kTestSystemLogFileNames[] = {"name1.txt", "name32.txt"};
22 // Generate the fake system log files.
23 SystemLogUploader::SystemLogs GenerateTestSystemLogFiles() {
24 SystemLogUploader::SystemLogs system_logs;
25 for (auto const file_path : kTestSystemLogFileNames) {
26 system_logs.push_back(std::make_pair(file_path, file_path));
28 return system_logs;
31 class MockUploadJob : public UploadJob {
32 public:
33 // If is_upload_error is false OnSuccess() will be invoked when the
34 // Start() method is called, otherwise OnFailure() will be invoked.
35 MockUploadJob(const GURL& upload_url,
36 UploadJob::Delegate* delegate,
37 bool is_upload_error,
38 int max_files);
39 ~MockUploadJob() override;
41 // policy::UploadJob:
42 void AddDataSegment(const std::string& name,
43 const std::string& filename,
44 const std::map<std::string, std::string>& header_entries,
45 scoped_ptr<std::string> data) override;
46 void Start() override;
48 protected:
49 UploadJob::Delegate* delegate_;
50 bool is_upload_error_;
51 int file_index_;
52 int max_files_;
55 MockUploadJob::MockUploadJob(const GURL& upload_url,
56 UploadJob::Delegate* delegate,
57 bool is_upload_error,
58 int max_files)
59 : delegate_(delegate),
60 is_upload_error_(is_upload_error),
61 file_index_(0),
62 max_files_(max_files) {}
64 MockUploadJob::~MockUploadJob() {}
66 void MockUploadJob::AddDataSegment(
67 const std::string& name,
68 const std::string& filename,
69 const std::map<std::string, std::string>& header_entries,
70 scoped_ptr<std::string> data) {
71 // Test all fields to upload.
72 EXPECT_LT(file_index_, max_files_);
73 EXPECT_GE(file_index_, 0);
75 EXPECT_EQ(base::StringPrintf(SystemLogUploader::kNameFieldTemplate,
76 file_index_ + 1),
77 name);
79 EXPECT_EQ(kTestSystemLogFileNames[file_index_], filename);
81 EXPECT_EQ(2U, header_entries.size());
82 EXPECT_EQ(
83 SystemLogUploader::kFileTypeLogFile,
84 header_entries.find(SystemLogUploader::kFileTypeHeaderName)->second);
85 EXPECT_EQ(SystemLogUploader::kContentTypePlainText,
86 header_entries.find(net::HttpRequestHeaders::kContentType)->second);
88 EXPECT_EQ(kTestSystemLogFileNames[file_index_], *data);
90 file_index_++;
93 void MockUploadJob::Start() {
94 DCHECK(delegate_);
95 // Check if all files were uploaded.
96 EXPECT_EQ(max_files_, file_index_);
98 if (is_upload_error_) {
99 // Send any ErrorCode.
100 delegate_->OnFailure(UploadJob::ErrorCode::NETWORK_ERROR);
101 } else {
102 delegate_->OnSuccess();
106 // MockSystemLogDelegate - mock class that creates an upload job and runs upload
107 // callback.
108 class MockSystemLogDelegate : public SystemLogUploader::Delegate {
109 public:
110 MockSystemLogDelegate(bool is_upload_error,
111 const SystemLogUploader::SystemLogs& system_logs)
112 : is_upload_error_(is_upload_error), system_logs_(system_logs) {}
113 ~MockSystemLogDelegate() override {}
115 void LoadSystemLogs(const LogUploadCallback& upload_callback) override {
116 EXPECT_TRUE(is_upload_allowed_);
117 upload_callback.Run(
118 make_scoped_ptr(new SystemLogUploader::SystemLogs(system_logs_)));
121 scoped_ptr<UploadJob> CreateUploadJob(
122 const GURL& url,
123 UploadJob::Delegate* delegate) override {
124 return make_scoped_ptr(new MockUploadJob(url, delegate, is_upload_error_,
125 system_logs_.size()));
128 void set_upload_allowed(bool is_upload_allowed) {
129 is_upload_allowed_ = is_upload_allowed;
132 private:
133 bool is_upload_allowed_;
134 bool is_upload_error_;
135 SystemLogUploader::SystemLogs system_logs_;
138 } // namespace
140 class SystemLogUploaderTest : public testing::Test {
141 public:
142 SystemLogUploaderTest() : task_runner_(new base::TestSimpleTaskRunner()) {}
144 void SetUp() override {
145 settings_helper_.ReplaceProvider(chromeos::kLogUploadEnabled);
148 void TearDown() override {
149 settings_helper_.RestoreProvider();
150 content::RunAllBlockingPoolTasksUntilIdle();
153 // Given a pending task to upload system logs.
154 void RunPendingUploadTaskAndCheckNext(const SystemLogUploader& uploader,
155 base::TimeDelta expected_delay) {
156 EXPECT_FALSE(task_runner_->GetPendingTasks().empty());
157 task_runner_->RunPendingTasks();
159 // The previous task should have uploaded another log upload task.
160 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
162 CheckPendingTaskDelay(uploader, expected_delay);
165 void CheckPendingTaskDelay(const SystemLogUploader& uploader,
166 base::TimeDelta expected_delay) {
167 // The next task should be scheduled sometime between
168 // |last_upload_attempt| + |expected_delay| and
169 // |now| + |expected_delay|.
170 base::Time now = base::Time::NowFromSystemTime();
171 base::Time next_task = now + task_runner_->NextPendingTaskDelay();
173 EXPECT_LE(next_task, now + expected_delay);
174 EXPECT_GE(next_task, uploader.last_upload_attempt() + expected_delay);
177 protected:
178 content::TestBrowserThreadBundle thread_bundle_;
179 chromeos::ScopedCrosSettingsTestHelper settings_helper_;
180 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
183 // Check disabled system log uploads by default.
184 TEST_F(SystemLogUploaderTest, Basic) {
185 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
187 scoped_ptr<MockSystemLogDelegate> syslog_delegate(
188 new MockSystemLogDelegate(false, SystemLogUploader::SystemLogs()));
189 syslog_delegate->set_upload_allowed(false);
190 SystemLogUploader uploader(syslog_delegate.Pass(), task_runner_);
192 task_runner_->RunPendingTasks();
195 // One success task pending.
196 TEST_F(SystemLogUploaderTest, SuccessTest) {
197 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
199 scoped_ptr<MockSystemLogDelegate> syslog_delegate(
200 new MockSystemLogDelegate(false, SystemLogUploader::SystemLogs()));
201 syslog_delegate->set_upload_allowed(true);
202 settings_helper_.SetBoolean(chromeos::kLogUploadEnabled, true);
203 SystemLogUploader uploader(syslog_delegate.Pass(), task_runner_);
205 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
207 RunPendingUploadTaskAndCheckNext(
208 uploader, base::TimeDelta::FromMilliseconds(
209 SystemLogUploader::kDefaultUploadDelayMs));
212 // Three failed responses recieved.
213 TEST_F(SystemLogUploaderTest, ThreeFailureTest) {
214 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
216 scoped_ptr<MockSystemLogDelegate> syslog_delegate(
217 new MockSystemLogDelegate(true, SystemLogUploader::SystemLogs()));
218 syslog_delegate->set_upload_allowed(true);
219 settings_helper_.SetBoolean(chromeos::kLogUploadEnabled, true);
220 SystemLogUploader uploader(syslog_delegate.Pass(), task_runner_);
222 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
224 // Do not retry two times consequentially.
225 RunPendingUploadTaskAndCheckNext(uploader,
226 base::TimeDelta::FromMilliseconds(
227 SystemLogUploader::kErrorUploadDelayMs));
228 // We are using the kDefaultUploadDelayMs and not the kErrorUploadDelayMs here
229 // because there's just one retry.
230 RunPendingUploadTaskAndCheckNext(
231 uploader, base::TimeDelta::FromMilliseconds(
232 SystemLogUploader::kDefaultUploadDelayMs));
233 RunPendingUploadTaskAndCheckNext(uploader,
234 base::TimeDelta::FromMilliseconds(
235 SystemLogUploader::kErrorUploadDelayMs));
238 // Check header fields of system log files to upload.
239 TEST_F(SystemLogUploaderTest, CheckHeaders) {
240 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
242 SystemLogUploader::SystemLogs system_logs = GenerateTestSystemLogFiles();
243 scoped_ptr<MockSystemLogDelegate> syslog_delegate(
244 new MockSystemLogDelegate(false, system_logs));
245 syslog_delegate->set_upload_allowed(true);
246 settings_helper_.SetBoolean(chromeos::kLogUploadEnabled, true);
247 SystemLogUploader uploader(syslog_delegate.Pass(), task_runner_);
249 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
251 RunPendingUploadTaskAndCheckNext(
252 uploader, base::TimeDelta::FromMilliseconds(
253 SystemLogUploader::kDefaultUploadDelayMs));
256 // Disable system log uploads after one failed log upload.
257 TEST_F(SystemLogUploaderTest, DisableLogUpload) {
258 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
260 scoped_ptr<MockSystemLogDelegate> syslog_delegate(
261 new MockSystemLogDelegate(true, SystemLogUploader::SystemLogs()));
262 MockSystemLogDelegate* mock_delegate = syslog_delegate.get();
263 settings_helper_.SetBoolean(chromeos::kLogUploadEnabled, true);
264 mock_delegate->set_upload_allowed(true);
265 SystemLogUploader uploader(syslog_delegate.Pass(), task_runner_);
267 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size());
268 RunPendingUploadTaskAndCheckNext(uploader,
269 base::TimeDelta::FromMilliseconds(
270 SystemLogUploader::kErrorUploadDelayMs));
272 // Disable log upload and check that frequency is usual, because there is no
273 // errors, we should not upload logs.
274 settings_helper_.SetBoolean(chromeos::kLogUploadEnabled, false);
275 mock_delegate->set_upload_allowed(false);
276 task_runner_->RunPendingTasks();
278 RunPendingUploadTaskAndCheckNext(
279 uploader, base::TimeDelta::FromMilliseconds(
280 SystemLogUploader::kDefaultUploadDelayMs));
281 RunPendingUploadTaskAndCheckNext(
282 uploader, base::TimeDelta::FromMilliseconds(
283 SystemLogUploader::kDefaultUploadDelayMs));
286 // Test RemovePII function.
287 TEST_F(SystemLogUploaderTest, TestPII) {
288 std::string data =
289 "aaaaaaaaSSID=123aaaaaaaaaaa\n" // SSID.
290 "aaaaaaaahttp://tets.comaaaaaaa\n" // URL.
291 "aaaaaemail@example.comaaa\n" // Email address.
292 "example@@1234\n" // No PII, it is not valid email address.
293 "255.255.355.255\n" // No PII, it is not valid IP address format.
294 "aaaa123.123.45.4aaa\n" // IP address.
295 "11:11;11::11\n" // IP address.
296 "11::11\n" // IP address.
297 "11:11:abcdef:0:0:0:0:0"; // No PII, it is not valid IP address format.
299 std::string result =
300 "example@@1234\n"
301 "255.255.355.255\n"
302 "11:11:abcdef:0:0:0:0:0\n";
304 EXPECT_EQ(result, SystemLogUploader::RemoveSensitiveData(data));
307 } // namespace policy