blob: 698f189ba8945c8ddc62316f433f1b2a2126dd2e [file] [log] [blame]
[email protected]848b1b62014-01-30 23:51: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]cd57f372014-06-09 17:13:065#include "components/gcm_driver/gcm_client_impl.h"
[email protected]848b1b62014-01-30 23:51:046
avi26062922015-12-26 00:14:187#include <stdint.h>
8
johnme627dc8c72016-08-19 21:49:399#include <initializer_list>
dchenga77e28eb2016-04-21 21:34:3710#include <memory>
11
Helen Li5f3d96a2018-08-10 20:37:2412#include "base/bind_helpers.h"
[email protected]fdeaf9732014-04-11 21:02:1513#include "base/command_line.h"
jianli00b4600f2015-02-10 23:32:4914#include "base/files/file_path.h"
15#include "base/files/file_util.h"
[email protected]848b1b62014-01-30 23:51:0416#include "base/files/scoped_temp_dir.h"
avi26062922015-12-26 00:14:1817#include "base/macros.h"
dchenga77e28eb2016-04-21 21:34:3718#include "base/memory/ptr_util.h"
[email protected]764c0442014-05-01 04:30:5519#include "base/strings/string_number_conversions.h"
Devlin Cronin69228f42018-06-01 17:25:1020#include "base/test/metrics/histogram_tester.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1321#include "base/test/scoped_feature_list.h"
Gabriel Charettec7108742019-08-23 03:31:4022#include "base/test/task_environment.h"
[email protected]764c0442014-05-01 04:30:5523#include "base/time/clock.h"
chirantan26436e402014-10-24 19:44:4224#include "base/timer/timer.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1325#include "components/gcm_driver/features.h"
[email protected]446f73c22014-05-14 20:47:1826#include "google_apis/gcm/base/fake_encryptor.h"
[email protected]848b1b62014-01-30 23:51:0427#include "google_apis/gcm/base/mcs_message.h"
28#include "google_apis/gcm/base/mcs_util.h"
29#include "google_apis/gcm/engine/fake_connection_factory.h"
30#include "google_apis/gcm/engine/fake_connection_handler.h"
[email protected]764c0442014-05-01 04:30:5531#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]436bcb82014-04-18 00:40:5732#include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
[email protected]848b1b62014-01-30 23:51:0433#include "google_apis/gcm/protocol/android_checkin.pb.h"
34#include "google_apis/gcm/protocol/checkin.pb.h"
35#include "google_apis/gcm/protocol/mcs.pb.h"
johnme627dc8c72016-08-19 21:49:3936#include "net/test/gtest_util.h"
37#include "net/test/scoped_disable_exit_on_dfatal.h"
Maks Orlovichc70c93c2018-07-12 02:45:4438#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
Robbie McElrathb01499332018-09-25 00:53:1339#include "services/network/test/test_network_connection_tracker.h"
Maks Orlovichc70c93c2018-07-12 02:45:4440#include "services/network/test/test_url_loader_factory.h"
41#include "services/network/test/test_utils.h"
johnme627dc8c72016-08-19 21:49:3942#include "testing/gtest/include/gtest/gtest-spi.h"
[email protected]848b1b62014-01-30 23:51:0443#include "testing/gtest/include/gtest/gtest.h"
Chris Mumforda884aa1f2017-10-24 22:46:3244#include "third_party/leveldatabase/leveldb_chrome.h"
[email protected]848b1b62014-01-30 23:51:0445
46namespace gcm {
[email protected]848b1b62014-01-30 23:51:0447namespace {
48
49enum LastEvent {
50 NONE,
51 LOADING_COMPLETED,
[email protected]848b1b62014-01-30 23:51:0452 REGISTRATION_COMPLETED,
[email protected]e4007042014-02-15 20:34:2853 UNREGISTRATION_COMPLETED,
[email protected]848b1b62014-01-30 23:51:0454 MESSAGE_SEND_ERROR,
[email protected]292af2b22014-08-06 19:42:4555 MESSAGE_SEND_ACK,
[email protected]848b1b62014-01-30 23:51:0456 MESSAGE_RECEIVED,
57 MESSAGES_DELETED,
58};
59
jianlic02d25e2015-05-27 22:24:3160const char kChromeVersion[] = "45.0.0.1";
avi26062922015-12-26 00:14:1861const uint64_t kDeviceAndroidId = 54321;
62const uint64_t kDeviceSecurityToken = 12345;
63const uint64_t kDeviceAndroidId2 = 11111;
64const uint64_t kDeviceSecurityToken2 = 2222;
65const int64_t kSettingsCheckinInterval = 16 * 60 * 60;
johnme627dc8c72016-08-19 21:49:3966const char kProductCategoryForSubtypes[] = "com.chrome.macosx";
67const char kExtensionAppId[] = "abcdefghijklmnopabcdefghijklmnop";
Mugdha Lakhani2ade6a02018-01-31 14:31:1168const char kRegistrationId[] = "reg_id";
johnme627dc8c72016-08-19 21:49:3969const char kSubtypeAppId[] = "app_id";
[email protected]3a20a4d2014-03-21 22:54:2170const char kSender[] = "project_id";
71const char kSender2[] = "project_id2";
[email protected]848b1b62014-01-30 23:51:0472const char kRegistrationResponsePrefix[] = "token=";
[email protected]e4007042014-02-15 20:34:2873const char kUnregistrationResponsePrefix[] = "deleted=";
petera5aedc52015-07-22 10:47:3074const char kRawData[] = "example raw data";
[email protected]848b1b62014-01-30 23:51:0475
jianlic02d25e2015-05-27 22:24:3176const char kInstanceID[] = "iid_1";
77const char kScope[] = "GCM";
78const char kDeleteTokenResponse[] = "token=foo";
Mugdha Lakhanic3d792e2018-02-13 15:10:1379const int kTestTokenInvalidationPeriod = 5;
Rayan Kansobcec0292019-05-09 13:27:5180const char kMessageId[] = "0:12345%5678";
jianlic02d25e2015-05-27 22:24:3181
Maks Orlovichc70c93c2018-07-12 02:45:4482const char kRegisterUrl[] = "https://android.clients.google.com/c2dm/register3";
83
[email protected]848b1b62014-01-30 23:51:0484// Helper for building arbitrary data messages.
85MCSMessage BuildDownstreamMessage(
86 const std::string& project_id,
johnme627dc8c72016-08-19 21:49:3987 const std::string& category,
88 const std::string& subtype,
petera5aedc52015-07-22 10:47:3089 const std::map<std::string, std::string>& data,
90 const std::string& raw_data) {
[email protected]848b1b62014-01-30 23:51:0491 mcs_proto::DataMessageStanza data_message;
[email protected]848b1b62014-01-30 23:51:0492 data_message.set_from(project_id);
johnme627dc8c72016-08-19 21:49:3993 data_message.set_category(category);
jdoerrie3feb1852018-10-05 12:16:4494 for (auto iter = data.begin(); iter != data.end(); ++iter) {
[email protected]848b1b62014-01-30 23:51:0495 mcs_proto::AppData* app_data = data_message.add_app_data();
96 app_data->set_key(iter->first);
97 app_data->set_value(iter->second);
98 }
johnme627dc8c72016-08-19 21:49:3999 if (!subtype.empty()) {
100 mcs_proto::AppData* app_data = data_message.add_app_data();
101 app_data->set_key("subtype");
102 app_data->set_value(subtype);
103 }
petera5aedc52015-07-22 10:47:30104 data_message.set_raw_data(raw_data);
Rayan Kansobcec0292019-05-09 13:27:51105 data_message.set_persistent_id(kMessageId);
[email protected]848b1b62014-01-30 23:51:04106 return MCSMessage(kDataMessageStanzaTag, data_message);
107}
108
fgorski58b9dfc2014-09-29 16:46:18109GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email,
110 const std::string& token) {
111 GCMClient::AccountTokenInfo account_token;
112 account_token.email = email;
113 account_token.access_token = token;
114 return account_token;
115}
116
117std::map<std::string, std::string> MakeEmailToTokenMap(
118 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
119 std::map<std::string, std::string> email_token_map;
jdoerrie3feb1852018-10-05 12:16:44120 for (auto iter = account_tokens.begin(); iter != account_tokens.end();
121 ++iter) {
fgorski58b9dfc2014-09-29 16:46:18122 email_token_map[iter->email] = iter->access_token;
123 }
124 return email_token_map;
125}
126
[email protected]848b1b62014-01-30 23:51:04127class FakeMCSClient : public MCSClient {
128 public:
129 FakeMCSClient(base::Clock* clock,
[email protected]2bbe0a682014-03-26 00:08:31130 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57131 GCMStore* gcm_store,
Steven Zhub36394c2019-06-04 16:10:46132 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
[email protected]436bcb82014-04-18 00:40:57133 GCMStatsRecorder* recorder);
dcheng00ea022b2014-10-21 11:24:56134 ~FakeMCSClient() override;
avi26062922015-12-26 00:14:18135 void Login(uint64_t android_id, uint64_t security_token) override;
dcheng00ea022b2014-10-21 11:24:56136 void SendMessage(const MCSMessage& message) override;
[email protected]848b1b62014-01-30 23:51:04137
avi26062922015-12-26 00:14:18138 uint64_t last_android_id() const { return last_android_id_; }
139 uint64_t last_security_token() const { return last_security_token_; }
140 uint8_t last_message_tag() const { return last_message_tag_; }
[email protected]848b1b62014-01-30 23:51:04141 const mcs_proto::DataMessageStanza& last_data_message_stanza() const {
142 return last_data_message_stanza_;
143 }
144
145 private:
avi26062922015-12-26 00:14:18146 uint64_t last_android_id_;
147 uint64_t last_security_token_;
148 uint8_t last_message_tag_;
[email protected]848b1b62014-01-30 23:51:04149 mcs_proto::DataMessageStanza last_data_message_stanza_;
150};
151
Steven Zhub36394c2019-06-04 16:10:46152FakeMCSClient::FakeMCSClient(
153 base::Clock* clock,
154 ConnectionFactory* connection_factory,
155 GCMStore* gcm_store,
156 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
157 GCMStatsRecorder* recorder)
158 : MCSClient("",
159 clock,
160 connection_factory,
161 gcm_store,
162 io_task_runner,
163 recorder),
[email protected]848b1b62014-01-30 23:51:04164 last_android_id_(0u),
165 last_security_token_(0u),
Steven Zhub36394c2019-06-04 16:10:46166 last_message_tag_(kNumProtoTypes) {}
[email protected]848b1b62014-01-30 23:51:04167
168FakeMCSClient::~FakeMCSClient() {
169}
170
avi26062922015-12-26 00:14:18171void FakeMCSClient::Login(uint64_t android_id, uint64_t security_token) {
[email protected]848b1b62014-01-30 23:51:04172 last_android_id_ = android_id;
173 last_security_token_ = security_token;
174}
175
176void FakeMCSClient::SendMessage(const MCSMessage& message) {
177 last_message_tag_ = message.tag();
178 if (last_message_tag_ == kDataMessageStanzaTag) {
179 last_data_message_stanza_.CopyFrom(
180 reinterpret_cast<const mcs_proto::DataMessageStanza&>(
181 message.GetProtobuf()));
182 }
183}
184
[email protected]764c0442014-05-01 04:30:55185class AutoAdvancingTestClock : public base::Clock {
186 public:
187 explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta);
dcheng00ea022b2014-10-21 11:24:56188 ~AutoAdvancingTestClock() override;
[email protected]764c0442014-05-01 04:30:55189
tzikee78e4962018-04-13 12:25:46190 base::Time Now() const override;
Matt Menke9d045092019-10-03 17:54:21191 void Advance(base::TimeDelta delta);
[email protected]764c0442014-05-01 04:30:55192 int call_count() const { return call_count_; }
193
194 private:
tzikee78e4962018-04-13 12:25:46195 mutable int call_count_;
[email protected]764c0442014-05-01 04:30:55196 base::TimeDelta auto_increment_time_delta_;
tzikee78e4962018-04-13 12:25:46197 mutable base::Time now_;
[email protected]764c0442014-05-01 04:30:55198
199 DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock);
200};
201
202AutoAdvancingTestClock::AutoAdvancingTestClock(
203 base::TimeDelta auto_increment_time_delta)
204 : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) {
205}
206
207AutoAdvancingTestClock::~AutoAdvancingTestClock() {
208}
209
tzikee78e4962018-04-13 12:25:46210base::Time AutoAdvancingTestClock::Now() const {
[email protected]764c0442014-05-01 04:30:55211 call_count_++;
212 now_ += auto_increment_time_delta_;
213 return now_;
214}
215
216void AutoAdvancingTestClock::Advance(base::TimeDelta delta) {
217 now_ += delta;
218}
219
[email protected]2bbe0a682014-03-26 00:08:31220class FakeGCMInternalsBuilder : public GCMInternalsBuilder {
221 public:
tzik48479d92018-03-20 15:20:42222 explicit FakeGCMInternalsBuilder(base::TimeDelta clock_step);
dcheng00ea022b2014-10-21 11:24:56223 ~FakeGCMInternalsBuilder() override;
[email protected]2bbe0a682014-03-26 00:08:31224
tzik48479d92018-03-20 15:20:42225 base::Clock* GetClock() override;
dchenga77e28eb2016-04-21 21:34:37226 std::unique_ptr<MCSClient> BuildMCSClient(
227 const std::string& version,
228 base::Clock* clock,
229 ConnectionFactory* connection_factory,
230 GCMStore* gcm_store,
Steven Zhub36394c2019-06-04 16:10:46231 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
dchenga77e28eb2016-04-21 21:34:37232 GCMStatsRecorder* recorder) override;
233 std::unique_ptr<ConnectionFactory> BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31234 const std::vector<GURL>& endpoints,
235 const net::BackoffEntry::Policy& backoff_policy,
Julie Jeongeun Kim17b2adf12019-10-29 10:24:48236 base::RepeatingCallback<void(
237 mojo::PendingReceiver<network::mojom::ProxyResolvingSocketFactory>)>
Helen Li5f3d96a2018-08-10 20:37:24238 get_socket_factory_callback,
Steven Zhub36394c2019-06-04 16:10:46239 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
Robbie McElrathb01499332018-09-25 00:53:13240 GCMStatsRecorder* recorder,
241 network::NetworkConnectionTracker* network_connection_tracker) override;
[email protected]764c0442014-05-01 04:30:55242
243 private:
tzik48479d92018-03-20 15:20:42244 AutoAdvancingTestClock clock_;
[email protected]2bbe0a682014-03-26 00:08:31245};
246
[email protected]764c0442014-05-01 04:30:55247FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step)
tzik48479d92018-03-20 15:20:42248 : clock_(clock_step) {}
[email protected]2bbe0a682014-03-26 00:08:31249
250FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {}
251
tzik48479d92018-03-20 15:20:42252base::Clock* FakeGCMInternalsBuilder::GetClock() {
253 return &clock_;
[email protected]2bbe0a682014-03-26 00:08:31254}
255
dchenga77e28eb2016-04-21 21:34:37256std::unique_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient(
[email protected]2bbe0a682014-03-26 00:08:31257 const std::string& version,
258 base::Clock* clock,
259 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57260 GCMStore* gcm_store,
Steven Zhub36394c2019-06-04 16:10:46261 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
[email protected]436bcb82014-04-18 00:40:57262 GCMStatsRecorder* recorder) {
Steven Zhub36394c2019-06-04 16:10:46263 return base::WrapUnique<MCSClient>(new FakeMCSClient(
264 clock, connection_factory, gcm_store, io_task_runner, recorder));
[email protected]2bbe0a682014-03-26 00:08:31265}
266
dchenga77e28eb2016-04-21 21:34:37267std::unique_ptr<ConnectionFactory>
268FakeGCMInternalsBuilder::BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31269 const std::vector<GURL>& endpoints,
270 const net::BackoffEntry::Policy& backoff_policy,
Julie Jeongeun Kim17b2adf12019-10-29 10:24:48271 base::RepeatingCallback<void(
272 mojo::PendingReceiver<network::mojom::ProxyResolvingSocketFactory>)>
Helen Li5f3d96a2018-08-10 20:37:24273 get_socket_factory_callback,
Steven Zhub36394c2019-06-04 16:10:46274 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
Robbie McElrathb01499332018-09-25 00:53:13275 GCMStatsRecorder* recorder,
276 network::NetworkConnectionTracker* network_connection_tracker) {
dchenga77e28eb2016-04-21 21:34:37277 return base::WrapUnique<ConnectionFactory>(new FakeConnectionFactory());
[email protected]2bbe0a682014-03-26 00:08:31278}
279
[email protected]848b1b62014-01-30 23:51:04280} // namespace
281
282class GCMClientImplTest : public testing::Test,
283 public GCMClient::Delegate {
284 public:
285 GCMClientImplTest();
dcheng30a1b1542014-10-29 21:27:50286 ~GCMClientImplTest() override;
[email protected]848b1b62014-01-30 23:51:04287
dcheng30a1b1542014-10-29 21:27:50288 void SetUp() override;
Mugdha Lakhanic3d792e2018-02-13 15:10:13289 void TearDown() override;
290
291 void SetFeatureParams(const base::Feature& feature,
Alexei Svitkine8160de8d2019-07-02 00:06:37292 const base::FieldTrialParams& params);
[email protected]848b1b62014-01-30 23:51:04293
Mugdha Lakhanic3d792e2018-02-13 15:10:13294 void InitializeInvalidationFieldTrial();
fgorski5df101702014-10-28 02:09:31295
[email protected]764c0442014-05-01 04:30:55296 void BuildGCMClient(base::TimeDelta clock_step);
[email protected]848b1b62014-01-30 23:51:04297 void InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53298 void StartGCMClient();
jianli7a0c9b62015-05-26 23:24:47299 void Register(const std::string& app_id,
300 const std::vector<std::string>& senders);
301 void Unregister(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04302 void ReceiveMessageFromMCS(const MCSMessage& message);
[email protected]292af2b22014-08-06 19:42:45303 void ReceiveOnMessageSentToMCS(
304 const std::string& app_id,
305 const std::string& message_id,
306 const MCSClient::MessageSendStatus status);
zea76342abf2016-11-01 17:26:04307 void FailCheckin(net::HttpStatusCode response_code);
avi26062922015-12-26 00:14:18308 void CompleteCheckin(uint64_t android_id,
309 uint64_t security_token,
[email protected]764c0442014-05-01 04:30:55310 const std::string& digest,
311 const std::map<std::string, std::string>& settings);
zea76342abf2016-11-01 17:26:04312 void CompleteCheckinImpl(uint64_t android_id,
313 uint64_t security_token,
314 const std::string& digest,
315 const std::map<std::string, std::string>& settings,
316 net::HttpStatusCode response_code);
[email protected]848b1b62014-01-30 23:51:04317 void CompleteRegistration(const std::string& registration_id);
[email protected]e4007042014-02-15 20:34:28318 void CompleteUnregistration(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04319
[email protected]3a20a4d2014-03-21 22:54:21320 bool ExistsRegistration(const std::string& app_id) const;
321 void AddRegistration(const std::string& app_id,
322 const std::vector<std::string>& sender_ids,
323 const std::string& registration_id);
324
[email protected]848b1b62014-01-30 23:51:04325 // GCMClient::Delegate overrides (for verification).
Rayan Kanso3da434f2018-12-19 17:09:39326 void OnRegisterFinished(scoped_refptr<RegistrationInfo> registration_info,
dcheng00ea022b2014-10-21 11:24:56327 const std::string& registration_id,
328 GCMClient::Result result) override;
Rayan Kanso3da434f2018-12-19 17:09:39329 void OnUnregisterFinished(scoped_refptr<RegistrationInfo> registration_info,
330 GCMClient::Result result) override;
dcheng00ea022b2014-10-21 11:24:56331 void OnSendFinished(const std::string& app_id,
332 const std::string& message_id,
333 GCMClient::Result result) override {}
334 void OnMessageReceived(const std::string& registration_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06335 const IncomingMessage& message) override;
dcheng00ea022b2014-10-21 11:24:56336 void OnMessagesDeleted(const std::string& app_id) override;
337 void OnMessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12338 const std::string& app_id,
mostynbfe59f482014-10-06 15:04:46339 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
dcheng00ea022b2014-10-21 11:24:56340 void OnSendAcknowledged(const std::string& app_id,
341 const std::string& message_id) override;
fgorski5df101702014-10-28 02:09:31342 void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
343 const base::Time& last_token_fetch_time) override;
dcheng00ea022b2014-10-21 11:24:56344 void OnActivityRecorded() override {}
345 void OnConnected(const net::IPEndPoint& ip_endpoint) override {}
346 void OnDisconnected() override {}
johnme93ec7932016-11-17 14:26:58347 void OnStoreReset() override {}
[email protected]848b1b62014-01-30 23:51:04348
[email protected]848b1b62014-01-30 23:51:04349 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
jianlif3e52af42015-01-21 23:18:47350 GCMClientImpl::State gcm_client_state() const {
351 return gcm_client_->state_;
352 }
[email protected]848b1b62014-01-30 23:51:04353 FakeMCSClient* mcs_client() const {
354 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
355 }
[email protected]2bbe0a682014-03-26 00:08:31356 ConnectionFactory* connection_factory() const {
357 return gcm_client_->connection_factory_.get();
358 }
[email protected]848b1b62014-01-30 23:51:04359
[email protected]7df5ef22014-07-17 07:35:58360 const GCMClientImpl::CheckinInfo& device_checkin_info() const {
361 return gcm_client_->device_checkin_info_;
362 }
363
[email protected]3a20a4d2014-03-21 22:54:21364 void reset_last_event() {
365 last_event_ = NONE;
366 last_app_id_.clear();
367 last_registration_id_.clear();
368 last_message_id_.clear();
369 last_result_ = GCMClient::UNKNOWN_ERROR;
fgorski5df101702014-10-28 02:09:31370 last_account_mappings_.clear();
371 last_token_fetch_time_ = base::Time();
[email protected]3a20a4d2014-03-21 22:54:21372 }
373
[email protected]848b1b62014-01-30 23:51:04374 LastEvent last_event() const { return last_event_; }
375 const std::string& last_app_id() const { return last_app_id_; }
376 const std::string& last_registration_id() const {
377 return last_registration_id_;
378 }
379 const std::string& last_message_id() const { return last_message_id_; }
380 GCMClient::Result last_result() const { return last_result_; }
mvanouwerkerkf8633deb2015-07-13 11:04:06381 const IncomingMessage& last_message() const { return last_message_; }
[email protected]c6fe36b2014-03-11 10:58:12382 const GCMClient::SendErrorDetails& last_error_details() const {
383 return last_error_details_;
384 }
fgorski5df101702014-10-28 02:09:31385 const base::Time& last_token_fetch_time() const {
386 return last_token_fetch_time_;
387 }
388 const std::vector<AccountMapping>& last_account_mappings() {
389 return last_account_mappings_;
390 }
[email protected]848b1b62014-01-30 23:51:04391
[email protected]06e45272014-05-06 03:41:34392 const GServicesSettings& gservices_settings() const {
393 return gcm_client_->gservices_settings_;
[email protected]764c0442014-05-01 04:30:55394 }
395
jianli00b4600f2015-02-10 23:32:49396 const base::FilePath& temp_directory_path() const {
vabr0c237ae2016-09-14 09:24:28397 return temp_directory_.GetPath();
jianli00b4600f2015-02-10 23:32:49398 }
399
jianli9f9a73672015-06-16 19:39:34400 base::FilePath gcm_store_path() const {
401 // Pass an non-existent directory as store path to match the exact
402 // behavior in the production code. Currently GCMStoreImpl checks if
403 // the directory exist or not to determine the store existence.
vabr0c237ae2016-09-14 09:24:28404 return temp_directory_.GetPath().Append(FILE_PATH_LITERAL("GCM Store"));
jianli9f9a73672015-06-16 19:39:34405 }
406
avi26062922015-12-26 00:14:18407 int64_t CurrentTime();
[email protected]d3ba08d92014-04-04 23:03:55408
409 // Tooling.
[email protected]d3ba08d92014-04-04 23:03:55410 void PumpLoopUntilIdle();
[email protected]764c0442014-05-01 04:30:55411 bool CreateUniqueTempDir();
412 AutoAdvancingTestClock* clock() const {
tzik48479d92018-03-20 15:20:42413 return static_cast<AutoAdvancingTestClock*>(gcm_client_->clock_);
[email protected]a0144ceb2014-04-04 23:49:34414 }
Maks Orlovichc70c93c2018-07-12 02:45:44415 network::TestURLLoaderFactory* url_loader_factory() {
416 return &test_url_loader_factory_;
417 }
Hajime Hoshi7a5c8c82019-01-10 05:44:51418
419 void FastForwardBy(const base::TimeDelta& duration) {
Gabriel Charettedfa36042019-08-19 17:30:11420 task_environment_.FastForwardBy(duration);
jianli78b56042015-06-17 01:21:22421 }
[email protected]d3ba08d92014-04-04 23:03:55422
[email protected]764c0442014-05-01 04:30:55423 private:
Gabriel Charettedfa36042019-08-19 17:30:11424 base::test::TaskEnvironment task_environment_{
425 base::test::TaskEnvironment::TimeSource::MOCK_TIME};
Hajime Hoshi7a5c8c82019-01-10 05:44:51426
wkormanc1e71b62015-11-09 22:56:04427 // Must be declared first so that it is destroyed last. Injected to
428 // GCM client.
429 base::ScopedTempDir temp_directory_;
430
[email protected]848b1b62014-01-30 23:51:04431 // Variables used for verification.
432 LastEvent last_event_;
433 std::string last_app_id_;
434 std::string last_registration_id_;
435 std::string last_message_id_;
436 GCMClient::Result last_result_;
mvanouwerkerkf8633deb2015-07-13 11:04:06437 IncomingMessage last_message_;
[email protected]c6fe36b2014-03-11 10:58:12438 GCMClient::SendErrorDetails last_error_details_;
fgorski5df101702014-10-28 02:09:31439 base::Time last_token_fetch_time_;
440 std::vector<AccountMapping> last_account_mappings_;
[email protected]848b1b62014-01-30 23:51:04441
dchenga77e28eb2016-04-21 21:34:37442 std::unique_ptr<GCMClientImpl> gcm_client_;
[email protected]848b1b62014-01-30 23:51:04443
wkormanc1e71b62015-11-09 22:56:04444 // Injected to GCM client.
Maks Orlovichc70c93c2018-07-12 02:45:44445 network::TestURLLoaderFactory test_url_loader_factory_;
Mugdha Lakhanic3d792e2018-02-13 15:10:13446 base::test::ScopedFeatureList scoped_feature_list_;
[email protected]848b1b62014-01-30 23:51:04447};
448
449GCMClientImplTest::GCMClientImplTest()
Matt Menke9d045092019-10-03 17:54:21450 : last_event_(NONE), last_result_(GCMClient::UNKNOWN_ERROR) {}
[email protected]848b1b62014-01-30 23:51:04451
452GCMClientImplTest::~GCMClientImplTest() {}
453
454void GCMClientImplTest::SetUp() {
[email protected]fdeaf9732014-04-11 21:02:15455 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55456 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:55457 BuildGCMClient(base::TimeDelta());
[email protected]848b1b62014-01-30 23:51:04458 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53459 StartGCMClient();
Mugdha Lakhanic3d792e2018-02-13 15:10:13460 InitializeInvalidationFieldTrial();
johnme0fb2fc02016-07-14 14:47:44461 ASSERT_NO_FATAL_FAILURE(
462 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
463 std::map<std::string, std::string>()));
[email protected]848b1b62014-01-30 23:51:04464}
465
Mugdha Lakhanic3d792e2018-02-13 15:10:13466void GCMClientImplTest::TearDown() {
Mugdha Lakhanic3d792e2018-02-13 15:10:13467}
468
Alexei Svitkine8160de8d2019-07-02 00:06:37469void GCMClientImplTest::SetFeatureParams(const base::Feature& feature,
470 const base::FieldTrialParams& params) {
471 scoped_feature_list_.InitAndEnableFeatureWithParameters(feature, params);
472
473 base::FieldTrialParams actual_params;
Mugdha Lakhanic3d792e2018-02-13 15:10:13474 EXPECT_TRUE(base::GetFieldTrialParamsByFeature(
475 features::kInvalidateTokenFeature, &actual_params));
476 EXPECT_EQ(params, actual_params);
477}
478
479void GCMClientImplTest::InitializeInvalidationFieldTrial() {
Mugdha Lakhanic3d792e2018-02-13 15:10:13480 std::map<std::string, std::string> params;
481 params[features::kParamNameTokenInvalidationPeriodDays] =
482 std::to_string(kTestTokenInvalidationPeriod);
483 ASSERT_NO_FATAL_FAILURE(
484 SetFeatureParams(features::kInvalidateTokenFeature, std::move(params)));
Mugdha Lakhanic3d792e2018-02-13 15:10:13485}
486
[email protected]848b1b62014-01-30 23:51:04487void GCMClientImplTest::PumpLoopUntilIdle() {
Gabriel Charettedfa36042019-08-19 17:30:11488 task_environment_.RunUntilIdle();
[email protected]848b1b62014-01-30 23:51:04489}
490
[email protected]764c0442014-05-01 04:30:55491bool GCMClientImplTest::CreateUniqueTempDir() {
492 return temp_directory_.CreateUniqueTempDir();
493}
494
495void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
dchenga77e28eb2016-04-21 21:34:37496 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>(
[email protected]764c0442014-05-01 04:30:55497 new FakeGCMInternalsBuilder(clock_step))));
498}
499
zea76342abf2016-11-01 17:26:04500void GCMClientImplTest::FailCheckin(net::HttpStatusCode response_code) {
501 std::map<std::string, std::string> settings;
502 CompleteCheckinImpl(0, 0, GServicesSettings::CalculateDigest(settings),
503 settings, response_code);
504}
505
[email protected]764c0442014-05-01 04:30:55506void GCMClientImplTest::CompleteCheckin(
avi26062922015-12-26 00:14:18507 uint64_t android_id,
508 uint64_t security_token,
[email protected]764c0442014-05-01 04:30:55509 const std::string& digest,
510 const std::map<std::string, std::string>& settings) {
zea76342abf2016-11-01 17:26:04511 CompleteCheckinImpl(android_id, security_token, digest, settings,
512 net::HTTP_OK);
513}
514
515void GCMClientImplTest::CompleteCheckinImpl(
516 uint64_t android_id,
517 uint64_t security_token,
518 const std::string& digest,
519 const std::map<std::string, std::string>& settings,
520 net::HttpStatusCode response_code) {
[email protected]848b1b62014-01-30 23:51:04521 checkin_proto::AndroidCheckinResponse response;
522 response.set_stats_ok(true);
523 response.set_android_id(android_id);
524 response.set_security_token(security_token);
525
[email protected]764c0442014-05-01 04:30:55526 // For testing G-services settings.
527 if (!digest.empty()) {
528 response.set_digest(digest);
jdoerrie3feb1852018-10-05 12:16:44529 for (auto it = settings.begin(); it != settings.end(); ++it) {
[email protected]764c0442014-05-01 04:30:55530 checkin_proto::GservicesSetting* setting = response.add_setting();
531 setting->set_name(it->first);
532 setting->set_value(it->second);
533 }
[email protected]aae544d72014-05-20 06:53:10534 response.set_settings_diff(false);
[email protected]764c0442014-05-01 04:30:55535 }
536
[email protected]848b1b62014-01-30 23:51:04537 std::string response_string;
538 response.SerializeToString(&response_string);
539
Maks Orlovich278f2572018-07-16 18:18:41540 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
541 gservices_settings().GetCheckinURL(),
542 network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:27543 network::CreateURLResponseHead(response_code), response_string));
jianlid7e80f22015-06-18 22:21:31544 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
545 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04546}
547
548void GCMClientImplTest::CompleteRegistration(
549 const std::string& registration_id) {
550 std::string response(kRegistrationResponsePrefix);
551 response.append(registration_id);
Maks Orlovichc70c93c2018-07-12 02:45:44552
553 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
554 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:27555 network::CreateURLResponseHead(net::HTTP_OK), response));
Maks Orlovichc70c93c2018-07-12 02:45:44556
jianlid7e80f22015-06-18 22:21:31557 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
558 PumpLoopUntilIdle();
[email protected]e4007042014-02-15 20:34:28559}
560
561void GCMClientImplTest::CompleteUnregistration(
562 const std::string& app_id) {
563 std::string response(kUnregistrationResponsePrefix);
564 response.append(app_id);
Maks Orlovich278f2572018-07-16 18:18:41565
566 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
567 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:27568 network::CreateURLResponseHead(net::HTTP_OK), response));
Maks Orlovich278f2572018-07-16 18:18:41569
jianlid7e80f22015-06-18 22:21:31570 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
571 PumpLoopUntilIdle();
fgorskie45a34f2014-10-08 17:37:46572}
573
[email protected]3a20a4d2014-03-21 22:54:21574bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const {
jianli7a0c9b62015-05-26 23:24:47575 return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id);
[email protected]3a20a4d2014-03-21 22:54:21576}
577
578void GCMClientImplTest::AddRegistration(
579 const std::string& app_id,
580 const std::vector<std::string>& sender_ids,
581 const std::string& registration_id) {
Rayan Kanso3da434f2018-12-19 17:09:39582 auto registration = base::MakeRefCounted<GCMRegistrationInfo>();
jianli7a0c9b62015-05-26 23:24:47583 registration->app_id = app_id;
[email protected]3a20a4d2014-03-21 22:54:21584 registration->sender_ids = sender_ids;
Rayan Kanso3da434f2018-12-19 17:09:39585 gcm_client_->registrations_.emplace(std::move(registration), registration_id);
[email protected]848b1b62014-01-30 23:51:04586}
587
588void GCMClientImplTest::InitializeGCMClient() {
[email protected]848b1b62014-01-30 23:51:04589 clock()->Advance(base::TimeDelta::FromMilliseconds(1));
[email protected]2bbe0a682014-03-26 00:08:31590
[email protected]848b1b62014-01-30 23:51:04591 // Actual initialization.
[email protected]8ad80512014-05-23 09:40:47592 GCMClient::ChromeBuildInfo chrome_build_info;
jianlic02d25e2015-05-27 22:24:31593 chrome_build_info.version = kChromeVersion;
johnme627dc8c72016-08-19 21:49:39594 chrome_build_info.product_category_for_subtypes = kProductCategoryForSubtypes;
Maks Orlovichc70c93c2018-07-12 02:45:44595 gcm_client_->Initialize(
Hajime Hoshi7a5c8c82019-01-10 05:44:51596 chrome_build_info, gcm_store_path(),
Gabriel Charettedfa36042019-08-19 17:30:11597 task_environment_.GetMainThreadTaskRunner(),
Steven Zhub36394c2019-06-04 16:10:46598 base::ThreadTaskRunnerHandle::Get(), base::DoNothing(),
Maks Orlovichc70c93c2018-07-12 02:45:44599 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
600 &test_url_loader_factory_),
Robbie McElrathb01499332018-09-25 00:53:13601 network::TestNetworkConnectionTracker::GetInstance(),
Maks Orlovichc70c93c2018-07-12 02:45:44602 base::WrapUnique<Encryptor>(new FakeEncryptor), this);
[email protected]1abdf202014-06-13 19:38:53603}
[email protected]d3a4b2e2014-02-27 13:46:54604
[email protected]1abdf202014-06-13 19:38:53605void GCMClientImplTest::StartGCMClient() {
[email protected]2bbe0a682014-03-26 00:08:31606 // Start loading and check-in.
jianlif3e52af42015-01-21 23:18:47607 gcm_client_->Start(GCMClient::IMMEDIATE_START);
[email protected]d3a4b2e2014-02-27 13:46:54608
[email protected]848b1b62014-01-30 23:51:04609 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04610}
611
jianli7a0c9b62015-05-26 23:24:47612void GCMClientImplTest::Register(const std::string& app_id,
613 const std::vector<std::string>& senders) {
Rayan Kanso3da434f2018-12-19 17:09:39614 auto gcm_info = base::MakeRefCounted<GCMRegistrationInfo>();
jianli7a0c9b62015-05-26 23:24:47615 gcm_info->app_id = app_id;
616 gcm_info->sender_ids = senders;
Rayan Kanso3da434f2018-12-19 17:09:39617 gcm_client()->Register(std::move(gcm_info));
jianli7a0c9b62015-05-26 23:24:47618}
619
620void GCMClientImplTest::Unregister(const std::string& app_id) {
Rayan Kanso3da434f2018-12-19 17:09:39621 auto gcm_info = base::MakeRefCounted<GCMRegistrationInfo>();
jianli7a0c9b62015-05-26 23:24:47622 gcm_info->app_id = app_id;
Rayan Kanso3da434f2018-12-19 17:09:39623 gcm_client()->Unregister(std::move(gcm_info));
jianli7a0c9b62015-05-26 23:24:47624}
625
[email protected]848b1b62014-01-30 23:51:04626void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
[email protected]dd47c4ce2014-08-05 23:38:50627 gcm_client_->recorder_.RecordConnectionInitiated(std::string());
628 gcm_client_->recorder_.RecordConnectionSuccess();
[email protected]848b1b62014-01-30 23:51:04629 gcm_client_->OnMessageReceivedFromMCS(message);
630}
631
[email protected]292af2b22014-08-06 19:42:45632void GCMClientImplTest::ReceiveOnMessageSentToMCS(
633 const std::string& app_id,
634 const std::string& message_id,
635 const MCSClient::MessageSendStatus status) {
636 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status);
637}
638
fgorskid578c18b2014-09-24 23:40:17639void GCMClientImplTest::OnGCMReady(
fgorski5df101702014-10-28 02:09:31640 const std::vector<AccountMapping>& account_mappings,
641 const base::Time& last_token_fetch_time) {
[email protected]848b1b62014-01-30 23:51:04642 last_event_ = LOADING_COMPLETED;
fgorski5df101702014-10-28 02:09:31643 last_account_mappings_ = account_mappings;
644 last_token_fetch_time_ = last_token_fetch_time;
[email protected]848b1b62014-01-30 23:51:04645}
646
mvanouwerkerkf8633deb2015-07-13 11:04:06647void GCMClientImplTest::OnMessageReceived(const std::string& registration_id,
648 const IncomingMessage& message) {
[email protected]848b1b62014-01-30 23:51:04649 last_event_ = MESSAGE_RECEIVED;
650 last_app_id_ = registration_id;
651 last_message_ = message;
[email protected]848b1b62014-01-30 23:51:04652}
653
jianli7a0c9b62015-05-26 23:24:47654void GCMClientImplTest::OnRegisterFinished(
Rayan Kanso3da434f2018-12-19 17:09:39655 scoped_refptr<RegistrationInfo> registration_info,
jianli7a0c9b62015-05-26 23:24:47656 const std::string& registration_id,
657 GCMClient::Result result) {
[email protected]848b1b62014-01-30 23:51:04658 last_event_ = REGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47659 last_app_id_ = registration_info->app_id;
[email protected]848b1b62014-01-30 23:51:04660 last_registration_id_ = registration_id;
661 last_result_ = result;
662}
663
jianli7a0c9b62015-05-26 23:24:47664void GCMClientImplTest::OnUnregisterFinished(
Rayan Kanso3da434f2018-12-19 17:09:39665 scoped_refptr<RegistrationInfo> registration_info,
jianli7a0c9b62015-05-26 23:24:47666 GCMClient::Result result) {
[email protected]e4007042014-02-15 20:34:28667 last_event_ = UNREGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47668 last_app_id_ = registration_info->app_id;
[email protected]0e88e1d12014-03-19 06:53:08669 last_result_ = result;
[email protected]e4007042014-02-15 20:34:28670}
671
[email protected]848b1b62014-01-30 23:51:04672void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
673 last_event_ = MESSAGES_DELETED;
674 last_app_id_ = app_id;
675}
676
[email protected]c6fe36b2014-03-11 10:58:12677void GCMClientImplTest::OnMessageSendError(
678 const std::string& app_id,
679 const gcm::GCMClient::SendErrorDetails& send_error_details) {
[email protected]848b1b62014-01-30 23:51:04680 last_event_ = MESSAGE_SEND_ERROR;
681 last_app_id_ = app_id;
[email protected]c6fe36b2014-03-11 10:58:12682 last_error_details_ = send_error_details;
[email protected]848b1b62014-01-30 23:51:04683}
684
[email protected]292af2b22014-08-06 19:42:45685void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id,
686 const std::string& message_id) {
687 last_event_ = MESSAGE_SEND_ACK;
688 last_app_id_ = app_id;
689 last_message_id_ = message_id;
690}
691
avi26062922015-12-26 00:14:18692int64_t GCMClientImplTest::CurrentTime() {
[email protected]848b1b62014-01-30 23:51:04693 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
694}
695
[email protected]848b1b62014-01-30 23:51:04696TEST_F(GCMClientImplTest, LoadingCompleted) {
697 EXPECT_EQ(LOADING_COMPLETED, last_event());
698 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
699 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
[email protected]7df5ef22014-07-17 07:35:58700
701 // Checking freshly loaded CheckinInfo.
702 EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id);
703 EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret);
704 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
705 EXPECT_TRUE(device_checkin_info().accounts_set);
706 EXPECT_TRUE(device_checkin_info().account_tokens.empty());
[email protected]848b1b62014-01-30 23:51:04707}
708
jianli00b4600f2015-02-10 23:32:49709TEST_F(GCMClientImplTest, LoadingBusted) {
710 // Close the GCM store.
711 gcm_client()->Stop();
712 PumpLoopUntilIdle();
713
714 // Mess up the store.
Chris Mumforda884aa1f2017-10-24 22:46:32715 EXPECT_TRUE(leveldb_chrome::CorruptClosedDBForTesting(gcm_store_path()));
jianli00b4600f2015-02-10 23:32:49716
717 // Restart GCM client. The store should be reset and the loading should
718 // complete successfully.
719 reset_last_event();
720 BuildGCMClient(base::TimeDelta());
721 InitializeGCMClient();
722 StartGCMClient();
johnme0fb2fc02016-07-14 14:47:44723 ASSERT_NO_FATAL_FAILURE(
724 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(),
725 std::map<std::string, std::string>()));
jianli00b4600f2015-02-10 23:32:49726
727 EXPECT_EQ(LOADING_COMPLETED, last_event());
728 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
729 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
730}
731
johnme6d6c7802016-09-29 22:27:16732TEST_F(GCMClientImplTest, LoadingWithEmptyDirectory) {
733 // Close the GCM store.
734 gcm_client()->Stop();
735 PumpLoopUntilIdle();
736
737 // Make the store directory empty, to simulate a previous destroy store
738 // operation failing to delete the store directory.
Lei Zhang091d4232019-10-15 21:12:51739 ASSERT_TRUE(base::DeleteFileRecursively(gcm_store_path()));
johnme6d6c7802016-09-29 22:27:16740 ASSERT_TRUE(base::CreateDirectory(gcm_store_path()));
741
742 base::HistogramTester histogram_tester;
743
744 // Restart GCM client. The store should be considered to not exist.
745 BuildGCMClient(base::TimeDelta());
746 InitializeGCMClient();
747 gcm_client()->Start(GCMClient::DELAYED_START);
748 PumpLoopUntilIdle();
749 histogram_tester.ExpectUniqueSample("GCM.LoadStatus",
750 13 /* STORE_DOES_NOT_EXIST */, 1);
751 // Since the store does not exist, the database should not have been opened.
752 histogram_tester.ExpectTotalCount("GCM.Database.Open", 0);
753 // Without a store, DELAYED_START loading should only reach INITIALIZED state.
754 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
755
756 // The store directory should still exist (and be empty). If not, then the
757 // DELAYED_START load has probably reset the store, rather than leaving that
758 // to the next IMMEDIATE_START load as expected.
759 ASSERT_TRUE(base::DirectoryExists(gcm_store_path()));
760 ASSERT_FALSE(
761 base::PathExists(gcm_store_path().Append(FILE_PATH_LITERAL("CURRENT"))));
762
763 // IMMEDIATE_START loading should successfully create a new store despite the
764 // empty directory.
765 reset_last_event();
766 StartGCMClient();
767 ASSERT_NO_FATAL_FAILURE(
768 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(),
769 std::map<std::string, std::string>()));
770 EXPECT_EQ(LOADING_COMPLETED, last_event());
771 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
772 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
773 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
774}
775
jianli78b56042015-06-17 01:21:22776TEST_F(GCMClientImplTest, DestroyStoreWhenNotNeeded) {
777 // Close the GCM store.
778 gcm_client()->Stop();
779 PumpLoopUntilIdle();
780
781 // Restart GCM client. The store is loaded successfully.
782 reset_last_event();
783 BuildGCMClient(base::TimeDelta());
784 InitializeGCMClient();
785 gcm_client()->Start(GCMClient::DELAYED_START);
786 PumpLoopUntilIdle();
787
788 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
789 EXPECT_TRUE(device_checkin_info().android_id);
790 EXPECT_TRUE(device_checkin_info().secret);
791
792 // Fast forward the clock to trigger the store destroying logic.
Hajime Hoshi7a5c8c82019-01-10 05:44:51793 FastForwardBy(base::TimeDelta::FromMilliseconds(300000));
jianli78b56042015-06-17 01:21:22794 PumpLoopUntilIdle();
795
796 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
797 EXPECT_FALSE(device_checkin_info().android_id);
798 EXPECT_FALSE(device_checkin_info().secret);
799}
800
Mugdha Lakhani2ade6a02018-01-31 14:31:11801TEST_F(GCMClientImplTest, SerializeAndDeserialize) {
802 std::vector<std::string> senders{"sender"};
Rayan Kanso3da434f2018-12-19 17:09:39803 auto gcm_info = base::MakeRefCounted<GCMRegistrationInfo>();
Mugdha Lakhani2ade6a02018-01-31 14:31:11804 gcm_info->app_id = kExtensionAppId;
805 gcm_info->sender_ids = senders;
806 gcm_info->last_validated = clock()->Now();
807
Rayan Kanso3da434f2018-12-19 17:09:39808 auto gcm_info_deserialized = base::MakeRefCounted<GCMRegistrationInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02809 std::string gcm_registration_id_deserialized;
810 {
811 std::string serialized_key = gcm_info->GetSerializedKey();
812 std::string serialized_value =
813 gcm_info->GetSerializedValue(kRegistrationId);
814
815 ASSERT_TRUE(gcm_info_deserialized->Deserialize(
816 serialized_key, serialized_value, &gcm_registration_id_deserialized));
817 }
Mugdha Lakhani2ade6a02018-01-31 14:31:11818
819 EXPECT_EQ(gcm_info->app_id, gcm_info_deserialized->app_id);
820 EXPECT_EQ(gcm_info->sender_ids, gcm_info_deserialized->sender_ids);
821 EXPECT_EQ(gcm_info->last_validated, gcm_info_deserialized->last_validated);
Peter Beverloo1b76cbea2018-02-23 16:23:02822 EXPECT_EQ(kRegistrationId, gcm_registration_id_deserialized);
823
Rayan Kanso3da434f2018-12-19 17:09:39824 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02825 instance_id_info->app_id = kExtensionAppId;
826 instance_id_info->last_validated = clock()->Now();
827 instance_id_info->authorized_entity = "different_sender";
828 instance_id_info->scope = "scope";
829
Rayan Kanso3da434f2018-12-19 17:09:39830 auto instance_id_info_deserialized =
831 base::MakeRefCounted<InstanceIDTokenInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02832 std::string instance_id_registration_id_deserialized;
833 {
834 std::string serialized_key = instance_id_info->GetSerializedKey();
835 std::string serialized_value =
836 instance_id_info->GetSerializedValue(kRegistrationId);
837
838 ASSERT_TRUE(instance_id_info_deserialized->Deserialize(
839 serialized_key, serialized_value,
840 &instance_id_registration_id_deserialized));
841 }
842
843 EXPECT_EQ(instance_id_info->app_id, instance_id_info_deserialized->app_id);
844 EXPECT_EQ(instance_id_info->last_validated,
845 instance_id_info_deserialized->last_validated);
846 EXPECT_EQ(instance_id_info->authorized_entity,
847 instance_id_info_deserialized->authorized_entity);
848 EXPECT_EQ(instance_id_info->scope, instance_id_info_deserialized->scope);
849 EXPECT_EQ(kRegistrationId, instance_id_registration_id_deserialized);
Mugdha Lakhani2ade6a02018-01-31 14:31:11850}
851
[email protected]848b1b62014-01-30 23:51:04852TEST_F(GCMClientImplTest, RegisterApp) {
johnme627dc8c72016-08-19 21:49:39853 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21854
[email protected]848b1b62014-01-30 23:51:04855 std::vector<std::string> senders;
856 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39857 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44858 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
[email protected]848b1b62014-01-30 23:51:04859
860 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39861 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04862 EXPECT_EQ("reg_id", last_registration_id());
863 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39864 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21865}
866
Peter Beverloo71568a442018-03-01 16:07:41867TEST_F(GCMClientImplTest, RegisterAppFromCache) {
johnme627dc8c72016-08-19 21:49:39868 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21869
870 std::vector<std::string> senders;
871 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39872 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44873 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
johnme627dc8c72016-08-19 21:49:39874 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21875
johnme627dc8c72016-08-19 21:49:39876 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]3a20a4d2014-03-21 22:54:21877 EXPECT_EQ("reg_id", last_registration_id());
878 EXPECT_EQ(GCMClient::SUCCESS, last_result());
879 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
880
881 // Recreate GCMClient in order to load from the persistent store.
[email protected]764c0442014-05-01 04:30:55882 BuildGCMClient(base::TimeDelta());
[email protected]3a20a4d2014-03-21 22:54:21883 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53884 StartGCMClient();
[email protected]3a20a4d2014-03-21 22:54:21885
johnme627dc8c72016-08-19 21:49:39886 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]848b1b62014-01-30 23:51:04887}
888
jianli055634b2015-06-29 21:32:12889TEST_F(GCMClientImplTest, RegisterPreviousSenderAgain) {
johnme627dc8c72016-08-19 21:49:39890 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12891
892 // Register a sender.
893 std::vector<std::string> senders;
894 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39895 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44896 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianli055634b2015-06-29 21:32:12897
898 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39899 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12900 EXPECT_EQ("reg_id", last_registration_id());
901 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39902 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12903
904 reset_last_event();
905
906 // Register a different sender. Different registration ID from previous one
907 // should be returned.
908 std::vector<std::string> senders2;
909 senders2.push_back("sender2");
johnme627dc8c72016-08-19 21:49:39910 Register(kExtensionAppId, senders2);
johnme0fb2fc02016-07-14 14:47:44911 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2"));
jianli055634b2015-06-29 21:32:12912
913 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39914 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12915 EXPECT_EQ("reg_id2", last_registration_id());
916 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39917 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12918
919 reset_last_event();
920
921 // Register the 1st sender again. Different registration ID from previous one
922 // should be returned.
923 std::vector<std::string> senders3;
924 senders3.push_back("sender");
johnme627dc8c72016-08-19 21:49:39925 Register(kExtensionAppId, senders3);
johnme0fb2fc02016-07-14 14:47:44926 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianli055634b2015-06-29 21:32:12927
928 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39929 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12930 EXPECT_EQ("reg_id", last_registration_id());
931 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39932 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12933}
934
[email protected]617b22e92019-09-30 20:29:37935TEST_F(GCMClientImplTest, DISABLED_RegisterAgainWhenTokenIsFresh) {
Mugdha Lakhanic3d792e2018-02-13 15:10:13936 // Register a sender.
937 std::vector<std::string> senders;
938 senders.push_back("sender");
939 Register(kExtensionAppId, senders);
940 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
941 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
942 EXPECT_EQ(kExtensionAppId, last_app_id());
943 EXPECT_EQ("reg_id", last_registration_id());
944 EXPECT_EQ(GCMClient::SUCCESS, last_result());
945 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
946
947 reset_last_event();
948
949 // Advance time by (kTestTokenInvalidationPeriod)/2
950 clock()->Advance(base::TimeDelta::FromDays(kTestTokenInvalidationPeriod / 2));
951
952 // Register the same sender again. The same registration ID as the
953 // previous one should be returned, and we should *not* send a
954 // registration request to the GCM server.
955 Register(kExtensionAppId, senders);
956 PumpLoopUntilIdle();
957
958 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
959 EXPECT_EQ(kExtensionAppId, last_app_id());
960 EXPECT_EQ("reg_id", last_registration_id());
961 EXPECT_EQ(GCMClient::SUCCESS, last_result());
962 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
963}
964
965TEST_F(GCMClientImplTest, RegisterAgainWhenTokenIsStale) {
966 // Register a sender.
967 std::vector<std::string> senders;
968 senders.push_back("sender");
969 Register(kExtensionAppId, senders);
970 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
971
972 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
973 EXPECT_EQ(kExtensionAppId, last_app_id());
974 EXPECT_EQ("reg_id", last_registration_id());
975 EXPECT_EQ(GCMClient::SUCCESS, last_result());
976 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
977
978 reset_last_event();
979
980 // Advance time by kTestTokenInvalidationPeriod
981 clock()->Advance(base::TimeDelta::FromDays(kTestTokenInvalidationPeriod));
982
983 // Register the same sender again. Different registration ID from the
984 // previous one should be returned.
985 Register(kExtensionAppId, senders);
986 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2"));
987
988 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
989 EXPECT_EQ(kExtensionAppId, last_app_id());
990 EXPECT_EQ("reg_id2", last_registration_id());
991 EXPECT_EQ(GCMClient::SUCCESS, last_result());
992 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
993}
994
[email protected]e4007042014-02-15 20:34:28995TEST_F(GCMClientImplTest, UnregisterApp) {
johnme627dc8c72016-08-19 21:49:39996 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21997
998 std::vector<std::string> senders;
999 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391000 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:441001 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
johnme627dc8c72016-08-19 21:49:391002 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:211003
johnme627dc8c72016-08-19 21:49:391004 Unregister(kExtensionAppId);
1005 ASSERT_NO_FATAL_FAILURE(CompleteUnregistration(kExtensionAppId));
[email protected]e4007042014-02-15 20:34:281006
1007 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391008 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]e4007042014-02-15 20:34:281009 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391010 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]e4007042014-02-15 20:34:281011}
1012
fgorskie45a34f2014-10-08 17:37:461013// Tests that stopping the GCMClient also deletes pending registration requests.
1014// This is tested by checking that url fetcher contained in the request was
1015// deleted.
1016TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) {
1017 std::vector<std::string> senders;
1018 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391019 Register(kExtensionAppId, senders);
fgorskie45a34f2014-10-08 17:37:461020
1021 gcm_client()->Stop();
jianlid7e80f22015-06-18 22:21:311022 PumpLoopUntilIdle();
Maks Orlovichc70c93c2018-07-12 02:45:441023 EXPECT_EQ(0, url_loader_factory()->NumPending());
fgorskie45a34f2014-10-08 17:37:461024}
1025
[email protected]848b1b62014-01-30 23:51:041026TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
[email protected]3a20a4d2014-03-21 22:54:211027 // Register to receive messages from kSender and kSender2 only.
1028 std::vector<std::string> senders;
1029 senders.push_back(kSender);
1030 senders.push_back(kSender2);
johnme627dc8c72016-08-19 21:49:391031 AddRegistration(kExtensionAppId, senders, "reg_id");
[email protected]3a20a4d2014-03-21 22:54:211032
[email protected]848b1b62014-01-30 23:51:041033 std::map<std::string, std::string> expected_data;
1034 expected_data["message_type"] = "gcm";
1035 expected_data["key"] = "value";
1036 expected_data["key2"] = "value2";
[email protected]3a20a4d2014-03-21 22:54:211037
1038 // Message for kSender will be received.
johnme627dc8c72016-08-19 21:49:391039 MCSMessage message(BuildDownstreamMessage(
1040 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1041 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041042 EXPECT_TRUE(message.IsValid());
1043 ReceiveMessageFromMCS(message);
1044
1045 expected_data.erase(expected_data.find("message_type"));
1046 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391047 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:041048 EXPECT_EQ(expected_data.size(), last_message().data.size());
1049 EXPECT_EQ(expected_data, last_message().data);
[email protected]3a20a4d2014-03-21 22:54:211050 EXPECT_EQ(kSender, last_message().sender_id);
1051
1052 reset_last_event();
1053
1054 // Message for kSender2 will be received.
johnme627dc8c72016-08-19 21:49:391055 MCSMessage message2(BuildDownstreamMessage(
1056 kSender2, kExtensionAppId, std::string() /* subtype */, expected_data,
1057 std::string() /* raw_data */));
[email protected]3a20a4d2014-03-21 22:54:211058 EXPECT_TRUE(message2.IsValid());
1059 ReceiveMessageFromMCS(message2);
1060
1061 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391062 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]3a20a4d2014-03-21 22:54:211063 EXPECT_EQ(expected_data.size(), last_message().data.size());
1064 EXPECT_EQ(expected_data, last_message().data);
1065 EXPECT_EQ(kSender2, last_message().sender_id);
[email protected]848b1b62014-01-30 23:51:041066}
1067
petera5aedc52015-07-22 10:47:301068TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) {
1069 std::vector<std::string> senders(1, kSender);
johnme627dc8c72016-08-19 21:49:391070 AddRegistration(kExtensionAppId, senders, "reg_id");
petera5aedc52015-07-22 10:47:301071
1072 std::map<std::string, std::string> expected_data;
1073
johnme627dc8c72016-08-19 21:49:391074 MCSMessage message(BuildDownstreamMessage(kSender, kExtensionAppId,
1075 std::string() /* subtype */,
1076 expected_data, kRawData));
petera5aedc52015-07-22 10:47:301077 EXPECT_TRUE(message.IsValid());
1078 ReceiveMessageFromMCS(message);
1079
1080 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391081 EXPECT_EQ(kExtensionAppId, last_app_id());
petera5aedc52015-07-22 10:47:301082 EXPECT_EQ(expected_data.size(), last_message().data.size());
1083 EXPECT_EQ(kSender, last_message().sender_id);
1084 EXPECT_EQ(kRawData, last_message().raw_data);
1085}
1086
[email protected]617b22e92019-09-30 20:29:371087TEST_F(GCMClientImplTest, DISABLED_DispatchDownstreamMessageSendError) {
Rayan Kansobcec0292019-05-09 13:27:511088 std::map<std::string, std::string> expected_data = {
1089 {"message_type", "send_error"}, {"error_details", "some details"}};
1090
johnme627dc8c72016-08-19 21:49:391091 MCSMessage message(BuildDownstreamMessage(
1092 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1093 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041094 EXPECT_TRUE(message.IsValid());
1095 ReceiveMessageFromMCS(message);
1096
1097 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
johnme627dc8c72016-08-19 21:49:391098 EXPECT_EQ(kExtensionAppId, last_app_id());
Rayan Kansobcec0292019-05-09 13:27:511099 EXPECT_EQ(kMessageId, last_error_details().message_id);
[email protected]c6fe36b2014-03-11 10:58:121100 EXPECT_EQ(1UL, last_error_details().additional_data.size());
jdoerrie3feb1852018-10-05 12:16:441101 auto iter = last_error_details().additional_data.find("error_details");
[email protected]c6fe36b2014-03-11 10:58:121102 EXPECT_TRUE(iter != last_error_details().additional_data.end());
1103 EXPECT_EQ("some details", iter->second);
[email protected]848b1b62014-01-30 23:51:041104}
1105
1106TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
1107 std::map<std::string, std::string> expected_data;
1108 expected_data["message_type"] = "deleted_messages";
johnme627dc8c72016-08-19 21:49:391109 MCSMessage message(BuildDownstreamMessage(
1110 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1111 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041112 EXPECT_TRUE(message.IsValid());
1113 ReceiveMessageFromMCS(message);
1114
1115 EXPECT_EQ(MESSAGES_DELETED, last_event());
johnme627dc8c72016-08-19 21:49:391116 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:041117}
1118
1119TEST_F(GCMClientImplTest, SendMessage) {
mvanouwerkerkf8633deb2015-07-13 11:04:061120 OutgoingMessage message;
[email protected]848b1b62014-01-30 23:51:041121 message.id = "007";
1122 message.time_to_live = 500;
1123 message.data["key"] = "value";
johnme627dc8c72016-08-19 21:49:391124 gcm_client()->Send(kExtensionAppId, kSender, message);
[email protected]848b1b62014-01-30 23:51:041125
1126 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
johnme627dc8c72016-08-19 21:49:391127 EXPECT_EQ(kExtensionAppId,
1128 mcs_client()->last_data_message_stanza().category());
[email protected]3a20a4d2014-03-21 22:54:211129 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:041130 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
1131 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
1132 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
[email protected]848b1b62014-01-30 23:51:041133 EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from());
[email protected]3a20a4d2014-03-21 22:54:211134 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:041135 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
1136 EXPECT_EQ("value",
1137 mcs_client()->last_data_message_stanza().app_data(0).value());
1138}
1139
[email protected]292af2b22014-08-06 19:42:451140TEST_F(GCMClientImplTest, SendMessageAcknowledged) {
johnme627dc8c72016-08-19 21:49:391141 ReceiveOnMessageSentToMCS(kExtensionAppId, "007", MCSClient::SENT);
[email protected]292af2b22014-08-06 19:42:451142 EXPECT_EQ(MESSAGE_SEND_ACK, last_event());
johnme627dc8c72016-08-19 21:49:391143 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]292af2b22014-08-06 19:42:451144 EXPECT_EQ("007", last_message_id());
1145}
1146
[email protected]764c0442014-05-01 04:30:551147class GCMClientImplCheckinTest : public GCMClientImplTest {
1148 public:
1149 GCMClientImplCheckinTest();
dcheng30a1b1542014-10-29 21:27:501150 ~GCMClientImplCheckinTest() override;
[email protected]764c0442014-05-01 04:30:551151
dcheng30a1b1542014-10-29 21:27:501152 void SetUp() override;
[email protected]764c0442014-05-01 04:30:551153};
1154
1155GCMClientImplCheckinTest::GCMClientImplCheckinTest() {
1156}
1157
1158GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {
1159}
1160
1161void GCMClientImplCheckinTest::SetUp() {
1162 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:551163 // Creating unique temp directory that will be used by GCMStore shared between
1164 // GCM Client and G-services settings.
1165 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:551166 // Time will be advancing one hour every time it is checked.
1167 BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval));
1168 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:531169 StartGCMClient();
[email protected]764c0442014-05-01 04:30:551170}
1171
1172TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
1173 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031174 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]764c0442014-05-01 04:30:551175 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131176 settings["gcm_hostname"] = "alternative.gcm.host";
1177 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:551178 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441179 ASSERT_NO_FATAL_FAILURE(
1180 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1181 GServicesSettings::CalculateDigest(settings), settings));
[email protected]f09354512014-05-02 00:51:131182 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:101183 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:341184 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:101185 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:341186 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:101187 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:131188 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:101189 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:131190 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:101191 gservices_settings().GetMCSFallbackEndpoint());
[email protected]764c0442014-05-01 04:30:551192}
1193
1194// This test only checks that periodic checkin happens.
1195TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
1196 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031197 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]764c0442014-05-01 04:30:551198 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131199 settings["gcm_hostname"] = "alternative.gcm.host";
1200 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:551201 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441202 ASSERT_NO_FATAL_FAILURE(
1203 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1204 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581205
[email protected]764c0442014-05-01 04:30:551206 EXPECT_EQ(2, clock()->call_count());
1207
1208 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441209 ASSERT_NO_FATAL_FAILURE(
1210 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1211 GServicesSettings::CalculateDigest(settings), settings));
[email protected]764c0442014-05-01 04:30:551212}
1213
[email protected]06e45272014-05-06 03:41:341214TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) {
1215 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031216 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]06e45272014-05-06 03:41:341217 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131218 settings["gcm_hostname"] = "alternative.gcm.host";
1219 settings["gcm_secure_port"] = "7777";
[email protected]06e45272014-05-06 03:41:341220 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441221 ASSERT_NO_FATAL_FAILURE(
1222 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1223 GServicesSettings::CalculateDigest(settings), settings));
[email protected]06e45272014-05-06 03:41:341224
1225 BuildGCMClient(base::TimeDelta());
1226 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:531227 StartGCMClient();
[email protected]06e45272014-05-06 03:41:341228
1229 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:101230 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:341231 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:101232 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:341233 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:101234 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:131235 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:101236 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:131237 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:101238 gservices_settings().GetMCSFallbackEndpoint());
[email protected]06e45272014-05-06 03:41:341239}
1240
[email protected]7df5ef22014-07-17 07:35:581241// This test only checks that periodic checkin happens.
1242TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) {
1243 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031244 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581245 settings["checkin_url"] = "http://alternative.url/checkin";
1246 settings["gcm_hostname"] = "alternative.gcm.host";
1247 settings["gcm_secure_port"] = "7777";
1248 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441249 ASSERT_NO_FATAL_FAILURE(
1250 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1251 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581252
fgorski58b9dfc2014-09-29 16:46:181253 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1254 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1255 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1256 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581257
1258 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
1259 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181260 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1261 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581262
1263 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441264 ASSERT_NO_FATAL_FAILURE(
1265 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1266 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581267
1268 std::set<std::string> accounts;
1269 accounts.insert("[email protected]");
1270 accounts.insert("[email protected]");
1271 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1272 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181273 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1274 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581275}
1276
1277// This test only checks that periodic checkin happens.
1278TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) {
1279 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031280 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581281 settings["checkin_url"] = "http://alternative.url/checkin";
1282 settings["gcm_hostname"] = "alternative.gcm.host";
1283 settings["gcm_secure_port"] = "7777";
1284 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441285 ASSERT_NO_FATAL_FAILURE(
1286 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1287 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581288
fgorski58b9dfc2014-09-29 16:46:181289 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1290 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1291 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1292 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581293 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441294 ASSERT_NO_FATAL_FAILURE(
1295 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1296 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581297
1298 EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size());
1299 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181300 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1301 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581302
fgorski58b9dfc2014-09-29 16:46:181303 account_tokens.erase(account_tokens.begin() + 1);
1304 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581305
1306 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441307 ASSERT_NO_FATAL_FAILURE(
1308 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1309 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581310
1311 std::set<std::string> accounts;
1312 accounts.insert("[email protected]");
1313 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1314 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181315 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1316 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581317}
1318
1319// This test only checks that periodic checkin happens.
1320TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) {
1321 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031322 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581323 settings["checkin_url"] = "http://alternative.url/checkin";
1324 settings["gcm_hostname"] = "alternative.gcm.host";
1325 settings["gcm_secure_port"] = "7777";
1326 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441327 ASSERT_NO_FATAL_FAILURE(
1328 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1329 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581330
fgorski58b9dfc2014-09-29 16:46:181331 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1332 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1333 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581334
1335 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441336 ASSERT_NO_FATAL_FAILURE(
1337 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1338 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581339
1340 std::set<std::string> accounts;
1341 accounts.insert("[email protected]");
1342 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1343
1344 // This should trigger another checkin, because the list of accounts is
1345 // different.
fgorski58b9dfc2014-09-29 16:46:181346 account_tokens.clear();
1347 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1348 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581349
1350 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441351 ASSERT_NO_FATAL_FAILURE(
1352 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1353 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581354
1355 accounts.clear();
1356 accounts.insert("[email protected]");
1357 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1358 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181359 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1360 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581361}
1362
zea76342abf2016-11-01 17:26:041363TEST_F(GCMClientImplCheckinTest, ResetStoreWhenCheckinRejected) {
1364 base::HistogramTester histogram_tester;
1365 std::map<std::string, std::string> settings;
1366 ASSERT_NO_FATAL_FAILURE(FailCheckin(net::HTTP_UNAUTHORIZED));
1367 PumpLoopUntilIdle();
1368
1369 // Store should have been destroyed. Restart client and verify the initial
1370 // checkin response is persisted.
1371 BuildGCMClient(base::TimeDelta());
1372 InitializeGCMClient();
1373 StartGCMClient();
1374 ASSERT_NO_FATAL_FAILURE(
1375 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2,
1376 GServicesSettings::CalculateDigest(settings), settings));
1377
1378 EXPECT_EQ(LOADING_COMPLETED, last_event());
1379 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
1380 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
1381}
1382
[email protected]1abdf202014-06-13 19:38:531383class GCMClientImplStartAndStopTest : public GCMClientImplTest {
jianli78b56042015-06-17 01:21:221384 public:
[email protected]1abdf202014-06-13 19:38:531385 GCMClientImplStartAndStopTest();
dcheng30a1b1542014-10-29 21:27:501386 ~GCMClientImplStartAndStopTest() override;
[email protected]1abdf202014-06-13 19:38:531387
dcheng30a1b1542014-10-29 21:27:501388 void SetUp() override;
fgorski5df101702014-10-28 02:09:311389
1390 void DefaultCompleteCheckin();
[email protected]1abdf202014-06-13 19:38:531391};
1392
1393GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() {
1394}
1395
1396GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() {
1397}
1398
1399void GCMClientImplStartAndStopTest::SetUp() {
1400 testing::Test::SetUp();
1401 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]1abdf202014-06-13 19:38:531402 BuildGCMClient(base::TimeDelta());
1403 InitializeGCMClient();
1404}
1405
fgorski5df101702014-10-28 02:09:311406void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() {
johnme0fb2fc02016-07-14 14:47:441407 ASSERT_NO_FATAL_FAILURE(
1408 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
1409 std::map<std::string, std::string>()));
fgorski5df101702014-10-28 02:09:311410 PumpLoopUntilIdle();
1411}
1412
[email protected]617b22e92019-09-30 20:29:371413TEST_F(GCMClientImplStartAndStopTest, DISABLED_StartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471414 // GCMClientImpl should be in INITIALIZED state at first.
1415 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1416
1417 // Delay start the GCM.
1418 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531419 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341420 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531421
1422 // Stop the GCM.
1423 gcm_client()->Stop();
1424 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471425 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531426
jianlif3e52af42015-01-21 23:18:471427 // Restart the GCM without delay.
1428 gcm_client()->Start(GCMClient::IMMEDIATE_START);
[email protected]1abdf202014-06-13 19:38:531429 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471430 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531431}
1432
jianli9f9a73672015-06-16 19:39:341433TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) {
jianlif3e52af42015-01-21 23:18:471434 // GCMClientImpl should be in INITIALIZED state at first.
1435 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531436
jianlif3e52af42015-01-21 23:18:471437 // Delay start the GCM and then stop it immediately.
1438 gcm_client()->Start(GCMClient::DELAYED_START);
1439 gcm_client()->Stop();
[email protected]1abdf202014-06-13 19:38:531440 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471441 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341442}
1443
1444TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) {
1445 // GCMClientImpl should be in INITIALIZED state at first.
1446 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471447
1448 // Start the GCM and then stop it immediately.
1449 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1450 gcm_client()->Stop();
1451 PumpLoopUntilIdle();
1452 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531453}
1454
jianli9f9a73672015-06-16 19:39:341455TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471456 // GCMClientImpl should be in INITIALIZED state at first.
1457 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531458
jianlif3e52af42015-01-21 23:18:471459 // Delay start the GCM and then stop and restart it immediately.
1460 gcm_client()->Start(GCMClient::DELAYED_START);
1461 gcm_client()->Stop();
1462 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531463 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341464 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1465}
1466
1467TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) {
1468 // GCMClientImpl should be in INITIALIZED state at first.
1469 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471470
1471 // Start the GCM and then stop and restart it immediately.
1472 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1473 gcm_client()->Stop();
1474 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1475 PumpLoopUntilIdle();
1476 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1477}
1478
jianli9f9a73672015-06-16 19:39:341479TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) {
jianlif3e52af42015-01-21 23:18:471480 // GCMClientImpl should be in INITIALIZED state at first.
1481 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1482
jianli9f9a73672015-06-16 19:39:341483 // Start the GCM immediately and complete the checkin.
1484 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1485 PumpLoopUntilIdle();
1486 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441487 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianli9f9a73672015-06-16 19:39:341488 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1489
1490 // Stop the GCM.
1491 gcm_client()->Stop();
1492 PumpLoopUntilIdle();
1493 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1494
1495 // Start the GCM immediately. GCMClientImpl should be in READY state.
1496 BuildGCMClient(base::TimeDelta());
1497 InitializeGCMClient();
1498 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1499 PumpLoopUntilIdle();
1500 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1501}
1502
1503TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) {
1504 // GCMClientImpl should be in INITIALIZED state at first.
1505 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1506
1507 // Start the GCM immediately and complete the checkin.
1508 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1509 PumpLoopUntilIdle();
1510 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441511 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianli9f9a73672015-06-16 19:39:341512 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1513
1514 // Stop the GCM.
1515 gcm_client()->Stop();
1516 PumpLoopUntilIdle();
1517 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1518
1519 // Delay start the GCM. GCMClientImpl should be in LOADED state.
1520 BuildGCMClient(base::TimeDelta());
1521 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471522 gcm_client()->Start(GCMClient::DELAYED_START);
1523 PumpLoopUntilIdle();
1524 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341525}
1526
[email protected]617b22e92019-09-30 20:29:371527TEST_F(GCMClientImplStartAndStopTest, DISABLED_DelayedStartRace) {
johnme902c4062015-08-21 22:06:171528 // GCMClientImpl should be in INITIALIZED state at first.
1529 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1530
1531 // Delay start the GCM, then start it immediately while it's still loading.
1532 gcm_client()->Start(GCMClient::DELAYED_START);
1533 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1534 PumpLoopUntilIdle();
1535 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441536 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
johnme902c4062015-08-21 22:06:171537 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1538}
1539
jianli9f9a73672015-06-16 19:39:341540TEST_F(GCMClientImplStartAndStopTest, DelayedStart) {
1541 // GCMClientImpl should be in INITIALIZED state at first.
1542 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1543
1544 // Delay start the GCM. The store will not be loaded and GCMClientImpl should
1545 // still be in INITIALIZED state.
1546 gcm_client()->Start(GCMClient::DELAYED_START);
1547 PumpLoopUntilIdle();
1548 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471549
1550 // Start the GCM immediately and complete the checkin.
1551 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1552 PumpLoopUntilIdle();
1553 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441554 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianlif3e52af42015-01-21 23:18:471555 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1556
1557 // Registration.
1558 std::vector<std::string> senders;
1559 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391560 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:441561 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianlif3e52af42015-01-21 23:18:471562 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1563
1564 // Stop the GCM.
1565 gcm_client()->Stop();
1566 PumpLoopUntilIdle();
1567 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1568
1569 // Delay start the GCM. GCM is indeed started without delay because the
1570 // registration record has been found.
jianli9f9a73672015-06-16 19:39:341571 BuildGCMClient(base::TimeDelta());
1572 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471573 gcm_client()->Start(GCMClient::DELAYED_START);
1574 PumpLoopUntilIdle();
1575 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531576}
1577
fgorski5df101702014-10-28 02:09:311578// Test for known account mappings and last token fetching time being passed
1579// to OnGCMReady.
1580TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) {
1581 // Start the GCM and wait until it is ready.
jianlif3e52af42015-01-21 23:18:471582 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311583 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441584 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
fgorski5df101702014-10-28 02:09:311585
1586 base::Time expected_time = base::Time::Now();
1587 gcm_client()->SetLastTokenFetchTime(expected_time);
1588 AccountMapping expected_mapping;
Tanmoy Mollik93ed1532019-11-07 11:53:121589 expected_mapping.account_id = CoreAccountId("accId");
fgorski5df101702014-10-28 02:09:311590 expected_mapping.email = "[email protected]";
1591 expected_mapping.status = AccountMapping::MAPPED;
1592 expected_mapping.status_change_timestamp = expected_time;
1593 gcm_client()->UpdateAccountMapping(expected_mapping);
1594 PumpLoopUntilIdle();
1595
1596 // Stop the GCM.
1597 gcm_client()->Stop();
1598 PumpLoopUntilIdle();
1599
1600 // Restart the GCM.
jianlif3e52af42015-01-21 23:18:471601 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311602 PumpLoopUntilIdle();
1603
1604 EXPECT_EQ(LOADING_COMPLETED, last_event());
1605 EXPECT_EQ(expected_time, last_token_fetch_time());
1606 ASSERT_EQ(1UL, last_account_mappings().size());
1607 const AccountMapping& actual_mapping = last_account_mappings()[0];
1608 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id);
1609 EXPECT_EQ(expected_mapping.email, actual_mapping.email);
1610 EXPECT_EQ(expected_mapping.status, actual_mapping.status);
1611 EXPECT_EQ(expected_mapping.status_change_timestamp,
1612 actual_mapping.status_change_timestamp);
1613}
1614
jianlic02d25e2015-05-27 22:24:311615
1616class GCMClientInstanceIDTest : public GCMClientImplTest {
1617 public:
1618 GCMClientInstanceIDTest();
1619 ~GCMClientInstanceIDTest() override;
1620
1621 void AddInstanceID(const std::string& app_id,
1622 const std::string& instance_id);
1623 void RemoveInstanceID(const std::string& app_id);
1624 void GetToken(const std::string& app_id,
1625 const std::string& authorized_entity,
1626 const std::string& scope);
1627 void DeleteToken(const std::string& app_id,
1628 const std::string& authorized_entity,
1629 const std::string& scope);
1630 void CompleteDeleteToken();
1631 bool ExistsToken(const std::string& app_id,
1632 const std::string& authorized_entity,
1633 const std::string& scope) const;
1634};
1635
1636GCMClientInstanceIDTest::GCMClientInstanceIDTest() {
1637}
1638
1639GCMClientInstanceIDTest::~GCMClientInstanceIDTest() {
1640}
1641
1642void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id,
1643 const std::string& instance_id) {
1644 gcm_client()->AddInstanceIDData(app_id, instance_id, "123");
1645}
1646
1647void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) {
1648 gcm_client()->RemoveInstanceIDData(app_id);
1649}
1650
1651void GCMClientInstanceIDTest::GetToken(const std::string& app_id,
1652 const std::string& authorized_entity,
1653 const std::string& scope) {
Rayan Kanso3da434f2018-12-19 17:09:391654 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
jianlic02d25e2015-05-27 22:24:311655 instance_id_info->app_id = app_id;
1656 instance_id_info->authorized_entity = authorized_entity;
1657 instance_id_info->scope = scope;
Rayan Kanso3da434f2018-12-19 17:09:391658 gcm_client()->Register(std::move(instance_id_info));
jianlic02d25e2015-05-27 22:24:311659}
1660
1661void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id,
1662 const std::string& authorized_entity,
1663 const std::string& scope) {
Rayan Kanso3da434f2018-12-19 17:09:391664 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
jianlic02d25e2015-05-27 22:24:311665 instance_id_info->app_id = app_id;
1666 instance_id_info->authorized_entity = authorized_entity;
1667 instance_id_info->scope = scope;
Rayan Kanso3da434f2018-12-19 17:09:391668 gcm_client()->Unregister(std::move(instance_id_info));
jianlic02d25e2015-05-27 22:24:311669}
1670
1671void GCMClientInstanceIDTest::CompleteDeleteToken() {
1672 std::string response(kDeleteTokenResponse);
Maks Orlovich278f2572018-07-16 18:18:411673
1674 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
1675 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:271676 network::CreateURLResponseHead(net::HTTP_OK), response));
Maks Orlovich278f2572018-07-16 18:18:411677
jianlid7e80f22015-06-18 22:21:311678 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
1679 PumpLoopUntilIdle();
jianlic02d25e2015-05-27 22:24:311680}
1681
1682bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id,
1683 const std::string& authorized_entity,
1684 const std::string& scope) const {
Rayan Kanso3da434f2018-12-19 17:09:391685 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
jianlic02d25e2015-05-27 22:24:311686 instance_id_info->app_id = app_id;
1687 instance_id_info->authorized_entity = authorized_entity;
1688 instance_id_info->scope = scope;
Rayan Kanso3da434f2018-12-19 17:09:391689 return gcm_client()->registrations_.count(std::move(instance_id_info)) > 0;
jianlic02d25e2015-05-27 22:24:311690}
1691
1692TEST_F(GCMClientInstanceIDTest, GetToken) {
johnme627dc8c72016-08-19 21:49:391693 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311694
1695 // Get a token.
johnme627dc8c72016-08-19 21:49:391696 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1697 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441698 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311699
1700 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391701 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311702 EXPECT_EQ("token1", last_registration_id());
1703 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391704 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311705
1706 // Get another token.
johnme627dc8c72016-08-19 21:49:391707 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1708 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441709 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311710
1711 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391712 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311713 EXPECT_EQ("token2", last_registration_id());
1714 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391715 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311716 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391717 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
1718}
1719
1720// Most tests in this file use kExtensionAppId which is special-cased by
1721// InstanceIDUsesSubtypeForAppId in gcm_client_impl.cc. This test uses
1722// kSubtypeAppId to cover the alternate case.
1723TEST_F(GCMClientInstanceIDTest, GetTokenWithSubtype) {
1724 ASSERT_EQ(GCMClientImpl::READY, gcm_client_state());
1725
1726 AddInstanceID(kSubtypeAppId, kInstanceID);
1727
1728 EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope));
1729
1730 // Get a token.
1731 GetToken(kSubtypeAppId, kSender, kScope);
1732 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1733 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1734 EXPECT_EQ(kSubtypeAppId, last_app_id());
1735 EXPECT_EQ("token1", last_registration_id());
1736 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1737 EXPECT_TRUE(ExistsToken(kSubtypeAppId, kSender, kScope));
1738
1739 // Delete the token.
1740 DeleteToken(kSubtypeAppId, kSender, kScope);
1741 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
1742 EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311743}
1744
jianli70715cc2015-06-23 21:46:481745TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) {
johnme627dc8c72016-08-19 21:49:391746 AddInstanceID(kExtensionAppId, kInstanceID);
jianli70715cc2015-06-23 21:46:481747
1748 // Delete an invalid token.
johnme627dc8c72016-08-19 21:49:391749 DeleteToken(kExtensionAppId, "Foo@#$", kScope);
jianli70715cc2015-06-23 21:46:481750 PumpLoopUntilIdle();
1751
1752 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391753 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481754 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1755
1756 reset_last_event();
1757
1758 // Delete a non-existing token.
johnme627dc8c72016-08-19 21:49:391759 DeleteToken(kExtensionAppId, kSender, kScope);
jianli70715cc2015-06-23 21:46:481760 PumpLoopUntilIdle();
1761
1762 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391763 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481764 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1765}
1766
jianlic02d25e2015-05-27 22:24:311767TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) {
johnme627dc8c72016-08-19 21:49:391768 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311769
1770 // Get a token.
johnme627dc8c72016-08-19 21:49:391771 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1772 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441773 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311774
1775 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391776 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311777 EXPECT_EQ("token1", last_registration_id());
1778 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391779 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311780
jianli70715cc2015-06-23 21:46:481781 reset_last_event();
1782
jianlic02d25e2015-05-27 22:24:311783 // Get another token.
johnme627dc8c72016-08-19 21:49:391784 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1785 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441786 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311787
1788 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391789 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311790 EXPECT_EQ("token2", last_registration_id());
1791 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391792 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311793 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391794 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311795
jianli70715cc2015-06-23 21:46:481796 reset_last_event();
1797
jianlic02d25e2015-05-27 22:24:311798 // Delete the 2nd token.
johnme627dc8c72016-08-19 21:49:391799 DeleteToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441800 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311801
1802 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391803 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311804 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1805 // The 2nd token is gone while the 1st token still exists.
johnme627dc8c72016-08-19 21:49:391806 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
1807 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311808
jianli70715cc2015-06-23 21:46:481809 reset_last_event();
1810
jianlic02d25e2015-05-27 22:24:311811 // Delete the 1st token.
johnme627dc8c72016-08-19 21:49:391812 DeleteToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441813 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311814
1815 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391816 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311817 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1818 // Both tokens are gone now.
johnme627dc8c72016-08-19 21:49:391819 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1820 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
jianli70715cc2015-06-23 21:46:481821
1822 reset_last_event();
1823
1824 // Trying to delete the token again will get an error.
johnme627dc8c72016-08-19 21:49:391825 DeleteToken(kExtensionAppId, kSender, kScope);
jianli70715cc2015-06-23 21:46:481826 PumpLoopUntilIdle();
1827
1828 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391829 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481830 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
jianlic02d25e2015-05-27 22:24:311831}
1832
[email protected]617b22e92019-09-30 20:29:371833TEST_F(GCMClientInstanceIDTest, DISABLED_DeleteAllTokens) {
johnme627dc8c72016-08-19 21:49:391834 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311835
1836 // Get a token.
johnme627dc8c72016-08-19 21:49:391837 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1838 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441839 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311840
1841 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391842 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311843 EXPECT_EQ("token1", last_registration_id());
1844 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391845 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311846
jianli70715cc2015-06-23 21:46:481847 reset_last_event();
1848
jianlic02d25e2015-05-27 22:24:311849 // Get another token.
johnme627dc8c72016-08-19 21:49:391850 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1851 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441852 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311853
1854 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391855 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311856 EXPECT_EQ("token2", last_registration_id());
1857 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391858 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311859 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391860 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311861
jianli70715cc2015-06-23 21:46:481862 reset_last_event();
1863
jianlic02d25e2015-05-27 22:24:311864 // Delete all tokens.
johnme627dc8c72016-08-19 21:49:391865 DeleteToken(kExtensionAppId, "*", "*");
johnme0fb2fc02016-07-14 14:47:441866 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311867
1868 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391869 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311870 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1871 // All tokens are gone now.
johnme627dc8c72016-08-19 21:49:391872 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1873 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311874}
1875
jianliea8534872015-06-22 21:06:221876TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) {
johnme627dc8c72016-08-19 21:49:391877 AddInstanceID(kExtensionAppId, kInstanceID);
jianliea8534872015-06-22 21:06:221878
1879 // Delete all tokens without getting a token first.
johnme627dc8c72016-08-19 21:49:391880 DeleteToken(kExtensionAppId, "*", "*");
jianliea8534872015-06-22 21:06:221881 // No need to call CompleteDeleteToken since unregistration request should
1882 // not be triggered.
1883 PumpLoopUntilIdle();
1884
1885 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391886 EXPECT_EQ(kExtensionAppId, last_app_id());
jianliea8534872015-06-22 21:06:221887 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1888}
1889
johnme627dc8c72016-08-19 21:49:391890TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) {
1891 AddInstanceID(kExtensionAppId, kInstanceID);
1892 GetToken(kExtensionAppId, kSender, kScope);
1893 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
johnme627dc8c72016-08-19 21:49:391894
1895 std::map<std::string, std::string> expected_data;
1896
Peter Beverlooc66fc63c2017-07-07 22:58:211897 MCSMessage message(BuildDownstreamMessage(
johnme627dc8c72016-08-19 21:49:391898 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1899 std::string() /* raw_data */));
Peter Beverlooc66fc63c2017-07-07 22:58:211900 EXPECT_TRUE(message.IsValid());
1901 ReceiveMessageFromMCS(message);
johnme627dc8c72016-08-19 21:49:391902
1903 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1904 EXPECT_EQ(kExtensionAppId, last_app_id());
1905 EXPECT_EQ(expected_data.size(), last_message().data.size());
1906 EXPECT_EQ(expected_data, last_message().data);
1907 EXPECT_EQ(kSender, last_message().sender_id);
johnme627dc8c72016-08-19 21:49:391908}
1909
1910TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) {
1911 AddInstanceID(kSubtypeAppId, kInstanceID);
1912 GetToken(kSubtypeAppId, kSender, kScope);
1913 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
johnme627dc8c72016-08-19 21:49:391914
1915 std::map<std::string, std::string> expected_data;
1916
Peter Beverlooc66fc63c2017-07-07 22:58:211917 MCSMessage message(BuildDownstreamMessage(
johnme627dc8c72016-08-19 21:49:391918 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */,
1919 expected_data, std::string() /* raw_data */));
Peter Beverlooc66fc63c2017-07-07 22:58:211920 EXPECT_TRUE(message.IsValid());
1921 ReceiveMessageFromMCS(message);
johnme627dc8c72016-08-19 21:49:391922
1923 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1924 EXPECT_EQ(kSubtypeAppId, last_app_id());
1925 EXPECT_EQ(expected_data.size(), last_message().data.size());
1926 EXPECT_EQ(expected_data, last_message().data);
1927 EXPECT_EQ(kSender, last_message().sender_id);
johnme627dc8c72016-08-19 21:49:391928}
1929
1930TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithFakeSubtype) {
1931 // Victim non-extension registration.
1932 AddInstanceID(kSubtypeAppId, "iid_1");
1933 GetToken(kSubtypeAppId, kSender, kScope);
1934 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1935
1936 // Malicious extension registration.
1937 AddInstanceID(kExtensionAppId, "iid_2");
1938 GetToken(kExtensionAppId, kSender, kScope);
1939 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
1940
1941 std::map<std::string, std::string> expected_data;
1942
1943 // Message for kExtensionAppId should be delivered to the extension rather
1944 // than the victim app, despite the malicious subtype property attempting to
1945 // impersonate victim app.
1946 MCSMessage message(BuildDownstreamMessage(
1947 kSender, kExtensionAppId /* category */, kSubtypeAppId /* subtype */,
1948 expected_data, std::string() /* raw_data */));
1949 EXPECT_TRUE(message.IsValid());
1950 ReceiveMessageFromMCS(message);
1951
1952 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1953 EXPECT_EQ(kExtensionAppId, last_app_id());
1954 EXPECT_EQ(expected_data.size(), last_message().data.size());
1955 EXPECT_EQ(expected_data, last_message().data);
1956 EXPECT_EQ(kSender, last_message().sender_id);
1957}
1958
[email protected]848b1b62014-01-30 23:51:041959} // namespace gcm