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

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

Issue 2469293002: Handle push resubscribes with no sender info (avoids DCHECK) (Closed)
Patch Set: call it fixed_sender_id when it is (hopefully) fixed 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 523
518 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 524 ASSERT_TRUE(RunScript("isControlled()", &script_result));
519 ASSERT_EQ("true - is controlled", script_result); 525 ASSERT_EQ("true - is controlled", script_result);
520 526
521 // Try to subscribe from a worker without a key. This should fail. 527 // Try to subscribe from a worker without a key. This should fail.
522 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 528 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
523 EXPECT_EQ( 529 EXPECT_EQ(
524 "AbortError - Registration failed - missing applicationServerKey, and " 530 "AbortError - Registration failed - missing applicationServerKey, and "
525 "gcm_sender_id not found in manifest", 531 "gcm_sender_id not found in manifest",
526 script_result); 532 script_result);
527 // Now run the subscribe from the service worker with a key. This 533
528 // should succeed, and write the key to the datastore. 534 // Now run the subscribe with a key. This should succeed.
529 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result)); 535 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
530 std::string token1;
531 ASSERT_NO_FATAL_FAILURE( 536 ASSERT_NO_FATAL_FAILURE(
532 EndpointToToken(script_result, true /* standard_protocol */, &token1)); 537 EndpointToToken(script_result, true /* standard_protocol */));
533
534 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
535 EXPECT_EQ("unsubscribe result: true", script_result);
536 EXPECT_NE(push_service(), GetAppHandler());
537
538 // Now run the subscribe from the service worker without a key.
539 // In this case, the key will be read from the datastore.
540 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
541 std::string token2;
542 ASSERT_NO_FATAL_FAILURE(
543 EndpointToToken(script_result, true /* standard_protocol */, &token2));
544 EXPECT_NE(token1, token2);
545 538
546 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 539 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
547 EXPECT_EQ("unsubscribe result: true", script_result); 540 EXPECT_EQ("unsubscribe result: true", script_result);
548 EXPECT_NE(push_service(), GetAppHandler()); 541 EXPECT_NE(push_service(), GetAppHandler());
549 } 542 }
550 543
551 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 544 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
552 SubscribeWorkerUsingManifest) { 545 ResubscribeWithoutKeyAfterSubscribingWithKeyInManifest) {
553 std::string script_result; 546 std::string script_result;
554 547
555 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 548 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
549 ASSERT_EQ("ok - service worker registered", script_result);
550
551 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
552
553 LoadTestPage(); // Reload to become controlled.
554
555 ASSERT_TRUE(RunScript("isControlled()", &script_result));
556 ASSERT_EQ("true - is controlled", script_result);
557
558 // Run the subscription from the document without a key, this will trigger
559 // the code to read sender id from the manifest and will write it to the
560 // datastore.
561 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
562 std::string token1;
563 ASSERT_NO_FATAL_FAILURE(
564 EndpointToToken(script_result, false /* standard_protocol */, &token1));
565
566 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
567 ASSERT_EQ("manifest removed", script_result);
568
569 // Try to resubscribe from the document without a key or manifest.
570 // This should fail.
571 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
572 EXPECT_EQ(
573 "AbortError - Registration failed - missing applicationServerKey, "
574 "and manifest empty or missing",
575 script_result);
576
577 // Now run the subscribe from the service worker without a key.
578 // In this case, the sender id should be read from the datastore.
579 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
580 std::string token2;
581 ASSERT_NO_FATAL_FAILURE(
582 EndpointToToken(script_result, false /* standard_protocol */, &token2));
583 EXPECT_EQ(token1, token2);
584
585 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
586 EXPECT_EQ("unsubscribe result: true", script_result);
587 EXPECT_NE(push_service(), GetAppHandler());
588
589 // After unsubscribing, subscribe again from the worker with no key.
590 // The sender id should again be read from the datastore, so the
591 // subcribe should succeed, and we should get a new subscription token.
592 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
593 std::string token3;
594 ASSERT_NO_FATAL_FAILURE(
595 EndpointToToken(script_result, false /* standard_protocol */, &token3));
596 EXPECT_NE(token1, token3);
597
598 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
599 EXPECT_EQ("unsubscribe result: true", script_result);
600 EXPECT_NE(push_service(), GetAppHandler());
601 }
602
603 IN_PROC_BROWSER_TEST_F(
604 PushMessagingBrowserTest,
605 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithP256Key) {
606 std::string script_result;
607
608 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
556 ASSERT_EQ("ok - service worker registered", script_result); 609 ASSERT_EQ("ok - service worker registered", script_result);
557 610
558 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission()); 611 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
559 612
560 LoadTestPage(); // Reload to become controlled. 613 LoadTestPageWithoutManifest(); // Reload to become controlled.
561 614
562 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 615 ASSERT_TRUE(RunScript("isControlled()", &script_result));
563 ASSERT_EQ("true - is controlled", script_result); 616 ASSERT_EQ("true - is controlled", script_result);
564 617
565 // Try to subscribe from a worker without a key. This should fail. 618 // Run the subscription from the document with a key.
619 ASSERT_TRUE(RunScript("documentSubscribePush()", &script_result));
620 ASSERT_NO_FATAL_FAILURE(EndpointToToken(script_result));
621
622 // Try to resubscribe from the document without a key - should fail.
623 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
624 EXPECT_EQ(
625 "AbortError - Registration failed - missing applicationServerKey, "
626 "and manifest empty or missing",
627 script_result);
628
629 // Now try to resubscribe from the service worker without a key.
630 // This should also fail as the original key was not numeric.
631 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
632 EXPECT_EQ(
633 "AbortError - Registration failed - missing applicationServerKey, "
634 "and gcm_sender_id not found in manifest",
635 script_result);
636
637 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
638 EXPECT_EQ("unsubscribe result: true", script_result);
639 EXPECT_NE(push_service(), GetAppHandler());
640
641 // After unsubscribing, try to resubscribe again without a key.
642 // This should again fail.
643 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
644 EXPECT_EQ(
645 "AbortError - Registration failed - missing applicationServerKey, "
646 "and gcm_sender_id not found in manifest",
647 script_result);
648 }
649
650 IN_PROC_BROWSER_TEST_F(
651 PushMessagingBrowserTest,
652 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithP256Key) {
653 std::string script_result;
654
655 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
656 ASSERT_EQ("ok - service worker registered", script_result);
657
658 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
659
660 LoadTestPageWithoutManifest(); // Reload to become controlled.
661
662 ASSERT_TRUE(RunScript("isControlled()", &script_result));
663 ASSERT_EQ("true - is controlled", script_result);
664
665 // Run the subscribe from the service worker with a key.
666 // This should succeed.
667 ASSERT_TRUE(RunScript("workerSubscribePush()", &script_result));
668 ASSERT_NO_FATAL_FAILURE(
669 EndpointToToken(script_result, true /* standard_protocol */));
670
671 // Try to resubscribe from the document without a key - should fail.
672 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
673 EXPECT_EQ(
674 "AbortError - Registration failed - missing applicationServerKey, "
675 "and manifest empty or missing",
676 script_result);
677
678 // Now try to resubscribe from the service worker without a key.
679 // This should also fail as the original key was not numeric.
566 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 680 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
567 EXPECT_EQ( 681 EXPECT_EQ(
568 "AbortError - Registration failed - missing applicationServerKey, and " 682 "AbortError - Registration failed - missing applicationServerKey, and "
569 "gcm_sender_id not found in manifest", 683 "gcm_sender_id not found in manifest",
570 script_result); 684 script_result);
571 EXPECT_NE(push_service(), GetAppHandler()); 685
572 686 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
573 // Run the subscription from the document without a key, this will trigger 687 EXPECT_EQ("unsubscribe result: true", script_result);
574 // the code to read sender id from the manifest and will write it to the 688
575 // datastore. 689 // After unsubscribing, try to resubscribe again without a key.
576 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result)); 690 // This should again fail.
691 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
692 EXPECT_EQ(
693 "AbortError - Registration failed - missing applicationServerKey, "
694 "and gcm_sender_id not found in manifest",
695 script_result);
696 }
697
698 IN_PROC_BROWSER_TEST_F(
699 PushMessagingBrowserTest,
700 ResubscribeWithoutKeyAfterSubscribingFromDocumentWithNumber) {
701 std::string script_result;
702
703 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
704 ASSERT_EQ("ok - service worker registered", script_result);
705
706 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
707
708 LoadTestPageWithoutManifest(); // Reload to become controlled.
709
710 ASSERT_TRUE(RunScript("isControlled()", &script_result));
711 ASSERT_EQ("true - is controlled", script_result);
712
713 // Run the subscribe from the document with a numeric key.
714 // This should succeed.
715 ASSERT_TRUE(
716 RunScript("documentSubscribePushWithNumericKey()", &script_result));
577 std::string token1; 717 std::string token1;
578 ASSERT_NO_FATAL_FAILURE( 718 ASSERT_NO_FATAL_FAILURE(
579 EndpointToToken(script_result, false /* standard_protocol */, &token1)); 719 EndpointToToken(script_result, false /* standard_protocol */, &token1));
580 720
581 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 721 // Try to resubscribe from the document without a key - should fail.
582 EXPECT_EQ("unsubscribe result: true", script_result); 722 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
583 EXPECT_NE(push_service(), GetAppHandler()); 723 EXPECT_EQ(
724 "AbortError - Registration failed - missing applicationServerKey, "
725 "and manifest empty or missing",
726 script_result);
584 727
585 // Now run the subscribe from the service worker without a key. 728 // Now run the subscribe from the service worker without a key.
586 // In this case, the sender id will be read from the datastore. 729 // In this case, the sender id should be read from the datastore.
730 // Note, we would rather this failed as we only really want to support
731 // no-key subscribes after subscribing with a numeric gcm sender id in the
732 // manifest, not a numeric applicationServerKey, but for code simplicity
733 // this case is allowed.
587 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result)); 734 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
588 std::string token2; 735 std::string token2;
589 ASSERT_NO_FATAL_FAILURE( 736 ASSERT_NO_FATAL_FAILURE(
590 EndpointToToken(script_result, false /* standard_protocol */, &token2)); 737 EndpointToToken(script_result, false /* standard_protocol */, &token2));
591 EXPECT_NE(token1, token2); 738 EXPECT_EQ(token1, token2);
592 739
593 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 740 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
594 EXPECT_EQ("unsubscribe result: true", script_result); 741 EXPECT_EQ("unsubscribe result: true", script_result);
742 EXPECT_NE(push_service(), GetAppHandler());
743
744 // After unsubscribing, subscribe again from the worker with no key.
745 // The sender id should again be read from the datastore, so the
746 // subcribe should succeed, and we should get a new subscription token.
747 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
748 std::string token3;
749 ASSERT_NO_FATAL_FAILURE(
750 EndpointToToken(script_result, false /* standard_protocol */, &token3));
751 EXPECT_NE(token1, token3);
752
753 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
754 EXPECT_EQ("unsubscribe result: true", script_result);
755 EXPECT_NE(push_service(), GetAppHandler());
756 }
757
758 IN_PROC_BROWSER_TEST_F(
759 PushMessagingBrowserTest,
760 ResubscribeWithoutKeyAfterSubscribingFromWorkerWithNumber) {
761 std::string script_result;
762
763 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
764 ASSERT_EQ("ok - service worker registered", script_result);
765
766 ASSERT_NO_FATAL_FAILURE(RequestAndAcceptPermission());
767
768 LoadTestPageWithoutManifest(); // Reload to become controlled.
769
770 ASSERT_TRUE(RunScript("isControlled()", &script_result));
771 ASSERT_EQ("true - is controlled", script_result);
772
773 // Run the subscribe from the service worker with a numeric key.
774 // This should succeed.
775 ASSERT_TRUE(RunScript("workerSubscribePushWithNumericKey()", &script_result));
776 std::string token1;
777 ASSERT_NO_FATAL_FAILURE(
778 EndpointToToken(script_result, false /* standard_protocol */, &token1));
779
780 // Try to resubscribe from the document without a key - should fail.
781 ASSERT_TRUE(RunScript("documentSubscribePushWithoutKey()", &script_result));
782 EXPECT_EQ(
783 "AbortError - Registration failed - missing applicationServerKey, "
784 "and manifest empty or missing",
785 script_result);
786
787 // Now run the subscribe from the service worker without a key.
788 // In this case, the sender id should be read from the datastore.
789 // Note, we would rather this failed as we only really want to support
790 // no-key subscribes after subscribing with a numeric gcm sender id in the
791 // manifest, not a numeric applicationServerKey, but for code simplicity
792 // this case is allowed.
793 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
794 std::string token2;
795 ASSERT_NO_FATAL_FAILURE(
796 EndpointToToken(script_result, false /* standard_protocol */, &token2));
797 EXPECT_EQ(token1, token2);
798
799 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
800 EXPECT_EQ("unsubscribe result: true", script_result);
801 EXPECT_NE(push_service(), GetAppHandler());
802
803 // After unsubscribing, subscribe again from the worker with no key.
804 // The sender id should again be read from the datastore, so the
805 // subcribe should succeed, and we should get a new subscription token.
806 ASSERT_TRUE(RunScript("workerSubscribePushNoKey()", &script_result));
807 std::string token3;
808 ASSERT_NO_FATAL_FAILURE(
809 EndpointToToken(script_result, false /* standard_protocol */, &token3));
810 EXPECT_NE(token1, token3);
811
812 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
813 EXPECT_EQ("unsubscribe result: true", script_result);
595 EXPECT_NE(push_service(), GetAppHandler()); 814 EXPECT_NE(push_service(), GetAppHandler());
596 } 815 }
597 816
598 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribePersisted) { 817 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, SubscribePersisted) {
599 std::string script_result; 818 std::string script_result;
600 819
601 // First, test that Service Worker registration IDs are assigned in order of 820 // First, test that Service Worker registration IDs are assigned in order of
602 // registering the Service Workers, and the (fake) push subscription ids are 821 // registering the Service Workers, and the (fake) push subscription ids are
603 // assigned in order of push subscription (even when these orders are 822 // assigned in order of push subscription (even when these orders are
604 // different). 823 // different).
(...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after
1858 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully()); 2077 ASSERT_NO_FATAL_FAILURE(SubscribeSuccessfully());
1859 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2078 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1860 2079
1861 // After dropping the last subscription background mode is still inactive. 2080 // After dropping the last subscription background mode is still inactive.
1862 std::string script_result; 2081 std::string script_result;
1863 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); 2082 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result));
1864 EXPECT_EQ("unsubscribe result: true", script_result); 2083 EXPECT_EQ("unsubscribe result: true", script_result);
1865 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); 2084 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive());
1866 } 2085 }
1867 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) 2086 #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