Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/policy/upload_job.h" | 5 #include "chrome/browser/chromeos/policy/upload_job.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/test/test_simple_task_runner.h" | |
| 20 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "chrome/browser/chromeos/policy/upload_job_impl.h" | 23 #include "chrome/browser/chromeos/policy/upload_job_impl.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "google_apis/gaia/fake_oauth2_token_service.h" | 25 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 25 #include "google_apis/gaia/google_service_auth_error.h" | 26 #include "google_apis/gaia/google_service_auth_error.h" |
| 26 #include "net/http/http_status_code.h" | 27 #include "net/http/http_status_code.h" |
| 27 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 28 #include "net/test/embedded_test_server/http_request.h" | 29 #include "net/test/embedded_test_server/http_request.h" |
| 29 #include "net/test/embedded_test_server/http_response.h" | 30 #include "net/test/embedded_test_server/http_response.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 void MockOAuth2TokenService::InvalidateAccessTokenImpl( | 144 void MockOAuth2TokenService::InvalidateAccessTokenImpl( |
| 144 const std::string& account_id, | 145 const std::string& account_id, |
| 145 const std::string& client_id, | 146 const std::string& client_id, |
| 146 const ScopeSet& scopes, | 147 const ScopeSet& scopes, |
| 147 const std::string& access_token) { | 148 const std::string& access_token) { |
| 148 SetTokenInvalid(access_token); | 149 SetTokenInvalid(access_token); |
| 149 } | 150 } |
| 150 | 151 |
| 151 } // namespace | 152 } // namespace |
| 152 | 153 |
| 154 // Mock thread runner which runs each tasks immediately. | |
| 155 class ImmediateThreadTaskRunner : public base::SingleThreadTaskRunner { | |
|
Andrew T Wilson (Slow)
2016/05/02 09:15:49
Can you use TestSimpleTaskRunner instead?
Marton Hunyady
2016/05/04 11:26:41
Unfortunately, I think no. Other possible solution
| |
| 156 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 157 | |
| 158 public: | |
| 159 ImmediateThreadTaskRunner() | |
| 160 : task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
| 161 | |
| 162 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 163 const base::Closure& task, | |
| 164 base::TimeDelta delay) override { | |
| 165 return task_runner_->PostTask(from_here, task); | |
| 166 } | |
| 167 | |
| 168 bool RunsTasksOnCurrentThread() const override { | |
| 169 return task_runner_->RunsTasksOnCurrentThread(); | |
| 170 } | |
| 171 | |
| 172 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
| 173 const base::Closure& task, | |
| 174 base::TimeDelta delay) override { | |
| 175 return task_runner_->PostTask(from_here, task); | |
| 176 } | |
| 177 | |
| 178 protected: | |
| 179 ~ImmediateThreadTaskRunner() override {} | |
| 180 | |
| 181 DISALLOW_COPY_AND_ASSIGN(ImmediateThreadTaskRunner); | |
| 182 }; | |
| 183 | |
| 153 class UploadJobTestBase : public testing::Test, public UploadJob::Delegate { | 184 class UploadJobTestBase : public testing::Test, public UploadJob::Delegate { |
| 154 public: | 185 public: |
| 155 UploadJobTestBase() | 186 UploadJobTestBase() |
| 156 : test_browser_thread_bundle_( | 187 : test_browser_thread_bundle_( |
| 157 content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 188 content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 189 task_runner_(new ImmediateThreadTaskRunner) {} | |
| 158 | 190 |
| 159 // policy::UploadJob::Delegate: | 191 // policy::UploadJob::Delegate: |
| 160 void OnSuccess() override { | 192 void OnSuccess() override { |
| 161 if (!expected_error_) | 193 if (!expected_error_) |
| 162 run_loop_.Quit(); | 194 run_loop_.Quit(); |
| 163 else | 195 else |
| 164 FAIL(); | 196 FAIL(); |
| 165 } | 197 } |
| 166 | 198 |
| 167 // policy::UploadJob::Delegate: | 199 // policy::UploadJob::Delegate: |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 191 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 223 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 192 } | 224 } |
| 193 | 225 |
| 194 protected: | 226 protected: |
| 195 std::unique_ptr<UploadJob> PrepareUploadJob( | 227 std::unique_ptr<UploadJob> PrepareUploadJob( |
| 196 std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator> | 228 std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator> |
| 197 mime_boundary_generator) { | 229 mime_boundary_generator) { |
| 198 std::unique_ptr<UploadJob> upload_job( | 230 std::unique_ptr<UploadJob> upload_job( |
| 199 new UploadJobImpl(GetServerURL(), kRobotAccountId, &oauth2_service_, | 231 new UploadJobImpl(GetServerURL(), kRobotAccountId, &oauth2_service_, |
| 200 request_context_getter_.get(), this, | 232 request_context_getter_.get(), this, |
| 201 std::move(mime_boundary_generator))); | 233 std::move(mime_boundary_generator), task_runner_)); |
| 202 | 234 |
| 203 std::map<std::string, std::string> header_entries; | 235 std::map<std::string, std::string> header_entries; |
| 204 header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1")); | 236 header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1")); |
| 205 std::unique_ptr<std::string> data(new std::string(kTestPayload1)); | 237 std::unique_ptr<std::string> data(new std::string(kTestPayload1)); |
| 206 upload_job->AddDataSegment("Name1", "file1.ext", header_entries, | 238 upload_job->AddDataSegment("Name1", "file1.ext", header_entries, |
| 207 std::move(data)); | 239 std::move(data)); |
| 208 | 240 |
| 209 header_entries.insert(std::make_pair(kCustomField2, "CUSTOM2")); | 241 header_entries.insert(std::make_pair(kCustomField2, "CUSTOM2")); |
| 210 std::unique_ptr<std::string> data2(new std::string(kTestPayload2)); | 242 std::unique_ptr<std::string> data2(new std::string(kTestPayload2)); |
| 211 upload_job->AddDataSegment("Name2", "", header_entries, std::move(data2)); | 243 upload_job->AddDataSegment("Name2", "", header_entries, std::move(data2)); |
| 212 return upload_job; | 244 return upload_job; |
| 213 } | 245 } |
| 214 | 246 |
| 215 content::TestBrowserThreadBundle test_browser_thread_bundle_; | 247 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 216 base::RunLoop run_loop_; | 248 base::RunLoop run_loop_; |
| 217 net::EmbeddedTestServer test_server_; | 249 net::EmbeddedTestServer test_server_; |
| 218 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 250 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 219 MockOAuth2TokenService oauth2_service_; | 251 MockOAuth2TokenService oauth2_service_; |
| 220 | 252 |
| 221 std::unique_ptr<UploadJob::ErrorCode> expected_error_; | 253 std::unique_ptr<UploadJob::ErrorCode> expected_error_; |
| 254 | |
| 255 // TaskRunner used to run individual tests. | |
| 256 scoped_refptr<ImmediateThreadTaskRunner> task_runner_; | |
| 222 }; | 257 }; |
| 223 | 258 |
| 224 class UploadFlowTest : public UploadJobTestBase { | 259 class UploadFlowTest : public UploadJobTestBase { |
| 225 public: | 260 public: |
| 226 UploadFlowTest() {} | 261 UploadFlowTest() {} |
| 227 | 262 |
| 228 // UploadJobTestBase: | 263 // UploadJobTestBase: |
| 229 void SetUp() override { | 264 void SetUp() override { |
| 230 UploadJobTestBase::SetUp(); | 265 UploadJobTestBase::SetUp(); |
| 231 test_server_.RegisterRequestHandler( | 266 test_server_.RegisterRequestHandler( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 oauth2_service_.AddTokenToQueue(kTokenValid); | 303 oauth2_service_.AddTokenToQueue(kTokenValid); |
| 269 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 304 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 270 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 305 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 271 upload_job->Start(); | 306 upload_job->Start(); |
| 272 run_loop_.Run(); | 307 run_loop_.Run(); |
| 273 } | 308 } |
| 274 | 309 |
| 275 TEST_F(UploadFlowTest, TokenInvalid) { | 310 TEST_F(UploadFlowTest, TokenInvalid) { |
| 276 oauth2_service_.AddTokenToQueue(kTokenInvalid); | 311 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 277 oauth2_service_.AddTokenToQueue(kTokenInvalid); | 312 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 313 oauth2_service_.AddTokenToQueue(kTokenInvalid); | |
| 314 oauth2_service_.AddTokenToQueue(kTokenInvalid); | |
| 278 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( | 315 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( |
| 279 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); | 316 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); |
| 280 | 317 |
| 281 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 318 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 282 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 319 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 283 upload_job->Start(); | 320 upload_job->Start(); |
| 284 run_loop_.Run(); | 321 run_loop_.Run(); |
| 285 } | 322 } |
| 286 | 323 |
| 324 TEST_F(UploadFlowTest, TokenMultipleTries) { | |
| 325 oauth2_service_.SetTokenValid(kTokenValid); | |
| 326 oauth2_service_.AddTokenToQueue(kTokenInvalid); | |
| 327 oauth2_service_.AddTokenToQueue(kTokenInvalid); | |
| 328 oauth2_service_.AddTokenToQueue(kTokenValid); | |
| 329 | |
| 330 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | |
| 331 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | |
| 332 upload_job->Start(); | |
| 333 run_loop_.Run(); | |
| 334 } | |
| 335 | |
| 287 TEST_F(UploadFlowTest, TokenFetchFailure) { | 336 TEST_F(UploadFlowTest, TokenFetchFailure) { |
| 288 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( | 337 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( |
| 289 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); | 338 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); |
| 290 | 339 |
| 291 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 340 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 292 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 341 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 293 upload_job->Start(); | 342 upload_job->Start(); |
| 294 run_loop_.Run(); | 343 run_loop_.Run(); |
| 295 } | 344 } |
| 296 | 345 |
| 346 class UploadServerErrorTest : public UploadJobTestBase { | |
| 347 public: | |
| 348 UploadServerErrorTest() {} | |
| 349 | |
| 350 // UploadJobTestBase: | |
| 351 void SetUp() override { | |
| 352 UploadJobTestBase::SetUp(); | |
| 353 test_server_.RegisterRequestHandler(base::Bind( | |
| 354 &UploadServerErrorTest::HandlePostRequest, base::Unretained(this))); | |
| 355 } | |
| 356 | |
| 357 std::unique_ptr<net::test_server::HttpResponse> HandlePostRequest( | |
| 358 const net::test_server::HttpRequest& request) { | |
| 359 std::unique_ptr<net::test_server::BasicHttpResponse> response( | |
| 360 new net::test_server::BasicHttpResponse); | |
| 361 response->set_code(net::HTTP_INTERNAL_SERVER_ERROR); | |
| 362 return std::move(response); | |
| 363 } | |
| 364 }; | |
| 365 | |
| 366 TEST_F(UploadServerErrorTest, InternalServerError) { | |
| 367 oauth2_service_.SetTokenValid(kTokenValid); | |
| 368 oauth2_service_.AddTokenToQueue(kTokenValid); | |
|
Andrew T Wilson (Slow)
2016/05/02 09:15:49
Is there a test that ensures that we retry multipl
Marton Hunyady
2016/05/04 11:26:41
Done.
| |
| 369 | |
| 370 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( | |
| 371 new UploadJob::ErrorCode(UploadJob::SERVER_ERROR))); | |
| 372 | |
| 373 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | |
| 374 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | |
| 375 upload_job->Start(); | |
| 376 run_loop_.Run(); | |
| 377 } | |
| 378 | |
| 297 class UploadRequestTest : public UploadJobTestBase { | 379 class UploadRequestTest : public UploadJobTestBase { |
| 298 public: | 380 public: |
| 299 UploadRequestTest() {} | 381 UploadRequestTest() {} |
| 300 | 382 |
| 301 // UploadJobTestBase: | 383 // UploadJobTestBase: |
| 302 void SetUp() override { | 384 void SetUp() override { |
| 303 UploadJobTestBase::SetUp(); | 385 UploadJobTestBase::SetUp(); |
| 304 test_server_.RegisterRequestHandler(base::Bind( | 386 test_server_.RegisterRequestHandler(base::Bind( |
| 305 &UploadRequestTest::HandlePostRequest, base::Unretained(this))); | 387 &UploadRequestTest::HandlePostRequest, base::Unretained(this))); |
| 306 } | 388 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 "customfield2: CUSTOM2\r\n" | 422 "customfield2: CUSTOM2\r\n" |
| 341 "\r\n" | 423 "\r\n" |
| 342 "**||--||PAYLOAD2||--||**\r\n--" | 424 "**||--||PAYLOAD2||--||**\r\n--" |
| 343 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--\r\n"); | 425 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--\r\n"); |
| 344 | 426 |
| 345 upload_job->Start(); | 427 upload_job->Start(); |
| 346 run_loop_.Run(); | 428 run_loop_.Run(); |
| 347 } | 429 } |
| 348 | 430 |
| 349 } // namespace policy | 431 } // namespace policy |
| OLD | NEW |