blob: fb911be02c47104ba997d2e67018ded6891d4aee [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]848b1b62014-01-30 23:51:0412#include "base/message_loop/message_loop.h"
13#include "base/run_loop.h"
[email protected]764c0442014-05-01 04:30:5514#include "base/strings/string_number_conversions.h"
15#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
[email protected]d3ba08d92014-04-04 23:03:55354 int64 CurrentTime();
355
356 // Tooling.
357 void PumpLoop();
358 void PumpLoopUntilIdle();
359 void QuitLoop();
[email protected]764c0442014-05-01 04:30:55360 void InitializeLoop();
361 bool CreateUniqueTempDir();
362 AutoAdvancingTestClock* clock() const {
363 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get());
[email protected]a0144ceb2014-04-04 23:49:34364 }
jianlic02d25e2015-05-27 22:24:31365 net::TestURLFetcherFactory* url_fetcher_factory() {
366 return &url_fetcher_factory_;
367 }
[email protected]d3ba08d92014-04-04 23:03:55368
[email protected]764c0442014-05-01 04:30:55369 private:
[email protected]848b1b62014-01-30 23:51:04370 // Variables used for verification.
371 LastEvent last_event_;
372 std::string last_app_id_;
373 std::string last_registration_id_;
374 std::string last_message_id_;
375 GCMClient::Result last_result_;
[email protected]848b1b62014-01-30 23:51:04376 GCMClient::IncomingMessage last_message_;
[email protected]c6fe36b2014-03-11 10:58:12377 GCMClient::SendErrorDetails last_error_details_;
fgorski5df101702014-10-28 02:09:31378 base::Time last_token_fetch_time_;
379 std::vector<AccountMapping> last_account_mappings_;
[email protected]848b1b62014-01-30 23:51:04380
381 scoped_ptr<GCMClientImpl> gcm_client_;
[email protected]848b1b62014-01-30 23:51:04382
383 base::MessageLoop message_loop_;
384 scoped_ptr<base::RunLoop> run_loop_;
385 net::TestURLFetcherFactory url_fetcher_factory_;
386
387 // Injected to GCM client:
388 base::ScopedTempDir temp_directory_;
389 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
390};
391
392GCMClientImplTest::GCMClientImplTest()
393 : last_event_(NONE),
394 last_result_(GCMClient::UNKNOWN_ERROR),
395 url_request_context_getter_(new net::TestURLRequestContextGetter(
396 message_loop_.message_loop_proxy())) {
397}
398
399GCMClientImplTest::~GCMClientImplTest() {}
400
401void GCMClientImplTest::SetUp() {
[email protected]fdeaf9732014-04-11 21:02:15402 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55403 ASSERT_TRUE(CreateUniqueTempDir());
404 InitializeLoop();
405 BuildGCMClient(base::TimeDelta());
[email protected]848b1b62014-01-30 23:51:04406 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53407 StartGCMClient();
fgorski5df101702014-10-28 02:09:31408 SetUpUrlFetcherFactory();
[email protected]764c0442014-05-01 04:30:55409 CompleteCheckin(kDeviceAndroidId,
410 kDeviceSecurityToken,
411 std::string(),
412 std::map<std::string, std::string>());
[email protected]848b1b62014-01-30 23:51:04413}
414
fgorski5df101702014-10-28 02:09:31415void GCMClientImplTest::SetUpUrlFetcherFactory() {
416 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
417}
418
[email protected]848b1b62014-01-30 23:51:04419void GCMClientImplTest::PumpLoop() {
420 run_loop_->Run();
421 run_loop_.reset(new base::RunLoop());
422}
423
424void GCMClientImplTest::PumpLoopUntilIdle() {
425 run_loop_->RunUntilIdle();
426 run_loop_.reset(new base::RunLoop());
427}
428
429void GCMClientImplTest::QuitLoop() {
430 if (run_loop_ && run_loop_->running())
431 run_loop_->Quit();
432}
433
[email protected]764c0442014-05-01 04:30:55434void GCMClientImplTest::InitializeLoop() {
435 run_loop_.reset(new base::RunLoop);
[email protected]848b1b62014-01-30 23:51:04436}
437
[email protected]764c0442014-05-01 04:30:55438bool GCMClientImplTest::CreateUniqueTempDir() {
439 return temp_directory_.CreateUniqueTempDir();
440}
441
442void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
443 gcm_client_.reset(new GCMClientImpl(make_scoped_ptr<GCMInternalsBuilder>(
444 new FakeGCMInternalsBuilder(clock_step))));
445}
446
447void GCMClientImplTest::CompleteCheckin(
448 uint64 android_id,
449 uint64 security_token,
450 const std::string& digest,
451 const std::map<std::string, std::string>& settings) {
[email protected]848b1b62014-01-30 23:51:04452 checkin_proto::AndroidCheckinResponse response;
453 response.set_stats_ok(true);
454 response.set_android_id(android_id);
455 response.set_security_token(security_token);
456
[email protected]764c0442014-05-01 04:30:55457 // For testing G-services settings.
458 if (!digest.empty()) {
459 response.set_digest(digest);
460 for (std::map<std::string, std::string>::const_iterator it =
461 settings.begin();
462 it != settings.end();
463 ++it) {
464 checkin_proto::GservicesSetting* setting = response.add_setting();
465 setting->set_name(it->first);
466 setting->set_value(it->second);
467 }
[email protected]aae544d72014-05-20 06:53:10468 response.set_settings_diff(false);
[email protected]764c0442014-05-01 04:30:55469 }
470
[email protected]848b1b62014-01-30 23:51:04471 std::string response_string;
472 response.SerializeToString(&response_string);
473
474 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
475 ASSERT_TRUE(fetcher);
476 fetcher->set_response_code(net::HTTP_OK);
477 fetcher->SetResponseString(response_string);
478 fetcher->delegate()->OnURLFetchComplete(fetcher);
[email protected]848b1b62014-01-30 23:51:04479}
480
481void GCMClientImplTest::CompleteRegistration(
482 const std::string& registration_id) {
483 std::string response(kRegistrationResponsePrefix);
484 response.append(registration_id);
485 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
486 ASSERT_TRUE(fetcher);
487 fetcher->set_response_code(net::HTTP_OK);
488 fetcher->SetResponseString(response);
489 fetcher->delegate()->OnURLFetchComplete(fetcher);
[email protected]e4007042014-02-15 20:34:28490}
491
492void GCMClientImplTest::CompleteUnregistration(
493 const std::string& app_id) {
494 std::string response(kUnregistrationResponsePrefix);
495 response.append(app_id);
496 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
497 ASSERT_TRUE(fetcher);
498 fetcher->set_response_code(net::HTTP_OK);
499 fetcher->SetResponseString(response);
500 fetcher->delegate()->OnURLFetchComplete(fetcher);
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;
[email protected]8ad80512014-05-23 09:40:47528 gcm_client_->Initialize(chrome_build_info,
[email protected]848b1b62014-01-30 23:51:04529 temp_directory_.path(),
530 message_loop_.message_loop_proxy(),
[email protected]5799d052014-02-12 20:47:39531 url_request_context_getter_,
[email protected]446f73c22014-05-14 20:47:18532 make_scoped_ptr<Encryptor>(new FakeEncryptor),
[email protected]5799d052014-02-12 20:47:39533 this);
[email protected]1abdf202014-06-13 19:38:53534}
[email protected]d3a4b2e2014-02-27 13:46:54535
[email protected]1abdf202014-06-13 19:38:53536void GCMClientImplTest::StartGCMClient() {
[email protected]2bbe0a682014-03-26 00:08:31537 // Start loading and check-in.
jianlif3e52af42015-01-21 23:18:47538 gcm_client_->Start(GCMClient::IMMEDIATE_START);
[email protected]d3a4b2e2014-02-27 13:46:54539
[email protected]848b1b62014-01-30 23:51:04540 PumpLoopUntilIdle();
[email protected]848b1b62014-01-30 23:51:04541}
542
jianli7a0c9b62015-05-26 23:24:47543void GCMClientImplTest::Register(const std::string& app_id,
544 const std::vector<std::string>& senders) {
545 scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
546 gcm_info->app_id = app_id;
547 gcm_info->sender_ids = senders;
548 gcm_client()->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release()));
549}
550
551void GCMClientImplTest::Unregister(const std::string& app_id) {
552 scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
553 gcm_info->app_id = app_id;
554 gcm_client()->Unregister(
555 make_linked_ptr<RegistrationInfo>(gcm_info.release()));
556}
557
[email protected]848b1b62014-01-30 23:51:04558void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
[email protected]dd47c4ce2014-08-05 23:38:50559 gcm_client_->recorder_.RecordConnectionInitiated(std::string());
560 gcm_client_->recorder_.RecordConnectionSuccess();
[email protected]848b1b62014-01-30 23:51:04561 gcm_client_->OnMessageReceivedFromMCS(message);
562}
563
[email protected]292af2b22014-08-06 19:42:45564void GCMClientImplTest::ReceiveOnMessageSentToMCS(
565 const std::string& app_id,
566 const std::string& message_id,
567 const MCSClient::MessageSendStatus status) {
568 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status);
569}
570
fgorskid578c18b2014-09-24 23:40:17571void GCMClientImplTest::OnGCMReady(
fgorski5df101702014-10-28 02:09:31572 const std::vector<AccountMapping>& account_mappings,
573 const base::Time& last_token_fetch_time) {
[email protected]848b1b62014-01-30 23:51:04574 last_event_ = LOADING_COMPLETED;
fgorski5df101702014-10-28 02:09:31575 last_account_mappings_ = account_mappings;
576 last_token_fetch_time_ = last_token_fetch_time;
[email protected]848b1b62014-01-30 23:51:04577 QuitLoop();
578}
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;
586 QuitLoop();
587}
588
jianli7a0c9b62015-05-26 23:24:47589void GCMClientImplTest::OnRegisterFinished(
590 const linked_ptr<RegistrationInfo>& registration_info,
591 const std::string& registration_id,
592 GCMClient::Result result) {
[email protected]848b1b62014-01-30 23:51:04593 last_event_ = REGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47594 last_app_id_ = registration_info->app_id;
[email protected]848b1b62014-01-30 23:51:04595 last_registration_id_ = registration_id;
596 last_result_ = result;
597}
598
jianli7a0c9b62015-05-26 23:24:47599void GCMClientImplTest::OnUnregisterFinished(
600 const linked_ptr<RegistrationInfo>& registration_info,
601 GCMClient::Result result) {
[email protected]e4007042014-02-15 20:34:28602 last_event_ = UNREGISTRATION_COMPLETED;
jianli7a0c9b62015-05-26 23:24:47603 last_app_id_ = registration_info->app_id;
[email protected]0e88e1d12014-03-19 06:53:08604 last_result_ = result;
[email protected]e4007042014-02-15 20:34:28605}
606
[email protected]848b1b62014-01-30 23:51:04607void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
608 last_event_ = MESSAGES_DELETED;
609 last_app_id_ = app_id;
610}
611
[email protected]c6fe36b2014-03-11 10:58:12612void GCMClientImplTest::OnMessageSendError(
613 const std::string& app_id,
614 const gcm::GCMClient::SendErrorDetails& send_error_details) {
[email protected]848b1b62014-01-30 23:51:04615 last_event_ = MESSAGE_SEND_ERROR;
616 last_app_id_ = app_id;
[email protected]c6fe36b2014-03-11 10:58:12617 last_error_details_ = send_error_details;
[email protected]848b1b62014-01-30 23:51:04618}
619
[email protected]292af2b22014-08-06 19:42:45620void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id,
621 const std::string& message_id) {
622 last_event_ = MESSAGE_SEND_ACK;
623 last_app_id_ = app_id;
624 last_message_id_ = message_id;
625}
626
[email protected]848b1b62014-01-30 23:51:04627int64 GCMClientImplTest::CurrentTime() {
628 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
629}
630
[email protected]848b1b62014-01-30 23:51:04631TEST_F(GCMClientImplTest, LoadingCompleted) {
632 EXPECT_EQ(LOADING_COMPLETED, last_event());
633 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
634 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
[email protected]7df5ef22014-07-17 07:35:58635
636 // Checking freshly loaded CheckinInfo.
637 EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id);
638 EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret);
639 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
640 EXPECT_TRUE(device_checkin_info().accounts_set);
641 EXPECT_TRUE(device_checkin_info().account_tokens.empty());
[email protected]848b1b62014-01-30 23:51:04642}
643
jianli00b4600f2015-02-10 23:32:49644TEST_F(GCMClientImplTest, LoadingBusted) {
645 // Close the GCM store.
646 gcm_client()->Stop();
647 PumpLoopUntilIdle();
648
649 // Mess up the store.
650 base::FilePath store_file_path =
651 temp_directory_path().Append(FILE_PATH_LITERAL("CURRENT"));
652 ASSERT_TRUE(base::AppendToFile(store_file_path, "A", 1));
653
654 // Restart GCM client. The store should be reset and the loading should
655 // complete successfully.
656 reset_last_event();
657 BuildGCMClient(base::TimeDelta());
658 InitializeGCMClient();
659 StartGCMClient();
660 CompleteCheckin(kDeviceAndroidId2,
661 kDeviceSecurityToken2,
662 std::string(),
663 std::map<std::string, std::string>());
664
665 EXPECT_EQ(LOADING_COMPLETED, last_event());
666 EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id());
667 EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token());
668}
669
[email protected]848b1b62014-01-30 23:51:04670TEST_F(GCMClientImplTest, RegisterApp) {
[email protected]3a20a4d2014-03-21 22:54:21671 EXPECT_FALSE(ExistsRegistration(kAppId));
672
[email protected]848b1b62014-01-30 23:51:04673 std::vector<std::string> senders;
674 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47675 Register(kAppId, senders);
[email protected]848b1b62014-01-30 23:51:04676 CompleteRegistration("reg_id");
677
678 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21679 EXPECT_EQ(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04680 EXPECT_EQ("reg_id", last_registration_id());
681 EXPECT_EQ(GCMClient::SUCCESS, last_result());
[email protected]3a20a4d2014-03-21 22:54:21682 EXPECT_TRUE(ExistsRegistration(kAppId));
683}
684
[email protected]3904d2b2014-03-22 01:34:31685TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) {
[email protected]3a20a4d2014-03-21 22:54:21686 EXPECT_FALSE(ExistsRegistration(kAppId));
687
688 std::vector<std::string> senders;
689 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47690 Register(kAppId, senders);
[email protected]3a20a4d2014-03-21 22:54:21691 CompleteRegistration("reg_id");
692 EXPECT_TRUE(ExistsRegistration(kAppId));
693
694 EXPECT_EQ(kAppId, last_app_id());
695 EXPECT_EQ("reg_id", last_registration_id());
696 EXPECT_EQ(GCMClient::SUCCESS, last_result());
697 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
698
699 // Recreate GCMClient in order to load from the persistent store.
[email protected]764c0442014-05-01 04:30:55700 BuildGCMClient(base::TimeDelta());
[email protected]3a20a4d2014-03-21 22:54:21701 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53702 StartGCMClient();
[email protected]3a20a4d2014-03-21 22:54:21703
704 EXPECT_TRUE(ExistsRegistration(kAppId));
[email protected]848b1b62014-01-30 23:51:04705}
706
[email protected]e4007042014-02-15 20:34:28707TEST_F(GCMClientImplTest, UnregisterApp) {
[email protected]3a20a4d2014-03-21 22:54:21708 EXPECT_FALSE(ExistsRegistration(kAppId));
709
710 std::vector<std::string> senders;
711 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47712 Register(kAppId, senders);
[email protected]3a20a4d2014-03-21 22:54:21713 CompleteRegistration("reg_id");
714 EXPECT_TRUE(ExistsRegistration(kAppId));
715
jianli7a0c9b62015-05-26 23:24:47716 Unregister(kAppId);
[email protected]3a20a4d2014-03-21 22:54:21717 CompleteUnregistration(kAppId);
[email protected]e4007042014-02-15 20:34:28718
719 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21720 EXPECT_EQ(kAppId, last_app_id());
[email protected]e4007042014-02-15 20:34:28721 EXPECT_EQ(GCMClient::SUCCESS, last_result());
[email protected]3a20a4d2014-03-21 22:54:21722 EXPECT_FALSE(ExistsRegistration(kAppId));
[email protected]e4007042014-02-15 20:34:28723}
724
fgorskie45a34f2014-10-08 17:37:46725// Tests that stopping the GCMClient also deletes pending registration requests.
726// This is tested by checking that url fetcher contained in the request was
727// deleted.
728TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) {
729 std::vector<std::string> senders;
730 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:47731 Register(kAppId, senders);
fgorskie45a34f2014-10-08 17:37:46732
733 gcm_client()->Stop();
734 VerifyPendingRequestFetcherDeleted();
735}
736
[email protected]848b1b62014-01-30 23:51:04737TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
[email protected]3a20a4d2014-03-21 22:54:21738 // Register to receive messages from kSender and kSender2 only.
739 std::vector<std::string> senders;
740 senders.push_back(kSender);
741 senders.push_back(kSender2);
742 AddRegistration(kAppId, senders, "reg_id");
743
[email protected]848b1b62014-01-30 23:51:04744 std::map<std::string, std::string> expected_data;
745 expected_data["message_type"] = "gcm";
746 expected_data["key"] = "value";
747 expected_data["key2"] = "value2";
[email protected]3a20a4d2014-03-21 22:54:21748
749 // Message for kSender will be received.
750 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data));
[email protected]848b1b62014-01-30 23:51:04751 EXPECT_TRUE(message.IsValid());
752 ReceiveMessageFromMCS(message);
753
754 expected_data.erase(expected_data.find("message_type"));
755 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21756 EXPECT_EQ(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04757 EXPECT_EQ(expected_data.size(), last_message().data.size());
758 EXPECT_EQ(expected_data, last_message().data);
[email protected]3a20a4d2014-03-21 22:54:21759 EXPECT_EQ(kSender, last_message().sender_id);
760
761 reset_last_event();
762
763 // Message for kSender2 will be received.
764 MCSMessage message2(BuildDownstreamMessage(kSender2, kAppId, expected_data));
765 EXPECT_TRUE(message2.IsValid());
766 ReceiveMessageFromMCS(message2);
767
768 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
769 EXPECT_EQ(kAppId, last_app_id());
770 EXPECT_EQ(expected_data.size(), last_message().data.size());
771 EXPECT_EQ(expected_data, last_message().data);
772 EXPECT_EQ(kSender2, last_message().sender_id);
773
774 reset_last_event();
775
776 // Message from kSender3 will be dropped.
777 MCSMessage message3(BuildDownstreamMessage(kSender3, kAppId, expected_data));
778 EXPECT_TRUE(message3.IsValid());
779 ReceiveMessageFromMCS(message3);
780
781 EXPECT_NE(MESSAGE_RECEIVED, last_event());
782 EXPECT_NE(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04783}
784
785TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) {
786 std::map<std::string, std::string> expected_data;
787 expected_data["message_type"] = "send_error";
788 expected_data["google.message_id"] = "007";
[email protected]c6fe36b2014-03-11 10:58:12789 expected_data["error_details"] = "some details";
[email protected]848b1b62014-01-30 23:51:04790 MCSMessage message(BuildDownstreamMessage(
[email protected]3a20a4d2014-03-21 22:54:21791 kSender, kAppId, expected_data));
[email protected]848b1b62014-01-30 23:51:04792 EXPECT_TRUE(message.IsValid());
793 ReceiveMessageFromMCS(message);
794
795 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
[email protected]3a20a4d2014-03-21 22:54:21796 EXPECT_EQ(kAppId, last_app_id());
[email protected]c6fe36b2014-03-11 10:58:12797 EXPECT_EQ("007", last_error_details().message_id);
798 EXPECT_EQ(1UL, last_error_details().additional_data.size());
799 GCMClient::MessageData::const_iterator iter =
800 last_error_details().additional_data.find("error_details");
801 EXPECT_TRUE(iter != last_error_details().additional_data.end());
802 EXPECT_EQ("some details", iter->second);
[email protected]848b1b62014-01-30 23:51:04803}
804
805TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
806 std::map<std::string, std::string> expected_data;
807 expected_data["message_type"] = "deleted_messages";
808 MCSMessage message(BuildDownstreamMessage(
[email protected]3a20a4d2014-03-21 22:54:21809 kSender, kAppId, expected_data));
[email protected]848b1b62014-01-30 23:51:04810 EXPECT_TRUE(message.IsValid());
811 ReceiveMessageFromMCS(message);
812
813 EXPECT_EQ(MESSAGES_DELETED, last_event());
[email protected]3a20a4d2014-03-21 22:54:21814 EXPECT_EQ(kAppId, last_app_id());
[email protected]848b1b62014-01-30 23:51:04815}
816
817TEST_F(GCMClientImplTest, SendMessage) {
[email protected]848b1b62014-01-30 23:51:04818 GCMClient::OutgoingMessage message;
819 message.id = "007";
820 message.time_to_live = 500;
821 message.data["key"] = "value";
[email protected]3a20a4d2014-03-21 22:54:21822 gcm_client()->Send(kAppId, kSender, message);
[email protected]848b1b62014-01-30 23:51:04823
824 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
[email protected]3a20a4d2014-03-21 22:54:21825 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category());
826 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:04827 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
828 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
829 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
[email protected]848b1b62014-01-30 23:51:04830 EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from());
[email protected]3a20a4d2014-03-21 22:54:21831 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
[email protected]848b1b62014-01-30 23:51:04832 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
833 EXPECT_EQ("value",
834 mcs_client()->last_data_message_stanza().app_data(0).value());
835}
836
[email protected]292af2b22014-08-06 19:42:45837TEST_F(GCMClientImplTest, SendMessageAcknowledged) {
838 ReceiveOnMessageSentToMCS(kAppId, "007", MCSClient::SENT);
839 EXPECT_EQ(MESSAGE_SEND_ACK, last_event());
840 EXPECT_EQ(kAppId, last_app_id());
841 EXPECT_EQ("007", last_message_id());
842}
843
[email protected]764c0442014-05-01 04:30:55844class GCMClientImplCheckinTest : public GCMClientImplTest {
845 public:
846 GCMClientImplCheckinTest();
dcheng30a1b1542014-10-29 21:27:50847 ~GCMClientImplCheckinTest() override;
[email protected]764c0442014-05-01 04:30:55848
dcheng30a1b1542014-10-29 21:27:50849 void SetUp() override;
[email protected]764c0442014-05-01 04:30:55850};
851
852GCMClientImplCheckinTest::GCMClientImplCheckinTest() {
853}
854
855GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {
856}
857
858void GCMClientImplCheckinTest::SetUp() {
859 testing::Test::SetUp();
[email protected]764c0442014-05-01 04:30:55860 // Creating unique temp directory that will be used by GCMStore shared between
861 // GCM Client and G-services settings.
862 ASSERT_TRUE(CreateUniqueTempDir());
863 InitializeLoop();
864 // Time will be advancing one hour every time it is checked.
865 BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval));
866 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53867 StartGCMClient();
[email protected]764c0442014-05-01 04:30:55868}
869
870TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
871 std::map<std::string, std::string> settings;
872 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
873 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:13874 settings["gcm_hostname"] = "alternative.gcm.host";
875 settings["gcm_secure_port"] = "7777";
[email protected]764c0442014-05-01 04:30:55876 settings["gcm_registration_url"] = "http://alternative.url/registration";
[email protected]aae544d72014-05-20 06:53:10877 CompleteCheckin(kDeviceAndroidId,
878 kDeviceSecurityToken,
879 GServicesSettings::CalculateDigest(settings),
880 settings);
[email protected]f09354512014-05-02 00:51:13881 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:10882 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:34883 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:10884 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:34885 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:10886 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:13887 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:10888 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:13889 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:10890 gservices_settings().GetMCSFallbackEndpoint());
[email protected]764c0442014-05-01 04:30:55891}
892
893// This test only checks that periodic checkin happens.
894TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
895 std::map<std::string, std::string> settings;
896 settings["checkin_interval"] = base::IntToString(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]7df5ef22014-07-17 07:35:58905
[email protected]764c0442014-05-01 04:30:55906 EXPECT_EQ(2, clock()->call_count());
907
908 PumpLoopUntilIdle();
[email protected]aae544d72014-05-20 06:53:10909 CompleteCheckin(kDeviceAndroidId,
910 kDeviceSecurityToken,
911 GServicesSettings::CalculateDigest(settings),
912 settings);
[email protected]764c0442014-05-01 04:30:55913}
914
[email protected]06e45272014-05-06 03:41:34915TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) {
916 std::map<std::string, std::string> settings;
917 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
918 settings["checkin_url"] = "http://alternative.url/checkin";
[email protected]8ab0c4b22014-05-10 20:40:13919 settings["gcm_hostname"] = "alternative.gcm.host";
920 settings["gcm_secure_port"] = "7777";
[email protected]06e45272014-05-06 03:41:34921 settings["gcm_registration_url"] = "http://alternative.url/registration";
[email protected]aae544d72014-05-20 06:53:10922 CompleteCheckin(kDeviceAndroidId,
923 kDeviceSecurityToken,
924 GServicesSettings::CalculateDigest(settings),
925 settings);
[email protected]06e45272014-05-06 03:41:34926
927 BuildGCMClient(base::TimeDelta());
928 InitializeGCMClient();
[email protected]1abdf202014-06-13 19:38:53929 StartGCMClient();
[email protected]06e45272014-05-06 03:41:34930
931 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
[email protected]aae544d72014-05-20 06:53:10932 gservices_settings().GetCheckinInterval());
[email protected]06e45272014-05-06 03:41:34933 EXPECT_EQ(GURL("http://alternative.url/checkin"),
[email protected]aae544d72014-05-20 06:53:10934 gservices_settings().GetCheckinURL());
[email protected]06e45272014-05-06 03:41:34935 EXPECT_EQ(GURL("http://alternative.url/registration"),
[email protected]aae544d72014-05-20 06:53:10936 gservices_settings().GetRegistrationURL());
[email protected]8ab0c4b22014-05-10 20:40:13937 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
[email protected]aae544d72014-05-20 06:53:10938 gservices_settings().GetMCSMainEndpoint());
[email protected]8ab0c4b22014-05-10 20:40:13939 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
[email protected]aae544d72014-05-20 06:53:10940 gservices_settings().GetMCSFallbackEndpoint());
[email protected]06e45272014-05-06 03:41:34941}
942
[email protected]7df5ef22014-07-17 07:35:58943// This test only checks that periodic checkin happens.
944TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) {
945 std::map<std::string, std::string> settings;
946 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
947 settings["checkin_url"] = "http://alternative.url/checkin";
948 settings["gcm_hostname"] = "alternative.gcm.host";
949 settings["gcm_secure_port"] = "7777";
950 settings["gcm_registration_url"] = "http://alternative.url/registration";
951 CompleteCheckin(kDeviceAndroidId,
952 kDeviceSecurityToken,
953 GServicesSettings::CalculateDigest(settings),
954 settings);
955
fgorski58b9dfc2014-09-29 16:46:18956 std::vector<GCMClient::AccountTokenInfo> account_tokens;
957 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
958 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
959 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:58960
961 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
962 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:18963 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
964 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:58965
966 PumpLoopUntilIdle();
967 CompleteCheckin(kDeviceAndroidId,
968 kDeviceSecurityToken,
969 GServicesSettings::CalculateDigest(settings),
970 settings);
971
972 std::set<std::string> accounts;
973 accounts.insert("[email protected]");
974 accounts.insert("[email protected]");
975 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
976 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:18977 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
978 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:58979}
980
981// This test only checks that periodic checkin happens.
982TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) {
983 std::map<std::string, std::string> settings;
984 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
985 settings["checkin_url"] = "http://alternative.url/checkin";
986 settings["gcm_hostname"] = "alternative.gcm.host";
987 settings["gcm_secure_port"] = "7777";
988 settings["gcm_registration_url"] = "http://alternative.url/registration";
989 CompleteCheckin(kDeviceAndroidId,
990 kDeviceSecurityToken,
991 GServicesSettings::CalculateDigest(settings),
992 settings);
993
fgorski58b9dfc2014-09-29 16:46:18994 std::vector<GCMClient::AccountTokenInfo> account_tokens;
995 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
996 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
997 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:58998 PumpLoopUntilIdle();
999 CompleteCheckin(kDeviceAndroidId,
1000 kDeviceSecurityToken,
1001 GServicesSettings::CalculateDigest(settings),
1002 settings);
1003
1004 EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size());
1005 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181006 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1007 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581008
fgorski58b9dfc2014-09-29 16:46:181009 account_tokens.erase(account_tokens.begin() + 1);
1010 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581011
1012 PumpLoopUntilIdle();
1013 CompleteCheckin(kDeviceAndroidId,
1014 kDeviceSecurityToken,
1015 GServicesSettings::CalculateDigest(settings),
1016 settings);
1017
1018 std::set<std::string> accounts;
1019 accounts.insert("[email protected]");
1020 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1021 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181022 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1023 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581024}
1025
1026// This test only checks that periodic checkin happens.
1027TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) {
1028 std::map<std::string, std::string> settings;
1029 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
1030 settings["checkin_url"] = "http://alternative.url/checkin";
1031 settings["gcm_hostname"] = "alternative.gcm.host";
1032 settings["gcm_secure_port"] = "7777";
1033 settings["gcm_registration_url"] = "http://alternative.url/registration";
1034 CompleteCheckin(kDeviceAndroidId,
1035 kDeviceSecurityToken,
1036 GServicesSettings::CalculateDigest(settings),
1037 settings);
1038
fgorski58b9dfc2014-09-29 16:46:181039 std::vector<GCMClient::AccountTokenInfo> account_tokens;
1040 account_tokens.push_back(MakeAccountToken("[email protected]", "token1"));
1041 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581042
1043 PumpLoopUntilIdle();
1044 CompleteCheckin(kDeviceAndroidId,
1045 kDeviceSecurityToken,
1046 GServicesSettings::CalculateDigest(settings),
1047 settings);
1048
1049 std::set<std::string> accounts;
1050 accounts.insert("[email protected]");
1051 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1052
1053 // This should trigger another checkin, because the list of accounts is
1054 // different.
fgorski58b9dfc2014-09-29 16:46:181055 account_tokens.clear();
1056 account_tokens.push_back(MakeAccountToken("[email protected]", "token2"));
1057 gcm_client()->SetAccountTokens(account_tokens);
[email protected]7df5ef22014-07-17 07:35:581058
1059 PumpLoopUntilIdle();
1060 CompleteCheckin(kDeviceAndroidId,
1061 kDeviceSecurityToken,
1062 GServicesSettings::CalculateDigest(settings),
1063 settings);
1064
1065 accounts.clear();
1066 accounts.insert("[email protected]");
1067 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1068 EXPECT_TRUE(device_checkin_info().accounts_set);
fgorski58b9dfc2014-09-29 16:46:181069 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1070 device_checkin_info().account_tokens);
[email protected]7df5ef22014-07-17 07:35:581071}
1072
[email protected]1abdf202014-06-13 19:38:531073class GCMClientImplStartAndStopTest : public GCMClientImplTest {
1074public:
1075 GCMClientImplStartAndStopTest();
dcheng30a1b1542014-10-29 21:27:501076 ~GCMClientImplStartAndStopTest() override;
[email protected]1abdf202014-06-13 19:38:531077
dcheng30a1b1542014-10-29 21:27:501078 void SetUp() override;
fgorski5df101702014-10-28 02:09:311079
1080 void DefaultCompleteCheckin();
[email protected]1abdf202014-06-13 19:38:531081};
1082
1083GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() {
1084}
1085
1086GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() {
1087}
1088
1089void GCMClientImplStartAndStopTest::SetUp() {
1090 testing::Test::SetUp();
1091 ASSERT_TRUE(CreateUniqueTempDir());
1092 InitializeLoop();
1093 BuildGCMClient(base::TimeDelta());
1094 InitializeGCMClient();
1095}
1096
fgorski5df101702014-10-28 02:09:311097void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() {
1098 SetUpUrlFetcherFactory();
1099 CompleteCheckin(kDeviceAndroidId,
1100 kDeviceSecurityToken,
1101 std::string(),
1102 std::map<std::string, std::string>());
1103 PumpLoopUntilIdle();
1104}
1105
[email protected]1abdf202014-06-13 19:38:531106TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) {
jianlif3e52af42015-01-21 23:18:471107 // GCMClientImpl should be in INITIALIZED state at first.
1108 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1109
1110 // Delay start the GCM.
1111 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531112 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471113 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531114
1115 // Stop the GCM.
1116 gcm_client()->Stop();
1117 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471118 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531119
jianlif3e52af42015-01-21 23:18:471120 // Restart the GCM without delay.
1121 gcm_client()->Start(GCMClient::IMMEDIATE_START);
[email protected]1abdf202014-06-13 19:38:531122 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471123 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531124}
1125
1126TEST_F(GCMClientImplStartAndStopTest, StartAndStopImmediately) {
jianlif3e52af42015-01-21 23:18:471127 // GCMClientImpl should be in INITIALIZED state at first.
1128 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531129
jianlif3e52af42015-01-21 23:18:471130 // Delay start the GCM and then stop it immediately.
1131 gcm_client()->Start(GCMClient::DELAYED_START);
1132 gcm_client()->Stop();
[email protected]1abdf202014-06-13 19:38:531133 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471134 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1135
1136 // Start the GCM and then stop it immediately.
1137 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1138 gcm_client()->Stop();
1139 PumpLoopUntilIdle();
1140 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531141}
1142
1143TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) {
jianlif3e52af42015-01-21 23:18:471144 // GCMClientImpl should be in INITIALIZED state at first.
1145 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531146
jianlif3e52af42015-01-21 23:18:471147 // Delay start the GCM and then stop and restart it immediately.
1148 gcm_client()->Start(GCMClient::DELAYED_START);
1149 gcm_client()->Stop();
1150 gcm_client()->Start(GCMClient::DELAYED_START);
[email protected]1abdf202014-06-13 19:38:531151 PumpLoopUntilIdle();
jianlif3e52af42015-01-21 23:18:471152 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
1153
1154 // Start the GCM and then stop and restart it immediately.
1155 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1156 gcm_client()->Stop();
1157 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1158 PumpLoopUntilIdle();
1159 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1160}
1161
1162TEST_F(GCMClientImplStartAndStopTest, DelayStart) {
1163 // GCMClientImpl should be in INITIALIZED state at first.
1164 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1165
1166 // Delay start the GCM.
1167 gcm_client()->Start(GCMClient::DELAYED_START);
1168 PumpLoopUntilIdle();
1169 EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state());
1170
1171 // Start the GCM immediately and complete the checkin.
1172 gcm_client()->Start(GCMClient::IMMEDIATE_START);
1173 PumpLoopUntilIdle();
1174 EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state());
1175 DefaultCompleteCheckin();
1176 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1177
1178 // Registration.
1179 std::vector<std::string> senders;
1180 senders.push_back("sender");
jianli7a0c9b62015-05-26 23:24:471181 Register(kAppId, senders);
jianlif3e52af42015-01-21 23:18:471182 CompleteRegistration("reg_id");
1183 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
1184
1185 // Stop the GCM.
1186 gcm_client()->Stop();
1187 PumpLoopUntilIdle();
1188 EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state());
1189
1190 // Delay start the GCM. GCM is indeed started without delay because the
1191 // registration record has been found.
1192 gcm_client()->Start(GCMClient::DELAYED_START);
1193 PumpLoopUntilIdle();
1194 EXPECT_EQ(GCMClientImpl::READY, gcm_client_state());
[email protected]1abdf202014-06-13 19:38:531195}
1196
fgorski5df101702014-10-28 02:09:311197// Test for known account mappings and last token fetching time being passed
1198// to OnGCMReady.
1199TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) {
1200 // Start the GCM and wait until it is ready.
jianlif3e52af42015-01-21 23:18:471201 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311202 PumpLoopUntilIdle();
1203 DefaultCompleteCheckin();
1204
1205 base::Time expected_time = base::Time::Now();
1206 gcm_client()->SetLastTokenFetchTime(expected_time);
1207 AccountMapping expected_mapping;
1208 expected_mapping.account_id = "accId";
1209 expected_mapping.email = "[email protected]";
1210 expected_mapping.status = AccountMapping::MAPPED;
1211 expected_mapping.status_change_timestamp = expected_time;
1212 gcm_client()->UpdateAccountMapping(expected_mapping);
1213 PumpLoopUntilIdle();
1214
1215 // Stop the GCM.
1216 gcm_client()->Stop();
1217 PumpLoopUntilIdle();
1218
1219 // Restart the GCM.
jianlif3e52af42015-01-21 23:18:471220 gcm_client()->Start(GCMClient::IMMEDIATE_START);
fgorski5df101702014-10-28 02:09:311221 PumpLoopUntilIdle();
1222
1223 EXPECT_EQ(LOADING_COMPLETED, last_event());
1224 EXPECT_EQ(expected_time, last_token_fetch_time());
1225 ASSERT_EQ(1UL, last_account_mappings().size());
1226 const AccountMapping& actual_mapping = last_account_mappings()[0];
1227 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id);
1228 EXPECT_EQ(expected_mapping.email, actual_mapping.email);
1229 EXPECT_EQ(expected_mapping.status, actual_mapping.status);
1230 EXPECT_EQ(expected_mapping.status_change_timestamp,
1231 actual_mapping.status_change_timestamp);
1232}
1233
jianlic02d25e2015-05-27 22:24:311234
1235class GCMClientInstanceIDTest : public GCMClientImplTest {
1236 public:
1237 GCMClientInstanceIDTest();
1238 ~GCMClientInstanceIDTest() override;
1239
1240 void AddInstanceID(const std::string& app_id,
1241 const std::string& instance_id);
1242 void RemoveInstanceID(const std::string& app_id);
1243 void GetToken(const std::string& app_id,
1244 const std::string& authorized_entity,
1245 const std::string& scope);
1246 void DeleteToken(const std::string& app_id,
1247 const std::string& authorized_entity,
1248 const std::string& scope);
1249 void CompleteDeleteToken();
1250 bool ExistsToken(const std::string& app_id,
1251 const std::string& authorized_entity,
1252 const std::string& scope) const;
1253};
1254
1255GCMClientInstanceIDTest::GCMClientInstanceIDTest() {
1256}
1257
1258GCMClientInstanceIDTest::~GCMClientInstanceIDTest() {
1259}
1260
1261void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id,
1262 const std::string& instance_id) {
1263 gcm_client()->AddInstanceIDData(app_id, instance_id, "123");
1264}
1265
1266void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) {
1267 gcm_client()->RemoveInstanceIDData(app_id);
1268}
1269
1270void GCMClientInstanceIDTest::GetToken(const std::string& app_id,
1271 const std::string& authorized_entity,
1272 const std::string& scope) {
1273 scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo);
1274 instance_id_info->app_id = app_id;
1275 instance_id_info->authorized_entity = authorized_entity;
1276 instance_id_info->scope = scope;
1277 gcm_client()->Register(
1278 make_linked_ptr<RegistrationInfo>(instance_id_info.release()));
1279}
1280
1281void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id,
1282 const std::string& authorized_entity,
1283 const std::string& scope) {
1284 scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo);
1285 instance_id_info->app_id = app_id;
1286 instance_id_info->authorized_entity = authorized_entity;
1287 instance_id_info->scope = scope;
1288 gcm_client()->Unregister(
1289 make_linked_ptr<RegistrationInfo>(instance_id_info.release()));
1290}
1291
1292void GCMClientInstanceIDTest::CompleteDeleteToken() {
1293 std::string response(kDeleteTokenResponse);
1294 net::TestURLFetcher* fetcher = url_fetcher_factory()->GetFetcherByID(0);
1295 ASSERT_TRUE(fetcher);
1296 fetcher->set_response_code(net::HTTP_OK);
1297 fetcher->SetResponseString(response);
1298 fetcher->delegate()->OnURLFetchComplete(fetcher);
1299}
1300
1301bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id,
1302 const std::string& authorized_entity,
1303 const std::string& scope) const {
1304 scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo);
1305 instance_id_info->app_id = app_id;
1306 instance_id_info->authorized_entity = authorized_entity;
1307 instance_id_info->scope = scope;
1308 return gcm_client()->registrations_.count(
1309 make_linked_ptr<RegistrationInfo>(instance_id_info.release())) > 0;
1310}
1311
1312TEST_F(GCMClientInstanceIDTest, GetToken) {
1313 AddInstanceID(kAppId, kInstanceID);
1314
1315 // Get a token.
1316 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1317 GetToken(kAppId, kSender, kScope);
1318 CompleteRegistration("token1");
1319
1320 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1321 EXPECT_EQ(kAppId, last_app_id());
1322 EXPECT_EQ("token1", last_registration_id());
1323 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1324 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1325
1326 // Get another token.
1327 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1328 GetToken(kAppId, kSender2, kScope);
1329 CompleteRegistration("token2");
1330
1331 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1332 EXPECT_EQ(kAppId, last_app_id());
1333 EXPECT_EQ("token2", last_registration_id());
1334 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1335 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope));
1336 // The 1st token still exists.
1337 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1338}
1339
1340TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) {
1341 AddInstanceID(kAppId, kInstanceID);
1342
1343 // Get a token.
1344 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1345 GetToken(kAppId, kSender, kScope);
1346 CompleteRegistration("token1");
1347
1348 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1349 EXPECT_EQ(kAppId, last_app_id());
1350 EXPECT_EQ("token1", last_registration_id());
1351 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1352 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1353
1354 // Get another token.
1355 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1356 GetToken(kAppId, kSender2, kScope);
1357 CompleteRegistration("token2");
1358
1359 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1360 EXPECT_EQ(kAppId, last_app_id());
1361 EXPECT_EQ("token2", last_registration_id());
1362 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1363 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope));
1364 // The 1st token still exists.
1365 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1366
1367 // Delete the 2nd token.
1368 DeleteToken(kAppId, kSender2, kScope);
1369 CompleteDeleteToken();
1370
1371 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1372 EXPECT_EQ(kAppId, last_app_id());
1373 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1374 // The 2nd token is gone while the 1st token still exists.
1375 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1376 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1377
1378 // Delete the 1st token.
1379 DeleteToken(kAppId, kSender, kScope);
1380 CompleteDeleteToken();
1381
1382 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1383 EXPECT_EQ(kAppId, last_app_id());
1384 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1385 // Both tokens are gone now.
1386 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1387 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1388}
1389
1390TEST_F(GCMClientInstanceIDTest, DeleteMultiTokens) {
1391 AddInstanceID(kAppId, kInstanceID);
1392
1393 // Get a token.
1394 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1395 GetToken(kAppId, kSender, kScope);
1396 CompleteRegistration("token1");
1397
1398 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1399 EXPECT_EQ(kAppId, last_app_id());
1400 EXPECT_EQ("token1", last_registration_id());
1401 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1402 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1403
1404 // Get another token.
1405 EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope));
1406 GetToken(kAppId, kSender2, kScope);
1407 CompleteRegistration("token2");
1408
1409 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
1410 EXPECT_EQ(kAppId, last_app_id());
1411 EXPECT_EQ("token2", last_registration_id());
1412 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1413 EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope));
1414 // The 1st token still exists.
1415 EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope));
1416
1417 // Delete all tokens.
1418 DeleteToken(kAppId, "*", "*");
1419 CompleteDeleteToken();
1420
1421 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1422 EXPECT_EQ(kAppId, last_app_id());
1423 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1424 // All tokens are gone now.
1425 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1426 EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope));
1427}
1428
[email protected]848b1b62014-01-30 23:51:041429} // namespace gcm