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