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 "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 int rv = backend_->OpenEntry(key, entry, callback); | 115 int rv = backend_->OpenEntry(key, entry, callback); |
116 if (rv != net::ERR_IO_PENDING) | 116 if (rv != net::ERR_IO_PENDING) |
117 callback.Run(rv); | 117 callback.Run(rv); |
118 } | 118 } |
119 | 119 |
120 scoped_ptr<disk_cache::Backend> backend_; | 120 scoped_ptr<disk_cache::Backend> backend_; |
121 bool delay_open_; | 121 bool delay_open_; |
122 base::Closure open_entry_callback_; | 122 base::Closure open_entry_callback_; |
123 }; | 123 }; |
124 | 124 |
125 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | |
126 *output = std::string(); | |
127 scoped_ptr<storage::BlobDataSnapshot> data = blob_handle->CreateSnapshot(); | |
128 const auto& items = data->items(); | |
129 for (const auto& item : items) { | |
130 switch (item->type()) { | |
131 case storage::DataElement::TYPE_BYTES: { | |
132 output->append(item->bytes(), item->length()); | |
133 break; | |
134 } | |
135 case storage::DataElement::TYPE_DISK_CACHE_ENTRY: { | |
136 disk_cache::Entry* entry = item->disk_cache_entry(); | |
137 int32 body_size = entry->GetDataSize(item->disk_cache_stream_index()); | |
138 | |
139 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(body_size); | |
140 net::TestCompletionCallback callback; | |
141 int rv = | |
142 entry->ReadData(item->disk_cache_stream_index(), 0, io_buffer.get(), | |
143 body_size, callback.callback()); | |
144 if (rv == net::ERR_IO_PENDING) | |
145 rv = callback.WaitForResult(); | |
146 EXPECT_EQ(body_size, rv); | |
147 if (rv > 0) | |
148 output->append(io_buffer->data(), rv); | |
149 break; | |
150 } | |
151 default: { ADD_FAILURE() << "invalid response blob type"; } break; | |
152 } | |
153 } | |
154 } | |
155 | |
156 bool ResponseMetadataEqual(const ServiceWorkerResponse& expected, | |
157 const ServiceWorkerResponse& actual) { | |
158 EXPECT_EQ(expected.status_code, actual.status_code); | |
159 if (expected.status_code != actual.status_code) | |
160 return false; | |
161 EXPECT_EQ(expected.status_text, actual.status_text); | |
162 if (expected.status_text != actual.status_text) | |
163 return false; | |
164 EXPECT_EQ(expected.url, actual.url); | |
165 if (expected.url != actual.url) | |
166 return false; | |
167 EXPECT_EQ(expected.blob_size, actual.blob_size); | |
168 if (expected.blob_size != actual.blob_size) | |
169 return false; | |
170 | |
171 if (expected.blob_size == 0) { | |
172 EXPECT_STREQ("", actual.blob_uuid.c_str()); | |
173 if (!actual.blob_uuid.empty()) | |
174 return false; | |
175 } else { | |
176 EXPECT_STRNE("", actual.blob_uuid.c_str()); | |
177 if (actual.blob_uuid.empty()) | |
178 return false; | |
179 } | |
180 | |
181 return true; | |
182 } | |
183 | |
184 bool ResponseBodiesEqual(const std::string& expected_body, | |
185 storage::BlobDataHandle* actual_body_handle) { | |
186 std::string actual_body; | |
187 CopyBody(actual_body_handle, &actual_body); | |
188 EXPECT_EQ(expected_body, actual_body); | |
189 if (expected_body != actual_body) | |
jkarlin
2015/08/06 15:11:13
return expected_body == actual_body;
nhiroki
2015/08/07 13:12:16
Done.
| |
190 return false; | |
191 return true; | |
192 } | |
193 | |
125 } // namespace | 194 } // namespace |
126 | 195 |
127 // A CacheStorageCache that can optionally delay during backend creation. | 196 // A CacheStorageCache that can optionally delay during backend creation. |
128 class TestCacheStorageCache : public CacheStorageCache { | 197 class TestCacheStorageCache : public CacheStorageCache { |
129 public: | 198 public: |
130 TestCacheStorageCache( | 199 TestCacheStorageCache( |
131 const GURL& origin, | 200 const GURL& origin, |
132 const base::FilePath& path, | 201 const base::FilePath& path, |
133 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 202 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
134 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 203 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 | 357 |
289 cache_->Match( | 358 cache_->Match( |
290 CopyFetchRequest(request), | 359 CopyFetchRequest(request), |
291 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, | 360 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
292 base::Unretained(this), base::Unretained(loop.get()))); | 361 base::Unretained(this), base::Unretained(loop.get()))); |
293 loop->Run(); | 362 loop->Run(); |
294 | 363 |
295 return callback_error_ == CACHE_STORAGE_OK; | 364 return callback_error_ == CACHE_STORAGE_OK; |
296 } | 365 } |
297 | 366 |
367 bool MatchAll(scoped_ptr<CacheStorageCache::Responses>* responses, | |
368 ScopedVector<storage::BlobDataHandle>* body_handles) { | |
369 base::RunLoop loop; | |
370 cache_->MatchAll(base::Bind( | |
371 &CacheStorageCacheTest::ResponsesAndErrorCallback, | |
372 base::Unretained(this), loop.QuitClosure(), responses, body_handles)); | |
373 loop.Run(); | |
374 return callback_error_ == CACHE_STORAGE_OK; | |
375 } | |
376 | |
298 bool Delete(const ServiceWorkerFetchRequest& request) { | 377 bool Delete(const ServiceWorkerFetchRequest& request) { |
299 CacheStorageBatchOperation operation; | 378 CacheStorageBatchOperation operation; |
300 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; | 379 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; |
301 operation.request = request; | 380 operation.request = request; |
302 | 381 |
303 CacheStorageError error = | 382 CacheStorageError error = |
304 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); | 383 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); |
305 return error == CACHE_STORAGE_OK; | 384 return error == CACHE_STORAGE_OK; |
306 } | 385 } |
307 | 386 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 callback_error_ = error; | 442 callback_error_ = error; |
364 callback_response_ = response.Pass(); | 443 callback_response_ = response.Pass(); |
365 callback_response_data_.reset(); | 444 callback_response_data_.reset(); |
366 if (error == CACHE_STORAGE_OK && !callback_response_->blob_uuid.empty()) | 445 if (error == CACHE_STORAGE_OK && !callback_response_->blob_uuid.empty()) |
367 callback_response_data_ = body_handle.Pass(); | 446 callback_response_data_ = body_handle.Pass(); |
368 | 447 |
369 if (run_loop) | 448 if (run_loop) |
370 run_loop->Quit(); | 449 run_loop->Quit(); |
371 } | 450 } |
372 | 451 |
452 void ResponsesAndErrorCallback( | |
453 const base::Closure& quit_closure, | |
454 scoped_ptr<CacheStorageCache::Responses>* responses_out, | |
455 ScopedVector<storage::BlobDataHandle>* body_handles_out, | |
456 CacheStorageError error, | |
457 scoped_ptr<CacheStorageCache::Responses> responses, | |
458 ScopedVector<storage::BlobDataHandle> body_handles) { | |
459 callback_error_ = error; | |
460 responses_out->swap(responses); | |
461 body_handles_out->swap(body_handles); | |
462 quit_closure.Run(); | |
463 } | |
464 | |
373 void CloseCallback(base::RunLoop* run_loop) { | 465 void CloseCallback(base::RunLoop* run_loop) { |
374 EXPECT_FALSE(callback_closed_); | 466 EXPECT_FALSE(callback_closed_); |
375 callback_closed_ = true; | 467 callback_closed_ = true; |
376 if (run_loop) | 468 if (run_loop) |
377 run_loop->Quit(); | 469 run_loop->Quit(); |
378 } | 470 } |
379 | 471 |
380 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | |
381 *output = std::string(); | |
382 scoped_ptr<storage::BlobDataSnapshot> data = blob_handle->CreateSnapshot(); | |
383 const auto& items = data->items(); | |
384 for (const auto& item : items) { | |
385 switch (item->type()) { | |
386 case storage::DataElement::TYPE_BYTES: { | |
387 output->append(item->bytes(), item->length()); | |
388 break; | |
389 } | |
390 case storage::DataElement::TYPE_DISK_CACHE_ENTRY: { | |
391 disk_cache::Entry* entry = item->disk_cache_entry(); | |
392 int32 body_size = entry->GetDataSize(item->disk_cache_stream_index()); | |
393 | |
394 scoped_refptr<net::IOBuffer> io_buffer = new net::IOBuffer(body_size); | |
395 net::TestCompletionCallback callback; | |
396 int rv = | |
397 entry->ReadData(item->disk_cache_stream_index(), 0, | |
398 io_buffer.get(), body_size, callback.callback()); | |
399 if (rv == net::ERR_IO_PENDING) | |
400 rv = callback.WaitForResult(); | |
401 EXPECT_EQ(body_size, rv); | |
402 if (rv > 0) | |
403 output->append(io_buffer->data(), rv); | |
404 break; | |
405 } | |
406 default: { ADD_FAILURE() << "invalid response blob type"; } break; | |
407 } | |
408 } | |
409 } | |
410 | |
411 bool VerifyKeys(const std::vector<std::string>& expected_keys) { | 472 bool VerifyKeys(const std::vector<std::string>& expected_keys) { |
412 if (expected_keys.size() != callback_strings_.size()) | 473 if (expected_keys.size() != callback_strings_.size()) |
413 return false; | 474 return false; |
414 | 475 |
415 std::set<std::string> found_set; | 476 std::set<std::string> found_set; |
416 for (int i = 0, max = callback_strings_.size(); i < max; ++i) | 477 for (int i = 0, max = callback_strings_.size(); i < max; ++i) |
417 found_set.insert(callback_strings_[i]); | 478 found_set.insert(callback_strings_[i]); |
418 | 479 |
419 for (int i = 0, max = expected_keys.size(); i < max; ++i) { | 480 for (int i = 0, max = expected_keys.size(); i < max; ++i) { |
420 if (found_set.find(expected_keys[i]) == found_set.end()) | 481 if (found_set.find(expected_keys[i]) == found_set.end()) |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
590 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); | 651 EXPECT_EQ(CACHE_STORAGE_OK, BatchOperation(operations)); |
591 | 652 |
592 // |operation2| should win. | 653 // |operation2| should win. |
593 EXPECT_TRUE(Match(operation2.request)); | 654 EXPECT_TRUE(Match(operation2.request)); |
594 EXPECT_TRUE(callback_response_data_); | 655 EXPECT_TRUE(callback_response_data_); |
595 } | 656 } |
596 | 657 |
597 TEST_P(CacheStorageCacheTestP, MatchNoBody) { | 658 TEST_P(CacheStorageCacheTestP, MatchNoBody) { |
598 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 659 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
599 EXPECT_TRUE(Match(no_body_request_)); | 660 EXPECT_TRUE(Match(no_body_request_)); |
600 EXPECT_EQ(200, callback_response_->status_code); | 661 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, *callback_response_)); |
601 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 662 EXPECT_FALSE(callback_response_data_); |
602 EXPECT_STREQ("http://example.com/no_body.html", | |
603 callback_response_->url.spec().c_str()); | |
604 EXPECT_STREQ("", callback_response_->blob_uuid.c_str()); | |
605 EXPECT_EQ(0u, callback_response_->blob_size); | |
606 } | 663 } |
607 | 664 |
608 TEST_P(CacheStorageCacheTestP, MatchBody) { | 665 TEST_P(CacheStorageCacheTestP, MatchBody) { |
609 EXPECT_TRUE(Put(body_request_, body_response_)); | 666 EXPECT_TRUE(Put(body_request_, body_response_)); |
610 EXPECT_TRUE(Match(body_request_)); | 667 EXPECT_TRUE(Match(body_request_)); |
611 EXPECT_EQ(200, callback_response_->status_code); | 668 EXPECT_TRUE(ResponseMetadataEqual(body_response_, *callback_response_)); |
612 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 669 EXPECT_TRUE( |
613 EXPECT_STREQ("http://example.com/body.html", | 670 ResponseBodiesEqual(expected_blob_data_, callback_response_data_.get())); |
614 callback_response_->url.spec().c_str()); | 671 } |
615 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); | |
616 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); | |
617 | 672 |
618 std::string response_body; | 673 TEST_P(CacheStorageCacheTestP, MatchAll_Empty) { |
619 CopyBody(callback_response_data_.get(), &response_body); | 674 scoped_ptr<CacheStorageCache::Responses> responses; |
620 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 675 ScopedVector<storage::BlobDataHandle> body_handles; |
676 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
677 EXPECT_TRUE(responses->empty()); | |
678 EXPECT_TRUE(body_handles.empty()); | |
679 } | |
680 | |
681 TEST_P(CacheStorageCacheTestP, MatchAll_NoBody) { | |
682 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | |
683 | |
684 scoped_ptr<CacheStorageCache::Responses> responses; | |
685 ScopedVector<storage::BlobDataHandle> body_handles; | |
686 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
687 ASSERT_EQ(1u, responses->size()); | |
688 ASSERT_EQ(1u, body_handles.size()); | |
689 | |
690 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(0))); | |
691 EXPECT_FALSE(body_handles[0]); | |
692 } | |
693 | |
694 TEST_P(CacheStorageCacheTestP, MatchAll_Body) { | |
695 EXPECT_TRUE(Put(body_request_, body_response_)); | |
696 | |
697 scoped_ptr<CacheStorageCache::Responses> responses; | |
698 ScopedVector<storage::BlobDataHandle> body_handles; | |
699 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
700 ASSERT_EQ(1u, responses->size()); | |
701 ASSERT_EQ(1u, body_handles.size()); | |
702 | |
703 EXPECT_TRUE(ResponseMetadataEqual(body_response_, responses->at(0))); | |
704 EXPECT_TRUE(ResponseBodiesEqual(expected_blob_data_, body_handles[0])); | |
705 } | |
706 | |
707 TEST_P(CacheStorageCacheTestP, MatchAll_TwoResponsesThenOne) { | |
708 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | |
709 EXPECT_TRUE(Put(body_request_, body_response_)); | |
710 | |
711 scoped_ptr<CacheStorageCache::Responses> responses; | |
712 ScopedVector<storage::BlobDataHandle> body_handles; | |
713 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
714 ASSERT_EQ(2u, responses->size()); | |
715 ASSERT_EQ(2u, body_handles.size()); | |
716 | |
717 // Order of returned responses is not guaranteed. | |
718 std::set<std::string> matched_set; | |
719 for (size_t i = 0; i < responses->size(); ++i) { | |
720 if (responses->at(i).url.spec() == "http://example.com/no_body.html") { | |
721 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(i))); | |
722 EXPECT_FALSE(body_handles[i]); | |
723 matched_set.insert(responses->at(i).url.spec()); | |
724 } else if (responses->at(i).url.spec() == "http://example.com/body.html") { | |
725 EXPECT_TRUE(ResponseMetadataEqual(body_response_, responses->at(i))); | |
726 EXPECT_TRUE(ResponseBodiesEqual(expected_blob_data_, body_handles[i])); | |
727 matched_set.insert(responses->at(i).url.spec()); | |
728 } | |
729 } | |
730 EXPECT_EQ(2u, matched_set.size()); | |
731 | |
732 responses->clear(); | |
733 body_handles.clear(); | |
734 | |
735 EXPECT_TRUE(Delete(body_request_)); | |
736 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
737 | |
738 ASSERT_EQ(1u, responses->size()); | |
739 ASSERT_EQ(1u, body_handles.size()); | |
740 EXPECT_TRUE(ResponseMetadataEqual(no_body_response_, responses->at(0))); | |
741 EXPECT_FALSE(body_handles[0]); | |
621 } | 742 } |
622 | 743 |
623 TEST_P(CacheStorageCacheTestP, Vary) { | 744 TEST_P(CacheStorageCacheTestP, Vary) { |
624 body_request_.headers["vary_foo"] = "foo"; | 745 body_request_.headers["vary_foo"] = "foo"; |
625 body_response_.headers["vary"] = "vary_foo"; | 746 body_response_.headers["vary"] = "vary_foo"; |
626 EXPECT_TRUE(Put(body_request_, body_response_)); | 747 EXPECT_TRUE(Put(body_request_, body_response_)); |
627 EXPECT_TRUE(Match(body_request_)); | 748 EXPECT_TRUE(Match(body_request_)); |
628 | 749 |
629 body_request_.headers["vary_foo"] = "bar"; | 750 body_request_.headers["vary_foo"] = "bar"; |
630 EXPECT_FALSE(Match(body_request_)); | 751 EXPECT_FALSE(Match(body_request_)); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
889 EXPECT_EQ(1, sequence_out); | 1010 EXPECT_EQ(1, sequence_out); |
890 close_loop2->Run(); | 1011 close_loop2->Run(); |
891 EXPECT_EQ(2, sequence_out); | 1012 EXPECT_EQ(2, sequence_out); |
892 } | 1013 } |
893 | 1014 |
894 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1015 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
895 CacheStorageCacheTestP, | 1016 CacheStorageCacheTestP, |
896 ::testing::Values(false, true)); | 1017 ::testing::Values(false, true)); |
897 | 1018 |
898 } // namespace content | 1019 } // namespace content |
OLD | NEW |