| 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" | |
| 20 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 22 #include "chrome/browser/chromeos/policy/upload_job_impl.h" | 21 #include "chrome/browser/chromeos/policy/upload_job_impl.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "google_apis/gaia/fake_oauth2_token_service.h" | 23 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 25 #include "google_apis/gaia/google_service_auth_error.h" | 24 #include "google_apis/gaia/google_service_auth_error.h" |
| 26 #include "net/http/http_status_code.h" | 25 #include "net/http/http_status_code.h" |
| 27 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 28 #include "net/test/embedded_test_server/http_request.h" | 27 #include "net/test/embedded_test_server/http_request.h" |
| 29 #include "net/test/embedded_test_server/http_response.h" | 28 #include "net/test/embedded_test_server/http_response.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 void SetExpectedError(std::unique_ptr<UploadJob::ErrorCode> expected_error) { | 176 void SetExpectedError(std::unique_ptr<UploadJob::ErrorCode> expected_error) { |
| 178 expected_error_ = std::move(expected_error); | 177 expected_error_ = std::move(expected_error); |
| 179 } | 178 } |
| 180 | 179 |
| 181 // testing::Test: | 180 // testing::Test: |
| 182 void SetUp() override { | 181 void SetUp() override { |
| 183 request_context_getter_ = new net::TestURLRequestContextGetter( | 182 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 184 base::ThreadTaskRunnerHandle::Get()); | 183 base::ThreadTaskRunnerHandle::Get()); |
| 185 oauth2_service_.AddAccount("robot@gmail.com"); | 184 oauth2_service_.AddAccount("robot@gmail.com"); |
| 186 ASSERT_TRUE(test_server_.Start()); | 185 ASSERT_TRUE(test_server_.Start()); |
| 186 // Set retry delay to prevent timeouts |
| 187 policy::UploadJobImpl::SetRetryDelayForTesting(0); |
| 187 } | 188 } |
| 188 | 189 |
| 189 // testing::Test: | 190 // testing::Test: |
| 190 void TearDown() override { | 191 void TearDown() override { |
| 191 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 192 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 192 } | 193 } |
| 193 | 194 |
| 194 protected: | 195 protected: |
| 195 std::unique_ptr<UploadJob> PrepareUploadJob( | 196 std::unique_ptr<UploadJob> PrepareUploadJob( |
| 196 std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator> | 197 std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator> |
| 197 mime_boundary_generator) { | 198 mime_boundary_generator) { |
| 198 std::unique_ptr<UploadJob> upload_job( | 199 std::unique_ptr<UploadJob> upload_job(new UploadJobImpl( |
| 199 new UploadJobImpl(GetServerURL(), kRobotAccountId, &oauth2_service_, | 200 GetServerURL(), kRobotAccountId, &oauth2_service_, |
| 200 request_context_getter_.get(), this, | 201 request_context_getter_.get(), this, std::move(mime_boundary_generator), |
| 201 std::move(mime_boundary_generator))); | 202 base::ThreadTaskRunnerHandle::Get())); |
| 202 | 203 |
| 203 std::map<std::string, std::string> header_entries; | 204 std::map<std::string, std::string> header_entries; |
| 204 header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1")); | 205 header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1")); |
| 205 std::unique_ptr<std::string> data(new std::string(kTestPayload1)); | 206 std::unique_ptr<std::string> data(new std::string(kTestPayload1)); |
| 206 upload_job->AddDataSegment("Name1", "file1.ext", header_entries, | 207 upload_job->AddDataSegment("Name1", "file1.ext", header_entries, |
| 207 std::move(data)); | 208 std::move(data)); |
| 208 | 209 |
| 209 header_entries.insert(std::make_pair(kCustomField2, "CUSTOM2")); | 210 header_entries.insert(std::make_pair(kCustomField2, "CUSTOM2")); |
| 210 std::unique_ptr<std::string> data2(new std::string(kTestPayload2)); | 211 std::unique_ptr<std::string> data2(new std::string(kTestPayload2)); |
| 211 upload_job->AddDataSegment("Name2", "", header_entries, std::move(data2)); | 212 upload_job->AddDataSegment("Name2", "", header_entries, std::move(data2)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 223 | 224 |
| 224 class UploadFlowTest : public UploadJobTestBase { | 225 class UploadFlowTest : public UploadJobTestBase { |
| 225 public: | 226 public: |
| 226 UploadFlowTest() {} | 227 UploadFlowTest() {} |
| 227 | 228 |
| 228 // UploadJobTestBase: | 229 // UploadJobTestBase: |
| 229 void SetUp() override { | 230 void SetUp() override { |
| 230 UploadJobTestBase::SetUp(); | 231 UploadJobTestBase::SetUp(); |
| 231 test_server_.RegisterRequestHandler( | 232 test_server_.RegisterRequestHandler( |
| 232 base::Bind(&UploadFlowTest::HandlePostRequest, base::Unretained(this))); | 233 base::Bind(&UploadFlowTest::HandlePostRequest, base::Unretained(this))); |
| 234 upload_attempt_count_ = 0; |
| 235 } |
| 236 |
| 237 // Sets the response code which will be returned when no other problems occur. |
| 238 // Default is |net::HTTP_OK| |
| 239 void SetResponseDefaultStatusCode(net::HttpStatusCode code) { |
| 240 default_status_code_ = code; |
| 233 } | 241 } |
| 234 | 242 |
| 235 std::unique_ptr<net::test_server::HttpResponse> HandlePostRequest( | 243 std::unique_ptr<net::test_server::HttpResponse> HandlePostRequest( |
| 236 const net::test_server::HttpRequest& request) { | 244 const net::test_server::HttpRequest& request) { |
| 245 upload_attempt_count_++; |
| 237 EXPECT_TRUE(request.headers.find("Authorization") != request.headers.end()); | 246 EXPECT_TRUE(request.headers.find("Authorization") != request.headers.end()); |
| 238 const std::string authorization_header = | 247 const std::string authorization_header = |
| 239 request.headers.at("Authorization"); | 248 request.headers.at("Authorization"); |
| 240 std::unique_ptr<net::test_server::BasicHttpResponse> response( | 249 std::unique_ptr<net::test_server::BasicHttpResponse> response( |
| 241 new net::test_server::BasicHttpResponse); | 250 new net::test_server::BasicHttpResponse); |
| 242 const size_t pos = authorization_header.find(" "); | 251 const size_t pos = authorization_header.find(" "); |
| 243 if (pos == std::string::npos) { | 252 if (pos == std::string::npos) { |
| 244 response->set_code(net::HTTP_UNAUTHORIZED); | 253 response->set_code(net::HTTP_UNAUTHORIZED); |
| 245 return std::move(response); | 254 return std::move(response); |
| 246 } | 255 } |
| 247 | 256 |
| 248 const std::string token = authorization_header.substr(pos + 1); | 257 const std::string token = authorization_header.substr(pos + 1); |
| 249 response->set_code(oauth2_service_.IsTokenValid(token) | 258 response->set_code(oauth2_service_.IsTokenValid(token) |
| 250 ? net::HTTP_OK | 259 ? default_status_code_ |
| 251 : net::HTTP_UNAUTHORIZED); | 260 : net::HTTP_UNAUTHORIZED); |
| 252 return std::move(response); | 261 return std::move(response); |
| 253 } | 262 } |
| 263 |
| 264 protected: |
| 265 int upload_attempt_count_; |
| 266 net::HttpStatusCode default_status_code_ = net::HTTP_OK; |
| 254 }; | 267 }; |
| 255 | 268 |
| 256 TEST_F(UploadFlowTest, SuccessfulUpload) { | 269 TEST_F(UploadFlowTest, SuccessfulUpload) { |
| 257 oauth2_service_.SetTokenValid(kTokenValid); | 270 oauth2_service_.SetTokenValid(kTokenValid); |
| 258 oauth2_service_.AddTokenToQueue(kTokenValid); | 271 oauth2_service_.AddTokenToQueue(kTokenValid); |
| 259 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 272 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 260 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 273 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 261 upload_job->Start(); | 274 upload_job->Start(); |
| 262 run_loop_.Run(); | 275 run_loop_.Run(); |
| 276 ASSERT_EQ(1, upload_attempt_count_); |
| 263 } | 277 } |
| 264 | 278 |
| 265 TEST_F(UploadFlowTest, TokenExpired) { | 279 TEST_F(UploadFlowTest, TokenExpired) { |
| 266 oauth2_service_.SetTokenValid(kTokenValid); | 280 oauth2_service_.SetTokenValid(kTokenValid); |
| 267 oauth2_service_.AddTokenToQueue(kTokenExpired); | 281 oauth2_service_.AddTokenToQueue(kTokenExpired); |
| 268 oauth2_service_.AddTokenToQueue(kTokenValid); | 282 oauth2_service_.AddTokenToQueue(kTokenValid); |
| 269 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 283 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 270 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 284 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 271 upload_job->Start(); | 285 upload_job->Start(); |
| 272 run_loop_.Run(); | 286 run_loop_.Run(); |
| 287 ASSERT_EQ(2, upload_attempt_count_); |
| 273 } | 288 } |
| 274 | 289 |
| 275 TEST_F(UploadFlowTest, TokenInvalid) { | 290 TEST_F(UploadFlowTest, TokenInvalid) { |
| 276 oauth2_service_.AddTokenToQueue(kTokenInvalid); | 291 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 277 oauth2_service_.AddTokenToQueue(kTokenInvalid); | 292 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 293 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 294 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 278 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( | 295 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( |
| 279 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); | 296 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); |
| 280 | 297 |
| 281 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 298 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 282 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 299 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 283 upload_job->Start(); | 300 upload_job->Start(); |
| 284 run_loop_.Run(); | 301 run_loop_.Run(); |
| 302 ASSERT_EQ(4, upload_attempt_count_); |
| 303 } |
| 304 |
| 305 TEST_F(UploadFlowTest, TokenMultipleTries) { |
| 306 oauth2_service_.SetTokenValid(kTokenValid); |
| 307 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 308 oauth2_service_.AddTokenToQueue(kTokenInvalid); |
| 309 oauth2_service_.AddTokenToQueue(kTokenValid); |
| 310 |
| 311 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 312 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 313 upload_job->Start(); |
| 314 run_loop_.Run(); |
| 315 ASSERT_EQ(3, upload_attempt_count_); |
| 285 } | 316 } |
| 286 | 317 |
| 287 TEST_F(UploadFlowTest, TokenFetchFailure) { | 318 TEST_F(UploadFlowTest, TokenFetchFailure) { |
| 288 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( | 319 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( |
| 289 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); | 320 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR))); |
| 290 | 321 |
| 291 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( | 322 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 292 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); | 323 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 293 upload_job->Start(); | 324 upload_job->Start(); |
| 294 run_loop_.Run(); | 325 run_loop_.Run(); |
| 326 // Without a token we don't try to upload |
| 327 ASSERT_EQ(0, upload_attempt_count_); |
| 328 } |
| 329 |
| 330 TEST_F(UploadFlowTest, InternalServerError) { |
| 331 SetResponseDefaultStatusCode(net::HTTP_INTERNAL_SERVER_ERROR); |
| 332 oauth2_service_.SetTokenValid(kTokenValid); |
| 333 oauth2_service_.AddTokenToQueue(kTokenValid); |
| 334 |
| 335 SetExpectedError(std::unique_ptr<UploadJob::ErrorCode>( |
| 336 new UploadJob::ErrorCode(UploadJob::SERVER_ERROR))); |
| 337 |
| 338 std::unique_ptr<UploadJob> upload_job = PrepareUploadJob( |
| 339 base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator)); |
| 340 upload_job->Start(); |
| 341 run_loop_.Run(); |
| 342 // kMaxAttempts |
| 343 ASSERT_EQ(4, upload_attempt_count_); |
| 295 } | 344 } |
| 296 | 345 |
| 297 class UploadRequestTest : public UploadJobTestBase { | 346 class UploadRequestTest : public UploadJobTestBase { |
| 298 public: | 347 public: |
| 299 UploadRequestTest() {} | 348 UploadRequestTest() {} |
| 300 | 349 |
| 301 // UploadJobTestBase: | 350 // UploadJobTestBase: |
| 302 void SetUp() override { | 351 void SetUp() override { |
| 303 UploadJobTestBase::SetUp(); | 352 UploadJobTestBase::SetUp(); |
| 304 test_server_.RegisterRequestHandler(base::Bind( | 353 test_server_.RegisterRequestHandler(base::Bind( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 "customfield2: CUSTOM2\r\n" | 389 "customfield2: CUSTOM2\r\n" |
| 341 "\r\n" | 390 "\r\n" |
| 342 "**||--||PAYLOAD2||--||**\r\n--" | 391 "**||--||PAYLOAD2||--||**\r\n--" |
| 343 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--\r\n"); | 392 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--\r\n"); |
| 344 | 393 |
| 345 upload_job->Start(); | 394 upload_job->Start(); |
| 346 run_loop_.Run(); | 395 run_loop_.Run(); |
| 347 } | 396 } |
| 348 | 397 |
| 349 } // namespace policy | 398 } // namespace policy |
| OLD | NEW |