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

Side by Side Diff: chrome/browser/push_messaging/push_messaging_browsertest.cc

Issue 2487233002: Handle push resubscribes with no sender info (avoids DCHECK) (Closed)
Patch Set: Handle push resubscribes with no sender info (avoids DCHECK) Created 4 years, 1 month 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
« no previous file with comments | « no previous file | chrome/test/data/push_messaging/push_test.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 180
181 InProcessBrowserTest::TearDown(); 181 InProcessBrowserTest::TearDown();
182 } 182 }
183 183
184 void LoadTestPage(const std::string& path) { 184 void LoadTestPage(const std::string& path) {
185 ui_test_utils::NavigateToURL(GetBrowser(), https_server_->GetURL(path)); 185 ui_test_utils::NavigateToURL(GetBrowser(), https_server_->GetURL(path));
186 } 186 }
187 187
188 void LoadTestPage() { LoadTestPage(GetTestURL()); } 188 void LoadTestPage() { LoadTestPage(GetTestURL()); }
189 189
190 void LoadTestPageWithoutManifest() { LoadTestPage(GetNoManifestTestURL()); }
191
190 bool RunScript(const std::string& script, std::string* result) { 192 bool RunScript(const std::string& script, std::string* result) {
191 return RunScript(script, result, nullptr); 193 return RunScript(script, result, nullptr);
192 } 194 }
193 195
194 bool RunScript(const std::string& script, std::string* result, 196 bool RunScript(const std::string& script, std::string* result,
195 content::WebContents* web_contents) { 197 content::WebContents* web_contents) {
196 if (!web_contents) 198 if (!web_contents)
197 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents(); 199 web_contents = GetBrowser()->tab_strip_model()->GetActiveWebContents();
198 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(), 200 return content::ExecuteScriptAndExtractString(web_contents->GetMainFrame(),
199 script, result); 201 script, result);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 266
265 void SetSiteEngagementScore(const GURL& url, double score) { 267 void SetSiteEngagementScore(const GURL& url, double score) {
266 SiteEngagementService* service = 268 SiteEngagementService* service =
267 SiteEngagementService::Get(GetBrowser()->profile()); 269 SiteEngagementService::Get(GetBrowser()->profile());
268 service->ResetScoreForURL(url, score); 270 service->ResetScoreForURL(url, score);
269 } 271 }
270 272
271 protected: 273 protected:
272 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } 274 virtual std::string GetTestURL() { return "/push_messaging/test.html"; }
273 275
276 virtual std::string GetNoManifestTestURL() {
277 return "/push_messaging/test_no_manifest.html";
278 }
279
274 virtual Browser* GetBrowser() const { return browser(); } 280 virtual Browser* GetBrowser() const { return browser(); }
275 281
276 gcm::FakeGCMProfileService* gcm_service_; 282 gcm::FakeGCMProfileService* gcm_service_;
277 instance_id::FakeGCMDriverForInstanceID* gcm_driver_; 283 instance_id::FakeGCMDriverForInstanceID* gcm_driver_;
278 base::HistogramTester histogram_tester_; 284 base::HistogramTester histogram_tester_;
279 285
280 private: 286 private:
281 std::unique_ptr<net::EmbeddedTestServer> https_server_; 287 std::unique_ptr<net::EmbeddedTestServer> https_server_;
282 PushMessagingServiceImpl* push_service_; 288 PushMessagingServiceImpl* push_service_;
283 289
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 544
539 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 545 ASSERT_TRUE(RunScript("isControlled()", &script_result));
540 ASSERT_EQ("true - is controlled", script_result); 546 ASSERT_EQ("true - is controlled", script_result);
541 547
542 // Try to subscribe from a worker without a key. This should fail. 548 // Try to subscribe from a worker without a key. This should fail.
543 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 549 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
544 EXPECT_EQ( 550 EXPECT_EQ(
545 "AbortError - Registration failed - missing applicationServerKey, and " 551 "AbortError - Registration failed - missing applicationServerKey, and "
546 "gcm_sender_id not found in manifest", 552 "gcm_sender_id not found in manifest",
547 script_result); 553 script_result);
548 // Now run the subscribe from the service worker with a key. This 554
549 // should succeed, and write the key to the datastore. 555 // Now run the subscribe with a key. This should succeed.
550 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); 556 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
551 std::string token1;
552 ASSERT_NO_FATAL_FAILURE( 557 ASSERT_NO_FATAL_FAILURE(
553 EndpointToToken(script_result, true /* standard_protocol */, &token1)); 558 EndpointToToken(script_result, true /* standard_protocol */));
554
555 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
556 EXPECT_EQ("unsubscribe result: true", script_result);
557 EXPECT_NE(push_service(), GetAppHandler());
558
559 // Now run the subscribe from the service worker without a key.
560 // In this case, the key will be read from the datastore.
561 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
562 std::string token2;
563 ASSERT_NO_FATAL_FAILURE(
564 EndpointToToken(script_result, true /* standard_protocol */, &token2));
565 EXPECT_NE(token1, token2);
566 559
567 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 560 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
568 EXPECT_EQ("unsubscribe result: true", script_result); 561 EXPECT_EQ("unsubscribe result: true", script_result);
569 EXPECT_NE(push_service(), GetAppHandler()); 562 EXPECT_NE(push_service(), GetAppHandler());
570 } 563 }
571 564
572 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribeWorkerUsingManifest) { 565 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
566 ResubscribeWithoutKeyAfterSubscribingWithKeyInManifest) {
573 std::string script_result; 567 std::string script_result;
574 568
575 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 569 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
570 ASSERT_EQ("ok - service worker registered", script_result);
571
572 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
573
574 LoadTestPage(); // Reload to become controlled.
575
576 ASSERT_TRUE(RunScript("isControlled()", &script_result));
577 ASSERT_EQ("true - is controlled", script_result);
578
579 // Run the subscription from the document without a key, this will trigger
580 // the code to read sender id from the manifest and will write it to the
581 // datastore.
582 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
583 std::string token1;
584 ASSERT_NO_FATAL_FAILURE(
585 EndpointToToken(script_result, false /* standard_protocol */, &token1));
586
587 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
588 ASSERT_EQ("manifest removed", script_result);
589
590 // Try to resubscribe from the document without a key or manifest.
591 // This should fail.
592 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
593 EXPECT_EQ(
594 "AbortError - Registration failed - missing applicationServerKey, "
595 "and manifest empty or missing",
596 script_result);
597
598 // Now run the subscribe from the service worker without a key.
599 // In this case, the sender id should be read from the datastore.
600 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
601 std::string token2;
602 ASSERT_NO_FATAL_FAILURE(
603 EndpointToToken(script_result, false /* standard_protocol */, &token2));
604 EXPECT_EQ(token1, token2);
605
606 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
607 EXPECT_EQ("unsubscribe result: true", script_result);
608 EXPECT_NE(push_service(), GetAppHandler());
609
610 // After unsubscribing, subscribe again from the worker with no key.
611 // The sender id should again be read from the datastore, so the
612 // subcribe should succeed, and we should get a new subscription token.
613 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
614 std::string token3;
615 ASSERT_NO_FATAL_FAILURE(
616 EndpointToToken(script_result, false /* standard_protocol */, &token3));
617 EXPECT_NE(token1, token3);
618
619 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
620 EXPECT_EQ("unsubscribe result: true", script_result);
621 EXPECT_NE(push_service(), GetAppHandler());
622 }
623
624 IN_PROC_BROWSER_TEST_F(
625 PushMessagingBrowserTest,
626 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithP256Key) {
627 std::string script_result;
628
629 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
576 ASSERT_EQ("ok - service worker registered", script_result); 630 ASSERT_EQ("ok - service worker registered", script_result);
577 631
578 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); 632 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
579 633
580 LoadTestPage(); // Reload to become controlled. 634 LoadTestPageWithoutManifest(); // Reload to become controlled.
581 635
582 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 636 ASSERT_TRUE(RunScript("isControlled()", &script_result));
583 ASSERT_EQ("true - is controlled", script_result); 637 ASSERT_EQ("true - is controlled", script_result);
584 638
585 // Try to subscribe from a worker without a key. This should fail. 639 // Run the subscription from the document with a key.
640 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
641 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result));
642
643 // Try to resubscribe from the document without a key - should fail.
644 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
645 EXPECT_EQ(
646 "AbortError - Registration failed - missing applicationServerKey, "
647 "and manifest empty or missing",
648 script_result);
649
650 // Now try to resubscribe from the service worker without a key.
651 // This should also fail as the original key was not numeric.
652 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
653 EXPECT_EQ(
654 "AbortError - Registration failed - missing applicationServerKey, "
655 "and gcm_sender_id not found in manifest",
656 script_result);
657
658 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
659 EXPECT_EQ("unsubscribe result: true", script_result);
660 EXPECT_NE(push_service(), GetAppHandler());
661
662 // After unsubscribing, try to resubscribe again without a key.
663 // This should again fail.
664 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
665 EXPECT_EQ(
666 "AbortError - Registration failed - missing applicationServerKey, "
667 "and gcm_sender_id not found in manifest",
668 script_result);
669 }
670
671 IN_PROC_BROWSER_TEST_F(
672 PushMessagingBrowserTest,
673 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithP256Key) {
674 std::string script_result;
675
676 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
677 ASSERT_EQ("ok - service worker registered", script_result);
678
679 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
680
681 LoadTestPageWithoutManifest(); // Reload to become controlled.
682
683 ASSERT_TRUE(RunScript("isControlled()", &script_result));
684 ASSERT_EQ("true - is controlled", script_result);
685
686 // Run the subscribe from the service worker with a key.
687 // This should succeed.
688 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
689 ASSERT_NO_FATAL_FAILURE(
690 EndpointToToken(script_result, true /* standard_protocol */));
691
692 // Try to resubscribe from the document without a key - should fail.
693 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
694 EXPECT_EQ(
695 "AbortError - Registration failed - missing applicationServerKey, "
696 "and manifest empty or missing",
697 script_result);
698
699 // Now try to resubscribe from the service worker without a key.
700 // This should also fail as the original key was not numeric.
586 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 701 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
587 EXPECT_EQ( 702 EXPECT_EQ(
588 "AbortError - Registration failed - missing applicationServerKey, and " 703 "AbortError - Registration failed - missing applicationServerKey, and "
589 "gcm_sender_id not found in manifest", 704 "gcm_sender_id not found in manifest",
590 script_result); 705 script_result);
591 EXPECT_NE(push_service(), GetAppHandler()); 706
592 707 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
593 // Run the subscription from the document without a key, this will trigger 708 EXPECT_EQ("unsubscribe result: true", script_result);
594 // the code to read sender id from the manifest and will write it to the 709
595 // datastore. 710 // After unsubscribing, try to resubscribe again without a key.
596 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 711 // This should again fail.
712 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
713 EXPECT_EQ(
714 "AbortError - Registration failed - missing applicationServerKey, "
715 "and gcm_sender_id not found in manifest",
716 script_result);
717 }
718
719 IN_PROC_BROWSER_TEST_F(
720 PushMessagingBrowserTest,
721 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithNumber) {
722 std::string script_result;
723
724 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
725 ASSERT_EQ("ok - service worker registered", script_result);
726
727 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
728
729 LoadTestPageWithoutManifest(); // Reload to become controlled.
730
731 ASSERT_TRUE(RunScript("isControlled()", &script_result));
732 ASSERT_EQ("true - is controlled", script_result);
733
734 // Run the subscribe from the document with a numeric key.
735 // This should succeed.
736 ASSERT_TRUE(
737 RunScript("documentSubscribePushWithNumericKey()", &script_result));
597 std::string token1; 738 std::string token1;
598 ASSERT_NO_FATAL_FAILURE( 739 ASSERT_NO_FATAL_FAILURE(
599 EndpointToToken(script_result, false /* standard_protocol */, &token1)); 740 EndpointToToken(script_result, false /* standard_protocol */, &token1));
600 741
601 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 742 // Try to resubscribe from the document without a key - should fail.
602 EXPECT_EQ("unsubscribe result: true", script_result); 743 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
603 EXPECT_NE(push_service(), GetAppHandler()); 744 EXPECT_EQ(
745 "AbortError - Registration failed - missing applicationServerKey, "
746 "and manifest empty or missing",
747 script_result);
604 748
605 // Now run the subscribe from the service worker without a key. 749 // Now run the subscribe from the service worker without a key.
606 // In this case, the sender id will be read from the datastore. 750 // In this case, the sender id should be read from the datastore.
751 // Note, we would rather this failed as we only really want to support
752 // no-key subscribes after subscribing with a numeric gcm sender id in the
753 // manifest, not a numeric applicationServerKey, but for code simplicity
754 // this case is allowed.
607 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 755 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
608 std::string token2; 756 std::string token2;
609 ASSERT_NO_FATAL_FAILURE( 757 ASSERT_NO_FATAL_FAILURE(
610 EndpointToToken(script_result, false /* standard_protocol */, &token2)); 758 EndpointToToken(script_result, false /* standard_protocol */, &token2));
611 EXPECT_NE(token1, token2); 759 EXPECT_EQ(token1, token2);
612 760
613 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 761 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
614 EXPECT_EQ("unsubscribe result: true", script_result); 762 EXPECT_EQ("unsubscribe result: true", script_result);
763 EXPECT_NE(push_service(), GetAppHandler());
764
765 // After unsubscribing, subscribe again from the worker with no key.
766 // The sender id should again be read from the datastore, so the
767 // subcribe should succeed, and we should get a new subscription token.
768 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
769 std::string token3;
770 ASSERT_NO_FATAL_FAILURE(
771 EndpointToToken(script_result, false /* standard_protocol */, &token3));
772 EXPECT_NE(token1, token3);
773
774 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
775 EXPECT_EQ("unsubscribe result: true", script_result);
776 EXPECT_NE(push_service(), GetAppHandler());
777 }
778
779 IN_PROC_BROWSER_TEST_F(
780 PushMessagingBrowserTest,
781 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithNumber) {
782 std::string script_result;
783
784 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
785 ASSERT_EQ("ok - service worker registered", script_result);
786
787 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
788
789 LoadTestPageWithoutManifest(); // Reload to become controlled.
790
791 ASSERT_TRUE(RunScript("isControlled()", &script_result));
792 ASSERT_EQ("true - is controlled", script_result);
793
794 // Run the subscribe from the service worker with a numeric key.
795 // This should succeed.
796 ASSERT_TRUE(RunScript("workerSubscribePushWithNumericKey()", &script_result));
797 std::string token1;
798 ASSERT_NO_FATAL_FAILURE(
799 EndpointToToken(script_result, false /* standard_protocol */, &token1));
800
801 // Try to resubscribe from the document without a key - should fail.
802 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
803 EXPECT_EQ(
804 "AbortError - Registration failed - missing applicationServerKey, "
805 "and manifest empty or missing",
806 script_result);
807
808 // Now run the subscribe from the service worker without a key.
809 // In this case, the sender id should be read from the datastore.
810 // Note, we would rather this failed as we only really want to support
811 // no-key subscribes after subscribing with a numeric gcm sender id in the
812 // manifest, not a numeric applicationServerKey, but for code simplicity
813 // this case is allowed.
814 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
815 std::string token2;
816 ASSERT_NO_FATAL_FAILURE(
817 EndpointToToken(script_result, false /* standard_protocol */, &token2));
818 EXPECT_EQ(token1, token2);
819
820 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
821 EXPECT_EQ("unsubscribe result: true", script_result);
822 EXPECT_NE(push_service(), GetAppHandler());
823
824 // After unsubscribing, subscribe again from the worker with no key.
825 // The sender id should again be read from the datastore, so the
826 // subcribe should succeed, and we should get a new subscription token.
827 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
828 std::string token3;
829 ASSERT_NO_FATAL_FAILURE(
830 EndpointToToken(script_result, false /* standard_protocol */, &token3));
831 EXPECT_NE(token1, token3);
832
833 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
834 EXPECT_EQ("unsubscribe result: true", script_result);
615 EXPECT_NE(push_service(), GetAppHandler()); 835 EXPECT_NE(push_service(), GetAppHandler());
616 } 836 }
617 837
618 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003). 838 // Disabled on Windows and Linux due to flakiness (http://crbug.com/554003).
619 #if defined(OS_WIN) || defined(OS_LINUX) 839 #if defined(OS_WIN) || defined(OS_LINUX)
620 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted 840 #define MAYBE_SubscribePersisted DISABLED_SubscribePersisted
621 #else 841 #else
622 #define MAYBE_SubscribePersisted SubscribePersisted 842 #define MAYBE_SubscribePersisted SubscribePersisted
623 #endif 843 #endif
624 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) { 844 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, MAYBE_SubscribePersisted) {
(...skipping 1259 matching lines...) Expand 10 before | Expand all | Expand 10 after
1884 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 2104 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1885 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2105 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1886 2106
1887 // After dropping the last subscription background mode is still inactive. 2107 // After dropping the last subscription background mode is still inactive.
1888 std::string script_result; 2108 std::string script_result;
1889 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 2109 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1890 EXPECT_EQ("unsubscribe result: true", script_result); 2110 EXPECT_EQ("unsubscribe result: true", script_result);
1891 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2111 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1892 } 2112 }
1893 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 2113 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS)
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/push_messaging/push_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698