Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: webkit/appcache/appcache_update_job_unittest.cc

Issue 7720022: Third-party appcache blocking. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Moving checks. Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 6
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/threading/thread.h" 8 #include "base/threading/thread.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
11 #include "net/http/http_response_headers.h" 11 #include "net/http/http_response_headers.h"
12 #include "net/url_request/url_request_error_job.h" 12 #include "net/url_request/url_request_error_job.h"
13 #include "net/url_request/url_request_job_factory.h" 13 #include "net/url_request/url_request_job_factory.h"
14 #include "net/url_request/url_request_test_job.h" 14 #include "net/url_request/url_request_test_job.h"
15 #include "net/url_request/url_request_test_util.h" 15 #include "net/url_request/url_request_test_util.h"
16 #include "webkit/appcache/appcache_group.h" 16 #include "webkit/appcache/appcache_group.h"
17 #include "webkit/appcache/appcache_host.h" 17 #include "webkit/appcache/appcache_host.h"
18 #include "webkit/appcache/appcache_policy.h"
19 #include "webkit/appcache/appcache_response.h" 18 #include "webkit/appcache/appcache_response.h"
20 #include "webkit/appcache/appcache_update_job.h" 19 #include "webkit/appcache/appcache_update_job.h"
21 #include "webkit/appcache/mock_appcache_service.h" 20 #include "webkit/appcache/mock_appcache_service.h"
22 21
23 namespace appcache { 22 namespace appcache {
24 class AppCacheUpdateJobTest; 23 class AppCacheUpdateJobTest;
25 24
26 namespace { 25 namespace {
27 26
28 const char kManifest1Contents[] = 27 const char kManifest1Contents[] =
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 } 543 }
545 544
546 private: 545 private:
547 scoped_ptr<net::URLRequestJobFactory> job_factory_; 546 scoped_ptr<net::URLRequestJobFactory> job_factory_;
548 scoped_refptr<net::URLRequestContext> request_context_; 547 scoped_refptr<net::URLRequestContext> request_context_;
549 }; 548 };
550 549
551 class AppCacheUpdateJobTest : public testing::Test, 550 class AppCacheUpdateJobTest : public testing::Test,
552 public AppCacheGroup::UpdateObserver { 551 public AppCacheGroup::UpdateObserver {
553 public: 552 public:
554 class MockAppCachePolicy : public AppCachePolicy {
555 public:
556 MockAppCachePolicy()
557 : can_create_return_value_(net::OK), return_immediately_(true),
558 callback_(NULL) {
559 }
560
561 virtual bool CanLoadAppCache(const GURL& manifest_url) {
562 return true;
563 }
564
565 virtual int CanCreateAppCache(const GURL& manifest_url,
566 net::CompletionCallback* callback) {
567 requested_manifest_url_ = manifest_url;
568 callback_ = callback;
569 if (return_immediately_)
570 return can_create_return_value_;
571
572 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
573 this, &MockAppCachePolicy::CompleteCanCreateAppCache));
574 return net::ERR_IO_PENDING;
575 }
576
577 void CompleteCanCreateAppCache() {
578 callback_->Run(can_create_return_value_);
579 }
580
581 int can_create_return_value_;
582 bool return_immediately_;
583 GURL requested_manifest_url_;
584 net::CompletionCallback* callback_;
585 };
586
587
588 AppCacheUpdateJobTest() 553 AppCacheUpdateJobTest()
589 : do_checks_after_update_finished_(false), 554 : do_checks_after_update_finished_(false),
590 expect_group_obsolete_(false), 555 expect_group_obsolete_(false),
591 expect_group_has_cache_(false), 556 expect_group_has_cache_(false),
592 expect_old_cache_(NULL), 557 expect_old_cache_(NULL),
593 expect_newest_cache_(NULL), 558 expect_newest_cache_(NULL),
594 expect_non_null_update_time_(false), 559 expect_non_null_update_time_(false),
595 tested_manifest_(NONE), 560 tested_manifest_(NONE),
596 tested_manifest_path_override_(NULL) { 561 tested_manifest_path_override_(NULL) {
597 io_thread_.reset(new IOThread("AppCacheUpdateJob IO test thread")); 562 io_thread_.reset(new IOThread("AppCacheUpdateJob IO test thread"));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 EXPECT_EQ(expected, events.size()); 602 EXPECT_EQ(expected, events.size());
638 EXPECT_EQ(expected, events[0].first.size()); 603 EXPECT_EQ(expected, events[0].first.size());
639 EXPECT_EQ(host.host_id(), events[0].first[0]); 604 EXPECT_EQ(host.host_id(), events[0].first[0]);
640 EXPECT_EQ(CHECKING_EVENT, events[0].second); 605 EXPECT_EQ(CHECKING_EVENT, events[0].second);
641 606
642 // Abort as we're not testing actual URL fetches in this test. 607 // Abort as we're not testing actual URL fetches in this test.
643 delete update; 608 delete update;
644 UpdateFinished(); 609 UpdateFinished();
645 } 610 }
646 611
647 void ImmediatelyBlockCacheAttemptTest() {
648 BlockCacheAttemptTest(true);
649 }
650
651 void DelayedBlockCacheAttemptTest() {
652 BlockCacheAttemptTest(false);
653 }
654
655 void BlockCacheAttemptTest(bool immediately) {
656 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
657
658 GURL manifest_url = GURL("http://failme");
659
660 // Setup to block the cache attempt immediately.
661 policy_.return_immediately_ = immediately;
662 policy_.can_create_return_value_ = net::ERR_ACCESS_DENIED;
663
664 MakeService();
665 group_ = new AppCacheGroup(service_.get(), manifest_url,
666 service_->storage()->NewGroupId());
667
668 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_);
669 group_->update_job_ = update;
670
671 MockFrontend mock_frontend;
672 AppCacheHost host(1, &mock_frontend, service_.get());
673
674 update->StartUpdate(&host, GURL());
675 EXPECT_EQ(manifest_url, policy_.requested_manifest_url_);
676
677 // Verify state.
678 EXPECT_EQ(AppCacheUpdateJob::CACHE_ATTEMPT, update->update_type_);
679 EXPECT_EQ(AppCacheUpdateJob::FETCH_MANIFEST, update->internal_state_);
680 EXPECT_EQ(AppCacheGroup::CHECKING, group_->update_status());
681
682 // Verify notifications.
683 MockFrontend::RaisedEvents& events = mock_frontend.raised_events_;
684 size_t expected = 1;
685 EXPECT_EQ(expected, events.size());
686 EXPECT_EQ(1U, events[0].first.size());
687 EXPECT_EQ(host.host_id(), events[0].first[0]);
688 EXPECT_EQ(CHECKING_EVENT, events[0].second);
689
690 WaitForUpdateToFinish();
691 }
692
693 void StartUpgradeAttemptTest() { 612 void StartUpgradeAttemptTest() {
694 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); 613 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
695 614
696 { 615 {
697 MakeService(); 616 MakeService();
698 group_ = new AppCacheGroup(service_.get(), GURL("http://failme"), 617 group_ = new AppCacheGroup(service_.get(), GURL("http://failme"),
699 service_->storage()->NewGroupId()); 618 service_->storage()->NewGroupId());
700 619
701 // Give the group some existing caches. 620 // Give the group some existing caches.
702 AppCache* cache1 = MakeCacheForGroup(1, 111); 621 AppCache* cache1 = MakeCacheForGroup(1, 111);
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 EXPECT_TRUE(update->manifest_fetcher_ != NULL); 970 EXPECT_TRUE(update->manifest_fetcher_ != NULL);
1052 971
1053 WaitForUpdateToFinish(); 972 WaitForUpdateToFinish();
1054 } 973 }
1055 974
1056 void BasicCacheAttemptSuccessTest() { 975 void BasicCacheAttemptSuccessTest() {
1057 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); 976 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
1058 977
1059 GURL manifest_url = MockHttpServer::GetMockUrl("files/manifest1"); 978 GURL manifest_url = MockHttpServer::GetMockUrl("files/manifest1");
1060 979
1061 // We also test the async AppCachePolicy return path in this test case.
1062 policy_.return_immediately_ = false;
1063 policy_.can_create_return_value_ = net::OK;
1064
1065 MakeService(); 980 MakeService();
1066 group_ = new AppCacheGroup( 981 group_ = new AppCacheGroup(
1067 service_.get(), manifest_url, 982 service_.get(), manifest_url,
1068 service_->storage()->NewGroupId()); 983 service_->storage()->NewGroupId());
1069 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_); 984 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_);
1070 group_->update_job_ = update; 985 group_->update_job_ = update;
1071 986
1072 MockFrontend* frontend = MakeMockFrontend(); 987 MockFrontend* frontend = MakeMockFrontend();
1073 AppCacheHost* host = MakeHost(1, frontend); 988 AppCacheHost* host = MakeHost(1, frontend);
1074 update->StartUpdate(host, GURL()); 989 update->StartUpdate(host, GURL());
1075 EXPECT_EQ(manifest_url, policy_.requested_manifest_url_);;
1076 990
1077 // Set up checks for when update job finishes. 991 // Set up checks for when update job finishes.
1078 do_checks_after_update_finished_ = true; 992 do_checks_after_update_finished_ = true;
1079 expect_group_obsolete_ = false; 993 expect_group_obsolete_ = false;
1080 expect_group_has_cache_ = true; 994 expect_group_has_cache_ = true;
1081 tested_manifest_ = MANIFEST1; 995 tested_manifest_ = MANIFEST1;
1082 frontend->AddExpectedEvent(MockFrontend::HostIds(1, host->host_id()), 996 frontend->AddExpectedEvent(MockFrontend::HostIds(1, host->host_id()),
1083 CHECKING_EVENT); 997 CHECKING_EVENT);
1084 998
1085 WaitForUpdateToFinish(); 999 WaitForUpdateToFinish();
(...skipping 1750 matching lines...) Expand 10 before | Expand all | Expand 10 after
2836 MakeService(); 2750 MakeService();
2837 group_ = new AppCacheGroup( 2751 group_ = new AppCacheGroup(
2838 service_.get(), manifest_url, 2752 service_.get(), manifest_url,
2839 service_->storage()->NewGroupId()); 2753 service_->storage()->NewGroupId());
2840 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_); 2754 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_);
2841 group_->update_job_ = update; 2755 group_->update_job_ = update;
2842 2756
2843 MockFrontend* frontend = MakeMockFrontend(); 2757 MockFrontend* frontend = MakeMockFrontend();
2844 AppCacheHost* host = MakeHost(1, frontend); 2758 AppCacheHost* host = MakeHost(1, frontend);
2845 update->StartUpdate(host, GURL()); 2759 update->StartUpdate(host, GURL());
2846 EXPECT_EQ(manifest_url, policy_.requested_manifest_url_);
2847 2760
2848 // Set up checks for when update job finishes. 2761 // Set up checks for when update job finishes.
2849 do_checks_after_update_finished_ = true; 2762 do_checks_after_update_finished_ = true;
2850 expect_group_obsolete_ = false; 2763 expect_group_obsolete_ = false;
2851 expect_group_has_cache_ = true; 2764 expect_group_has_cache_ = true;
2852 tested_manifest_ = NONE; 2765 tested_manifest_ = NONE;
2853 MockFrontend::HostIds host_ids(1, host->host_id()); 2766 MockFrontend::HostIds host_ids(1, host->host_id());
2854 frontend->AddExpectedEvent(host_ids, CHECKING_EVENT); 2767 frontend->AddExpectedEvent(host_ids, CHECKING_EVENT);
2855 2768
2856 WaitForUpdateToFinish(); 2769 WaitForUpdateToFinish();
2857 } 2770 }
2858 2771
2859 void CrossOriginHttpsDeniedTest() { 2772 void CrossOriginHttpsDeniedTest() {
2860 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); 2773 ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
2861 2774
2862 GURL manifest_url = MockHttpServer::GetMockHttpsUrl( 2775 GURL manifest_url = MockHttpServer::GetMockHttpsUrl(
2863 "files/invalid_cross_origin_https_manifest"); 2776 "files/invalid_cross_origin_https_manifest");
2864 2777
2865 MakeService(); 2778 MakeService();
2866 group_ = new AppCacheGroup( 2779 group_ = new AppCacheGroup(
2867 service_.get(), manifest_url, 2780 service_.get(), manifest_url,
2868 service_->storage()->NewGroupId()); 2781 service_->storage()->NewGroupId());
2869 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_); 2782 AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_);
2870 group_->update_job_ = update; 2783 group_->update_job_ = update;
2871 2784
2872 MockFrontend* frontend = MakeMockFrontend(); 2785 MockFrontend* frontend = MakeMockFrontend();
2873 AppCacheHost* host = MakeHost(1, frontend); 2786 AppCacheHost* host = MakeHost(1, frontend);
2874 update->StartUpdate(host, GURL()); 2787 update->StartUpdate(host, GURL());
2875 EXPECT_EQ(manifest_url, policy_.requested_manifest_url_);
2876 2788
2877 // Set up checks for when update job finishes. 2789 // Set up checks for when update job finishes.
2878 do_checks_after_update_finished_ = true; 2790 do_checks_after_update_finished_ = true;
2879 expect_group_obsolete_ = false; 2791 expect_group_obsolete_ = false;
2880 expect_group_has_cache_ = false; 2792 expect_group_has_cache_ = false;
2881 tested_manifest_ = NONE; 2793 tested_manifest_ = NONE;
2882 MockFrontend::HostIds host_ids(1, host->host_id()); 2794 MockFrontend::HostIds host_ids(1, host->host_id());
2883 frontend->AddExpectedEvent(host_ids, CHECKING_EVENT); 2795 frontend->AddExpectedEvent(host_ids, CHECKING_EVENT);
2884 2796
2885 WaitForUpdateToFinish(); 2797 WaitForUpdateToFinish();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2921 STLDeleteContainerPointers(frontends_.begin(), frontends_.end()); 2833 STLDeleteContainerPointers(frontends_.begin(), frontends_.end());
2922 response_infos_.clear(); 2834 response_infos_.clear();
2923 service_.reset(NULL); 2835 service_.reset(NULL);
2924 2836
2925 event_->Signal(); 2837 event_->Signal();
2926 } 2838 }
2927 2839
2928 void MakeService() { 2840 void MakeService() {
2929 service_.reset(new MockAppCacheService()); 2841 service_.reset(new MockAppCacheService());
2930 service_->set_request_context(io_thread_->request_context()); 2842 service_->set_request_context(io_thread_->request_context());
2931 service_->set_appcache_policy(&policy_);
2932 } 2843 }
2933 2844
2934 AppCache* MakeCacheForGroup(int64 cache_id, int64 manifest_response_id) { 2845 AppCache* MakeCacheForGroup(int64 cache_id, int64 manifest_response_id) {
2935 AppCache* cache = new AppCache(service_.get(), cache_id); 2846 AppCache* cache = new AppCache(service_.get(), cache_id);
2936 cache->set_complete(true); 2847 cache->set_complete(true);
2937 cache->set_update_time(base::Time::Now()); 2848 cache->set_update_time(base::Time::Now());
2938 group_->AddCache(cache); 2849 group_->AddCache(cache);
2939 2850
2940 // Add manifest entry to cache. 2851 // Add manifest entry to cache.
2941 cache->AddEntry(group_->manifest_url(), 2852 cache->AddEntry(group_->manifest_url(),
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
3234 EMPTY_FILE_MANIFEST, 3145 EMPTY_FILE_MANIFEST,
3235 PENDING_MASTER_NO_UPDATE, 3146 PENDING_MASTER_NO_UPDATE,
3236 }; 3147 };
3237 3148
3238 scoped_ptr<IOThread> io_thread_; 3149 scoped_ptr<IOThread> io_thread_;
3239 3150
3240 scoped_ptr<MockAppCacheService> service_; 3151 scoped_ptr<MockAppCacheService> service_;
3241 scoped_refptr<AppCacheGroup> group_; 3152 scoped_refptr<AppCacheGroup> group_;
3242 scoped_refptr<AppCache> protect_newest_cache_; 3153 scoped_refptr<AppCache> protect_newest_cache_;
3243 scoped_ptr<base::WaitableEvent> event_; 3154 scoped_ptr<base::WaitableEvent> event_;
3244 MockAppCachePolicy policy_;
3245 3155
3246 scoped_ptr<AppCacheResponseWriter> response_writer_; 3156 scoped_ptr<AppCacheResponseWriter> response_writer_;
3247 scoped_ptr<net::CompletionCallbackImpl<AppCacheUpdateJobTest> > 3157 scoped_ptr<net::CompletionCallbackImpl<AppCacheUpdateJobTest> >
3248 write_callback_; 3158 write_callback_;
3249 3159
3250 // Hosts used by an async test that need to live until update job finishes. 3160 // Hosts used by an async test that need to live until update job finishes.
3251 // Otherwise, test can put host on the stack instead of here. 3161 // Otherwise, test can put host on the stack instead of here.
3252 std::vector<AppCacheHost*> hosts_; 3162 std::vector<AppCacheHost*> hosts_;
3253 3163
3254 // Response infos used by an async test that need to live until update job 3164 // Response infos used by an async test that need to live until update job
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
3328 EXPECT_EQ(host.host_id(), events[1].first[0]); 3238 EXPECT_EQ(host.host_id(), events[1].first[0]);
3329 EXPECT_EQ(appcache::DOWNLOADING_EVENT, events[1].second); 3239 EXPECT_EQ(appcache::DOWNLOADING_EVENT, events[1].second);
3330 3240
3331 EXPECT_EQ(AppCacheGroup::DOWNLOADING, group->update_status()); 3241 EXPECT_EQ(AppCacheGroup::DOWNLOADING, group->update_status());
3332 } 3242 }
3333 3243
3334 TEST_F(AppCacheUpdateJobTest, StartCacheAttempt) { 3244 TEST_F(AppCacheUpdateJobTest, StartCacheAttempt) {
3335 RunTestOnIOThread(&AppCacheUpdateJobTest::StartCacheAttemptTest); 3245 RunTestOnIOThread(&AppCacheUpdateJobTest::StartCacheAttemptTest);
3336 } 3246 }
3337 3247
3338 TEST_F(AppCacheUpdateJobTest, ImmediatelyBlockCacheAttemptTest) {
michaeln 2011/08/31 23:42:08 Since these 'blocking' functions are now being per
marja 2011/09/05 10:42:24 Done. (Added tests for AppCacheHost and AppCacheRe
3339 RunTestOnIOThread(&AppCacheUpdateJobTest::ImmediatelyBlockCacheAttemptTest);
3340 }
3341
3342 TEST_F(AppCacheUpdateJobTest, DelayedBlockCacheAttemptTest) {
3343 RunTestOnIOThread(&AppCacheUpdateJobTest::DelayedBlockCacheAttemptTest);
3344 }
3345
3346 TEST_F(AppCacheUpdateJobTest, StartUpgradeAttempt) { 3248 TEST_F(AppCacheUpdateJobTest, StartUpgradeAttempt) {
3347 RunTestOnIOThread(&AppCacheUpdateJobTest::StartUpgradeAttemptTest); 3249 RunTestOnIOThread(&AppCacheUpdateJobTest::StartUpgradeAttemptTest);
3348 } 3250 }
3349 3251
3350 TEST_F(AppCacheUpdateJobTest, CacheAttemptFetchManifestFail) { 3252 TEST_F(AppCacheUpdateJobTest, CacheAttemptFetchManifestFail) {
3351 RunTestOnIOThread(&AppCacheUpdateJobTest::CacheAttemptFetchManifestFailTest); 3253 RunTestOnIOThread(&AppCacheUpdateJobTest::CacheAttemptFetchManifestFailTest);
3352 } 3254 }
3353 3255
3354 TEST_F(AppCacheUpdateJobTest, UpgradeFetchManifestFail) { 3256 TEST_F(AppCacheUpdateJobTest, UpgradeFetchManifestFail) {
3355 RunTestOnIOThread(&AppCacheUpdateJobTest::UpgradeFetchManifestFailTest); 3257 RunTestOnIOThread(&AppCacheUpdateJobTest::UpgradeFetchManifestFailTest);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
3543 3445
3544 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) { 3446 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) {
3545 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest); 3447 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest);
3546 } 3448 }
3547 3449
3548 } // namespace appcache 3450 } // namespace appcache
3549 3451
3550 // AppCacheUpdateJobTest is expected to always live longer than the 3452 // AppCacheUpdateJobTest is expected to always live longer than the
3551 // runnable methods. This lets us call NewRunnableMethod on its instances. 3453 // runnable methods. This lets us call NewRunnableMethod on its instances.
3552 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheUpdateJobTest); 3454 DISABLE_RUNNABLE_METHOD_REFCOUNT(appcache::AppCacheUpdateJobTest);
3553 DISABLE_RUNNABLE_METHOD_REFCOUNT(
3554 appcache::AppCacheUpdateJobTest::MockAppCachePolicy);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698