OLD | NEW |
---|---|
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 "components/gcm_driver/gcm_driver_desktop.h" | 5 #include "components/gcm_driver/gcm_driver_desktop.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 FakeGCMConnectionObserver* gcm_connection_observer() { | 113 FakeGCMConnectionObserver* gcm_connection_observer() { |
114 return gcm_connection_observer_.get(); | 114 return gcm_connection_observer_.get(); |
115 } | 115 } |
116 const std::string& registration_id() const { return registration_id_; } | 116 const std::string& registration_id() const { return registration_id_; } |
117 GCMClient::Result registration_result() const { return registration_result_; } | 117 GCMClient::Result registration_result() const { return registration_result_; } |
118 const std::string& send_message_id() const { return send_message_id_; } | 118 const std::string& send_message_id() const { return send_message_id_; } |
119 GCMClient::Result send_result() const { return send_result_; } | 119 GCMClient::Result send_result() const { return send_result_; } |
120 GCMClient::Result unregistration_result() const { | 120 GCMClient::Result unregistration_result() const { |
121 return unregistration_result_; | 121 return unregistration_result_; |
122 } | 122 } |
123 const std::string& p256dh() const { return p256dh_; } | |
124 const std::string& auth_secret() const { return auth_secret_; } | |
123 | 125 |
124 void PumpIOLoop(); | 126 void PumpIOLoop(); |
125 | 127 |
126 void ClearResults(); | 128 void ClearResults(); |
127 | 129 |
128 bool HasAppHandlers() const; | 130 bool HasAppHandlers() const; |
129 FakeGCMClient* GetGCMClient(); | 131 FakeGCMClient* GetGCMClient(); |
130 | 132 |
131 void CreateDriver(); | 133 void CreateDriver(); |
132 void ShutdownDriver(); | 134 void ShutdownDriver(); |
133 void AddAppHandlers(); | 135 void AddAppHandlers(); |
134 void RemoveAppHandlers(); | 136 void RemoveAppHandlers(); |
135 | 137 |
136 void Register(const std::string& app_id, | 138 void Register(const std::string& app_id, |
137 const std::vector<std::string>& sender_ids, | 139 const std::vector<std::string>& sender_ids, |
138 WaitToFinish wait_to_finish); | 140 WaitToFinish wait_to_finish); |
139 void Send(const std::string& app_id, | 141 void Send(const std::string& app_id, |
140 const std::string& receiver_id, | 142 const std::string& receiver_id, |
141 const OutgoingMessage& message, | 143 const OutgoingMessage& message, |
142 WaitToFinish wait_to_finish); | 144 WaitToFinish wait_to_finish); |
145 void GetEncryptionInfo(const std::string& app_id, | |
146 WaitToFinish wait_to_finish); | |
143 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); | 147 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); |
144 | 148 |
145 void WaitForAsyncOperation(); | 149 void WaitForAsyncOperation(); |
146 | 150 |
147 void RegisterCompleted(const std::string& registration_id, | 151 void RegisterCompleted(const std::string& registration_id, |
148 GCMClient::Result result); | 152 GCMClient::Result result); |
149 void SendCompleted(const std::string& message_id, GCMClient::Result result); | 153 void SendCompleted(const std::string& message_id, GCMClient::Result result); |
154 void GetEncryptionInfoCompleted(const std::string& p256dh, | |
155 const std::string& auth_secret); | |
150 void UnregisterCompleted(GCMClient::Result result); | 156 void UnregisterCompleted(GCMClient::Result result); |
151 | 157 |
152 const base::Closure& async_operation_completed_callback() const { | 158 const base::Closure& async_operation_completed_callback() const { |
153 return async_operation_completed_callback_; | 159 return async_operation_completed_callback_; |
154 } | 160 } |
155 void set_async_operation_completed_callback(const base::Closure& callback) { | 161 void set_async_operation_completed_callback(const base::Closure& callback) { |
156 async_operation_completed_callback_ = callback; | 162 async_operation_completed_callback_ = callback; |
157 } | 163 } |
158 | 164 |
159 private: | 165 private: |
160 base::ScopedTempDir temp_dir_; | 166 base::ScopedTempDir temp_dir_; |
161 TestingPrefServiceSimple prefs_; | 167 TestingPrefServiceSimple prefs_; |
162 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
163 base::MessageLoopForUI message_loop_; | 168 base::MessageLoopForUI message_loop_; |
164 base::Thread io_thread_; | 169 base::Thread io_thread_; |
165 base::FieldTrialList field_trial_list_; | 170 base::FieldTrialList field_trial_list_; |
166 scoped_ptr<GCMDriverDesktop> driver_; | 171 scoped_ptr<GCMDriverDesktop> driver_; |
167 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | 172 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; |
168 scoped_ptr<FakeGCMConnectionObserver> gcm_connection_observer_; | 173 scoped_ptr<FakeGCMConnectionObserver> gcm_connection_observer_; |
169 | 174 |
170 base::Closure async_operation_completed_callback_; | 175 base::Closure async_operation_completed_callback_; |
171 | 176 |
172 std::string registration_id_; | 177 std::string registration_id_; |
173 GCMClient::Result registration_result_; | 178 GCMClient::Result registration_result_; |
174 std::string send_message_id_; | 179 std::string send_message_id_; |
175 GCMClient::Result send_result_; | 180 GCMClient::Result send_result_; |
176 GCMClient::Result unregistration_result_; | 181 GCMClient::Result unregistration_result_; |
182 std::string p256dh_; | |
183 std::string auth_secret_; | |
177 | 184 |
178 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); | 185 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); |
179 }; | 186 }; |
180 | 187 |
181 GCMDriverTest::GCMDriverTest() | 188 GCMDriverTest::GCMDriverTest() |
182 : task_runner_(new base::TestSimpleTaskRunner()), | 189 : io_thread_("IOThread"), |
183 io_thread_("IOThread"), | |
184 field_trial_list_(NULL), | 190 field_trial_list_(NULL), |
185 registration_result_(GCMClient::UNKNOWN_ERROR), | 191 registration_result_(GCMClient::UNKNOWN_ERROR), |
186 send_result_(GCMClient::UNKNOWN_ERROR), | 192 send_result_(GCMClient::UNKNOWN_ERROR), |
187 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | 193 unregistration_result_(GCMClient::UNKNOWN_ERROR) { |
188 } | 194 } |
189 | 195 |
190 GCMDriverTest::~GCMDriverTest() { | 196 GCMDriverTest::~GCMDriverTest() { |
191 } | 197 } |
192 | 198 |
193 void GCMDriverTest::SetUp() { | 199 void GCMDriverTest::SetUp() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 return !driver_->app_handlers().empty(); | 234 return !driver_->app_handlers().empty(); |
229 } | 235 } |
230 | 236 |
231 FakeGCMClient* GCMDriverTest::GetGCMClient() { | 237 FakeGCMClient* GCMDriverTest::GetGCMClient() { |
232 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); | 238 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); |
233 } | 239 } |
234 | 240 |
235 void GCMDriverTest::CreateDriver() { | 241 void GCMDriverTest::CreateDriver() { |
236 scoped_refptr<net::URLRequestContextGetter> request_context = | 242 scoped_refptr<net::URLRequestContextGetter> request_context = |
237 new net::TestURLRequestContextGetter(io_thread_.task_runner()); | 243 new net::TestURLRequestContextGetter(io_thread_.task_runner()); |
238 // TODO(johnme): Need equivalent test coverage of GCMDriverAndroid. | |
239 driver_.reset(new GCMDriverDesktop( | 244 driver_.reset(new GCMDriverDesktop( |
240 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( | 245 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( |
241 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner())), | 246 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner())), |
242 GCMClient::ChromeBuildInfo(), "http://channel.status.request.url", | 247 GCMClient::ChromeBuildInfo(), "http://channel.status.request.url", |
243 "user-agent-string", &prefs_, temp_dir_.path(), request_context, | 248 "user-agent-string", &prefs_, temp_dir_.path(), request_context, |
244 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner(), | 249 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner(), |
245 task_runner_)); | 250 message_loop_.task_runner())); |
246 | 251 |
247 gcm_app_handler_.reset(new FakeGCMAppHandler); | 252 gcm_app_handler_.reset(new FakeGCMAppHandler); |
248 gcm_connection_observer_.reset(new FakeGCMConnectionObserver); | 253 gcm_connection_observer_.reset(new FakeGCMConnectionObserver); |
249 | 254 |
250 driver_->AddConnectionObserver(gcm_connection_observer_.get()); | 255 driver_->AddConnectionObserver(gcm_connection_observer_.get()); |
251 } | 256 } |
252 | 257 |
253 void GCMDriverTest::ShutdownDriver() { | 258 void GCMDriverTest::ShutdownDriver() { |
254 if (gcm_connection_observer()) | 259 if (gcm_connection_observer()) |
255 driver()->RemoveConnectionObserver(gcm_connection_observer()); | 260 driver()->RemoveConnectionObserver(gcm_connection_observer()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 async_operation_completed_callback_ = run_loop.QuitClosure(); | 292 async_operation_completed_callback_ = run_loop.QuitClosure(); |
288 driver_->Send(app_id, | 293 driver_->Send(app_id, |
289 receiver_id, | 294 receiver_id, |
290 message, | 295 message, |
291 base::Bind(&GCMDriverTest::SendCompleted, | 296 base::Bind(&GCMDriverTest::SendCompleted, |
292 base::Unretained(this))); | 297 base::Unretained(this))); |
293 if (wait_to_finish == WAIT) | 298 if (wait_to_finish == WAIT) |
294 run_loop.Run(); | 299 run_loop.Run(); |
295 } | 300 } |
296 | 301 |
302 void GCMDriverTest::GetEncryptionInfo(const std::string& app_id, | |
303 WaitToFinish wait_to_finish) { | |
304 base::RunLoop run_loop; | |
305 async_operation_completed_callback_ = run_loop.QuitClosure(); | |
306 driver_->GetEncryptionInfo( | |
307 app_id, base::Bind(&GCMDriverTest::GetEncryptionInfoCompleted, | |
308 base::Unretained(this))); | |
309 if (wait_to_finish == WAIT) | |
310 run_loop.Run(); | |
311 } | |
312 | |
297 void GCMDriverTest::Unregister(const std::string& app_id, | 313 void GCMDriverTest::Unregister(const std::string& app_id, |
298 WaitToFinish wait_to_finish) { | 314 WaitToFinish wait_to_finish) { |
299 base::RunLoop run_loop; | 315 base::RunLoop run_loop; |
300 async_operation_completed_callback_ = run_loop.QuitClosure(); | 316 async_operation_completed_callback_ = run_loop.QuitClosure(); |
301 driver_->Unregister(app_id, | 317 driver_->Unregister(app_id, |
302 base::Bind(&GCMDriverTest::UnregisterCompleted, | 318 base::Bind(&GCMDriverTest::UnregisterCompleted, |
303 base::Unretained(this))); | 319 base::Unretained(this))); |
304 if (wait_to_finish == WAIT) | 320 if (wait_to_finish == WAIT) |
305 run_loop.Run(); | 321 run_loop.Run(); |
306 } | 322 } |
(...skipping 13 matching lines...) Expand all Loading... | |
320 } | 336 } |
321 | 337 |
322 void GCMDriverTest::SendCompleted(const std::string& message_id, | 338 void GCMDriverTest::SendCompleted(const std::string& message_id, |
323 GCMClient::Result result) { | 339 GCMClient::Result result) { |
324 send_message_id_ = message_id; | 340 send_message_id_ = message_id; |
325 send_result_ = result; | 341 send_result_ = result; |
326 if (!async_operation_completed_callback_.is_null()) | 342 if (!async_operation_completed_callback_.is_null()) |
327 async_operation_completed_callback_.Run(); | 343 async_operation_completed_callback_.Run(); |
328 } | 344 } |
329 | 345 |
346 void GCMDriverTest::GetEncryptionInfoCompleted(const std::string& p256dh, | |
347 const std::string& auth_secret) { | |
348 p256dh_ = p256dh; | |
349 auth_secret_ = auth_secret; | |
350 if (!async_operation_completed_callback_.is_null()) | |
johnme
2016/02/16 18:36:13
FWIW, this can never be null, since you never rese
Peter Beverloo
2016/02/16 21:50:50
Just being consistent with the rest of the file -
| |
351 async_operation_completed_callback_.Run(); | |
352 } | |
353 | |
330 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { | 354 void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) { |
331 unregistration_result_ = result; | 355 unregistration_result_ = result; |
332 if (!async_operation_completed_callback_.is_null()) | 356 if (!async_operation_completed_callback_.is_null()) |
333 async_operation_completed_callback_.Run(); | 357 async_operation_completed_callback_.Run(); |
334 } | 358 } |
335 | 359 |
336 TEST_F(GCMDriverTest, Create) { | 360 TEST_F(GCMDriverTest, Create) { |
337 // Create GCMDriver first. By default GCM is set to delay start. | 361 // Create GCMDriver first. By default GCM is set to delay start. |
338 CreateDriver(); | 362 CreateDriver(); |
339 EXPECT_FALSE(driver()->IsStarted()); | 363 EXPECT_FALSE(driver()->IsStarted()); |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
651 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | 675 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); |
652 | 676 |
653 EXPECT_FALSE(registration_id().empty()); | 677 EXPECT_FALSE(registration_id().empty()); |
654 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | 678 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); |
655 | 679 |
656 Unregister(kTestAppID1, GCMDriverTest::WAIT); | 680 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
657 | 681 |
658 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | 682 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); |
659 } | 683 } |
660 | 684 |
685 TEST_F(GCMDriverFunctionalTest, UnregisterRemovesEncryptionInfo) { | |
686 std::vector<std::string> sender_ids; | |
687 sender_ids.push_back("sender1"); | |
688 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT); | |
689 | |
690 EXPECT_FALSE(registration_id().empty()); | |
691 EXPECT_EQ(GCMClient::SUCCESS, registration_result()); | |
692 | |
693 GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT); | |
694 | |
695 EXPECT_FALSE(p256dh().empty()); | |
696 EXPECT_FALSE(auth_secret().empty()); | |
697 | |
698 const std::string app_p256dh = p256dh(); | |
699 const std::string app_auth_secret = auth_secret(); | |
700 | |
701 GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT); | |
702 | |
703 EXPECT_EQ(app_p256dh, p256dh()); | |
704 EXPECT_EQ(app_auth_secret, auth_secret()); | |
705 | |
706 Unregister(kTestAppID1, GCMDriverTest::WAIT); | |
707 | |
708 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | |
709 | |
710 GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT); | |
711 | |
712 // The GCMKeyStore eagerly creates new keying material for registrations that | |
johnme
2016/02/16 18:36:13
It seems odd (and a potential security foot-gun fo
Peter Beverloo
2016/02/16 21:50:50
There are two requirements that led to this -
(
| |
713 // don't have any associated with them, so the most appropriate check to do is | |
714 // to verify that the returned material is different from before. | |
715 | |
716 EXPECT_NE(app_p256dh, p256dh()); | |
717 EXPECT_NE(app_auth_secret, auth_secret()); | |
718 } | |
719 | |
661 TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) { | 720 TEST_F(GCMDriverFunctionalTest, UnregisterWhenAsyncOperationPending) { |
662 std::vector<std::string> sender_ids; | 721 std::vector<std::string> sender_ids; |
663 sender_ids.push_back("sender1"); | 722 sender_ids.push_back("sender1"); |
664 // First start registration without waiting for it to complete. | 723 // First start registration without waiting for it to complete. |
665 Register(kTestAppID1, sender_ids, GCMDriverTest::DO_NOT_WAIT); | 724 Register(kTestAppID1, sender_ids, GCMDriverTest::DO_NOT_WAIT); |
666 | 725 |
667 // Test that unregistration fails with async operation pending when there is a | 726 // Test that unregistration fails with async operation pending when there is a |
668 // registration already in progress. | 727 // registration already in progress. |
669 Unregister(kTestAppID1, GCMDriverTest::WAIT); | 728 Unregister(kTestAppID1, GCMDriverTest::WAIT); |
670 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | 729 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1313 PumpUILoop(); | 1372 PumpUILoop(); |
1314 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result()); | 1373 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result()); |
1315 | 1374 |
1316 // DeleteToken operation will be invoked after GCMClient becomes ready. | 1375 // DeleteToken operation will be invoked after GCMClient becomes ready. |
1317 GetGCMClient()->PerformDelayedStart(); | 1376 GetGCMClient()->PerformDelayedStart(); |
1318 WaitForAsyncOperation(); | 1377 WaitForAsyncOperation(); |
1319 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); | 1378 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result()); |
1320 } | 1379 } |
1321 | 1380 |
1322 } // namespace gcm | 1381 } // namespace gcm |
OLD | NEW |