| 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 "sync/internal_api/public/attachments/attachment_uploader_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "google_apis/gaia/fake_oauth2_token_service.h" | 18 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 19 #include "google_apis/gaia/gaia_constants.h" | 19 #include "google_apis/gaia/gaia_constants.h" |
| 20 #include "google_apis/gaia/oauth2_token_service_request.h" | 20 #include "google_apis/gaia/oauth2_token_service_request.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 #include "net/test/embedded_test_server/http_request.h" | 22 #include "net/test/embedded_test_server/http_request.h" |
| 23 #include "net/test/embedded_test_server/http_response.h" | 23 #include "net/test/embedded_test_server/http_response.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 25 #include "sync/api/attachments/attachment.h" | 25 #include "sync/api/attachments/attachment.h" |
| 26 #include "sync/internal_api/public/attachments/attachment_util.h" |
| 26 #include "sync/protocol/sync.pb.h" | 27 #include "sync/protocol/sync.pb.h" |
| 27 #include "testing/gmock/include/gmock/gmock-matchers.h" | 28 #include "testing/gmock/include/gmock/gmock-matchers.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const char kAttachmentData[] = "some data"; | 33 const char kAttachmentData[] = "some data"; |
| 33 const char kAccountId[] = "some-account-id"; | 34 const char kAccountId[] = "some-account-id"; |
| 34 const char kAccessToken[] = "some-access-token"; | 35 const char kAccessToken[] = "some-access-token"; |
| 35 const char kAuthorization[] = "Authorization"; | 36 const char kAuthorization[] = "Authorization"; |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 run_loop.Run(); | 306 run_loop.Run(); |
| 306 } | 307 } |
| 307 } | 308 } |
| 308 | 309 |
| 309 Attachment AttachmentUploaderImplTest::UploadAndRespondWith( | 310 Attachment AttachmentUploaderImplTest::UploadAndRespondWith( |
| 310 const net::HttpStatusCode& status_code) { | 311 const net::HttpStatusCode& status_code) { |
| 311 token_service().AddAccount(kAccountId); | 312 token_service().AddAccount(kAccountId); |
| 312 request_handler().SetStatusCode(status_code); | 313 request_handler().SetStatusCode(status_code); |
| 313 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 314 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 314 some_data->data() = kAttachmentData; | 315 some_data->data() = kAttachmentData; |
| 315 Attachment attachment = Attachment::Create(some_data); | 316 Attachment attachment = Attachment::CreateNew(some_data); |
| 316 uploader()->UploadAttachment(attachment, upload_callback()); | 317 uploader()->UploadAttachment(attachment, upload_callback()); |
| 317 return attachment; | 318 return attachment; |
| 318 } | 319 } |
| 319 | 320 |
| 320 scoped_ptr<AttachmentUploader>& AttachmentUploaderImplTest::uploader() { | 321 scoped_ptr<AttachmentUploader>& AttachmentUploaderImplTest::uploader() { |
| 321 return uploader_; | 322 return uploader_; |
| 322 } | 323 } |
| 323 | 324 |
| 324 const AttachmentUploader::UploadCallback& | 325 const AttachmentUploader::UploadCallback& |
| 325 AttachmentUploaderImplTest::upload_callback() const { | 326 AttachmentUploaderImplTest::upload_callback() const { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 | 527 |
| 527 // Verify that we do not issue an HTTP request when we fail to receive an access | 528 // Verify that we do not issue an HTTP request when we fail to receive an access |
| 528 // token. | 529 // token. |
| 529 // | 530 // |
| 530 // Token is requested, no token is returned, no HTTP request is made | 531 // Token is requested, no token is returned, no HTTP request is made |
| 531 TEST_F(AttachmentUploaderImplTest, UploadAttachment_FailToGetToken) { | 532 TEST_F(AttachmentUploaderImplTest, UploadAttachment_FailToGetToken) { |
| 532 // Note, we won't receive a token because we did not add kAccountId to the | 533 // Note, we won't receive a token because we did not add kAccountId to the |
| 533 // token service. | 534 // token service. |
| 534 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 535 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 535 some_data->data() = kAttachmentData; | 536 some_data->data() = kAttachmentData; |
| 536 Attachment attachment = Attachment::Create(some_data); | 537 Attachment attachment = Attachment::CreateNew(some_data); |
| 537 uploader()->UploadAttachment(attachment, upload_callback()); | 538 uploader()->UploadAttachment(attachment, upload_callback()); |
| 538 | 539 |
| 539 RunAndWaitFor(1); | 540 RunAndWaitFor(1); |
| 540 | 541 |
| 541 // See that the done callback was invoked. | 542 // See that the done callback was invoked. |
| 542 ASSERT_EQ(1U, upload_results().size()); | 543 ASSERT_EQ(1U, upload_results().size()); |
| 543 EXPECT_EQ(AttachmentUploader::UPLOAD_TRANSIENT_ERROR, upload_results()[0]); | 544 EXPECT_EQ(AttachmentUploader::UPLOAD_TRANSIENT_ERROR, upload_results()[0]); |
| 544 ASSERT_EQ(1U, attachment_ids().size()); | 545 ASSERT_EQ(1U, attachment_ids().size()); |
| 545 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); | 546 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 546 | 547 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 EXPECT_EQ(kAttachmentData, http_request.content); | 625 EXPECT_EQ(kAttachmentData, http_request.content); |
| 625 | 626 |
| 626 // See that we invalidated the token. | 627 // See that we invalidated the token. |
| 627 ASSERT_EQ(1, token_service().num_invalidate_token()); | 628 ASSERT_EQ(1, token_service().num_invalidate_token()); |
| 628 } | 629 } |
| 629 | 630 |
| 630 TEST_F(AttachmentUploaderImplTest, ComputeCrc32cHash) { | 631 TEST_F(AttachmentUploaderImplTest, ComputeCrc32cHash) { |
| 631 scoped_refptr<base::RefCountedString> empty(new base::RefCountedString); | 632 scoped_refptr<base::RefCountedString> empty(new base::RefCountedString); |
| 632 empty->data() = ""; | 633 empty->data() = ""; |
| 633 EXPECT_EQ("AAAAAA==", | 634 EXPECT_EQ("AAAAAA==", |
| 634 AttachmentUploaderImpl::ComputeCrc32cHash(empty->front_as<char>(), | 635 AttachmentUploaderImpl::FormatCrc32cHash(ComputeCrc32c(empty))); |
| 635 empty->size())); | |
| 636 | 636 |
| 637 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); | 637 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); |
| 638 hello_world->data() = "hello world"; | 638 hello_world->data() = "hello world"; |
| 639 EXPECT_EQ("yZRlqg==", | 639 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( |
| 640 AttachmentUploaderImpl::ComputeCrc32cHash( | 640 ComputeCrc32c(hello_world))); |
| 641 hello_world->front_as<char>(), hello_world->size())); | |
| 642 } | 641 } |
| 643 | 642 |
| 644 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 643 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
| 645 // already exists. 409 Conflict? (bug 379825) | 644 // already exists. 409 Conflict? (bug 379825) |
| 646 | 645 |
| 647 } // namespace syncer | 646 } // namespace syncer |
| OLD | NEW |