OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/public/browser/push_messaging_service.h" | 5 #include "content/public/browser/push_messaging_service.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
25 #include "components/content_settings/core/browser/host_content_settings_map.h" | 25 #include "components/content_settings/core/browser/host_content_settings_map.h" |
26 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" | 26 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" |
27 #include "components/gcm_driver/fake_gcm_client_factory.h" | 27 #include "components/gcm_driver/fake_gcm_client_factory.h" |
28 #include "components/gcm_driver/gcm_profile_service.h" | 28 #include "components/gcm_driver/gcm_profile_service.h" |
29 #include "content/public/common/push_event_payload.h" | 29 #include "content/public/common/push_event_payload.h" |
30 #include "content/public/common/push_subscription_options.h" | 30 #include "content/public/common/push_subscription_options.h" |
31 #include "content/public/test/test_browser_thread_bundle.h" | 31 #include "content/public/test/test_browser_thread_bundle.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
33 | 33 |
| 34 #if defined(OS_ANDROID) |
| 35 #include "components/gcm_driver/instance_id/instance_id_android.h" |
| 36 #include "components/gcm_driver/instance_id/scoped_use_fake_instance_id_android.
h" |
| 37 #endif // OS_ANDROID |
| 38 |
34 namespace { | 39 namespace { |
35 | 40 |
36 const char kTestOrigin[] = "https://example.com"; | 41 const char kTestOrigin[] = "https://example.com"; |
37 const char kTestSenderId[] = "1234567890"; | 42 const char kTestSenderId[] = "1234567890"; |
38 const int64_t kTestServiceWorkerId = 42; | 43 const int64_t kTestServiceWorkerId = 42; |
39 const char kTestPayload[] = "Hello, world!"; | 44 const char kTestPayload[] = "Hello, world!"; |
40 | 45 |
41 // NIST P-256 public key in uncompressed format per SEC1 2.3.3. | 46 // NIST P-256 public key in uncompressed format per SEC1 2.3.3. |
42 const uint8_t kTestP256Key[] = { | 47 const uint8_t kTestP256Key[] = { |
43 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, | 48 0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( | 99 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( |
95 &profile_, &BuildFakeGCMProfileService); | 100 &profile_, &BuildFakeGCMProfileService); |
96 } | 101 } |
97 | 102 |
98 ~PushMessagingServiceTest() override {} | 103 ~PushMessagingServiceTest() override {} |
99 | 104 |
100 // Callback to use when the subscription may have been subscribed. | 105 // Callback to use when the subscription may have been subscribed. |
101 void DidRegister(std::string* subscription_id_out, | 106 void DidRegister(std::string* subscription_id_out, |
102 std::vector<uint8_t>* p256dh_out, | 107 std::vector<uint8_t>* p256dh_out, |
103 std::vector<uint8_t>* auth_out, | 108 std::vector<uint8_t>* auth_out, |
| 109 base::Closure done_callback, |
104 const std::string& registration_id, | 110 const std::string& registration_id, |
105 const std::vector<uint8_t>& p256dh, | 111 const std::vector<uint8_t>& p256dh, |
106 const std::vector<uint8_t>& auth, | 112 const std::vector<uint8_t>& auth, |
107 content::PushRegistrationStatus status) { | 113 content::PushRegistrationStatus status) { |
108 ASSERT_EQ(content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 114 EXPECT_EQ(content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
109 status); | 115 status); |
110 | 116 |
111 *subscription_id_out = registration_id; | 117 *subscription_id_out = registration_id; |
112 *p256dh_out = p256dh; | 118 *p256dh_out = p256dh; |
113 *auth_out = auth; | 119 *auth_out = auth; |
| 120 |
| 121 done_callback.Run(); |
114 } | 122 } |
115 | 123 |
116 // Callback to use when observing messages dispatched by the push service. | 124 // Callback to use when observing messages dispatched by the push service. |
117 void DidDispatchMessage(std::string* app_id_out, | 125 void DidDispatchMessage(std::string* app_id_out, |
118 GURL* origin_out, | 126 GURL* origin_out, |
119 int64_t* service_worker_registration_id_out, | 127 int64_t* service_worker_registration_id_out, |
120 content::PushEventPayload* payload_out, | 128 content::PushEventPayload* payload_out, |
121 const std::string& app_id, | 129 const std::string& app_id, |
122 const GURL& origin, | 130 const GURL& origin, |
123 int64_t service_worker_registration_id, | 131 int64_t service_worker_registration_id, |
124 const content::PushEventPayload& payload) { | 132 const content::PushEventPayload& payload) { |
125 *app_id_out = app_id; | 133 *app_id_out = app_id; |
126 *origin_out = origin; | 134 *origin_out = origin; |
127 *service_worker_registration_id_out = service_worker_registration_id; | 135 *service_worker_registration_id_out = service_worker_registration_id; |
128 *payload_out = payload; | 136 *payload_out = payload; |
129 } | 137 } |
130 | 138 |
131 protected: | 139 protected: |
132 PushMessagingTestingProfile* profile() { return &profile_; } | 140 PushMessagingTestingProfile* profile() { return &profile_; } |
133 | 141 |
134 private: | 142 private: |
135 content::TestBrowserThreadBundle thread_bundle_; | 143 content::TestBrowserThreadBundle thread_bundle_; |
136 PushMessagingTestingProfile profile_; | 144 PushMessagingTestingProfile profile_; |
| 145 |
| 146 #if defined(OS_ANDROID) |
| 147 instance_id::InstanceIDAndroid::ScopedBlockOnAsyncTasksForTesting |
| 148 block_async_; |
| 149 instance_id::ScopedUseFakeInstanceIDAndroid use_fake_; |
| 150 #endif // OS_ANDROID |
137 }; | 151 }; |
138 | 152 |
139 TEST_F(PushMessagingServiceTest, PayloadEncryptionTest) { | 153 TEST_F(PushMessagingServiceTest, PayloadEncryptionTest) { |
140 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); | 154 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); |
141 ASSERT_TRUE(push_service); | 155 ASSERT_TRUE(push_service); |
142 | 156 |
143 const GURL origin(kTestOrigin); | 157 const GURL origin(kTestOrigin); |
144 | 158 |
145 // (1) Make sure that |kExampleOrigin| has access to use Push Messaging. | 159 // (1) Make sure that |kExampleOrigin| has access to use Push Messaging. |
146 ASSERT_EQ(blink::WebPushPermissionStatusGranted, | 160 ASSERT_EQ(blink::WebPushPermissionStatusGranted, |
147 push_service->GetPermissionStatus(origin, true)); | 161 push_service->GetPermissionStatus(origin, true)); |
148 | 162 |
149 std::string subscription_id; | 163 std::string subscription_id; |
150 std::vector<uint8_t> p256dh, auth; | 164 std::vector<uint8_t> p256dh, auth; |
151 | 165 |
| 166 base::RunLoop run_loop; |
| 167 |
152 // (2) Subscribe for Push Messaging, and verify that we've got the required | 168 // (2) Subscribe for Push Messaging, and verify that we've got the required |
153 // information in order to be able to create encrypted messages. | 169 // information in order to be able to create encrypted messages. |
154 content::PushSubscriptionOptions options; | 170 content::PushSubscriptionOptions options; |
155 options.user_visible_only = true; | 171 options.user_visible_only = true; |
156 options.sender_info = kTestSenderId; | 172 options.sender_info = kTestSenderId; |
157 push_service->SubscribeFromWorker( | 173 push_service->SubscribeFromWorker( |
158 origin, kTestServiceWorkerId, options, | 174 origin, kTestServiceWorkerId, options, |
159 base::Bind(&PushMessagingServiceTest::DidRegister, base::Unretained(this), | 175 base::Bind(&PushMessagingServiceTest::DidRegister, base::Unretained(this), |
160 &subscription_id, &p256dh, &auth)); | 176 &subscription_id, &p256dh, &auth, run_loop.QuitClosure())); |
161 | 177 |
162 EXPECT_EQ(0u, subscription_id.size()); // this must be asynchronous | 178 EXPECT_EQ(0u, subscription_id.size()); // this must be asynchronous |
163 | 179 |
164 base::RunLoop().RunUntilIdle(); | 180 run_loop.Run(); |
165 | 181 |
166 ASSERT_GT(subscription_id.size(), 0u); | 182 ASSERT_GT(subscription_id.size(), 0u); |
167 ASSERT_GT(p256dh.size(), 0u); | 183 ASSERT_GT(p256dh.size(), 0u); |
168 ASSERT_GT(auth.size(), 0u); | 184 ASSERT_GT(auth.size(), 0u); |
169 | 185 |
170 // (3) Encrypt a message using the public key and authentication secret that | 186 // (3) Encrypt a message using the public key and authentication secret that |
171 // are associated with the subscription. | 187 // are associated with the subscription. |
172 | 188 |
173 gcm::IncomingMessage message; | 189 gcm::IncomingMessage message; |
174 message.sender_id = kTestSenderId; | 190 message.sender_id = kTestSenderId; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 } | 258 } |
243 | 259 |
244 TEST_F(PushMessagingServiceTest, DifferentEndpoints) { | 260 TEST_F(PushMessagingServiceTest, DifferentEndpoints) { |
245 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); | 261 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); |
246 ASSERT_TRUE(push_service); | 262 ASSERT_TRUE(push_service); |
247 | 263 |
248 // Verifies that the service returns different endpoints depending on whether | 264 // Verifies that the service returns different endpoints depending on whether |
249 // support for the standard protocol is requested. | 265 // support for the standard protocol is requested. |
250 EXPECT_NE(push_service->GetEndpoint(true), push_service->GetEndpoint(false)); | 266 EXPECT_NE(push_service->GetEndpoint(true), push_service->GetEndpoint(false)); |
251 } | 267 } |
OLD | NEW |