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/service_worker/service_worker_cache.h" | 5 #include "content/browser/service_worker/service_worker_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/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } else { | 71 } else { |
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
73 cache_ = ServiceWorkerCache::CreatePersistentCache( | 73 cache_ = ServiceWorkerCache::CreatePersistentCache( |
74 temp_dir_.path(), | 74 temp_dir_.path(), |
75 url_request_context, | 75 url_request_context, |
76 blob_storage_context->context()->AsWeakPtr()); | 76 blob_storage_context->context()->AsWeakPtr()); |
77 } | 77 } |
78 CreateBackend(); | 78 CreateBackend(); |
79 } | 79 } |
80 | 80 |
81 virtual void TearDown() OVERRIDE { | |
82 base::RunLoop().RunUntilIdle(); | |
83 cache_.reset(); | |
84 base::RunLoop().RunUntilIdle(); | |
85 } | |
86 | |
87 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { | 81 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
88 std::map<std::string, std::string> headers; | 82 std::map<std::string, std::string> headers; |
89 headers.insert(std::make_pair("a", "a")); | 83 headers.insert(std::make_pair("a", "a")); |
90 headers.insert(std::make_pair("b", "b")); | 84 headers.insert(std::make_pair("b", "b")); |
91 body_request_.reset(new ServiceWorkerFetchRequest( | 85 body_request_.reset(new ServiceWorkerFetchRequest( |
92 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false)); | 86 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false)); |
93 no_body_request_.reset( | 87 no_body_request_.reset( |
94 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), | 88 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), |
95 "GET", | 89 "GET", |
96 headers, | 90 headers, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 156 |
163 cache_->Delete(request, | 157 cache_->Delete(request, |
164 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, | 158 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, |
165 base::Unretained(this), | 159 base::Unretained(this), |
166 base::Unretained(loop.get()))); | 160 base::Unretained(loop.get()))); |
167 loop->Run(); | 161 loop->Run(); |
168 | 162 |
169 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | 163 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
170 } | 164 } |
171 | 165 |
172 bool Keys() { | |
173 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
174 | |
175 cache_->Keys(base::Bind(&ServiceWorkerCacheTest::RequestsCallback, | |
176 base::Unretained(this), | |
177 base::Unretained(loop.get()))); | |
178 loop->Run(); | |
179 | |
180 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | |
181 } | |
182 | |
183 void RequestsCallback(base::RunLoop* run_loop, | |
184 ServiceWorkerCache::ErrorType error, | |
185 scoped_ptr<ServiceWorkerCache::Requests> requests) { | |
186 callback_error_ = error; | |
187 callback_strings_.clear(); | |
188 for (size_t i = 0u; i < requests->size(); ++i) | |
189 callback_strings_.push_back(requests->at(i).url.spec()); | |
190 run_loop->Quit(); | |
191 } | |
192 | |
193 void ErrorTypeCallback(base::RunLoop* run_loop, | 166 void ErrorTypeCallback(base::RunLoop* run_loop, |
194 ServiceWorkerCache::ErrorType error) { | 167 ServiceWorkerCache::ErrorType error) { |
195 callback_error_ = error; | 168 callback_error_ = error; |
196 run_loop->Quit(); | 169 run_loop->Quit(); |
197 } | 170 } |
198 | 171 |
199 void ResponseAndErrorCallback( | 172 void ResponseAndErrorCallback( |
200 base::RunLoop* run_loop, | 173 base::RunLoop* run_loop, |
201 ServiceWorkerCache::ErrorType error, | 174 ServiceWorkerCache::ErrorType error, |
202 scoped_ptr<ServiceWorkerResponse> response, | 175 scoped_ptr<ServiceWorkerResponse> response, |
203 scoped_ptr<storage::BlobDataHandle> body_handle) { | 176 scoped_ptr<storage::BlobDataHandle> body_handle) { |
204 callback_error_ = error; | 177 callback_error_ = error; |
205 callback_response_ = response.Pass(); | 178 callback_response_ = response.Pass(); |
206 | 179 |
207 if (error == ServiceWorkerCache::ErrorTypeOK && | 180 if (error == ServiceWorkerCache::ErrorTypeOK && |
208 !callback_response_->blob_uuid.empty()) { | 181 !callback_response_->blob_uuid.empty()) { |
209 callback_response_data_ = body_handle.Pass(); | 182 callback_response_data_ = body_handle.Pass(); |
210 } | 183 } |
211 | 184 |
212 run_loop->Quit(); | 185 run_loop->Quit(); |
213 } | 186 } |
214 | 187 |
215 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | 188 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { |
216 storage::BlobData* data = blob_handle->data(); | 189 storage::BlobData* data = blob_handle->data(); |
217 std::vector<storage::BlobData::Item> items = data->items(); | 190 std::vector<storage::BlobData::Item> items = data->items(); |
218 for (size_t i = 0, max = items.size(); i < max; ++i) | 191 for (size_t i = 0, max = items.size(); i < max; ++i) |
219 output->append(items[i].bytes(), items[i].length()); | 192 output->append(items[i].bytes(), items[i].length()); |
220 } | 193 } |
221 | 194 |
222 bool VerifyKeys(const std::vector<std::string>& expected_keys) { | |
223 if (expected_keys.size() != callback_strings_.size()) | |
224 return false; | |
225 | |
226 std::set<std::string> found_set; | |
227 for (int i = 0, max = callback_strings_.size(); i < max; ++i) | |
228 found_set.insert(callback_strings_[i]); | |
229 | |
230 for (int i = 0, max = expected_keys.size(); i < max; ++i) { | |
231 if (found_set.find(expected_keys[i]) == found_set.end()) | |
232 return false; | |
233 } | |
234 return true; | |
235 } | |
236 | |
237 virtual bool MemoryOnly() { return false; } | 195 virtual bool MemoryOnly() { return false; } |
238 | 196 |
239 protected: | 197 protected: |
240 TestBrowserContext browser_context_; | 198 TestBrowserContext browser_context_; |
241 TestBrowserThreadBundle browser_thread_bundle_; | 199 TestBrowserThreadBundle browser_thread_bundle_; |
242 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 200 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
243 storage::BlobStorageContext* blob_storage_context_; | 201 storage::BlobStorageContext* blob_storage_context_; |
244 | 202 |
245 base::ScopedTempDir temp_dir_; | 203 base::ScopedTempDir temp_dir_; |
246 scoped_ptr<ServiceWorkerCache> cache_; | 204 scoped_ptr<ServiceWorkerCache> cache_; |
247 | 205 |
248 scoped_ptr<ServiceWorkerFetchRequest> body_request_; | 206 scoped_ptr<ServiceWorkerFetchRequest> body_request_; |
249 scoped_ptr<ServiceWorkerResponse> body_response_; | 207 scoped_ptr<ServiceWorkerResponse> body_response_; |
250 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; | 208 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; |
251 scoped_ptr<ServiceWorkerResponse> no_body_response_; | 209 scoped_ptr<ServiceWorkerResponse> no_body_response_; |
252 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 210 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
253 std::string expected_blob_data_; | 211 std::string expected_blob_data_; |
254 | 212 |
255 ServiceWorkerCache::ErrorType callback_error_; | 213 ServiceWorkerCache::ErrorType callback_error_; |
256 scoped_ptr<ServiceWorkerResponse> callback_response_; | 214 scoped_ptr<ServiceWorkerResponse> callback_response_; |
257 scoped_ptr<storage::BlobDataHandle> callback_response_data_; | 215 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
258 std::vector<std::string> callback_strings_; | |
259 }; | 216 }; |
260 | 217 |
261 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, | 218 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, |
262 public testing::WithParamInterface<bool> { | 219 public testing::WithParamInterface<bool> { |
263 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } | 220 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } |
264 }; | 221 }; |
265 | 222 |
266 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { | 223 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { |
267 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 224 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
268 } | 225 } |
(...skipping 10 matching lines...) Expand all Loading... |
279 base::Unretained(this), | 236 base::Unretained(this), |
280 base::Unretained(loop.get()))); | 237 base::Unretained(loop.get()))); |
281 // The handle should be held by the cache now so the deref here should be | 238 // The handle should be held by the cache now so the deref here should be |
282 // okay. | 239 // okay. |
283 blob_handle_.reset(); | 240 blob_handle_.reset(); |
284 loop->Run(); | 241 loop->Run(); |
285 | 242 |
286 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); | 243 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); |
287 } | 244 } |
288 | 245 |
| 246 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { |
| 247 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 248 EXPECT_TRUE(Match(no_body_request_.get())); |
| 249 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 250 EXPECT_FALSE(Match(no_body_request_.get())); |
| 251 EXPECT_FALSE(Delete(no_body_request_.get())); |
| 252 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 253 EXPECT_TRUE(Match(no_body_request_.get())); |
| 254 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 255 } |
| 256 |
| 257 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { |
| 258 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 259 EXPECT_TRUE(Match(body_request_.get())); |
| 260 EXPECT_TRUE(Delete(body_request_.get())); |
| 261 EXPECT_FALSE(Match(body_request_.get())); |
| 262 EXPECT_FALSE(Delete(body_request_.get())); |
| 263 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 264 EXPECT_TRUE(Match(body_request_.get())); |
| 265 EXPECT_TRUE(Delete(body_request_.get())); |
| 266 } |
| 267 |
289 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { | 268 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { |
290 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 269 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
291 EXPECT_TRUE(Match(no_body_request_.get())); | 270 EXPECT_TRUE(Match(no_body_request_.get())); |
292 EXPECT_EQ(200, callback_response_->status_code); | 271 EXPECT_EQ(200, callback_response_->status_code); |
293 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 272 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
294 EXPECT_STREQ("http://example.com/no_body.html", | 273 EXPECT_STREQ("http://example.com/no_body.html", |
295 callback_response_->url.spec().c_str()); | 274 callback_response_->url.spec().c_str()); |
296 } | 275 } |
297 | 276 |
298 TEST_P(ServiceWorkerCacheTestP, MatchBody) { | 277 TEST_P(ServiceWorkerCacheTestP, MatchBody) { |
299 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | 278 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
300 EXPECT_TRUE(Match(body_request_.get())); | 279 EXPECT_TRUE(Match(body_request_.get())); |
301 EXPECT_EQ(200, callback_response_->status_code); | 280 EXPECT_EQ(200, callback_response_->status_code); |
302 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 281 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
303 EXPECT_STREQ("http://example.com/body.html", | 282 EXPECT_STREQ("http://example.com/body.html", |
304 callback_response_->url.spec().c_str()); | 283 callback_response_->url.spec().c_str()); |
305 std::string response_body; | 284 std::string response_body; |
306 CopyBody(callback_response_data_.get(), &response_body); | 285 CopyBody(callback_response_data_.get(), &response_body); |
307 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 286 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
308 } | 287 } |
309 | 288 |
310 TEST_P(ServiceWorkerCacheTestP, EmptyKeys) { | |
311 EXPECT_TRUE(Keys()); | |
312 EXPECT_EQ(0u, callback_strings_.size()); | |
313 } | |
314 | |
315 TEST_P(ServiceWorkerCacheTestP, TwoKeys) { | |
316 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
317 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
318 EXPECT_TRUE(Keys()); | |
319 EXPECT_EQ(2u, callback_strings_.size()); | |
320 std::vector<std::string> expected_keys; | |
321 expected_keys.push_back(no_body_request_->url.spec()); | |
322 expected_keys.push_back(body_request_->url.spec()); | |
323 EXPECT_TRUE(VerifyKeys(expected_keys)); | |
324 } | |
325 | |
326 TEST_P(ServiceWorkerCacheTestP, TwoKeysThenOne) { | |
327 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
328 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
329 EXPECT_TRUE(Keys()); | |
330 EXPECT_EQ(2u, callback_strings_.size()); | |
331 std::vector<std::string> expected_keys; | |
332 expected_keys.push_back(no_body_request_->url.spec()); | |
333 expected_keys.push_back(body_request_->url.spec()); | |
334 EXPECT_TRUE(VerifyKeys(expected_keys)); | |
335 | |
336 EXPECT_TRUE(Delete(body_request_.get())); | |
337 EXPECT_TRUE(Keys()); | |
338 EXPECT_EQ(1u, callback_strings_.size()); | |
339 std::vector<std::string> expected_key; | |
340 expected_key.push_back(no_body_request_->url.spec()); | |
341 EXPECT_TRUE(VerifyKeys(expected_key)); | |
342 } | |
343 | |
344 // TODO(jkarlin): Once SimpleCache is working bug-free on Windows reenable these | |
345 // tests. In the meanwhile we know that Windows operations will be a little | |
346 // flaky (though not crashy). See https://crbug.com/409109 | |
347 #ifndef OS_WIN | |
348 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { | |
349 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
350 EXPECT_TRUE(Match(no_body_request_.get())); | |
351 EXPECT_TRUE(Delete(no_body_request_.get())); | |
352 EXPECT_FALSE(Match(no_body_request_.get())); | |
353 EXPECT_FALSE(Delete(no_body_request_.get())); | |
354 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
355 EXPECT_TRUE(Match(no_body_request_.get())); | |
356 EXPECT_TRUE(Delete(no_body_request_.get())); | |
357 } | |
358 | |
359 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { | |
360 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
361 EXPECT_TRUE(Match(body_request_.get())); | |
362 EXPECT_TRUE(Delete(body_request_.get())); | |
363 EXPECT_FALSE(Match(body_request_.get())); | |
364 EXPECT_FALSE(Delete(body_request_.get())); | |
365 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
366 EXPECT_TRUE(Match(body_request_.get())); | |
367 EXPECT_TRUE(Delete(body_request_.get())); | |
368 } | |
369 | |
370 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { | 289 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { |
371 for (int i = 0; i < 100; ++i) { | 290 for (int i = 0; i < 100; ++i) { |
372 EXPECT_FALSE(Match(no_body_request_.get())); | 291 EXPECT_FALSE(Match(no_body_request_.get())); |
373 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 292 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
374 EXPECT_TRUE(Match(no_body_request_.get())); | 293 EXPECT_TRUE(Match(no_body_request_.get())); |
375 EXPECT_TRUE(Delete(no_body_request_.get())); | 294 EXPECT_TRUE(Delete(no_body_request_.get())); |
376 } | 295 } |
377 } | 296 } |
378 | 297 |
379 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { | 298 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { |
380 for (int i = 0; i < 100; ++i) { | 299 for (int i = 0; i < 100; ++i) { |
381 ASSERT_FALSE(Match(body_request_.get())); | 300 ASSERT_FALSE(Match(body_request_.get())); |
382 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); | 301 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); |
383 ASSERT_TRUE(Match(body_request_.get())); | 302 ASSERT_TRUE(Match(body_request_.get())); |
384 ASSERT_TRUE(Delete(body_request_.get())); | 303 ASSERT_TRUE(Delete(body_request_.get())); |
385 } | 304 } |
386 } | 305 } |
387 #endif // OS_WIN | |
388 | 306 |
389 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 307 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
390 ServiceWorkerCacheTestP, | 308 ServiceWorkerCacheTestP, |
391 ::testing::Values(false, true)); | 309 ::testing::Values(false, true)); |
392 | 310 |
393 } // namespace content | 311 } // namespace content |
OLD | NEW |