blob: a698e2005c09435059fc834e8a2f2b76aac2568a [file] [log] [blame]
[email protected]530f2162014-05-15 01:05:041// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]21b77652014-05-31 01:21:095#include "components/gcm_driver/gcm_driver_desktop.h"
[email protected]530f2162014-05-15 01:05:046
avi26062922015-12-26 00:14:187#include <stdint.h>
8
[email protected]530f2162014-05-15 01:05:049#include "base/bind.h"
10#include "base/bind_helpers.h"
11#include "base/files/scoped_temp_dir.h"
12#include "base/location.h"
avi26062922015-12-26 00:14:1813#include "base/macros.h"
Gabriel Charette0889bbd2018-04-23 22:24:3614#include "base/message_loop/message_loop_current.h"
[email protected]530f2162014-05-15 01:05:0415#include "base/run_loop.h"
16#include "base/strings/string_util.h"
Gabriel Charettec7108742019-08-23 03:31:4017#include "base/test/task_environment.h"
[email protected]1795df51a2014-05-22 20:18:0418#include "base/test/test_simple_task_runner.h"
19#include "base/threading/thread.h"
gab7966d312016-05-11 20:35:0120#include "base/threading/thread_task_runner_handle.h"
peteree284ba52016-02-01 11:53:2821#include "components/gcm_driver/crypto/gcm_encryption_provider.h"
[email protected]8d41e5a2014-05-28 03:18:4922#include "components/gcm_driver/fake_gcm_app_handler.h"
23#include "components/gcm_driver/fake_gcm_client.h"
24#include "components/gcm_driver/fake_gcm_client_factory.h"
25#include "components/gcm_driver/gcm_app_handler.h"
[email protected]530f2162014-05-15 01:05:0426#include "components/gcm_driver/gcm_client_factory.h"
fgorski0d5c00d2014-08-28 16:21:4527#include "components/gcm_driver/gcm_connection_observer.h"
brettw066508682016-02-03 08:22:0228#include "components/prefs/pref_registry_simple.h"
29#include "components/prefs/testing_pref_service.h"
[email protected]530f2162014-05-15 01:05:0430#include "net/url_request/url_request_context_getter.h"
31#include "net/url_request/url_request_test_util.h"
Mark Pilgrim7634f5b52018-06-27 19:53:2732#include "services/network/public/cpp/shared_url_loader_factory.h"
Maks Orlovichc70c93c2018-07-12 02:45:4433#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
Robbie McElrathb01499332018-09-25 00:53:1334#include "services/network/test/test_network_connection_tracker.h"
Maks Orlovichc70c93c2018-07-12 02:45:4435#include "services/network/test/test_url_loader_factory.h"
[email protected]530f2162014-05-15 01:05:0436#include "testing/gtest/include/gtest/gtest.h"
37
38namespace gcm {
39
40namespace {
41
[email protected]530f2162014-05-15 01:05:0442const char kTestAppID1[] = "TestApp1";
43const char kTestAppID2[] = "TestApp2";
44const char kUserID1[] = "user1";
jianli012b5c82015-05-28 01:41:2945const char kScope[] = "GCM";
jianliea8534872015-06-22 21:06:2246const char kInstanceID1[] = "IID1";
47const char kInstanceID2[] = "IID2";
[email protected]530f2162014-05-15 01:05:0448
fgorski0d5c00d2014-08-28 16:21:4549class FakeGCMConnectionObserver : public GCMConnectionObserver {
50 public:
51 FakeGCMConnectionObserver();
dcheng00ea022b2014-10-21 11:24:5652 ~FakeGCMConnectionObserver() override;
fgorski0d5c00d2014-08-28 16:21:4553
54 // gcm::GCMConnectionObserver implementation:
dcheng00ea022b2014-10-21 11:24:5655 void OnConnected(const net::IPEndPoint& ip_endpoint) override;
56 void OnDisconnected() override;
fgorski0d5c00d2014-08-28 16:21:4557
58 bool connected() const { return connected_; }
59
60 private:
61 bool connected_;
62};
63
64FakeGCMConnectionObserver::FakeGCMConnectionObserver() : connected_(false) {
65}
66
67FakeGCMConnectionObserver::~FakeGCMConnectionObserver() {
68}
69
70void FakeGCMConnectionObserver::OnConnected(
71 const net::IPEndPoint& ip_endpoint) {
72 connected_ = true;
73}
74
75void FakeGCMConnectionObserver::OnDisconnected() {
76 connected_ = false;
77}
78
[email protected]530f2162014-05-15 01:05:0479void PumpCurrentLoop() {
Gabriel Charettebfad9ffe2018-04-27 19:55:2580 base::RunLoop(base::RunLoop::Type::kNestableTasksAllowed).RunUntilIdle();
[email protected]530f2162014-05-15 01:05:0481}
82
83void PumpUILoop() {
84 PumpCurrentLoop();
85}
86
[email protected]530f2162014-05-15 01:05:0487std::vector<std::string> ToSenderList(const std::string& sender_ids) {
brettwb45192d2015-06-29 22:53:2488 return base::SplitString(
89 sender_ids, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
[email protected]530f2162014-05-15 01:05:0490}
91
[email protected]530f2162014-05-15 01:05:0492} // namespace
93
94class GCMDriverTest : public testing::Test {
95 public:
96 enum WaitToFinish {
97 DO_NOT_WAIT,
98 WAIT
99 };
100
101 GCMDriverTest();
dcheng30a1b1542014-10-29 21:27:50102 ~GCMDriverTest() override;
[email protected]530f2162014-05-15 01:05:04103
104 // testing::Test:
dcheng30a1b1542014-10-29 21:27:50105 void SetUp() override;
106 void TearDown() override;
[email protected]530f2162014-05-15 01:05:04107
jianli2dc910b02014-09-19 02:42:46108 GCMDriverDesktop* driver() { return driver_.get(); }
[email protected]530f2162014-05-15 01:05:04109 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
fgorski0d5c00d2014-08-28 16:21:45110 FakeGCMConnectionObserver* gcm_connection_observer() {
111 return gcm_connection_observer_.get();
112 }
[email protected]530f2162014-05-15 01:05:04113 const std::string& registration_id() const { return registration_id_; }
114 GCMClient::Result registration_result() const { return registration_result_; }
115 const std::string& send_message_id() const { return send_message_id_; }
116 GCMClient::Result send_result() const { return send_result_; }
117 GCMClient::Result unregistration_result() const {
118 return unregistration_result_;
119 }
petera4795ae832016-02-17 11:35:27120 const std::string& p256dh() const { return p256dh_; }
121 const std::string& auth_secret() const { return auth_secret_; }
[email protected]530f2162014-05-15 01:05:04122
[email protected]1795df51a2014-05-22 20:18:04123 void PumpIOLoop();
124
[email protected]9d7e5c02014-05-21 03:09:03125 void ClearResults();
[email protected]530f2162014-05-15 01:05:04126
127 bool HasAppHandlers() const;
[email protected]accbbc92014-05-15 21:28:59128 FakeGCMClient* GetGCMClient();
[email protected]530f2162014-05-15 01:05:04129
jianlif3e52af42015-01-21 23:18:47130 void CreateDriver();
jianli2dc910b02014-09-19 02:42:46131 void ShutdownDriver();
[email protected]3ecb2b582014-05-23 00:25:39132 void AddAppHandlers();
133 void RemoveAppHandlers();
[email protected]530f2162014-05-15 01:05:04134
[email protected]530f2162014-05-15 01:05:04135 void Register(const std::string& app_id,
136 const std::vector<std::string>& sender_ids,
137 WaitToFinish wait_to_finish);
138 void Send(const std::string& app_id,
139 const std::string& receiver_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06140 const OutgoingMessage& message,
[email protected]530f2162014-05-15 01:05:04141 WaitToFinish wait_to_finish);
petera4795ae832016-02-17 11:35:27142 void GetEncryptionInfo(const std::string& app_id,
143 WaitToFinish wait_to_finish);
[email protected]530f2162014-05-15 01:05:04144 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
145
146 void WaitForAsyncOperation();
147
[email protected]530f2162014-05-15 01:05:04148 void RegisterCompleted(const std::string& registration_id,
149 GCMClient::Result result);
150 void SendCompleted(const std::string& message_id, GCMClient::Result result);
Peter Beverloo9635b662019-07-10 19:05:18151 void GetEncryptionInfoCompleted(std::string p256dh, std::string auth_secret);
[email protected]530f2162014-05-15 01:05:04152 void UnregisterCompleted(GCMClient::Result result);
153
Reilly Grantaa7bc2f2020-01-31 11:49:29154 void AsyncOperationCompleted() {
155 if (async_operation_completed_callback_)
156 std::move(async_operation_completed_callback_).Run();
jianliea8534872015-06-22 21:06:22157 }
Reilly Grantaa7bc2f2020-01-31 11:49:29158 void set_async_operation_completed_callback(base::OnceClosure callback) {
159 async_operation_completed_callback_ = std::move(callback);
jianli012b5c82015-05-28 01:41:29160 }
161
162 private:
[email protected]9d7e5c02014-05-21 03:09:03163 base::ScopedTempDir temp_dir_;
jianli2dc910b02014-09-19 02:42:46164 TestingPrefServiceSimple prefs_;
Gabriel Charette1e59e47d2019-09-12 18:10:22165 base::test::SingleThreadTaskEnvironment task_environment_{
166 base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
[email protected]1795df51a2014-05-22 20:18:04167 base::Thread io_thread_;
Maks Orlovichc70c93c2018-07-12 02:45:44168 network::TestURLLoaderFactory test_url_loader_factory_;
169
dchenga77e28eb2016-04-21 21:34:37170 std::unique_ptr<GCMDriverDesktop> driver_;
171 std::unique_ptr<FakeGCMAppHandler> gcm_app_handler_;
172 std::unique_ptr<FakeGCMConnectionObserver> gcm_connection_observer_;
[email protected]530f2162014-05-15 01:05:04173
Reilly Grantaa7bc2f2020-01-31 11:49:29174 base::OnceClosure async_operation_completed_callback_;
[email protected]530f2162014-05-15 01:05:04175
176 std::string registration_id_;
177 GCMClient::Result registration_result_;
178 std::string send_message_id_;
179 GCMClient::Result send_result_;
180 GCMClient::Result unregistration_result_;
petera4795ae832016-02-17 11:35:27181 std::string p256dh_;
182 std::string auth_secret_;
[email protected]530f2162014-05-15 01:05:04183
184 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
185};
186
187GCMDriverTest::GCMDriverTest()
petera4795ae832016-02-17 11:35:27188 : io_thread_("IOThread"),
[email protected]530f2162014-05-15 01:05:04189 registration_result_(GCMClient::UNKNOWN_ERROR),
190 send_result_(GCMClient::UNKNOWN_ERROR),
Maks Orlovichc70c93c2018-07-12 02:45:44191 unregistration_result_(GCMClient::UNKNOWN_ERROR) {}
[email protected]530f2162014-05-15 01:05:04192
193GCMDriverTest::~GCMDriverTest() {
194}
195
196void GCMDriverTest::SetUp() {
[email protected]1795df51a2014-05-22 20:18:04197 io_thread_.Start();
[email protected]9d7e5c02014-05-21 03:09:03198 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]530f2162014-05-15 01:05:04199}
200
201void GCMDriverTest::TearDown() {
202 if (!driver_)
203 return;
204
jianli2dc910b02014-09-19 02:42:46205 ShutdownDriver();
[email protected]530f2162014-05-15 01:05:04206 driver_.reset();
207 PumpIOLoop();
[email protected]1795df51a2014-05-22 20:18:04208
209 io_thread_.Stop();
Greg Thompson527a1a12019-11-28 14:23:18210 task_environment_.RunUntilIdle();
Greg Thompson28dd8462019-11-29 13:28:07211 ASSERT_TRUE(temp_dir_.Delete());
[email protected]1795df51a2014-05-22 20:18:04212}
213
214void GCMDriverTest::PumpIOLoop() {
215 base::RunLoop run_loop;
skyostilb0daa012015-06-02 19:03:48216 io_thread_.task_runner()->PostTaskAndReply(
tzik2bcf8e42018-07-31 11:22:15217 FROM_HERE, base::BindOnce(&PumpCurrentLoop), run_loop.QuitClosure());
[email protected]1795df51a2014-05-22 20:18:04218 run_loop.Run();
[email protected]530f2162014-05-15 01:05:04219}
220
[email protected]9d7e5c02014-05-21 03:09:03221void GCMDriverTest::ClearResults() {
[email protected]530f2162014-05-15 01:05:04222 registration_id_.clear();
223 registration_result_ = GCMClient::UNKNOWN_ERROR;
[email protected]530f2162014-05-15 01:05:04224
[email protected]9d7e5c02014-05-21 03:09:03225 send_message_id_.clear();
226 send_result_ = GCMClient::UNKNOWN_ERROR;
227
[email protected]530f2162014-05-15 01:05:04228 unregistration_result_ = GCMClient::UNKNOWN_ERROR;
229}
230
231bool GCMDriverTest::HasAppHandlers() const {
232 return !driver_->app_handlers().empty();
233}
234
[email protected]accbbc92014-05-15 21:28:59235FakeGCMClient* GCMDriverTest::GetGCMClient() {
236 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
[email protected]530f2162014-05-15 01:05:04237}
238
jianlif3e52af42015-01-21 23:18:47239void GCMDriverTest::CreateDriver() {
[email protected]530f2162014-05-15 01:05:04240 scoped_refptr<net::URLRequestContextGetter> request_context =
skyostilb0daa012015-06-02 19:03:48241 new net::TestURLRequestContextGetter(io_thread_.task_runner());
johnme627dc8c72016-08-19 21:49:39242 GCMClient::ChromeBuildInfo chrome_build_info;
243 chrome_build_info.product_category_for_subtypes = "com.chrome.macosx";
Helen Li5f3d96a2018-08-10 20:37:24244 driver_ = std::make_unique<GCMDriverDesktop>(
dchenga77e28eb2016-04-21 21:34:37245 std::unique_ptr<GCMClientFactory>(new FakeGCMClientFactory(
dcheng51606352015-12-26 21:16:23246 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner())),
Marc Treibae8cac692020-01-28 12:10:12247 chrome_build_info, "user-agent-string", &prefs_, temp_dir_.GetPath(),
Mihai Sardarescud260bd262019-12-10 16:40:25248 /*remove_account_mappings_with_email_key=*/true, base::DoNothing(),
Maks Orlovichc70c93c2018-07-12 02:45:44249 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
250 &test_url_loader_factory_),
Robbie McElrathb01499332018-09-25 00:53:13251 network::TestNetworkConnectionTracker::GetInstance(),
skyostilb0daa012015-06-02 19:03:48252 base::ThreadTaskRunnerHandle::Get(), io_thread_.task_runner(),
Carlos Caballero1eae25d2018-12-13 18:44:15253 task_environment_.GetMainThreadTaskRunner());
[email protected]530f2162014-05-15 01:05:04254
255 gcm_app_handler_.reset(new FakeGCMAppHandler);
fgorski0d5c00d2014-08-28 16:21:45256 gcm_connection_observer_.reset(new FakeGCMConnectionObserver);
257
258 driver_->AddConnectionObserver(gcm_connection_observer_.get());
[email protected]3ecb2b582014-05-23 00:25:39259}
260
jianli2dc910b02014-09-19 02:42:46261void GCMDriverTest::ShutdownDriver() {
262 if (gcm_connection_observer())
263 driver()->RemoveConnectionObserver(gcm_connection_observer());
264 driver()->Shutdown();
265}
266
[email protected]3ecb2b582014-05-23 00:25:39267void GCMDriverTest::AddAppHandlers() {
[email protected]530f2162014-05-15 01:05:04268 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
269 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
270}
271
[email protected]3ecb2b582014-05-23 00:25:39272void GCMDriverTest::RemoveAppHandlers() {
273 driver_->RemoveAppHandler(kTestAppID1);
274 driver_->RemoveAppHandler(kTestAppID2);
275}
276
[email protected]530f2162014-05-15 01:05:04277void GCMDriverTest::Register(const std::string& app_id,
278 const std::vector<std::string>& sender_ids,
279 WaitToFinish wait_to_finish) {
280 base::RunLoop run_loop;
281 async_operation_completed_callback_ = run_loop.QuitClosure();
Reilly Grantaa7bc2f2020-01-31 11:49:29282 driver_->Register(app_id, sender_ids,
283 base::BindOnce(&GCMDriverTest::RegisterCompleted,
284 base::Unretained(this)));
[email protected]530f2162014-05-15 01:05:04285 if (wait_to_finish == WAIT)
286 run_loop.Run();
287}
288
289void GCMDriverTest::Send(const std::string& app_id,
290 const std::string& receiver_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06291 const OutgoingMessage& message,
[email protected]530f2162014-05-15 01:05:04292 WaitToFinish wait_to_finish) {
293 base::RunLoop run_loop;
294 async_operation_completed_callback_ = run_loop.QuitClosure();
Reilly Grantaa7bc2f2020-01-31 11:49:29295 driver_->Send(
296 app_id, receiver_id, message,
297 base::BindOnce(&GCMDriverTest::SendCompleted, base::Unretained(this)));
[email protected]530f2162014-05-15 01:05:04298 if (wait_to_finish == WAIT)
299 run_loop.Run();
300}
301
petera4795ae832016-02-17 11:35:27302void 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(
Peter Beverloo9635b662019-07-10 19:05:18307 app_id, base::BindOnce(&GCMDriverTest::GetEncryptionInfoCompleted,
308 base::Unretained(this)));
petera4795ae832016-02-17 11:35:27309 if (wait_to_finish == WAIT)
310 run_loop.Run();
311}
312
[email protected]530f2162014-05-15 01:05:04313void GCMDriverTest::Unregister(const std::string& app_id,
314 WaitToFinish wait_to_finish) {
315 base::RunLoop run_loop;
316 async_operation_completed_callback_ = run_loop.QuitClosure();
317 driver_->Unregister(app_id,
Reilly Grantaa7bc2f2020-01-31 11:49:29318 base::BindOnce(&GCMDriverTest::UnregisterCompleted,
319 base::Unretained(this)));
[email protected]530f2162014-05-15 01:05:04320 if (wait_to_finish == WAIT)
321 run_loop.Run();
322}
323
324void GCMDriverTest::WaitForAsyncOperation() {
325 base::RunLoop run_loop;
326 async_operation_completed_callback_ = run_loop.QuitClosure();
327 run_loop.Run();
328}
329
330void GCMDriverTest::RegisterCompleted(const std::string& registration_id,
331 GCMClient::Result result) {
332 registration_id_ = registration_id;
333 registration_result_ = result;
Reilly Grantaa7bc2f2020-01-31 11:49:29334 AsyncOperationCompleted();
[email protected]530f2162014-05-15 01:05:04335}
336
337void GCMDriverTest::SendCompleted(const std::string& message_id,
338 GCMClient::Result result) {
339 send_message_id_ = message_id;
340 send_result_ = result;
Reilly Grantaa7bc2f2020-01-31 11:49:29341 AsyncOperationCompleted();
[email protected]530f2162014-05-15 01:05:04342}
343
Peter Beverloo9635b662019-07-10 19:05:18344void GCMDriverTest::GetEncryptionInfoCompleted(std::string p256dh,
345 std::string auth_secret) {
346 p256dh_ = std::move(p256dh);
347 auth_secret_ = std::move(auth_secret);
Reilly Grantaa7bc2f2020-01-31 11:49:29348 AsyncOperationCompleted();
petera4795ae832016-02-17 11:35:27349}
350
[email protected]530f2162014-05-15 01:05:04351void GCMDriverTest::UnregisterCompleted(GCMClient::Result result) {
352 unregistration_result_ = result;
Reilly Grantaa7bc2f2020-01-31 11:49:29353 AsyncOperationCompleted();
[email protected]530f2162014-05-15 01:05:04354}
355
[email protected]da54623f2014-06-17 17:03:55356TEST_F(GCMDriverTest, Create) {
jianlif3e52af42015-01-21 23:18:47357 // Create GCMDriver first. By default GCM is set to delay start.
358 CreateDriver();
[email protected]530f2162014-05-15 01:05:04359 EXPECT_FALSE(driver()->IsStarted());
360
jianlif3e52af42015-01-21 23:18:47361 // Adding an app handler will not start GCM.
[email protected]3ecb2b582014-05-23 00:25:39362 AddAppHandlers();
[email protected]fc6078a2014-06-14 08:28:43363 PumpIOLoop();
jianlif3e52af42015-01-21 23:18:47364 PumpUILoop();
365 EXPECT_FALSE(driver()->IsStarted());
366 EXPECT_FALSE(driver()->IsConnected());
367 EXPECT_FALSE(gcm_connection_observer()->connected());
368
369 // The GCM registration will kick off the GCM.
370 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
371 EXPECT_TRUE(driver()->IsStarted());
[email protected]fc6078a2014-06-14 08:28:43372 EXPECT_TRUE(driver()->IsConnected());
fgorski0d5c00d2014-08-28 16:21:45373 EXPECT_TRUE(gcm_connection_observer()->connected());
[email protected]530f2162014-05-15 01:05:04374}
375
[email protected]530f2162014-05-15 01:05:04376TEST_F(GCMDriverTest, Shutdown) {
jianlif3e52af42015-01-21 23:18:47377 CreateDriver();
[email protected]3ecb2b582014-05-23 00:25:39378 EXPECT_FALSE(HasAppHandlers());
379
380 AddAppHandlers();
[email protected]530f2162014-05-15 01:05:04381 EXPECT_TRUE(HasAppHandlers());
382
jianli2dc910b02014-09-19 02:42:46383 ShutdownDriver();
[email protected]530f2162014-05-15 01:05:04384 EXPECT_FALSE(HasAppHandlers());
[email protected]fc6078a2014-06-14 08:28:43385 EXPECT_FALSE(driver()->IsConnected());
fgorski0d5c00d2014-08-28 16:21:45386 EXPECT_FALSE(gcm_connection_observer()->connected());
[email protected]530f2162014-05-15 01:05:04387}
388
[email protected]3ecb2b582014-05-23 00:25:39389TEST_F(GCMDriverTest, StartOrStopGCMOnDemand) {
jianlif3e52af42015-01-21 23:18:47390 CreateDriver();
jianli753282a32015-01-09 02:07:40391 PumpIOLoop();
392 PumpUILoop();
jianlif3e52af42015-01-21 23:18:47393 EXPECT_FALSE(driver()->IsStarted());
[email protected]3ecb2b582014-05-23 00:25:39394
jianlif3e52af42015-01-21 23:18:47395 // Adding an app handler will not start GCM.
[email protected]3ecb2b582014-05-23 00:25:39396 driver()->AddAppHandler(kTestAppID1, gcm_app_handler());
397 PumpIOLoop();
398 PumpUILoop();
jianlif3e52af42015-01-21 23:18:47399 EXPECT_FALSE(driver()->IsStarted());
400
401 // The GCM registration will kick off the GCM.
402 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
jianli19562592015-01-12 20:16:30403 EXPECT_TRUE(driver()->IsStarted());
[email protected]3ecb2b582014-05-23 00:25:39404
405 // Add another app handler.
406 driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
407 PumpIOLoop();
408 PumpUILoop();
jianli19562592015-01-12 20:16:30409 EXPECT_TRUE(driver()->IsStarted());
[email protected]3ecb2b582014-05-23 00:25:39410
411 // GCMClient remains active after one app handler is gone.
412 driver()->RemoveAppHandler(kTestAppID1);
413 PumpIOLoop();
414 PumpUILoop();
jianli19562592015-01-12 20:16:30415 EXPECT_TRUE(driver()->IsStarted());
[email protected]3ecb2b582014-05-23 00:25:39416
417 // GCMClient should be stopped after the last app handler is gone.
418 driver()->RemoveAppHandler(kTestAppID2);
419 PumpIOLoop();
420 PumpUILoop();
jianli19562592015-01-12 20:16:30421 EXPECT_FALSE(driver()->IsStarted());
[email protected]3ecb2b582014-05-23 00:25:39422
423 // GCMClient is restarted after an app handler has been added.
424 driver()->AddAppHandler(kTestAppID2, gcm_app_handler());
425 PumpIOLoop();
426 PumpUILoop();
jianli19562592015-01-12 20:16:30427 EXPECT_TRUE(driver()->IsStarted());
[email protected]3ecb2b582014-05-23 00:25:39428}
429
[email protected]9d7e5c02014-05-21 03:09:03430TEST_F(GCMDriverTest, RegisterFailed) {
[email protected]530f2162014-05-15 01:05:04431 std::vector<std::string> sender_ids;
432 sender_ids.push_back("sender1");
[email protected]530f2162014-05-15 01:05:04433
jianlif3e52af42015-01-21 23:18:47434 CreateDriver();
435
436 // Registration fails when the no app handler is added.
437 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
438 EXPECT_TRUE(registration_id().empty());
439 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
[email protected]530f2162014-05-15 01:05:04440}
441
[email protected]9d7e5c02014-05-21 03:09:03442TEST_F(GCMDriverTest, UnregisterFailed) {
jianlif3e52af42015-01-21 23:18:47443 CreateDriver();
[email protected]530f2162014-05-15 01:05:04444
jianlif3e52af42015-01-21 23:18:47445 // Unregistration fails when the no app handler is added.
[email protected]9d7e5c02014-05-21 03:09:03446 Unregister(kTestAppID1, GCMDriverTest::WAIT);
jianlif3e52af42015-01-21 23:18:47447 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
[email protected]530f2162014-05-15 01:05:04448}
449
[email protected]9d7e5c02014-05-21 03:09:03450TEST_F(GCMDriverTest, SendFailed) {
mvanouwerkerkf8633deb2015-07-13 11:04:06451 OutgoingMessage message;
[email protected]530f2162014-05-15 01:05:04452 message.id = "1";
453 message.data["key1"] = "value1";
[email protected]530f2162014-05-15 01:05:04454
jianlif3e52af42015-01-21 23:18:47455 CreateDriver();
456
457 // Sending fails when the no app handler is added.
458 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
459 EXPECT_TRUE(send_message_id().empty());
460 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
[email protected]530f2162014-05-15 01:05:04461}
462
[email protected]617b22e92019-09-30 20:29:37463TEST_F(GCMDriverTest, DISABLED_GCMClientNotReadyBeforeRegistration) {
jianlif3e52af42015-01-21 23:18:47464 CreateDriver();
465 PumpIOLoop();
466 PumpUILoop();
467
468 // Make GCMClient not ready until PerformDelayedStart is called.
469 GetGCMClient()->set_start_mode_overridding(
470 FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
471
[email protected]3ecb2b582014-05-23 00:25:39472 AddAppHandlers();
[email protected]530f2162014-05-15 01:05:04473
474 // The registration is on hold until GCMClient is ready.
475 std::vector<std::string> sender_ids;
476 sender_ids.push_back("sender1");
477 Register(kTestAppID1,
478 sender_ids,
479 GCMDriverTest::DO_NOT_WAIT);
480 PumpIOLoop();
481 PumpUILoop();
482 EXPECT_TRUE(registration_id().empty());
483 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
484
485 // Register operation will be invoked after GCMClient becomes ready.
jianlif3e52af42015-01-21 23:18:47486 GetGCMClient()->PerformDelayedStart();
[email protected]530f2162014-05-15 01:05:04487 WaitForAsyncOperation();
488 EXPECT_FALSE(registration_id().empty());
489 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
490}
491
492TEST_F(GCMDriverTest, GCMClientNotReadyBeforeSending) {
jianlif3e52af42015-01-21 23:18:47493 CreateDriver();
494 PumpIOLoop();
495 PumpUILoop();
496
497 // Make GCMClient not ready until PerformDelayedStart is called.
498 GetGCMClient()->set_start_mode_overridding(
499 FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
500
[email protected]3ecb2b582014-05-23 00:25:39501 AddAppHandlers();
[email protected]530f2162014-05-15 01:05:04502
503 // The sending is on hold until GCMClient is ready.
mvanouwerkerkf8633deb2015-07-13 11:04:06504 OutgoingMessage message;
[email protected]530f2162014-05-15 01:05:04505 message.id = "1";
506 message.data["key1"] = "value1";
507 message.data["key2"] = "value2";
508 Send(kTestAppID1, kUserID1, message, GCMDriverTest::DO_NOT_WAIT);
509 PumpIOLoop();
510 PumpUILoop();
511
512 EXPECT_TRUE(send_message_id().empty());
513 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, send_result());
514
515 // Send operation will be invoked after GCMClient becomes ready.
jianlif3e52af42015-01-21 23:18:47516 GetGCMClient()->PerformDelayedStart();
[email protected]530f2162014-05-15 01:05:04517 WaitForAsyncOperation();
518 EXPECT_EQ(message.id, send_message_id());
519 EXPECT_EQ(GCMClient::SUCCESS, send_result());
520}
521
522// Tests a single instance of GCMDriver.
523class GCMDriverFunctionalTest : public GCMDriverTest {
524 public:
525 GCMDriverFunctionalTest();
dcheng30a1b1542014-10-29 21:27:50526 ~GCMDriverFunctionalTest() override;
[email protected]530f2162014-05-15 01:05:04527
528 // GCMDriverTest:
dcheng30a1b1542014-10-29 21:27:50529 void SetUp() override;
[email protected]530f2162014-05-15 01:05:04530
531 private:
532 DISALLOW_COPY_AND_ASSIGN(GCMDriverFunctionalTest);
533};
534
535GCMDriverFunctionalTest::GCMDriverFunctionalTest() {
536}
537
538GCMDriverFunctionalTest::~GCMDriverFunctionalTest() {
539}
540
541void GCMDriverFunctionalTest::SetUp() {
542 GCMDriverTest::SetUp();
543
jianlif3e52af42015-01-21 23:18:47544 CreateDriver();
[email protected]3ecb2b582014-05-23 00:25:39545 AddAppHandlers();
jianli753282a32015-01-09 02:07:40546 PumpIOLoop();
547 PumpUILoop();
[email protected]530f2162014-05-15 01:05:04548}
549
[email protected]617b22e92019-09-30 20:29:37550TEST_F(GCMDriverFunctionalTest, DISABLED_Register) {
[email protected]530f2162014-05-15 01:05:04551 std::vector<std::string> sender_ids;
552 sender_ids.push_back("sender1");
553 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
554 const std::string expected_registration_id =
jianli012b5c82015-05-28 01:41:29555 FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
[email protected]530f2162014-05-15 01:05:04556
557 EXPECT_EQ(expected_registration_id, registration_id());
558 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
559}
560
David Roger28e888e2019-10-10 13:32:35561// This test is flaky, see https://crbug.com/1010462
562TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterError) {
[email protected]530f2162014-05-15 01:05:04563 std::vector<std::string> sender_ids;
564 sender_ids.push_back("sender1@error");
565 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
566
567 EXPECT_TRUE(registration_id().empty());
568 EXPECT_NE(GCMClient::SUCCESS, registration_result());
569}
570
David Roger28e888e2019-10-10 13:32:35571// This test is flaky, see https://crbug.com/1010462
572TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterAgainWithSameSenderIDs) {
[email protected]530f2162014-05-15 01:05:04573 std::vector<std::string> sender_ids;
574 sender_ids.push_back("sender1");
575 sender_ids.push_back("sender2");
576 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
577 const std::string expected_registration_id =
jianli012b5c82015-05-28 01:41:29578 FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
[email protected]530f2162014-05-15 01:05:04579
580 EXPECT_EQ(expected_registration_id, registration_id());
581 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
582
583 // Clears the results the would be set by the Register callback in preparation
584 // to call register 2nd time.
[email protected]9d7e5c02014-05-21 03:09:03585 ClearResults();
[email protected]530f2162014-05-15 01:05:04586
587 // Calling register 2nd time with the same set of sender IDs but different
588 // ordering will get back the same registration ID.
589 std::vector<std::string> another_sender_ids;
590 another_sender_ids.push_back("sender2");
591 another_sender_ids.push_back("sender1");
592 Register(kTestAppID1, another_sender_ids, GCMDriverTest::WAIT);
593
594 EXPECT_EQ(expected_registration_id, registration_id());
595 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
596}
597
David Roger28e888e2019-10-10 13:32:35598// This test is flaky, see https://crbug.com/1010462
599TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterAgainWithDifferentSenderIDs) {
[email protected]530f2162014-05-15 01:05:04600 std::vector<std::string> sender_ids;
601 sender_ids.push_back("sender1");
602 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
603 const std::string expected_registration_id =
jianli012b5c82015-05-28 01:41:29604 FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
[email protected]530f2162014-05-15 01:05:04605
606 EXPECT_EQ(expected_registration_id, registration_id());
607 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
608
609 // Make sender IDs different.
610 sender_ids.push_back("sender2");
611 const std::string expected_registration_id2 =
jianli012b5c82015-05-28 01:41:29612 FakeGCMClient::GenerateGCMRegistrationID(sender_ids);
[email protected]530f2162014-05-15 01:05:04613
614 // Calling register 2nd time with the different sender IDs will get back a new
615 // registration ID.
616 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
617 EXPECT_EQ(expected_registration_id2, registration_id());
618 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
619}
620
[email protected]530f2162014-05-15 01:05:04621TEST_F(GCMDriverFunctionalTest, UnregisterExplicitly) {
622 std::vector<std::string> sender_ids;
623 sender_ids.push_back("sender1");
624 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
625
626 EXPECT_FALSE(registration_id().empty());
627 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
628
629 Unregister(kTestAppID1, GCMDriverTest::WAIT);
630
631 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
632}
633
Sergey Poromovb78738212019-09-30 11:55:03634// TODO(crbug.com/1009185): Test is failing on ASan build.
635#if defined(ADDRESS_SANITIZER)
636TEST_F(GCMDriverFunctionalTest, DISABLED_UnregisterRemovesEncryptionInfo) {
637#else
petera4795ae832016-02-17 11:35:27638TEST_F(GCMDriverFunctionalTest, UnregisterRemovesEncryptionInfo) {
Sergey Poromovb78738212019-09-30 11:55:03639#endif
petera4795ae832016-02-17 11:35:27640 std::vector<std::string> sender_ids;
641 sender_ids.push_back("sender1");
642 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
643
644 EXPECT_FALSE(registration_id().empty());
645 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
646
647 GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT);
648
649 EXPECT_FALSE(p256dh().empty());
650 EXPECT_FALSE(auth_secret().empty());
651
652 const std::string app_p256dh = p256dh();
653 const std::string app_auth_secret = auth_secret();
654
655 GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT);
656
657 EXPECT_EQ(app_p256dh, p256dh());
658 EXPECT_EQ(app_auth_secret, auth_secret());
659
660 Unregister(kTestAppID1, GCMDriverTest::WAIT);
661
662 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
663
664 GetEncryptionInfo(kTestAppID1, GCMDriverTest::WAIT);
665
666 // The GCMKeyStore eagerly creates new keying material for registrations that
667 // don't have any associated with them, so the most appropriate check to do is
668 // to verify that the returned material is different from before.
669
670 EXPECT_NE(app_p256dh, p256dh());
671 EXPECT_NE(app_auth_secret, auth_secret());
672}
673
[email protected]617b22e92019-09-30 20:29:37674TEST_F(GCMDriverFunctionalTest, DISABLED_UnregisterWhenAsyncOperationPending) {
[email protected]530f2162014-05-15 01:05:04675 std::vector<std::string> sender_ids;
676 sender_ids.push_back("sender1");
677 // First start registration without waiting for it to complete.
jianlif17b85a2014-10-29 21:42:30678 Register(kTestAppID1, sender_ids, GCMDriverTest::DO_NOT_WAIT);
[email protected]530f2162014-05-15 01:05:04679
680 // Test that unregistration fails with async operation pending when there is a
681 // registration already in progress.
682 Unregister(kTestAppID1, GCMDriverTest::WAIT);
683 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
684 unregistration_result());
685
686 // Complete the unregistration.
687 WaitForAsyncOperation();
688 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
689
690 // Start unregistration without waiting for it to complete. This time no async
691 // operation is pending.
692 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
693
694 // Test that unregistration fails with async operation pending when there is
695 // an unregistration already in progress.
696 Unregister(kTestAppID1, GCMDriverTest::WAIT);
697 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
698 unregistration_result());
[email protected]9d7e5c02014-05-21 03:09:03699 ClearResults();
[email protected]530f2162014-05-15 01:05:04700
701 // Complete unregistration.
702 WaitForAsyncOperation();
703 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
704}
705
706TEST_F(GCMDriverFunctionalTest, RegisterWhenAsyncOperationPending) {
707 std::vector<std::string> sender_ids;
708 sender_ids.push_back("sender1");
709 // First start registration without waiting for it to complete.
jianlif17b85a2014-10-29 21:42:30710 Register(kTestAppID1, sender_ids, GCMDriverTest::DO_NOT_WAIT);
[email protected]530f2162014-05-15 01:05:04711
712 // Test that registration fails with async operation pending when there is a
713 // registration already in progress.
714 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
715 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING,
716 registration_result());
[email protected]9d7e5c02014-05-21 03:09:03717 ClearResults();
[email protected]530f2162014-05-15 01:05:04718
719 // Complete the registration.
720 WaitForAsyncOperation();
721 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
jianlif17b85a2014-10-29 21:42:30722}
[email protected]530f2162014-05-15 01:05:04723
David Roger28e888e2019-10-10 13:32:35724// This test is flaky, see https://crbug.com/1010462
725TEST_F(GCMDriverFunctionalTest, DISABLED_RegisterAfterUnfinishedUnregister) {
jianlif17b85a2014-10-29 21:42:30726 // Register and wait for it to complete.
727 std::vector<std::string> sender_ids;
728 sender_ids.push_back("sender1");
[email protected]530f2162014-05-15 01:05:04729 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
730 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
jianli012b5c82015-05-28 01:41:29731 EXPECT_EQ(FakeGCMClient::GenerateGCMRegistrationID(sender_ids),
jianlif17b85a2014-10-29 21:42:30732 registration_id());
733
734 // Clears the results the would be set by the Register callback in preparation
735 // to call register 2nd time.
736 ClearResults();
737
738 // Start unregistration without waiting for it to complete.
739 Unregister(kTestAppID1, GCMDriverTest::DO_NOT_WAIT);
740
741 // Register immeidately after unregistration is not completed.
742 sender_ids.push_back("sender2");
743 Register(kTestAppID1, sender_ids, GCMDriverTest::WAIT);
744
745 // We need one more waiting since the waiting in Register is indeed for
746 // uncompleted Unregister.
747 WaitForAsyncOperation();
748 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
jianli012b5c82015-05-28 01:41:29749 EXPECT_EQ(FakeGCMClient::GenerateGCMRegistrationID(sender_ids),
jianlif17b85a2014-10-29 21:42:30750 registration_id());
[email protected]530f2162014-05-15 01:05:04751}
752
753TEST_F(GCMDriverFunctionalTest, Send) {
mvanouwerkerkf8633deb2015-07-13 11:04:06754 OutgoingMessage message;
[email protected]292af2b22014-08-06 19:42:45755 message.id = "1@ack";
[email protected]530f2162014-05-15 01:05:04756 message.data["key1"] = "value1";
757 message.data["key2"] = "value2";
758 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
759
760 EXPECT_EQ(message.id, send_message_id());
761 EXPECT_EQ(GCMClient::SUCCESS, send_result());
[email protected]292af2b22014-08-06 19:42:45762
763 gcm_app_handler()->WaitForNotification();
764 EXPECT_EQ(message.id, gcm_app_handler()->acked_message_id());
765 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
[email protected]530f2162014-05-15 01:05:04766}
767
[email protected]530f2162014-05-15 01:05:04768TEST_F(GCMDriverFunctionalTest, SendError) {
mvanouwerkerkf8633deb2015-07-13 11:04:06769 OutgoingMessage message;
[email protected]530f2162014-05-15 01:05:04770 // Embedding error in id will tell the mock to simulate the send error.
771 message.id = "1@error";
772 message.data["key1"] = "value1";
773 message.data["key2"] = "value2";
774 Send(kTestAppID1, kUserID1, message, GCMDriverTest::WAIT);
775
776 EXPECT_EQ(message.id, send_message_id());
777 EXPECT_EQ(GCMClient::SUCCESS, send_result());
778
779 // Wait for the send error.
780 gcm_app_handler()->WaitForNotification();
781 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT,
782 gcm_app_handler()->received_event());
783 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
784 EXPECT_EQ(message.id,
785 gcm_app_handler()->send_error_details().message_id);
786 EXPECT_NE(GCMClient::SUCCESS,
787 gcm_app_handler()->send_error_details().result);
788 EXPECT_EQ(message.data,
789 gcm_app_handler()->send_error_details().additional_data);
790}
791
David Roger28e888e2019-10-10 13:32:35792// This test is flaky, see https://crbug.com/1010462
793TEST_F(GCMDriverFunctionalTest, DISABLED_MessageReceived) {
jianlif3e52af42015-01-21 23:18:47794 // GCM registration has to be performed otherwise GCM will not be started.
[email protected]530f2162014-05-15 01:05:04795 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
jianlif3e52af42015-01-21 23:18:47796
mvanouwerkerkf8633deb2015-07-13 11:04:06797 IncomingMessage message;
[email protected]530f2162014-05-15 01:05:04798 message.data["key1"] = "value1";
799 message.data["key2"] = "value2";
800 message.sender_id = "sender";
801 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
802 gcm_app_handler()->WaitForNotification();
803 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
804 gcm_app_handler()->received_event());
805 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
806 EXPECT_EQ(message.data, gcm_app_handler()->message().data);
807 EXPECT_TRUE(gcm_app_handler()->message().collapse_key.empty());
808 EXPECT_EQ(message.sender_id, gcm_app_handler()->message().sender_id);
809}
810
David Roger28e888e2019-10-10 13:32:35811// This test is flaky, see https://crbug.com/1010462
812TEST_F(GCMDriverFunctionalTest, DISABLED_MessageWithCollapseKeyReceived) {
jianlif3e52af42015-01-21 23:18:47813 // GCM registration has to be performed otherwise GCM will not be started.
[email protected]530f2162014-05-15 01:05:04814 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
jianlif3e52af42015-01-21 23:18:47815
mvanouwerkerkf8633deb2015-07-13 11:04:06816 IncomingMessage message;
[email protected]530f2162014-05-15 01:05:04817 message.data["key1"] = "value1";
818 message.collapse_key = "collapse_key_value";
819 message.sender_id = "sender";
820 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
821 gcm_app_handler()->WaitForNotification();
822 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT,
823 gcm_app_handler()->received_event());
824 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
825 EXPECT_EQ(message.data, gcm_app_handler()->message().data);
826 EXPECT_EQ(message.collapse_key,
827 gcm_app_handler()->message().collapse_key);
828}
829
peteree284ba52016-02-01 11:53:28830TEST_F(GCMDriverFunctionalTest, EncryptedMessageReceivedError) {
831 // GCM registration has to be performed otherwise GCM will not be started.
832 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
833
834 IncomingMessage message;
835
836 // All required information to trigger the encryption path, but with an
837 // invalid Crypto-Key header value to trigger an error.
838 message.data["encryption"] = "salt=ysyxqlYTgE0WvcZrmHbUbg";
839 message.data["crypto-key"] = "hey=thereisnopublickey";
840 message.sender_id = "sender";
841 message.raw_data = "foobar";
842
843 GetGCMClient()->SetRecording(true);
844 GetGCMClient()->ReceiveMessage(kTestAppID1, message);
845
846 PumpIOLoop();
847 PumpUILoop();
848 PumpIOLoop();
849
Rayan Kanso9e5adf212019-05-09 16:07:53850 EXPECT_EQ(FakeGCMAppHandler::DECRYPTION_FAILED_EVENT,
851 gcm_app_handler()->received_event());
852
peteree284ba52016-02-01 11:53:28853 GCMClient::GCMStatistics statistics = GetGCMClient()->GetStatistics();
854 EXPECT_TRUE(statistics.is_recording);
855 EXPECT_EQ(
856 1u, statistics.recorded_activities.decryption_failure_activities.size());
857}
858
[email protected]530f2162014-05-15 01:05:04859TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
jianlif3e52af42015-01-21 23:18:47860 // GCM registration has to be performed otherwise GCM will not be started.
861 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
862
[email protected]530f2162014-05-15 01:05:04863 GetGCMClient()->DeleteMessages(kTestAppID1);
864 gcm_app_handler()->WaitForNotification();
865 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
866 gcm_app_handler()->received_event());
867 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
868}
869
fgorski9a405102014-11-19 01:25:16870TEST_F(GCMDriverFunctionalTest, LastTokenFetchTime) {
jianlif3e52af42015-01-21 23:18:47871 // GCM registration has to be performed otherwise GCM will not be started.
872 Register(kTestAppID1, ToSenderList("sender"), GCMDriverTest::WAIT);
873
fgorski9a405102014-11-19 01:25:16874 EXPECT_EQ(base::Time(), driver()->GetLastTokenFetchTime());
875 base::Time fetch_time = base::Time::Now();
876 driver()->SetLastTokenFetchTime(fetch_time);
877 EXPECT_EQ(fetch_time, driver()->GetLastTokenFetchTime());
878}
879
jianli012b5c82015-05-28 01:41:29880class GCMDriverInstanceIDTest : public GCMDriverTest {
881 public:
882 GCMDriverInstanceIDTest();
883 ~GCMDriverInstanceIDTest() override;
884
885 void GetReady();
jianliea8534872015-06-22 21:06:22886 void GetInstanceID(const std::string& app_id, WaitToFinish wait_to_finish);
887 void GetInstanceIDDataCompleted(const std::string& instance_id,
888 const std::string& extra_data);
jianli012b5c82015-05-28 01:41:29889 void GetToken(const std::string& app_id,
890 const std::string& authorized_entity,
891 const std::string& scope,
892 WaitToFinish wait_to_finish);
893 void DeleteToken(const std::string& app_id,
894 const std::string& authorized_entity,
895 const std::string& scope,
896 WaitToFinish wait_to_finish);
johnme54a3e1482016-03-11 19:13:22897 void AddInstanceIDData(const std::string& app_id,
898 const std::string& instance_id,
899 const std::string& extra_data);
900 void RemoveInstanceIDData(const std::string& app_id);
jianli012b5c82015-05-28 01:41:29901
jianliea8534872015-06-22 21:06:22902 std::string instance_id() const { return instance_id_; }
903 std::string extra_data() const { return extra_data_; }
904
jianli012b5c82015-05-28 01:41:29905 private:
jianliea8534872015-06-22 21:06:22906 std::string instance_id_;
907 std::string extra_data_;
908
jianli012b5c82015-05-28 01:41:29909 DISALLOW_COPY_AND_ASSIGN(GCMDriverInstanceIDTest);
910};
911
912GCMDriverInstanceIDTest::GCMDriverInstanceIDTest() {
913}
914
915GCMDriverInstanceIDTest::~GCMDriverInstanceIDTest() {
916}
917
918void GCMDriverInstanceIDTest::GetReady() {
919 CreateDriver();
920 AddAppHandlers();
921 PumpIOLoop();
922 PumpUILoop();
923}
924
jianliea8534872015-06-22 21:06:22925void GCMDriverInstanceIDTest::GetInstanceID(const std::string& app_id,
926 WaitToFinish wait_to_finish) {
927 base::RunLoop run_loop;
928 set_async_operation_completed_callback(run_loop.QuitClosure());
johnme54a3e1482016-03-11 19:13:22929 driver()->GetInstanceIDHandlerInternal()->GetInstanceIDData(
Reilly Grantaa7bc2f2020-01-31 11:49:29930 app_id,
931 base::BindOnce(&GCMDriverInstanceIDTest::GetInstanceIDDataCompleted,
932 base::Unretained(this)));
jianliea8534872015-06-22 21:06:22933 if (wait_to_finish == WAIT)
934 run_loop.Run();
935}
936
937void GCMDriverInstanceIDTest::GetInstanceIDDataCompleted(
938 const std::string& instance_id, const std::string& extra_data) {
939 instance_id_ = instance_id;
940 extra_data_ = extra_data;
Reilly Grantaa7bc2f2020-01-31 11:49:29941 AsyncOperationCompleted();
jianliea8534872015-06-22 21:06:22942}
943
jianli012b5c82015-05-28 01:41:29944void GCMDriverInstanceIDTest::GetToken(const std::string& app_id,
945 const std::string& authorized_entity,
946 const std::string& scope,
947 WaitToFinish wait_to_finish) {
948 base::RunLoop run_loop;
949 set_async_operation_completed_callback(run_loop.QuitClosure());
950 std::map<std::string, std::string> options;
johnme54a3e1482016-03-11 19:13:22951 driver()->GetInstanceIDHandlerInternal()->GetToken(
Maksim Moskvitin8b448982020-02-06 13:23:19952 app_id, authorized_entity, scope, /*time_to_live=*/base::TimeDelta(),
953 options,
Reilly Grantaa7bc2f2020-01-31 11:49:29954 base::BindOnce(&GCMDriverTest::RegisterCompleted,
955 base::Unretained(this)));
jianli012b5c82015-05-28 01:41:29956 if (wait_to_finish == WAIT)
957 run_loop.Run();
958}
959
960void GCMDriverInstanceIDTest::DeleteToken(const std::string& app_id,
961 const std::string& authorized_entity,
962 const std::string& scope,
963 WaitToFinish wait_to_finish) {
964 base::RunLoop run_loop;
965 set_async_operation_completed_callback(run_loop.QuitClosure());
johnme54a3e1482016-03-11 19:13:22966 driver()->GetInstanceIDHandlerInternal()->DeleteToken(
967 app_id, authorized_entity, scope,
Reilly Grantaa7bc2f2020-01-31 11:49:29968 base::BindOnce(&GCMDriverTest::UnregisterCompleted,
969 base::Unretained(this)));
jianli012b5c82015-05-28 01:41:29970 if (wait_to_finish == WAIT)
971 run_loop.Run();
972}
973
johnme54a3e1482016-03-11 19:13:22974void GCMDriverInstanceIDTest::AddInstanceIDData(const std::string& app_id,
975 const std::string& instance_id,
976 const std::string& extra_data) {
977 driver()->GetInstanceIDHandlerInternal()->AddInstanceIDData(
978 app_id, instance_id, extra_data);
979}
980
981void GCMDriverInstanceIDTest::RemoveInstanceIDData(const std::string& app_id) {
982 driver()->GetInstanceIDHandlerInternal()->RemoveInstanceIDData(app_id);
983}
984
jianliea8534872015-06-22 21:06:22985TEST_F(GCMDriverInstanceIDTest, InstanceIDData) {
986 GetReady();
987
johnme54a3e1482016-03-11 19:13:22988 AddInstanceIDData(kTestAppID1, kInstanceID1, "Foo");
jianliea8534872015-06-22 21:06:22989 GetInstanceID(kTestAppID1, GCMDriverTest::WAIT);
990
991 EXPECT_EQ(kInstanceID1, instance_id());
992 EXPECT_EQ("Foo", extra_data());
993
johnme54a3e1482016-03-11 19:13:22994 RemoveInstanceIDData(kTestAppID1);
jianliea8534872015-06-22 21:06:22995 GetInstanceID(kTestAppID1, GCMDriverTest::WAIT);
996
997 EXPECT_TRUE(instance_id().empty());
998 EXPECT_TRUE(extra_data().empty());
999}
1000
David Roger28e888e2019-10-10 13:32:351001// This test is flaky, see https://crbug.com/1010462
1002TEST_F(GCMDriverInstanceIDTest,
1003 DISABLED_GCMClientNotReadyBeforeInstanceIDData) {
jianliea8534872015-06-22 21:06:221004 CreateDriver();
1005 PumpIOLoop();
1006 PumpUILoop();
1007
1008 // Make GCMClient not ready until PerformDelayedStart is called.
1009 GetGCMClient()->set_start_mode_overridding(
1010 FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
1011
1012 AddAppHandlers();
1013
1014 // All operations are on hold until GCMClient is ready.
johnme54a3e1482016-03-11 19:13:221015 AddInstanceIDData(kTestAppID1, kInstanceID1, "Foo");
1016 AddInstanceIDData(kTestAppID2, kInstanceID2, "Bar");
1017 RemoveInstanceIDData(kTestAppID1);
jianliea8534872015-06-22 21:06:221018 GetInstanceID(kTestAppID2, GCMDriverTest::DO_NOT_WAIT);
1019 PumpIOLoop();
1020 PumpUILoop();
1021 EXPECT_TRUE(instance_id().empty());
1022 EXPECT_TRUE(extra_data().empty());
1023
1024 // All operations will be performed after GCMClient becomes ready.
1025 GetGCMClient()->PerformDelayedStart();
1026 WaitForAsyncOperation();
1027 EXPECT_EQ(kInstanceID2, instance_id());
1028 EXPECT_EQ("Bar", extra_data());
1029}
1030
[email protected]617b22e92019-09-30 20:29:371031TEST_F(GCMDriverInstanceIDTest, DISABLED_GetToken) {
jianli012b5c82015-05-28 01:41:291032 GetReady();
1033
1034 const std::string expected_token =
1035 FakeGCMClient::GenerateInstanceIDToken(kUserID1, kScope);
1036 GetToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::WAIT);
1037
1038 EXPECT_EQ(expected_token, registration_id());
1039 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
1040}
1041
1042TEST_F(GCMDriverInstanceIDTest, GetTokenError) {
1043 GetReady();
1044
1045 std::string error_entity = "sender@error";
1046 GetToken(kTestAppID1, error_entity, kScope, GCMDriverTest::WAIT);
1047
1048 EXPECT_TRUE(registration_id().empty());
1049 EXPECT_NE(GCMClient::SUCCESS, registration_result());
1050}
1051
1052TEST_F(GCMDriverInstanceIDTest, GCMClientNotReadyBeforeGetToken) {
1053 CreateDriver();
1054 PumpIOLoop();
1055 PumpUILoop();
1056
1057 // Make GCMClient not ready until PerformDelayedStart is called.
1058 GetGCMClient()->set_start_mode_overridding(
1059 FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
1060
1061 AddAppHandlers();
1062
1063 // GetToken operation is on hold until GCMClient is ready.
1064 GetToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::DO_NOT_WAIT);
1065 PumpIOLoop();
1066 PumpUILoop();
1067 EXPECT_TRUE(registration_id().empty());
1068 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, registration_result());
1069
1070 // GetToken operation will be invoked after GCMClient becomes ready.
1071 GetGCMClient()->PerformDelayedStart();
1072 WaitForAsyncOperation();
1073 EXPECT_FALSE(registration_id().empty());
1074 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
1075}
1076
1077TEST_F(GCMDriverInstanceIDTest, DeleteToken) {
1078 GetReady();
1079
1080 const std::string expected_token =
1081 FakeGCMClient::GenerateInstanceIDToken(kUserID1, kScope);
1082 GetToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::WAIT);
1083 EXPECT_EQ(expected_token, registration_id());
1084 EXPECT_EQ(GCMClient::SUCCESS, registration_result());
1085
1086 DeleteToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::WAIT);
1087 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
1088}
1089
1090TEST_F(GCMDriverInstanceIDTest, GCMClientNotReadyBeforeDeleteToken) {
1091 CreateDriver();
1092 PumpIOLoop();
1093 PumpUILoop();
1094
1095 // Make GCMClient not ready until PerformDelayedStart is called.
1096 GetGCMClient()->set_start_mode_overridding(
1097 FakeGCMClient::FORCE_TO_ALWAYS_DELAY_START_GCM);
1098
1099 AddAppHandlers();
1100
1101 // DeleteToken operation is on hold until GCMClient is ready.
1102 DeleteToken(kTestAppID1, kUserID1, kScope, GCMDriverTest::DO_NOT_WAIT);
1103 PumpIOLoop();
1104 PumpUILoop();
1105 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, unregistration_result());
1106
1107 // DeleteToken operation will be invoked after GCMClient becomes ready.
1108 GetGCMClient()->PerformDelayedStart();
1109 WaitForAsyncOperation();
1110 EXPECT_EQ(GCMClient::SUCCESS, unregistration_result());
1111}
1112
[email protected]530f2162014-05-15 01:05:041113} // namespace gcm