OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/test/ash_test_base.h" | |
6 #include "base/run_loop.h" | |
7 #include "base/test/test_mock_time_task_runner.h" | |
8 #include "base/thread_task_runner_handle.h" | |
9 #include "base/time/time.h" | |
10 #include "chrome/browser/chromeos/policy/system_log_upload_job.h" | |
11 #include "chrome/browser/chromeos/policy/upload_job.h" | |
12 | |
13 namespace policy { | |
14 | |
15 namespace { | |
16 | |
17 const char* const kMockUploadUrl = "http://example.com/upload"; | |
18 | |
19 const char* const kTestSystemLogFileNames[] = {"my_system_log_file.txt"}; | |
20 | |
21 class MockUploadJob : public policy::UploadJob { | |
22 public: | |
23 // If |error_code| is a null pointer OnSuccess() will be invoked when the | |
24 // Start() method is called, otherwise OnFailure() will be invoked with the | |
25 // respective |error_code|. | |
26 MockUploadJob(const GURL& upload_url, | |
27 UploadJob::Delegate* delegate, | |
28 scoped_ptr<UploadJob::ErrorCode> error_code); | |
29 ~MockUploadJob() override; | |
30 | |
31 // policy::UploadJob: | |
32 void AddDataSegment(const std::string& name, | |
33 const std::string& filename, | |
34 const std::map<std::string, std::string>& header_entries, | |
35 scoped_ptr<std::string> data) override; | |
36 void Start() override; | |
37 | |
38 protected: | |
39 const GURL upload_url_; | |
40 UploadJob::Delegate* delegate_; | |
41 scoped_ptr<UploadJob::ErrorCode> error_code_; | |
42 }; | |
43 | |
44 MockUploadJob::MockUploadJob(const GURL& upload_url, | |
45 UploadJob::Delegate* delegate, | |
46 scoped_ptr<UploadJob::ErrorCode> error_code) | |
47 : upload_url_(upload_url), | |
48 delegate_(delegate), | |
49 error_code_(error_code.Pass()) { | |
50 } | |
51 | |
52 MockUploadJob::~MockUploadJob() { | |
53 } | |
54 | |
55 void MockUploadJob::AddDataSegment( | |
56 const std::string& name, | |
57 const std::string& filename, | |
58 const std::map<std::string, std::string>& header_entries, | |
59 scoped_ptr<std::string> data) { | |
60 } | |
61 | |
62 void MockUploadJob::Start() { | |
63 DCHECK(delegate_); | |
64 EXPECT_EQ(kMockUploadUrl, upload_url_.spec()); | |
Andrew T Wilson (Slow)
2015/07/03 16:19:59
What is this testing, exactly? And why are we test
Polina Bondarenko
2015/07/10 13:24:22
Yes, removed.
| |
65 if (error_code_) { | |
66 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
67 FROM_HERE, base::Bind(&UploadJob::Delegate::OnFailure, | |
68 base::Unretained(delegate_), *error_code_)); | |
69 return; | |
Andrew T Wilson (Slow)
2015/07/03 16:20:00
This is OK as is, but usually I prefer:
if (error
Polina Bondarenko
2015/07/10 13:24:22
Done.
| |
70 } | |
71 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
72 FROM_HERE, | |
73 base::Bind(&UploadJob::Delegate::OnSuccess, base::Unretained(delegate_))); | |
74 } | |
75 | |
76 // Generate the fake system log files. | |
77 scoped_ptr<SystemLogUploadJob::SystemLogs> GenerateTestSystemLogFiles() { | |
78 scoped_ptr<SystemLogUploadJob::SystemLogs> system_logs( | |
79 new SystemLogUploadJob::SystemLogs()); | |
80 for (auto const file_path : kTestSystemLogFileNames) { | |
81 system_logs->push_back(std::make_pair(file_path, file_path)); | |
Andrew T Wilson (Slow)
2015/07/03 16:20:00
Is there any code that tests that this file data i
Polina Bondarenko
2015/07/10 13:24:21
Added.
| |
82 } | |
83 return system_logs; | |
84 } | |
85 | |
86 class MockSystemLogDelegate : public SystemLogUploadJob::Delegate { | |
87 public: | |
88 explicit MockSystemLogDelegate( | |
89 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code); | |
90 ~MockSystemLogDelegate() override; | |
91 | |
92 void LoadSystemLogs( | |
93 const SystemLogUploadJob::LogUploadCallback& upload_callback) override; | |
94 scoped_ptr<UploadJob> CreateUploadJob(const GURL&, | |
95 UploadJob::Delegate*) override; | |
96 | |
97 private: | |
98 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code_; | |
99 }; | |
100 | |
101 MockSystemLogDelegate::MockSystemLogDelegate( | |
102 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code) | |
103 : upload_job_error_code_(upload_job_error_code.Pass()) { | |
104 } | |
105 | |
106 MockSystemLogDelegate::~MockSystemLogDelegate() { | |
107 } | |
108 | |
109 void MockSystemLogDelegate::LoadSystemLogs( | |
110 const SystemLogUploadJob::LogUploadCallback& upload_callback) { | |
111 scoped_ptr<SystemLogUploadJob::SystemLogs> test_files = | |
112 GenerateTestSystemLogFiles(); | |
113 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
114 FROM_HERE, | |
115 base::Bind(upload_callback, base::Owned(test_files.release()))); | |
116 } | |
117 | |
118 scoped_ptr<UploadJob> MockSystemLogDelegate::CreateUploadJob( | |
119 const GURL& upload_url, | |
120 UploadJob::Delegate* delegate) { | |
121 return make_scoped_ptr(new MockUploadJob(GURL(kMockUploadUrl), delegate, | |
122 upload_job_error_code_.Pass())); | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 class SystemLogUploadTest : public ash::test::AshTestBase { | |
Andrew T Wilson (Slow)
2015/07/03 16:19:59
Why are we deriving from AshTestBase? I don't thin
Polina Bondarenko
2015/07/10 13:24:21
Done.
| |
128 public: | |
129 void VerifyResults(SystemLogUploadJob* job, | |
130 SystemLogUploadJob::Status expected_status); | |
131 | |
132 protected: | |
133 SystemLogUploadTest(); | |
134 | |
135 base::RunLoop run_loop_; | |
136 | |
137 private: | |
138 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(SystemLogUploadTest); | |
141 }; | |
142 | |
143 SystemLogUploadTest::SystemLogUploadTest() | |
144 : task_runner_(new base::TestMockTimeTaskRunner()) { | |
145 } | |
146 | |
147 void SystemLogUploadTest::VerifyResults( | |
148 SystemLogUploadJob* job, | |
149 SystemLogUploadJob::Status expected_status) { | |
150 EXPECT_EQ(expected_status, job->status()); | |
151 | |
152 run_loop_.Quit(); | |
153 } | |
154 | |
155 TEST_F(SystemLogUploadTest, Success) { | |
156 scoped_ptr<SystemLogUploadJob> job(new SystemLogUploadJob( | |
157 make_scoped_ptr(new MockSystemLogDelegate(nullptr)))); | |
158 | |
159 job->Run( | |
160 base::Bind(&SystemLogUploadTest::VerifyResults, base::Unretained(this), | |
161 base::Unretained(job.get()), SystemLogUploadJob::SUCCEEDED), | |
162 base::Bind(&SystemLogUploadTest::VerifyResults, base::Unretained(this), | |
Andrew T Wilson (Slow)
2015/07/03 16:19:59
Seems like it should be an error for this second c
Polina Bondarenko
2015/07/10 13:24:22
Done.
| |
163 base::Unretained(job.get()), SystemLogUploadJob::SUCCEEDED)); | |
164 run_loop_.Run(); | |
165 } | |
166 | |
167 TEST_F(SystemLogUploadTest, Failure) { | |
168 using ErrorCode = UploadJob::ErrorCode; | |
169 scoped_ptr<ErrorCode> error_code( | |
170 new ErrorCode(UploadJob::AUTHENTICATION_ERROR)); | |
171 scoped_ptr<SystemLogUploadJob> job(new SystemLogUploadJob( | |
172 make_scoped_ptr(new MockSystemLogDelegate(error_code.Pass())))); | |
173 | |
174 job->Run( | |
175 base::Bind(&SystemLogUploadTest::VerifyResults, base::Unretained(this), | |
176 base::Unretained(job.get()), SystemLogUploadJob::FAILED), | |
177 base::Bind(&SystemLogUploadTest::VerifyResults, base::Unretained(this), | |
Andrew T Wilson (Slow)
2015/07/03 16:20:00
ditto here, success callback shouldn't be called,
Polina Bondarenko
2015/07/10 13:24:21
Done.
| |
178 base::Unretained(job.get()), SystemLogUploadJob::FAILED)); | |
179 run_loop_.Run(); | |
180 } | |
181 | |
182 } // namespace policy | |
OLD | NEW |