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

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