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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "content/browser/appcache/appcache_group.h" | 10 #include "content/browser/appcache/appcache_group.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 (*headers) = std::string(not_found_headers, | 188 (*headers) = std::string(not_found_headers, |
189 arraysize(not_found_headers)); | 189 arraysize(not_found_headers)); |
190 (*body) = ""; | 190 (*body) = ""; |
191 } | 191 } |
192 } | 192 } |
193 }; | 193 }; |
194 | 194 |
195 class MockHttpServerJobFactory | 195 class MockHttpServerJobFactory |
196 : public net::URLRequestJobFactory::ProtocolHandler { | 196 : public net::URLRequestJobFactory::ProtocolHandler { |
197 public: | 197 public: |
198 virtual net::URLRequestJob* MaybeCreateJob( | 198 net::URLRequestJob* MaybeCreateJob( |
199 net::URLRequest* request, | 199 net::URLRequest* request, |
200 net::NetworkDelegate* network_delegate) const override { | 200 net::NetworkDelegate* network_delegate) const override { |
201 return MockHttpServer::JobFactory(request, network_delegate); | 201 return MockHttpServer::JobFactory(request, network_delegate); |
202 } | 202 } |
203 }; | 203 }; |
204 | 204 |
205 inline bool operator==(const AppCacheNamespace& lhs, | 205 inline bool operator==(const AppCacheNamespace& lhs, |
206 const AppCacheNamespace& rhs) { | 206 const AppCacheNamespace& rhs) { |
207 return lhs.type == rhs.type && | 207 return lhs.type == rhs.type && |
208 lhs.namespace_url == rhs.namespace_url && | 208 lhs.namespace_url == rhs.namespace_url && |
209 lhs.target_url == rhs.target_url; | 209 lhs.target_url == rhs.target_url; |
210 } | 210 } |
211 | 211 |
212 } // namespace | 212 } // namespace |
213 | 213 |
214 class MockFrontend : public AppCacheFrontend { | 214 class MockFrontend : public AppCacheFrontend { |
215 public: | 215 public: |
216 MockFrontend() | 216 MockFrontend() |
217 : ignore_progress_events_(false), verify_progress_events_(false), | 217 : ignore_progress_events_(false), verify_progress_events_(false), |
218 last_progress_total_(-1), last_progress_complete_(-1), | 218 last_progress_total_(-1), last_progress_complete_(-1), |
219 start_update_trigger_(APPCACHE_CHECKING_EVENT), update_(NULL) { | 219 start_update_trigger_(APPCACHE_CHECKING_EVENT), update_(NULL) { |
220 } | 220 } |
221 | 221 |
222 virtual void OnCacheSelected( | 222 void OnCacheSelected(int host_id, const AppCacheInfo& info) override {} |
223 int host_id, const AppCacheInfo& info) override { | |
224 } | |
225 | 223 |
226 virtual void OnStatusChanged(const std::vector<int>& host_ids, | 224 void OnStatusChanged(const std::vector<int>& host_ids, |
227 AppCacheStatus status) override { | 225 AppCacheStatus status) override {} |
228 } | |
229 | 226 |
230 virtual void OnEventRaised(const std::vector<int>& host_ids, | 227 void OnEventRaised(const std::vector<int>& host_ids, |
231 AppCacheEventID event_id) override { | 228 AppCacheEventID event_id) override { |
232 raised_events_.push_back(RaisedEvent(host_ids, event_id)); | 229 raised_events_.push_back(RaisedEvent(host_ids, event_id)); |
233 | 230 |
234 // Trigger additional updates if requested. | 231 // Trigger additional updates if requested. |
235 if (event_id == start_update_trigger_ && update_) { | 232 if (event_id == start_update_trigger_ && update_) { |
236 for (std::vector<AppCacheHost*>::iterator it = update_hosts_.begin(); | 233 for (std::vector<AppCacheHost*>::iterator it = update_hosts_.begin(); |
237 it != update_hosts_.end(); ++it) { | 234 it != update_hosts_.end(); ++it) { |
238 AppCacheHost* host = *it; | 235 AppCacheHost* host = *it; |
239 update_->StartUpdate(host, | 236 update_->StartUpdate(host, |
240 (host ? host->pending_master_entry_url() : GURL())); | 237 (host ? host->pending_master_entry_url() : GURL())); |
241 } | 238 } |
242 update_hosts_.clear(); // only trigger once | 239 update_hosts_.clear(); // only trigger once |
243 } | 240 } |
244 } | 241 } |
245 | 242 |
246 virtual void OnErrorEventRaised(const std::vector<int>& host_ids, | 243 void OnErrorEventRaised(const std::vector<int>& host_ids, |
247 const AppCacheErrorDetails& details) | 244 const AppCacheErrorDetails& details) override { |
248 override { | |
249 error_message_ = details.message; | 245 error_message_ = details.message; |
250 OnEventRaised(host_ids, APPCACHE_ERROR_EVENT); | 246 OnEventRaised(host_ids, APPCACHE_ERROR_EVENT); |
251 } | 247 } |
252 | 248 |
253 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, | 249 void OnProgressEventRaised(const std::vector<int>& host_ids, |
254 const GURL& url, | 250 const GURL& url, |
255 int num_total, | 251 int num_total, |
256 int num_complete) override { | 252 int num_complete) override { |
257 if (!ignore_progress_events_) | 253 if (!ignore_progress_events_) |
258 OnEventRaised(host_ids, APPCACHE_PROGRESS_EVENT); | 254 OnEventRaised(host_ids, APPCACHE_PROGRESS_EVENT); |
259 | 255 |
260 if (verify_progress_events_) { | 256 if (verify_progress_events_) { |
261 EXPECT_GE(num_total, num_complete); | 257 EXPECT_GE(num_total, num_complete); |
262 EXPECT_GE(num_complete, 0); | 258 EXPECT_GE(num_complete, 0); |
263 | 259 |
264 if (last_progress_total_ == -1) { | 260 if (last_progress_total_ == -1) { |
265 // Should start at zero. | 261 // Should start at zero. |
266 EXPECT_EQ(0, num_complete); | 262 EXPECT_EQ(0, num_complete); |
267 } else { | 263 } else { |
268 // Total should be stable and complete should bump up by one at a time. | 264 // Total should be stable and complete should bump up by one at a time. |
269 EXPECT_EQ(last_progress_total_, num_total); | 265 EXPECT_EQ(last_progress_total_, num_total); |
270 EXPECT_EQ(last_progress_complete_ + 1, num_complete); | 266 EXPECT_EQ(last_progress_complete_ + 1, num_complete); |
271 } | 267 } |
272 | 268 |
273 // Url should be valid for all except the 'final' event. | 269 // Url should be valid for all except the 'final' event. |
274 if (num_total == num_complete) | 270 if (num_total == num_complete) |
275 EXPECT_TRUE(url.is_empty()); | 271 EXPECT_TRUE(url.is_empty()); |
276 else | 272 else |
277 EXPECT_TRUE(url.is_valid()); | 273 EXPECT_TRUE(url.is_valid()); |
278 | 274 |
279 last_progress_total_ = num_total; | 275 last_progress_total_ = num_total; |
280 last_progress_complete_ = num_complete; | 276 last_progress_complete_ = num_complete; |
281 } | 277 } |
282 } | 278 } |
283 | 279 |
284 virtual void OnLogMessage(int host_id, | 280 void OnLogMessage(int host_id, |
285 AppCacheLogLevel log_level, | 281 AppCacheLogLevel log_level, |
286 const std::string& message) override { | 282 const std::string& message) override {} |
287 } | |
288 | 283 |
289 virtual void OnContentBlocked(int host_id, | 284 void OnContentBlocked(int host_id, const GURL& manifest_url) override {} |
290 const GURL& manifest_url) override { | |
291 } | |
292 | 285 |
293 void AddExpectedEvent(const std::vector<int>& host_ids, | 286 void AddExpectedEvent(const std::vector<int>& host_ids, |
294 AppCacheEventID event_id) { | 287 AppCacheEventID event_id) { |
295 DCHECK(!ignore_progress_events_ || event_id != APPCACHE_PROGRESS_EVENT); | 288 DCHECK(!ignore_progress_events_ || event_id != APPCACHE_PROGRESS_EVENT); |
296 expected_events_.push_back(RaisedEvent(host_ids, event_id)); | 289 expected_events_.push_back(RaisedEvent(host_ids, event_id)); |
297 } | 290 } |
298 | 291 |
299 void SetIgnoreProgressEvents(bool ignore) { | 292 void SetIgnoreProgressEvents(bool ignore) { |
300 // Some tests involve joining new hosts to an already running update job | 293 // Some tests involve joining new hosts to an already running update job |
301 // or intentionally failing. The timing and sequencing of the progress | 294 // or intentionally failing. The timing and sequencing of the progress |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 | 331 |
339 // Add ability for frontend to add master entries to an inprogress update. | 332 // Add ability for frontend to add master entries to an inprogress update. |
340 AppCacheEventID start_update_trigger_; | 333 AppCacheEventID start_update_trigger_; |
341 AppCacheUpdateJob* update_; | 334 AppCacheUpdateJob* update_; |
342 std::vector<AppCacheHost*> update_hosts_; | 335 std::vector<AppCacheHost*> update_hosts_; |
343 }; | 336 }; |
344 | 337 |
345 // Helper factories to simulate redirected URL responses for tests. | 338 // Helper factories to simulate redirected URL responses for tests. |
346 class RedirectFactory : public net::URLRequestJobFactory::ProtocolHandler { | 339 class RedirectFactory : public net::URLRequestJobFactory::ProtocolHandler { |
347 public: | 340 public: |
348 virtual net::URLRequestJob* MaybeCreateJob( | 341 net::URLRequestJob* MaybeCreateJob( |
349 net::URLRequest* request, | 342 net::URLRequest* request, |
350 net::NetworkDelegate* network_delegate) const override { | 343 net::NetworkDelegate* network_delegate) const override { |
351 return new net::URLRequestTestJob( | 344 return new net::URLRequestTestJob( |
352 request, | 345 request, |
353 network_delegate, | 346 network_delegate, |
354 net::URLRequestTestJob::test_redirect_headers(), | 347 net::URLRequestTestJob::test_redirect_headers(), |
355 net::URLRequestTestJob::test_data_1(), | 348 net::URLRequestTestJob::test_data_1(), |
356 true); | 349 true); |
357 } | 350 } |
358 }; | 351 }; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 return new RetryRequestTestJob( | 385 return new RetryRequestTestJob( |
393 request, network_delegate, RetryRequestTestJob::retry_headers(), 503); | 386 request, network_delegate, RetryRequestTestJob::retry_headers(), 503); |
394 } else { | 387 } else { |
395 return new RetryRequestTestJob( | 388 return new RetryRequestTestJob( |
396 request, | 389 request, |
397 network_delegate, | 390 network_delegate, |
398 RetryRequestTestJob::manifest_headers(), 200); | 391 RetryRequestTestJob::manifest_headers(), 200); |
399 } | 392 } |
400 } | 393 } |
401 | 394 |
402 virtual int GetResponseCode() const override { return response_code_; } | 395 int GetResponseCode() const override { return response_code_; } |
403 | 396 |
404 private: | 397 private: |
405 virtual ~RetryRequestTestJob() {} | 398 ~RetryRequestTestJob() override {} |
406 | 399 |
407 static std::string retry_headers() { | 400 static std::string retry_headers() { |
408 const char no_retry_after[] = | 401 const char no_retry_after[] = |
409 "HTTP/1.1 503 BOO HOO\0" | 402 "HTTP/1.1 503 BOO HOO\0" |
410 "\0"; | 403 "\0"; |
411 const char nonzero[] = | 404 const char nonzero[] = |
412 "HTTP/1.1 503 BOO HOO\0" | 405 "HTTP/1.1 503 BOO HOO\0" |
413 "Retry-After: 60\0" | 406 "Retry-After: 60\0" |
414 "\0"; | 407 "\0"; |
415 const char retry_after_0[] = | 408 const char retry_after_0[] = |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 | 447 |
455 static int num_requests_; | 448 static int num_requests_; |
456 static int num_retries_; | 449 static int num_retries_; |
457 static RetryHeader retry_after_; | 450 static RetryHeader retry_after_; |
458 static int expected_requests_; | 451 static int expected_requests_; |
459 }; | 452 }; |
460 | 453 |
461 class RetryRequestTestJobFactory | 454 class RetryRequestTestJobFactory |
462 : public net::URLRequestJobFactory::ProtocolHandler { | 455 : public net::URLRequestJobFactory::ProtocolHandler { |
463 public: | 456 public: |
464 virtual net::URLRequestJob* MaybeCreateJob( | 457 net::URLRequestJob* MaybeCreateJob( |
465 net::URLRequest* request, | 458 net::URLRequest* request, |
466 net::NetworkDelegate* network_delegate) const override { | 459 net::NetworkDelegate* network_delegate) const override { |
467 return RetryRequestTestJob::RetryFactory(request, network_delegate); | 460 return RetryRequestTestJob::RetryFactory(request, network_delegate); |
468 } | 461 } |
469 }; | 462 }; |
470 | 463 |
471 // static | 464 // static |
472 const GURL RetryRequestTestJob::kRetryUrl("http://retry"); | 465 const GURL RetryRequestTestJob::kRetryUrl("http://retry"); |
473 int RetryRequestTestJob::num_requests_ = 0; | 466 int RetryRequestTestJob::num_requests_ = 0; |
474 int RetryRequestTestJob::num_retries_; | 467 int RetryRequestTestJob::num_retries_; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 | 507 |
515 saw_if_none_match_ = | 508 saw_if_none_match_ = |
516 extra_headers.GetHeader( | 509 extra_headers.GetHeader( |
517 net::HttpRequestHeaders::kIfNoneMatch, &header_value) && | 510 net::HttpRequestHeaders::kIfNoneMatch, &header_value) && |
518 header_value == expect_if_none_match_; | 511 header_value == expect_if_none_match_; |
519 } | 512 } |
520 return MockHttpServer::JobFactory(request, network_delegate); | 513 return MockHttpServer::JobFactory(request, network_delegate); |
521 } | 514 } |
522 | 515 |
523 protected: | 516 protected: |
524 virtual ~HttpHeadersRequestTestJob() {} | 517 ~HttpHeadersRequestTestJob() override {} |
525 | 518 |
526 private: | 519 private: |
527 static std::string expect_if_modified_since_; | 520 static std::string expect_if_modified_since_; |
528 static bool saw_if_modified_since_; | 521 static bool saw_if_modified_since_; |
529 static std::string expect_if_none_match_; | 522 static std::string expect_if_none_match_; |
530 static bool saw_if_none_match_; | 523 static bool saw_if_none_match_; |
531 static bool already_checked_; | 524 static bool already_checked_; |
532 }; | 525 }; |
533 | 526 |
534 // static | 527 // static |
535 std::string HttpHeadersRequestTestJob::expect_if_modified_since_; | 528 std::string HttpHeadersRequestTestJob::expect_if_modified_since_; |
536 bool HttpHeadersRequestTestJob::saw_if_modified_since_ = false; | 529 bool HttpHeadersRequestTestJob::saw_if_modified_since_ = false; |
537 std::string HttpHeadersRequestTestJob::expect_if_none_match_; | 530 std::string HttpHeadersRequestTestJob::expect_if_none_match_; |
538 bool HttpHeadersRequestTestJob::saw_if_none_match_ = false; | 531 bool HttpHeadersRequestTestJob::saw_if_none_match_ = false; |
539 bool HttpHeadersRequestTestJob::already_checked_ = false; | 532 bool HttpHeadersRequestTestJob::already_checked_ = false; |
540 | 533 |
541 class IfModifiedSinceJobFactory | 534 class IfModifiedSinceJobFactory |
542 : public net::URLRequestJobFactory::ProtocolHandler { | 535 : public net::URLRequestJobFactory::ProtocolHandler { |
543 public: | 536 public: |
544 virtual net::URLRequestJob* MaybeCreateJob( | 537 net::URLRequestJob* MaybeCreateJob( |
545 net::URLRequest* request, | 538 net::URLRequest* request, |
546 net::NetworkDelegate* network_delegate) const override { | 539 net::NetworkDelegate* network_delegate) const override { |
547 return HttpHeadersRequestTestJob::IfModifiedSinceFactory( | 540 return HttpHeadersRequestTestJob::IfModifiedSinceFactory( |
548 request, network_delegate); | 541 request, network_delegate); |
549 } | 542 } |
550 }; | 543 }; |
551 | 544 |
552 class IOThread : public base::Thread { | 545 class IOThread : public base::Thread { |
553 public: | 546 public: |
554 explicit IOThread(const char* name) | 547 explicit IOThread(const char* name) |
555 : base::Thread(name) { | 548 : base::Thread(name) { |
556 } | 549 } |
557 | 550 |
558 virtual ~IOThread() { | 551 ~IOThread() override { Stop(); } |
559 Stop(); | |
560 } | |
561 | 552 |
562 net::URLRequestContext* request_context() { | 553 net::URLRequestContext* request_context() { |
563 return request_context_.get(); | 554 return request_context_.get(); |
564 } | 555 } |
565 | 556 |
566 void SetNewJobFactory(net::URLRequestJobFactory* job_factory) { | 557 void SetNewJobFactory(net::URLRequestJobFactory* job_factory) { |
567 DCHECK(job_factory); | 558 DCHECK(job_factory); |
568 job_factory_.reset(job_factory); | 559 job_factory_.reset(job_factory); |
569 request_context_->set_job_factory(job_factory_.get()); | 560 request_context_->set_job_factory(job_factory_.get()); |
570 } | 561 } |
571 | 562 |
572 virtual void Init() override { | 563 void Init() override { |
573 scoped_ptr<net::URLRequestJobFactoryImpl> factory( | 564 scoped_ptr<net::URLRequestJobFactoryImpl> factory( |
574 new net::URLRequestJobFactoryImpl()); | 565 new net::URLRequestJobFactoryImpl()); |
575 factory->SetProtocolHandler("http", new MockHttpServerJobFactory); | 566 factory->SetProtocolHandler("http", new MockHttpServerJobFactory); |
576 factory->SetProtocolHandler("https", new MockHttpServerJobFactory); | 567 factory->SetProtocolHandler("https", new MockHttpServerJobFactory); |
577 job_factory_ = factory.Pass(); | 568 job_factory_ = factory.Pass(); |
578 request_context_.reset(new net::TestURLRequestContext()); | 569 request_context_.reset(new net::TestURLRequestContext()); |
579 request_context_->set_job_factory(job_factory_.get()); | 570 request_context_->set_job_factory(job_factory_.get()); |
580 } | 571 } |
581 | 572 |
582 virtual void CleanUp() override { | 573 void CleanUp() override { |
583 request_context_.reset(); | 574 request_context_.reset(); |
584 job_factory_.reset(); | 575 job_factory_.reset(); |
585 } | 576 } |
586 | 577 |
587 private: | 578 private: |
588 scoped_ptr<net::URLRequestJobFactory> job_factory_; | 579 scoped_ptr<net::URLRequestJobFactory> job_factory_; |
589 scoped_ptr<net::URLRequestContext> request_context_; | 580 scoped_ptr<net::URLRequestContext> request_context_; |
590 }; | 581 }; |
591 | 582 |
592 class AppCacheUpdateJobTest : public testing::Test, | 583 class AppCacheUpdateJobTest : public testing::Test, |
(...skipping 2417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3010 WaitForUpdateToFinish(); | 3001 WaitForUpdateToFinish(); |
3011 } | 3002 } |
3012 | 3003 |
3013 void WaitForUpdateToFinish() { | 3004 void WaitForUpdateToFinish() { |
3014 if (group_->update_status() == AppCacheGroup::IDLE) | 3005 if (group_->update_status() == AppCacheGroup::IDLE) |
3015 UpdateFinished(); | 3006 UpdateFinished(); |
3016 else | 3007 else |
3017 group_->AddUpdateObserver(this); | 3008 group_->AddUpdateObserver(this); |
3018 } | 3009 } |
3019 | 3010 |
3020 virtual void OnUpdateComplete(AppCacheGroup* group) override { | 3011 void OnUpdateComplete(AppCacheGroup* group) override { |
3021 ASSERT_EQ(group_.get(), group); | 3012 ASSERT_EQ(group_.get(), group); |
3022 protect_newest_cache_ = group->newest_complete_cache(); | 3013 protect_newest_cache_ = group->newest_complete_cache(); |
3023 UpdateFinished(); | 3014 UpdateFinished(); |
3024 } | 3015 } |
3025 | 3016 |
3026 void UpdateFinished() { | 3017 void UpdateFinished() { |
3027 // We unwind the stack prior to finishing up to let stack-based objects | 3018 // We unwind the stack prior to finishing up to let stack-based objects |
3028 // get deleted. | 3019 // get deleted. |
3029 base::MessageLoop::current()->PostTask( | 3020 base::MessageLoop::current()->PostTask( |
3030 FROM_HERE, | 3021 FROM_HERE, |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3687 | 3678 |
3688 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsSuccess) { | 3679 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsSuccess) { |
3689 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsSuccessTest); | 3680 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsSuccessTest); |
3690 } | 3681 } |
3691 | 3682 |
3692 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) { | 3683 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) { |
3693 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest); | 3684 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest); |
3694 } | 3685 } |
3695 | 3686 |
3696 } // namespace content | 3687 } // namespace content |
OLD | NEW |