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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 std::string expected_response; | 399 std::string expected_response; |
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()); |
| 409 std::unique_ptr<std::vector<GURL>> body_response_url_list( |
| 410 base::MakeUnique<std::vector<GURL>>()); |
| 411 body_response_url_list->push_back(GURL("http://example.com/body.html")); |
404 | 412 |
405 body_response_ = ServiceWorkerResponse( | 413 body_response_ = ServiceWorkerResponse( |
406 GURL("http://example.com/body.html"), 200, "OK", | 414 std::move(body_response_url_list), 200, "OK", |
407 blink::WebServiceWorkerResponseTypeDefault, headers, | 415 blink::WebServiceWorkerResponseTypeDefault, |
408 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), | 416 base::MakeUnique<ServiceWorkerHeaderMap>(headers), blob_handle_->uuid(), |
| 417 expected_blob_data_.size(), GURL(), |
409 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), | 418 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), |
410 false /* is_in_cache_storage */, | 419 false /* is_in_cache_storage */, |
411 std::string() /* cache_storage_cache_name */, | 420 std::string() /* cache_storage_cache_name */, |
412 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | 421 base::MakeUnique< |
| 422 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
413 | 423 |
| 424 std::unique_ptr<std::vector<GURL>> body_response_with_query_url_list( |
| 425 base::MakeUnique<std::vector<GURL>>()); |
| 426 body_response_with_query_url_list->push_back( |
| 427 GURL("http://example.com/body.html?query=test")); |
| 428 std::unique_ptr<ServiceWorkerHeaderList> cors_exposed_header_names( |
| 429 base::MakeUnique<ServiceWorkerHeaderList>()); |
| 430 cors_exposed_header_names->push_back("a"); |
414 body_response_with_query_ = ServiceWorkerResponse( | 431 body_response_with_query_ = ServiceWorkerResponse( |
415 GURL("http://example.com/body.html?query=test"), 200, "OK", | 432 std::move(body_response_with_query_url_list), 200, "OK", |
416 blink::WebServiceWorkerResponseTypeDefault, headers, | 433 blink::WebServiceWorkerResponseTypeDefault, |
417 blob_handle_->uuid(), expected_blob_data_.size(), GURL(), | 434 base::MakeUnique<ServiceWorkerHeaderMap>(headers), blob_handle_->uuid(), |
| 435 expected_blob_data_.size(), GURL(), |
418 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), | 436 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), |
419 false /* is_in_cache_storage */, | 437 false /* is_in_cache_storage */, |
420 std::string() /* cache_storage_cache_name */, | 438 std::string() /* cache_storage_cache_name */, |
421 {"a"} /* cors_exposed_header_names */); | 439 std::move(cors_exposed_header_names)); |
422 | 440 |
| 441 std::unique_ptr<std::vector<GURL>> no_body_response_url_list( |
| 442 base::MakeUnique<std::vector<GURL>>()); |
| 443 no_body_response_url_list->push_back( |
| 444 GURL("http://example.com/no_body.html")); |
423 no_body_response_ = ServiceWorkerResponse( | 445 no_body_response_ = ServiceWorkerResponse( |
424 GURL("http://example.com/no_body.html"), 200, "OK", | 446 std::move(no_body_response_url_list), 200, "OK", |
425 blink::WebServiceWorkerResponseTypeDefault, headers, "", 0, GURL(), | 447 blink::WebServiceWorkerResponseTypeDefault, |
| 448 base::MakeUnique<ServiceWorkerHeaderMap>(headers), "", 0, GURL(), |
426 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), | 449 blink::WebServiceWorkerResponseErrorUnknown, base::Time::Now(), |
427 false /* is_in_cache_storage */, | 450 false /* is_in_cache_storage */, |
428 std::string() /* cache_storage_cache_name */, | 451 std::string() /* cache_storage_cache_name */, |
429 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | 452 base::MakeUnique< |
| 453 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
430 } | 454 } |
431 | 455 |
432 std::unique_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( | 456 std::unique_ptr<ServiceWorkerFetchRequest> CopyFetchRequest( |
433 const ServiceWorkerFetchRequest& request) { | 457 const ServiceWorkerFetchRequest& request) { |
434 return base::MakeUnique<ServiceWorkerFetchRequest>( | 458 return base::MakeUnique<ServiceWorkerFetchRequest>( |
435 request.url, request.method, request.headers, request.referrer, | 459 request.url, request.method, request.headers, request.referrer, |
436 request.is_reload); | 460 request.is_reload); |
437 } | 461 } |
438 | 462 |
439 CacheStorageError BatchOperation( | 463 CacheStorageError BatchOperation( |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 TEST_P(CacheStorageCacheTestP, PutBody) { | 737 TEST_P(CacheStorageCacheTestP, PutBody) { |
714 EXPECT_TRUE(Put(body_request_, body_response_)); | 738 EXPECT_TRUE(Put(body_request_, body_response_)); |
715 } | 739 } |
716 | 740 |
717 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) { | 741 TEST_P(CacheStorageCacheTestP, PutBody_Multiple) { |
718 CacheStorageBatchOperation operation1; | 742 CacheStorageBatchOperation operation1; |
719 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 743 operation1.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
720 operation1.request = body_request_; | 744 operation1.request = body_request_; |
721 operation1.request.url = GURL("http://example.com/1"); | 745 operation1.request.url = GURL("http://example.com/1"); |
722 operation1.response = body_response_; | 746 operation1.response = body_response_; |
723 operation1.response.url = GURL("http://example.com/1"); | 747 operation1.response.url_list.push_back(GURL("http://example.com/1")); |
724 | 748 |
725 CacheStorageBatchOperation operation2; | 749 CacheStorageBatchOperation operation2; |
726 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 750 operation2.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
727 operation2.request = body_request_; | 751 operation2.request = body_request_; |
728 operation2.request.url = GURL("http://example.com/2"); | 752 operation2.request.url = GURL("http://example.com/2"); |
729 operation2.response = body_response_; | 753 operation2.response = body_response_; |
730 operation2.response.url = GURL("http://example.com/2"); | 754 operation2.response.url_list.push_back(GURL("http://example.com/2")); |
731 | 755 |
732 CacheStorageBatchOperation operation3; | 756 CacheStorageBatchOperation operation3; |
733 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 757 operation3.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
734 operation3.request = body_request_; | 758 operation3.request = body_request_; |
735 operation3.request.url = GURL("http://example.com/3"); | 759 operation3.request.url = GURL("http://example.com/3"); |
736 operation3.response = body_response_; | 760 operation3.response = body_response_; |
737 operation3.response.url = GURL("http://example.com/3"); | 761 operation3.response.url_list.push_back(GURL("http://example.com/3")); |
738 | 762 |
739 std::vector<CacheStorageBatchOperation> operations; | 763 std::vector<CacheStorageBatchOperation> operations; |
740 operations.push_back(operation1); | 764 operations.push_back(operation1); |
741 operations.push_back(operation2); | 765 operations.push_back(operation2); |
742 operations.push_back(operation3); | 766 operations.push_back(operation3); |
743 | 767 |
744 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); | 768 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); |
745 EXPECT_TRUE(Match(operation1.request)); | 769 EXPECT_TRUE(Match(operation1.request)); |
746 EXPECT_TRUE(Match(operation2.request)); | 770 EXPECT_TRUE(Match(operation2.request)); |
747 EXPECT_TRUE(Match(operation3.request)); | 771 EXPECT_TRUE(Match(operation3.request)); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
809 EXPECT_FALSE(Keys()); | 833 EXPECT_FALSE(Keys()); |
810 EXPECT_EQ(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, callback_error_); | 834 EXPECT_EQ(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, callback_error_); |
811 } | 835 } |
812 | 836 |
813 // TODO(nhiroki): Add a test for the case where one of PUT operations fails. | 837 // 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. | 838 // 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 | 839 // This could be easily achieved after adding some security checks in the |
816 // browser side (http://crbug.com/425505). | 840 // browser side (http://crbug.com/425505). |
817 | 841 |
818 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { | 842 TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) { |
819 no_body_response_.url = GURL("http://example.com/foobar"); | 843 no_body_response_.url_list.clear(); |
| 844 no_body_response_.url_list.push_back(GURL("http://example.com/foobar")); |
820 EXPECT_STRNE("http://example.com/foobar", | 845 EXPECT_STRNE("http://example.com/foobar", |
821 no_body_request_.url.spec().c_str()); | 846 no_body_request_.url.spec().c_str()); |
822 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 847 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
823 EXPECT_TRUE(Match(no_body_request_)); | 848 EXPECT_TRUE(Match(no_body_request_)); |
| 849 ASSERT_EQ(1u, callback_response_->url_list.size()); |
824 EXPECT_STREQ("http://example.com/foobar", | 850 EXPECT_STREQ("http://example.com/foobar", |
825 callback_response_->url.spec().c_str()); | 851 callback_response_->url_list[0].spec().c_str()); |
826 } | 852 } |
827 | 853 |
828 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { | 854 TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) { |
829 no_body_response_.url = GURL(); | 855 no_body_response_.url_list.clear(); |
830 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); | 856 EXPECT_STRNE("", no_body_request_.url.spec().c_str()); |
831 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 857 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
832 EXPECT_TRUE(Match(no_body_request_)); | 858 EXPECT_TRUE(Match(no_body_request_)); |
833 EXPECT_STREQ("", callback_response_->url.spec().c_str()); | 859 EXPECT_EQ(0u, callback_response_->url_list.size()); |
834 } | 860 } |
835 | 861 |
836 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { | 862 TEST_F(CacheStorageCacheTest, PutBodyDropBlobRef) { |
837 CacheStorageBatchOperation operation; | 863 CacheStorageBatchOperation operation; |
838 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 864 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
839 operation.request = body_request_; | 865 operation.request = body_request_; |
840 operation.response = body_response_; | 866 operation.response = body_response_; |
841 | 867 |
842 std::unique_ptr<base::RunLoop> loop(new base::RunLoop()); | 868 std::unique_ptr<base::RunLoop> loop(new base::RunLoop()); |
843 cache_->BatchOperation( | 869 cache_->BatchOperation( |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1133 CacheStorageCacheQueryParams match_params; | 1159 CacheStorageCacheQueryParams match_params; |
1134 match_params.ignore_search = true; | 1160 match_params.ignore_search = true; |
1135 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); | 1161 EXPECT_TRUE(MatchAll(body_request_, match_params, &responses, &body_handles)); |
1136 | 1162 |
1137 ASSERT_EQ(2u, responses->size()); | 1163 ASSERT_EQ(2u, responses->size()); |
1138 ASSERT_EQ(2u, body_handles->size()); | 1164 ASSERT_EQ(2u, body_handles->size()); |
1139 | 1165 |
1140 // Order of returned responses is not guaranteed. | 1166 // Order of returned responses is not guaranteed. |
1141 std::set<std::string> matched_set; | 1167 std::set<std::string> matched_set; |
1142 for (const ServiceWorkerResponse& response : *responses) { | 1168 for (const ServiceWorkerResponse& response : *responses) { |
1143 if (response.url.spec() == "http://example.com/body.html?query=test") { | 1169 ASSERT_EQ(1u, response.url_list.size()); |
| 1170 if (response.url_list[0].spec() == |
| 1171 "http://example.com/body.html?query=test") { |
1144 EXPECT_TRUE(ResponseMetadataEqual(SetCacheName(body_response_with_query_), | 1172 EXPECT_TRUE(ResponseMetadataEqual(SetCacheName(body_response_with_query_), |
1145 response)); | 1173 response)); |
1146 matched_set.insert(response.url.spec()); | 1174 matched_set.insert(response.url_list[0].spec()); |
1147 } else if (response.url.spec() == "http://example.com/body.html") { | 1175 } else if (response.url_list[0].spec() == "http://example.com/body.html") { |
1148 EXPECT_TRUE( | 1176 EXPECT_TRUE( |
1149 ResponseMetadataEqual(SetCacheName(body_response_), response)); | 1177 ResponseMetadataEqual(SetCacheName(body_response_), response)); |
1150 matched_set.insert(response.url.spec()); | 1178 matched_set.insert(response.url_list[0].spec()); |
1151 } | 1179 } |
1152 } | 1180 } |
1153 EXPECT_EQ(2u, matched_set.size()); | 1181 EXPECT_EQ(2u, matched_set.size()); |
1154 } | 1182 } |
1155 | 1183 |
1156 TEST_P(CacheStorageCacheTestP, MatchAll_Head) { | 1184 TEST_P(CacheStorageCacheTestP, MatchAll_Head) { |
1157 EXPECT_TRUE(Put(body_request_, body_response_)); | 1185 EXPECT_TRUE(Put(body_request_, body_response_)); |
1158 | 1186 |
1159 std::unique_ptr<CacheStorageCache::Responses> responses; | 1187 std::unique_ptr<CacheStorageCache::Responses> responses; |
1160 std::unique_ptr<CacheStorageCache::BlobDataHandles> body_handles; | 1188 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); | 1512 memset(buffer->data(), 0, kSize); |
1485 EXPECT_FALSE(WriteSideData(GURL("http://www.example.com/not_exist"), | 1513 EXPECT_FALSE(WriteSideData(GURL("http://www.example.com/not_exist"), |
1486 base::Time::Now(), buffer, kSize)); | 1514 base::Time::Now(), buffer, kSize)); |
1487 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); | 1515 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
1488 } | 1516 } |
1489 | 1517 |
1490 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) { | 1518 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerResponseHeaders) { |
1491 // CacheStorageCache depends on ServiceWorkerResponse having caseless | 1519 // CacheStorageCache depends on ServiceWorkerResponse having caseless |
1492 // headers so that it can quickly lookup vary headers. | 1520 // headers so that it can quickly lookup vary headers. |
1493 ServiceWorkerResponse response( | 1521 ServiceWorkerResponse response( |
1494 GURL("http://www.example.com"), 200, "OK", | 1522 base::MakeUnique<std::vector<GURL>>(), 200, "OK", |
1495 blink::WebServiceWorkerResponseTypeDefault, ServiceWorkerHeaderMap(), "", | 1523 blink::WebServiceWorkerResponseTypeDefault, |
1496 0, GURL(), blink::WebServiceWorkerResponseErrorUnknown, base::Time(), | 1524 base::MakeUnique<ServiceWorkerHeaderMap>(), "", 0, GURL(), |
| 1525 blink::WebServiceWorkerResponseErrorUnknown, base::Time(), |
1497 false /* is_in_cache_storage */, | 1526 false /* is_in_cache_storage */, |
1498 std::string() /* cache_storage_cache_name */, | 1527 std::string() /* cache_storage_cache_name */, |
1499 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | 1528 base::MakeUnique< |
| 1529 ServiceWorkerHeaderList>() /* cors_exposed_header_names */); |
1500 response.headers["content-type"] = "foo"; | 1530 response.headers["content-type"] = "foo"; |
1501 response.headers["Content-Type"] = "bar"; | 1531 response.headers["Content-Type"] = "bar"; |
1502 EXPECT_EQ("bar", response.headers["content-type"]); | 1532 EXPECT_EQ("bar", response.headers["content-type"]); |
1503 } | 1533 } |
1504 | 1534 |
1505 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) { | 1535 TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) { |
1506 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless | 1536 // CacheStorageCache depends on ServiceWorkerFetchRequest having caseless |
1507 // headers so that it can quickly lookup vary headers. | 1537 // headers so that it can quickly lookup vary headers. |
1508 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET", | 1538 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), "GET", |
1509 ServiceWorkerHeaderMap(), Referrer(), | 1539 ServiceWorkerHeaderMap(), Referrer(), |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1624 EXPECT_EQ(1, sequence_out); | 1654 EXPECT_EQ(1, sequence_out); |
1625 close_loop2->Run(); | 1655 close_loop2->Run(); |
1626 EXPECT_EQ(2, sequence_out); | 1656 EXPECT_EQ(2, sequence_out); |
1627 } | 1657 } |
1628 | 1658 |
1629 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1659 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
1630 CacheStorageCacheTestP, | 1660 CacheStorageCacheTestP, |
1631 ::testing::Values(false, true)); | 1661 ::testing::Values(false, true)); |
1632 | 1662 |
1633 } // namespace content | 1663 } // namespace content |
OLD | NEW |