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_downloader_impl.h" | 5 #include "components/sync/core/attachments/attachment_downloader_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/test/histogram_tester.h" | 17 #include "base/test/histogram_tester.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "components/sync/api/attachments/attachment.h" |
| 20 #include "components/sync/base/model_type.h" |
| 21 #include "components/sync/core/attachments/attachment_uploader_impl.h" |
| 22 #include "components/sync/core/attachments/attachment_util.h" |
19 #include "google_apis/gaia/fake_oauth2_token_service.h" | 23 #include "google_apis/gaia/fake_oauth2_token_service.h" |
20 #include "google_apis/gaia/gaia_constants.h" | 24 #include "google_apis/gaia/gaia_constants.h" |
21 #include "net/http/http_response_headers.h" | 25 #include "net/http/http_response_headers.h" |
22 #include "net/url_request/test_url_fetcher_factory.h" | 26 #include "net/url_request/test_url_fetcher_factory.h" |
23 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
24 #include "sync/api/attachments/attachment.h" | |
25 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h" | |
26 #include "sync/internal_api/public/attachments/attachment_util.h" | |
27 #include "sync/internal_api/public/base/model_type.h" | |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "third_party/leveldatabase/src/util/crc32c.h" | 29 #include "third_party/leveldatabase/src/util/crc32c.h" |
30 | 30 |
31 namespace syncer { | 31 namespace syncer { |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 const char kAccountId[] = "attachments@gmail.com"; | 35 const char kAccountId[] = "attachments@gmail.com"; |
36 const char kAccessToken[] = "access.token"; | 36 const char kAccessToken[] = "access.token"; |
37 const char kAttachmentServerUrl[] = "http://attachments.com/"; | 37 const char kAttachmentServerUrl[] = "http://attachments.com/"; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 122 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
123 OAuth2TokenService* token_service_; | 123 OAuth2TokenService* token_service_; |
124 }; | 124 }; |
125 | 125 |
126 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service) | 126 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service) |
127 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 127 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
128 token_service_(token_service) { | 128 token_service_(token_service) { |
129 DCHECK(token_service_); | 129 DCHECK(token_service_); |
130 } | 130 } |
131 | 131 |
132 TokenServiceProvider::~TokenServiceProvider() { | 132 TokenServiceProvider::~TokenServiceProvider() {} |
133 } | |
134 | 133 |
135 scoped_refptr<base::SingleThreadTaskRunner> | 134 scoped_refptr<base::SingleThreadTaskRunner> |
136 TokenServiceProvider::GetTokenServiceTaskRunner() { | 135 TokenServiceProvider::GetTokenServiceTaskRunner() { |
137 return task_runner_; | 136 return task_runner_; |
138 } | 137 } |
139 | 138 |
140 OAuth2TokenService* TokenServiceProvider::GetTokenService() { | 139 OAuth2TokenService* TokenServiceProvider::GetTokenService() { |
141 DCHECK(task_runner_->BelongsToCurrentThread()); | 140 DCHECK(task_runner_->BelongsToCurrentThread()); |
142 return token_service_; | 141 return token_service_; |
143 } | 142 } |
144 | 143 |
145 } // namespace | 144 } // namespace |
146 | 145 |
147 class AttachmentDownloaderImplTest : public testing::Test { | 146 class AttachmentDownloaderImplTest : public testing::Test { |
148 protected: | 147 protected: |
149 typedef std::map<AttachmentId, AttachmentDownloader::DownloadResult> | 148 typedef std::map<AttachmentId, AttachmentDownloader::DownloadResult> |
150 ResultsMap; | 149 ResultsMap; |
151 | 150 |
152 enum HashHeaderType { | 151 enum HashHeaderType { |
153 HASH_HEADER_NONE, | 152 HASH_HEADER_NONE, |
154 HASH_HEADER_VALID, | 153 HASH_HEADER_VALID, |
155 HASH_HEADER_INVALID | 154 HASH_HEADER_INVALID |
156 }; | 155 }; |
157 | 156 |
158 AttachmentDownloaderImplTest() | 157 AttachmentDownloaderImplTest() |
159 : num_completed_downloads_(0), | 158 : num_completed_downloads_(0), |
160 attachment_id_( | 159 attachment_id_(Attachment::Create(new base::RefCountedStaticMemory( |
161 Attachment::Create(new base::RefCountedStaticMemory( | 160 kAttachmentContent, |
162 kAttachmentContent, | 161 strlen(kAttachmentContent))) |
163 strlen(kAttachmentContent))).GetId()) {} | 162 .GetId()) {} |
164 | 163 |
165 void SetUp() override; | 164 void SetUp() override; |
166 void TearDown() override; | 165 void TearDown() override; |
167 | 166 |
168 AttachmentDownloader* downloader() { return attachment_downloader_.get(); } | 167 AttachmentDownloader* downloader() { return attachment_downloader_.get(); } |
169 | 168 |
170 MockOAuth2TokenService* token_service() { return token_service_.get(); } | 169 MockOAuth2TokenService* token_service() { return token_service_.get(); } |
171 | 170 |
172 int num_completed_downloads() { return num_completed_downloads_; } | 171 int num_completed_downloads() { return num_completed_downloads_; } |
173 | 172 |
174 const AttachmentId attachment_id() const { return attachment_id_; } | 173 const AttachmentId attachment_id() const { return attachment_id_; } |
175 | 174 |
176 AttachmentDownloader::DownloadCallback download_callback( | 175 AttachmentDownloader::DownloadCallback download_callback( |
177 const AttachmentId& id) { | 176 const AttachmentId& id) { |
178 return base::Bind(&AttachmentDownloaderImplTest::DownloadDone, | 177 return base::Bind(&AttachmentDownloaderImplTest::DownloadDone, |
179 base::Unretained(this), | 178 base::Unretained(this), id); |
180 id); | |
181 } | 179 } |
182 | 180 |
183 // Respond with |response_code| and hash header of type |hash_header_type|. | 181 // Respond with |response_code| and hash header of type |hash_header_type|. |
184 void CompleteDownload(int response_code, HashHeaderType hash_header_type); | 182 void CompleteDownload(int response_code, HashHeaderType hash_header_type); |
185 | 183 |
186 void DownloadDone(const AttachmentId& attachment_id, | 184 void DownloadDone(const AttachmentId& attachment_id, |
187 const AttachmentDownloader::DownloadResult& result, | 185 const AttachmentDownloader::DownloadResult& result, |
188 std::unique_ptr<Attachment> attachment); | 186 std::unique_ptr<Attachment> attachment); |
189 | 187 |
190 void VerifyDownloadResult(const AttachmentId& attachment_id, | 188 void VerifyDownloadResult(const AttachmentId& attachment_id, |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 AttachmentId id1 = AttachmentId::Create(attachment_id().GetSize(), 12345); | 452 AttachmentId id1 = AttachmentId::Create(attachment_id().GetSize(), 12345); |
455 downloader()->DownloadAttachment(id1, download_callback(id1)); | 453 downloader()->DownloadAttachment(id1, download_callback(id1)); |
456 RunMessageLoop(); | 454 RunMessageLoop(); |
457 token_service()->RespondToAccessTokenRequest( | 455 token_service()->RespondToAccessTokenRequest( |
458 GoogleServiceAuthError::AuthErrorNone()); | 456 GoogleServiceAuthError::AuthErrorNone()); |
459 RunMessageLoop(); | 457 RunMessageLoop(); |
460 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); | 458 CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID); |
461 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_UNSPECIFIED_ERROR); | 459 VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_UNSPECIFIED_ERROR); |
462 } | 460 } |
463 | 461 |
464 | |
465 // Verify that extract fails when there is no headers object. | 462 // Verify that extract fails when there is no headers object. |
466 TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_NoHeaders) { | 463 TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_NoHeaders) { |
467 uint32_t extracted; | 464 uint32_t extracted; |
468 ASSERT_FALSE(AttachmentDownloaderImpl::ExtractCrc32c(nullptr, &extracted)); | 465 ASSERT_FALSE(AttachmentDownloaderImpl::ExtractCrc32c(nullptr, &extracted)); |
469 } | 466 } |
470 | 467 |
471 // Verify that extract fails when there is no crc32c value. | 468 // Verify that extract fails when there is no crc32c value. |
472 TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_Empty) { | 469 TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_Empty) { |
473 std::string raw; | 470 std::string raw; |
474 raw += "HTTP/1.1 200 OK\n"; | 471 raw += "HTTP/1.1 200 OK\n"; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 raw += "\n"; | 528 raw += "\n"; |
532 std::replace(raw.begin(), raw.end(), '\n', '\0'); | 529 std::replace(raw.begin(), raw.end(), '\n', '\0'); |
533 scoped_refptr<net::HttpResponseHeaders> headers( | 530 scoped_refptr<net::HttpResponseHeaders> headers( |
534 new net::HttpResponseHeaders(raw)); | 531 new net::HttpResponseHeaders(raw)); |
535 uint32_t extracted; | 532 uint32_t extracted; |
536 ASSERT_FALSE( | 533 ASSERT_FALSE( |
537 AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted)); | 534 AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted)); |
538 } | 535 } |
539 | 536 |
540 } // namespace syncer | 537 } // namespace syncer |
OLD | NEW |