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 bool ResponsesEqual(const ServiceWorkerResponse& expected, | |
126 const ServiceWorkerResponse& actual) { | |
127 EXPECT_EQ(expected.status_code, actual.status_code); | |
128 if (expected.status_code != actual.status_code) | |
129 return false; | |
130 EXPECT_EQ(expected.status_text, actual.status_text); | |
131 if (expected.status_text != actual.status_text) | |
132 return false; | |
133 EXPECT_EQ(expected.url, actual.url); | |
134 if (expected.url != actual.url) | |
135 return false; | |
136 EXPECT_EQ(expected.blob_size, actual.blob_size); | |
137 if (expected.blob_size != actual.blob_size) | |
138 return false; | |
139 | |
140 if (expected.blob_size == 0) { | |
141 EXPECT_STREQ("", actual.blob_uuid.c_str()); | |
142 if (!actual.blob_uuid.empty()) | |
143 return false; | |
144 } else { | |
145 EXPECT_STRNE("", actual.blob_uuid.c_str()); | |
146 if (actual.blob_uuid.empty()) | |
jkarlin
2015/08/05 12:09:05
Can you compare the blob contents of the two respo
nhiroki
2015/08/06 03:36:53
Calling CopyBody() here might complicate this func
| |
147 return false; | |
148 } | |
149 | |
150 return true; | |
151 } | |
152 | |
125 } // namespace | 153 } // namespace |
126 | 154 |
127 // A CacheStorageCache that can optionally delay during backend creation. | 155 // A CacheStorageCache that can optionally delay during backend creation. |
128 class TestCacheStorageCache : public CacheStorageCache { | 156 class TestCacheStorageCache : public CacheStorageCache { |
129 public: | 157 public: |
130 TestCacheStorageCache( | 158 TestCacheStorageCache( |
131 const GURL& origin, | 159 const GURL& origin, |
132 const base::FilePath& path, | 160 const base::FilePath& path, |
133 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 161 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
134 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 162 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 | 316 |
289 cache_->Match( | 317 cache_->Match( |
290 CopyFetchRequest(request), | 318 CopyFetchRequest(request), |
291 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, | 319 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
292 base::Unretained(this), base::Unretained(loop.get()))); | 320 base::Unretained(this), base::Unretained(loop.get()))); |
293 loop->Run(); | 321 loop->Run(); |
294 | 322 |
295 return callback_error_ == CACHE_STORAGE_OK; | 323 return callback_error_ == CACHE_STORAGE_OK; |
296 } | 324 } |
297 | 325 |
326 bool MatchAll(std::vector<ServiceWorkerResponse>* responses, | |
327 ScopedVector<storage::BlobDataHandle>* body_handles) { | |
328 base::RunLoop loop; | |
329 cache_->MatchAll(base::Bind( | |
330 &CacheStorageCacheTest::ResponsesAndErrorCallback, | |
331 base::Unretained(this), loop.QuitClosure(), responses, body_handles)); | |
332 loop.Run(); | |
333 return callback_error_ == CACHE_STORAGE_OK; | |
334 } | |
335 | |
298 bool Delete(const ServiceWorkerFetchRequest& request) { | 336 bool Delete(const ServiceWorkerFetchRequest& request) { |
299 CacheStorageBatchOperation operation; | 337 CacheStorageBatchOperation operation; |
300 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; | 338 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; |
301 operation.request = request; | 339 operation.request = request; |
302 | 340 |
303 CacheStorageError error = | 341 CacheStorageError error = |
304 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); | 342 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); |
305 return error == CACHE_STORAGE_OK; | 343 return error == CACHE_STORAGE_OK; |
306 } | 344 } |
307 | 345 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 callback_error_ = error; | 401 callback_error_ = error; |
364 callback_response_ = response.Pass(); | 402 callback_response_ = response.Pass(); |
365 callback_response_data_.reset(); | 403 callback_response_data_.reset(); |
366 if (error == CACHE_STORAGE_OK && !callback_response_->blob_uuid.empty()) | 404 if (error == CACHE_STORAGE_OK && !callback_response_->blob_uuid.empty()) |
367 callback_response_data_ = body_handle.Pass(); | 405 callback_response_data_ = body_handle.Pass(); |
368 | 406 |
369 if (run_loop) | 407 if (run_loop) |
370 run_loop->Quit(); | 408 run_loop->Quit(); |
371 } | 409 } |
372 | 410 |
411 void ResponsesAndErrorCallback( | |
412 const base::Closure& quit_closure, | |
413 std::vector<ServiceWorkerResponse>* responses_out, | |
414 ScopedVector<storage::BlobDataHandle>* body_handles_out, | |
415 CacheStorageError error, | |
416 const std::vector<ServiceWorkerResponse>& responses, | |
417 ScopedVector<storage::BlobDataHandle> body_handles) { | |
418 callback_error_ = error; | |
419 *responses_out = responses; | |
420 body_handles_out->swap(body_handles); | |
421 quit_closure.Run(); | |
422 } | |
423 | |
373 void CloseCallback(base::RunLoop* run_loop) { | 424 void CloseCallback(base::RunLoop* run_loop) { |
374 EXPECT_FALSE(callback_closed_); | 425 EXPECT_FALSE(callback_closed_); |
375 callback_closed_ = true; | 426 callback_closed_ = true; |
376 if (run_loop) | 427 if (run_loop) |
377 run_loop->Quit(); | 428 run_loop->Quit(); |
378 } | 429 } |
379 | 430 |
380 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | 431 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { |
381 *output = std::string(); | 432 *output = std::string(); |
382 scoped_ptr<storage::BlobDataSnapshot> data = blob_handle->CreateSnapshot(); | 433 scoped_ptr<storage::BlobDataSnapshot> data = blob_handle->CreateSnapshot(); |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 EXPECT_STREQ("http://example.com/body.html", | 664 EXPECT_STREQ("http://example.com/body.html", |
614 callback_response_->url.spec().c_str()); | 665 callback_response_->url.spec().c_str()); |
615 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); | 666 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); |
616 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); | 667 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); |
617 | 668 |
618 std::string response_body; | 669 std::string response_body; |
619 CopyBody(callback_response_data_.get(), &response_body); | 670 CopyBody(callback_response_data_.get(), &response_body); |
620 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 671 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
621 } | 672 } |
622 | 673 |
674 TEST_P(CacheStorageCacheTestP, MatchAll_Empty) { | |
675 std::vector<ServiceWorkerResponse> responses; | |
676 ScopedVector<storage::BlobDataHandle> body_handles; | |
677 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
678 EXPECT_TRUE(responses.empty()); | |
679 EXPECT_TRUE(body_handles.empty()); | |
680 } | |
681 | |
682 TEST_P(CacheStorageCacheTestP, MatchAll_NoBody) { | |
683 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | |
684 | |
685 std::vector<ServiceWorkerResponse> responses; | |
686 ScopedVector<storage::BlobDataHandle> body_handles; | |
687 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
688 ASSERT_EQ(1u, responses.size()); | |
689 ASSERT_EQ(1u, body_handles.size()); | |
690 | |
691 EXPECT_TRUE(ResponsesEqual(no_body_response_, responses[0])); | |
692 EXPECT_FALSE(body_handles[0]); | |
693 } | |
694 | |
695 TEST_P(CacheStorageCacheTestP, MatchAll_Body) { | |
696 EXPECT_TRUE(Put(body_request_, body_response_)); | |
697 | |
698 std::vector<ServiceWorkerResponse> responses; | |
699 ScopedVector<storage::BlobDataHandle> body_handles; | |
700 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
701 ASSERT_EQ(1u, responses.size()); | |
702 ASSERT_EQ(1u, body_handles.size()); | |
703 | |
704 EXPECT_TRUE(ResponsesEqual(body_response_, responses[0])); | |
705 std::string response_body; | |
706 CopyBody(body_handles[0], &response_body); | |
707 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | |
708 } | |
709 | |
710 TEST_P(CacheStorageCacheTestP, MatchAll_TwoResponsesThenOne) { | |
711 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | |
712 EXPECT_TRUE(Put(body_request_, body_response_)); | |
713 | |
714 std::vector<ServiceWorkerResponse> responses; | |
715 ScopedVector<storage::BlobDataHandle> body_handles; | |
716 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
717 ASSERT_EQ(2u, responses.size()); | |
718 ASSERT_EQ(2u, body_handles.size()); | |
719 | |
720 // Order of returned responses is not guaranteed. | |
721 std::set<std::string> matched_set; | |
722 for (size_t i = 0; i < responses.size(); ++i) { | |
723 if (responses[i].url.spec() == "http://example.com/no_body.html") { | |
724 EXPECT_TRUE(ResponsesEqual(no_body_response_, responses[i])); | |
725 EXPECT_FALSE(body_handles[i]); | |
726 matched_set.insert(responses[i].url.spec()); | |
727 } else if (responses[i].url.spec() == "http://example.com/body.html") { | |
728 EXPECT_TRUE(ResponsesEqual(body_response_, responses[i])); | |
729 std::string response_body; | |
730 CopyBody(body_handles[i], &response_body); | |
731 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | |
732 matched_set.insert(responses[i].url.spec()); | |
733 } | |
734 } | |
735 EXPECT_EQ(2u, matched_set.size()); | |
736 | |
737 responses.clear(); | |
738 body_handles.clear(); | |
739 | |
740 EXPECT_TRUE(Delete(body_request_)); | |
741 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
742 | |
743 ASSERT_EQ(1u, responses.size()); | |
744 ASSERT_EQ(1u, body_handles.size()); | |
745 EXPECT_TRUE(ResponsesEqual(no_body_response_, responses[0])); | |
746 EXPECT_FALSE(body_handles[0]); | |
747 } | |
748 | |
623 TEST_P(CacheStorageCacheTestP, Vary) { | 749 TEST_P(CacheStorageCacheTestP, Vary) { |
624 body_request_.headers["vary_foo"] = "foo"; | 750 body_request_.headers["vary_foo"] = "foo"; |
625 body_response_.headers["vary"] = "vary_foo"; | 751 body_response_.headers["vary"] = "vary_foo"; |
626 EXPECT_TRUE(Put(body_request_, body_response_)); | 752 EXPECT_TRUE(Put(body_request_, body_response_)); |
627 EXPECT_TRUE(Match(body_request_)); | 753 EXPECT_TRUE(Match(body_request_)); |
628 | 754 |
629 body_request_.headers["vary_foo"] = "bar"; | 755 body_request_.headers["vary_foo"] = "bar"; |
630 EXPECT_FALSE(Match(body_request_)); | 756 EXPECT_FALSE(Match(body_request_)); |
631 | 757 |
632 body_request_.headers.erase("vary_foo"); | 758 body_request_.headers.erase("vary_foo"); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
889 EXPECT_EQ(1, sequence_out); | 1015 EXPECT_EQ(1, sequence_out); |
890 close_loop2->Run(); | 1016 close_loop2->Run(); |
891 EXPECT_EQ(2, sequence_out); | 1017 EXPECT_EQ(2, sequence_out); |
892 } | 1018 } |
893 | 1019 |
894 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1020 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
895 CacheStorageCacheTestP, | 1021 CacheStorageCacheTestP, |
896 ::testing::Values(false, true)); | 1022 ::testing::Values(false, true)); |
897 | 1023 |
898 } // namespace content | 1024 } // namespace content |
OLD | NEW |