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> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/metrics/field_trial.h" | |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
16 #include "chrome/browser/permissions/permission_manager.h" | 17 #include "chrome/browser/permissions/permission_manager.h" |
17 #include "chrome/browser/permissions/permission_manager_factory.h" | 18 #include "chrome/browser/permissions/permission_manager_factory.h" |
18 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 19 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
19 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" | 20 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" |
20 #include "chrome/browser/push_messaging/push_messaging_permission_context_factor y.h" | 21 #include "chrome/browser/push_messaging/push_messaging_permission_context_factor y.h" |
21 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 22 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
22 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" | 23 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
23 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 24 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 25 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
25 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
26 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" | 27 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" |
27 #include "components/gcm_driver/fake_gcm_client_factory.h" | 28 #include "components/gcm_driver/fake_gcm_client_factory.h" |
28 #include "components/gcm_driver/gcm_profile_service.h" | 29 #include "components/gcm_driver/gcm_profile_service.h" |
29 #include "content/public/common/push_event_payload.h" | 30 #include "content/public/common/push_event_payload.h" |
30 #include "content/public/common/push_subscription_options.h" | 31 #include "content/public/common/push_subscription_options.h" |
31 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
33 | 34 |
35 #if defined(OS_ANDROID) | |
36 #include "components/gcm_driver/instance_id/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 // Implementation of the TestingProfile that provides the Push Messaging Service | 46 // Implementation of the TestingProfile that provides the Push Messaging Service |
42 // and the Permission Manager, both of which are required for the tests. | 47 // and the Permission Manager, both of which are required for the tests. |
43 class PushMessagingTestingProfile : public TestingProfile { | 48 class PushMessagingTestingProfile : public TestingProfile { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 | 86 |
82 scoped_ptr<KeyedService> BuildFakeGCMProfileService( | 87 scoped_ptr<KeyedService> BuildFakeGCMProfileService( |
83 content::BrowserContext* context) { | 88 content::BrowserContext* context) { |
84 return gcm::FakeGCMProfileService::Build(static_cast<Profile*>(context)); | 89 return gcm::FakeGCMProfileService::Build(static_cast<Profile*>(context)); |
85 } | 90 } |
86 | 91 |
87 } // namespace | 92 } // namespace |
88 | 93 |
89 class PushMessagingServiceTest : public ::testing::Test { | 94 class PushMessagingServiceTest : public ::testing::Test { |
90 public: | 95 public: |
91 PushMessagingServiceTest() { | 96 PushMessagingServiceTest() |
97 : thread_bundle_( | |
98 content::TestBrowserThreadBundle::NESTED_JAVA_ON_ANDROID), | |
99 field_trial_list_(nullptr /* entropy_provider */) { | |
100 base::FieldTrialList::CreateFieldTrial("InstanceID", "Enabled"); | |
Peter Beverloo
2016/04/11 15:47:56
Why are you removing the enable-InstanceID-field-t
johnme
2016/04/21 10:58:37
Hadn't yet rebased this. Removed.
| |
101 | |
92 // Override the permission context factory to always allow Push Messaging. | 102 // Override the permission context factory to always allow Push Messaging. |
93 PushMessagingPermissionContextFactory::GetInstance()->SetTestingFactory( | 103 PushMessagingPermissionContextFactory::GetInstance()->SetTestingFactory( |
94 &profile_, &BuildFakePushMessagingPermissionContext); | 104 &profile_, &BuildFakePushMessagingPermissionContext); |
95 | 105 |
96 // Override the GCM Profile service so that we can send fake messages. | 106 // Override the GCM Profile service so that we can send fake messages. |
97 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( | 107 gcm::GCMProfileServiceFactory::GetInstance()->SetTestingFactory( |
98 &profile_, &BuildFakeGCMProfileService); | 108 &profile_, &BuildFakeGCMProfileService); |
99 } | 109 } |
100 | 110 |
101 ~PushMessagingServiceTest() override {} | 111 ~PushMessagingServiceTest() override {} |
102 | 112 |
113 #if defined(OS_ANDROID) | |
114 void SetUp() override { | |
115 instance_id::InstanceIDAndroid::ClearDataAndSetUseFakeForTesting(true); | |
116 } | |
117 void TearDown() override { | |
118 instance_id::InstanceIDAndroid::ClearDataAndSetUseFakeForTesting(false); | |
119 } | |
120 #endif // OS_ANDROID | |
121 | |
103 // Callback to use when the subscription may have been subscribed. | 122 // Callback to use when the subscription may have been subscribed. |
104 void DidRegister(std::string* subscription_id_out, | 123 void DidRegister(std::string* subscription_id_out, |
105 std::vector<uint8_t>* p256dh_out, | 124 std::vector<uint8_t>* p256dh_out, |
106 std::vector<uint8_t>* auth_out, | 125 std::vector<uint8_t>* auth_out, |
126 base::Closure done_callback, | |
107 const std::string& registration_id, | 127 const std::string& registration_id, |
108 const std::vector<uint8_t>& p256dh, | 128 const std::vector<uint8_t>& p256dh, |
109 const std::vector<uint8_t>& auth, | 129 const std::vector<uint8_t>& auth, |
110 content::PushRegistrationStatus status) { | 130 content::PushRegistrationStatus status) { |
111 ASSERT_EQ(content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 131 EXPECT_EQ(content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
112 status); | 132 status); |
113 | 133 |
114 *subscription_id_out = registration_id; | 134 *subscription_id_out = registration_id; |
115 *p256dh_out = p256dh; | 135 *p256dh_out = p256dh; |
116 *auth_out = auth; | 136 *auth_out = auth; |
137 | |
138 done_callback.Run(); | |
117 } | 139 } |
118 | 140 |
119 // Callback to use when observing messages dispatched by the push service. | 141 // Callback to use when observing messages dispatched by the push service. |
120 void DidDispatchMessage(std::string* app_id_out, | 142 void DidDispatchMessage(std::string* app_id_out, |
121 GURL* origin_out, | 143 GURL* origin_out, |
122 int64_t* service_worker_registration_id_out, | 144 int64_t* service_worker_registration_id_out, |
123 content::PushEventPayload* payload_out, | 145 content::PushEventPayload* payload_out, |
124 const std::string& app_id, | 146 const std::string& app_id, |
125 const GURL& origin, | 147 const GURL& origin, |
126 int64_t service_worker_registration_id, | 148 int64_t service_worker_registration_id, |
127 const content::PushEventPayload& payload) { | 149 const content::PushEventPayload& payload) { |
128 *app_id_out = app_id; | 150 *app_id_out = app_id; |
129 *origin_out = origin; | 151 *origin_out = origin; |
130 *service_worker_registration_id_out = service_worker_registration_id; | 152 *service_worker_registration_id_out = service_worker_registration_id; |
131 *payload_out = payload; | 153 *payload_out = payload; |
132 } | 154 } |
133 | 155 |
134 protected: | 156 protected: |
135 PushMessagingTestingProfile* profile() { return &profile_; } | 157 PushMessagingTestingProfile* profile() { return &profile_; } |
136 | 158 |
137 private: | 159 private: |
138 content::TestBrowserThreadBundle thread_bundle_; | 160 content::TestBrowserThreadBundle thread_bundle_; |
139 PushMessagingTestingProfile profile_; | 161 PushMessagingTestingProfile profile_; |
162 base::FieldTrialList field_trial_list_; | |
140 }; | 163 }; |
141 | 164 |
142 TEST_F(PushMessagingServiceTest, PayloadEncryptionTest) { | 165 TEST_F(PushMessagingServiceTest, PayloadEncryptionTest) { |
143 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); | 166 PushMessagingServiceImpl* push_service = profile()->GetPushMessagingService(); |
144 ASSERT_TRUE(push_service); | 167 ASSERT_TRUE(push_service); |
145 | 168 |
146 const GURL origin(kTestOrigin); | 169 const GURL origin(kTestOrigin); |
147 | 170 |
148 // (1) Make sure that |kExampleOrigin| has access to use Push Messaging. | 171 // (1) Make sure that |kExampleOrigin| has access to use Push Messaging. |
149 ASSERT_EQ(blink::WebPushPermissionStatusGranted, | 172 ASSERT_EQ(blink::WebPushPermissionStatusGranted, |
150 push_service->GetPermissionStatus(origin, true)); | 173 push_service->GetPermissionStatus(origin, true)); |
151 | 174 |
152 std::string subscription_id; | 175 std::string subscription_id; |
153 std::vector<uint8_t> p256dh, auth; | 176 std::vector<uint8_t> p256dh, auth; |
154 | 177 |
178 base::RunLoop run_loop; | |
179 | |
155 // (2) Subscribe for Push Messaging, and verify that we've got the required | 180 // (2) Subscribe for Push Messaging, and verify that we've got the required |
156 // information in order to be able to create encrypted messages. | 181 // information in order to be able to create encrypted messages. |
157 content::PushSubscriptionOptions options; | 182 content::PushSubscriptionOptions options; |
158 options.user_visible_only = true; | 183 options.user_visible_only = true; |
159 options.sender_info = kTestSenderId; | 184 options.sender_info = kTestSenderId; |
160 push_service->SubscribeFromWorker( | 185 push_service->SubscribeFromWorker( |
161 origin, kTestServiceWorkerId, options, | 186 origin, kTestServiceWorkerId, options, |
162 base::Bind(&PushMessagingServiceTest::DidRegister, base::Unretained(this), | 187 base::Bind(&PushMessagingServiceTest::DidRegister, base::Unretained(this), |
163 &subscription_id, &p256dh, &auth)); | 188 &subscription_id, &p256dh, &auth, run_loop.QuitClosure())); |
164 | 189 |
165 EXPECT_EQ(0u, subscription_id.size()); // this must be asynchronous | 190 EXPECT_EQ(0u, subscription_id.size()); // this must be asynchronous |
166 | 191 |
167 base::RunLoop().RunUntilIdle(); | 192 run_loop.Run(); |
168 | 193 |
169 ASSERT_GT(subscription_id.size(), 0u); | 194 ASSERT_GT(subscription_id.size(), 0u); |
170 ASSERT_GT(p256dh.size(), 0u); | 195 ASSERT_GT(p256dh.size(), 0u); |
171 ASSERT_GT(auth.size(), 0u); | 196 ASSERT_GT(auth.size(), 0u); |
172 | 197 |
173 // (3) Encrypt a message using the public key and authentication secret that | 198 // (3) Encrypt a message using the public key and authentication secret that |
174 // are associated with the subscription. | 199 // are associated with the subscription. |
175 | 200 |
176 gcm::IncomingMessage message; | 201 gcm::IncomingMessage message; |
177 message.sender_id = kTestSenderId; | 202 message.sender_id = kTestSenderId; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 // (6) Verify that the message, as received by the Push Messaging Service, has | 241 // (6) Verify that the message, as received by the Push Messaging Service, has |
217 // indeed been decrypted by the GCM Driver, and has been forwarded to the | 242 // indeed been decrypted by the GCM Driver, and has been forwarded to the |
218 // Service Worker that has been associated with the subscription. | 243 // Service Worker that has been associated with the subscription. |
219 EXPECT_EQ(app_identifier.app_id(), app_id); | 244 EXPECT_EQ(app_identifier.app_id(), app_id); |
220 EXPECT_EQ(origin, dispatched_origin); | 245 EXPECT_EQ(origin, dispatched_origin); |
221 EXPECT_EQ(service_worker_registration_id, kTestServiceWorkerId); | 246 EXPECT_EQ(service_worker_registration_id, kTestServiceWorkerId); |
222 | 247 |
223 EXPECT_FALSE(payload.is_null); | 248 EXPECT_FALSE(payload.is_null); |
224 EXPECT_EQ(kTestPayload, payload.data); | 249 EXPECT_EQ(kTestPayload, payload.data); |
225 } | 250 } |
OLD | NEW |