1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/engine_impl/attachments/attachment_downloader_impl.h"
#include <map>
#include <utility>
#include "base/bind.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/histogram_tester.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/sync/engine/attachments/attachment_util.h"
#include "components/sync/engine_impl/attachments/attachment_uploader_impl.h"
#include "components/sync/model/attachments/attachment.h"
#include "google_apis/gaia/fake_oauth2_token_service.h"
#include "google_apis/gaia/gaia_constants.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/util/crc32c.h"
namespace syncer {
namespace {
const char kAccountId[] = "attachments@gmail.com";
const char kAccessToken[] = "access.token";
const char kAttachmentServerUrl[] = "http://attachments.com/";
const char kAttachmentContent[] = "attachment.content";
const char kStoreBirthday[] = "z00000000-0000-007b-0000-0000000004d2";
const ModelType kModelType = ModelType::ARTICLES;
// MockOAuth2TokenService remembers last request for access token and verifies
// that only one request is active at a time.
// Call RespondToAccessTokenRequest to respond to it.
class MockOAuth2TokenService : public FakeOAuth2TokenService {
public:
MockOAuth2TokenService() : num_invalidate_token_(0) {}
~MockOAuth2TokenService() override {}
void RespondToAccessTokenRequest(GoogleServiceAuthError error);
int num_invalidate_token() const { return num_invalidate_token_; }
protected:
void FetchOAuth2Token(RequestImpl* request,
const std::string& account_id,
net::URLRequestContextGetter* getter,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes) override;
void InvalidateAccessTokenImpl(const std::string& account_id,
const std::string& client_id,
const ScopeSet& scopes,
const std::string& access_token) override;
private:
base::WeakPtr<RequestImpl> last_request_;
int num_invalidate_token_;
};
void MockOAuth2TokenService::RespondToAccessTokenRequest(
GoogleServiceAuthError error) {
EXPECT_TRUE(last_request_);
std::string access_token;
base::Time expiration_time;
if (error == GoogleServiceAuthError::AuthErrorNone()) {
access_token = kAccessToken;
expiration_time = base::Time::Max();
}
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(&OAuth2TokenService::RequestImpl::InformConsumer,
last_request_, error, access_token, expiration_time));
}
void MockOAuth2TokenService::FetchOAuth2Token(
RequestImpl* request,
const std::string& account_id,
net::URLRequestContextGetter* getter,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes) {
// Only one request at a time is allowed.
EXPECT_FALSE(last_request_);
last_request_ = request->AsWeakPtr();
}
void MockOAuth2TokenService::InvalidateAccessTokenImpl(
const std::string& account_id,
const std::string& client_id,
const ScopeSet& scopes,
const std::string& access_token) {
++num_invalidate_token_;
}
class TokenServiceProvider
: public OAuth2TokenServiceRequest::TokenServiceProvider,
base::NonThreadSafe {
public:
explicit TokenServiceProvider(OAuth2TokenService* token_service);
// OAuth2TokenService::TokenServiceProvider implementation.
scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner()
override;
OAuth2TokenService* GetTokenService() override;
private:
~TokenServiceProvider() override;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
OAuth2TokenService* token_service_;
};
TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service)
: task_runner_(base::ThreadTaskRunnerHandle::Get()),
token_service_(token_service) {
DCHECK(token_service_);
}
TokenServiceProvider::~TokenServiceProvider() {}
scoped_refptr<base::SingleThreadTaskRunner>
TokenServiceProvider::GetTokenServiceTaskRunner() {
return task_runner_;
}
OAuth2TokenService* TokenServiceProvider::GetTokenService() {
DCHECK(task_runner_->BelongsToCurrentThread());
return token_service_;
}
} // namespace
class AttachmentDownloaderImplTest : public testing::Test {
protected:
typedef std::map<AttachmentId, AttachmentDownloader::DownloadResult>
ResultsMap;
enum HashHeaderType {
HASH_HEADER_NONE,
HASH_HEADER_VALID,
HASH_HEADER_INVALID
};
AttachmentDownloaderImplTest()
: num_completed_downloads_(0),
attachment_id_(Attachment::Create(new base::RefCountedStaticMemory(
kAttachmentContent,
strlen(kAttachmentContent)))
.GetId()) {}
void SetUp() override;
void TearDown() override;
AttachmentDownloader* downloader() { return attachment_downloader_.get(); }
MockOAuth2TokenService* token_service() { return token_service_.get(); }
int num_completed_downloads() { return num_completed_downloads_; }
const AttachmentId attachment_id() const { return attachment_id_; }
AttachmentDownloader::DownloadCallback download_callback(
const AttachmentId& id) {
return base::Bind(&AttachmentDownloaderImplTest::DownloadDone,
base::Unretained(this), id);
}
// Respond with |response_code| and hash header of type |hash_header_type|.
void CompleteDownload(int response_code, HashHeaderType hash_header_type);
void DownloadDone(const AttachmentId& attachment_id,
const AttachmentDownloader::DownloadResult& result,
std::unique_ptr<Attachment> attachment);
void VerifyDownloadResult(const AttachmentId& attachment_id,
const AttachmentDownloader::DownloadResult& result);
void RunMessageLoop();
private:
static void AddHashHeader(HashHeaderType hash_header_type,
net::TestURLFetcher* fetcher);
base::MessageLoopForIO message_loop_;
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
net::TestURLFetcherFactory url_fetcher_factory_;
std::unique_ptr<MockOAuth2TokenService> token_service_;
std::unique_ptr<AttachmentDownloader> attachment_downloader_;
ResultsMap download_results_;
int num_completed_downloads_;
const AttachmentId attachment_id_;
};
void AttachmentDownloaderImplTest::SetUp() {
url_request_context_getter_ =
new net::TestURLRequestContextGetter(message_loop_.task_runner());
url_fetcher_factory_.set_remove_fetcher_on_delete(true);
token_service_ = base::MakeUnique<MockOAuth2TokenService>();
token_service_->AddAccount(kAccountId);
scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
token_service_provider(new TokenServiceProvider(token_service_.get()));
OAuth2TokenService::ScopeSet scopes;
scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
attachment_downloader_ = AttachmentDownloader::Create(
GURL(kAttachmentServerUrl), url_request_context_getter_, kAccountId,
scopes, token_service_provider, std::string(kStoreBirthday), kModelType);
}
void AttachmentDownloaderImplTest::TearDown() {
RunMessageLoop();
}
void AttachmentDownloaderImplTest::CompleteDownload(
int response_code,
HashHeaderType hash_header_type) {
// TestURLFetcherFactory remembers last active URLFetcher.
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
// There should be outstanding url fetch request.
EXPECT_TRUE(fetcher);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(response_code);
if (response_code == net::HTTP_OK) {
fetcher->SetResponseString(kAttachmentContent);
}
AddHashHeader(hash_header_type, fetcher);
// Call URLFetcherDelegate.
net::URLFetcherDelegate* delegate = fetcher->delegate();
delegate->OnURLFetchComplete(fetcher);
RunMessageLoop();
// Once result is processed URLFetcher should be deleted.
fetcher = url_fetcher_factory_.GetFetcherByID(0);
EXPECT_FALSE(fetcher);
}
void AttachmentDownloaderImplTest::DownloadDone(
const AttachmentId& attachment_id,
const AttachmentDownloader::DownloadResult& result,
std::unique_ptr<Attachment> attachment) {
download_results_.insert(std::make_pair(attachment_id, result));
if (result == AttachmentDownloader::DOWNLOAD_SUCCESS) {
// Successful download should be accompanied by valid attachment with
// matching id and valid data.
EXPECT_TRUE(attachment);
EXPECT_EQ(attachment_id, attachment->GetId());
scoped_refptr<base::RefCountedMemory> data = attachment->GetData();
std::string data_as_string(data->front_as<char>(), data->size());
EXPECT_EQ(data_as_string, kAttachmentContent);
} else {
EXPECT_FALSE(attachment);
}
++num_completed_downloads_;
}
void AttachmentDownloaderImplTest::VerifyDownloadResult(
const AttachmentId& attachment_id,
const AttachmentDownloader::DownloadResult& result) {
ResultsMap::const_iterator iter = download_results_.find(attachment_id);
EXPECT_TRUE(iter != download_results_.end());
EXPECT_EQ(iter->second, result);
}
void AttachmentDownloaderImplTest::RunMessageLoop() {
base::RunLoop run_loop;
run_loop.RunUntilIdle();
}
void AttachmentDownloaderImplTest::AddHashHeader(
HashHeaderType hash_header_type,
net::TestURLFetcher* fetcher) {
std::string header = "X-Goog-Hash: crc32c=";
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders(""));
switch (hash_header_type) {
case HASH_HEADER_NONE:
break;
case HASH_HEADER_VALID:
header += AttachmentUploaderImpl::FormatCrc32cHash(leveldb::crc32c::Value(
kAttachmentContent, strlen(kAttachmentContent)));
headers->AddHeader(header);
break;
case HASH_HEADER_INVALID:
header += "BOGUS1==";
headers->AddHeader(header);
break;
}
fetcher->set_response_headers(headers);
}
TEST_F(AttachmentDownloaderImplTest, HappyCase) {
AttachmentId id1 = attachment_id();
// DownloadAttachment should trigger RequestAccessToken.
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
// Return valid access token.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
// Catch histogram entries.
base::HistogramTester histogram_tester;
// Check that there is outstanding URLFetcher request and complete it.
CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID);
// Verify that the response code was logged properly.
histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode",
net::HTTP_OK, 1);
// Verify that callback was called for the right id with the right result.
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS);
}
TEST_F(AttachmentDownloaderImplTest, SameIdMultipleDownloads) {
AttachmentId id1 = attachment_id();
base::HistogramTester histogram_tester;
// Call DownloadAttachment two times for the same id.
downloader()->DownloadAttachment(id1, download_callback(id1));
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
// Return valid access token.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
// Start one more download after access token is received.
downloader()->DownloadAttachment(id1, download_callback(id1));
// Complete URLFetcher request.
CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID);
// Verify that all download requests completed.
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS);
EXPECT_EQ(3, num_completed_downloads());
// Let's download the same attachment again.
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
// Verify that it didn't finish prematurely.
EXPECT_EQ(3, num_completed_downloads());
// Return valid access token.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
// Complete URLFetcher request.
CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID);
// Verify that all download requests completed.
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS);
EXPECT_EQ(4, num_completed_downloads());
histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode",
net::HTTP_OK, 2);
}
TEST_F(AttachmentDownloaderImplTest, RequestAccessTokenFails) {
AttachmentId id1 = attachment_id();
AttachmentId id2 = AttachmentId::Create(id1.GetSize(), id1.GetCrc32c());
// Trigger first RequestAccessToken.
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
// Return valid access token.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
// Trigger second RequestAccessToken.
downloader()->DownloadAttachment(id2, download_callback(id2));
RunMessageLoop();
// Fail RequestAccessToken.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
RunMessageLoop();
// Only id2 should fail.
VerifyDownloadResult(id2, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR);
// Complete request for id1.
CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID);
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS);
}
TEST_F(AttachmentDownloaderImplTest, URLFetcher_BadToken) {
AttachmentId id1 = attachment_id();
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
// Return valid access token.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
// Fail URLFetcher. This should trigger download failure and access token
// invalidation.
base::HistogramTester histogram_tester;
CompleteDownload(net::HTTP_UNAUTHORIZED, HASH_HEADER_VALID);
EXPECT_EQ(1, token_service()->num_invalidate_token());
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR);
histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode",
net::HTTP_UNAUTHORIZED, 1);
}
TEST_F(AttachmentDownloaderImplTest, URLFetcher_ServiceUnavailable) {
AttachmentId id1 = attachment_id();
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
// Return valid access token.
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
// Fail URLFetcher. This should trigger download failure. Access token
// shouldn't be invalidated.
base::HistogramTester histogram_tester;
CompleteDownload(net::HTTP_SERVICE_UNAVAILABLE, HASH_HEADER_VALID);
EXPECT_EQ(0, token_service()->num_invalidate_token());
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR);
histogram_tester.ExpectUniqueSample("Sync.Attachments.DownloadResponseCode",
net::HTTP_SERVICE_UNAVAILABLE, 1);
}
// Verify that if no hash is present on the response the downloader accepts the
// received attachment.
TEST_F(AttachmentDownloaderImplTest, NoHash) {
AttachmentId id1 = attachment_id();
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
CompleteDownload(net::HTTP_OK, HASH_HEADER_NONE);
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_SUCCESS);
}
// Verify that if an invalid hash is present on the response the downloader
// treats it as a transient error.
TEST_F(AttachmentDownloaderImplTest, InvalidHash) {
AttachmentId id1 = attachment_id();
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
CompleteDownload(net::HTTP_OK, HASH_HEADER_INVALID);
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_TRANSIENT_ERROR);
}
// Verify that when the hash from the attachment id does not match the one on
// the response the result is an unspecified error.
TEST_F(AttachmentDownloaderImplTest, IdHashDoesNotMatch) {
// id1 has the wrong crc32c.
AttachmentId id1 = AttachmentId::Create(attachment_id().GetSize(), 12345);
downloader()->DownloadAttachment(id1, download_callback(id1));
RunMessageLoop();
token_service()->RespondToAccessTokenRequest(
GoogleServiceAuthError::AuthErrorNone());
RunMessageLoop();
CompleteDownload(net::HTTP_OK, HASH_HEADER_VALID);
VerifyDownloadResult(id1, AttachmentDownloader::DOWNLOAD_UNSPECIFIED_ERROR);
}
// Verify that extract fails when there is no headers object.
TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_NoHeaders) {
uint32_t extracted;
ASSERT_FALSE(AttachmentDownloaderImpl::ExtractCrc32c(nullptr, &extracted));
}
// Verify that extract fails when there is no crc32c value.
TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_Empty) {
std::string raw;
raw += "HTTP/1.1 200 OK\n";
raw += "Foo: bar\n";
raw += "X-Goog-HASH: crc32c=\n";
raw += "\n";
std::replace(raw.begin(), raw.end(), '\n', '\0');
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders(raw));
uint32_t extracted;
ASSERT_FALSE(
AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted));
}
// Verify that extract finds the first crc32c and ignores others.
TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_First) {
const std::string expected_encoded = "z8SuHQ==";
const uint32_t expected = 3485773341;
std::string raw;
raw += "HTTP/1.1 200 OK\n";
raw += "Foo: bar\n";
// Ignored because it's the wrong header.
raw += "X-Goog-Hashes: crc32c=AAAAAA==\n";
// Header name matches. The md5 item is ignored.
raw += "X-Goog-HASH: md5=rL0Y20zC+Fzt72VPzMSk2A==,crc32c=" +
expected_encoded + "\n";
// Ignored because we already found a crc32c in the one above.
raw += "X-Goog-HASH: crc32c=AAAAAA==\n";
raw += "\n";
std::replace(raw.begin(), raw.end(), '\n', '\0');
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders(raw));
uint32_t extracted;
ASSERT_TRUE(
AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted));
ASSERT_EQ(expected, extracted);
}
// Verify that extract fails when encoded value is too long.
TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_TooLong) {
std::string raw;
raw += "HTTP/1.1 200 OK\n";
raw += "Foo: bar\n";
raw += "X-Goog-HASH: crc32c=AAAAAAAA\n";
raw += "\n";
std::replace(raw.begin(), raw.end(), '\n', '\0');
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders(raw));
uint32_t extracted;
ASSERT_FALSE(
AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted));
}
// Verify that extract fails if there is no crc32c.
TEST_F(AttachmentDownloaderImplTest, ExtractCrc32c_None) {
std::string raw;
raw += "HTTP/1.1 200 OK\n";
raw += "Foo: bar\n";
raw += "X-Goog-Hash: md5=rL0Y20zC+Fzt72VPzMSk2A==\n";
raw += "\n";
std::replace(raw.begin(), raw.end(), '\n', '\0');
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders(raw));
uint32_t extracted;
ASSERT_FALSE(
AttachmentDownloaderImpl::ExtractCrc32c(headers.get(), &extracted));
}
} // namespace syncer
|