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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
|
// 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 "content/browser/service_worker/service_worker_cache.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/run_loop.h"
#include "content/browser/fileapi/chrome_blob_storage_context.h"
#include "content/browser/fileapi/mock_url_request_delegate.h"
#include "content/browser/quota/mock_quota_manager_proxy.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/referrer.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/blob/blob_url_request_job_factory.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/common/blob/blob_data.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
const char kTestData[] = "Hello World";
// Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
// the memory.
storage::BlobProtocolHandler* CreateMockBlobProtocolHandler(
storage::BlobStorageContext* blob_storage_context) {
// The FileSystemContext and MessageLoopProxy are not actually used but a
// MessageLoopProxy is needed to avoid a DCHECK in BlobURLRequestJob ctor.
return new storage::BlobProtocolHandler(
blob_storage_context, NULL, base::MessageLoopProxy::current().get());
}
// A disk_cache::Backend wrapper that can delay operations.
class DelayableBackend : public disk_cache::Backend {
public:
DelayableBackend(scoped_ptr<disk_cache::Backend> backend)
: backend_(backend.Pass()), delay_open_(false) {}
// disk_cache::Backend overrides
net::CacheType GetCacheType() const override {
return backend_->GetCacheType();
}
int32 GetEntryCount() const override { return backend_->GetEntryCount(); }
int OpenEntry(const std::string& key,
disk_cache::Entry** entry,
const CompletionCallback& callback) override {
if (delay_open_) {
open_entry_callback_ =
base::Bind(&DelayableBackend::OpenEntryDelayedImpl,
base::Unretained(this), key, entry, callback);
return net::ERR_IO_PENDING;
}
return backend_->OpenEntry(key, entry, callback);
}
int CreateEntry(const std::string& key,
disk_cache::Entry** entry,
const CompletionCallback& callback) override {
return backend_->CreateEntry(key, entry, callback);
}
int DoomEntry(const std::string& key,
const CompletionCallback& callback) override {
return backend_->DoomEntry(key, callback);
}
int DoomAllEntries(const CompletionCallback& callback) override {
return backend_->DoomAllEntries(callback);
}
int DoomEntriesBetween(base::Time initial_time,
base::Time end_time,
const CompletionCallback& callback) override {
return backend_->DoomEntriesBetween(initial_time, end_time, callback);
}
int DoomEntriesSince(base::Time initial_time,
const CompletionCallback& callback) override {
return backend_->DoomEntriesSince(initial_time, callback);
}
scoped_ptr<Iterator> CreateIterator() override {
return backend_->CreateIterator();
}
void GetStats(
std::vector<std::pair<std::string, std::string>>* stats) override {
return backend_->GetStats(stats);
}
void OnExternalCacheHit(const std::string& key) override {
return backend_->OnExternalCacheHit(key);
}
// Call to continue a delayed open.
void OpenEntryContinue() {
EXPECT_FALSE(open_entry_callback_.is_null());
open_entry_callback_.Run();
}
void set_delay_open(bool value) { delay_open_ = value; }
private:
void OpenEntryDelayedImpl(const std::string& key,
disk_cache::Entry** entry,
const CompletionCallback& callback) {
int rv = backend_->OpenEntry(key, entry, callback);
if (rv != net::ERR_IO_PENDING)
callback.Run(rv);
}
scoped_ptr<disk_cache::Backend> backend_;
bool delay_open_;
base::Closure open_entry_callback_;
};
} // namespace
// A ServiceWorkerCache that can optionally delay during backend creation.
class TestServiceWorkerCache : public ServiceWorkerCache {
public:
TestServiceWorkerCache(
const GURL& origin,
const base::FilePath& path,
net::URLRequestContext* request_context,
const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
base::WeakPtr<storage::BlobStorageContext> blob_context)
: ServiceWorkerCache(origin,
path,
request_context,
quota_manager_proxy,
blob_context),
delay_backend_creation_(false) {}
void CreateBackend(const ErrorCallback& callback) override {
backend_creation_callback_ = callback;
if (delay_backend_creation_)
return;
ContinueCreateBackend();
}
void ContinueCreateBackend() {
ServiceWorkerCache::CreateBackend(backend_creation_callback_);
}
void set_delay_backend_creation(bool delay) {
delay_backend_creation_ = delay;
}
// Swap the existing backend with a delayable one. The backend must have been
// created before calling this.
DelayableBackend* UseDelayableBackend() {
EXPECT_TRUE(backend_);
DelayableBackend* delayable_backend = new DelayableBackend(backend_.Pass());
backend_.reset(delayable_backend);
return delayable_backend;
}
private:
~TestServiceWorkerCache() override {}
bool delay_backend_creation_;
ErrorCallback backend_creation_callback_;
DISALLOW_COPY_AND_ASSIGN(TestServiceWorkerCache);
};
class ServiceWorkerCacheTest : public testing::Test {
public:
ServiceWorkerCacheTest()
: browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
callback_error_(ServiceWorkerCache::ErrorTypeOK),
callback_closed_(false) {}
void SetUp() override {
ChromeBlobStorageContext* blob_storage_context =
ChromeBlobStorageContext::GetFor(&browser_context_);
// Wait for chrome_blob_storage_context to finish initializing.
base::RunLoop().RunUntilIdle();
blob_storage_context_ = blob_storage_context->context();
quota_manager_proxy_ = new MockQuotaManagerProxy(
nullptr, base::MessageLoopProxy::current().get());
url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
url_request_job_factory_->SetProtocolHandler(
"blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
net::URLRequestContext* url_request_context =
browser_context_.GetRequestContext()->GetURLRequestContext();
url_request_context->set_job_factory(url_request_job_factory_.get());
CreateRequests(blob_storage_context);
if (!MemoryOnly())
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path();
cache_ = make_scoped_refptr(new TestServiceWorkerCache(
GURL("http://example.com"), path, url_request_context,
quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr()));
}
void TearDown() override {
quota_manager_proxy_->SimulateQuotaManagerDestroyed();
base::RunLoop().RunUntilIdle();
}
void CreateRequests(ChromeBlobStorageContext* blob_storage_context) {
ServiceWorkerHeaderMap headers;
headers.insert(std::make_pair("a", "a"));
headers.insert(std::make_pair("b", "b"));
body_request_ =
ServiceWorkerFetchRequest(GURL("http://example.com/body.html"), "GET",
headers, Referrer(), false);
no_body_request_ =
ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"),
"GET",
headers,
Referrer(),
false);
std::string expected_response;
for (int i = 0; i < 100; ++i)
expected_blob_data_ += kTestData;
scoped_refptr<storage::BlobData> blob_data(
new storage::BlobData("blob-id:myblob"));
blob_data->AppendData(expected_blob_data_);
blob_handle_ =
blob_storage_context->context()->AddFinishedBlob(blob_data.get());
body_response_ =
ServiceWorkerResponse(GURL("http://example.com/body.html"),
200,
"OK",
blink::WebServiceWorkerResponseTypeDefault,
headers,
blob_handle_->uuid(),
expected_blob_data_.size(),
GURL());
no_body_response_ =
ServiceWorkerResponse(GURL("http://example.com/no_body.html"),
200,
"OK",
blink::WebServiceWorkerResponseTypeDefault,
headers,
"",
0,
GURL());
}
scoped_ptr<ServiceWorkerFetchRequest> CopyFetchRequest(
const ServiceWorkerFetchRequest& request) {
return make_scoped_ptr(new ServiceWorkerFetchRequest(request.url,
request.method,
request.headers,
request.referrer,
request.is_reload));
}
scoped_ptr<ServiceWorkerResponse> CopyFetchResponse(
const ServiceWorkerResponse& response) {
scoped_ptr<ServiceWorkerResponse> sw_response(
new ServiceWorkerResponse(response.url,
response.status_code,
response.status_text,
response.response_type,
response.headers,
response.blob_uuid,
response.blob_size,
response.stream_url));
return sw_response.Pass();
}
bool Put(const ServiceWorkerFetchRequest& request,
const ServiceWorkerResponse& response) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Put(CopyFetchRequest(request),
CopyFetchResponse(response),
base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
base::Unretained(this),
base::Unretained(loop.get())));
// TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle()
// once the cache uses a passed in MessageLoopProxy instead of the CACHE
// thread.
loop->Run();
return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
}
bool Match(const ServiceWorkerFetchRequest& request) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Match(CopyFetchRequest(request),
base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
base::Unretained(this),
base::Unretained(loop.get())));
loop->Run();
return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
}
bool Delete(const ServiceWorkerFetchRequest& request) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Delete(CopyFetchRequest(request),
base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
base::Unretained(this),
base::Unretained(loop.get())));
loop->Run();
return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
}
bool Keys() {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Keys(base::Bind(&ServiceWorkerCacheTest::RequestsCallback,
base::Unretained(this),
base::Unretained(loop.get())));
loop->Run();
return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
}
bool Close() {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback,
base::Unretained(this),
base::Unretained(loop.get())));
loop->Run();
return callback_closed_;
}
void RequestsCallback(base::RunLoop* run_loop,
ServiceWorkerCache::ErrorType error,
scoped_ptr<ServiceWorkerCache::Requests> requests) {
callback_error_ = error;
callback_strings_.clear();
if (requests) {
for (size_t i = 0u; i < requests->size(); ++i)
callback_strings_.push_back(requests->at(i).url.spec());
}
if (run_loop)
run_loop->Quit();
}
void ErrorTypeCallback(base::RunLoop* run_loop,
ServiceWorkerCache::ErrorType error) {
callback_error_ = error;
if (run_loop)
run_loop->Quit();
}
void ResponseAndErrorCallback(
base::RunLoop* run_loop,
ServiceWorkerCache::ErrorType error,
scoped_ptr<ServiceWorkerResponse> response,
scoped_ptr<storage::BlobDataHandle> body_handle) {
callback_error_ = error;
callback_response_ = response.Pass();
callback_response_data_.reset();
if (error == ServiceWorkerCache::ErrorTypeOK &&
!callback_response_->blob_uuid.empty()) {
callback_response_data_ = body_handle.Pass();
}
if (run_loop)
run_loop->Quit();
}
void CloseCallback(base::RunLoop* run_loop) {
EXPECT_FALSE(callback_closed_);
callback_closed_ = true;
if (run_loop)
run_loop->Quit();
}
void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) {
storage::BlobData* data = blob_handle->data();
std::vector<storage::BlobData::Item> items = data->items();
for (size_t i = 0, max = items.size(); i < max; ++i)
output->append(items[i].bytes(), items[i].length());
}
bool VerifyKeys(const std::vector<std::string>& expected_keys) {
if (expected_keys.size() != callback_strings_.size())
return false;
std::set<std::string> found_set;
for (int i = 0, max = callback_strings_.size(); i < max; ++i)
found_set.insert(callback_strings_[i]);
for (int i = 0, max = expected_keys.size(); i < max; ++i) {
if (found_set.find(expected_keys[i]) == found_set.end())
return false;
}
return true;
}
bool TestResponseType(blink::WebServiceWorkerResponseType response_type) {
body_response_.response_type = response_type;
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_TRUE(Delete(body_request_));
return response_type == callback_response_->response_type;
}
void VerifyAllOpsFail() {
EXPECT_FALSE(Put(no_body_request_, no_body_response_));
EXPECT_FALSE(Match(no_body_request_));
EXPECT_FALSE(Delete(body_request_));
EXPECT_FALSE(Keys());
}
virtual bool MemoryOnly() { return false; }
protected:
TestBrowserContext browser_context_;
TestBrowserThreadBundle browser_thread_bundle_;
scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
storage::BlobStorageContext* blob_storage_context_;
base::ScopedTempDir temp_dir_;
scoped_refptr<TestServiceWorkerCache> cache_;
ServiceWorkerFetchRequest body_request_;
ServiceWorkerResponse body_response_;
ServiceWorkerFetchRequest no_body_request_;
ServiceWorkerResponse no_body_response_;
scoped_ptr<storage::BlobDataHandle> blob_handle_;
std::string expected_blob_data_;
ServiceWorkerCache::ErrorType callback_error_;
scoped_ptr<ServiceWorkerResponse> callback_response_;
scoped_ptr<storage::BlobDataHandle> callback_response_data_;
std::vector<std::string> callback_strings_;
bool callback_closed_;
};
class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
public testing::WithParamInterface<bool> {
bool MemoryOnly() override { return !GetParam(); }
};
class ServiceWorkerCacheMemoryOnlyTest
: public ServiceWorkerCacheTest,
public testing::WithParamInterface<bool> {
bool MemoryOnly() override { return true; }
};
TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(callback_response_);
EXPECT_STREQ(no_body_response_.url.spec().c_str(),
callback_response_->url.spec().c_str());
EXPECT_FALSE(callback_response_data_);
EXPECT_STREQ("", callback_response_->blob_uuid.c_str());
EXPECT_EQ(0u, callback_response_->blob_size);
}
TEST_P(ServiceWorkerCacheTestP, PutBody) {
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(callback_response_);
EXPECT_STREQ(body_response_.url.spec().c_str(),
callback_response_->url.spec().c_str());
EXPECT_TRUE(callback_response_data_);
EXPECT_STRNE("", callback_response_->blob_uuid.c_str());
EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size);
std::string response_body;
CopyBody(callback_response_data_.get(), &response_body);
EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
}
TEST_P(ServiceWorkerCacheTestP, ResponseURLDiffersFromRequestURL) {
no_body_response_.url = GURL("http://example.com/foobar");
EXPECT_STRNE("http://example.com/foobar",
no_body_request_.url.spec().c_str());
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Match(no_body_request_));
EXPECT_STREQ("http://example.com/foobar",
callback_response_->url.spec().c_str());
}
TEST_P(ServiceWorkerCacheTestP, ResponseURLEmpty) {
no_body_response_.url = GURL();
EXPECT_STRNE("", no_body_request_.url.spec().c_str());
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Match(no_body_request_));
EXPECT_STREQ("", callback_response_->url.spec().c_str());
}
TEST_F(ServiceWorkerCacheTest, PutBodyDropBlobRef) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_->Put(CopyFetchRequest(body_request_),
CopyFetchResponse(body_response_),
base::Bind(&ServiceWorkerCacheTestP::ResponseAndErrorCallback,
base::Unretained(this),
base::Unretained(loop.get())));
// The handle should be held by the cache now so the deref here should be
// okay.
blob_handle_.reset();
loop->Run();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
}
TEST_P(ServiceWorkerCacheTestP, PutReplace) {
EXPECT_TRUE(Put(body_request_, no_body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_FALSE(callback_response_data_);
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_TRUE(callback_response_data_);
EXPECT_TRUE(Put(body_request_, no_body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_FALSE(callback_response_data_);
}
TEST_P(ServiceWorkerCacheTestP, MatchNoBody) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Match(no_body_request_));
EXPECT_EQ(200, callback_response_->status_code);
EXPECT_STREQ("OK", callback_response_->status_text.c_str());
EXPECT_STREQ("http://example.com/no_body.html",
callback_response_->url.spec().c_str());
EXPECT_STREQ("", callback_response_->blob_uuid.c_str());
EXPECT_EQ(0u, callback_response_->blob_size);
}
TEST_P(ServiceWorkerCacheTestP, MatchBody) {
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_EQ(200, callback_response_->status_code);
EXPECT_STREQ("OK", callback_response_->status_text.c_str());
EXPECT_STREQ("http://example.com/body.html",
callback_response_->url.spec().c_str());
EXPECT_STRNE("", callback_response_->blob_uuid.c_str());
EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size);
std::string response_body;
CopyBody(callback_response_data_.get(), &response_body);
EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
}
TEST_P(ServiceWorkerCacheTestP, Vary) {
body_request_.headers["vary_foo"] = "foo";
body_response_.headers["vary"] = "vary_foo";
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
body_request_.headers["vary_foo"] = "bar";
EXPECT_FALSE(Match(body_request_));
body_request_.headers.erase("vary_foo");
EXPECT_FALSE(Match(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, EmptyVary) {
body_response_.headers["vary"] = "";
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
body_request_.headers["zoo"] = "zoo";
EXPECT_TRUE(Match(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, NoVaryButDiffHeaders) {
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
body_request_.headers["zoo"] = "zoo";
EXPECT_TRUE(Match(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, VaryMultiple) {
body_request_.headers["vary_foo"] = "foo";
body_request_.headers["vary_bar"] = "bar";
body_response_.headers["vary"] = " vary_foo , vary_bar";
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
body_request_.headers["vary_bar"] = "foo";
EXPECT_FALSE(Match(body_request_));
body_request_.headers.erase("vary_bar");
EXPECT_FALSE(Match(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, VaryNewHeader) {
body_request_.headers["vary_foo"] = "foo";
body_response_.headers["vary"] = " vary_foo, vary_bar";
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
body_request_.headers["vary_bar"] = "bar";
EXPECT_FALSE(Match(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, VaryStar) {
body_response_.headers["vary"] = "*";
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_FALSE(Match(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, EmptyKeys) {
EXPECT_TRUE(Keys());
EXPECT_EQ(0u, callback_strings_.size());
}
TEST_P(ServiceWorkerCacheTestP, TwoKeys) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Keys());
EXPECT_EQ(2u, callback_strings_.size());
std::vector<std::string> expected_keys;
expected_keys.push_back(no_body_request_.url.spec());
expected_keys.push_back(body_request_.url.spec());
EXPECT_TRUE(VerifyKeys(expected_keys));
}
TEST_P(ServiceWorkerCacheTestP, TwoKeysThenOne) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Keys());
EXPECT_EQ(2u, callback_strings_.size());
std::vector<std::string> expected_keys;
expected_keys.push_back(no_body_request_.url.spec());
expected_keys.push_back(body_request_.url.spec());
EXPECT_TRUE(VerifyKeys(expected_keys));
EXPECT_TRUE(Delete(body_request_));
EXPECT_TRUE(Keys());
EXPECT_EQ(1u, callback_strings_.size());
std::vector<std::string> expected_key;
expected_key.push_back(no_body_request_.url.spec());
EXPECT_TRUE(VerifyKeys(expected_key));
}
TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Match(no_body_request_));
EXPECT_TRUE(Delete(no_body_request_));
EXPECT_FALSE(Match(no_body_request_));
EXPECT_FALSE(Delete(no_body_request_));
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Match(no_body_request_));
EXPECT_TRUE(Delete(no_body_request_));
}
TEST_P(ServiceWorkerCacheTestP, DeleteBody) {
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_TRUE(Delete(body_request_));
EXPECT_FALSE(Match(body_request_));
EXPECT_FALSE(Delete(body_request_));
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Match(body_request_));
EXPECT_TRUE(Delete(body_request_));
}
TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) {
for (int i = 0; i < 100; ++i) {
EXPECT_FALSE(Match(no_body_request_));
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(Match(no_body_request_));
EXPECT_TRUE(Delete(no_body_request_));
}
}
TEST_P(ServiceWorkerCacheTestP, QuickStressBody) {
for (int i = 0; i < 100; ++i) {
ASSERT_FALSE(Match(body_request_));
ASSERT_TRUE(Put(body_request_, body_response_));
ASSERT_TRUE(Match(body_request_));
ASSERT_TRUE(Delete(body_request_));
}
}
TEST_P(ServiceWorkerCacheTestP, PutResponseType) {
EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeBasic));
EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeCORS));
EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeDefault));
EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeError));
EXPECT_TRUE(TestResponseType(blink::WebServiceWorkerResponseTypeOpaque));
}
TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerResponseHeaders) {
// ServiceWorkerCache depends on ServiceWorkerResponse having caseless
// headers so that it can quickly lookup vary headers.
ServiceWorkerResponse response(GURL("http://www.example.com"),
200,
"OK",
blink::WebServiceWorkerResponseTypeDefault,
ServiceWorkerHeaderMap(),
"",
0,
GURL());
response.headers["content-type"] = "foo";
response.headers["Content-Type"] = "bar";
EXPECT_EQ("bar", response.headers["content-type"]);
}
TEST_F(ServiceWorkerCacheTest, CaselessServiceWorkerFetchRequestHeaders) {
// ServiceWorkerCache depends on ServiceWorkerFetchRequest having caseless
// headers so that it can quickly lookup vary headers.
ServiceWorkerFetchRequest request(GURL("http://www.example.com"),
"GET",
ServiceWorkerHeaderMap(),
Referrer(),
false);
request.headers["content-type"] = "foo";
request.headers["Content-Type"] = "bar";
EXPECT_EQ("bar", request.headers["content-type"]);
}
TEST_P(ServiceWorkerCacheTestP, QuotaManagerModified) {
EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count());
EXPECT_LT(0, quota_manager_proxy_->last_notified_delta());
int64 sum_delta = quota_manager_proxy_->last_notified_delta();
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_EQ(2, quota_manager_proxy_->notify_storage_modified_count());
EXPECT_LT(sum_delta, quota_manager_proxy_->last_notified_delta());
sum_delta += quota_manager_proxy_->last_notified_delta();
EXPECT_TRUE(Delete(body_request_));
EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count());
sum_delta += quota_manager_proxy_->last_notified_delta();
EXPECT_TRUE(Delete(no_body_request_));
EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count());
sum_delta += quota_manager_proxy_->last_notified_delta();
EXPECT_EQ(0, sum_delta);
}
TEST_F(ServiceWorkerCacheMemoryOnlyTest, MemoryBackedSize) {
EXPECT_EQ(0, cache_->MemoryBackedSize());
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_LT(0, cache_->MemoryBackedSize());
int64 no_body_size = cache_->MemoryBackedSize();
EXPECT_TRUE(Delete(no_body_request_));
EXPECT_EQ(0, cache_->MemoryBackedSize());
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_LT(no_body_size, cache_->MemoryBackedSize());
EXPECT_TRUE(Delete(body_request_));
EXPECT_EQ(0, cache_->MemoryBackedSize());
}
TEST_F(ServiceWorkerCacheTest, MemoryBackedSizePersistent) {
EXPECT_EQ(0, cache_->MemoryBackedSize());
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_EQ(0, cache_->MemoryBackedSize());
}
TEST_P(ServiceWorkerCacheTestP, OpsFailOnClosedBackendNeverCreated) {
cache_->set_delay_backend_creation(
true); // Will hang the test if a backend is created.
EXPECT_TRUE(Close());
VerifyAllOpsFail();
}
TEST_P(ServiceWorkerCacheTestP, OpsFailOnClosedBackend) {
// Create the backend and put something in it.
EXPECT_TRUE(Put(body_request_, body_response_));
EXPECT_TRUE(Close());
VerifyAllOpsFail();
}
TEST_P(ServiceWorkerCacheTestP, ClosedDuringPutInitBackend) {
// Even though Close is called in the middle of a Put operation (during
// backend creation), the put operation should exit early.
cache_->set_delay_backend_creation(true);
scoped_ptr<base::RunLoop> close_loop(new base::RunLoop());
cache_->Put(CopyFetchRequest(body_request_),
CopyFetchResponse(body_response_),
base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
base::Unretained(this), nullptr));
cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback,
base::Unretained(this), close_loop.get()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
EXPECT_FALSE(callback_closed_);
cache_->ContinueCreateBackend();
close_loop->Run();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeStorage, callback_error_);
EXPECT_TRUE(callback_closed_);
VerifyAllOpsFail();
}
TEST_P(ServiceWorkerCacheTestP, ClosedDuringMatchInitBackend) {
// Even though Close is called in the middle of a Match operation (during
// backend creation), the match operation should exit early.
cache_->set_delay_backend_creation(true);
scoped_ptr<base::RunLoop> close_loop(new base::RunLoop());
cache_->Match(CopyFetchRequest(body_request_),
base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
base::Unretained(this), nullptr));
cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback,
base::Unretained(this), close_loop.get()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
EXPECT_FALSE(callback_closed_);
cache_->ContinueCreateBackend();
close_loop->Run();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeStorage, callback_error_);
EXPECT_TRUE(callback_closed_);
VerifyAllOpsFail();
}
TEST_P(ServiceWorkerCacheTestP, ClosedDuringDeleteInitBackend) {
// Even though Close is called in the middle of a Delete operation (during
// backend creation), the delete operation should exit early.
cache_->set_delay_backend_creation(true);
scoped_ptr<base::RunLoop> close_loop(new base::RunLoop());
cache_->Delete(CopyFetchRequest(body_request_),
base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
base::Unretained(this), nullptr));
cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback,
base::Unretained(this), close_loop.get()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
EXPECT_FALSE(callback_closed_);
cache_->ContinueCreateBackend();
close_loop->Run();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeStorage, callback_error_);
EXPECT_TRUE(callback_closed_);
VerifyAllOpsFail();
}
TEST_P(ServiceWorkerCacheTestP, ClosedDuringKeysInitBackend) {
// Even though Close is called in the middle of a Keys operation (during
// backend creation), the keys operation should exit early.
cache_->set_delay_backend_creation(true);
scoped_ptr<base::RunLoop> close_loop(new base::RunLoop());
cache_->Keys(base::Bind(&ServiceWorkerCacheTest::RequestsCallback,
base::Unretained(this), nullptr));
cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback,
base::Unretained(this), close_loop.get()));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
EXPECT_FALSE(callback_closed_);
cache_->ContinueCreateBackend();
close_loop->Run();
EXPECT_EQ(ServiceWorkerCache::ErrorTypeStorage, callback_error_);
EXPECT_TRUE(callback_closed_);
VerifyAllOpsFail();
}
TEST_P(ServiceWorkerCacheTestP, ClosedDuringPutOpenEntry) {
EXPECT_TRUE(Keys()); // Opens the backend.
DelayableBackend* delayable_backend = cache_->UseDelayableBackend();
delayable_backend->set_delay_open(true);
// Run Put and Close. Put will delay on OpenEntry, Close will wait for Put to
// finish.
scoped_ptr<base::RunLoop> close_loop(new base::RunLoop());
cache_->Put(CopyFetchRequest(body_request_),
CopyFetchResponse(body_response_),
base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
base::Unretained(this), nullptr));
cache_->Close(base::Bind(&ServiceWorkerCacheTest::CloseCallback,
base::Unretained(this), close_loop.get()));
base::RunLoop().RunUntilIdle();
// Verify that neither operation has finished.
EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
EXPECT_FALSE(callback_closed_);
delayable_backend->OpenEntryContinue();
close_loop->Run();
// Put failed because the backend was closed while it was running.
EXPECT_EQ(ServiceWorkerCache::ErrorTypeStorage, callback_error_);
EXPECT_TRUE(callback_closed_);
VerifyAllOpsFail();
}
INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest,
ServiceWorkerCacheTestP,
::testing::Values(false, true));
} // namespace content
|