blob: a2895c0e5e694d5b9da4749627a9fcd0c1283d53 [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
[email protected]fdeaf9732014-04-11 21:02:1512#include "base/command_line.h"
jianli00b4600f2015-02-10 23:32:4913#include "base/files/file_path.h"
14#include "base/files/file_util.h"
[email protected]848b1b62014-01-30 23:51:0415#include "base/files/scoped_temp_dir.h"
avi26062922015-12-26 00:14:1816#include "base/macros.h"
dchenga77e28eb2016-04-21 21:34:3717#include "base/memory/ptr_util.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1318#include "base/metrics/field_trial_param_associator.h"
19#include "base/metrics/field_trial_params.h"
[email protected]764c0442014-05-01 04:30:5520#include "base/strings/string_number_conversions.h"
Devlin Cronin69228f42018-06-01 17:25:1021#include "base/test/metrics/histogram_tester.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1322#include "base/test/scoped_feature_list.h"
jianli78b56042015-06-17 01:21:2223#include "base/test/test_mock_time_task_runner.h"
[email protected]764c0442014-05-01 04:30:5524#include "base/time/clock.h"
chirantan26436e402014-10-24 19:44:4225#include "base/timer/timer.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1326#include "components/gcm_driver/features.h"
[email protected]446f73c22014-05-14 20:47:1827#include "google_apis/gcm/base/fake_encryptor.h"
[email protected]848b1b62014-01-30 23:51:0428#include "google_apis/gcm/base/mcs_message.h"
29#include "google_apis/gcm/base/mcs_util.h"
30#include "google_apis/gcm/engine/fake_connection_factory.h"
31#include "google_apis/gcm/engine/fake_connection_handler.h"
[email protected]764c0442014-05-01 04:30:5532#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]436bcb82014-04-18 00:40:5733#include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
[email protected]848b1b62014-01-30 23:51:0434#include "google_apis/gcm/protocol/android_checkin.pb.h"
35#include "google_apis/gcm/protocol/checkin.pb.h"
36#include "google_apis/gcm/protocol/mcs.pb.h"
johnme627dc8c72016-08-19 21:49:3937#include "net/test/gtest_util.h"
38#include "net/test/scoped_disable_exit_on_dfatal.h"
[email protected]848b1b62014-01-30 23:51:0439#include "net/url_request/test_url_fetcher_factory.h"
40#include "net/url_request/url_fetcher_delegate.h"
41#include "net/url_request/url_request_test_util.h"
Maks Orlovichc70c93c2018-07-12 02:45:4442#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
43#include "services/network/test/test_url_loader_factory.h"
44#include "services/network/test/test_utils.h"
johnme627dc8c72016-08-19 21:49:3945#include "testing/gtest/include/gtest/gtest-spi.h"
[email protected]848b1b62014-01-30 23:51:0446#include "testing/gtest/include/gtest/gtest.h"
Chris Mumforda884aa1f2017-10-24 22:46:3247#include "third_party/leveldatabase/leveldb_chrome.h"
[email protected]848b1b62014-01-30 23:51:0448
49namespace gcm {
[email protected]848b1b62014-01-30 23:51:0450namespace {
51
52enum LastEvent {
53 NONE,
54 LOADING_COMPLETED,
[email protected]848b1b62014-01-30 23:51:0455 REGISTRATION_COMPLETED,
[email protected]e4007042014-02-15 20:34:2856 UNREGISTRATION_COMPLETED,
[email protected]848b1b62014-01-30 23:51:0457 MESSAGE_SEND_ERROR,
[email protected]292af2b22014-08-06 19:42:4558 MESSAGE_SEND_ACK,
[email protected]848b1b62014-01-30 23:51:0459 MESSAGE_RECEIVED,
60 MESSAGES_DELETED,
61};
62
jianlic02d25e2015-05-27 22:24:3163const char kChromeVersion[] = "45.0.0.1";
avi26062922015-12-26 00:14:1864const uint64_t kDeviceAndroidId = 54321;
65const uint64_t kDeviceSecurityToken = 12345;
66const uint64_t kDeviceAndroidId2 = 11111;
67const uint64_t kDeviceSecurityToken2 = 2222;
68const int64_t kSettingsCheckinInterval = 16 * 60 * 60;
johnme627dc8c72016-08-19 21:49:3969const char kProductCategoryForSubtypes[] = "com.chrome.macosx";
70const char kExtensionAppId[] = "abcdefghijklmnopabcdefghijklmnop";
Mugdha Lakhani2ade6a02018-01-31 14:31:1171const char kRegistrationId[] = "reg_id";
johnme627dc8c72016-08-19 21:49:3972const char kSubtypeAppId[] = "app_id";
[email protected]3a20a4d2014-03-21 22:54:2173const char kSender[] = "project_id";
74const char kSender2[] = "project_id2";
[email protected]848b1b62014-01-30 23:51:0475const char kRegistrationResponsePrefix[] = "token=";
[email protected]e4007042014-02-15 20:34:2876const char kUnregistrationResponsePrefix[] = "deleted=";
petera5aedc52015-07-22 10:47:3077const char kRawData[] = "example raw data";
[email protected]848b1b62014-01-30 23:51:0478
jianlic02d25e2015-05-27 22:24:3179const char kInstanceID[] = "iid_1";
80const char kScope[] = "GCM";
81const char kDeleteTokenResponse[] = "token=foo";
Mugdha Lakhanic3d792e2018-02-13 15:10:1382const int kTestTokenInvalidationPeriod = 5;
83const char kGroupName[] = "Enabled";
84const char kInvalidateTokenTrialName[] = "InvalidateTokenTrial";
jianlic02d25e2015-05-27 22:24:3185
Maks Orlovichc70c93c2018-07-12 02:45:4486const char kRegisterUrl[] = "https://android.clients.google.com/c2dm/register3";
87
[email protected]848b1b62014-01-30 23:51:0488// Helper for building arbitrary data messages.
89MCSMessage BuildDownstreamMessage(
90 const std::string& project_id,
johnme627dc8c72016-08-19 21:49:3991 const std::string& category,
92 const std::string& subtype,
petera5aedc52015-07-22 10:47:3093 const std::map<std::string, std::string>& data,
94 const std::string& raw_data) {
[email protected]848b1b62014-01-30 23:51:0495 mcs_proto::DataMessageStanza data_message;
[email protected]848b1b62014-01-30 23:51:0496 data_message.set_from(project_id);
johnme627dc8c72016-08-19 21:49:3997 data_message.set_category(category);
[email protected]848b1b62014-01-30 23:51:0498 for (std::map<std::string, std::string>::const_iterator iter = data.begin();
99 iter != data.end();
100 ++iter) {
101 mcs_proto::AppData* app_data = data_message.add_app_data();
102 app_data->set_key(iter->first);
103 app_data->set_value(iter->second);
104 }
johnme627dc8c72016-08-19 21:49:39105 if (!subtype.empty()) {
106 mcs_proto::AppData* app_data = data_message.add_app_data();
107 app_data->set_key("subtype");
108 app_data->set_value(subtype);
109 }
petera5aedc52015-07-22 10:47:30110 data_message.set_raw_data(raw_data);
[email protected]848b1b62014-01-30 23:51:04111 return MCSMessage(kDataMessageStanzaTag, data_message);
112}
113
fgorski58b9dfc2014-09-29 16:46:18114GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email,
115 const std::string& token) {
116 GCMClient::AccountTokenInfo account_token;
117 account_token.email = email;
118 account_token.access_token = token;
119 return account_token;
120}
121
122std::map<std::string, std::string> MakeEmailToTokenMap(
123 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
124 std::map<std::string, std::string> email_token_map;
125 for (std::vector<GCMClient::AccountTokenInfo>::const_iterator iter =
126 account_tokens.begin(); iter != account_tokens.end(); ++iter) {
127 email_token_map[iter->email] = iter->access_token;
128 }
129 return email_token_map;
130}
131
[email protected]848b1b62014-01-30 23:51:04132class FakeMCSClient : public MCSClient {
133 public:
134 FakeMCSClient(base::Clock* clock,
[email protected]2bbe0a682014-03-26 00:08:31135 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57136 GCMStore* gcm_store,
137 GCMStatsRecorder* recorder);
dcheng00ea022b2014-10-21 11:24:56138 ~FakeMCSClient() override;
avi26062922015-12-26 00:14:18139 void Login(uint64_t android_id, uint64_t security_token) override;
dcheng00ea022b2014-10-21 11:24:56140 void SendMessage(const MCSMessage& message) override;
[email protected]848b1b62014-01-30 23:51:04141
avi26062922015-12-26 00:14:18142 uint64_t last_android_id() const { return last_android_id_; }
143 uint64_t last_security_token() const { return last_security_token_; }
144 uint8_t last_message_tag() const { return last_message_tag_; }
[email protected]848b1b62014-01-30 23:51:04145 const mcs_proto::DataMessageStanza& last_data_message_stanza() const {
146 return last_data_message_stanza_;
147 }
148
149 private:
avi26062922015-12-26 00:14:18150 uint64_t last_android_id_;
151 uint64_t last_security_token_;
152 uint8_t last_message_tag_;
[email protected]848b1b62014-01-30 23:51:04153 mcs_proto::DataMessageStanza last_data_message_stanza_;
154};
155
156FakeMCSClient::FakeMCSClient(base::Clock* clock,
[email protected]2bbe0a682014-03-26 00:08:31157 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57158 GCMStore* gcm_store,
159 GCMStatsRecorder* recorder)
chirantan192a9212014-12-06 03:30:45160 : MCSClient("", clock, connection_factory, gcm_store, recorder),
[email protected]848b1b62014-01-30 23:51:04161 last_android_id_(0u),
162 last_security_token_(0u),
163 last_message_tag_(kNumProtoTypes) {
164}
165
166FakeMCSClient::~FakeMCSClient() {
167}
168
avi26062922015-12-26 00:14:18169void FakeMCSClient::Login(uint64_t android_id, uint64_t security_token) {
[email protected]848b1b62014-01-30 23:51:04170 last_android_id_ = android_id;
171 last_security_token_ = security_token;
172}
173
174void FakeMCSClient::SendMessage(const MCSMessage& message) {
175 last_message_tag_ = message.tag();
176 if (last_message_tag_ == kDataMessageStanzaTag) {
177 last_data_message_stanza_.CopyFrom(
178 reinterpret_cast<const mcs_proto::DataMessageStanza&>(
179 message.GetProtobuf()));
180 }
181}
182
[email protected]764c0442014-05-01 04:30:55183class AutoAdvancingTestClock : public base::Clock {
184 public:
185 explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta);
dcheng00ea022b2014-10-21 11:24:56186 ~AutoAdvancingTestClock() override;
[email protected]764c0442014-05-01 04:30:55187
tzikee78e4962018-04-13 12:25:46188 base::Time Now() const override;
[email protected]764c0442014-05-01 04:30:55189 void Advance(TimeDelta delta);
190 int call_count() const { return call_count_; }
191
192 private:
tzikee78e4962018-04-13 12:25:46193 mutable int call_count_;
[email protected]764c0442014-05-01 04:30:55194 base::TimeDelta auto_increment_time_delta_;
tzikee78e4962018-04-13 12:25:46195 mutable base::Time now_;
[email protected]764c0442014-05-01 04:30:55196
197 DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock);
198};
199
200AutoAdvancingTestClock::AutoAdvancingTestClock(
201 base::TimeDelta auto_increment_time_delta)
202 : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) {
203}
204
205AutoAdvancingTestClock::~AutoAdvancingTestClock() {
206}
207
tzikee78e4962018-04-13 12:25:46208base::Time AutoAdvancingTestClock::Now() const {
[email protected]764c0442014-05-01 04:30:55209 call_count_++;
210 now_ += auto_increment_time_delta_;
211 return now_;
212}
213
214void AutoAdvancingTestClock::Advance(base::TimeDelta delta) {
215 now_ += delta;
216}
217
[email protected]2bbe0a682014-03-26 00:08:31218class FakeGCMInternalsBuilder : public GCMInternalsBuilder {
219 public:
tzik48479d92018-03-20 15:20:42220 explicit FakeGCMInternalsBuilder(base::TimeDelta clock_step);
dcheng00ea022b2014-10-21 11:24:56221 ~FakeGCMInternalsBuilder() override;
[email protected]2bbe0a682014-03-26 00:08:31222
tzik48479d92018-03-20 15:20:42223 base::Clock* GetClock() override;
dchenga77e28eb2016-04-21 21:34:37224 std::unique_ptr<MCSClient> BuildMCSClient(
225 const std::string& version,
226 base::Clock* clock,
227 ConnectionFactory* connection_factory,
228 GCMStore* gcm_store,
229 GCMStatsRecorder* recorder) override;
230 std::unique_ptr<ConnectionFactory> BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31231 const std::vector<GURL>& endpoints,
232 const net::BackoffEntry::Policy& backoff_policy,
Helen Lib5b864d2018-06-15 16:10:26233 net::URLRequestContext* url_request_context,
mostynbfe59f482014-10-06 15:04:46234 GCMStatsRecorder* recorder) override;
[email protected]764c0442014-05-01 04:30:55235
236 private:
tzik48479d92018-03-20 15:20:42237 AutoAdvancingTestClock clock_;
[email protected]2bbe0a682014-03-26 00:08:31238};
239
[email protected]764c0442014-05-01 04:30:55240FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step)
tzik48479d92018-03-20 15:20:42241 : clock_(clock_step) {}
[email protected]2bbe0a682014-03-26 00:08:31242
243FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {}
244
tzik48479d92018-03-20 15:20:42245base::Clock* FakeGCMInternalsBuilder::GetClock() {
246 return &clock_;
[email protected]2bbe0a682014-03-26 00:08:31247}
248
dchenga77e28eb2016-04-21 21:34:37249std::unique_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient(
[email protected]2bbe0a682014-03-26 00:08:31250 const std::string& version,
251 base::Clock* clock,
252 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57253 GCMStore* gcm_store,
254 GCMStatsRecorder* recorder) {
dchenga77e28eb2016-04-21 21:34:37255 return base::WrapUnique<MCSClient>(
256 new FakeMCSClient(clock, connection_factory, gcm_store, recorder));
[email protected]2bbe0a682014-03-26 00:08:31257}
258
dchenga77e28eb2016-04-21 21:34:37259std::unique_ptr<ConnectionFactory>
260FakeGCMInternalsBuilder::BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31261 const std::vector<GURL>& endpoints,
262 const net::BackoffEntry::Policy& backoff_policy,
Helen Lib5b864d2018-06-15 16:10:26263 net::URLRequestContext* url_request_context,
[email protected]9df5b932014-04-30 00:39:06264 GCMStatsRecorder* recorder) {
dchenga77e28eb2016-04-21 21:34:37265 return base::WrapUnique<ConnectionFactory>(new FakeConnectionFactory());
[email protected]2bbe0a682014-03-26 00:08:31266}
267
[email protected]848b1b62014-01-30 23:51:04268} // namespace
269
270class GCMClientImplTest : public testing::Test,
271 public GCMClient::Delegate {
272 public:
273 GCMClientImplTest();
dcheng30a1b1542014-10-29 21:27:50274 ~GCMClientImplTest() override;
[email protected]848b1b62014-01-30 23:51:04275
dcheng30a1b1542014-10-29 21:27:50276 void SetUp() override;
Mugdha Lakhanic3d792e2018-02-13 15:10:13277 void TearDown() override;
278
279 void SetFeatureParams(const base::Feature& feature,
280 std::map<std::string, std::string> params);
[email protected]848b1b62014-01-30 23:51:04281
fgorski5df101702014-10-28 02:09:31282 void SetUpUrlFetcherFactory();
Mugdha Lakhanic3d792e2018-02-13 15:10:13283 void InitializeInvalidationFieldTrial();
fgorski5df101702014-10-28 02:09:31284
[email protected]764c0442014-05-01 04:30:55285 void BuildGCMClient(base::TimeDelta clock_step);
[email protected]848b1b62014-01-30 23:51:04286 void InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53287 void StartGCMClient();
jianli7a0c9b62015-05-26 23:24:47288 void Register(const std::string& app_id,
289 const std::vector<std::string>& senders);
290 void Unregister(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04291 void ReceiveMessageFromMCS(const MCSMessage& message);
[email protected]292af2b22014-08-06 19:42:45292 void ReceiveOnMessageSentToMCS(
293 const std::string& app_id,
294 const std::string& message_id,
295 const MCSClient::MessageSendStatus status);
zea76342abf2016-11-01 17:26:04296 void FailCheckin(net::HttpStatusCode response_code);
avi26062922015-12-26 00:14:18297 void CompleteCheckin(uint64_t android_id,
298 uint64_t security_token,
[email protected]764c0442014-05-01 04:30:55299 const std::string& digest,
300 const std::map<std::string, std::string>& settings);
zea76342abf2016-11-01 17:26:04301 void CompleteCheckinImpl(uint64_t android_id,
302 uint64_t security_token,
303 const std::string& digest,
304 const std::map<std::string, std::string>& settings,
305 net::HttpStatusCode response_code);
[email protected]848b1b62014-01-30 23:51:04306 void CompleteRegistration(const std::string& registration_id);
[email protected]e4007042014-02-15 20:34:28307 void CompleteUnregistration(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04308
[email protected]3a20a4d2014-03-21 22:54:21309 bool ExistsRegistration(const std::string& app_id) const;
310 void AddRegistration(const std::string& app_id,
311 const std::vector<std::string>& sender_ids,
312 const std::string& registration_id);
313
[email protected]848b1b62014-01-30 23:51:04314 // GCMClient::Delegate overrides (for verification).
jianli7a0c9b62015-05-26 23:24:47315 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info,
dcheng00ea022b2014-10-21 11:24:56316 const std::string& registration_id,
317 GCMClient::Result result) override;
jianli7a0c9b62015-05-26 23:24:47318 void OnUnregisterFinished(
319 const linked_ptr<RegistrationInfo>& registration_info,
320 GCMClient::Result result) override;
dcheng00ea022b2014-10-21 11:24:56321 void OnSendFinished(const std::string& app_id,
322 const std::string& message_id,
323 GCMClient::Result result) override {}
324 void OnMessageReceived(const std::string& registration_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06325 const IncomingMessage& message) override;
dcheng00ea022b2014-10-21 11:24:56326 void OnMessagesDeleted(const std::string& app_id) override;
327 void OnMessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12328 const std::string& app_id,
mostynbfe59f482014-10-06 15:04:46329 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
dcheng00ea022b2014-10-21 11:24:56330 void OnSendAcknowledged(const std::string& app_id,
331 const std::string& message_id) override;
fgorski5df101702014-10-28 02:09:31332 void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
333 const base::Time& last_token_fetch_time) override;
dcheng00ea022b2014-10-21 11:24:56334 void OnActivityRecorded() override {}
335 void OnConnected(const net::IPEndPoint& ip_endpoint) override {}
336 void OnDisconnected() override {}
johnme93ec7932016-11-17 14:26:58337 void OnStoreReset() override {}
[email protected]848b1b62014-01-30 23:51:04338
[email protected]848b1b62014-01-30 23:51:04339 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
jianlif3e52af42015-01-21 23:18:47340 GCMClientImpl::State gcm_client_state() const {
341 return gcm_client_->state_;
342 }
[email protected]848b1b62014-01-30 23:51:04343 FakeMCSClient* mcs_client() const {
344 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
345 }
[email protected]2bbe0a682014-03-26 00:08:31346 ConnectionFactory* connection_factory() const {
347 return gcm_client_->connection_factory_.get();
348 }
[email protected]848b1b62014-01-30 23:51:04349
[email protected]7df5ef22014-07-17 07:35:58350 const GCMClientImpl::CheckinInfo& device_checkin_info() const {
351 return gcm_client_->device_checkin_info_;
352 }
353
[email protected]3a20a4d2014-03-21 22:54:21354 void reset_last_event() {
355 last_event_ = NONE;
356 last_app_id_.clear();
357 last_registration_id_.clear();
358 last_message_id_.clear();
359 last_result_ = GCMClient::UNKNOWN_ERROR;
fgorski5df101702014-10-28 02:09:31360 last_account_mappings_.clear();
361 last_token_fetch_time_ = base::Time();
[email protected]3a20a4d2014-03-21 22:54:21362 }
363
[email protected]848b1b62014-01-30 23:51:04364 LastEvent last_event() const { return last_event_; }
365 const std::string& last_app_id() const { return last_app_id_; }
366 const std::string& last_registration_id() const {
367 return last_registration_id_;
368 }
369 const std::string& last_message_id() const { return last_message_id_; }
370 GCMClient::Result last_result() const { return last_result_; }
mvanouwerkerkf8633deb2015-07-13 11:04:06371 const IncomingMessage& last_message() const { return last_message_; }
[email protected]c6fe36b2014-03-11 10:58:12372 const GCMClient::SendErrorDetails& last_error_details() const {
373 return last_error_details_;
374 }
fgorski5df101702014-10-28 02:09:31375 const base::Time& last_token_fetch_time() const {
376 return last_token_fetch_time_;
377 }
378 const std::vector<AccountMapping>& last_account_mappings() {
379 return last_account_mappings_;
380 }
[email protected]848b1b62014-01-30 23:51:04381
[email protected]06e45272014-05-06 03:41:34382 const GServicesSettings& gservices_settings() const {
383 return gcm_client_->gservices_settings_;
[email protected]764c0442014-05-01 04:30:55384 }
385
jianli00b4600f2015-02-10 23:32:49386 const base::FilePath& temp_directory_path() const {
vabr0c237ae2016-09-14 09:24:28387 return temp_directory_.GetPath();
jianli00b4600f2015-02-10 23:32:49388 }
389
jianli9f9a73672015-06-16 19:39:34390 base::FilePath gcm_store_path() const {
391 // Pass an non-existent directory as store path to match the exact
392 // behavior in the production code. Currently GCMStoreImpl checks if
393 // the directory exist or not to determine the store existence.
vabr0c237ae2016-09-14 09:24:28394 return temp_directory_.GetPath().Append(FILE_PATH_LITERAL("GCM Store"));
jianli9f9a73672015-06-16 19:39:34395 }
396
avi26062922015-12-26 00:14:18397 int64_t CurrentTime();
[email protected]d3ba08d92014-04-04 23:03:55398
399 // Tooling.
[email protected]d3ba08d92014-04-04 23:03:55400 void PumpLoopUntilIdle();
[email protected]764c0442014-05-01 04:30:55401 bool CreateUniqueTempDir();
402 AutoAdvancingTestClock* clock() const {
tzik48479d92018-03-20 15:20:42403 return static_cast<AutoAdvancingTestClock*>(gcm_client_->clock_);
[email protected]a0144ceb2014-04-04 23:49:34404 }
jianlic02d25e2015-05-27 22:24:31405 net::TestURLFetcherFactory* url_fetcher_factory() {
406 return &url_fetcher_factory_;
407 }
Maks Orlovichc70c93c2018-07-12 02:45:44408 network::TestURLLoaderFactory* url_loader_factory() {
409 return &test_url_loader_factory_;
410 }
jianli78b56042015-06-17 01:21:22411 base::TestMockTimeTaskRunner* task_runner() {
412 return task_runner_.get();
413 }
[email protected]d3ba08d92014-04-04 23:03:55414
[email protected]764c0442014-05-01 04:30:55415 private:
wkormanc1e71b62015-11-09 22:56:04416 // Must be declared first so that it is destroyed last. Injected to
417 // GCM client.
418 base::ScopedTempDir temp_directory_;
419
[email protected]848b1b62014-01-30 23:51:04420 // Variables used for verification.
421 LastEvent last_event_;
422 std::string last_app_id_;
423 std::string last_registration_id_;
424 std::string last_message_id_;
425 GCMClient::Result last_result_;
mvanouwerkerkf8633deb2015-07-13 11:04:06426 IncomingMessage last_message_;
[email protected]c6fe36b2014-03-11 10:58:12427 GCMClient::SendErrorDetails last_error_details_;
fgorski5df101702014-10-28 02:09:31428 base::Time last_token_fetch_time_;
429 std::vector<AccountMapping> last_account_mappings_;
[email protected]848b1b62014-01-30 23:51:04430
dchenga77e28eb2016-04-21 21:34:37431 std::unique_ptr<GCMClientImpl> gcm_client_;
[email protected]848b1b62014-01-30 23:51:04432
[email protected]848b1b62014-01-30 23:51:04433 net::TestURLFetcherFactory url_fetcher_factory_;
434
jianli78b56042015-06-17 01:21:22435 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
jianli78b56042015-06-17 01:21:22436
wkormanc1e71b62015-11-09 22:56:04437 // Injected to GCM client.
[email protected]848b1b62014-01-30 23:51:04438 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
Maks Orlovichc70c93c2018-07-12 02:45:44439 network::TestURLLoaderFactory test_url_loader_factory_;
Mugdha Lakhanic3d792e2018-02-13 15:10:13440 base::test::ScopedFeatureList scoped_feature_list_;
441 base::FieldTrialList field_trial_list_;
442 std::map<std::string, base::FieldTrial*> trials_;
[email protected]848b1b62014-01-30 23:51:04443};
444
445GCMClientImplTest::GCMClientImplTest()
446 : last_event_(NONE),
447 last_result_(GCMClient::UNKNOWN_ERROR),
Maks Orlovichc70c93c2018-07-12 02:45:44448 task_runner_(new base::TestMockTimeTaskRunner(
449 base::TestMockTimeTaskRunner::Type::kBoundToThread)),
skyostilb0daa012015-06-02 19:03:48450 url_request_context_getter_(
Mugdha Lakhanic3d792e2018-02-13 15:10:13451 new net::TestURLRequestContextGetter(task_runner_)),
452 field_trial_list_(nullptr) {}
[email protected]848b1b62014-01-30 23:51:04453
454GCMClientImplTest::~GCMClientImplTest() {}
455
456void GCMClientImplTest::SetUp() {
[email protected]fdeaf9732014-04-11 21:02:15457 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55458 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:55459 BuildGCMClient(base::TimeDelta());
[email protected]848b1b62014-01-30 23:51:04460 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53461 StartGCMClient();
fgorski5df101702014-10-28 02:09:31462 SetUpUrlFetcherFactory();
Mugdha Lakhanic3d792e2018-02-13 15:10:13463 InitializeInvalidationFieldTrial();
johnme0fb2fc02016-07-14 14:47:44464 ASSERT_NO_FATAL_FAILURE(
465 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
466 std::map<std::string, std::string>()));
[email protected]848b1b62014-01-30 23:51:04467}
468
Mugdha Lakhanic3d792e2018-02-13 15:10:13469void GCMClientImplTest::TearDown() {
470 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
471}
472
fgorski5df101702014-10-28 02:09:31473void GCMClientImplTest::SetUpUrlFetcherFactory() {
474 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
475}
476
Mugdha Lakhanic3d792e2018-02-13 15:10:13477void GCMClientImplTest::SetFeatureParams(
478 const base::Feature& feature,
479 std::map<std::string, std::string> params) {
480 ASSERT_TRUE(
481 base::FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
482 trials_[feature.name]->trial_name(), kGroupName, params));
483 std::map<std::string, std::string> actual_params;
484 EXPECT_TRUE(base::GetFieldTrialParamsByFeature(
485 features::kInvalidateTokenFeature, &actual_params));
486 EXPECT_EQ(params, actual_params);
487}
488
489void GCMClientImplTest::InitializeInvalidationFieldTrial() {
490 // Set up the InvalidateToken field trial.
491 base::FieldTrial* invalidate_token_trial =
492 base::FieldTrialList::CreateFieldTrial(kInvalidateTokenTrialName,
493 kGroupName);
494 trials_[features::kInvalidateTokenFeature.name] = invalidate_token_trial;
495
496 std::unique_ptr<base::FeatureList> feature_list =
497 std::make_unique<base::FeatureList>();
498 feature_list->RegisterFieldTrialOverride(
499 features::kInvalidateTokenFeature.name,
500 base::FeatureList::OVERRIDE_ENABLE_FEATURE, invalidate_token_trial);
501 scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
502
503 std::map<std::string, std::string> params;
504 params[features::kParamNameTokenInvalidationPeriodDays] =
505 std::to_string(kTestTokenInvalidationPeriod);
506 ASSERT_NO_FATAL_FAILURE(
507 SetFeatureParams(features::kInvalidateTokenFeature, std::move(params)));
508
509 ASSERT_EQ(invalidate_token_trial, base::FeatureList::GetFieldTrial(
510 features::kInvalidateTokenFeature));
511}
512
[email protected]848b1b62014-01-30 23:51:04513void GCMClientImplTest::PumpLoopUntilIdle() {
jianli78b56042015-06-17 01:21:22514 task_runner_->RunUntilIdle();
[email protected]848b1b62014-01-30 23:51:04515}
516
[email protected]764c0442014-05-01 04:30:55517bool GCMClientImplTest::CreateUniqueTempDir() {
518 return temp_directory_.CreateUniqueTempDir();
519}
520
521void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
dchenga77e28eb2016-04-21 21:34:37522 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>(
[email protected]764c0442014-05-01 04:30:55523 new FakeGCMInternalsBuilder(clock_step))));
524}
525
zea76342abf2016-11-01 17:26:04526void GCMClientImplTest::FailCheckin(net::HttpStatusCode response_code) {
527 std::map<std::string, std::string> settings;
528 CompleteCheckinImpl(0, 0, GServicesSettings::CalculateDigest(settings),
529 settings, response_code);
530}
531
[email protected]764c0442014-05-01 04:30:55532void GCMClientImplTest::CompleteCheckin(
avi26062922015-12-26 00:14:18533 uint64_t android_id,
534 uint64_t security_token,
[email protected]764c0442014-05-01 04:30:55535 const std::string& digest,
536 const std::map<std::string, std::string>& settings) {
zea76342abf2016-11-01 17:26:04537 CompleteCheckinImpl(android_id, security_token, digest, settings,
538 net::HTTP_OK);
539}
540
541void GCMClientImplTest::CompleteCheckinImpl(
542 uint64_t android_id,
543 uint64_t security_token,
544 const std::string& digest,
545 const std::map<std::string, std::string>& settings,
546 net::HttpStatusCode response_code) {
[email protected]848b1b62014-01-30 23:51:04547 checkin_proto::AndroidCheckinResponse response;
548 response.set_stats_ok(true);
549 response.set_android_id(android_id);
550 response.set_security_token(security_token);
551
[email protected]764c0442014-05-01 04:30:55552 // For testing G-services settings.
553 if (!digest.empty()) {
554 response.set_digest(digest);
555 for (std::map<std::string, std::string>::const_iterator it =
556 settings.begin();
557 it != settings.end();
558 ++it) {
559 checkin_proto::GservicesSetting* setting = response.add_setting();
560 setting->set_name(it->first);
561 setting->set_value(it->second);
562 }
[email protected]aae544d72014-05-20 06:53:10563 response.set_settings_diff(false);
[email protected]764c0442014-05-01 04:30:55564 }
565
[email protected]848b1b62014-01-30 23:51:04566 std::string response_string;
567 response.SerializeToString(&response_string);
568
Maks Orlovich278f2572018-07-16 18:18:41569 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
570 gservices_settings().GetCheckinURL(),
571 network::URLLoaderCompletionStatus(net::OK),
572 network::CreateResourceResponseHead(response_code), response_string));
jianlid7e80f22015-06-18 22:21:31573 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
574 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04575}
576
577void GCMClientImplTest::CompleteRegistration(
578 const std::string& registration_id) {
579 std::string response(kRegistrationResponsePrefix);
580 response.append(registration_id);
Maks Orlovichc70c93c2018-07-12 02:45:44581
582 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
583 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
584 network::CreateResourceResponseHead(net::HTTP_OK), response));
585
jianlid7e80f22015-06-18 22:21:31586 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
587 PumpLoopUntilIdle();
[email protected]e4007042014-02-15 20:34:28588}
589
590void GCMClientImplTest::CompleteUnregistration(
591 const std::string& app_id) {
592 std::string response(kUnregistrationResponsePrefix);
593 response.append(app_id);
Maks Orlovich278f2572018-07-16 18:18:41594
595 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
596 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
597 network::CreateResourceResponseHead(net::HTTP_OK), response));
598
jianlid7e80f22015-06-18 22:21:31599 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
600 PumpLoopUntilIdle();
fgorskie45a34f2014-10-08 17:37:46601}
602
[email protected]3a20a4d2014-03-21 22:54:21603bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const {
jianli7a0c9b62015-05-26 23:24:47604 return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id);
[email protected]3a20a4d2014-03-21 22:54:21605}
606
607void GCMClientImplTest::AddRegistration(
608 const std::string& app_id,
609 const std::vector<std::string>& sender_ids,
610 const std::string& registration_id) {
jianli7a0c9b62015-05-26 23:24:47611 linked_ptr<GCMRegistrationInfo> registration(new GCMRegistrationInfo);
612 registration->app_id = app_id;
[email protected]3a20a4d2014-03-21 22:54:21613 registration->sender_ids = sender_ids;
jianli7a0c9b62015-05-26 23:24:47614 gcm_client_->registrations_[registration] = registration_id;
[email protected]848b1b62014-01-30 23:51:04615}
616
617void GCMClientImplTest::InitializeGCMClient() {
[email protected]848b1b62014-01-30 23:51:04618 clock()->Advance(base::TimeDelta::FromMilliseconds(1));
[email protected]2bbe0a682014-03-26 00:08:31619
[email protected]848b1b62014-01-30 23:51:04620 // Actual initialization.
[email protected]8ad80512014-05-23 09:40:47621 GCMClient::ChromeBuildInfo chrome_build_info;
jianlic02d25e2015-05-27 22:24:31622 chrome_build_info.version = kChromeVersion;
johnme627dc8c72016-08-19 21:49:39623 chrome_build_info.product_category_for_subtypes = kProductCategoryForSubtypes;
Maks Orlovichc70c93c2018-07-12 02:45:44624 gcm_client_->Initialize(
625 chrome_build_info, gcm_store_path(), task_runner_,
626 url_request_context_getter_,
627 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
628 &test_url_loader_factory_),
629 base::WrapUnique<Encryptor>(new FakeEncryptor), this);
[email protected]1abdf202014-06-13 19:38:53630}
[email protected]d3a4b2e2014-02-27 13:46:54631
[email protected]1abdf202014-06-13 19:38:53632void GCMClientImplTest::StartGCMClient() {
[email protected]2bbe0a682014-03-26 00:08:31633 // Start loading and check-in.
jianlif3e52af42015-01-21 23:18:47634 gcm_client_->Start(GCMClient::IMMEDIATE_START);
[email protected]d3a4b2e2014-02-27 13:46:54635
[email protected]848b1b62014-01-30 23:51:04636 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04637}
638
jianli7a0c9b62015-05-26 23:24:47639void GCMClientImplTest::Register(const std::string& app_id,
640 const std::vector<std::string>& senders) {
dchenga77e28eb2016-04-21 21:34:37641 std::unique_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
jianli7a0c9b62015-05-26 23:24:47642 gcm_info->app_id = app_id;
643 gcm_info->sender_ids = senders;
644 gcm_client()->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release()));
645}
646
647void GCMClientImplTest::Unregister(const std::string& app_id) {
dchenga77e28eb2016-04-21 21:34:37648 std::unique_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
jianli7a0c9b62015-05-26 23:24:47649 gcm_info->app_id = app_id;
650 gcm_client()->Unregister(
651 make_linked_ptr<RegistrationInfo>(gcm_info.release()));
652}
653
[email protected]848b1b62014-01-30 23:51:04654void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
[email protected]dd47c4ce2014-08-05 23:38:50655 gcm_client_->recorder_.RecordConnectionInitiated(std::string());
656 gcm_client_->recorder_.RecordConnectionSuccess();
[email protected]848b1b62014-01-30 23:51:04657 gcm_client_->OnMessageReceivedFromMCS(message);
658}
659
[email protected]292af2b22014-08-06 19:42:45660void GCMClientImplTest::ReceiveOnMessageSentToMCS(
661 const std::string& app_id,
662 const std::string& message_id,
663 const MCSClient::MessageSendStatus status) {
664 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status);
665}
666
fgorskid578c18b2014-09-24 23:40:17667void GCMClientImplTest::OnGCMReady(
fgorski5df101702014-10-28 02:09:31668 const std::vector<AccountMapping>& account_mappings,
669 const base::Time& last_token_fetch_time) {
[email protected]848b1b62014-01-30 23:51:04670 last_event_ = LOADING_COMPLETED;
fgorski5df101702014-10-28 02:09:31671 last_account_mappings_ = account_mappings;
672 last_token_fetch_time_ = last_token_fetch_time;
[email protected]848b1b62014-01-30 23:51:04673}
674
mvanouwerkerkf8633deb2015-07-13 11:04:06675void GCMClientImplTest::OnMessageReceived(const std::string& registration_id,
676 const IncomingMessage& message) {
[email protected]848b1b62014-01-30 23:51:04677 last_event_ = MESSAGE_RECEIVED;
678 last_app_id_ = registration_id;
679 last_message_ = message;
[email protected]848b1b62014-01-30 23:51:04680}
681
jianli7a0c9b62015-05-26 23:24:47682void GCMClientImplTest::OnRegisterFinished(
683 const linked_ptr<RegistrationInfo>& registration_info,
684 const std::string& registration_id,
685 GCMClient::Result result) {
[email protected]848b1b62014-01-30 23:51:04686 last_event_ = REGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47687 last_app_id_ = registration_info->app_id;
[email protected]848b1b62014-01-30 23:51:04688 last_registration_id_ = registration_id;
689 last_result_ = result;
690}
691
jianli7a0c9b62015-05-26 23:24:47692void GCMClientImplTest::OnUnregisterFinished(
693 const linked_ptr<RegistrationInfo>& registration_info,
694 GCMClient::Result result) {
[email protected]e4007042014-02-15 20:34:28695 last_event_ = UNREGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47696 last_app_id_ = registration_info->app_id;
[email protected]0e88e1d12014-03-19 06:53:08697 last_result_ = result;
[email protected]e4007042014-02-15 20:34:28698}
699
[email protected]848b1b62014-01-30 23:51:04700void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
701 last_event_ = MESSAGES_DELETED;
702 last_app_id_ = app_id;
703}
704
[email protected]c6fe36b2014-03-11 10:58:12705void GCMClientImplTest::OnMessageSendError(
706 const std::string& app_id,
707 const gcm::GCMClient::SendErrorDetails& send_error_details) {
[email protected]848b1b62014-01-30 23:51:04708 last_event_ = MESSAGE_SEND_ERROR;
709 last_app_id_ = app_id;
[email protected]c6fe36b2014-03-11 10:58:12710 last_error_details_ = send_error_details;
[email protected]848b1b62014-01-30 23:51:04711}
712
[email protected]292af2b22014-08-06 19:42:45713void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id,
714 const std::string& message_id) {
715 last_event_ = MESSAGE_SEND_ACK;
716 last_app_id_ = app_id;
717 last_message_id_ = message_id;
718}
719
avi26062922015-12-26 00:14:18720int64_t GCMClientImplTest::CurrentTime() {
[email protected]848b1b62014-01-30 23:51:04721 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
722}
723
[email protected]848b1b62014-01-30 23:51:04724TEST_F(GCMClientImplTest, LoadingCompleted) {
725 EXPECT_EQ(LOADING_COMPLETED, last_event());
726 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
727 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
[email protected]7df5ef22014-07-17 07:35:58728
729 // Checking freshly loaded CheckinInfo.
730 EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id);
731 EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret);
732 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
733 EXPECT_TRUE(device_checkin_info().accounts_set);
734 EXPECT_TRUE(device_checkin_info().account_tokens.empty());
[email protected]848b1b62014-01-30 23:51:04735}
736
jianli00b4600f2015-02-10 23:32:49737TEST_F(GCMClientImplTest, LoadingBusted) {
738 // Close the GCM store.
739 gcm_client()->Stop();
740 PumpLoopUntilIdle();
741
742 // Mess up the store.
Chris Mumforda884aa1f2017-10-24 22:46:32743 EXPECT_TRUE(leveldb_chrome::CorruptClosedDBForTesting(gcm_store_path()));
jianli00b4600f2015-02-10 23:32:49744
745 // Restart GCM client. The store should be reset and the loading should
746 // complete successfully.
747 reset_last_event();
748 BuildGCMClient(base::TimeDelta());
749 InitializeGCMClient();
750 StartGCMClient();
johnme0fb2fc02016-07-14 14:47:44751 ASSERT_NO_FATAL_FAILURE(
752 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(),
753 std::map<std::string, std::string>()));
jianli00b4600f2015-02-10 23:32:49754
755 EXPECT_EQ(LOADING_COMPLETED, last_event());
756 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
757 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
758}
759
johnme6d6c7802016-09-29 22:27:16760TEST_F(GCMClientImplTest, LoadingWithEmptyDirectory) {
761 // Close the GCM store.
762 gcm_client()->Stop();
763 PumpLoopUntilIdle();
764
765 // Make the store directory empty, to simulate a previous destroy store
766 // operation failing to delete the store directory.
767 ASSERT_TRUE(base::DeleteFile(gcm_store_path(), true /* recursive */));
768 ASSERT_TRUE(base::CreateDirectory(gcm_store_path()));
769
770 base::HistogramTester histogram_tester;
771
772 // Restart GCM client. The store should be considered to not exist.
773 BuildGCMClient(base::TimeDelta());
774 InitializeGCMClient();
775 gcm_client()->Start(GCMClient::DELAYED_START);
776 PumpLoopUntilIdle();
777 histogram_tester.ExpectUniqueSample("GCM.LoadStatus",
778 13 /* STORE_DOES_NOT_EXIST */, 1);
779 // Since the store does not exist, the database should not have been opened.
780 histogram_tester.ExpectTotalCount("GCM.Database.Open", 0);
781 // Without a store, DELAYED_START loading should only reach INITIALIZED state.
782 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
783
784 // The store directory should still exist (and be empty). If not, then the
785 // DELAYED_START load has probably reset the store, rather than leaving that
786 // to the next IMMEDIATE_START load as expected.
787 ASSERT_TRUE(base::DirectoryExists(gcm_store_path()));
788 ASSERT_FALSE(
789 base::PathExists(gcm_store_path().Append(FILE_PATH_LITERAL("CURRENT"))));
790
791 // IMMEDIATE_START loading should successfully create a new store despite the
792 // empty directory.
793 reset_last_event();
794 StartGCMClient();
795 ASSERT_NO_FATAL_FAILURE(
796 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(),
797 std::map<std::string, std::string>()));
798 EXPECT_EQ(LOADING_COMPLETED, last_event());
799 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
800 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
801 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
802}
803
jianli78b56042015-06-17 01:21:22804TEST_F(GCMClientImplTest, DestroyStoreWhenNotNeeded) {
805 // Close the GCM store.
806 gcm_client()->Stop();
807 PumpLoopUntilIdle();
808
809 // Restart GCM client. The store is loaded successfully.
810 reset_last_event();
811 BuildGCMClient(base::TimeDelta());
812 InitializeGCMClient();
813 gcm_client()->Start(GCMClient::DELAYED_START);
814 PumpLoopUntilIdle();
815
816 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
817 EXPECT_TRUE(device_checkin_info().android_id);
818 EXPECT_TRUE(device_checkin_info().secret);
819
820 // Fast forward the clock to trigger the store destroying logic.
821 task_runner()->FastForwardBy(base::TimeDelta::FromMilliseconds(300000));
822 PumpLoopUntilIdle();
823
824 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
825 EXPECT_FALSE(device_checkin_info().android_id);
826 EXPECT_FALSE(device_checkin_info().secret);
827}
828
Mugdha Lakhani2ade6a02018-01-31 14:31:11829TEST_F(GCMClientImplTest, SerializeAndDeserialize) {
830 std::vector<std::string> senders{"sender"};
831 auto gcm_info = std::make_unique<GCMRegistrationInfo>();
832 gcm_info->app_id = kExtensionAppId;
833 gcm_info->sender_ids = senders;
834 gcm_info->last_validated = clock()->Now();
835
Mugdha Lakhani2ade6a02018-01-31 14:31:11836 auto gcm_info_deserialized = std::make_unique<GCMRegistrationInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02837 std::string gcm_registration_id_deserialized;
838 {
839 std::string serialized_key = gcm_info->GetSerializedKey();
840 std::string serialized_value =
841 gcm_info->GetSerializedValue(kRegistrationId);
842
843 ASSERT_TRUE(gcm_info_deserialized->Deserialize(
844 serialized_key, serialized_value, &gcm_registration_id_deserialized));
845 }
Mugdha Lakhani2ade6a02018-01-31 14:31:11846
847 EXPECT_EQ(gcm_info->app_id, gcm_info_deserialized->app_id);
848 EXPECT_EQ(gcm_info->sender_ids, gcm_info_deserialized->sender_ids);
849 EXPECT_EQ(gcm_info->last_validated, gcm_info_deserialized->last_validated);
Peter Beverloo1b76cbea2018-02-23 16:23:02850 EXPECT_EQ(kRegistrationId, gcm_registration_id_deserialized);
851
852 auto instance_id_info = std::make_unique<InstanceIDTokenInfo>();
853 instance_id_info->app_id = kExtensionAppId;
854 instance_id_info->last_validated = clock()->Now();
855 instance_id_info->authorized_entity = "different_sender";
856 instance_id_info->scope = "scope";
857
858 auto instance_id_info_deserialized = std::make_unique<InstanceIDTokenInfo>();
859 std::string instance_id_registration_id_deserialized;
860 {
861 std::string serialized_key = instance_id_info->GetSerializedKey();
862 std::string serialized_value =
863 instance_id_info->GetSerializedValue(kRegistrationId);
864
865 ASSERT_TRUE(instance_id_info_deserialized->Deserialize(
866 serialized_key, serialized_value,
867 &instance_id_registration_id_deserialized));
868 }
869
870 EXPECT_EQ(instance_id_info->app_id, instance_id_info_deserialized->app_id);
871 EXPECT_EQ(instance_id_info->last_validated,
872 instance_id_info_deserialized->last_validated);
873 EXPECT_EQ(instance_id_info->authorized_entity,
874 instance_id_info_deserialized->authorized_entity);
875 EXPECT_EQ(instance_id_info->scope, instance_id_info_deserialized->scope);
876 EXPECT_EQ(kRegistrationId, instance_id_registration_id_deserialized);
Mugdha Lakhani2ade6a02018-01-31 14:31:11877}
878
[email protected]848b1b62014-01-30 23:51:04879TEST_F(GCMClientImplTest, RegisterApp) {
johnme627dc8c72016-08-19 21:49:39880 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21881
[email protected]848b1b62014-01-30 23:51:04882 std::vector<std::string> senders;
883 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39884 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44885 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
[email protected]848b1b62014-01-30 23:51:04886
887 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39888 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04889 EXPECT_EQ("reg_id", last_registration_id());
890 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39891 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21892}
893
Peter Beverloo71568a442018-03-01 16:07:41894TEST_F(GCMClientImplTest, RegisterAppFromCache) {
johnme627dc8c72016-08-19 21:49:39895 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21896
897 std::vector<std::string> senders;
898 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39899 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44900 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
johnme627dc8c72016-08-19 21:49:39901 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21902
johnme627dc8c72016-08-19 21:49:39903 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]3a20a4d2014-03-21 22:54:21904 EXPECT_EQ("reg_id", last_registration_id());
905 EXPECT_EQ(GCMClient::SUCCESS, last_result());
906 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
907
908 // Recreate GCMClient in order to load from the persistent store.
[email protected]764c0442014-05-01 04:30:55909 BuildGCMClient(base::TimeDelta());
[email protected]3a20a4d2014-03-21 22:54:21910 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53911 StartGCMClient();
[email protected]3a20a4d2014-03-21 22:54:21912
johnme627dc8c72016-08-19 21:49:39913 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]848b1b62014-01-30 23:51:04914}
915
jianli055634b2015-06-29 21:32:12916TEST_F(GCMClientImplTest, RegisterPreviousSenderAgain) {
johnme627dc8c72016-08-19 21:49:39917 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12918
919 // Register a sender.
920 std::vector<std::string> senders;
921 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39922 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44923 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianli055634b2015-06-29 21:32:12924
925 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39926 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12927 EXPECT_EQ("reg_id", last_registration_id());
928 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39929 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12930
931 reset_last_event();
932
933 // Register a different sender. Different registration ID from previous one
934 // should be returned.
935 std::vector<std::string> senders2;
936 senders2.push_back("sender2");
johnme627dc8c72016-08-19 21:49:39937 Register(kExtensionAppId, senders2);
johnme0fb2fc02016-07-14 14:47:44938 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2"));
jianli055634b2015-06-29 21:32:12939
940 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39941 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12942 EXPECT_EQ("reg_id2", last_registration_id());
943 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39944 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12945
946 reset_last_event();
947
948 // Register the 1st sender again. Different registration ID from previous one
949 // should be returned.
950 std::vector<std::string> senders3;
951 senders3.push_back("sender");
johnme627dc8c72016-08-19 21:49:39952 Register(kExtensionAppId, senders3);
johnme0fb2fc02016-07-14 14:47:44953 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianli055634b2015-06-29 21:32:12954
955 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39956 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12957 EXPECT_EQ("reg_id", last_registration_id());
958 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39959 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12960}
961
Mugdha Lakhanic3d792e2018-02-13 15:10:13962TEST_F(GCMClientImplTest, RegisterAgainWhenTokenIsFresh) {
963 // Register a sender.
964 std::vector<std::string> senders;
965 senders.push_back("sender");
966 Register(kExtensionAppId, senders);
967 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
968 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
969 EXPECT_EQ(kExtensionAppId, last_app_id());
970 EXPECT_EQ("reg_id", last_registration_id());
971 EXPECT_EQ(GCMClient::SUCCESS, last_result());
972 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
973
974 reset_last_event();
975
976 // Advance time by (kTestTokenInvalidationPeriod)/2
977 clock()->Advance(base::TimeDelta::FromDays(kTestTokenInvalidationPeriod / 2));
978
979 // Register the same sender again. The same registration ID as the
980 // previous one should be returned, and we should *not* send a
981 // registration request to the GCM server.
982 Register(kExtensionAppId, senders);
983 PumpLoopUntilIdle();
984
985 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
986 EXPECT_EQ(kExtensionAppId, last_app_id());
987 EXPECT_EQ("reg_id", last_registration_id());
988 EXPECT_EQ(GCMClient::SUCCESS, last_result());
989 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
990}
991
992TEST_F(GCMClientImplTest, RegisterAgainWhenTokenIsStale) {
993 // Register a sender.
994 std::vector<std::string> senders;
995 senders.push_back("sender");
996 Register(kExtensionAppId, senders);
997 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
998
999 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1000 EXPECT_EQ(kExtensionAppId, last_app_id());
1001 EXPECT_EQ("reg_id", last_registration_id());
1002 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1003 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
1004
1005 reset_last_event();
1006
1007 // Advance time by kTestTokenInvalidationPeriod
1008 clock()->Advance(base::TimeDelta::FromDays(kTestTokenInvalidationPeriod));
1009
1010 // Register the same sender again. Different registration ID from the
1011 // previous one should be returned.
1012 Register(kExtensionAppId, senders);
1013 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2"));
1014
1015 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1016 EXPECT_EQ(kExtensionAppId, last_app_id());
1017 EXPECT_EQ("reg_id2", last_registration_id());
1018 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1019 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
1020}
1021
[email protected]e4007042014-02-15 20:34:281022TEST_F(GCMClientImplTest, UnregisterApp) {
johnme627dc8c72016-08-19 21:49:391023 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:211024
1025 std::vector<std::string> senders;
1026 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391027 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:441028 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
johnme627dc8c72016-08-19 21:49:391029 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:211030
johnme627dc8c72016-08-19 21:49:391031 Unregister(kExtensionAppId);
1032 ASSERT_NO_FATAL_FAILURE(CompleteUnregistration(kExtensionAppId));
[email protected]e4007042014-02-15 20:34:281033
1034 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391035 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]e4007042014-02-15 20:34:281036 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391037 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]e4007042014-02-15 20:34:281038}
1039
fgorskie45a34f2014-10-08 17:37:461040// Tests that stopping the GCMClient also deletes pending registration requests.
1041// This is tested by checking that url fetcher contained in the request was
1042// deleted.
1043TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) {
1044 std::vector<std::string> senders;
1045 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391046 Register(kExtensionAppId, senders);
fgorskie45a34f2014-10-08 17:37:461047
1048 gcm_client()->Stop();
jianlid7e80f22015-06-18 22:21:311049 PumpLoopUntilIdle();
Maks Orlovichc70c93c2018-07-12 02:45:441050 EXPECT_EQ(0, url_loader_factory()->NumPending());
fgorskie45a34f2014-10-08 17:37:461051}
1052
[email protected]848b1b62014-01-30 23:51:041053TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
[email protected]3a20a4d2014-03-21 22:54:211054 // Register to receive messages from kSender and kSender2 only.
1055 std::vector<std::string> senders;
1056 senders.push_back(kSender);
1057 senders.push_back(kSender2);
johnme627dc8c72016-08-19 21:49:391058 AddRegistration(kExtensionAppId, senders, "reg_id");
[email protected]3a20a4d2014-03-21 22:54:211059
[email protected]848b1b62014-01-30 23:51:041060 std::map<std::string, std::string> expected_data;
1061 expected_data["message_type"] = "gcm";
1062 expected_data["key"] = "value";
1063 expected_data["key2"] = "value2";
[email protected]3a20a4d2014-03-21 22:54:211064
1065 // Message for kSender will be received.
johnme627dc8c72016-08-19 21:49:391066 MCSMessage message(BuildDownstreamMessage(
1067 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1068 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041069 EXPECT_TRUE(message.IsValid());
1070 ReceiveMessageFromMCS(message);
1071
1072 expected_data.erase(expected_data.find("message_type"));
1073 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391074 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:041075 EXPECT_EQ(expected_data.size(), last_message().data.size());
1076 EXPECT_EQ(expected_data, last_message().data);
[email protected]3a20a4d2014-03-21 22:54:211077 EXPECT_EQ(kSender, last_message().sender_id);
1078
1079 reset_last_event();
1080
1081 // Message for kSender2 will be received.
johnme627dc8c72016-08-19 21:49:391082 MCSMessage message2(BuildDownstreamMessage(
1083 kSender2, kExtensionAppId, std::string() /* subtype */, expected_data,
1084 std::string() /* raw_data */));
[email protected]3a20a4d2014-03-21 22:54:211085 EXPECT_TRUE(message2.IsValid());
1086 ReceiveMessageFromMCS(message2);
1087
1088 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391089 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]3a20a4d2014-03-21 22:54:211090 EXPECT_EQ(expected_data.size(), last_message().data.size());
1091 EXPECT_EQ(expected_data, last_message().data);
1092 EXPECT_EQ(kSender2, last_message().sender_id);
[email protected]848b1b62014-01-30 23:51:041093}
1094
petera5aedc52015-07-22 10:47:301095TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) {
1096 std::vector<std::string> senders(1, kSender);
johnme627dc8c72016-08-19 21:49:391097 AddRegistration(kExtensionAppId, senders, "reg_id");
petera5aedc52015-07-22 10:47:301098
1099 std::map<std::string, std::string> expected_data;
1100
johnme627dc8c72016-08-19 21:49:391101 MCSMessage message(BuildDownstreamMessage(kSender, kExtensionAppId,
1102 std::string() /* subtype */,
1103 expected_data, kRawData));
petera5aedc52015-07-22 10:47:301104 EXPECT_TRUE(message.IsValid());
1105 ReceiveMessageFromMCS(message);
1106
1107 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391108 EXPECT_EQ(kExtensionAppId, last_app_id());
petera5aedc52015-07-22 10:47:301109 EXPECT_EQ(expected_data.size(), last_message().data.size());
1110 EXPECT_EQ(kSender, last_message().sender_id);
1111 EXPECT_EQ(kRawData, last_message().raw_data);
1112}
1113
[email protected]848b1b62014-01-30 23:51:041114TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) {
1115 std::map<std::string, std::string> expected_data;
1116 expected_data["message_type"] = "send_error";
1117 expected_data["google.message_id"] = "007";
[email protected]c6fe36b2014-03-11 10:58:121118 expected_data["error_details"] = "some details";
johnme627dc8c72016-08-19 21:49:391119 MCSMessage message(BuildDownstreamMessage(
1120 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1121 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041122 EXPECT_TRUE(message.IsValid());
1123 ReceiveMessageFromMCS(message);
1124
1125 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
johnme627dc8c72016-08-19 21:49:391126 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]c6fe36b2014-03-11 10:58:121127 EXPECT_EQ("007", last_error_details().message_id);
1128 EXPECT_EQ(1UL, last_error_details().additional_data.size());
mvanouwerkerkf8633deb2015-07-13 11:04:061129 MessageData::const_iterator iter =
[email protected]c6fe36b2014-03-11 10:58:121130 last_error_details().additional_data.find("error_details");
1131 EXPECT_TRUE(iter != last_error_details().additional_data.end());
1132 EXPECT_EQ("some details", iter->second);
[email protected]848b1b62014-01-30 23:51:041133}
1134
1135TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
1136 std::map<std::string, std::string> expected_data;
1137 expected_data["message_type"] = "deleted_messages";
johnme627dc8c72016-08-19 21:49:391138 MCSMessage message(BuildDownstreamMessage(
1139 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1140 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041141 EXPECT_TRUE(message.IsValid());
1142 ReceiveMessageFromMCS(message);
1143
1144 EXPECT_EQ(MESSAGES_DELETED, last_event());
johnme627dc8c72016-08-19 21:49:391145 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:041146}
1147
1148TEST_F(GCMClientImplTest, SendMessage) {
mvanouwerkerkf8633deb2015-07-13 11:04:061149 OutgoingMessage message;
[email protected]848b1b62014-01-30 23:51:041150 message.id = "007";
1151 message.time_to_live = 500;
1152 message.data["key"] = "value";
johnme627dc8c72016-08-19 21:49:391153 gcm_client()->Send(kExtensionAppId, kSender, message);
[email protected]848b1b62014-01-30 23:51:041154
1155 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
johnme627dc8c72016-08-19 21:49:391156 EXPECT_EQ(kExtensionAppId,
1157 mcs_client()->last_data_message_stanza().category());
[email protected]3a20a4d2014-03-21 22:54:211158 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:041159 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
1160 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
1161 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
[email protected]848b1b62014-01-30 23:51:041162 EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from());
[email protected]3a20a4d2014-03-21 22:54:211163 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:041164 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
1165 EXPECT_EQ("value",
1166 mcs_client()->last_data_message_stanza().app_data(0).value());
1167}
1168
[email protected]292af2b22014-08-06 19:42:451169TEST_F(GCMClientImplTest, SendMessageAcknowledged) {
johnme627dc8c72016-08-19 21:49:391170 ReceiveOnMessageSentToMCS(kExtensionAppId, "007", MCSClient::SENT);
[email protected]292af2b22014-08-06 19:42:451171 EXPECT_EQ(MESSAGE_SEND_ACK, last_event());
johnme627dc8c72016-08-19 21:49:391172 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]292af2b22014-08-06 19:42:451173 EXPECT_EQ("007", last_message_id());
1174}
1175
[email protected]764c0442014-05-01 04:30:551176class GCMClientImplCheckinTest : public GCMClientImplTest {
1177 public:
1178 GCMClientImplCheckinTest();
dcheng30a1b1542014-10-29 21:27:501179 ~GCMClientImplCheckinTest() override;
[email protected]764c0442014-05-01 04:30:551180
dcheng30a1b1542014-10-29 21:27:501181 void SetUp() override;
[email protected]764c0442014-05-01 04:30:551182};
1183
1184GCMClientImplCheckinTest::GCMClientImplCheckinTest() {
1185}
1186
1187GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {
1188}
1189
1190void GCMClientImplCheckinTest::SetUp() {
1191 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:551192 // Creating unique temp directory that will be used by GCMStore shared between
1193 // GCM Client and G-services settings.
1194 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:551195 // Time will be advancing one hour every time it is checked.
1196 BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval));
1197 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:531198 StartGCMClient();
[email protected]764c0442014-05-01 04:30:551199}
1200
1201TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
1202 std::map<std::string, std::string> settings;
1203 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
1204 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131205 settings["gcm_hostname"] = "alternative.gcm.host";
1206 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:551207 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441208 ASSERT_NO_FATAL_FAILURE(
1209 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1210 GServicesSettings::CalculateDigest(settings), settings));
[email protected]f09354512014-05-02 00:51:131211 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:101212 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:341213 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:101214 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:341215 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:101216 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:131217 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:101218 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:131219 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:101220 gservices_settings().GetMCSFallbackEndpoint());
[email protected]764c0442014-05-01 04:30:551221}
1222
1223// This test only checks that periodic checkin happens.
1224TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
1225 std::map<std::string, std::string> settings;
riceaf44cda22015-09-24 14:08:511226 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
[email protected]764c0442014-05-01 04:30:551227 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131228 settings["gcm_hostname"] = "alternative.gcm.host";
1229 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:551230 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441231 ASSERT_NO_FATAL_FAILURE(
1232 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1233 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581234
[email protected]764c0442014-05-01 04:30:551235 EXPECT_EQ(2, clock()->call_count());
1236
1237 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441238 ASSERT_NO_FATAL_FAILURE(
1239 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1240 GServicesSettings::CalculateDigest(settings), settings));
[email protected]764c0442014-05-01 04:30:551241}
1242
[email protected]06e45272014-05-06 03:41:341243TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) {
1244 std::map<std::string, std::string> settings;
riceaf44cda22015-09-24 14:08:511245 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
[email protected]06e45272014-05-06 03:41:341246 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131247 settings["gcm_hostname"] = "alternative.gcm.host";
1248 settings["gcm_secure_port"] = "7777";
[email protected]06e45272014-05-06 03:41:341249 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441250 ASSERT_NO_FATAL_FAILURE(
1251 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1252 GServicesSettings::CalculateDigest(settings), settings));
[email protected]06e45272014-05-06 03:41:341253
1254 BuildGCMClient(base::TimeDelta());
1255 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:531256 StartGCMClient();
[email protected]06e45272014-05-06 03:41:341257
1258 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:101259 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:341260 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:101261 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:341262 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:101263 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:131264 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:101265 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:131266 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:101267 gservices_settings().GetMCSFallbackEndpoint());
[email protected]06e45272014-05-06 03:41:341268}
1269
[email protected]7df5ef22014-07-17 07:35:581270// This test only checks that periodic checkin happens.
1271TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) {
1272 std::map<std::string, std::string> settings;
riceaf44cda22015-09-24 14:08:511273 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581274 settings["checkin_url"] = "http://alternative.url/checkin";
1275 settings["gcm_hostname"] = "alternative.gcm.host";
1276 settings["gcm_secure_port"] = "7777";
1277 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441278 ASSERT_NO_FATAL_FAILURE(
1279 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1280 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581281
fgorski58b9dfc2014-09-29 16:46:181282 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1283 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1284 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1285 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581286
1287 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
1288 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181289 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1290 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581291
1292 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441293 ASSERT_NO_FATAL_FAILURE(
1294 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1295 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581296
1297 std::set<std::string> accounts;
1298 accounts.insert("[email protected]");
1299 accounts.insert("[email protected]");
1300 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1301 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181302 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1303 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581304}
1305
1306// This test only checks that periodic checkin happens.
1307TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) {
1308 std::map<std::string, std::string> settings;
riceaf44cda22015-09-24 14:08:511309 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581310 settings["checkin_url"] = "http://alternative.url/checkin";
1311 settings["gcm_hostname"] = "alternative.gcm.host";
1312 settings["gcm_secure_port"] = "7777";
1313 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441314 ASSERT_NO_FATAL_FAILURE(
1315 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1316 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581317
fgorski58b9dfc2014-09-29 16:46:181318 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1319 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1320 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1321 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581322 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441323 ASSERT_NO_FATAL_FAILURE(
1324 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1325 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581326
1327 EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size());
1328 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181329 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1330 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581331
fgorski58b9dfc2014-09-29 16:46:181332 account_tokens.erase(account_tokens.begin() + 1);
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 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181344 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1345 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581346}
1347
1348// This test only checks that periodic checkin happens.
1349TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) {
1350 std::map<std::string, std::string> settings;
riceaf44cda22015-09-24 14:08:511351 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581352 settings["checkin_url"] = "http://alternative.url/checkin";
1353 settings["gcm_hostname"] = "alternative.gcm.host";
1354 settings["gcm_secure_port"] = "7777";
1355 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441356 ASSERT_NO_FATAL_FAILURE(
1357 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1358 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581359
fgorski58b9dfc2014-09-29 16:46:181360 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1361 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1362 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581363
1364 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441365 ASSERT_NO_FATAL_FAILURE(
1366 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1367 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581368
1369 std::set<std::string> accounts;
1370 accounts.insert("[email protected]");
1371 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1372
1373 // This should trigger another checkin, because the list of accounts is
1374 // different.
fgorski58b9dfc2014-09-29 16:46:181375 account_tokens.clear();
1376 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1377 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581378
1379 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441380 ASSERT_NO_FATAL_FAILURE(
1381 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1382 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581383
1384 accounts.clear();
1385 accounts.insert("[email protected]");
1386 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1387 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181388 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1389 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581390}
1391
zea76342abf2016-11-01 17:26:041392TEST_F(GCMClientImplCheckinTest, ResetStoreWhenCheckinRejected) {
1393 base::HistogramTester histogram_tester;
1394 std::map<std::string, std::string> settings;
1395 ASSERT_NO_FATAL_FAILURE(FailCheckin(net::HTTP_UNAUTHORIZED));
1396 PumpLoopUntilIdle();
1397
1398 // Store should have been destroyed. Restart client and verify the initial
1399 // checkin response is persisted.
1400 BuildGCMClient(base::TimeDelta());
1401 InitializeGCMClient();
1402 StartGCMClient();
1403 ASSERT_NO_FATAL_FAILURE(
1404 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2,
1405 GServicesSettings::CalculateDigest(settings), settings));
1406
1407 EXPECT_EQ(LOADING_COMPLETED, last_event());
1408 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
1409 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
1410}
1411
[email protected]1abdf202014-06-13 19:38:531412class GCMClientImplStartAndStopTest : public GCMClientImplTest {
jianli78b56042015-06-17 01:21:221413 public:
[email protected]1abdf202014-06-13 19:38:531414 GCMClientImplStartAndStopTest();
dcheng30a1b1542014-10-29 21:27:501415 ~GCMClientImplStartAndStopTest() override;
[email protected]1abdf202014-06-13 19:38:531416
dcheng30a1b1542014-10-29 21:27:501417 void SetUp() override;
fgorski5df101702014-10-28 02:09:311418
1419 void DefaultCompleteCheckin();
[email protected]1abdf202014-06-13 19:38:531420};
1421
1422GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() {
1423}
1424
1425GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() {
1426}
1427
1428void GCMClientImplStartAndStopTest::SetUp() {
1429 testing::Test::SetUp();
1430 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]1abdf202014-06-13 19:38:531431 BuildGCMClient(base::TimeDelta());
1432 InitializeGCMClient();
1433}
1434
fgorski5df101702014-10-28 02:09:311435void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() {
1436 SetUpUrlFetcherFactory();
johnme0fb2fc02016-07-14 14:47:441437 ASSERT_NO_FATAL_FAILURE(
1438 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
1439 std::map<std::string, std::string>()));
fgorski5df101702014-10-28 02:09:311440 PumpLoopUntilIdle();
1441}
1442
[email protected]1abdf202014-06-13 19:38:531443TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471444 // GCMClientImpl should be in INITIALIZED state at first.
1445 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1446
1447 // Delay start the GCM.
1448 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531449 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341450 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531451
1452 // Stop the GCM.
1453 gcm_client()->Stop();
1454 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471455 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531456
jianlif3e52af42015-01-21 23:18:471457 // Restart the GCM without delay.
1458 gcm_client()->Start(GCMClient::IMMEDIATE_START);
[email protected]1abdf202014-06-13 19:38:531459 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471460 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531461}
1462
jianli9f9a73672015-06-16 19:39:341463TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) {
jianlif3e52af42015-01-21 23:18:471464 // GCMClientImpl should be in INITIALIZED state at first.
1465 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531466
jianlif3e52af42015-01-21 23:18:471467 // Delay start the GCM and then stop it immediately.
1468 gcm_client()->Start(GCMClient::DELAYED_START);
1469 gcm_client()->Stop();
[email protected]1abdf202014-06-13 19:38:531470 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471471 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341472}
1473
1474TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) {
1475 // GCMClientImpl should be in INITIALIZED state at first.
1476 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471477
1478 // Start the GCM and then stop it immediately.
1479 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1480 gcm_client()->Stop();
1481 PumpLoopUntilIdle();
1482 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531483}
1484
jianli9f9a73672015-06-16 19:39:341485TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471486 // GCMClientImpl should be in INITIALIZED state at first.
1487 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531488
jianlif3e52af42015-01-21 23:18:471489 // Delay start the GCM and then stop and restart it immediately.
1490 gcm_client()->Start(GCMClient::DELAYED_START);
1491 gcm_client()->Stop();
1492 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531493 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341494 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1495}
1496
1497TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) {
1498 // GCMClientImpl should be in INITIALIZED state at first.
1499 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471500
1501 // Start the GCM and then stop and restart it immediately.
1502 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1503 gcm_client()->Stop();
1504 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1505 PumpLoopUntilIdle();
1506 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1507}
1508
jianli9f9a73672015-06-16 19:39:341509TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) {
jianlif3e52af42015-01-21 23:18:471510 // GCMClientImpl should be in INITIALIZED state at first.
1511 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1512
jianli9f9a73672015-06-16 19:39:341513 // Start the GCM immediately and complete the checkin.
1514 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1515 PumpLoopUntilIdle();
1516 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441517 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianli9f9a73672015-06-16 19:39:341518 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1519
1520 // Stop the GCM.
1521 gcm_client()->Stop();
1522 PumpLoopUntilIdle();
1523 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1524
1525 // Start the GCM immediately. GCMClientImpl should be in READY state.
1526 BuildGCMClient(base::TimeDelta());
1527 InitializeGCMClient();
1528 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1529 PumpLoopUntilIdle();
1530 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1531}
1532
1533TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) {
1534 // GCMClientImpl should be in INITIALIZED state at first.
1535 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1536
1537 // Start the GCM immediately and complete the checkin.
1538 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1539 PumpLoopUntilIdle();
1540 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441541 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianli9f9a73672015-06-16 19:39:341542 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1543
1544 // Stop the GCM.
1545 gcm_client()->Stop();
1546 PumpLoopUntilIdle();
1547 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1548
1549 // Delay start the GCM. GCMClientImpl should be in LOADED state.
1550 BuildGCMClient(base::TimeDelta());
1551 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471552 gcm_client()->Start(GCMClient::DELAYED_START);
1553 PumpLoopUntilIdle();
1554 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341555}
1556
johnme902c4062015-08-21 22:06:171557TEST_F(GCMClientImplStartAndStopTest, DelayedStartRace) {
1558 // GCMClientImpl should be in INITIALIZED state at first.
1559 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1560
1561 // Delay start the GCM, then start it immediately while it's still loading.
1562 gcm_client()->Start(GCMClient::DELAYED_START);
1563 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1564 PumpLoopUntilIdle();
1565 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441566 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
johnme902c4062015-08-21 22:06:171567 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1568}
1569
jianli9f9a73672015-06-16 19:39:341570TEST_F(GCMClientImplStartAndStopTest, DelayedStart) {
1571 // GCMClientImpl should be in INITIALIZED state at first.
1572 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1573
1574 // Delay start the GCM. The store will not be loaded and GCMClientImpl should
1575 // still be in INITIALIZED state.
1576 gcm_client()->Start(GCMClient::DELAYED_START);
1577 PumpLoopUntilIdle();
1578 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471579
1580 // Start the GCM immediately and complete the checkin.
1581 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1582 PumpLoopUntilIdle();
1583 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441584 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianlif3e52af42015-01-21 23:18:471585 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1586
1587 // Registration.
1588 std::vector<std::string> senders;
1589 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391590 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:441591 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianlif3e52af42015-01-21 23:18:471592 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1593
1594 // Stop the GCM.
1595 gcm_client()->Stop();
1596 PumpLoopUntilIdle();
1597 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1598
1599 // Delay start the GCM. GCM is indeed started without delay because the
1600 // registration record has been found.
jianli9f9a73672015-06-16 19:39:341601 BuildGCMClient(base::TimeDelta());
1602 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471603 gcm_client()->Start(GCMClient::DELAYED_START);
1604 PumpLoopUntilIdle();
1605 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531606}
1607
fgorski5df101702014-10-28 02:09:311608// Test for known account mappings and last token fetching time being passed
1609// to OnGCMReady.
1610TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) {
1611 // Start the GCM and wait until it is ready.
jianlif3e52af42015-01-21 23:18:471612 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311613 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441614 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
fgorski5df101702014-10-28 02:09:311615
1616 base::Time expected_time = base::Time::Now();
1617 gcm_client()->SetLastTokenFetchTime(expected_time);
1618 AccountMapping expected_mapping;
1619 expected_mapping.account_id = "accId";
1620 expected_mapping.email = "[email protected]";
1621 expected_mapping.status = AccountMapping::MAPPED;
1622 expected_mapping.status_change_timestamp = expected_time;
1623 gcm_client()->UpdateAccountMapping(expected_mapping);
1624 PumpLoopUntilIdle();
1625
1626 // Stop the GCM.
1627 gcm_client()->Stop();
1628 PumpLoopUntilIdle();
1629
1630 // Restart the GCM.
jianlif3e52af42015-01-21 23:18:471631 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311632 PumpLoopUntilIdle();
1633
1634 EXPECT_EQ(LOADING_COMPLETED, last_event());
1635 EXPECT_EQ(expected_time, last_token_fetch_time());
1636 ASSERT_EQ(1UL, last_account_mappings().size());
1637 const AccountMapping& actual_mapping = last_account_mappings()[0];
1638 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id);
1639 EXPECT_EQ(expected_mapping.email, actual_mapping.email);
1640 EXPECT_EQ(expected_mapping.status, actual_mapping.status);
1641 EXPECT_EQ(expected_mapping.status_change_timestamp,
1642 actual_mapping.status_change_timestamp);
1643}
1644
jianlic02d25e2015-05-27 22:24:311645
1646class GCMClientInstanceIDTest : public GCMClientImplTest {
1647 public:
1648 GCMClientInstanceIDTest();
1649 ~GCMClientInstanceIDTest() override;
1650
1651 void AddInstanceID(const std::string& app_id,
1652 const std::string& instance_id);
1653 void RemoveInstanceID(const std::string& app_id);
1654 void GetToken(const std::string& app_id,
1655 const std::string& authorized_entity,
1656 const std::string& scope);
1657 void DeleteToken(const std::string& app_id,
1658 const std::string& authorized_entity,
1659 const std::string& scope);
1660 void CompleteDeleteToken();
1661 bool ExistsToken(const std::string& app_id,
1662 const std::string& authorized_entity,
1663 const std::string& scope) const;
1664};
1665
1666GCMClientInstanceIDTest::GCMClientInstanceIDTest() {
1667}
1668
1669GCMClientInstanceIDTest::~GCMClientInstanceIDTest() {
1670}
1671
1672void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id,
1673 const std::string& instance_id) {
1674 gcm_client()->AddInstanceIDData(app_id, instance_id, "123");
1675}
1676
1677void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) {
1678 gcm_client()->RemoveInstanceIDData(app_id);
1679}
1680
1681void GCMClientInstanceIDTest::GetToken(const std::string& app_id,
1682 const std::string& authorized_entity,
1683 const std::string& scope) {
dchenga77e28eb2016-04-21 21:34:371684 std::unique_ptr<InstanceIDTokenInfo> instance_id_info(
1685 new 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;
1689 gcm_client()->Register(
1690 make_linked_ptr<RegistrationInfo>(instance_id_info.release()));
1691}
1692
1693void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id,
1694 const std::string& authorized_entity,
1695 const std::string& scope) {
dchenga77e28eb2016-04-21 21:34:371696 std::unique_ptr<InstanceIDTokenInfo> instance_id_info(
1697 new InstanceIDTokenInfo);
jianlic02d25e2015-05-27 22:24:311698 instance_id_info->app_id = app_id;
1699 instance_id_info->authorized_entity = authorized_entity;
1700 instance_id_info->scope = scope;
1701 gcm_client()->Unregister(
1702 make_linked_ptr<RegistrationInfo>(instance_id_info.release()));
1703}
1704
1705void GCMClientInstanceIDTest::CompleteDeleteToken() {
1706 std::string response(kDeleteTokenResponse);
Maks Orlovich278f2572018-07-16 18:18:411707
1708 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
1709 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
1710 network::CreateResourceResponseHead(net::HTTP_OK), response));
1711
jianlid7e80f22015-06-18 22:21:311712 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
1713 PumpLoopUntilIdle();
jianlic02d25e2015-05-27 22:24:311714}
1715
1716bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id,
1717 const std::string& authorized_entity,
1718 const std::string& scope) const {
dchenga77e28eb2016-04-21 21:34:371719 std::unique_ptr<InstanceIDTokenInfo> instance_id_info(
1720 new InstanceIDTokenInfo);
jianlic02d25e2015-05-27 22:24:311721 instance_id_info->app_id = app_id;
1722 instance_id_info->authorized_entity = authorized_entity;
1723 instance_id_info->scope = scope;
1724 return gcm_client()->registrations_.count(
1725 make_linked_ptr<RegistrationInfo>(instance_id_info.release())) > 0;
1726}
1727
1728TEST_F(GCMClientInstanceIDTest, GetToken) {
johnme627dc8c72016-08-19 21:49:391729 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311730
1731 // Get a token.
johnme627dc8c72016-08-19 21:49:391732 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1733 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441734 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311735
1736 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391737 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311738 EXPECT_EQ("token1", last_registration_id());
1739 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391740 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311741
1742 // Get another token.
johnme627dc8c72016-08-19 21:49:391743 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1744 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441745 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311746
1747 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391748 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311749 EXPECT_EQ("token2", last_registration_id());
1750 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391751 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311752 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391753 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
1754}
1755
1756// Most tests in this file use kExtensionAppId which is special-cased by
1757// InstanceIDUsesSubtypeForAppId in gcm_client_impl.cc. This test uses
1758// kSubtypeAppId to cover the alternate case.
1759TEST_F(GCMClientInstanceIDTest, GetTokenWithSubtype) {
1760 ASSERT_EQ(GCMClientImpl::READY, gcm_client_state());
1761
1762 AddInstanceID(kSubtypeAppId, kInstanceID);
1763
1764 EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope));
1765
1766 // Get a token.
1767 GetToken(kSubtypeAppId, kSender, kScope);
1768 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1769 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1770 EXPECT_EQ(kSubtypeAppId, last_app_id());
1771 EXPECT_EQ("token1", last_registration_id());
1772 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1773 EXPECT_TRUE(ExistsToken(kSubtypeAppId, kSender, kScope));
1774
1775 // Delete the token.
1776 DeleteToken(kSubtypeAppId, kSender, kScope);
1777 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
1778 EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311779}
1780
jianli70715cc2015-06-23 21:46:481781TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) {
johnme627dc8c72016-08-19 21:49:391782 AddInstanceID(kExtensionAppId, kInstanceID);
jianli70715cc2015-06-23 21:46:481783
1784 // Delete an invalid token.
johnme627dc8c72016-08-19 21:49:391785 DeleteToken(kExtensionAppId, "Foo@#$", kScope);
jianli70715cc2015-06-23 21:46:481786 PumpLoopUntilIdle();
1787
1788 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391789 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481790 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1791
1792 reset_last_event();
1793
1794 // Delete a non-existing token.
johnme627dc8c72016-08-19 21:49:391795 DeleteToken(kExtensionAppId, kSender, kScope);
jianli70715cc2015-06-23 21:46:481796 PumpLoopUntilIdle();
1797
1798 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391799 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481800 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1801}
1802
jianlic02d25e2015-05-27 22:24:311803TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) {
johnme627dc8c72016-08-19 21:49:391804 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311805
1806 // Get a token.
johnme627dc8c72016-08-19 21:49:391807 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1808 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441809 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311810
1811 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391812 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311813 EXPECT_EQ("token1", last_registration_id());
1814 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391815 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311816
jianli70715cc2015-06-23 21:46:481817 reset_last_event();
1818
jianlic02d25e2015-05-27 22:24:311819 // Get another token.
johnme627dc8c72016-08-19 21:49:391820 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1821 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441822 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311823
1824 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391825 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311826 EXPECT_EQ("token2", last_registration_id());
1827 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391828 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311829 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391830 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311831
jianli70715cc2015-06-23 21:46:481832 reset_last_event();
1833
jianlic02d25e2015-05-27 22:24:311834 // Delete the 2nd token.
johnme627dc8c72016-08-19 21:49:391835 DeleteToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441836 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311837
1838 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391839 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311840 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1841 // The 2nd token is gone while the 1st token still exists.
johnme627dc8c72016-08-19 21:49:391842 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
1843 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311844
jianli70715cc2015-06-23 21:46:481845 reset_last_event();
1846
jianlic02d25e2015-05-27 22:24:311847 // Delete the 1st token.
johnme627dc8c72016-08-19 21:49:391848 DeleteToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441849 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311850
1851 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391852 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311853 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1854 // Both tokens are gone now.
johnme627dc8c72016-08-19 21:49:391855 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1856 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
jianli70715cc2015-06-23 21:46:481857
1858 reset_last_event();
1859
1860 // Trying to delete the token again will get an error.
johnme627dc8c72016-08-19 21:49:391861 DeleteToken(kExtensionAppId, kSender, kScope);
jianli70715cc2015-06-23 21:46:481862 PumpLoopUntilIdle();
1863
1864 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391865 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481866 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
jianlic02d25e2015-05-27 22:24:311867}
1868
jianliea8534872015-06-22 21:06:221869TEST_F(GCMClientInstanceIDTest, DeleteAllTokens) {
johnme627dc8c72016-08-19 21:49:391870 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311871
1872 // Get a token.
johnme627dc8c72016-08-19 21:49:391873 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1874 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441875 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311876
1877 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391878 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311879 EXPECT_EQ("token1", last_registration_id());
1880 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391881 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311882
jianli70715cc2015-06-23 21:46:481883 reset_last_event();
1884
jianlic02d25e2015-05-27 22:24:311885 // Get another token.
johnme627dc8c72016-08-19 21:49:391886 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1887 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441888 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311889
1890 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391891 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311892 EXPECT_EQ("token2", last_registration_id());
1893 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391894 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311895 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391896 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311897
jianli70715cc2015-06-23 21:46:481898 reset_last_event();
1899
jianlic02d25e2015-05-27 22:24:311900 // Delete all tokens.
johnme627dc8c72016-08-19 21:49:391901 DeleteToken(kExtensionAppId, "*", "*");
johnme0fb2fc02016-07-14 14:47:441902 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311903
1904 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391905 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311906 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1907 // All tokens are gone now.
johnme627dc8c72016-08-19 21:49:391908 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1909 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311910}
1911
jianliea8534872015-06-22 21:06:221912TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) {
johnme627dc8c72016-08-19 21:49:391913 AddInstanceID(kExtensionAppId, kInstanceID);
jianliea8534872015-06-22 21:06:221914
1915 // Delete all tokens without getting a token first.
johnme627dc8c72016-08-19 21:49:391916 DeleteToken(kExtensionAppId, "*", "*");
jianliea8534872015-06-22 21:06:221917 // No need to call CompleteDeleteToken since unregistration request should
1918 // not be triggered.
1919 PumpLoopUntilIdle();
1920
1921 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391922 EXPECT_EQ(kExtensionAppId, last_app_id());
jianliea8534872015-06-22 21:06:221923 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1924}
1925
johnme627dc8c72016-08-19 21:49:391926TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) {
1927 AddInstanceID(kExtensionAppId, kInstanceID);
1928 GetToken(kExtensionAppId, kSender, kScope);
1929 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
johnme627dc8c72016-08-19 21:49:391930
1931 std::map<std::string, std::string> expected_data;
1932
Peter Beverlooc66fc63c2017-07-07 22:58:211933 MCSMessage message(BuildDownstreamMessage(
johnme627dc8c72016-08-19 21:49:391934 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1935 std::string() /* raw_data */));
Peter Beverlooc66fc63c2017-07-07 22:58:211936 EXPECT_TRUE(message.IsValid());
1937 ReceiveMessageFromMCS(message);
johnme627dc8c72016-08-19 21:49:391938
1939 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1940 EXPECT_EQ(kExtensionAppId, last_app_id());
1941 EXPECT_EQ(expected_data.size(), last_message().data.size());
1942 EXPECT_EQ(expected_data, last_message().data);
1943 EXPECT_EQ(kSender, last_message().sender_id);
johnme627dc8c72016-08-19 21:49:391944}
1945
1946TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) {
1947 AddInstanceID(kSubtypeAppId, kInstanceID);
1948 GetToken(kSubtypeAppId, kSender, kScope);
1949 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
johnme627dc8c72016-08-19 21:49:391950
1951 std::map<std::string, std::string> expected_data;
1952
Peter Beverlooc66fc63c2017-07-07 22:58:211953 MCSMessage message(BuildDownstreamMessage(
johnme627dc8c72016-08-19 21:49:391954 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */,
1955 expected_data, std::string() /* raw_data */));
Peter Beverlooc66fc63c2017-07-07 22:58:211956 EXPECT_TRUE(message.IsValid());
1957 ReceiveMessageFromMCS(message);
johnme627dc8c72016-08-19 21:49:391958
1959 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1960 EXPECT_EQ(kSubtypeAppId, last_app_id());
1961 EXPECT_EQ(expected_data.size(), last_message().data.size());
1962 EXPECT_EQ(expected_data, last_message().data);
1963 EXPECT_EQ(kSender, last_message().sender_id);
johnme627dc8c72016-08-19 21:49:391964}
1965
1966TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithFakeSubtype) {
1967 // Victim non-extension registration.
1968 AddInstanceID(kSubtypeAppId, "iid_1");
1969 GetToken(kSubtypeAppId, kSender, kScope);
1970 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1971
1972 // Malicious extension registration.
1973 AddInstanceID(kExtensionAppId, "iid_2");
1974 GetToken(kExtensionAppId, kSender, kScope);
1975 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
1976
1977 std::map<std::string, std::string> expected_data;
1978
1979 // Message for kExtensionAppId should be delivered to the extension rather
1980 // than the victim app, despite the malicious subtype property attempting to
1981 // impersonate victim app.
1982 MCSMessage message(BuildDownstreamMessage(
1983 kSender, kExtensionAppId /* category */, kSubtypeAppId /* subtype */,
1984 expected_data, std::string() /* raw_data */));
1985 EXPECT_TRUE(message.IsValid());
1986 ReceiveMessageFromMCS(message);
1987
1988 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1989 EXPECT_EQ(kExtensionAppId, last_app_id());
1990 EXPECT_EQ(expected_data.size(), last_message().data.size());
1991 EXPECT_EQ(expected_data, last_message().data);
1992 EXPECT_EQ(kSender, last_message().sender_id);
1993}
1994
[email protected]848b1b62014-01-30 23:51:041995} // namespace gcm