blob: 6078c761da682026960bd6b93e00d00b8a0570d0 [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
[email protected]fdeaf9732014-04-11 21:02:157#include "base/command_line.h"
jianli00b4600f2015-02-10 23:32:498#include "base/files/file_path.h"
9#include "base/files/file_util.h"
[email protected]848b1b62014-01-30 23:51:0410#include "base/files/scoped_temp_dir.h"
chirantan26436e402014-10-24 19:44:4211#include "base/memory/scoped_ptr.h"
[email protected]764c0442014-05-01 04:30:5512#include "base/strings/string_number_conversions.h"
jianli78b56042015-06-17 01:21:2213#include "base/test/test_mock_time_task_runner.h"
14#include "base/thread_task_runner_handle.h"
[email protected]764c0442014-05-01 04:30:5515#include "base/time/clock.h"
chirantan26436e402014-10-24 19:44:4216#include "base/timer/timer.h"
[email protected]446f73c22014-05-14 20:47:1817#include "google_apis/gcm/base/fake_encryptor.h"
[email protected]848b1b62014-01-30 23:51:0418#include "google_apis/gcm/base/mcs_message.h"
19#include "google_apis/gcm/base/mcs_util.h"
20#include "google_apis/gcm/engine/fake_connection_factory.h"
21#include "google_apis/gcm/engine/fake_connection_handler.h"
[email protected]764c0442014-05-01 04:30:5522#include "google_apis/gcm/engine/gservices_settings.h"
[email protected]436bcb82014-04-18 00:40:5723#include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
[email protected]848b1b62014-01-30 23:51:0424#include "google_apis/gcm/protocol/android_checkin.pb.h"
25#include "google_apis/gcm/protocol/checkin.pb.h"
26#include "google_apis/gcm/protocol/mcs.pb.h"
27#include "net/url_request/test_url_fetcher_factory.h"
28#include "net/url_request/url_fetcher_delegate.h"
29#include "net/url_request/url_request_test_util.h"
30#include "testing/gtest/include/gtest/gtest.h"
31
32namespace gcm {
33
34namespace {
35
36enum LastEvent {
37 NONE,
38 LOADING_COMPLETED,
[email protected]848b1b62014-01-30 23:51:0439 REGISTRATION_COMPLETED,
[email protected]e4007042014-02-15 20:34:2840 UNREGISTRATION_COMPLETED,
[email protected]848b1b62014-01-30 23:51:0441 MESSAGE_SEND_ERROR,
[email protected]292af2b22014-08-06 19:42:4542 MESSAGE_SEND_ACK,
[email protected]848b1b62014-01-30 23:51:0443 MESSAGE_RECEIVED,
44 MESSAGES_DELETED,
45};
46
jianlic02d25e2015-05-27 22:24:3147const char kChromeVersion[] = "45.0.0.1";
[email protected]848b1b62014-01-30 23:51:0448const uint64 kDeviceAndroidId = 54321;
49const uint64 kDeviceSecurityToken = 12345;
jianli00b4600f2015-02-10 23:32:4950const uint64 kDeviceAndroidId2 = 11111;
51const uint64 kDeviceSecurityToken2 = 2222;
[email protected]764c0442014-05-01 04:30:5552const int64 kSettingsCheckinInterval = 16 * 60 * 60;
[email protected]3a20a4d2014-03-21 22:54:2153const char kAppId[] = "app_id";
54const char kSender[] = "project_id";
55const char kSender2[] = "project_id2";
56const char kSender3[] = "project_id3";
[email protected]848b1b62014-01-30 23:51:0457const char kRegistrationResponsePrefix[] = "token=";
[email protected]e4007042014-02-15 20:34:2858const char kUnregistrationResponsePrefix[] = "deleted=";
[email protected]848b1b62014-01-30 23:51:0459
jianlic02d25e2015-05-27 22:24:3160const char kInstanceID[] = "iid_1";
61const char kScope[] = "GCM";
62const char kDeleteTokenResponse[] = "token=foo";
63
[email protected]848b1b62014-01-30 23:51:0464// Helper for building arbitrary data messages.
65MCSMessage BuildDownstreamMessage(
66 const std::string& project_id,
67 const std::string& app_id,
68 const std::map<std::string, std::string>& data) {
69 mcs_proto::DataMessageStanza data_message;
[email protected]848b1b62014-01-30 23:51:0470 data_message.set_from(project_id);
71 data_message.set_category(app_id);
72 for (std::map<std::string, std::string>::const_iterator iter = data.begin();
73 iter != data.end();
74 ++iter) {
75 mcs_proto::AppData* app_data = data_message.add_app_data();
76 app_data->set_key(iter->first);
77 app_data->set_value(iter->second);
78 }
79 return MCSMessage(kDataMessageStanzaTag, data_message);
80}
81
fgorski58b9dfc2014-09-29 16:46:1882GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email,
83 const std::string& token) {
84 GCMClient::AccountTokenInfo account_token;
85 account_token.email = email;
86 account_token.access_token = token;
87 return account_token;
88}
89
90std::map<std::string, std::string> MakeEmailToTokenMap(
91 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
92 std::map<std::string, std::string> email_token_map;
93 for (std::vector<GCMClient::AccountTokenInfo>::const_iterator iter =
94 account_tokens.begin(); iter != account_tokens.end(); ++iter) {
95 email_token_map[iter->email] = iter->access_token;
96 }
97 return email_token_map;
98}
99
[email protected]848b1b62014-01-30 23:51:04100class FakeMCSClient : public MCSClient {
101 public:
102 FakeMCSClient(base::Clock* clock,
[email protected]2bbe0a682014-03-26 00:08:31103 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57104 GCMStore* gcm_store,
105 GCMStatsRecorder* recorder);
dcheng00ea022b2014-10-21 11:24:56106 ~FakeMCSClient() override;
107 void Login(uint64 android_id, uint64 security_token) override;
108 void SendMessage(const MCSMessage& message) override;
[email protected]848b1b62014-01-30 23:51:04109
110 uint64 last_android_id() const { return last_android_id_; }
111 uint64 last_security_token() const { return last_security_token_; }
112 uint8 last_message_tag() const { return last_message_tag_; }
113 const mcs_proto::DataMessageStanza& last_data_message_stanza() const {
114 return last_data_message_stanza_;
115 }
116
117 private:
118 uint64 last_android_id_;
119 uint64 last_security_token_;
120 uint8 last_message_tag_;
121 mcs_proto::DataMessageStanza last_data_message_stanza_;
122};
123
124FakeMCSClient::FakeMCSClient(base::Clock* clock,
[email protected]2bbe0a682014-03-26 00:08:31125 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57126 GCMStore* gcm_store,
127 GCMStatsRecorder* recorder)
chirantan192a9212014-12-06 03:30:45128 : MCSClient("", clock, connection_factory, gcm_store, recorder),
[email protected]848b1b62014-01-30 23:51:04129 last_android_id_(0u),
130 last_security_token_(0u),
131 last_message_tag_(kNumProtoTypes) {
132}
133
134FakeMCSClient::~FakeMCSClient() {
135}
136
[email protected]5799d052014-02-12 20:47:39137void FakeMCSClient::Login(uint64 android_id, uint64 security_token) {
[email protected]848b1b62014-01-30 23:51:04138 last_android_id_ = android_id;
139 last_security_token_ = security_token;
140}
141
142void FakeMCSClient::SendMessage(const MCSMessage& message) {
143 last_message_tag_ = message.tag();
144 if (last_message_tag_ == kDataMessageStanzaTag) {
145 last_data_message_stanza_.CopyFrom(
146 reinterpret_cast<const mcs_proto::DataMessageStanza&>(
147 message.GetProtobuf()));
148 }
149}
150
[email protected]764c0442014-05-01 04:30:55151class AutoAdvancingTestClock : public base::Clock {
152 public:
153 explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta);
dcheng00ea022b2014-10-21 11:24:56154 ~AutoAdvancingTestClock() override;
[email protected]764c0442014-05-01 04:30:55155
dcheng00ea022b2014-10-21 11:24:56156 base::Time Now() override;
[email protected]764c0442014-05-01 04:30:55157 void Advance(TimeDelta delta);
158 int call_count() const { return call_count_; }
159
160 private:
161 int call_count_;
162 base::TimeDelta auto_increment_time_delta_;
163 base::Time now_;
164
165 DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock);
166};
167
168AutoAdvancingTestClock::AutoAdvancingTestClock(
169 base::TimeDelta auto_increment_time_delta)
170 : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) {
171}
172
173AutoAdvancingTestClock::~AutoAdvancingTestClock() {
174}
175
176base::Time AutoAdvancingTestClock::Now() {
177 call_count_++;
178 now_ += auto_increment_time_delta_;
179 return now_;
180}
181
182void AutoAdvancingTestClock::Advance(base::TimeDelta delta) {
183 now_ += delta;
184}
185
[email protected]2bbe0a682014-03-26 00:08:31186class FakeGCMInternalsBuilder : public GCMInternalsBuilder {
187 public:
[email protected]764c0442014-05-01 04:30:55188 FakeGCMInternalsBuilder(base::TimeDelta clock_step);
dcheng00ea022b2014-10-21 11:24:56189 ~FakeGCMInternalsBuilder() override;
[email protected]2bbe0a682014-03-26 00:08:31190
dcheng00ea022b2014-10-21 11:24:56191 scoped_ptr<base::Clock> BuildClock() override;
192 scoped_ptr<MCSClient> BuildMCSClient(const std::string& version,
193 base::Clock* clock,
194 ConnectionFactory* connection_factory,
195 GCMStore* gcm_store,
196 GCMStatsRecorder* recorder) override;
197 scoped_ptr<ConnectionFactory> BuildConnectionFactory(
[email protected]2bbe0a682014-03-26 00:08:31198 const std::vector<GURL>& endpoints,
199 const net::BackoffEntry::Policy& backoff_policy,
[email protected]6f63606a2014-07-18 02:18:55200 const scoped_refptr<net::HttpNetworkSession>& gcm_network_session,
201 const scoped_refptr<net::HttpNetworkSession>& http_network_session,
[email protected]9df5b932014-04-30 00:39:06202 net::NetLog* net_log,
mostynbfe59f482014-10-06 15:04:46203 GCMStatsRecorder* recorder) override;
[email protected]764c0442014-05-01 04:30:55204
205 private:
206 base::TimeDelta clock_step_;
[email protected]2bbe0a682014-03-26 00:08:31207};
208
[email protected]764c0442014-05-01 04:30:55209FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step)
210 : clock_step_(clock_step) {
211}
[email protected]2bbe0a682014-03-26 00:08:31212
213FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {}
214
215scoped_ptr<base::Clock> FakeGCMInternalsBuilder::BuildClock() {
[email protected]764c0442014-05-01 04:30:55216 return make_scoped_ptr<base::Clock>(new AutoAdvancingTestClock(clock_step_));
[email protected]2bbe0a682014-03-26 00:08:31217}
218
219scoped_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient(
220 const std::string& version,
221 base::Clock* clock,
222 ConnectionFactory* connection_factory,
[email protected]436bcb82014-04-18 00:40:57223 GCMStore* gcm_store,
224 GCMStatsRecorder* recorder) {
[email protected]2bbe0a682014-03-26 00:08:31225 return make_scoped_ptr<MCSClient>(new FakeMCSClient(clock,
226 connection_factory,
[email protected]436bcb82014-04-18 00:40:57227 gcm_store,
228 recorder));
[email protected]2bbe0a682014-03-26 00:08:31229}
230
231scoped_ptr<ConnectionFactory> FakeGCMInternalsBuilder::BuildConnectionFactory(
232 const std::vector<GURL>& endpoints,
233 const net::BackoffEntry::Policy& backoff_policy,
[email protected]6f63606a2014-07-18 02:18:55234 const scoped_refptr<net::HttpNetworkSession>& gcm_network_session,
235 const scoped_refptr<net::HttpNetworkSession>& http_network_session,
[email protected]9df5b932014-04-30 00:39:06236 net::NetLog* net_log,
237 GCMStatsRecorder* recorder) {
[email protected]2bbe0a682014-03-26 00:08:31238 return make_scoped_ptr<ConnectionFactory>(new FakeConnectionFactory());
239}
240
[email protected]848b1b62014-01-30 23:51:04241} // namespace
242
243class GCMClientImplTest : public testing::Test,
244 public GCMClient::Delegate {
245 public:
246 GCMClientImplTest();
dcheng30a1b1542014-10-29 21:27:50247 ~GCMClientImplTest() override;
[email protected]848b1b62014-01-30 23:51:04248
dcheng30a1b1542014-10-29 21:27:50249 void SetUp() override;
[email protected]848b1b62014-01-30 23:51:04250
fgorski5df101702014-10-28 02:09:31251 void SetUpUrlFetcherFactory();
252
[email protected]764c0442014-05-01 04:30:55253 void BuildGCMClient(base::TimeDelta clock_step);
[email protected]848b1b62014-01-30 23:51:04254 void InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53255 void StartGCMClient();
jianli7a0c9b62015-05-26 23:24:47256 void Register(const std::string& app_id,
257 const std::vector<std::string>& senders);
258 void Unregister(const std::string& app_id);
[email protected]848b1b62014-01-30 23:51:04259 void ReceiveMessageFromMCS(const MCSMessage& message);
[email protected]292af2b22014-08-06 19:42:45260 void ReceiveOnMessageSentToMCS(
261 const std::string& app_id,
262 const std::string& message_id,
263 const MCSClient::MessageSendStatus status);
[email protected]764c0442014-05-01 04:30:55264 void CompleteCheckin(uint64 android_id,
265 uint64 security_token,
266 const std::string& digest,
267 const std::map<std::string, std::string>& settings);
[email protected]848b1b62014-01-30 23:51:04268 void CompleteRegistration(const std::string& registration_id);
[email protected]e4007042014-02-15 20:34:28269 void CompleteUnregistration(const std::string& app_id);
fgorskie45a34f2014-10-08 17:37:46270 void VerifyPendingRequestFetcherDeleted();
[email protected]848b1b62014-01-30 23:51:04271
[email protected]3a20a4d2014-03-21 22:54:21272 bool ExistsRegistration(const std::string& app_id) const;
273 void AddRegistration(const std::string& app_id,
274 const std::vector<std::string>& sender_ids,
275 const std::string& registration_id);
276
[email protected]848b1b62014-01-30 23:51:04277 // GCMClient::Delegate overrides (for verification).
jianli7a0c9b62015-05-26 23:24:47278 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info,
dcheng00ea022b2014-10-21 11:24:56279 const std::string& registration_id,
280 GCMClient::Result result) override;
jianli7a0c9b62015-05-26 23:24:47281 void OnUnregisterFinished(
282 const linked_ptr<RegistrationInfo>& registration_info,
283 GCMClient::Result result) override;
dcheng00ea022b2014-10-21 11:24:56284 void OnSendFinished(const std::string& app_id,
285 const std::string& message_id,
286 GCMClient::Result result) override {}
287 void OnMessageReceived(const std::string& registration_id,
288 const GCMClient::IncomingMessage& message) override;
289 void OnMessagesDeleted(const std::string& app_id) override;
290 void OnMessageSendError(
[email protected]c6fe36b2014-03-11 10:58:12291 const std::string& app_id,
mostynbfe59f482014-10-06 15:04:46292 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
dcheng00ea022b2014-10-21 11:24:56293 void OnSendAcknowledged(const std::string& app_id,
294 const std::string& message_id) override;
fgorski5df101702014-10-28 02:09:31295 void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
296 const base::Time& last_token_fetch_time) override;
dcheng00ea022b2014-10-21 11:24:56297 void OnActivityRecorded() override {}
298 void OnConnected(const net::IPEndPoint& ip_endpoint) override {}
299 void OnDisconnected() override {}
[email protected]848b1b62014-01-30 23:51:04300
[email protected]848b1b62014-01-30 23:51:04301 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
jianlif3e52af42015-01-21 23:18:47302 GCMClientImpl::State gcm_client_state() const {
303 return gcm_client_->state_;
304 }
[email protected]848b1b62014-01-30 23:51:04305 FakeMCSClient* mcs_client() const {
306 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
307 }
[email protected]2bbe0a682014-03-26 00:08:31308 ConnectionFactory* connection_factory() const {
309 return gcm_client_->connection_factory_.get();
310 }
[email protected]848b1b62014-01-30 23:51:04311
[email protected]7df5ef22014-07-17 07:35:58312 const GCMClientImpl::CheckinInfo& device_checkin_info() const {
313 return gcm_client_->device_checkin_info_;
314 }
315
[email protected]3a20a4d2014-03-21 22:54:21316 void reset_last_event() {
317 last_event_ = NONE;
318 last_app_id_.clear();
319 last_registration_id_.clear();
320 last_message_id_.clear();
321 last_result_ = GCMClient::UNKNOWN_ERROR;
fgorski5df101702014-10-28 02:09:31322 last_account_mappings_.clear();
323 last_token_fetch_time_ = base::Time();
[email protected]3a20a4d2014-03-21 22:54:21324 }
325
[email protected]848b1b62014-01-30 23:51:04326 LastEvent last_event() const { return last_event_; }
327 const std::string& last_app_id() const { return last_app_id_; }
328 const std::string& last_registration_id() const {
329 return last_registration_id_;
330 }
331 const std::string& last_message_id() const { return last_message_id_; }
332 GCMClient::Result last_result() const { return last_result_; }
[email protected]848b1b62014-01-30 23:51:04333 const GCMClient::IncomingMessage& last_message() const {
334 return last_message_;
335 }
[email protected]c6fe36b2014-03-11 10:58:12336 const GCMClient::SendErrorDetails& last_error_details() const {
337 return last_error_details_;
338 }
fgorski5df101702014-10-28 02:09:31339 const base::Time& last_token_fetch_time() const {
340 return last_token_fetch_time_;
341 }
342 const std::vector<AccountMapping>& last_account_mappings() {
343 return last_account_mappings_;
344 }
[email protected]848b1b62014-01-30 23:51:04345
[email protected]06e45272014-05-06 03:41:34346 const GServicesSettings& gservices_settings() const {
347 return gcm_client_->gservices_settings_;
[email protected]764c0442014-05-01 04:30:55348 }
349
jianli00b4600f2015-02-10 23:32:49350 const base::FilePath& temp_directory_path() const {
351 return temp_directory_.path();
352 }
353
jianli9f9a73672015-06-16 19:39:34354 base::FilePath gcm_store_path() const {
355 // Pass an non-existent directory as store path to match the exact
356 // behavior in the production code. Currently GCMStoreImpl checks if
357 // the directory exist or not to determine the store existence.
358 return temp_directory_.path().Append(FILE_PATH_LITERAL("GCM Store"));
359 }
360
[email protected]d3ba08d92014-04-04 23:03:55361 int64 CurrentTime();
362
363 // Tooling.
[email protected]d3ba08d92014-04-04 23:03:55364 void PumpLoopUntilIdle();
[email protected]764c0442014-05-01 04:30:55365 bool CreateUniqueTempDir();
366 AutoAdvancingTestClock* clock() const {
367 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get());
[email protected]a0144ceb2014-04-04 23:49:34368 }
jianlic02d25e2015-05-27 22:24:31369 net::TestURLFetcherFactory* url_fetcher_factory() {
370 return &url_fetcher_factory_;
371 }
jianli78b56042015-06-17 01:21:22372 base::TestMockTimeTaskRunner* task_runner() {
373 return task_runner_.get();
374 }
[email protected]d3ba08d92014-04-04 23:03:55375
[email protected]764c0442014-05-01 04:30:55376 private:
[email protected]848b1b62014-01-30 23:51:04377 // Variables used for verification.
378 LastEvent last_event_;
379 std::string last_app_id_;
380 std::string last_registration_id_;
381 std::string last_message_id_;
382 GCMClient::Result last_result_;
[email protected]848b1b62014-01-30 23:51:04383 GCMClient::IncomingMessage last_message_;
[email protected]c6fe36b2014-03-11 10:58:12384 GCMClient::SendErrorDetails last_error_details_;
fgorski5df101702014-10-28 02:09:31385 base::Time last_token_fetch_time_;
386 std::vector<AccountMapping> last_account_mappings_;
[email protected]848b1b62014-01-30 23:51:04387
388 scoped_ptr<GCMClientImpl> gcm_client_;
[email protected]848b1b62014-01-30 23:51:04389
[email protected]848b1b62014-01-30 23:51:04390 net::TestURLFetcherFactory url_fetcher_factory_;
391
jianli78b56042015-06-17 01:21:22392 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
393 base::ThreadTaskRunnerHandle task_runner_handle_;
394
[email protected]848b1b62014-01-30 23:51:04395 // Injected to GCM client:
396 base::ScopedTempDir temp_directory_;
397 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
398};
399
400GCMClientImplTest::GCMClientImplTest()
401 : last_event_(NONE),
402 last_result_(GCMClient::UNKNOWN_ERROR),
jianli78b56042015-06-17 01:21:22403 task_runner_(new base::TestMockTimeTaskRunner),
404 task_runner_handle_(task_runner_),
skyostilb0daa012015-06-02 19:03:48405 url_request_context_getter_(
jianli78b56042015-06-17 01:21:22406 new net::TestURLRequestContextGetter(task_runner_)) {
[email protected]848b1b62014-01-30 23:51:04407}
408
409GCMClientImplTest::~GCMClientImplTest() {}
410
411void GCMClientImplTest::SetUp() {
[email protected]fdeaf9732014-04-11 21:02:15412 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55413 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:55414 BuildGCMClient(base::TimeDelta());
[email protected]848b1b62014-01-30 23:51:04415 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53416 StartGCMClient();
fgorski5df101702014-10-28 02:09:31417 SetUpUrlFetcherFactory();
[email protected]764c0442014-05-01 04:30:55418 CompleteCheckin(kDeviceAndroidId,
419 kDeviceSecurityToken,
420 std::string(),
421 std::map<std::string, std::string>());
[email protected]848b1b62014-01-30 23:51:04422}
423
fgorski5df101702014-10-28 02:09:31424void GCMClientImplTest::SetUpUrlFetcherFactory() {
425 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
426}
427
[email protected]848b1b62014-01-30 23:51:04428void GCMClientImplTest::PumpLoopUntilIdle() {
jianli78b56042015-06-17 01:21:22429 task_runner_->RunUntilIdle();
[email protected]848b1b62014-01-30 23:51:04430}
431
[email protected]764c0442014-05-01 04:30:55432bool GCMClientImplTest::CreateUniqueTempDir() {
433 return temp_directory_.CreateUniqueTempDir();
434}
435
436void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
437 gcm_client_.reset(new GCMClientImpl(make_scoped_ptr<GCMInternalsBuilder>(
438 new FakeGCMInternalsBuilder(clock_step))));
439}
440
441void GCMClientImplTest::CompleteCheckin(
442 uint64 android_id,
443 uint64 security_token,
444 const std::string& digest,
445 const std::map<std::string, std::string>& settings) {
[email protected]848b1b62014-01-30 23:51:04446 checkin_proto::AndroidCheckinResponse response;
447 response.set_stats_ok(true);
448 response.set_android_id(android_id);
449 response.set_security_token(security_token);
450
[email protected]764c0442014-05-01 04:30:55451 // For testing G-services settings.
452 if (!digest.empty()) {
453 response.set_digest(digest);
454 for (std::map<std::string, std::string>::const_iterator it =
455 settings.begin();
456 it != settings.end();
457 ++it) {
458 checkin_proto::GservicesSetting* setting = response.add_setting();
459 setting->set_name(it->first);
460 setting->set_value(it->second);
461 }
[email protected]aae544d72014-05-20 06:53:10462 response.set_settings_diff(false);
[email protected]764c0442014-05-01 04:30:55463 }
464
[email protected]848b1b62014-01-30 23:51:04465 std::string response_string;
466 response.SerializeToString(&response_string);
467
468 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
469 ASSERT_TRUE(fetcher);
470 fetcher->set_response_code(net::HTTP_OK);
471 fetcher->SetResponseString(response_string);
472 fetcher->delegate()->OnURLFetchComplete(fetcher);
jianlid7e80f22015-06-18 22:21:31473 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
474 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04475}
476
477void GCMClientImplTest::CompleteRegistration(
478 const std::string& registration_id) {
479 std::string response(kRegistrationResponsePrefix);
480 response.append(registration_id);
481 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
482 ASSERT_TRUE(fetcher);
483 fetcher->set_response_code(net::HTTP_OK);
484 fetcher->SetResponseString(response);
485 fetcher->delegate()->OnURLFetchComplete(fetcher);
jianlid7e80f22015-06-18 22:21:31486 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
487 PumpLoopUntilIdle();
[email protected]e4007042014-02-15 20:34:28488}
489
490void GCMClientImplTest::CompleteUnregistration(
491 const std::string& app_id) {
492 std::string response(kUnregistrationResponsePrefix);
493 response.append(app_id);
494 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
495 ASSERT_TRUE(fetcher);
496 fetcher->set_response_code(net::HTTP_OK);
497 fetcher->SetResponseString(response);
498 fetcher->delegate()->OnURLFetchComplete(fetcher);
jianlid7e80f22015-06-18 22:21:31499 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
500 PumpLoopUntilIdle();
fgorskie45a34f2014-10-08 17:37:46501}
502
503void GCMClientImplTest::VerifyPendingRequestFetcherDeleted() {
504 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
505 EXPECT_FALSE(fetcher);
[email protected]3a20a4d2014-03-21 22:54:21506}
507
508bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const {
jianli7a0c9b62015-05-26 23:24:47509 return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id);
[email protected]3a20a4d2014-03-21 22:54:21510}
511
512void GCMClientImplTest::AddRegistration(
513 const std::string& app_id,
514 const std::vector<std::string>& sender_ids,
515 const std::string& registration_id) {
jianli7a0c9b62015-05-26 23:24:47516 linked_ptr<GCMRegistrationInfo> registration(new GCMRegistrationInfo);
517 registration->app_id = app_id;
[email protected]3a20a4d2014-03-21 22:54:21518 registration->sender_ids = sender_ids;
jianli7a0c9b62015-05-26 23:24:47519 gcm_client_->registrations_[registration] = registration_id;
[email protected]848b1b62014-01-30 23:51:04520}
521
522void GCMClientImplTest::InitializeGCMClient() {
[email protected]848b1b62014-01-30 23:51:04523 clock()->Advance(base::TimeDelta::FromMilliseconds(1));
[email protected]2bbe0a682014-03-26 00:08:31524
[email protected]848b1b62014-01-30 23:51:04525 // Actual initialization.
[email protected]8ad80512014-05-23 09:40:47526 GCMClient::ChromeBuildInfo chrome_build_info;
jianlic02d25e2015-05-27 22:24:31527 chrome_build_info.version = kChromeVersion;
jianli9f9a73672015-06-16 19:39:34528 gcm_client_->Initialize(
529 chrome_build_info,
530 gcm_store_path(),
jianli78b56042015-06-17 01:21:22531 task_runner_,
jianli9f9a73672015-06-16 19:39:34532 url_request_context_getter_,
533 make_scoped_ptr<Encryptor>(new FakeEncryptor),
534 this);
[email protected]1abdf202014-06-13 19:38:53535}
[email protected]d3a4b2e2014-02-27 13:46:54536
[email protected]1abdf202014-06-13 19:38:53537void GCMClientImplTest::StartGCMClient() {
[email protected]2bbe0a682014-03-26 00:08:31538 // Start loading and check-in.
jianlif3e52af42015-01-21 23:18:47539 gcm_client_->Start(GCMClient::IMMEDIATE_START);
[email protected]d3a4b2e2014-02-27 13:46:54540
[email protected]848b1b62014-01-30 23:51:04541 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04542}
543
jianli7a0c9b62015-05-26 23:24:47544void GCMClientImplTest::Register(const std::string& app_id,
545 const std::vector<std::string>& senders) {
546 scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
547 gcm_info->app_id = app_id;
548 gcm_info->sender_ids = senders;
549 gcm_client()->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release()));
550}
551
552void GCMClientImplTest::Unregister(const std::string& app_id) {
553 scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
554 gcm_info->app_id = app_id;
555 gcm_client()->Unregister(
556 make_linked_ptr<RegistrationInfo>(gcm_info.release()));
557}
558
[email protected]848b1b62014-01-30 23:51:04559void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
[email protected]dd47c4ce2014-08-05 23:38:50560 gcm_client_->recorder_.RecordConnectionInitiated(std::string());
561 gcm_client_->recorder_.RecordConnectionSuccess();
[email protected]848b1b62014-01-30 23:51:04562 gcm_client_->OnMessageReceivedFromMCS(message);
563}
564
[email protected]292af2b22014-08-06 19:42:45565void GCMClientImplTest::ReceiveOnMessageSentToMCS(
566 const std::string& app_id,
567 const std::string& message_id,
568 const MCSClient::MessageSendStatus status) {
569 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status);
570}
571
fgorskid578c18b2014-09-24 23:40:17572void GCMClientImplTest::OnGCMReady(
fgorski5df101702014-10-28 02:09:31573 const std::vector<AccountMapping>& account_mappings,
574 const base::Time& last_token_fetch_time) {
[email protected]848b1b62014-01-30 23:51:04575 last_event_ = LOADING_COMPLETED;
fgorski5df101702014-10-28 02:09:31576 last_account_mappings_ = account_mappings;
577 last_token_fetch_time_ = last_token_fetch_time;
[email protected]848b1b62014-01-30 23:51:04578}
579
[email protected]848b1b62014-01-30 23:51:04580void GCMClientImplTest::OnMessageReceived(
581 const std::string& registration_id,
582 const GCMClient::IncomingMessage& message) {
583 last_event_ = MESSAGE_RECEIVED;
584 last_app_id_ = registration_id;
585 last_message_ = message;
[email protected]848b1b62014-01-30 23:51:04586}
587
jianli7a0c9b62015-05-26 23:24:47588void GCMClientImplTest::OnRegisterFinished(
589 const linked_ptr<RegistrationInfo>& registration_info,
590 const std::string& registration_id,
591 GCMClient::Result result) {
[email protected]848b1b62014-01-30 23:51:04592 last_event_ = REGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47593 last_app_id_ = registration_info->app_id;
[email protected]848b1b62014-01-30 23:51:04594 last_registration_id_ = registration_id;
595 last_result_ = result;
596}
597
jianli7a0c9b62015-05-26 23:24:47598void GCMClientImplTest::OnUnregisterFinished(
599 const linked_ptr<RegistrationInfo>& registration_info,
600 GCMClient::Result result) {
[email protected]e4007042014-02-15 20:34:28601 last_event_ = UNREGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47602 last_app_id_ = registration_info->app_id;
[email protected]0e88e1d12014-03-19 06:53:08603 last_result_ = result;
[email protected]e4007042014-02-15 20:34:28604}
605
[email protected]848b1b62014-01-30 23:51:04606void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
607 last_event_ = MESSAGES_DELETED;
608 last_app_id_ = app_id;
609}
610
[email protected]c6fe36b2014-03-11 10:58:12611void GCMClientImplTest::OnMessageSendError(
612 const std::string& app_id,
613 const gcm::GCMClient::SendErrorDetails& send_error_details) {
[email protected]848b1b62014-01-30 23:51:04614 last_event_ = MESSAGE_SEND_ERROR;
615 last_app_id_ = app_id;
[email protected]c6fe36b2014-03-11 10:58:12616 last_error_details_ = send_error_details;
[email protected]848b1b62014-01-30 23:51:04617}
618
[email protected]292af2b22014-08-06 19:42:45619void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id,
620 const std::string& message_id) {
621 last_event_ = MESSAGE_SEND_ACK;
622 last_app_id_ = app_id;
623 last_message_id_ = message_id;
624}
625
[email protected]848b1b62014-01-30 23:51:04626int64 GCMClientImplTest::CurrentTime() {
627 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
628}
629
[email protected]848b1b62014-01-30 23:51:04630TEST_F(GCMClientImplTest, LoadingCompleted) {
631 EXPECT_EQ(LOADING_COMPLETED, last_event());
632 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
633 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
[email protected]7df5ef22014-07-17 07:35:58634
635 // Checking freshly loaded CheckinInfo.
636 EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id);
637 EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret);
638 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
639 EXPECT_TRUE(device_checkin_info().accounts_set);
640 EXPECT_TRUE(device_checkin_info().account_tokens.empty());
[email protected]848b1b62014-01-30 23:51:04641}
642
jianli00b4600f2015-02-10 23:32:49643TEST_F(GCMClientImplTest, LoadingBusted) {
644 // Close the GCM store.
645 gcm_client()->Stop();
646 PumpLoopUntilIdle();
647
648 // Mess up the store.
649 base::FilePath store_file_path =
jianli9f9a73672015-06-16 19:39:34650 gcm_store_path().Append(FILE_PATH_LITERAL("CURRENT"));
jianli00b4600f2015-02-10 23:32:49651 ASSERT_TRUE(base::AppendToFile(store_file_path, "A", 1));
652
653 // Restart GCM client. The store should be reset and the loading should
654 // complete successfully.
655 reset_last_event();
656 BuildGCMClient(base::TimeDelta());
657 InitializeGCMClient();
658 StartGCMClient();
659 CompleteCheckin(kDeviceAndroidId2,
660 kDeviceSecurityToken2,
661 std::string(),
662 std::map<std::string, std::string>());
663
664 EXPECT_EQ(LOADING_COMPLETED, last_event());
665 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
666 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
667}
668
jianli78b56042015-06-17 01:21:22669TEST_F(GCMClientImplTest, DestroyStoreWhenNotNeeded) {
670 // Close the GCM store.
671 gcm_client()->Stop();
672 PumpLoopUntilIdle();
673
674 // Restart GCM client. The store is loaded successfully.
675 reset_last_event();
676 BuildGCMClient(base::TimeDelta());
677 InitializeGCMClient();
678 gcm_client()->Start(GCMClient::DELAYED_START);
679 PumpLoopUntilIdle();
680
681 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
682 EXPECT_TRUE(device_checkin_info().android_id);
683 EXPECT_TRUE(device_checkin_info().secret);
684
685 // Fast forward the clock to trigger the store destroying logic.
686 task_runner()->FastForwardBy(base::TimeDelta::FromMilliseconds(300000));
687 PumpLoopUntilIdle();
688
689 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
690 EXPECT_FALSE(device_checkin_info().android_id);
691 EXPECT_FALSE(device_checkin_info().secret);
692}
693
[email protected]848b1b62014-01-30 23:51:04694TEST_F(GCMClientImplTest, RegisterApp) {
[email protected]3a20a4d2014-03-21 22:54:21695 EXPECT_FALSE(ExistsRegistration(kAppId));
696
[email protected]848b1b62014-01-30 23:51:04697 std::vector<std::string> senders;
698 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47699 Register(kAppId, senders);
[email protected]848b1b62014-01-30 23:51:04700 CompleteRegistration("reg_id");
701
702 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21703 EXPECT_EQ(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04704 EXPECT_EQ("reg_id", last_registration_id());
705 EXPECT_EQ(GCMClient::SUCCESS, last_result());
[email protected]3a20a4d2014-03-21 22:54:21706 EXPECT_TRUE(ExistsRegistration(kAppId));
707}
708
[email protected]3904d2b2014-03-22 01:34:31709TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) {
[email protected]3a20a4d2014-03-21 22:54:21710 EXPECT_FALSE(ExistsRegistration(kAppId));
711
712 std::vector<std::string> senders;
713 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47714 Register(kAppId, senders);
[email protected]3a20a4d2014-03-21 22:54:21715 CompleteRegistration("reg_id");
716 EXPECT_TRUE(ExistsRegistration(kAppId));
717
718 EXPECT_EQ(kAppId, last_app_id());
719 EXPECT_EQ("reg_id", last_registration_id());
720 EXPECT_EQ(GCMClient::SUCCESS, last_result());
721 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
722
723 // Recreate GCMClient in order to load from the persistent store.
[email protected]764c0442014-05-01 04:30:55724 BuildGCMClient(base::TimeDelta());
[email protected]3a20a4d2014-03-21 22:54:21725 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53726 StartGCMClient();
[email protected]3a20a4d2014-03-21 22:54:21727
728 EXPECT_TRUE(ExistsRegistration(kAppId));
[email protected]848b1b62014-01-30 23:51:04729}
730
[email protected]e4007042014-02-15 20:34:28731TEST_F(GCMClientImplTest, UnregisterApp) {
[email protected]3a20a4d2014-03-21 22:54:21732 EXPECT_FALSE(ExistsRegistration(kAppId));
733
734 std::vector<std::string> senders;
735 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47736 Register(kAppId, senders);
[email protected]3a20a4d2014-03-21 22:54:21737 CompleteRegistration("reg_id");
738 EXPECT_TRUE(ExistsRegistration(kAppId));
739
jianli7a0c9b62015-05-26 23:24:47740 Unregister(kAppId);
[email protected]3a20a4d2014-03-21 22:54:21741 CompleteUnregistration(kAppId);
[email protected]e4007042014-02-15 20:34:28742
743 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21744 EXPECT_EQ(kAppId, last_app_id());
[email protected]e4007042014-02-15 20:34:28745 EXPECT_EQ(GCMClient::SUCCESS, last_result());
[email protected]3a20a4d2014-03-21 22:54:21746 EXPECT_FALSE(ExistsRegistration(kAppId));
[email protected]e4007042014-02-15 20:34:28747}
748
fgorskie45a34f2014-10-08 17:37:46749// Tests that stopping the GCMClient also deletes pending registration requests.
750// This is tested by checking that url fetcher contained in the request was
751// deleted.
752TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) {
753 std::vector<std::string> senders;
754 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47755 Register(kAppId, senders);
fgorskie45a34f2014-10-08 17:37:46756
757 gcm_client()->Stop();
jianlid7e80f22015-06-18 22:21:31758 PumpLoopUntilIdle();
fgorskie45a34f2014-10-08 17:37:46759 VerifyPendingRequestFetcherDeleted();
760}
761
[email protected]848b1b62014-01-30 23:51:04762TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
[email protected]3a20a4d2014-03-21 22:54:21763 // Register to receive messages from kSender and kSender2 only.
764 std::vector<std::string> senders;
765 senders.push_back(kSender);
766 senders.push_back(kSender2);
767 AddRegistration(kAppId, senders, "reg_id");
768
[email protected]848b1b62014-01-30 23:51:04769 std::map<std::string, std::string> expected_data;
770 expected_data["message_type"] = "gcm";
771 expected_data["key"] = "value";
772 expected_data["key2"] = "value2";
[email protected]3a20a4d2014-03-21 22:54:21773
774 // Message for kSender will be received.
775 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data));
[email protected]848b1b62014-01-30 23:51:04776 EXPECT_TRUE(message.IsValid());
777 ReceiveMessageFromMCS(message);
778
779 expected_data.erase(expected_data.find("message_type"));
780 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21781 EXPECT_EQ(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04782 EXPECT_EQ(expected_data.size(), last_message().data.size());
783 EXPECT_EQ(expected_data, last_message().data);
[email protected]3a20a4d2014-03-21 22:54:21784 EXPECT_EQ(kSender, last_message().sender_id);
785
786 reset_last_event();
787
788 // Message for kSender2 will be received.
789 MCSMessage message2(BuildDownstreamMessage(kSender2, kAppId, expected_data));
790 EXPECT_TRUE(message2.IsValid());
791 ReceiveMessageFromMCS(message2);
792
793 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
794 EXPECT_EQ(kAppId, last_app_id());
795 EXPECT_EQ(expected_data.size(), last_message().data.size());
796 EXPECT_EQ(expected_data, last_message().data);
797 EXPECT_EQ(kSender2, last_message().sender_id);
798
799 reset_last_event();
800
801 // Message from kSender3 will be dropped.
802 MCSMessage message3(BuildDownstreamMessage(kSender3, kAppId, expected_data));
803 EXPECT_TRUE(message3.IsValid());
804 ReceiveMessageFromMCS(message3);
805
806 EXPECT_NE(MESSAGE_RECEIVED, last_event());
807 EXPECT_NE(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04808}
809
810TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) {
811 std::map<std::string, std::string> expected_data;
812 expected_data["message_type"] = "send_error";
813 expected_data["google.message_id"] = "007";
[email protected]c6fe36b2014-03-11 10:58:12814 expected_data["error_details"] = "some details";
[email protected]848b1b62014-01-30 23:51:04815 MCSMessage message(BuildDownstreamMessage(
[email protected]3a20a4d2014-03-21 22:54:21816 kSender, kAppId, expected_data));
[email protected]848b1b62014-01-30 23:51:04817 EXPECT_TRUE(message.IsValid());
818 ReceiveMessageFromMCS(message);
819
820 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
[email protected]3a20a4d2014-03-21 22:54:21821 EXPECT_EQ(kAppId, last_app_id());
[email protected]c6fe36b2014-03-11 10:58:12822 EXPECT_EQ("007", last_error_details().message_id);
823 EXPECT_EQ(1UL, last_error_details().additional_data.size());
824 GCMClient::MessageData::const_iterator iter =
825 last_error_details().additional_data.find("error_details");
826 EXPECT_TRUE(iter != last_error_details().additional_data.end());
827 EXPECT_EQ("some details", iter->second);
[email protected]848b1b62014-01-30 23:51:04828}
829
830TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
831 std::map<std::string, std::string> expected_data;
832 expected_data["message_type"] = "deleted_messages";
833 MCSMessage message(BuildDownstreamMessage(
[email protected]3a20a4d2014-03-21 22:54:21834 kSender, kAppId, expected_data));
[email protected]848b1b62014-01-30 23:51:04835 EXPECT_TRUE(message.IsValid());
836 ReceiveMessageFromMCS(message);
837
838 EXPECT_EQ(MESSAGES_DELETED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21839 EXPECT_EQ(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04840}
841
842TEST_F(GCMClientImplTest, SendMessage) {
[email protected]848b1b62014-01-30 23:51:04843 GCMClient::OutgoingMessage message;
844 message.id = "007";
845 message.time_to_live = 500;
846 message.data["key"] = "value";
[email protected]3a20a4d2014-03-21 22:54:21847 gcm_client()->Send(kAppId, kSender, message);
[email protected]848b1b62014-01-30 23:51:04848
849 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
[email protected]3a20a4d2014-03-21 22:54:21850 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category());
851 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:04852 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
853 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
854 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
[email protected]848b1b62014-01-30 23:51:04855 EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from());
[email protected]3a20a4d2014-03-21 22:54:21856 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:04857 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
858 EXPECT_EQ("value",
859 mcs_client()->last_data_message_stanza().app_data(0).value());
860}
861
[email protected]292af2b22014-08-06 19:42:45862TEST_F(GCMClientImplTest, SendMessageAcknowledged) {
863 ReceiveOnMessageSentToMCS(kAppId, "007", MCSClient::SENT);
864 EXPECT_EQ(MESSAGE_SEND_ACK, last_event());
865 EXPECT_EQ(kAppId, last_app_id());
866 EXPECT_EQ("007", last_message_id());
867}
868
[email protected]764c0442014-05-01 04:30:55869class GCMClientImplCheckinTest : public GCMClientImplTest {
870 public:
871 GCMClientImplCheckinTest();
dcheng30a1b1542014-10-29 21:27:50872 ~GCMClientImplCheckinTest() override;
[email protected]764c0442014-05-01 04:30:55873
dcheng30a1b1542014-10-29 21:27:50874 void SetUp() override;
[email protected]764c0442014-05-01 04:30:55875};
876
877GCMClientImplCheckinTest::GCMClientImplCheckinTest() {
878}
879
880GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {
881}
882
883void GCMClientImplCheckinTest::SetUp() {
884 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55885 // Creating unique temp directory that will be used by GCMStore shared between
886 // GCM Client and G-services settings.
887 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]764c0442014-05-01 04:30:55888 // Time will be advancing one hour every time it is checked.
889 BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval));
890 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53891 StartGCMClient();
[email protected]764c0442014-05-01 04:30:55892}
893
894TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
895 std::map<std::string, std::string> settings;
896 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
897 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:13898 settings["gcm_hostname"] = "alternative.gcm.host";
899 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:55900 settings["gcm_registration_url"] = "http://alternative.url/registration";
[email protected]aae544d72014-05-20 06:53:10901 CompleteCheckin(kDeviceAndroidId,
902 kDeviceSecurityToken,
903 GServicesSettings::CalculateDigest(settings),
904 settings);
[email protected]f09354512014-05-02 00:51:13905 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:10906 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:34907 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:10908 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:34909 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:10910 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:13911 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:10912 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:13913 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:10914 gservices_settings().GetMCSFallbackEndpoint());
[email protected]764c0442014-05-01 04:30:55915}
916
917// This test only checks that periodic checkin happens.
918TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
919 std::map<std::string, std::string> settings;
920 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
921 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:13922 settings["gcm_hostname"] = "alternative.gcm.host";
923 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:55924 settings["gcm_registration_url"] = "http://alternative.url/registration";
[email protected]aae544d72014-05-20 06:53:10925 CompleteCheckin(kDeviceAndroidId,
926 kDeviceSecurityToken,
927 GServicesSettings::CalculateDigest(settings),
928 settings);
[email protected]7df5ef22014-07-17 07:35:58929
[email protected]764c0442014-05-01 04:30:55930 EXPECT_EQ(2, clock()->call_count());
931
932 PumpLoopUntilIdle();
[email protected]aae544d72014-05-20 06:53:10933 CompleteCheckin(kDeviceAndroidId,
934 kDeviceSecurityToken,
935 GServicesSettings::CalculateDigest(settings),
936 settings);
[email protected]764c0442014-05-01 04:30:55937}
938
[email protected]06e45272014-05-06 03:41:34939TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) {
940 std::map<std::string, std::string> settings;
941 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
942 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:13943 settings["gcm_hostname"] = "alternative.gcm.host";
944 settings["gcm_secure_port"] = "7777";
[email protected]06e45272014-05-06 03:41:34945 settings["gcm_registration_url"] = "http://alternative.url/registration";
[email protected]aae544d72014-05-20 06:53:10946 CompleteCheckin(kDeviceAndroidId,
947 kDeviceSecurityToken,
948 GServicesSettings::CalculateDigest(settings),
949 settings);
[email protected]06e45272014-05-06 03:41:34950
951 BuildGCMClient(base::TimeDelta());
952 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53953 StartGCMClient();
[email protected]06e45272014-05-06 03:41:34954
955 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:10956 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:34957 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:10958 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:34959 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:10960 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:13961 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:10962 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:13963 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:10964 gservices_settings().GetMCSFallbackEndpoint());
[email protected]06e45272014-05-06 03:41:34965}
966
[email protected]7df5ef22014-07-17 07:35:58967// This test only checks that periodic checkin happens.
968TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) {
969 std::map<std::string, std::string> settings;
970 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
971 settings["checkin_url"] = "http://alternative.url/checkin";
972 settings["gcm_hostname"] = "alternative.gcm.host";
973 settings["gcm_secure_port"] = "7777";
974 settings["gcm_registration_url"] = "http://alternative.url/registration";
975 CompleteCheckin(kDeviceAndroidId,
976 kDeviceSecurityToken,
977 GServicesSettings::CalculateDigest(settings),
978 settings);
979
fgorski58b9dfc2014-09-29 16:46:18980 std::vector<GCMClient::AccountTokenInfo> account_tokens;
981 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
982 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
983 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:58984
985 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
986 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:18987 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
988 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:58989
990 PumpLoopUntilIdle();
991 CompleteCheckin(kDeviceAndroidId,
992 kDeviceSecurityToken,
993 GServicesSettings::CalculateDigest(settings),
994 settings);
995
996 std::set<std::string> accounts;
997 accounts.insert("[email protected]");
998 accounts.insert("[email protected]");
999 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1000 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181001 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1002 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581003}
1004
1005// This test only checks that periodic checkin happens.
1006TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) {
1007 std::map<std::string, std::string> settings;
1008 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
1009 settings["checkin_url"] = "http://alternative.url/checkin";
1010 settings["gcm_hostname"] = "alternative.gcm.host";
1011 settings["gcm_secure_port"] = "7777";
1012 settings["gcm_registration_url"] = "http://alternative.url/registration";
1013 CompleteCheckin(kDeviceAndroidId,
1014 kDeviceSecurityToken,
1015 GServicesSettings::CalculateDigest(settings),
1016 settings);
1017
fgorski58b9dfc2014-09-29 16:46:181018 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1019 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1020 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1021 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581022 PumpLoopUntilIdle();
1023 CompleteCheckin(kDeviceAndroidId,
1024 kDeviceSecurityToken,
1025 GServicesSettings::CalculateDigest(settings),
1026 settings);
1027
1028 EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size());
1029 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181030 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1031 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581032
fgorski58b9dfc2014-09-29 16:46:181033 account_tokens.erase(account_tokens.begin() + 1);
1034 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581035
1036 PumpLoopUntilIdle();
1037 CompleteCheckin(kDeviceAndroidId,
1038 kDeviceSecurityToken,
1039 GServicesSettings::CalculateDigest(settings),
1040 settings);
1041
1042 std::set<std::string> accounts;
1043 accounts.insert("[email protected]");
1044 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1045 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181046 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1047 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581048}
1049
1050// This test only checks that periodic checkin happens.
1051TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) {
1052 std::map<std::string, std::string> settings;
1053 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
1054 settings["checkin_url"] = "http://alternative.url/checkin";
1055 settings["gcm_hostname"] = "alternative.gcm.host";
1056 settings["gcm_secure_port"] = "7777";
1057 settings["gcm_registration_url"] = "http://alternative.url/registration";
1058 CompleteCheckin(kDeviceAndroidId,
1059 kDeviceSecurityToken,
1060 GServicesSettings::CalculateDigest(settings),
1061 settings);
1062
fgorski58b9dfc2014-09-29 16:46:181063 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1064 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1065 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581066
1067 PumpLoopUntilIdle();
1068 CompleteCheckin(kDeviceAndroidId,
1069 kDeviceSecurityToken,
1070 GServicesSettings::CalculateDigest(settings),
1071 settings);
1072
1073 std::set<std::string> accounts;
1074 accounts.insert("[email protected]");
1075 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1076
1077 // This should trigger another checkin, because the list of accounts is
1078 // different.
fgorski58b9dfc2014-09-29 16:46:181079 account_tokens.clear();
1080 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1081 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581082
1083 PumpLoopUntilIdle();
1084 CompleteCheckin(kDeviceAndroidId,
1085 kDeviceSecurityToken,
1086 GServicesSettings::CalculateDigest(settings),
1087 settings);
1088
1089 accounts.clear();
1090 accounts.insert("[email protected]");
1091 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1092 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181093 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1094 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581095}
1096
[email protected]1abdf202014-06-13 19:38:531097class GCMClientImplStartAndStopTest : public GCMClientImplTest {
jianli78b56042015-06-17 01:21:221098 public:
[email protected]1abdf202014-06-13 19:38:531099 GCMClientImplStartAndStopTest();
dcheng30a1b1542014-10-29 21:27:501100 ~GCMClientImplStartAndStopTest() override;
[email protected]1abdf202014-06-13 19:38:531101
dcheng30a1b1542014-10-29 21:27:501102 void SetUp() override;
fgorski5df101702014-10-28 02:09:311103
1104 void DefaultCompleteCheckin();
[email protected]1abdf202014-06-13 19:38:531105};
1106
1107GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() {
1108}
1109
1110GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() {
1111}
1112
1113void GCMClientImplStartAndStopTest::SetUp() {
1114 testing::Test::SetUp();
1115 ASSERT_TRUE(CreateUniqueTempDir());
[email protected]1abdf202014-06-13 19:38:531116 BuildGCMClient(base::TimeDelta());
1117 InitializeGCMClient();
1118}
1119
fgorski5df101702014-10-28 02:09:311120void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() {
1121 SetUpUrlFetcherFactory();
1122 CompleteCheckin(kDeviceAndroidId,
1123 kDeviceSecurityToken,
1124 std::string(),
1125 std::map<std::string, std::string>());
1126 PumpLoopUntilIdle();
1127}
1128
[email protected]1abdf202014-06-13 19:38:531129TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471130 // GCMClientImpl should be in INITIALIZED state at first.
1131 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1132
1133 // Delay start the GCM.
1134 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531135 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341136 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531137
1138 // Stop the GCM.
1139 gcm_client()->Stop();
1140 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471141 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531142
jianlif3e52af42015-01-21 23:18:471143 // Restart the GCM without delay.
1144 gcm_client()->Start(GCMClient::IMMEDIATE_START);
[email protected]1abdf202014-06-13 19:38:531145 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471146 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531147}
1148
jianli9f9a73672015-06-16 19:39:341149TEST_F(GCMClientImplStartAndStopTest, DelayedStartAndStopImmediately) {
jianlif3e52af42015-01-21 23:18:471150 // GCMClientImpl should be in INITIALIZED state at first.
1151 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531152
jianlif3e52af42015-01-21 23:18:471153 // Delay start the GCM and then stop it immediately.
1154 gcm_client()->Start(GCMClient::DELAYED_START);
1155 gcm_client()->Stop();
[email protected]1abdf202014-06-13 19:38:531156 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471157 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341158}
1159
1160TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndStopImmediately) {
1161 // GCMClientImpl should be in INITIALIZED state at first.
1162 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471163
1164 // Start the GCM and then stop it immediately.
1165 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1166 gcm_client()->Stop();
1167 PumpLoopUntilIdle();
1168 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531169}
1170
jianli9f9a73672015-06-16 19:39:341171TEST_F(GCMClientImplStartAndStopTest, DelayedStartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471172 // GCMClientImpl should be in INITIALIZED state at first.
1173 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531174
jianlif3e52af42015-01-21 23:18:471175 // Delay start the GCM and then stop and restart it immediately.
1176 gcm_client()->Start(GCMClient::DELAYED_START);
1177 gcm_client()->Stop();
1178 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531179 PumpLoopUntilIdle();
jianli9f9a73672015-06-16 19:39:341180 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1181}
1182
1183TEST_F(GCMClientImplStartAndStopTest, ImmediateStartStopAndRestart) {
1184 // GCMClientImpl should be in INITIALIZED state at first.
1185 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471186
1187 // Start the GCM and then stop and restart it immediately.
1188 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1189 gcm_client()->Stop();
1190 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1191 PumpLoopUntilIdle();
1192 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1193}
1194
jianli9f9a73672015-06-16 19:39:341195TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenImmediateStart) {
jianlif3e52af42015-01-21 23:18:471196 // GCMClientImpl should be in INITIALIZED state at first.
1197 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1198
jianli9f9a73672015-06-16 19:39:341199 // Start the GCM immediately and complete the checkin.
1200 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1201 PumpLoopUntilIdle();
1202 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1203 DefaultCompleteCheckin();
1204 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1205
1206 // Stop the GCM.
1207 gcm_client()->Stop();
1208 PumpLoopUntilIdle();
1209 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1210
1211 // Start the GCM immediately. GCMClientImpl should be in READY state.
1212 BuildGCMClient(base::TimeDelta());
1213 InitializeGCMClient();
1214 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1215 PumpLoopUntilIdle();
1216 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1217}
1218
1219TEST_F(GCMClientImplStartAndStopTest, ImmediateStartAndThenDelayStart) {
1220 // GCMClientImpl should be in INITIALIZED state at first.
1221 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1222
1223 // Start the GCM immediately and complete the checkin.
1224 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1225 PumpLoopUntilIdle();
1226 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1227 DefaultCompleteCheckin();
1228 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1229
1230 // Stop the GCM.
1231 gcm_client()->Stop();
1232 PumpLoopUntilIdle();
1233 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1234
1235 // Delay start the GCM. GCMClientImpl should be in LOADED state.
1236 BuildGCMClient(base::TimeDelta());
1237 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471238 gcm_client()->Start(GCMClient::DELAYED_START);
1239 PumpLoopUntilIdle();
1240 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
jianli9f9a73672015-06-16 19:39:341241}
1242
1243TEST_F(GCMClientImplStartAndStopTest, DelayedStart) {
1244 // GCMClientImpl should be in INITIALIZED state at first.
1245 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1246
1247 // Delay start the GCM. The store will not be loaded and GCMClientImpl should
1248 // still be in INITIALIZED state.
1249 gcm_client()->Start(GCMClient::DELAYED_START);
1250 PumpLoopUntilIdle();
1251 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
jianlif3e52af42015-01-21 23:18:471252
1253 // Start the GCM immediately and complete the checkin.
1254 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1255 PumpLoopUntilIdle();
1256 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1257 DefaultCompleteCheckin();
1258 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1259
1260 // Registration.
1261 std::vector<std::string> senders;
1262 senders.push_back("sender");
jianli9f9a73672015-06-16 19:39:341263 Register(kAppId, senders);
jianlif3e52af42015-01-21 23:18:471264 CompleteRegistration("reg_id");
1265 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1266
1267 // Stop the GCM.
1268 gcm_client()->Stop();
1269 PumpLoopUntilIdle();
1270 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1271
1272 // Delay start the GCM. GCM is indeed started without delay because the
1273 // registration record has been found.
jianli9f9a73672015-06-16 19:39:341274 BuildGCMClient(base::TimeDelta());
1275 InitializeGCMClient();
jianlif3e52af42015-01-21 23:18:471276 gcm_client()->Start(GCMClient::DELAYED_START);
1277 PumpLoopUntilIdle();
1278 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531279}
1280
fgorski5df101702014-10-28 02:09:311281// Test for known account mappings and last token fetching time being passed
1282// to OnGCMReady.
1283TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) {
1284 // Start the GCM and wait until it is ready.
jianlif3e52af42015-01-21 23:18:471285 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311286 PumpLoopUntilIdle();
1287 DefaultCompleteCheckin();
1288
1289 base::Time expected_time = base::Time::Now();
1290 gcm_client()->SetLastTokenFetchTime(expected_time);
1291 AccountMapping expected_mapping;
1292 expected_mapping.account_id = "accId";
1293 expected_mapping.email = "[email protected]";
1294 expected_mapping.status = AccountMapping::MAPPED;
1295 expected_mapping.status_change_timestamp = expected_time;
1296 gcm_client()->UpdateAccountMapping(expected_mapping);
1297 PumpLoopUntilIdle();
1298
1299 // Stop the GCM.
1300 gcm_client()->Stop();
1301 PumpLoopUntilIdle();
1302
1303 // Restart the GCM.
jianlif3e52af42015-01-21 23:18:471304 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311305 PumpLoopUntilIdle();
1306
1307 EXPECT_EQ(LOADING_COMPLETED, last_event());
1308 EXPECT_EQ(expected_time, last_token_fetch_time());
1309 ASSERT_EQ(1UL, last_account_mappings().size());
1310 const AccountMapping& actual_mapping = last_account_mappings()[0];
1311 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id);
1312 EXPECT_EQ(expected_mapping.email, actual_mapping.email);
1313 EXPECT_EQ(expected_mapping.status, actual_mapping.status);
1314 EXPECT_EQ(expected_mapping.status_change_timestamp,
1315 actual_mapping.status_change_timestamp);
1316}
1317
jianlic02d25e2015-05-27 22:24:311318
1319class GCMClientInstanceIDTest : public GCMClientImplTest {
1320 public:
1321 GCMClientInstanceIDTest();
1322 ~GCMClientInstanceIDTest() override;
1323
1324 void AddInstanceID(const std::string& app_id,
1325 const std::string& instance_id);
1326 void RemoveInstanceID(const std::string& app_id);
1327 void GetToken(const std::string& app_id,
1328 const std::string& authorized_entity,
1329 const std::string& scope);
1330 void DeleteToken(const std::string& app_id,
1331 const std::string& authorized_entity,
1332 const std::string& scope);
1333 void CompleteDeleteToken();
1334 bool ExistsToken(const std::string& app_id,
1335 const std::string& authorized_entity,
1336 const std::string& scope) const;
1337};
1338
1339GCMClientInstanceIDTest::GCMClientInstanceIDTest() {
1340}
1341
1342GCMClientInstanceIDTest::~GCMClientInstanceIDTest() {
1343}
1344
1345void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id,
1346 const std::string& instance_id) {
1347 gcm_client()->AddInstanceIDData(app_id, instance_id, "123");
1348}
1349
1350void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) {
1351 gcm_client()->RemoveInstanceIDData(app_id);
1352}
1353
1354void GCMClientInstanceIDTest::GetToken(const std::string& app_id,
1355 const std::string& authorized_entity,
1356 const std::string& scope) {
1357 scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo);
1358 instance_id_info->app_id = app_id;
1359 instance_id_info->authorized_entity = authorized_entity;
1360 instance_id_info->scope = scope;
1361 gcm_client()->Register(
1362 make_linked_ptr<RegistrationInfo>(instance_id_info.release()));
1363}
1364
1365void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id,
1366 const std::string& authorized_entity,
1367 const std::string& scope) {
1368 scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo);
1369 instance_id_info->app_id = app_id;
1370 instance_id_info->authorized_entity = authorized_entity;
1371 instance_id_info->scope = scope;
1372 gcm_client()->Unregister(
1373 make_linked_ptr<RegistrationInfo>(instance_id_info.release()));
1374}
1375
1376void GCMClientInstanceIDTest::CompleteDeleteToken() {
1377 std::string response(kDeleteTokenResponse);
1378 net::TestURLFetcher* fetcher = url_fetcher_factory()->GetFetcherByID(0);
1379 ASSERT_TRUE(fetcher);
1380 fetcher->set_response_code(net::HTTP_OK);
1381 fetcher->SetResponseString(response);
1382 fetcher->delegate()->OnURLFetchComplete(fetcher);
jianlid7e80f22015-06-18 22:21:311383 // Give a chance for GCMStoreImpl::Backend to finish persisting data.
1384 PumpLoopUntilIdle();
jianlic02d25e2015-05-27 22:24:311385}
1386
1387bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id,
1388 const std::string& authorized_entity,
1389 const std::string& scope) const {
1390 scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo);
1391 instance_id_info->app_id = app_id;
1392 instance_id_info->authorized_entity = authorized_entity;
1393 instance_id_info->scope = scope;
1394 return gcm_client()->registrations_.count(
1395 make_linked_ptr<RegistrationInfo>(instance_id_info.release())) > 0;
1396}
1397
1398TEST_F(GCMClientInstanceIDTest, GetToken) {
1399 AddInstanceID(kAppId, kInstanceID);
1400
1401 // Get a token.
1402 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1403 GetToken(kAppId, kSender, kScope);
1404 CompleteRegistration("token1");
1405
1406 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1407 EXPECT_EQ(kAppId, last_app_id());
1408 EXPECT_EQ("token1", last_registration_id());
1409 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1410 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1411
1412 // Get another token.
1413 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1414 GetToken(kAppId, kSender2, kScope);
1415 CompleteRegistration("token2");
1416
1417 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1418 EXPECT_EQ(kAppId, last_app_id());
1419 EXPECT_EQ("token2", last_registration_id());
1420 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1421 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope));
1422 // The 1st token still exists.
1423 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1424}
1425
jianli70715cc2015-06-23 21:46:481426TEST_F(GCMClientInstanceIDTest, DeleteInvalidToken) {
1427 AddInstanceID(kAppId, kInstanceID);
1428
1429 // Delete an invalid token.
1430 DeleteToken(kAppId, "Foo@#$", kScope);
1431 PumpLoopUntilIdle();
1432
1433 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1434 EXPECT_EQ(kAppId, last_app_id());
1435 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1436
1437 reset_last_event();
1438
1439 // Delete a non-existing token.
1440 DeleteToken(kAppId, kSender, kScope);
1441 PumpLoopUntilIdle();
1442
1443 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1444 EXPECT_EQ(kAppId, last_app_id());
1445 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
1446}
1447
jianlic02d25e2015-05-27 22:24:311448TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) {
1449 AddInstanceID(kAppId, kInstanceID);
1450
1451 // Get a token.
1452 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1453 GetToken(kAppId, kSender, kScope);
1454 CompleteRegistration("token1");
1455
1456 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1457 EXPECT_EQ(kAppId, last_app_id());
1458 EXPECT_EQ("token1", last_registration_id());
1459 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1460 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1461
jianli70715cc2015-06-23 21:46:481462 reset_last_event();
1463
jianlic02d25e2015-05-27 22:24:311464 // Get another token.
1465 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1466 GetToken(kAppId, kSender2, kScope);
1467 CompleteRegistration("token2");
1468
1469 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1470 EXPECT_EQ(kAppId, last_app_id());
1471 EXPECT_EQ("token2", last_registration_id());
1472 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1473 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope));
1474 // The 1st token still exists.
1475 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1476
jianli70715cc2015-06-23 21:46:481477 reset_last_event();
1478
jianlic02d25e2015-05-27 22:24:311479 // Delete the 2nd token.
1480 DeleteToken(kAppId, kSender2, kScope);
1481 CompleteDeleteToken();
1482
1483 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1484 EXPECT_EQ(kAppId, last_app_id());
1485 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1486 // The 2nd token is gone while the 1st token still exists.
1487 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1488 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1489
jianli70715cc2015-06-23 21:46:481490 reset_last_event();
1491
jianlic02d25e2015-05-27 22:24:311492 // Delete the 1st token.
1493 DeleteToken(kAppId, kSender, kScope);
1494 CompleteDeleteToken();
1495
1496 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1497 EXPECT_EQ(kAppId, last_app_id());
1498 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1499 // Both tokens are gone now.
1500 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1501 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
jianli70715cc2015-06-23 21:46:481502
1503 reset_last_event();
1504
1505 // Trying to delete the token again will get an error.
1506 DeleteToken(kAppId, kSender, kScope);
1507 PumpLoopUntilIdle();
1508
1509 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1510 EXPECT_EQ(kAppId, last_app_id());
1511 EXPECT_EQ(GCMClient::INVALID_PARAMETER, last_result());
jianlic02d25e2015-05-27 22:24:311512}
1513
jianliea8534872015-06-22 21:06:221514TEST_F(GCMClientInstanceIDTest, DeleteAllTokens) {
jianlic02d25e2015-05-27 22:24:311515 AddInstanceID(kAppId, kInstanceID);
1516
1517 // Get a token.
1518 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1519 GetToken(kAppId, kSender, kScope);
1520 CompleteRegistration("token1");
1521
1522 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1523 EXPECT_EQ(kAppId, last_app_id());
1524 EXPECT_EQ("token1", last_registration_id());
1525 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1526 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1527
jianli70715cc2015-06-23 21:46:481528 reset_last_event();
1529
jianlic02d25e2015-05-27 22:24:311530 // Get another token.
1531 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1532 GetToken(kAppId, kSender2, kScope);
1533 CompleteRegistration("token2");
1534
1535 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1536 EXPECT_EQ(kAppId, last_app_id());
1537 EXPECT_EQ("token2", last_registration_id());
1538 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1539 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope));
1540 // The 1st token still exists.
1541 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1542
jianli70715cc2015-06-23 21:46:481543 reset_last_event();
1544
jianlic02d25e2015-05-27 22:24:311545 // Delete all tokens.
1546 DeleteToken(kAppId, "*", "*");
1547 CompleteDeleteToken();
1548
1549 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1550 EXPECT_EQ(kAppId, last_app_id());
1551 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1552 // All tokens are gone now.
1553 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1554 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1555}
1556
jianliea8534872015-06-22 21:06:221557TEST_F(GCMClientInstanceIDTest, DeleteAllTokensBeforeGetAnyToken) {
1558 AddInstanceID(kAppId, kInstanceID);
1559
1560 // Delete all tokens without getting a token first.
1561 DeleteToken(kAppId, "*", "*");
1562 // No need to call CompleteDeleteToken since unregistration request should
1563 // not be triggered.
1564 PumpLoopUntilIdle();
1565
1566 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1567 EXPECT_EQ(kAppId, last_app_id());
1568 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1569}
1570
[email protected]848b1b62014-01-30 23:51:041571} // namespace gcm