blob: 941ba3005baf2ed24ec7472d6425658bd1560139 [file] [log] [blame]
[email protected]848b1b62014-01-30 23:51:041// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cd57f372014-06-09 17:13:065#include "components/gcm_driver/gcm_client_impl.h"
[email protected]848b1b62014-01-30 23:51:046
avi26062922015-12-26 00:14:187#include <stdint.h>
8
johnme627dc8c72016-08-19 21:49:399#include <initializer_list>
dchenga77e28eb2016-04-21 21:34:3710#include <memory>
11
Helen Li5f3d96a2018-08-10 20:37:2412#include "base/bind_helpers.h"
[email protected]fdeaf9732014-04-11 21:02:1513#include "base/command_line.h"
jianli00b4600f2015-02-10 23:32:4914#include "base/files/file_path.h"
15#include "base/files/file_util.h"
[email protected]848b1b62014-01-30 23:51:0416#include "base/files/scoped_temp_dir.h"
avi26062922015-12-26 00:14:1817#include "base/macros.h"
dchenga77e28eb2016-04-21 21:34:3718#include "base/memory/ptr_util.h"
[email protected]764c0442014-05-01 04:30:5519#include "base/strings/string_number_conversions.h"
Devlin Cronin69228f42018-06-01 17:25:1020#include "base/test/metrics/histogram_tester.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1321#include "base/test/scoped_feature_list.h"
Gabriel Charettec7108742019-08-23 03:31:4022#include "base/test/task_environment.h"
[email protected]764c0442014-05-01 04:30:5523#include "base/time/clock.h"
chirantan26436e402014-10-24 19:44:4224#include "base/timer/timer.h"
Mugdha Lakhanic3d792e2018-02-13 15:10:1325#include "components/gcm_driver/features.h"
[email protected]446f73c22014-05-14 20:47:1826#include "google_apis/gcm/base/fake_encryptor.h"
[email protected]848b1b62014-01-30 23:51:0427#include "google_apis/gcm/base/mcs_message.h"
28#include "google_apis/gcm/base/mcs_util.h"
29#include "google_apis/gcm/engine/fake_connection_factory.h"
30#include "google_apis/gcm/engine/fake_connection_handler.h"
[email protected]764c0442014-05-01 04:30:5531#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]436bcb82014-04-18 00:40:5732#include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
[email protected]848b1b62014-01-30 23:51:0433#include "google_apis/gcm/protocol/android_checkin.pb.h"
34#include "google_apis/gcm/protocol/checkin.pb.h"
35#include "google_apis/gcm/protocol/mcs.pb.h"
johnme627dc8c72016-08-19 21:49:3936#include "net/test/gtest_util.h"
37#include "net/test/scoped_disable_exit_on_dfatal.h"
[email protected]848b1b62014-01-30 23:51:0438#include "net/url_request/test_url_fetcher_factory.h"
39#include "net/url_request/url_fetcher_delegate.h"
40#include "net/url_request/url_request_test_util.h"
Maks Orlovichc70c93c2018-07-12 02:45:4441#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
Robbie McElrathb01499332018-09-25 00:53:1342#include "services/network/test/test_network_connection_tracker.h"
Maks Orlovichc70c93c2018-07-12 02:45:4443#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;
Rayan Kansobcec0292019-05-09 13:27:5183const char kMessageId[] = "0:12345%5678";
jianlic02d25e2015-05-27 22:24:3184
Maks Orlovichc70c93c2018-07-12 02:45:4485const char kRegisterUrl[] = "https://android.clients.google.com/c2dm/register3";
86
[email protected]848b1b62014-01-30 23:51:0487// Helper for building arbitrary data messages.
88MCSMessage BuildDownstreamMessage(
89 const std::string& project_id,
johnme627dc8c72016-08-19 21:49:3990 const std::string& category,
91 const std::string& subtype,
petera5aedc52015-07-22 10:47:3092 const std::map<std::string, std::string>& data,
93 const std::string& raw_data) {
[email protected]848b1b62014-01-30 23:51:0494 mcs_proto::DataMessageStanza data_message;
[email protected]848b1b62014-01-30 23:51:0495 data_message.set_from(project_id);
johnme627dc8c72016-08-19 21:49:3996 data_message.set_category(category);
jdoerrie3feb1852018-10-05 12:16:4497 for (auto iter = data.begin(); iter != data.end(); ++iter) {
[email protected]848b1b62014-01-30 23:51:0498 mcs_proto::AppData* app_data = data_message.add_app_data();
99 app_data->set_key(iter->first);
100 app_data->set_value(iter->second);
101 }
johnme627dc8c72016-08-19 21:49:39102 if (!subtype.empty()) {
103 mcs_proto::AppData* app_data = data_message.add_app_data();
104 app_data->set_key("subtype");
105 app_data->set_value(subtype);
106 }
petera5aedc52015-07-22 10:47:30107 data_message.set_raw_data(raw_data);
Rayan Kansobcec0292019-05-09 13:27:51108 data_message.set_persistent_id(kMessageId);
[email protected]848b1b62014-01-30 23:51:04109 return MCSMessage(kDataMessageStanzaTag, data_message);
110}
111
fgorski58b9dfc2014-09-29 16:46:18112GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email,
113 const std::string& token) {
114 GCMClient::AccountTokenInfo account_token;
115 account_token.email = email;
116 account_token.access_token = token;
117 return account_token;
118}
119
120std::map<std::string, std::string> MakeEmailToTokenMap(
121 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
122 std::map<std::string, std::string> email_token_map;
jdoerrie3feb1852018-10-05 12:16:44123 for (auto iter = account_tokens.begin(); iter != account_tokens.end();
124 ++iter) {
fgorski58b9dfc2014-09-29 16:46:18125 email_token_map[iter->email] = iter->access_token;
126 }
127 return email_token_map;
128}
129
[email protected]848b1b62014-01-30 23:51:04130class FakeMCSClient : public MCSClient {
131 public:
132 FakeMCSClient(base::Clock* clock,
[email protected]2bbe0a682014-03-26 00:08:31133 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57134 GCMStore* gcm_store,
Steven Zhub36394c2019-06-04 16:10:46135 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
[email protected]436bcb82014-04-18 00:40:57136 GCMStatsRecorder* recorder);
dcheng00ea022b2014-10-21 11:24:56137 ~FakeMCSClient() override;
avi26062922015-12-26 00:14:18138 void Login(uint64_t android_id, uint64_t security_token) override;
dcheng00ea022b2014-10-21 11:24:56139 void SendMessage(const MCSMessage& message) override;
[email protected]848b1b62014-01-30 23:51:04140
avi26062922015-12-26 00:14:18141 uint64_t last_android_id() const { return last_android_id_; }
142 uint64_t last_security_token() const { return last_security_token_; }
143 uint8_t last_message_tag() const { return last_message_tag_; }
[email protected]848b1b62014-01-30 23:51:04144 const mcs_proto::DataMessageStanza& last_data_message_stanza() const {
145 return last_data_message_stanza_;
146 }
147
148 private:
avi26062922015-12-26 00:14:18149 uint64_t last_android_id_;
150 uint64_t last_security_token_;
151 uint8_t last_message_tag_;
[email protected]848b1b62014-01-30 23:51:04152 mcs_proto::DataMessageStanza last_data_message_stanza_;
153};
154
Steven Zhub36394c2019-06-04 16:10:46155FakeMCSClient::FakeMCSClient(
156 base::Clock* clock,
157 ConnectionFactory* connection_factory,
158 GCMStore* gcm_store,
159 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
160 GCMStatsRecorder* recorder)
161 : MCSClient("",
162 clock,
163 connection_factory,
164 gcm_store,
165 io_task_runner,
166 recorder),
[email protected]848b1b62014-01-30 23:51:04167 last_android_id_(0u),
168 last_security_token_(0u),
Steven Zhub36394c2019-06-04 16:10:46169 last_message_tag_(kNumProtoTypes) {}
[email protected]848b1b62014-01-30 23:51:04170
171FakeMCSClient::~FakeMCSClient() {
172}
173
avi26062922015-12-26 00:14:18174void FakeMCSClient::Login(uint64_t android_id, uint64_t security_token) {
[email protected]848b1b62014-01-30 23:51:04175 last_android_id_ = android_id;
176 last_security_token_ = security_token;
177}
178
179void FakeMCSClient::SendMessage(const MCSMessage& message) {
180 last_message_tag_ = message.tag();
181 if (last_message_tag_ == kDataMessageStanzaTag) {
182 last_data_message_stanza_.CopyFrom(
183 reinterpret_cast<const mcs_proto::DataMessageStanza&>(
184 message.GetProtobuf()));
185 }
186}
187
[email protected]764c0442014-05-01 04:30:55188class AutoAdvancingTestClock : public base::Clock {
189 public:
190 explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta);
dcheng00ea022b2014-10-21 11:24:56191 ~AutoAdvancingTestClock() override;
[email protected]764c0442014-05-01 04:30:55192
tzikee78e4962018-04-13 12:25:46193 base::Time Now() const override;
[email protected]764c0442014-05-01 04:30:55194 void Advance(TimeDelta delta);
195 int call_count() const { return call_count_; }
196
197 private:
tzikee78e4962018-04-13 12:25:46198 mutable int call_count_;
[email protected]764c0442014-05-01 04:30:55199 base::TimeDelta auto_increment_time_delta_;
tzikee78e4962018-04-13 12:25:46200 mutable base::Time now_;
[email protected]764c0442014-05-01 04:30:55201
202 DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock);
203};
204
205AutoAdvancingTestClock::AutoAdvancingTestClock(
206 base::TimeDelta auto_increment_time_delta)
207 : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) {
208}
209
210AutoAdvancingTestClock::~AutoAdvancingTestClock() {
211}
212
tzikee78e4962018-04-13 12:25:46213base::Time AutoAdvancingTestClock::Now() const {
[email protected]764c0442014-05-01 04:30:55214 call_count_++;
215 now_ += auto_increment_time_delta_;
216 return now_;
217}
218
219void AutoAdvancingTestClock::Advance(base::TimeDelta delta) {
220 now_ += delta;
221}
222
[email protected]2bbe0a682014-03-26 00:08:31223class FakeGCMInternalsBuilder : public GCMInternalsBuilder {
224 public:
tzik48479d92018-03-20 15:20:42225 explicit FakeGCMInternalsBuilder(base::TimeDelta clock_step);
dcheng00ea022b2014-10-21 11:24:56226 ~FakeGCMInternalsBuilder() override;
[email protected]2bbe0a682014-03-26 00:08:31227
tzik48479d92018-03-20 15:20:42228 base::Clock* GetClock() override;
dchenga77e28eb2016-04-21 21:34:37229 std::unique_ptr<MCSClient> BuildMCSClient(
230 const std::string& version,
231 base::Clock* clock,
232 ConnectionFactory* connection_factory,
233 GCMStore* gcm_store,
Steven Zhub36394c2019-06-04 16:10:46234 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
dchenga77e28eb2016-04-21 21:34:37235 GCMStatsRecorder* recorder) override;
236 std::unique_ptr<ConnectionFactory> BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31237 const std::vector<GURL>& endpoints,
238 const net::BackoffEntry::Policy& backoff_policy,
Helen Li5f3d96a2018-08-10 20:37:24239 base::RepeatingCallback<
240 void(network::mojom::ProxyResolvingSocketFactoryRequest)>
241 get_socket_factory_callback,
Steven Zhub36394c2019-06-04 16:10:46242 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
Robbie McElrathb01499332018-09-25 00:53:13243 GCMStatsRecorder* recorder,
244 network::NetworkConnectionTracker* network_connection_tracker) override;
[email protected]764c0442014-05-01 04:30:55245
246 private:
tzik48479d92018-03-20 15:20:42247 AutoAdvancingTestClock clock_;
[email protected]2bbe0a682014-03-26 00:08:31248};
249
[email protected]764c0442014-05-01 04:30:55250FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step)
tzik48479d92018-03-20 15:20:42251 : clock_(clock_step) {}
[email protected]2bbe0a682014-03-26 00:08:31252
253FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {}
254
tzik48479d92018-03-20 15:20:42255base::Clock* FakeGCMInternalsBuilder::GetClock() {
256 return &clock_;
[email protected]2bbe0a682014-03-26 00:08:31257}
258
dchenga77e28eb2016-04-21 21:34:37259std::unique_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient(
[email protected]2bbe0a682014-03-26 00:08:31260 const std::string& version,
261 base::Clock* clock,
262 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57263 GCMStore* gcm_store,
Steven Zhub36394c2019-06-04 16:10:46264 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
[email protected]436bcb82014-04-18 00:40:57265 GCMStatsRecorder* recorder) {
Steven Zhub36394c2019-06-04 16:10:46266 return base::WrapUnique<MCSClient>(new FakeMCSClient(
267 clock, connection_factory, gcm_store, io_task_runner, recorder));
[email protected]2bbe0a682014-03-26 00:08:31268}
269
dchenga77e28eb2016-04-21 21:34:37270std::unique_ptr<ConnectionFactory>
271FakeGCMInternalsBuilder::BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31272 const std::vector<GURL>& endpoints,
273 const net::BackoffEntry::Policy& backoff_policy,
Helen Li5f3d96a2018-08-10 20:37:24274 base::RepeatingCallback<
275 void(network::mojom::ProxyResolvingSocketFactoryRequest)>
276 get_socket_factory_callback,
Steven Zhub36394c2019-06-04 16:10:46277 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
Robbie McElrathb01499332018-09-25 00:53:13278 GCMStatsRecorder* recorder,
279 network::NetworkConnectionTracker* network_connection_tracker) {
dchenga77e28eb2016-04-21 21:34:37280 return base::WrapUnique<ConnectionFactory>(new FakeConnectionFactory());
[email protected]2bbe0a682014-03-26 00:08:31281}
282
[email protected]848b1b62014-01-30 23:51:04283} // namespace
284
285class GCMClientImplTest : public testing::Test,
286 public GCMClient::Delegate {
287 public:
288 GCMClientImplTest();
dcheng30a1b1542014-10-29 21:27:50289 ~GCMClientImplTest() override;
[email protected]848b1b62014-01-30 23:51:04290
dcheng30a1b1542014-10-29 21:27:50291 void SetUp() override;
Mugdha Lakhanic3d792e2018-02-13 15:10:13292 void TearDown() override;
293
294 void SetFeatureParams(const base::Feature& feature,
Alexei Svitkine8160de8d2019-07-02 00:06:37295 const base::FieldTrialParams& params);
[email protected]848b1b62014-01-30 23:51:04296
fgorski5df101702014-10-28 02:09:31297 void SetUpUrlFetcherFactory();
Mugdha Lakhanic3d792e2018-02-13 15:10:13298 void InitializeInvalidationFieldTrial();
fgorski5df101702014-10-28 02:09:31299
[email protected]764c0442014-05-01 04:30:55300 void BuildGCMClient(base::TimeDelta clock_step);
[email protected]848b1b62014-01-30 23:51:04301 void InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53302 void StartGCMClient();
jianli7a0c9b62015-05-26 23:24:47303 void Register(const std::string& app_id,
304 const std::vector<std::string>& senders);
305 void Unregister(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04306 void ReceiveMessageFromMCS(const MCSMessage& message);
[email protected]292af2b22014-08-06 19:42:45307 void ReceiveOnMessageSentToMCS(
308 const std::string& app_id,
309 const std::string& message_id,
310 const MCSClient::MessageSendStatus status);
zea76342abf2016-11-01 17:26:04311 void FailCheckin(net::HttpStatusCode response_code);
avi26062922015-12-26 00:14:18312 void CompleteCheckin(uint64_t android_id,
313 uint64_t security_token,
[email protected]764c0442014-05-01 04:30:55314 const std::string& digest,
315 const std::map<std::string, std::string>& settings);
zea76342abf2016-11-01 17:26:04316 void CompleteCheckinImpl(uint64_t android_id,
317 uint64_t security_token,
318 const std::string& digest,
319 const std::map<std::string, std::string>& settings,
320 net::HttpStatusCode response_code);
[email protected]848b1b62014-01-30 23:51:04321 void CompleteRegistration(const std::string& registration_id);
[email protected]e4007042014-02-15 20:34:28322 void CompleteUnregistration(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04323
[email protected]3a20a4d2014-03-21 22:54:21324 bool ExistsRegistration(const std::string& app_id) const;
325 void AddRegistration(const std::string& app_id,
326 const std::vector<std::string>& sender_ids,
327 const std::string& registration_id);
328
[email protected]848b1b62014-01-30 23:51:04329 // GCMClient::Delegate overrides (for verification).
Rayan Kanso3da434f2018-12-19 17:09:39330 void OnRegisterFinished(scoped_refptr<RegistrationInfo> registration_info,
dcheng00ea022b2014-10-21 11:24:56331 const std::string& registration_id,
332 GCMClient::Result result) override;
Rayan Kanso3da434f2018-12-19 17:09:39333 void OnUnregisterFinished(scoped_refptr<RegistrationInfo> registration_info,
334 GCMClient::Result result) override;
dcheng00ea022b2014-10-21 11:24:56335 void OnSendFinished(const std::string& app_id,
336 const std::string& message_id,
337 GCMClient::Result result) override {}
338 void OnMessageReceived(const std::string& registration_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06339 const IncomingMessage& message) override;
dcheng00ea022b2014-10-21 11:24:56340 void OnMessagesDeleted(const std::string& app_id) override;
341 void OnMessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12342 const std::string& app_id,
mostynbfe59f482014-10-06 15:04:46343 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
dcheng00ea022b2014-10-21 11:24:56344 void OnSendAcknowledged(const std::string& app_id,
345 const std::string& message_id) override;
fgorski5df101702014-10-28 02:09:31346 void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
347 const base::Time& last_token_fetch_time) override;
dcheng00ea022b2014-10-21 11:24:56348 void OnActivityRecorded() override {}
349 void OnConnected(const net::IPEndPoint& ip_endpoint) override {}
350 void OnDisconnected() override {}
johnme93ec7932016-11-17 14:26:58351 void OnStoreReset() override {}
[email protected]848b1b62014-01-30 23:51:04352
[email protected]848b1b62014-01-30 23:51:04353 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
jianlif3e52af42015-01-21 23:18:47354 GCMClientImpl::State gcm_client_state() const {
355 return gcm_client_->state_;
356 }
[email protected]848b1b62014-01-30 23:51:04357 FakeMCSClient* mcs_client() const {
358 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
359 }
[email protected]2bbe0a682014-03-26 00:08:31360 ConnectionFactory* connection_factory() const {
361 return gcm_client_->connection_factory_.get();
362 }
[email protected]848b1b62014-01-30 23:51:04363
[email protected]7df5ef22014-07-17 07:35:58364 const GCMClientImpl::CheckinInfo& device_checkin_info() const {
365 return gcm_client_->device_checkin_info_;
366 }
367
[email protected]3a20a4d2014-03-21 22:54:21368 void reset_last_event() {
369 last_event_ = NONE;
370 last_app_id_.clear();
371 last_registration_id_.clear();
372 last_message_id_.clear();
373 last_result_ = GCMClient::UNKNOWN_ERROR;
fgorski5df101702014-10-28 02:09:31374 last_account_mappings_.clear();
375 last_token_fetch_time_ = base::Time();
[email protected]3a20a4d2014-03-21 22:54:21376 }
377
[email protected]848b1b62014-01-30 23:51:04378 LastEvent last_event() const { return last_event_; }
379 const std::string& last_app_id() const { return last_app_id_; }
380 const std::string& last_registration_id() const {
381 return last_registration_id_;
382 }
383 const std::string& last_message_id() const { return last_message_id_; }
384 GCMClient::Result last_result() const { return last_result_; }
mvanouwerkerkf8633deb2015-07-13 11:04:06385 const IncomingMessage& last_message() const { return last_message_; }
[email protected]c6fe36b2014-03-11 10:58:12386 const GCMClient::SendErrorDetails& last_error_details() const {
387 return last_error_details_;
388 }
fgorski5df101702014-10-28 02:09:31389 const base::Time& last_token_fetch_time() const {
390 return last_token_fetch_time_;
391 }
392 const std::vector<AccountMapping>& last_account_mappings() {
393 return last_account_mappings_;
394 }
[email protected]848b1b62014-01-30 23:51:04395
[email protected]06e45272014-05-06 03:41:34396 const GServicesSettings& gservices_settings() const {
397 return gcm_client_->gservices_settings_;
[email protected]764c0442014-05-01 04:30:55398 }
399
jianli00b4600f2015-02-10 23:32:49400 const base::FilePath& temp_directory_path() const {
vabr0c237ae2016-09-14 09:24:28401 return temp_directory_.GetPath();
jianli00b4600f2015-02-10 23:32:49402 }
403
jianli9f9a73672015-06-16 19:39:34404 base::FilePath gcm_store_path() const {
405 // Pass an non-existent directory as store path to match the exact
406 // behavior in the production code. Currently GCMStoreImpl checks if
407 // the directory exist or not to determine the store existence.
vabr0c237ae2016-09-14 09:24:28408 return temp_directory_.GetPath().Append(FILE_PATH_LITERAL("GCM Store"));
jianli9f9a73672015-06-16 19:39:34409 }
410
avi26062922015-12-26 00:14:18411 int64_t CurrentTime();
[email protected]d3ba08d92014-04-04 23:03:55412
413 // Tooling.
[email protected]d3ba08d92014-04-04 23:03:55414 void PumpLoopUntilIdle();
[email protected]764c0442014-05-01 04:30:55415 bool CreateUniqueTempDir();
416 AutoAdvancingTestClock* clock() const {
tzik48479d92018-03-20 15:20:42417 return static_cast<AutoAdvancingTestClock*>(gcm_client_->clock_);
[email protected]a0144ceb2014-04-04 23:49:34418 }
jianlic02d25e2015-05-27 22:24:31419 net::TestURLFetcherFactory* url_fetcher_factory() {
420 return &url_fetcher_factory_;
421 }
Maks Orlovichc70c93c2018-07-12 02:45:44422 network::TestURLLoaderFactory* url_loader_factory() {
423 return &test_url_loader_factory_;
424 }
Hajime Hoshi7a5c8c82019-01-10 05:44:51425
426 void FastForwardBy(const base::TimeDelta& duration) {
Gabriel Charettedfa36042019-08-19 17:30:11427 task_environment_.FastForwardBy(duration);
jianli78b56042015-06-17 01:21:22428 }
[email protected]d3ba08d92014-04-04 23:03:55429
[email protected]764c0442014-05-01 04:30:55430 private:
Gabriel Charettedfa36042019-08-19 17:30:11431 base::test::TaskEnvironment task_environment_{
432 base::test::TaskEnvironment::TimeSource::MOCK_TIME};
Hajime Hoshi7a5c8c82019-01-10 05:44:51433
wkormanc1e71b62015-11-09 22:56:04434 // Must be declared first so that it is destroyed last. Injected to
435 // GCM client.
436 base::ScopedTempDir temp_directory_;
437
[email protected]848b1b62014-01-30 23:51:04438 // Variables used for verification.
439 LastEvent last_event_;
440 std::string last_app_id_;
441 std::string last_registration_id_;
442 std::string last_message_id_;
443 GCMClient::Result last_result_;
mvanouwerkerkf8633deb2015-07-13 11:04:06444 IncomingMessage last_message_;
[email protected]c6fe36b2014-03-11 10:58:12445 GCMClient::SendErrorDetails last_error_details_;
fgorski5df101702014-10-28 02:09:31446 base::Time last_token_fetch_time_;
447 std::vector<AccountMapping> last_account_mappings_;
[email protected]848b1b62014-01-30 23:51:04448
dchenga77e28eb2016-04-21 21:34:37449 std::unique_ptr<GCMClientImpl> gcm_client_;
[email protected]848b1b62014-01-30 23:51:04450
[email protected]848b1b62014-01-30 23:51:04451 net::TestURLFetcherFactory url_fetcher_factory_;
452
wkormanc1e71b62015-11-09 22:56:04453 // Injected to GCM client.
[email protected]848b1b62014-01-30 23:51:04454 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
Maks Orlovichc70c93c2018-07-12 02:45:44455 network::TestURLLoaderFactory test_url_loader_factory_;
Mugdha Lakhanic3d792e2018-02-13 15:10:13456 base::test::ScopedFeatureList scoped_feature_list_;
[email protected]848b1b62014-01-30 23:51:04457};
458
459GCMClientImplTest::GCMClientImplTest()
460 : last_event_(NONE),
461 last_result_(GCMClient::UNKNOWN_ERROR),
Hajime Hoshi7a5c8c82019-01-10 05:44:51462 url_request_context_getter_(new net::TestURLRequestContextGetter(
Gabriel Charettedfa36042019-08-19 17:30:11463 task_environment_.GetMainThreadTaskRunner())) {}
[email protected]848b1b62014-01-30 23:51:04464
465GCMClientImplTest::~GCMClientImplTest() {}
466
467void GCMClientImplTest::SetUp() {
[email protected]fdeaf9732014-04-11 21:02:15468 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55469 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:55470 BuildGCMClient(base::TimeDelta());
[email protected]848b1b62014-01-30 23:51:04471 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53472 StartGCMClient();
fgorski5df101702014-10-28 02:09:31473 SetUpUrlFetcherFactory();
Mugdha Lakhanic3d792e2018-02-13 15:10:13474 InitializeInvalidationFieldTrial();
johnme0fb2fc02016-07-14 14:47:44475 ASSERT_NO_FATAL_FAILURE(
476 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
477 std::map<std::string, std::string>()));
[email protected]848b1b62014-01-30 23:51:04478}
479
Mugdha Lakhanic3d792e2018-02-13 15:10:13480void GCMClientImplTest::TearDown() {
Mugdha Lakhanic3d792e2018-02-13 15:10:13481}
482
fgorski5df101702014-10-28 02:09:31483void GCMClientImplTest::SetUpUrlFetcherFactory() {
484 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
485}
486
Alexei Svitkine8160de8d2019-07-02 00:06:37487void GCMClientImplTest::SetFeatureParams(const base::Feature& feature,
488 const base::FieldTrialParams& params) {
489 scoped_feature_list_.InitAndEnableFeatureWithParameters(feature, params);
490
491 base::FieldTrialParams actual_params;
Mugdha Lakhanic3d792e2018-02-13 15:10:13492 EXPECT_TRUE(base::GetFieldTrialParamsByFeature(
493 features::kInvalidateTokenFeature, &actual_params));
494 EXPECT_EQ(params, actual_params);
495}
496
497void GCMClientImplTest::InitializeInvalidationFieldTrial() {
Mugdha Lakhanic3d792e2018-02-13 15:10:13498 std::map<std::string, std::string> params;
499 params[features::kParamNameTokenInvalidationPeriodDays] =
500 std::to_string(kTestTokenInvalidationPeriod);
501 ASSERT_NO_FATAL_FAILURE(
502 SetFeatureParams(features::kInvalidateTokenFeature, std::move(params)));
Mugdha Lakhanic3d792e2018-02-13 15:10:13503}
504
[email protected]848b1b62014-01-30 23:51:04505void GCMClientImplTest::PumpLoopUntilIdle() {
Gabriel Charettedfa36042019-08-19 17:30:11506 task_environment_.RunUntilIdle();
[email protected]848b1b62014-01-30 23:51:04507}
508
[email protected]764c0442014-05-01 04:30:55509bool GCMClientImplTest::CreateUniqueTempDir() {
510 return temp_directory_.CreateUniqueTempDir();
511}
512
513void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
dchenga77e28eb2016-04-21 21:34:37514 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>(
[email protected]764c0442014-05-01 04:30:55515 new FakeGCMInternalsBuilder(clock_step))));
516}
517
zea76342abf2016-11-01 17:26:04518void GCMClientImplTest::FailCheckin(net::HttpStatusCode response_code) {
519 std::map<std::string, std::string> settings;
520 CompleteCheckinImpl(0, 0, GServicesSettings::CalculateDigest(settings),
521 settings, response_code);
522}
523
[email protected]764c0442014-05-01 04:30:55524void GCMClientImplTest::CompleteCheckin(
avi26062922015-12-26 00:14:18525 uint64_t android_id,
526 uint64_t security_token,
[email protected]764c0442014-05-01 04:30:55527 const std::string& digest,
528 const std::map<std::string, std::string>& settings) {
zea76342abf2016-11-01 17:26:04529 CompleteCheckinImpl(android_id, security_token, digest, settings,
530 net::HTTP_OK);
531}
532
533void GCMClientImplTest::CompleteCheckinImpl(
534 uint64_t android_id,
535 uint64_t security_token,
536 const std::string& digest,
537 const std::map<std::string, std::string>& settings,
538 net::HttpStatusCode response_code) {
[email protected]848b1b62014-01-30 23:51:04539 checkin_proto::AndroidCheckinResponse response;
540 response.set_stats_ok(true);
541 response.set_android_id(android_id);
542 response.set_security_token(security_token);
543
[email protected]764c0442014-05-01 04:30:55544 // For testing G-services settings.
545 if (!digest.empty()) {
546 response.set_digest(digest);
jdoerrie3feb1852018-10-05 12:16:44547 for (auto it = settings.begin(); it != settings.end(); ++it) {
[email protected]764c0442014-05-01 04:30:55548 checkin_proto::GservicesSetting* setting = response.add_setting();
549 setting->set_name(it->first);
550 setting->set_value(it->second);
551 }
[email protected]aae544d72014-05-20 06:53:10552 response.set_settings_diff(false);
[email protected]764c0442014-05-01 04:30:55553 }
554
[email protected]848b1b62014-01-30 23:51:04555 std::string response_string;
556 response.SerializeToString(&response_string);
557
Maks Orlovich278f2572018-07-16 18:18:41558 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
559 gservices_settings().GetCheckinURL(),
560 network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:27561 network::CreateURLResponseHead(response_code), response_string));
jianlid7e80f22015-06-18 22:21:31562 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
563 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04564}
565
566void GCMClientImplTest::CompleteRegistration(
567 const std::string& registration_id) {
568 std::string response(kRegistrationResponsePrefix);
569 response.append(registration_id);
Maks Orlovichc70c93c2018-07-12 02:45:44570
571 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
572 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:27573 network::CreateURLResponseHead(net::HTTP_OK), response));
Maks Orlovichc70c93c2018-07-12 02:45:44574
jianlid7e80f22015-06-18 22:21:31575 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
576 PumpLoopUntilIdle();
[email protected]e4007042014-02-15 20:34:28577}
578
579void GCMClientImplTest::CompleteUnregistration(
580 const std::string& app_id) {
581 std::string response(kUnregistrationResponsePrefix);
582 response.append(app_id);
Maks Orlovich278f2572018-07-16 18:18:41583
584 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
585 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:27586 network::CreateURLResponseHead(net::HTTP_OK), response));
Maks Orlovich278f2572018-07-16 18:18:41587
jianlid7e80f22015-06-18 22:21:31588 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
589 PumpLoopUntilIdle();
fgorskie45a34f2014-10-08 17:37:46590}
591
[email protected]3a20a4d2014-03-21 22:54:21592bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const {
jianli7a0c9b62015-05-26 23:24:47593 return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id);
[email protected]3a20a4d2014-03-21 22:54:21594}
595
596void GCMClientImplTest::AddRegistration(
597 const std::string& app_id,
598 const std::vector<std::string>& sender_ids,
599 const std::string& registration_id) {
Rayan Kanso3da434f2018-12-19 17:09:39600 auto registration = base::MakeRefCounted<GCMRegistrationInfo>();
jianli7a0c9b62015-05-26 23:24:47601 registration->app_id = app_id;
[email protected]3a20a4d2014-03-21 22:54:21602 registration->sender_ids = sender_ids;
Rayan Kanso3da434f2018-12-19 17:09:39603 gcm_client_->registrations_.emplace(std::move(registration), registration_id);
[email protected]848b1b62014-01-30 23:51:04604}
605
606void GCMClientImplTest::InitializeGCMClient() {
[email protected]848b1b62014-01-30 23:51:04607 clock()->Advance(base::TimeDelta::FromMilliseconds(1));
[email protected]2bbe0a682014-03-26 00:08:31608
[email protected]848b1b62014-01-30 23:51:04609 // Actual initialization.
[email protected]8ad80512014-05-23 09:40:47610 GCMClient::ChromeBuildInfo chrome_build_info;
jianlic02d25e2015-05-27 22:24:31611 chrome_build_info.version = kChromeVersion;
johnme627dc8c72016-08-19 21:49:39612 chrome_build_info.product_category_for_subtypes = kProductCategoryForSubtypes;
Maks Orlovichc70c93c2018-07-12 02:45:44613 gcm_client_->Initialize(
Hajime Hoshi7a5c8c82019-01-10 05:44:51614 chrome_build_info, gcm_store_path(),
Gabriel Charettedfa36042019-08-19 17:30:11615 task_environment_.GetMainThreadTaskRunner(),
Steven Zhub36394c2019-06-04 16:10:46616 base::ThreadTaskRunnerHandle::Get(), base::DoNothing(),
Maks Orlovichc70c93c2018-07-12 02:45:44617 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
618 &test_url_loader_factory_),
Robbie McElrathb01499332018-09-25 00:53:13619 network::TestNetworkConnectionTracker::GetInstance(),
Maks Orlovichc70c93c2018-07-12 02:45:44620 base::WrapUnique<Encryptor>(new FakeEncryptor), this);
[email protected]1abdf202014-06-13 19:38:53621}
[email protected]d3a4b2e2014-02-27 13:46:54622
[email protected]1abdf202014-06-13 19:38:53623void GCMClientImplTest::StartGCMClient() {
[email protected]2bbe0a682014-03-26 00:08:31624 // Start loading and check-in.
jianlif3e52af42015-01-21 23:18:47625 gcm_client_->Start(GCMClient::IMMEDIATE_START);
[email protected]d3a4b2e2014-02-27 13:46:54626
[email protected]848b1b62014-01-30 23:51:04627 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04628}
629
jianli7a0c9b62015-05-26 23:24:47630void GCMClientImplTest::Register(const std::string& app_id,
631 const std::vector<std::string>& senders) {
Rayan Kanso3da434f2018-12-19 17:09:39632 auto gcm_info = base::MakeRefCounted<GCMRegistrationInfo>();
jianli7a0c9b62015-05-26 23:24:47633 gcm_info->app_id = app_id;
634 gcm_info->sender_ids = senders;
Rayan Kanso3da434f2018-12-19 17:09:39635 gcm_client()->Register(std::move(gcm_info));
jianli7a0c9b62015-05-26 23:24:47636}
637
638void GCMClientImplTest::Unregister(const std::string& app_id) {
Rayan Kanso3da434f2018-12-19 17:09:39639 auto gcm_info = base::MakeRefCounted<GCMRegistrationInfo>();
jianli7a0c9b62015-05-26 23:24:47640 gcm_info->app_id = app_id;
Rayan Kanso3da434f2018-12-19 17:09:39641 gcm_client()->Unregister(std::move(gcm_info));
jianli7a0c9b62015-05-26 23:24:47642}
643
[email protected]848b1b62014-01-30 23:51:04644void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
[email protected]dd47c4ce2014-08-05 23:38:50645 gcm_client_->recorder_.RecordConnectionInitiated(std::string());
646 gcm_client_->recorder_.RecordConnectionSuccess();
[email protected]848b1b62014-01-30 23:51:04647 gcm_client_->OnMessageReceivedFromMCS(message);
648}
649
[email protected]292af2b22014-08-06 19:42:45650void GCMClientImplTest::ReceiveOnMessageSentToMCS(
651 const std::string& app_id,
652 const std::string& message_id,
653 const MCSClient::MessageSendStatus status) {
654 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status);
655}
656
fgorskid578c18b2014-09-24 23:40:17657void GCMClientImplTest::OnGCMReady(
fgorski5df101702014-10-28 02:09:31658 const std::vector<AccountMapping>& account_mappings,
659 const base::Time& last_token_fetch_time) {
[email protected]848b1b62014-01-30 23:51:04660 last_event_ = LOADING_COMPLETED;
fgorski5df101702014-10-28 02:09:31661 last_account_mappings_ = account_mappings;
662 last_token_fetch_time_ = last_token_fetch_time;
[email protected]848b1b62014-01-30 23:51:04663}
664
mvanouwerkerkf8633deb2015-07-13 11:04:06665void GCMClientImplTest::OnMessageReceived(const std::string& registration_id,
666 const IncomingMessage& message) {
[email protected]848b1b62014-01-30 23:51:04667 last_event_ = MESSAGE_RECEIVED;
668 last_app_id_ = registration_id;
669 last_message_ = message;
[email protected]848b1b62014-01-30 23:51:04670}
671
jianli7a0c9b62015-05-26 23:24:47672void GCMClientImplTest::OnRegisterFinished(
Rayan Kanso3da434f2018-12-19 17:09:39673 scoped_refptr<RegistrationInfo> registration_info,
jianli7a0c9b62015-05-26 23:24:47674 const std::string& registration_id,
675 GCMClient::Result result) {
[email protected]848b1b62014-01-30 23:51:04676 last_event_ = REGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47677 last_app_id_ = registration_info->app_id;
[email protected]848b1b62014-01-30 23:51:04678 last_registration_id_ = registration_id;
679 last_result_ = result;
680}
681
jianli7a0c9b62015-05-26 23:24:47682void GCMClientImplTest::OnUnregisterFinished(
Rayan Kanso3da434f2018-12-19 17:09:39683 scoped_refptr<RegistrationInfo> registration_info,
jianli7a0c9b62015-05-26 23:24:47684 GCMClient::Result result) {
[email protected]e4007042014-02-15 20:34:28685 last_event_ = UNREGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47686 last_app_id_ = registration_info->app_id;
[email protected]0e88e1d12014-03-19 06:53:08687 last_result_ = result;
[email protected]e4007042014-02-15 20:34:28688}
689
[email protected]848b1b62014-01-30 23:51:04690void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
691 last_event_ = MESSAGES_DELETED;
692 last_app_id_ = app_id;
693}
694
[email protected]c6fe36b2014-03-11 10:58:12695void GCMClientImplTest::OnMessageSendError(
696 const std::string& app_id,
697 const gcm::GCMClient::SendErrorDetails& send_error_details) {
[email protected]848b1b62014-01-30 23:51:04698 last_event_ = MESSAGE_SEND_ERROR;
699 last_app_id_ = app_id;
[email protected]c6fe36b2014-03-11 10:58:12700 last_error_details_ = send_error_details;
[email protected]848b1b62014-01-30 23:51:04701}
702
[email protected]292af2b22014-08-06 19:42:45703void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id,
704 const std::string& message_id) {
705 last_event_ = MESSAGE_SEND_ACK;
706 last_app_id_ = app_id;
707 last_message_id_ = message_id;
708}
709
avi26062922015-12-26 00:14:18710int64_t GCMClientImplTest::CurrentTime() {
[email protected]848b1b62014-01-30 23:51:04711 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
712}
713
[email protected]848b1b62014-01-30 23:51:04714TEST_F(GCMClientImplTest, LoadingCompleted) {
715 EXPECT_EQ(LOADING_COMPLETED, last_event());
716 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
717 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
[email protected]7df5ef22014-07-17 07:35:58718
719 // Checking freshly loaded CheckinInfo.
720 EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id);
721 EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret);
722 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
723 EXPECT_TRUE(device_checkin_info().accounts_set);
724 EXPECT_TRUE(device_checkin_info().account_tokens.empty());
[email protected]848b1b62014-01-30 23:51:04725}
726
jianli00b4600f2015-02-10 23:32:49727TEST_F(GCMClientImplTest, LoadingBusted) {
728 // Close the GCM store.
729 gcm_client()->Stop();
730 PumpLoopUntilIdle();
731
732 // Mess up the store.
Chris Mumforda884aa1f2017-10-24 22:46:32733 EXPECT_TRUE(leveldb_chrome::CorruptClosedDBForTesting(gcm_store_path()));
jianli00b4600f2015-02-10 23:32:49734
735 // Restart GCM client. The store should be reset and the loading should
736 // complete successfully.
737 reset_last_event();
738 BuildGCMClient(base::TimeDelta());
739 InitializeGCMClient();
740 StartGCMClient();
johnme0fb2fc02016-07-14 14:47:44741 ASSERT_NO_FATAL_FAILURE(
742 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(),
743 std::map<std::string, std::string>()));
jianli00b4600f2015-02-10 23:32:49744
745 EXPECT_EQ(LOADING_COMPLETED, last_event());
746 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
747 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
748}
749
johnme6d6c7802016-09-29 22:27:16750TEST_F(GCMClientImplTest, LoadingWithEmptyDirectory) {
751 // Close the GCM store.
752 gcm_client()->Stop();
753 PumpLoopUntilIdle();
754
755 // Make the store directory empty, to simulate a previous destroy store
756 // operation failing to delete the store directory.
757 ASSERT_TRUE(base::DeleteFile(gcm_store_path(), true /* recursive */));
758 ASSERT_TRUE(base::CreateDirectory(gcm_store_path()));
759
760 base::HistogramTester histogram_tester;
761
762 // Restart GCM client. The store should be considered to not exist.
763 BuildGCMClient(base::TimeDelta());
764 InitializeGCMClient();
765 gcm_client()->Start(GCMClient::DELAYED_START);
766 PumpLoopUntilIdle();
767 histogram_tester.ExpectUniqueSample("GCM.LoadStatus",
768 13 /* STORE_DOES_NOT_EXIST */, 1);
769 // Since the store does not exist, the database should not have been opened.
770 histogram_tester.ExpectTotalCount("GCM.Database.Open", 0);
771 // Without a store, DELAYED_START loading should only reach INITIALIZED state.
772 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
773
774 // The store directory should still exist (and be empty). If not, then the
775 // DELAYED_START load has probably reset the store, rather than leaving that
776 // to the next IMMEDIATE_START load as expected.
777 ASSERT_TRUE(base::DirectoryExists(gcm_store_path()));
778 ASSERT_FALSE(
779 base::PathExists(gcm_store_path().Append(FILE_PATH_LITERAL("CURRENT"))));
780
781 // IMMEDIATE_START loading should successfully create a new store despite the
782 // empty directory.
783 reset_last_event();
784 StartGCMClient();
785 ASSERT_NO_FATAL_FAILURE(
786 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2, std::string(),
787 std::map<std::string, std::string>()));
788 EXPECT_EQ(LOADING_COMPLETED, last_event());
789 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
790 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
791 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
792}
793
jianli78b56042015-06-17 01:21:22794TEST_F(GCMClientImplTest, DestroyStoreWhenNotNeeded) {
795 // Close the GCM store.
796 gcm_client()->Stop();
797 PumpLoopUntilIdle();
798
799 // Restart GCM client. The store is loaded successfully.
800 reset_last_event();
801 BuildGCMClient(base::TimeDelta());
802 InitializeGCMClient();
803 gcm_client()->Start(GCMClient::DELAYED_START);
804 PumpLoopUntilIdle();
805
806 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
807 EXPECT_TRUE(device_checkin_info().android_id);
808 EXPECT_TRUE(device_checkin_info().secret);
809
810 // Fast forward the clock to trigger the store destroying logic.
Hajime Hoshi7a5c8c82019-01-10 05:44:51811 FastForwardBy(base::TimeDelta::FromMilliseconds(300000));
jianli78b56042015-06-17 01:21:22812 PumpLoopUntilIdle();
813
814 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
815 EXPECT_FALSE(device_checkin_info().android_id);
816 EXPECT_FALSE(device_checkin_info().secret);
817}
818
Mugdha Lakhani2ade6a02018-01-31 14:31:11819TEST_F(GCMClientImplTest, SerializeAndDeserialize) {
820 std::vector<std::string> senders{"sender"};
Rayan Kanso3da434f2018-12-19 17:09:39821 auto gcm_info = base::MakeRefCounted<GCMRegistrationInfo>();
Mugdha Lakhani2ade6a02018-01-31 14:31:11822 gcm_info->app_id = kExtensionAppId;
823 gcm_info->sender_ids = senders;
824 gcm_info->last_validated = clock()->Now();
825
Rayan Kanso3da434f2018-12-19 17:09:39826 auto gcm_info_deserialized = base::MakeRefCounted<GCMRegistrationInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02827 std::string gcm_registration_id_deserialized;
828 {
829 std::string serialized_key = gcm_info->GetSerializedKey();
830 std::string serialized_value =
831 gcm_info->GetSerializedValue(kRegistrationId);
832
833 ASSERT_TRUE(gcm_info_deserialized->Deserialize(
834 serialized_key, serialized_value, &gcm_registration_id_deserialized));
835 }
Mugdha Lakhani2ade6a02018-01-31 14:31:11836
837 EXPECT_EQ(gcm_info->app_id, gcm_info_deserialized->app_id);
838 EXPECT_EQ(gcm_info->sender_ids, gcm_info_deserialized->sender_ids);
839 EXPECT_EQ(gcm_info->last_validated, gcm_info_deserialized->last_validated);
Peter Beverloo1b76cbea2018-02-23 16:23:02840 EXPECT_EQ(kRegistrationId, gcm_registration_id_deserialized);
841
Rayan Kanso3da434f2018-12-19 17:09:39842 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02843 instance_id_info->app_id = kExtensionAppId;
844 instance_id_info->last_validated = clock()->Now();
845 instance_id_info->authorized_entity = "different_sender";
846 instance_id_info->scope = "scope";
847
Rayan Kanso3da434f2018-12-19 17:09:39848 auto instance_id_info_deserialized =
849 base::MakeRefCounted<InstanceIDTokenInfo>();
Peter Beverloo1b76cbea2018-02-23 16:23:02850 std::string instance_id_registration_id_deserialized;
851 {
852 std::string serialized_key = instance_id_info->GetSerializedKey();
853 std::string serialized_value =
854 instance_id_info->GetSerializedValue(kRegistrationId);
855
856 ASSERT_TRUE(instance_id_info_deserialized->Deserialize(
857 serialized_key, serialized_value,
858 &instance_id_registration_id_deserialized));
859 }
860
861 EXPECT_EQ(instance_id_info->app_id, instance_id_info_deserialized->app_id);
862 EXPECT_EQ(instance_id_info->last_validated,
863 instance_id_info_deserialized->last_validated);
864 EXPECT_EQ(instance_id_info->authorized_entity,
865 instance_id_info_deserialized->authorized_entity);
866 EXPECT_EQ(instance_id_info->scope, instance_id_info_deserialized->scope);
867 EXPECT_EQ(kRegistrationId, instance_id_registration_id_deserialized);
Mugdha Lakhani2ade6a02018-01-31 14:31:11868}
869
[email protected]848b1b62014-01-30 23:51:04870TEST_F(GCMClientImplTest, RegisterApp) {
johnme627dc8c72016-08-19 21:49:39871 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21872
[email protected]848b1b62014-01-30 23:51:04873 std::vector<std::string> senders;
874 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39875 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44876 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
[email protected]848b1b62014-01-30 23:51:04877
878 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39879 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04880 EXPECT_EQ("reg_id", last_registration_id());
881 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39882 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21883}
884
Peter Beverloo71568a442018-03-01 16:07:41885TEST_F(GCMClientImplTest, RegisterAppFromCache) {
johnme627dc8c72016-08-19 21:49:39886 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21887
888 std::vector<std::string> senders;
889 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39890 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44891 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
johnme627dc8c72016-08-19 21:49:39892 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:21893
johnme627dc8c72016-08-19 21:49:39894 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]3a20a4d2014-03-21 22:54:21895 EXPECT_EQ("reg_id", last_registration_id());
896 EXPECT_EQ(GCMClient::SUCCESS, last_result());
897 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
898
899 // Recreate GCMClient in order to load from the persistent store.
[email protected]764c0442014-05-01 04:30:55900 BuildGCMClient(base::TimeDelta());
[email protected]3a20a4d2014-03-21 22:54:21901 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53902 StartGCMClient();
[email protected]3a20a4d2014-03-21 22:54:21903
johnme627dc8c72016-08-19 21:49:39904 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]848b1b62014-01-30 23:51:04905}
906
jianli055634b2015-06-29 21:32:12907TEST_F(GCMClientImplTest, RegisterPreviousSenderAgain) {
johnme627dc8c72016-08-19 21:49:39908 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12909
910 // Register a sender.
911 std::vector<std::string> senders;
912 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:39913 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:44914 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianli055634b2015-06-29 21:32:12915
916 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39917 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12918 EXPECT_EQ("reg_id", last_registration_id());
919 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39920 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12921
922 reset_last_event();
923
924 // Register a different sender. Different registration ID from previous one
925 // should be returned.
926 std::vector<std::string> senders2;
927 senders2.push_back("sender2");
johnme627dc8c72016-08-19 21:49:39928 Register(kExtensionAppId, senders2);
johnme0fb2fc02016-07-14 14:47:44929 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2"));
jianli055634b2015-06-29 21:32:12930
931 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39932 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12933 EXPECT_EQ("reg_id2", last_registration_id());
934 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39935 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12936
937 reset_last_event();
938
939 // Register the 1st sender again. Different registration ID from previous one
940 // should be returned.
941 std::vector<std::string> senders3;
942 senders3.push_back("sender");
johnme627dc8c72016-08-19 21:49:39943 Register(kExtensionAppId, senders3);
johnme0fb2fc02016-07-14 14:47:44944 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianli055634b2015-06-29 21:32:12945
946 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:39947 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli055634b2015-06-29 21:32:12948 EXPECT_EQ("reg_id", last_registration_id());
949 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:39950 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
jianli055634b2015-06-29 21:32:12951}
952
[email protected]617b22e92019-09-30 20:29:37953TEST_F(GCMClientImplTest, DISABLED_RegisterAgainWhenTokenIsFresh) {
Mugdha Lakhanic3d792e2018-02-13 15:10:13954 // Register a sender.
955 std::vector<std::string> senders;
956 senders.push_back("sender");
957 Register(kExtensionAppId, senders);
958 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
959 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
960 EXPECT_EQ(kExtensionAppId, last_app_id());
961 EXPECT_EQ("reg_id", last_registration_id());
962 EXPECT_EQ(GCMClient::SUCCESS, last_result());
963 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
964
965 reset_last_event();
966
967 // Advance time by (kTestTokenInvalidationPeriod)/2
968 clock()->Advance(base::TimeDelta::FromDays(kTestTokenInvalidationPeriod / 2));
969
970 // Register the same sender again. The same registration ID as the
971 // previous one should be returned, and we should *not* send a
972 // registration request to the GCM server.
973 Register(kExtensionAppId, senders);
974 PumpLoopUntilIdle();
975
976 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
977 EXPECT_EQ(kExtensionAppId, last_app_id());
978 EXPECT_EQ("reg_id", last_registration_id());
979 EXPECT_EQ(GCMClient::SUCCESS, last_result());
980 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
981}
982
983TEST_F(GCMClientImplTest, RegisterAgainWhenTokenIsStale) {
984 // Register a sender.
985 std::vector<std::string> senders;
986 senders.push_back("sender");
987 Register(kExtensionAppId, senders);
988 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
989
990 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
991 EXPECT_EQ(kExtensionAppId, last_app_id());
992 EXPECT_EQ("reg_id", last_registration_id());
993 EXPECT_EQ(GCMClient::SUCCESS, last_result());
994 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
995
996 reset_last_event();
997
998 // Advance time by kTestTokenInvalidationPeriod
999 clock()->Advance(base::TimeDelta::FromDays(kTestTokenInvalidationPeriod));
1000
1001 // Register the same sender again. Different registration ID from the
1002 // previous one should be returned.
1003 Register(kExtensionAppId, senders);
1004 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id2"));
1005
1006 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1007 EXPECT_EQ(kExtensionAppId, last_app_id());
1008 EXPECT_EQ("reg_id2", last_registration_id());
1009 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1010 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
1011}
1012
[email protected]e4007042014-02-15 20:34:281013TEST_F(GCMClientImplTest, UnregisterApp) {
johnme627dc8c72016-08-19 21:49:391014 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:211015
1016 std::vector<std::string> senders;
1017 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391018 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:441019 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
johnme627dc8c72016-08-19 21:49:391020 EXPECT_TRUE(ExistsRegistration(kExtensionAppId));
[email protected]3a20a4d2014-03-21 22:54:211021
johnme627dc8c72016-08-19 21:49:391022 Unregister(kExtensionAppId);
1023 ASSERT_NO_FATAL_FAILURE(CompleteUnregistration(kExtensionAppId));
[email protected]e4007042014-02-15 20:34:281024
1025 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391026 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]e4007042014-02-15 20:34:281027 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391028 EXPECT_FALSE(ExistsRegistration(kExtensionAppId));
[email protected]e4007042014-02-15 20:34:281029}
1030
fgorskie45a34f2014-10-08 17:37:461031// Tests that stopping the GCMClient also deletes pending registration requests.
1032// This is tested by checking that url fetcher contained in the request was
1033// deleted.
1034TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) {
1035 std::vector<std::string> senders;
1036 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391037 Register(kExtensionAppId, senders);
fgorskie45a34f2014-10-08 17:37:461038
1039 gcm_client()->Stop();
jianlid7e80f22015-06-18 22:21:311040 PumpLoopUntilIdle();
Maks Orlovichc70c93c2018-07-12 02:45:441041 EXPECT_EQ(0, url_loader_factory()->NumPending());
fgorskie45a34f2014-10-08 17:37:461042}
1043
[email protected]848b1b62014-01-30 23:51:041044TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
[email protected]3a20a4d2014-03-21 22:54:211045 // Register to receive messages from kSender and kSender2 only.
1046 std::vector<std::string> senders;
1047 senders.push_back(kSender);
1048 senders.push_back(kSender2);
johnme627dc8c72016-08-19 21:49:391049 AddRegistration(kExtensionAppId, senders, "reg_id");
[email protected]3a20a4d2014-03-21 22:54:211050
[email protected]848b1b62014-01-30 23:51:041051 std::map<std::string, std::string> expected_data;
1052 expected_data["message_type"] = "gcm";
1053 expected_data["key"] = "value";
1054 expected_data["key2"] = "value2";
[email protected]3a20a4d2014-03-21 22:54:211055
1056 // Message for kSender will be received.
johnme627dc8c72016-08-19 21:49:391057 MCSMessage message(BuildDownstreamMessage(
1058 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1059 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041060 EXPECT_TRUE(message.IsValid());
1061 ReceiveMessageFromMCS(message);
1062
1063 expected_data.erase(expected_data.find("message_type"));
1064 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391065 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:041066 EXPECT_EQ(expected_data.size(), last_message().data.size());
1067 EXPECT_EQ(expected_data, last_message().data);
[email protected]3a20a4d2014-03-21 22:54:211068 EXPECT_EQ(kSender, last_message().sender_id);
1069
1070 reset_last_event();
1071
1072 // Message for kSender2 will be received.
johnme627dc8c72016-08-19 21:49:391073 MCSMessage message2(BuildDownstreamMessage(
1074 kSender2, kExtensionAppId, std::string() /* subtype */, expected_data,
1075 std::string() /* raw_data */));
[email protected]3a20a4d2014-03-21 22:54:211076 EXPECT_TRUE(message2.IsValid());
1077 ReceiveMessageFromMCS(message2);
1078
1079 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391080 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]3a20a4d2014-03-21 22:54:211081 EXPECT_EQ(expected_data.size(), last_message().data.size());
1082 EXPECT_EQ(expected_data, last_message().data);
1083 EXPECT_EQ(kSender2, last_message().sender_id);
[email protected]848b1b62014-01-30 23:51:041084}
1085
petera5aedc52015-07-22 10:47:301086TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) {
1087 std::vector<std::string> senders(1, kSender);
johnme627dc8c72016-08-19 21:49:391088 AddRegistration(kExtensionAppId, senders, "reg_id");
petera5aedc52015-07-22 10:47:301089
1090 std::map<std::string, std::string> expected_data;
1091
johnme627dc8c72016-08-19 21:49:391092 MCSMessage message(BuildDownstreamMessage(kSender, kExtensionAppId,
1093 std::string() /* subtype */,
1094 expected_data, kRawData));
petera5aedc52015-07-22 10:47:301095 EXPECT_TRUE(message.IsValid());
1096 ReceiveMessageFromMCS(message);
1097
1098 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
johnme627dc8c72016-08-19 21:49:391099 EXPECT_EQ(kExtensionAppId, last_app_id());
petera5aedc52015-07-22 10:47:301100 EXPECT_EQ(expected_data.size(), last_message().data.size());
1101 EXPECT_EQ(kSender, last_message().sender_id);
1102 EXPECT_EQ(kRawData, last_message().raw_data);
1103}
1104
[email protected]617b22e92019-09-30 20:29:371105TEST_F(GCMClientImplTest, DISABLED_DispatchDownstreamMessageSendError) {
Rayan Kansobcec0292019-05-09 13:27:511106 std::map<std::string, std::string> expected_data = {
1107 {"message_type", "send_error"}, {"error_details", "some details"}};
1108
johnme627dc8c72016-08-19 21:49:391109 MCSMessage message(BuildDownstreamMessage(
1110 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1111 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041112 EXPECT_TRUE(message.IsValid());
1113 ReceiveMessageFromMCS(message);
1114
1115 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
johnme627dc8c72016-08-19 21:49:391116 EXPECT_EQ(kExtensionAppId, last_app_id());
Rayan Kansobcec0292019-05-09 13:27:511117 EXPECT_EQ(kMessageId, last_error_details().message_id);
[email protected]c6fe36b2014-03-11 10:58:121118 EXPECT_EQ(1UL, last_error_details().additional_data.size());
jdoerrie3feb1852018-10-05 12:16:441119 auto iter = last_error_details().additional_data.find("error_details");
[email protected]c6fe36b2014-03-11 10:58:121120 EXPECT_TRUE(iter != last_error_details().additional_data.end());
1121 EXPECT_EQ("some details", iter->second);
[email protected]848b1b62014-01-30 23:51:041122}
1123
1124TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
1125 std::map<std::string, std::string> expected_data;
1126 expected_data["message_type"] = "deleted_messages";
johnme627dc8c72016-08-19 21:49:391127 MCSMessage message(BuildDownstreamMessage(
1128 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1129 std::string() /* raw_data */));
[email protected]848b1b62014-01-30 23:51:041130 EXPECT_TRUE(message.IsValid());
1131 ReceiveMessageFromMCS(message);
1132
1133 EXPECT_EQ(MESSAGES_DELETED, last_event());
johnme627dc8c72016-08-19 21:49:391134 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:041135}
1136
1137TEST_F(GCMClientImplTest, SendMessage) {
mvanouwerkerkf8633deb2015-07-13 11:04:061138 OutgoingMessage message;
[email protected]848b1b62014-01-30 23:51:041139 message.id = "007";
1140 message.time_to_live = 500;
1141 message.data["key"] = "value";
johnme627dc8c72016-08-19 21:49:391142 gcm_client()->Send(kExtensionAppId, kSender, message);
[email protected]848b1b62014-01-30 23:51:041143
1144 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
johnme627dc8c72016-08-19 21:49:391145 EXPECT_EQ(kExtensionAppId,
1146 mcs_client()->last_data_message_stanza().category());
[email protected]3a20a4d2014-03-21 22:54:211147 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:041148 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
1149 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
1150 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
[email protected]848b1b62014-01-30 23:51:041151 EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from());
[email protected]3a20a4d2014-03-21 22:54:211152 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:041153 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
1154 EXPECT_EQ("value",
1155 mcs_client()->last_data_message_stanza().app_data(0).value());
1156}
1157
[email protected]292af2b22014-08-06 19:42:451158TEST_F(GCMClientImplTest, SendMessageAcknowledged) {
johnme627dc8c72016-08-19 21:49:391159 ReceiveOnMessageSentToMCS(kExtensionAppId, "007", MCSClient::SENT);
[email protected]292af2b22014-08-06 19:42:451160 EXPECT_EQ(MESSAGE_SEND_ACK, last_event());
johnme627dc8c72016-08-19 21:49:391161 EXPECT_EQ(kExtensionAppId, last_app_id());
[email protected]292af2b22014-08-06 19:42:451162 EXPECT_EQ("007", last_message_id());
1163}
1164
[email protected]764c0442014-05-01 04:30:551165class GCMClientImplCheckinTest : public GCMClientImplTest {
1166 public:
1167 GCMClientImplCheckinTest();
dcheng30a1b1542014-10-29 21:27:501168 ~GCMClientImplCheckinTest() override;
[email protected]764c0442014-05-01 04:30:551169
dcheng30a1b1542014-10-29 21:27:501170 void SetUp() override;
[email protected]764c0442014-05-01 04:30:551171};
1172
1173GCMClientImplCheckinTest::GCMClientImplCheckinTest() {
1174}
1175
1176GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {
1177}
1178
1179void GCMClientImplCheckinTest::SetUp() {
1180 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:551181 // Creating unique temp directory that will be used by GCMStore shared between
1182 // GCM Client and G-services settings.
1183 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:551184 // Time will be advancing one hour every time it is checked.
1185 BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval));
1186 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:531187 StartGCMClient();
[email protected]764c0442014-05-01 04:30:551188}
1189
1190TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
1191 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031192 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]764c0442014-05-01 04:30:551193 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131194 settings["gcm_hostname"] = "alternative.gcm.host";
1195 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:551196 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441197 ASSERT_NO_FATAL_FAILURE(
1198 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1199 GServicesSettings::CalculateDigest(settings), settings));
[email protected]f09354512014-05-02 00:51:131200 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:101201 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:341202 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:101203 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:341204 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:101205 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:131206 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:101207 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:131208 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:101209 gservices_settings().GetMCSFallbackEndpoint());
[email protected]764c0442014-05-01 04:30:551210}
1211
1212// This test only checks that periodic checkin happens.
1213TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
1214 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031215 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]764c0442014-05-01 04:30:551216 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131217 settings["gcm_hostname"] = "alternative.gcm.host";
1218 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:551219 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441220 ASSERT_NO_FATAL_FAILURE(
1221 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1222 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581223
[email protected]764c0442014-05-01 04:30:551224 EXPECT_EQ(2, clock()->call_count());
1225
1226 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441227 ASSERT_NO_FATAL_FAILURE(
1228 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1229 GServicesSettings::CalculateDigest(settings), settings));
[email protected]764c0442014-05-01 04:30:551230}
1231
[email protected]06e45272014-05-06 03:41:341232TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) {
1233 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031234 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]06e45272014-05-06 03:41:341235 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:131236 settings["gcm_hostname"] = "alternative.gcm.host";
1237 settings["gcm_secure_port"] = "7777";
[email protected]06e45272014-05-06 03:41:341238 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441239 ASSERT_NO_FATAL_FAILURE(
1240 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1241 GServicesSettings::CalculateDigest(settings), settings));
[email protected]06e45272014-05-06 03:41:341242
1243 BuildGCMClient(base::TimeDelta());
1244 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:531245 StartGCMClient();
[email protected]06e45272014-05-06 03:41:341246
1247 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:101248 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:341249 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:101250 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:341251 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:101252 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:131253 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:101254 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:131255 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:101256 gservices_settings().GetMCSFallbackEndpoint());
[email protected]06e45272014-05-06 03:41:341257}
1258
[email protected]7df5ef22014-07-17 07:35:581259// This test only checks that periodic checkin happens.
1260TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) {
1261 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031262 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581263 settings["checkin_url"] = "http://alternative.url/checkin";
1264 settings["gcm_hostname"] = "alternative.gcm.host";
1265 settings["gcm_secure_port"] = "7777";
1266 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441267 ASSERT_NO_FATAL_FAILURE(
1268 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1269 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581270
fgorski58b9dfc2014-09-29 16:46:181271 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1272 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1273 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1274 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581275
1276 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
1277 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181278 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1279 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581280
1281 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441282 ASSERT_NO_FATAL_FAILURE(
1283 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1284 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581285
1286 std::set<std::string> accounts;
1287 accounts.insert("[email protected]");
1288 accounts.insert("[email protected]");
1289 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1290 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181291 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1292 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581293}
1294
1295// This test only checks that periodic checkin happens.
1296TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) {
1297 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031298 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581299 settings["checkin_url"] = "http://alternative.url/checkin";
1300 settings["gcm_hostname"] = "alternative.gcm.host";
1301 settings["gcm_secure_port"] = "7777";
1302 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441303 ASSERT_NO_FATAL_FAILURE(
1304 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1305 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581306
fgorski58b9dfc2014-09-29 16:46:181307 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1308 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1309 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1310 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581311 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441312 ASSERT_NO_FATAL_FAILURE(
1313 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1314 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581315
1316 EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size());
1317 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181318 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1319 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581320
fgorski58b9dfc2014-09-29 16:46:181321 account_tokens.erase(account_tokens.begin() + 1);
1322 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581323
1324 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441325 ASSERT_NO_FATAL_FAILURE(
1326 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1327 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581328
1329 std::set<std::string> accounts;
1330 accounts.insert("[email protected]");
1331 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1332 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181333 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1334 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581335}
1336
1337// This test only checks that periodic checkin happens.
1338TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) {
1339 std::map<std::string, std::string> settings;
Raul Tambref88e5102019-02-06 10:54:031340 settings["checkin_interval"] = base::NumberToString(kSettingsCheckinInterval);
[email protected]7df5ef22014-07-17 07:35:581341 settings["checkin_url"] = "http://alternative.url/checkin";
1342 settings["gcm_hostname"] = "alternative.gcm.host";
1343 settings["gcm_secure_port"] = "7777";
1344 settings["gcm_registration_url"] = "http://alternative.url/registration";
johnme0fb2fc02016-07-14 14:47:441345 ASSERT_NO_FATAL_FAILURE(
1346 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1347 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581348
fgorski58b9dfc2014-09-29 16:46:181349 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1350 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1351 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581352
1353 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441354 ASSERT_NO_FATAL_FAILURE(
1355 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1356 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581357
1358 std::set<std::string> accounts;
1359 accounts.insert("[email protected]");
1360 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1361
1362 // This should trigger another checkin, because the list of accounts is
1363 // different.
fgorski58b9dfc2014-09-29 16:46:181364 account_tokens.clear();
1365 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1366 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581367
1368 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441369 ASSERT_NO_FATAL_FAILURE(
1370 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken,
1371 GServicesSettings::CalculateDigest(settings), settings));
[email protected]7df5ef22014-07-17 07:35:581372
1373 accounts.clear();
1374 accounts.insert("[email protected]");
1375 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1376 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181377 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1378 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581379}
1380
zea76342abf2016-11-01 17:26:041381TEST_F(GCMClientImplCheckinTest, ResetStoreWhenCheckinRejected) {
1382 base::HistogramTester histogram_tester;
1383 std::map<std::string, std::string> settings;
1384 ASSERT_NO_FATAL_FAILURE(FailCheckin(net::HTTP_UNAUTHORIZED));
1385 PumpLoopUntilIdle();
1386
1387 // Store should have been destroyed. Restart client and verify the initial
1388 // checkin response is persisted.
1389 BuildGCMClient(base::TimeDelta());
1390 InitializeGCMClient();
1391 StartGCMClient();
1392 ASSERT_NO_FATAL_FAILURE(
1393 CompleteCheckin(kDeviceAndroidId2, kDeviceSecurityToken2,
1394 GServicesSettings::CalculateDigest(settings), settings));
1395
1396 EXPECT_EQ(LOADING_COMPLETED, last_event());
1397 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
1398 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
1399}
1400
[email protected]1abdf202014-06-13 19:38:531401class GCMClientImplStartAndStopTest : public GCMClientImplTest {
jianli78b56042015-06-17 01:21:221402 public:
[email protected]1abdf202014-06-13 19:38:531403 GCMClientImplStartAndStopTest();
dcheng30a1b1542014-10-29 21:27:501404 ~GCMClientImplStartAndStopTest() override;
[email protected]1abdf202014-06-13 19:38:531405
dcheng30a1b1542014-10-29 21:27:501406 void SetUp() override;
fgorski5df101702014-10-28 02:09:311407
1408 void DefaultCompleteCheckin();
[email protected]1abdf202014-06-13 19:38:531409};
1410
1411GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() {
1412}
1413
1414GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() {
1415}
1416
1417void GCMClientImplStartAndStopTest::SetUp() {
1418 testing::Test::SetUp();
1419 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]1abdf202014-06-13 19:38:531420 BuildGCMClient(base::TimeDelta());
1421 InitializeGCMClient();
1422}
1423
fgorski5df101702014-10-28 02:09:311424void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() {
1425 SetUpUrlFetcherFactory();
johnme0fb2fc02016-07-14 14:47:441426 ASSERT_NO_FATAL_FAILURE(
1427 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
1428 std::map<std::string, std::string>()));
fgorski5df101702014-10-28 02:09:311429 PumpLoopUntilIdle();
1430}
1431
[email protected]617b22e92019-09-30 20:29:371432TEST_F(GCMClientImplStartAndStopTest, DISABLED_StartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471433 // GCMClientImpl should be in INITIALIZED state at first.
1434 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1435
1436 // Delay start the GCM.
1437 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531438 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341439 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531440
1441 // Stop the GCM.
1442 gcm_client()->Stop();
1443 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471444 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531445
jianlif3e52af42015-01-21 23:18:471446 // Restart the GCM without delay.
1447 gcm_client()->Start(GCMClient::IMMEDIATE_START);
[email protected]1abdf202014-06-13 19:38:531448 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471449 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531450}
1451
jianli9f9a73672015-06-16 19:39:341452TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) {
jianlif3e52af42015-01-21 23:18:471453 // GCMClientImpl should be in INITIALIZED state at first.
1454 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531455
jianlif3e52af42015-01-21 23:18:471456 // Delay start the GCM and then stop it immediately.
1457 gcm_client()->Start(GCMClient::DELAYED_START);
1458 gcm_client()->Stop();
[email protected]1abdf202014-06-13 19:38:531459 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471460 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341461}
1462
1463TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) {
1464 // GCMClientImpl should be in INITIALIZED state at first.
1465 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471466
1467 // Start the GCM and then stop it immediately.
1468 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1469 gcm_client()->Stop();
1470 PumpLoopUntilIdle();
1471 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531472}
1473
jianli9f9a73672015-06-16 19:39:341474TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471475 // GCMClientImpl should be in INITIALIZED state at first.
1476 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531477
jianlif3e52af42015-01-21 23:18:471478 // Delay start the GCM and then stop and restart it immediately.
1479 gcm_client()->Start(GCMClient::DELAYED_START);
1480 gcm_client()->Stop();
1481 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531482 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341483 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1484}
1485
1486TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) {
1487 // GCMClientImpl should be in INITIALIZED state at first.
1488 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471489
1490 // Start the GCM and then stop and restart it immediately.
1491 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1492 gcm_client()->Stop();
1493 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1494 PumpLoopUntilIdle();
1495 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1496}
1497
jianli9f9a73672015-06-16 19:39:341498TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) {
jianlif3e52af42015-01-21 23:18:471499 // GCMClientImpl should be in INITIALIZED state at first.
1500 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1501
jianli9f9a73672015-06-16 19:39:341502 // Start the GCM immediately and complete the checkin.
1503 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1504 PumpLoopUntilIdle();
1505 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441506 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianli9f9a73672015-06-16 19:39:341507 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1508
1509 // Stop the GCM.
1510 gcm_client()->Stop();
1511 PumpLoopUntilIdle();
1512 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1513
1514 // Start the GCM immediately. GCMClientImpl should be in READY state.
1515 BuildGCMClient(base::TimeDelta());
1516 InitializeGCMClient();
1517 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1518 PumpLoopUntilIdle();
1519 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1520}
1521
1522TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) {
1523 // GCMClientImpl should be in INITIALIZED state at first.
1524 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1525
1526 // Start the GCM immediately and complete the checkin.
1527 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1528 PumpLoopUntilIdle();
1529 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441530 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianli9f9a73672015-06-16 19:39:341531 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1532
1533 // Stop the GCM.
1534 gcm_client()->Stop();
1535 PumpLoopUntilIdle();
1536 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1537
1538 // Delay start the GCM. GCMClientImpl should be in LOADED state.
1539 BuildGCMClient(base::TimeDelta());
1540 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471541 gcm_client()->Start(GCMClient::DELAYED_START);
1542 PumpLoopUntilIdle();
1543 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341544}
1545
[email protected]617b22e92019-09-30 20:29:371546TEST_F(GCMClientImplStartAndStopTest, DISABLED_DelayedStartRace) {
johnme902c4062015-08-21 22:06:171547 // GCMClientImpl should be in INITIALIZED state at first.
1548 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1549
1550 // Delay start the GCM, then start it immediately while it's still loading.
1551 gcm_client()->Start(GCMClient::DELAYED_START);
1552 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1553 PumpLoopUntilIdle();
1554 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441555 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
johnme902c4062015-08-21 22:06:171556 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1557}
1558
jianli9f9a73672015-06-16 19:39:341559TEST_F(GCMClientImplStartAndStopTest, DelayedStart) {
1560 // GCMClientImpl should be in INITIALIZED state at first.
1561 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1562
1563 // Delay start the GCM. The store will not be loaded and GCMClientImpl should
1564 // still be in INITIALIZED state.
1565 gcm_client()->Start(GCMClient::DELAYED_START);
1566 PumpLoopUntilIdle();
1567 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471568
1569 // Start the GCM immediately and complete the checkin.
1570 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1571 PumpLoopUntilIdle();
1572 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
johnme0fb2fc02016-07-14 14:47:441573 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
jianlif3e52af42015-01-21 23:18:471574 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1575
1576 // Registration.
1577 std::vector<std::string> senders;
1578 senders.push_back("sender");
johnme627dc8c72016-08-19 21:49:391579 Register(kExtensionAppId, senders);
johnme0fb2fc02016-07-14 14:47:441580 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("reg_id"));
jianlif3e52af42015-01-21 23:18:471581 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1582
1583 // Stop the GCM.
1584 gcm_client()->Stop();
1585 PumpLoopUntilIdle();
1586 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1587
1588 // Delay start the GCM. GCM is indeed started without delay because the
1589 // registration record has been found.
jianli9f9a73672015-06-16 19:39:341590 BuildGCMClient(base::TimeDelta());
1591 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471592 gcm_client()->Start(GCMClient::DELAYED_START);
1593 PumpLoopUntilIdle();
1594 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531595}
1596
fgorski5df101702014-10-28 02:09:311597// Test for known account mappings and last token fetching time being passed
1598// to OnGCMReady.
1599TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) {
1600 // Start the GCM and wait until it is ready.
jianlif3e52af42015-01-21 23:18:471601 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311602 PumpLoopUntilIdle();
johnme0fb2fc02016-07-14 14:47:441603 ASSERT_NO_FATAL_FAILURE(DefaultCompleteCheckin());
fgorski5df101702014-10-28 02:09:311604
1605 base::Time expected_time = base::Time::Now();
1606 gcm_client()->SetLastTokenFetchTime(expected_time);
1607 AccountMapping expected_mapping;
1608 expected_mapping.account_id = "accId";
1609 expected_mapping.email = "[email protected]";
1610 expected_mapping.status = AccountMapping::MAPPED;
1611 expected_mapping.status_change_timestamp = expected_time;
1612 gcm_client()->UpdateAccountMapping(expected_mapping);
1613 PumpLoopUntilIdle();
1614
1615 // Stop the GCM.
1616 gcm_client()->Stop();
1617 PumpLoopUntilIdle();
1618
1619 // Restart the GCM.
jianlif3e52af42015-01-21 23:18:471620 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311621 PumpLoopUntilIdle();
1622
1623 EXPECT_EQ(LOADING_COMPLETED, last_event());
1624 EXPECT_EQ(expected_time, last_token_fetch_time());
1625 ASSERT_EQ(1UL, last_account_mappings().size());
1626 const AccountMapping& actual_mapping = last_account_mappings()[0];
1627 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id);
1628 EXPECT_EQ(expected_mapping.email, actual_mapping.email);
1629 EXPECT_EQ(expected_mapping.status, actual_mapping.status);
1630 EXPECT_EQ(expected_mapping.status_change_timestamp,
1631 actual_mapping.status_change_timestamp);
1632}
1633
jianlic02d25e2015-05-27 22:24:311634
1635class GCMClientInstanceIDTest : public GCMClientImplTest {
1636 public:
1637 GCMClientInstanceIDTest();
1638 ~GCMClientInstanceIDTest() override;
1639
1640 void AddInstanceID(const std::string& app_id,
1641 const std::string& instance_id);
1642 void RemoveInstanceID(const std::string& app_id);
1643 void GetToken(const std::string& app_id,
1644 const std::string& authorized_entity,
1645 const std::string& scope);
1646 void DeleteToken(const std::string& app_id,
1647 const std::string& authorized_entity,
1648 const std::string& scope);
1649 void CompleteDeleteToken();
1650 bool ExistsToken(const std::string& app_id,
1651 const std::string& authorized_entity,
1652 const std::string& scope) const;
1653};
1654
1655GCMClientInstanceIDTest::GCMClientInstanceIDTest() {
1656}
1657
1658GCMClientInstanceIDTest::~GCMClientInstanceIDTest() {
1659}
1660
1661void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id,
1662 const std::string& instance_id) {
1663 gcm_client()->AddInstanceIDData(app_id, instance_id, "123");
1664}
1665
1666void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) {
1667 gcm_client()->RemoveInstanceIDData(app_id);
1668}
1669
1670void GCMClientInstanceIDTest::GetToken(const std::string& app_id,
1671 const std::string& authorized_entity,
1672 const std::string& scope) {
Rayan Kanso3da434f2018-12-19 17:09:391673 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
jianlic02d25e2015-05-27 22:24:311674 instance_id_info->app_id = app_id;
1675 instance_id_info->authorized_entity = authorized_entity;
1676 instance_id_info->scope = scope;
Rayan Kanso3da434f2018-12-19 17:09:391677 gcm_client()->Register(std::move(instance_id_info));
jianlic02d25e2015-05-27 22:24:311678}
1679
1680void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id,
1681 const std::string& authorized_entity,
1682 const std::string& scope) {
Rayan Kanso3da434f2018-12-19 17:09:391683 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
jianlic02d25e2015-05-27 22:24:311684 instance_id_info->app_id = app_id;
1685 instance_id_info->authorized_entity = authorized_entity;
1686 instance_id_info->scope = scope;
Rayan Kanso3da434f2018-12-19 17:09:391687 gcm_client()->Unregister(std::move(instance_id_info));
jianlic02d25e2015-05-27 22:24:311688}
1689
1690void GCMClientInstanceIDTest::CompleteDeleteToken() {
1691 std::string response(kDeleteTokenResponse);
Maks Orlovich278f2572018-07-16 18:18:411692
1693 EXPECT_TRUE(url_loader_factory()->SimulateResponseForPendingRequest(
1694 GURL(kRegisterUrl), network::URLLoaderCompletionStatus(net::OK),
Lucas Furukawa Gadani06902602019-09-27 20:44:271695 network::CreateURLResponseHead(net::HTTP_OK), response));
Maks Orlovich278f2572018-07-16 18:18:411696
jianlid7e80f22015-06-18 22:21:311697 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
1698 PumpLoopUntilIdle();
jianlic02d25e2015-05-27 22:24:311699}
1700
1701bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id,
1702 const std::string& authorized_entity,
1703 const std::string& scope) const {
Rayan Kanso3da434f2018-12-19 17:09:391704 auto instance_id_info = base::MakeRefCounted<InstanceIDTokenInfo>();
jianlic02d25e2015-05-27 22:24:311705 instance_id_info->app_id = app_id;
1706 instance_id_info->authorized_entity = authorized_entity;
1707 instance_id_info->scope = scope;
Rayan Kanso3da434f2018-12-19 17:09:391708 return gcm_client()->registrations_.count(std::move(instance_id_info)) > 0;
jianlic02d25e2015-05-27 22:24:311709}
1710
1711TEST_F(GCMClientInstanceIDTest, GetToken) {
johnme627dc8c72016-08-19 21:49:391712 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311713
1714 // Get a token.
johnme627dc8c72016-08-19 21:49:391715 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1716 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441717 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311718
1719 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391720 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311721 EXPECT_EQ("token1", last_registration_id());
1722 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391723 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311724
1725 // Get another token.
johnme627dc8c72016-08-19 21:49:391726 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1727 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441728 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311729
1730 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391731 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311732 EXPECT_EQ("token2", last_registration_id());
1733 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391734 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311735 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391736 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
1737}
1738
1739// Most tests in this file use kExtensionAppId which is special-cased by
1740// InstanceIDUsesSubtypeForAppId in gcm_client_impl.cc. This test uses
1741// kSubtypeAppId to cover the alternate case.
1742TEST_F(GCMClientInstanceIDTest, GetTokenWithSubtype) {
1743 ASSERT_EQ(GCMClientImpl::READY, gcm_client_state());
1744
1745 AddInstanceID(kSubtypeAppId, kInstanceID);
1746
1747 EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope));
1748
1749 // Get a token.
1750 GetToken(kSubtypeAppId, kSender, kScope);
1751 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1752 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1753 EXPECT_EQ(kSubtypeAppId, last_app_id());
1754 EXPECT_EQ("token1", last_registration_id());
1755 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1756 EXPECT_TRUE(ExistsToken(kSubtypeAppId, kSender, kScope));
1757
1758 // Delete the token.
1759 DeleteToken(kSubtypeAppId, kSender, kScope);
1760 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
1761 EXPECT_FALSE(ExistsToken(kSubtypeAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311762}
1763
jianli70715cc2015-06-23 21:46:481764TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) {
johnme627dc8c72016-08-19 21:49:391765 AddInstanceID(kExtensionAppId, kInstanceID);
jianli70715cc2015-06-23 21:46:481766
1767 // Delete an invalid token.
johnme627dc8c72016-08-19 21:49:391768 DeleteToken(kExtensionAppId, "Foo@#$", kScope);
jianli70715cc2015-06-23 21:46:481769 PumpLoopUntilIdle();
1770
1771 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391772 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481773 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1774
1775 reset_last_event();
1776
1777 // Delete a non-existing token.
johnme627dc8c72016-08-19 21:49:391778 DeleteToken(kExtensionAppId, kSender, kScope);
jianli70715cc2015-06-23 21:46:481779 PumpLoopUntilIdle();
1780
1781 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391782 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481783 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1784}
1785
jianlic02d25e2015-05-27 22:24:311786TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) {
johnme627dc8c72016-08-19 21:49:391787 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311788
1789 // Get a token.
johnme627dc8c72016-08-19 21:49:391790 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1791 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441792 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311793
1794 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391795 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311796 EXPECT_EQ("token1", last_registration_id());
1797 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391798 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311799
jianli70715cc2015-06-23 21:46:481800 reset_last_event();
1801
jianlic02d25e2015-05-27 22:24:311802 // Get another token.
johnme627dc8c72016-08-19 21:49:391803 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1804 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441805 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311806
1807 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391808 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311809 EXPECT_EQ("token2", last_registration_id());
1810 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391811 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311812 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391813 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311814
jianli70715cc2015-06-23 21:46:481815 reset_last_event();
1816
jianlic02d25e2015-05-27 22:24:311817 // Delete the 2nd token.
johnme627dc8c72016-08-19 21:49:391818 DeleteToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441819 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311820
1821 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391822 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311823 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1824 // The 2nd token is gone while the 1st token still exists.
johnme627dc8c72016-08-19 21:49:391825 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
1826 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311827
jianli70715cc2015-06-23 21:46:481828 reset_last_event();
1829
jianlic02d25e2015-05-27 22:24:311830 // Delete the 1st token.
johnme627dc8c72016-08-19 21:49:391831 DeleteToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441832 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311833
1834 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391835 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311836 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1837 // Both tokens are gone now.
johnme627dc8c72016-08-19 21:49:391838 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1839 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
jianli70715cc2015-06-23 21:46:481840
1841 reset_last_event();
1842
1843 // Trying to delete the token again will get an error.
johnme627dc8c72016-08-19 21:49:391844 DeleteToken(kExtensionAppId, kSender, kScope);
jianli70715cc2015-06-23 21:46:481845 PumpLoopUntilIdle();
1846
1847 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391848 EXPECT_EQ(kExtensionAppId, last_app_id());
jianli70715cc2015-06-23 21:46:481849 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
jianlic02d25e2015-05-27 22:24:311850}
1851
[email protected]617b22e92019-09-30 20:29:371852TEST_F(GCMClientInstanceIDTest, DISABLED_DeleteAllTokens) {
johnme627dc8c72016-08-19 21:49:391853 AddInstanceID(kExtensionAppId, kInstanceID);
jianlic02d25e2015-05-27 22:24:311854
1855 // Get a token.
johnme627dc8c72016-08-19 21:49:391856 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1857 GetToken(kExtensionAppId, kSender, kScope);
johnme0fb2fc02016-07-14 14:47:441858 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
jianlic02d25e2015-05-27 22:24:311859
1860 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391861 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311862 EXPECT_EQ("token1", last_registration_id());
1863 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391864 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311865
jianli70715cc2015-06-23 21:46:481866 reset_last_event();
1867
jianlic02d25e2015-05-27 22:24:311868 // Get another token.
johnme627dc8c72016-08-19 21:49:391869 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender2, kScope));
1870 GetToken(kExtensionAppId, kSender2, kScope);
johnme0fb2fc02016-07-14 14:47:441871 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
jianlic02d25e2015-05-27 22:24:311872
1873 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391874 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311875 EXPECT_EQ("token2", last_registration_id());
1876 EXPECT_EQ(GCMClient::SUCCESS, last_result());
johnme627dc8c72016-08-19 21:49:391877 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender2, kScope));
jianlic02d25e2015-05-27 22:24:311878 // The 1st token still exists.
johnme627dc8c72016-08-19 21:49:391879 EXPECT_TRUE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311880
jianli70715cc2015-06-23 21:46:481881 reset_last_event();
1882
jianlic02d25e2015-05-27 22:24:311883 // Delete all tokens.
johnme627dc8c72016-08-19 21:49:391884 DeleteToken(kExtensionAppId, "*", "*");
johnme0fb2fc02016-07-14 14:47:441885 ASSERT_NO_FATAL_FAILURE(CompleteDeleteToken());
jianlic02d25e2015-05-27 22:24:311886
1887 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391888 EXPECT_EQ(kExtensionAppId, last_app_id());
jianlic02d25e2015-05-27 22:24:311889 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1890 // All tokens are gone now.
johnme627dc8c72016-08-19 21:49:391891 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
1892 EXPECT_FALSE(ExistsToken(kExtensionAppId, kSender, kScope));
jianlic02d25e2015-05-27 22:24:311893}
1894
jianliea8534872015-06-22 21:06:221895TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) {
johnme627dc8c72016-08-19 21:49:391896 AddInstanceID(kExtensionAppId, kInstanceID);
jianliea8534872015-06-22 21:06:221897
1898 // Delete all tokens without getting a token first.
johnme627dc8c72016-08-19 21:49:391899 DeleteToken(kExtensionAppId, "*", "*");
jianliea8534872015-06-22 21:06:221900 // No need to call CompleteDeleteToken since unregistration request should
1901 // not be triggered.
1902 PumpLoopUntilIdle();
1903
1904 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
johnme627dc8c72016-08-19 21:49:391905 EXPECT_EQ(kExtensionAppId, last_app_id());
jianliea8534872015-06-22 21:06:221906 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1907}
1908
johnme627dc8c72016-08-19 21:49:391909TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) {
1910 AddInstanceID(kExtensionAppId, kInstanceID);
1911 GetToken(kExtensionAppId, kSender, kScope);
1912 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
johnme627dc8c72016-08-19 21:49:391913
1914 std::map<std::string, std::string> expected_data;
1915
Peter Beverlooc66fc63c2017-07-07 22:58:211916 MCSMessage message(BuildDownstreamMessage(
johnme627dc8c72016-08-19 21:49:391917 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1918 std::string() /* raw_data */));
Peter Beverlooc66fc63c2017-07-07 22:58:211919 EXPECT_TRUE(message.IsValid());
1920 ReceiveMessageFromMCS(message);
johnme627dc8c72016-08-19 21:49:391921
1922 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1923 EXPECT_EQ(kExtensionAppId, last_app_id());
1924 EXPECT_EQ(expected_data.size(), last_message().data.size());
1925 EXPECT_EQ(expected_data, last_message().data);
1926 EXPECT_EQ(kSender, last_message().sender_id);
johnme627dc8c72016-08-19 21:49:391927}
1928
1929TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) {
1930 AddInstanceID(kSubtypeAppId, kInstanceID);
1931 GetToken(kSubtypeAppId, kSender, kScope);
1932 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
johnme627dc8c72016-08-19 21:49:391933
1934 std::map<std::string, std::string> expected_data;
1935
Peter Beverlooc66fc63c2017-07-07 22:58:211936 MCSMessage message(BuildDownstreamMessage(
johnme627dc8c72016-08-19 21:49:391937 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */,
1938 expected_data, std::string() /* raw_data */));
Peter Beverlooc66fc63c2017-07-07 22:58:211939 EXPECT_TRUE(message.IsValid());
1940 ReceiveMessageFromMCS(message);
johnme627dc8c72016-08-19 21:49:391941
1942 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1943 EXPECT_EQ(kSubtypeAppId, last_app_id());
1944 EXPECT_EQ(expected_data.size(), last_message().data.size());
1945 EXPECT_EQ(expected_data, last_message().data);
1946 EXPECT_EQ(kSender, last_message().sender_id);
johnme627dc8c72016-08-19 21:49:391947}
1948
1949TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithFakeSubtype) {
1950 // Victim non-extension registration.
1951 AddInstanceID(kSubtypeAppId, "iid_1");
1952 GetToken(kSubtypeAppId, kSender, kScope);
1953 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1954
1955 // Malicious extension registration.
1956 AddInstanceID(kExtensionAppId, "iid_2");
1957 GetToken(kExtensionAppId, kSender, kScope);
1958 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
1959
1960 std::map<std::string, std::string> expected_data;
1961
1962 // Message for kExtensionAppId should be delivered to the extension rather
1963 // than the victim app, despite the malicious subtype property attempting to
1964 // impersonate victim app.
1965 MCSMessage message(BuildDownstreamMessage(
1966 kSender, kExtensionAppId /* category */, kSubtypeAppId /* subtype */,
1967 expected_data, std::string() /* raw_data */));
1968 EXPECT_TRUE(message.IsValid());
1969 ReceiveMessageFromMCS(message);
1970
1971 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1972 EXPECT_EQ(kExtensionAppId, last_app_id());
1973 EXPECT_EQ(expected_data.size(), last_message().data.size());
1974 EXPECT_EQ(expected_data, last_message().data);
1975 EXPECT_EQ(kSender, last_message().sender_id);
1976}
1977
[email protected]848b1b62014-01-30 23:51:041978} // namespace gcm