| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/internal_api/public/attachments/attachment_uploader_impl.h" | 5 #include "components/sync/core/attachments/attachment_uploader_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "base/test/histogram_tester.h" | 22 #include "base/test/histogram_tester.h" |
| 23 #include "base/threading/non_thread_safe.h" | 23 #include "base/threading/non_thread_safe.h" |
| 24 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
| 25 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
| 26 #include "components/sync/api/attachments/attachment.h" |
| 27 #include "components/sync/base/model_type.h" |
| 28 #include "components/sync/core/attachments/attachment_util.h" |
| 29 #include "components/sync/protocol/sync.pb.h" |
| 26 #include "google_apis/gaia/fake_oauth2_token_service.h" | 30 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 27 #include "google_apis/gaia/gaia_constants.h" | 31 #include "google_apis/gaia/gaia_constants.h" |
| 28 #include "google_apis/gaia/oauth2_token_service_request.h" | 32 #include "google_apis/gaia/oauth2_token_service_request.h" |
| 29 #include "net/http/http_status_code.h" | 33 #include "net/http/http_status_code.h" |
| 30 #include "net/test/embedded_test_server/embedded_test_server.h" | 34 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 31 #include "net/test/embedded_test_server/http_request.h" | 35 #include "net/test/embedded_test_server/http_request.h" |
| 32 #include "net/test/embedded_test_server/http_response.h" | 36 #include "net/test/embedded_test_server/http_response.h" |
| 33 #include "net/url_request/url_request_test_util.h" | 37 #include "net/url_request/url_request_test_util.h" |
| 34 #include "sync/api/attachments/attachment.h" | |
| 35 #include "sync/internal_api/public/attachments/attachment_util.h" | |
| 36 #include "sync/internal_api/public/base/model_type.h" | |
| 37 #include "sync/protocol/sync.pb.h" | |
| 38 #include "testing/gmock/include/gmock/gmock-matchers.h" | 38 #include "testing/gmock/include/gmock/gmock-matchers.h" |
| 39 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 40 | 40 |
| 41 namespace { | 41 namespace { |
| 42 | 42 |
| 43 const char kAttachmentData[] = "some data"; | 43 const char kAttachmentData[] = "some data"; |
| 44 const char kAccountId[] = "some-account-id"; | 44 const char kAccountId[] = "some-account-id"; |
| 45 const char kAccessToken[] = "some-access-token"; | 45 const char kAccessToken[] = "some-access-token"; |
| 46 const char kContentTypeValue[] = "application/octet-stream"; | 46 const char kContentTypeValue[] = "application/octet-stream"; |
| 47 const char kXGoogHash[] = "X-Goog-Hash"; | 47 const char kXGoogHash[] = "X-Goog-Hash"; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 std::string response_access_token_; | 105 std::string response_access_token_; |
| 106 base::Time response_expiration_; | 106 base::Time response_expiration_; |
| 107 int num_invalidate_token_; | 107 int num_invalidate_token_; |
| 108 std::string last_token_invalidated_; | 108 std::string last_token_invalidated_; |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 MockOAuth2TokenService::MockOAuth2TokenService() | 111 MockOAuth2TokenService::MockOAuth2TokenService() |
| 112 : response_error_(GoogleServiceAuthError::AuthErrorNone()), | 112 : response_error_(GoogleServiceAuthError::AuthErrorNone()), |
| 113 response_access_token_(kAccessToken), | 113 response_access_token_(kAccessToken), |
| 114 response_expiration_(base::Time::Max()), | 114 response_expiration_(base::Time::Max()), |
| 115 num_invalidate_token_(0) { | 115 num_invalidate_token_(0) {} |
| 116 } | |
| 117 | 116 |
| 118 MockOAuth2TokenService::~MockOAuth2TokenService() { | 117 MockOAuth2TokenService::~MockOAuth2TokenService() {} |
| 119 } | |
| 120 | 118 |
| 121 void MockOAuth2TokenService::SetResponse(const GoogleServiceAuthError& error, | 119 void MockOAuth2TokenService::SetResponse(const GoogleServiceAuthError& error, |
| 122 const std::string& access_token, | 120 const std::string& access_token, |
| 123 const base::Time& expiration) { | 121 const base::Time& expiration) { |
| 124 response_error_ = error; | 122 response_error_ = error; |
| 125 response_access_token_ = access_token; | 123 response_access_token_ = access_token; |
| 126 response_expiration_ = expiration; | 124 response_expiration_ = expiration; |
| 127 } | 125 } |
| 128 | 126 |
| 129 void MockOAuth2TokenService::FetchOAuth2Token( | 127 void MockOAuth2TokenService::FetchOAuth2Token( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 163 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 166 OAuth2TokenService* token_service_; | 164 OAuth2TokenService* token_service_; |
| 167 }; | 165 }; |
| 168 | 166 |
| 169 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service) | 167 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service) |
| 170 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 168 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 171 token_service_(token_service) { | 169 token_service_(token_service) { |
| 172 DCHECK(token_service_); | 170 DCHECK(token_service_); |
| 173 } | 171 } |
| 174 | 172 |
| 175 TokenServiceProvider::~TokenServiceProvider() { | 173 TokenServiceProvider::~TokenServiceProvider() {} |
| 176 } | |
| 177 | 174 |
| 178 scoped_refptr<base::SingleThreadTaskRunner> | 175 scoped_refptr<base::SingleThreadTaskRunner> |
| 179 TokenServiceProvider::GetTokenServiceTaskRunner() { | 176 TokenServiceProvider::GetTokenServiceTaskRunner() { |
| 180 return task_runner_; | 177 return task_runner_; |
| 181 } | 178 } |
| 182 | 179 |
| 183 OAuth2TokenService* TokenServiceProvider::GetTokenService() { | 180 OAuth2TokenService* TokenServiceProvider::GetTokenService() { |
| 184 DCHECK(task_runner_->BelongsToCurrentThread()); | 181 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 185 return token_service_; | 182 return token_service_; |
| 186 } | 183 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 private: | 259 private: |
| 263 // Protects status_code_. | 260 // Protects status_code_. |
| 264 mutable base::Lock mutex_; | 261 mutable base::Lock mutex_; |
| 265 net::HttpStatusCode status_code_; | 262 net::HttpStatusCode status_code_; |
| 266 | 263 |
| 267 scoped_refptr<base::SingleThreadTaskRunner> test_task_runner_; | 264 scoped_refptr<base::SingleThreadTaskRunner> test_task_runner_; |
| 268 base::WeakPtr<AttachmentUploaderImplTest> test_; | 265 base::WeakPtr<AttachmentUploaderImplTest> test_; |
| 269 }; | 266 }; |
| 270 | 267 |
| 271 AttachmentUploaderImplTest::AttachmentUploaderImplTest() | 268 AttachmentUploaderImplTest::AttachmentUploaderImplTest() |
| 272 : weak_ptr_factory_(this) { | 269 : weak_ptr_factory_(this) {} |
| 273 } | |
| 274 | 270 |
| 275 void AttachmentUploaderImplTest::OnRequestReceived(const HttpRequest& request) { | 271 void AttachmentUploaderImplTest::OnRequestReceived(const HttpRequest& request) { |
| 276 DCHECK(CalledOnValidThread()); | 272 DCHECK(CalledOnValidThread()); |
| 277 http_requests_received_.push_back(request); | 273 http_requests_received_.push_back(request); |
| 278 } | 274 } |
| 279 | 275 |
| 280 void AttachmentUploaderImplTest::SetUp() { | 276 void AttachmentUploaderImplTest::SetUp() { |
| 281 DCHECK(CalledOnValidThread()); | 277 DCHECK(CalledOnValidThread()); |
| 282 request_handler_.reset(new RequestHandler(message_loop_.task_runner(), | 278 request_handler_.reset(new RequestHandler(message_loop_.task_runner(), |
| 283 weak_ptr_factory_.GetWeakPtr())); | 279 weak_ptr_factory_.GetWeakPtr())); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 | 336 |
| 341 std::vector<HttpRequest>& AttachmentUploaderImplTest::http_requests_received() { | 337 std::vector<HttpRequest>& AttachmentUploaderImplTest::http_requests_received() { |
| 342 return http_requests_received_; | 338 return http_requests_received_; |
| 343 } | 339 } |
| 344 | 340 |
| 345 std::vector<AttachmentUploader::UploadResult>& | 341 std::vector<AttachmentUploader::UploadResult>& |
| 346 AttachmentUploaderImplTest::upload_results() { | 342 AttachmentUploaderImplTest::upload_results() { |
| 347 return upload_results_; | 343 return upload_results_; |
| 348 } | 344 } |
| 349 | 345 |
| 350 std::vector<AttachmentId>& | 346 std::vector<AttachmentId>& AttachmentUploaderImplTest::attachment_ids() { |
| 351 AttachmentUploaderImplTest::attachment_ids() { | |
| 352 return attachment_ids_; | 347 return attachment_ids_; |
| 353 } | 348 } |
| 354 | 349 |
| 355 MockOAuth2TokenService& AttachmentUploaderImplTest::token_service() { | 350 MockOAuth2TokenService& AttachmentUploaderImplTest::token_service() { |
| 356 return *token_service_; | 351 return *token_service_; |
| 357 } | 352 } |
| 358 | 353 |
| 359 base::MessageLoopForIO& AttachmentUploaderImplTest::message_loop() { | 354 base::MessageLoopForIO& AttachmentUploaderImplTest::message_loop() { |
| 360 return message_loop_; | 355 return message_loop_; |
| 361 } | 356 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 384 } | 379 } |
| 385 | 380 |
| 386 RequestHandler::~RequestHandler() { | 381 RequestHandler::~RequestHandler() { |
| 387 DetachFromThread(); | 382 DetachFromThread(); |
| 388 } | 383 } |
| 389 | 384 |
| 390 std::unique_ptr<HttpResponse> RequestHandler::HandleRequest( | 385 std::unique_ptr<HttpResponse> RequestHandler::HandleRequest( |
| 391 const HttpRequest& request) { | 386 const HttpRequest& request) { |
| 392 DCHECK(CalledOnValidThread()); | 387 DCHECK(CalledOnValidThread()); |
| 393 test_task_runner_->PostTask( | 388 test_task_runner_->PostTask( |
| 394 FROM_HERE, | 389 FROM_HERE, base::Bind(&AttachmentUploaderImplTest::OnRequestReceived, |
| 395 base::Bind( | 390 test_, request)); |
| 396 &AttachmentUploaderImplTest::OnRequestReceived, test_, request)); | |
| 397 std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse); | 391 std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse); |
| 398 response->set_code(GetStatusCode()); | 392 response->set_code(GetStatusCode()); |
| 399 response->set_content_type("text/plain"); | 393 response->set_content_type("text/plain"); |
| 400 return std::move(response); | 394 return std::move(response); |
| 401 } | 395 } |
| 402 | 396 |
| 403 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { | 397 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { |
| 404 base::AutoLock lock(mutex_); | 398 base::AutoLock lock(mutex_); |
| 405 status_code_ = status_code; | 399 status_code_ = status_code; |
| 406 } | 400 } |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); | 667 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); |
| 674 hello_world->data() = "hello world"; | 668 hello_world->data() = "hello world"; |
| 675 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( | 669 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( |
| 676 ComputeCrc32c(hello_world))); | 670 ComputeCrc32c(hello_world))); |
| 677 } | 671 } |
| 678 | 672 |
| 679 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 673 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
| 680 // already exists. 409 Conflict? (bug 379825) | 674 // already exists. 409 Conflict? (bug 379825) |
| 681 | 675 |
| 682 } // namespace syncer | 676 } // namespace syncer |
| OLD | NEW |