| 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 "content/browser/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 } | 197 } |
| 198 | 198 |
| 199 bool ResponseMetadataEqual(const ServiceWorkerResponse& expected, | 199 bool ResponseMetadataEqual(const ServiceWorkerResponse& expected, |
| 200 const ServiceWorkerResponse& actual) { | 200 const ServiceWorkerResponse& actual) { |
| 201 EXPECT_EQ(expected.status_code, actual.status_code); | 201 EXPECT_EQ(expected.status_code, actual.status_code); |
| 202 if (expected.status_code != actual.status_code) | 202 if (expected.status_code != actual.status_code) |
| 203 return false; | 203 return false; |
| 204 EXPECT_EQ(expected.status_text, actual.status_text); | 204 EXPECT_EQ(expected.status_text, actual.status_text); |
| 205 if (expected.status_text != actual.status_text) | 205 if (expected.status_text != actual.status_text) |
| 206 return false; | 206 return false; |
| 207 EXPECT_EQ(expected.url, actual.url); | 207 EXPECT_EQ(expected.url_list.size(), actual.url_list.size()); |
| 208 if (expected.url != actual.url) | 208 if (expected.url_list.size() != actual.url_list.size()) |
| 209 return false; | 209 return false; |
| 210 for (size_t i = 0; i < expected.url_list.size(); ++i) { |
| 211 EXPECT_EQ(expected.url_list[i], actual.url_list[i]); |
| 212 if (expected.url_list[i] != actual.url_list[i]) |
| 213 return false; |
| 214 } |
| 210 EXPECT_EQ(expected.blob_size, actual.blob_size); | 215 EXPECT_EQ(expected.blob_size, actual.blob_size); |
| 211 if (expected.blob_size != actual.blob_size) | 216 if (expected.blob_size != actual.blob_size) |
| 212 return false; | 217 return false; |
| 213 | 218 |
| 214 if (expected.blob_size == 0) { | 219 if (expected.blob_size == 0) { |
| 215 EXPECT_STREQ("", actual.blob_uuid.c_str()); | 220 EXPECT_STREQ("", actual.blob_uuid.c_str()); |
| 216 if (!actual.blob_uuid.empty()) | 221 if (!actual.blob_uuid.empty()) |
| 217 return false; | 222 return false; |
| 218 } else { | 223 } else { |
| 219 EXPECT_STRNE("", actual.blob_uuid.c_str()); | 224 EXPECT_STRNE("", actual.blob_uuid.c_str()); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 for (int i = 0; i < 100; ++i) | 400 for (int i = 0; i < 100; ++i) |
| 396 expected_blob_data_ += kTestData; | 401 expected_blob_data_ += kTestData; |
| 397 | 402 |
| 398 std::unique_ptr<storage::BlobDataBuilder> blob_data( | 403 std::unique_ptr<storage::BlobDataBuilder> blob_data( |
| 399 new storage::BlobDataBuilder("blob-id:myblob")); | 404 new storage::BlobDataBuilder("blob-id:myblob")); |
| 400 blob_data->AppendData(expected_blob_data_); | 405 blob_data->AppendData(expected_blob_data_); |
| 401 | 406 |
| 402 blob_handle_ = | 407 blob_handle_ = |
| 403 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); | 408 blob_storage_context->context()->AddFinishedBlob(blob_data.get()); |
| 404 | 409 |
| 405 body_response_ = ServiceWorkerResponse( | 410 body_response_ = CreateResponse( |
| 406 GURL("http://example.com/body.html"), 200, "OK", | 411 "http://example.com/body.html", |
| 407 blink::WebServiceWorkerResponseTypeDefault, headers, | 412 base::MakeUnique<ServiceWorkerHeaderMap>(headers), blob_handle_->uuid(), |
| 408 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), | 413 expected_blob_data_.size(), |
| 414 base::MakeUnique< |
| 415 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
| 416 |
| 417 body_response_with_query_ = |
| 418 CreateResponse("http://example.com/body.html?query=test", |
| 419 base::MakeUnique<ServiceWorkerHeaderMap>(headers), |
| 420 blob_handle_->uuid(), expected_blob_data_.size(), |
| 421 base::MakeUnique<ServiceWorkerHeaderList>( |
| 422 1, "a") /* cors_exposed_header_names */); |
| 423 |
| 424 no_body_response_ = CreateResponse( |
| 425 "http://example.com/no_body.html", |
| 426 base::MakeUnique<ServiceWorkerHeaderMap>(headers), "", 0, |
| 427 base::MakeUnique< |
| 428 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
| 429 } |
| 430 |
| 431 static ServiceWorkerResponse CreateResponse( |
| 432 const std::string& url, |
| 433 std::unique_ptr<ServiceWorkerHeaderMap> headers, |
| 434 const std::string& blob_uuid, |
| 435 uint64_t blob_size, |
| 436 std::unique_ptr<ServiceWorkerHeaderList> cors_exposed_header_names) { |
| 437 return ServiceWorkerResponse( |
| 438 base::MakeUnique<std::vector<GURL>>(1, GURL(url)), 200, "OK", |
| 439 blink::WebServiceWorkerResponseTypeDefault, std::move(headers), |
| 440 blob_uuid, blob_size, GURL() /* stream_url */, |
| 409 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), | 441 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), |
| 410 false /* is_in_cache_storage */, | 442 false /* is_in_cache_storage */, |
| 411 std::string() /* cache_storage_cache_name */, | 443 std::string() /* cache_storage_cache_name */, |
| 412 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | 444 std::move(cors_exposed_header_names)); |
| 413 | |
| 414 body_response_with_query_ = ServiceWorkerResponse( | |
| 415 GURL("http://example.com/body.html?query=test"), 200, "OK", | |
| 416 blink::WebServiceWorkerResponseTypeDefault, headers, | |
| 417 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), | |
| 418 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), | |
| 419 false /* is_in_cache_storage */, | |
| 420 std::string() /* cache_storage_cache_name */, | |
| 421 {"a"} /* cors_exposed_header_names */); | |
| 422 | |
| 423 no_body_response_ = ServiceWorkerResponse( | |
| 424 GURL("http://example.com/no_body.html"), 200, "OK", | |
| 425 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL(), | |
| 426 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), | |
| 427 false /* is_in_cache_storage */, | |
| 428 std::string() /* cache_storage_cache_name */, | |
| 429 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | |
| 430 } | 445 } |
| 431 | 446 |
| 432 std::unique_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( | 447 std::unique_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( |
| 433 const ServiceWorkerFetchRequest& request) { | 448 const ServiceWorkerFetchRequest& request) { |
| 434 return base::MakeUnique<ServiceWorkerFetchRequest>( | 449 return base::MakeUnique<ServiceWorkerFetchRequest>( |
| 435 request.url, request.method, request.headers, request.referrer, | 450 request.url, request.method, request.headers, request.referrer, |
| 436 request.is_reload); | 451 request.is_reload); |
| 437 } | 452 } |
| 438 | 453 |
| 439 CacheStorageError BatchOperation( | 454 CacheStorageError BatchOperation( |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 TEST_P(CacheStorageCacheTestP, PutBody) { | 728 TEST_P(CacheStorageCacheTestP, PutBody) { |
| 714 EXPECT_TRUE(Put(body_request_, body_response_)); | 729 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 715 } | 730 } |
| 716 | 731 |
| 717 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) { | 732 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) { |
| 718 CacheStorageBatchOperation operation1; | 733 CacheStorageBatchOperation operation1; |
| 719 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 734 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 720 operation1.request = body_request_; | 735 operation1.request = body_request_; |
| 721 operation1.request.url = GURL("http://example.com/1"); | 736 operation1.request.url = GURL("http://example.com/1"); |
| 722 operation1.response = body_response_; | 737 operation1.response = body_response_; |
| 723 operation1.response.url = GURL("http://example.com/1"); | 738 operation1.response.url_list.push_back(GURL("http://example.com/1")); |
| 724 | 739 |
| 725 CacheStorageBatchOperation operation2; | 740 CacheStorageBatchOperation operation2; |
| 726 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 741 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 727 operation2.request = body_request_; | 742 operation2.request = body_request_; |
| 728 operation2.request.url = GURL("http://example.com/2"); | 743 operation2.request.url = GURL("http://example.com/2"); |
| 729 operation2.response = body_response_; | 744 operation2.response = body_response_; |
| 730 operation2.response.url = GURL("http://example.com/2"); | 745 operation2.response.url_list.push_back(GURL("http://example.com/2")); |
| 731 | 746 |
| 732 CacheStorageBatchOperation operation3; | 747 CacheStorageBatchOperation operation3; |
| 733 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 748 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 734 operation3.request = body_request_; | 749 operation3.request = body_request_; |
| 735 operation3.request.url = GURL("http://example.com/3"); | 750 operation3.request.url = GURL("http://example.com/3"); |
| 736 operation3.response = body_response_; | 751 operation3.response = body_response_; |
| 737 operation3.response.url = GURL("http://example.com/3"); | 752 operation3.response.url_list.push_back(GURL("http://example.com/3")); |
| 738 | 753 |
| 739 std::vector<CacheStorageBatchOperation> operations; | 754 std::vector<CacheStorageBatchOperation> operations; |
| 740 operations.push_back(operation1); | 755 operations.push_back(operation1); |
| 741 operations.push_back(operation2); | 756 operations.push_back(operation2); |
| 742 operations.push_back(operation3); | 757 operations.push_back(operation3); |
| 743 | 758 |
| 744 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); | 759 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); |
| 745 EXPECT_TRUE(Match(operation1.request)); | 760 EXPECT_TRUE(Match(operation1.request)); |
| 746 EXPECT_TRUE(Match(operation2.request)); | 761 EXPECT_TRUE(Match(operation2.request)); |
| 747 EXPECT_TRUE(Match(operation3.request)); | 762 EXPECT_TRUE(Match(operation3.request)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 EXPECT_FALSE(Keys()); | 824 EXPECT_FALSE(Keys()); |
| 810 EXPECT_EQ(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, callback_error_); | 825 EXPECT_EQ(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, callback_error_); |
| 811 } | 826 } |
| 812 | 827 |
| 813 // TODO(nhiroki): Add a test for the case where one of PUT operations fails. | 828 // TODO(nhiroki): Add a test for the case where one of PUT operations fails. |
| 814 // Currently there is no handy way to fail only one operation in a batch. | 829 // Currently there is no handy way to fail only one operation in a batch. |
| 815 // This could be easily achieved after adding some security checks in the | 830 // This could be easily achieved after adding some security checks in the |
| 816 // browser side (http://crbug.com/425505). | 831 // browser side (http://crbug.com/425505). |
| 817 | 832 |
| 818 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { | 833 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { |
| 819 no_body_response_.url = GURL("http://example.com/foobar"); | 834 no_body_response_.url_list.clear(); |
| 835 no_body_response_.url_list.push_back(GURL("http://example.com/foobar")); |
| 820 EXPECT_STRNE("http://example.com/foobar", | 836 EXPECT_STRNE("http://example.com/foobar", |
| 821 no_body_request_.url.spec().c_str()); | 837 no_body_request_.url.spec().c_str()); |
| 822 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 838 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 823 EXPECT_TRUE(Match(no_body_request_)); | 839 EXPECT_TRUE(Match(no_body_request_)); |
| 840 ASSERT_EQ(1u, callback_response_->url_list.size()); |
| 824 EXPECT_STREQ("http://example.com/foobar", | 841 EXPECT_STREQ("http://example.com/foobar", |
| 825 callback_response_->url.spec().c_str()); | 842 callback_response_->url_list[0].spec().c_str()); |
| 826 } | 843 } |
| 827 | 844 |
| 828 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { | 845 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { |
| 829 no_body_response_.url = GURL(); | 846 no_body_response_.url_list.clear(); |
| 830 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); | 847 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); |
| 831 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 848 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 832 EXPECT_TRUE(Match(no_body_request_)); | 849 EXPECT_TRUE(Match(no_body_request_)); |
| 833 EXPECT_STREQ("", callback_response_->url.spec().c_str()); | 850 EXPECT_EQ(0u, callback_response_->url_list.size()); |
| 834 } | 851 } |
| 835 | 852 |
| 836 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { | 853 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { |
| 837 CacheStorageBatchOperation operation; | 854 CacheStorageBatchOperation operation; |
| 838 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 855 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 839 operation.request = body_request_; | 856 operation.request = body_request_; |
| 840 operation.response = body_response_; | 857 operation.response = body_response_; |
| 841 | 858 |
| 842 std::unique_ptr<base::RunLoop> loop(new base::RunLoop()); | 859 std::unique_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 843 cache_->BatchOperation( | 860 cache_->BatchOperation( |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1133 CacheStorageCacheQueryParams match_params; | 1150 CacheStorageCacheQueryParams match_params; |
| 1134 match_params.ignore_search = true; | 1151 match_params.ignore_search = true; |
| 1135 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); | 1152 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); |
| 1136 | 1153 |
| 1137 ASSERT_EQ(2u, responses->size()); | 1154 ASSERT_EQ(2u, responses->size()); |
| 1138 ASSERT_EQ(2u, body_handles->size()); | 1155 ASSERT_EQ(2u, body_handles->size()); |
| 1139 | 1156 |
| 1140 // Order of returned responses is not guaranteed. | 1157 // Order of returned responses is not guaranteed. |
| 1141 std::set<std::string> matched_set; | 1158 std::set<std::string> matched_set; |
| 1142 for (const ServiceWorkerResponse& response : *responses) { | 1159 for (const ServiceWorkerResponse& response : *responses) { |
| 1143 if (response.url.spec() == "http://example.com/body.html?query=test") { | 1160 ASSERT_EQ(1u, response.url_list.size()); |
| 1161 if (response.url_list[0].spec() == |
| 1162 "http://example.com/body.html?query=test") { |
| 1144 EXPECT_TRUE(ResponseMetadataEqual(SetCacheName(body_response_with_query_), | 1163 EXPECT_TRUE(ResponseMetadataEqual(SetCacheName(body_response_with_query_), |
| 1145 response)); | 1164 response)); |
| 1146 matched_set.insert(response.url.spec()); | 1165 matched_set.insert(response.url_list[0].spec()); |
| 1147 } else if (response.url.spec() == "http://example.com/body.html") { | 1166 } else if (response.url_list[0].spec() == "http://example.com/body.html") { |
| 1148 EXPECT_TRUE( | 1167 EXPECT_TRUE( |
| 1149 ResponseMetadataEqual(SetCacheName(body_response_), response)); | 1168 ResponseMetadataEqual(SetCacheName(body_response_), response)); |
| 1150 matched_set.insert(response.url.spec()); | 1169 matched_set.insert(response.url_list[0].spec()); |
| 1151 } | 1170 } |
| 1152 } | 1171 } |
| 1153 EXPECT_EQ(2u, matched_set.size()); | 1172 EXPECT_EQ(2u, matched_set.size()); |
| 1154 } | 1173 } |
| 1155 | 1174 |
| 1156 TEST_P(CacheStorageCacheTestP, MatchAll_Head) { | 1175 TEST_P(CacheStorageCacheTestP, MatchAll_Head) { |
| 1157 EXPECT_TRUE(Put(body_request_, body_response_)); | 1176 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 1158 | 1177 |
| 1159 std::unique_ptr<CacheStorageCache::Responses> responses; | 1178 std::unique_ptr<CacheStorageCache::Responses> responses; |
| 1160 std::unique_ptr<CacheStorageCache::BlobDataHandles> body_handles; | 1179 std::unique_ptr<CacheStorageCache::BlobDataHandles> body_handles; |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1484 memset(buffer->data(), 0, kSize); | 1503 memset(buffer->data(), 0, kSize); |
| 1485 EXPECT_FALSE(WriteSideData(GURL("http://www.example.com/not_exist"), | 1504 EXPECT_FALSE(WriteSideData(GURL("http://www.example.com/not_exist"), |
| 1486 base::Time::Now(), buffer, kSize)); | 1505 base::Time::Now(), buffer, kSize)); |
| 1487 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); | 1506 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
| 1488 } | 1507 } |
| 1489 | 1508 |
| 1490 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) { | 1509 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) { |
| 1491 // CacheStorageCache depends on ServiceWorkerResponse having caseless | 1510 // CacheStorageCache depends on ServiceWorkerResponse having caseless |
| 1492 // headers so that it can quickly lookup vary headers. | 1511 // headers so that it can quickly lookup vary headers. |
| 1493 ServiceWorkerResponse response( | 1512 ServiceWorkerResponse response( |
| 1494 GURL("http://www.example.com"), 200, "OK", | 1513 base::MakeUnique<std::vector<GURL>>(), 200, "OK", |
| 1495 blink::WebServiceWorkerResponseTypeDefault, ServiceWorkerHeaderMap(), "", | 1514 blink::WebServiceWorkerResponseTypeDefault, |
| 1496 0, GURL(), blink::WebServiceWorkerResponseErrorUnknown, base::Time(), | 1515 base::MakeUnique<ServiceWorkerHeaderMap>(), "", 0, GURL(), |
| 1516 blink::WebServiceWorkerResponseErrorUnknown, base::Time(), |
| 1497 false /* is_in_cache_storage */, | 1517 false /* is_in_cache_storage */, |
| 1498 std::string() /* cache_storage_cache_name */, | 1518 std::string() /* cache_storage_cache_name */, |
| 1499 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | 1519 base::MakeUnique< |
| 1520 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
| 1500 response.headers["content-type"] = "foo"; | 1521 response.headers["content-type"] = "foo"; |
| 1501 response.headers["Content-Type"] = "bar"; | 1522 response.headers["Content-Type"] = "bar"; |
| 1502 EXPECT_EQ("bar", response.headers["content-type"]); | 1523 EXPECT_EQ("bar", response.headers["content-type"]); |
| 1503 } | 1524 } |
| 1504 | 1525 |
| 1505 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) { | 1526 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) { |
| 1506 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless | 1527 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless |
| 1507 // headers so that it can quickly lookup vary headers. | 1528 // headers so that it can quickly lookup vary headers. |
| 1508 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET", | 1529 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET", |
| 1509 ServiceWorkerHeaderMap(), Referrer(), | 1530 ServiceWorkerHeaderMap(), Referrer(), |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1624 EXPECT_EQ(1, sequence_out); | 1645 EXPECT_EQ(1, sequence_out); |
| 1625 close_loop2->Run(); | 1646 close_loop2->Run(); |
| 1626 EXPECT_EQ(2, sequence_out); | 1647 EXPECT_EQ(2, sequence_out); |
| 1627 } | 1648 } |
| 1628 | 1649 |
| 1629 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1650 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
| 1630 CacheStorageCacheTestP, | 1651 CacheStorageCacheTestP, |
| 1631 ::testing::Values(false, true)); | 1652 ::testing::Values(false, true)); |
| 1632 | 1653 |
| 1633 } // namespace content | 1654 } // namespace content |
| OLD | NEW |