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/browser/services/gcm/gcm_profile_service_factory.h" | 24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
25 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( | 119 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( |
115 &profile_, &BuildFakeGCMProfileService); | 120 &profile_, &BuildFakeGCMProfileService); |
116 } | 121 } |
117 | 122 |
118 ~PushMessagingServiceTest() override {} | 123 ~PushMessagingServiceTest() override {} |
119 | 124 |
120 // Callback to use when the subscription may have been subscribed. | 125 // Callback to use when the subscription may have been subscribed. |
121 void DidRegister(std::string* subscription_id_out, | 126 void DidRegister(std::string* subscription_id_out, |
122 std::vector<uint8_t>* p256dh_out, | 127 std::vector<uint8_t>* p256dh_out, |
123 std::vector<uint8_t>* auth_out, | 128 std::vector<uint8_t>* auth_out, |
| 129 base::Closure done_callback, |
124 const std::string& registration_id, | 130 const std::string& registration_id, |
125 const std::vector<uint8_t>& p256dh, | 131 const std::vector<uint8_t>& p256dh, |
126 const std::vector<uint8_t>& auth, | 132 const std::vector<uint8_t>& auth, |
127 content::PushRegistrationStatus status) { | 133 content::PushRegistrationStatus status) { |
128 ASSERT_EQ(content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 134 EXPECT_EQ(content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
129 status); | 135 status); |
130 | 136 |
131 *subscription_id_out = registration_id; | 137 *subscription_id_out = registration_id; |
132 *p256dh_out = p256dh; | 138 *p256dh_out = p256dh; |
133 *auth_out = auth; | 139 *auth_out = auth; |
| 140 |
| 141 done_callback.Run(); |
134 } | 142 } |
135 | 143 |
136 // Callback to use when observing messages dispatched by the push service. | 144 // Callback to use when observing messages dispatched by the push service. |
137 void DidDispatchMessage(std::string* app_id_out, | 145 void DidDispatchMessage(std::string* app_id_out, |
138 GURL* origin_out, | 146 GURL* origin_out, |
139 int64_t* service_worker_registration_id_out, | 147 int64_t* service_worker_registration_id_out, |
140 content::PushEventPayload* payload_out, | 148 content::PushEventPayload* payload_out, |
141 const std::string& app_id, | 149 const std::string& app_id, |
142 const GURL& origin, | 150 const GURL& origin, |
143 int64_t service_worker_registration_id, | 151 int64_t service_worker_registration_id, |
144 const content::PushEventPayload& payload) { | 152 const content::PushEventPayload& payload) { |
145 *app_id_out = app_id; | 153 *app_id_out = app_id; |
146 *origin_out = origin; | 154 *origin_out = origin; |
147 *service_worker_registration_id_out = service_worker_registration_id; | 155 *service_worker_registration_id_out = service_worker_registration_id; |
148 *payload_out = payload; | 156 *payload_out = payload; |
149 } | 157 } |
150 | 158 |
151 protected: | 159 protected: |
152 PushMessagingTestingProfile* profile() { return &profile_; } | 160 PushMessagingTestingProfile* profile() { return &profile_; } |
153 | 161 |
154 private: | 162 private: |
155 content::TestBrowserThreadBundle thread_bundle_; | 163 content::TestBrowserThreadBundle thread_bundle_; |
156 PushMessagingTestingProfile profile_; | 164 PushMessagingTestingProfile profile_; |
| 165 |
| 166 #if defined(OS_ANDROID) |
| 167 instance_id::InstanceIDAndroid::ScopedBlockOnAsyncTasksForTesting |
| 168 block_async_; |
| 169 instance_id::ScopedUseFakeInstanceIDAndroid use_fake_; |
| 170 #endif // OS_ANDROID |
157 }; | 171 }; |
158 | 172 |
159 TEST_F(PushMessagingServiceTest, PayloadEncryptionTest) { | 173 TEST_F(PushMessagingServiceTest, PayloadEncryptionTest) { |
160 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); | 174 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); |
161 ASSERT_TRUE(push_service); | 175 ASSERT_TRUE(push_service); |
162 | 176 |
163 const GURL origin(kTestOrigin); | 177 const GURL origin(kTestOrigin); |
164 | 178 |
165 // (1) Make sure that |kExampleOrigin| has access to use Push Messaging. | 179 // (1) Make sure that |kExampleOrigin| has access to use Push Messaging. |
166 ASSERT_EQ(blink::WebPushPermissionStatusGranted, | 180 ASSERT_EQ(blink::WebPushPermissionStatusGranted, |
167 push_service->GetPermissionStatus(origin, true)); | 181 push_service->GetPermissionStatus(origin, true)); |
168 | 182 |
169 std::string subscription_id; | 183 std::string subscription_id; |
170 std::vector<uint8_t> p256dh, auth; | 184 std::vector<uint8_t> p256dh, auth; |
171 | 185 |
| 186 base::RunLoop run_loop; |
| 187 |
172 // (2) Subscribe for Push Messaging, and verify that we've got the required | 188 // (2) Subscribe for Push Messaging, and verify that we've got the required |
173 // information in order to be able to create encrypted messages. | 189 // information in order to be able to create encrypted messages. |
174 content::PushSubscriptionOptions options; | 190 content::PushSubscriptionOptions options; |
175 options.user_visible_only = true; | 191 options.user_visible_only = true; |
176 options.sender_info = kTestSenderId; | 192 options.sender_info = kTestSenderId; |
177 push_service->SubscribeFromWorker( | 193 push_service->SubscribeFromWorker( |
178 origin, kTestServiceWorkerId, options, | 194 origin, kTestServiceWorkerId, options, |
179 base::Bind(&PushMessagingServiceTest::DidRegister, base::Unretained(this), | 195 base::Bind(&PushMessagingServiceTest::DidRegister, base::Unretained(this), |
180 &subscription_id, &p256dh, &auth)); | 196 &subscription_id, &p256dh, &auth, run_loop.QuitClosure())); |
181 | 197 |
182 EXPECT_EQ(0u, subscription_id.size()); // this must be asynchronous | 198 EXPECT_EQ(0u, subscription_id.size()); // this must be asynchronous |
183 | 199 |
184 base::RunLoop().RunUntilIdle(); | 200 run_loop.Run(); |
185 | 201 |
186 ASSERT_GT(subscription_id.size(), 0u); | 202 ASSERT_GT(subscription_id.size(), 0u); |
187 ASSERT_GT(p256dh.size(), 0u); | 203 ASSERT_GT(p256dh.size(), 0u); |
188 ASSERT_GT(auth.size(), 0u); | 204 ASSERT_GT(auth.size(), 0u); |
189 | 205 |
190 // (3) Encrypt a message using the public key and authentication secret that | 206 // (3) Encrypt a message using the public key and authentication secret that |
191 // are associated with the subscription. | 207 // are associated with the subscription. |
192 | 208 |
193 gcm::IncomingMessage message; | 209 gcm::IncomingMessage message; |
194 message.sender_id = kTestSenderId; | 210 message.sender_id = kTestSenderId; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 EXPECT_EQ(kTestEncodedP256Key, push_service->NormalizeSenderInfo(p256dh)); | 269 EXPECT_EQ(kTestEncodedP256Key, push_service->NormalizeSenderInfo(p256dh)); |
254 | 270 |
255 // Any other value, binary or not, will be passed through as-is. | 271 // Any other value, binary or not, will be passed through as-is. |
256 EXPECT_EQ("1234567890", push_service->NormalizeSenderInfo("1234567890")); | 272 EXPECT_EQ("1234567890", push_service->NormalizeSenderInfo("1234567890")); |
257 EXPECT_EQ("foo@bar.com", push_service->NormalizeSenderInfo("foo@bar.com")); | 273 EXPECT_EQ("foo@bar.com", push_service->NormalizeSenderInfo("foo@bar.com")); |
258 | 274 |
259 p256dh[0] = 0x05; // invalidate |p256dh| as a public key. | 275 p256dh[0] = 0x05; // invalidate |p256dh| as a public key. |
260 | 276 |
261 EXPECT_EQ(p256dh, push_service->NormalizeSenderInfo(p256dh)); | 277 EXPECT_EQ(p256dh, push_service->NormalizeSenderInfo(p256dh)); |
262 } | 278 } |
OLD | NEW |