1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/gcm/gcm_client_impl.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/simple_test_clock.h"
#include "components/webdata/encryptor/encryptor.h"
#include "google_apis/gcm/base/mcs_message.h"
#include "google_apis/gcm/base/mcs_util.h"
#include "google_apis/gcm/engine/fake_connection_factory.h"
#include "google_apis/gcm/engine/fake_connection_handler.h"
#include "google_apis/gcm/protocol/android_checkin.pb.h"
#include "google_apis/gcm/protocol/checkin.pb.h"
#include "google_apis/gcm/protocol/mcs.pb.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gcm {
namespace {
enum LastEvent {
NONE,
LOADING_COMPLETED,
CHECKIN_COMPLETED,
REGISTRATION_COMPLETED,
MESSAGE_SEND_ERROR,
MESSAGE_RECEIVED,
MESSAGES_DELETED,
};
const uint64 kDeviceAndroidId = 54321;
const uint64 kDeviceSecurityToken = 12345;
const char kRegistrationResponsePrefix[] = "token=";
const char kUsername[] = "test";
const uint64 kUserAndroidId = 67890;
const uint64 kUserSecurityToken = 9876;
// Helper for building arbitrary data messages.
MCSMessage BuildDownstreamMessage(
const std::string& project_id,
const std::string& app_id,
const std::map<std::string, std::string>& data) {
mcs_proto::DataMessageStanza data_message;
data_message.set_device_user_id(1);
data_message.set_from(project_id);
data_message.set_category(app_id);
for (std::map<std::string, std::string>::const_iterator iter = data.begin();
iter != data.end();
++iter) {
mcs_proto::AppData* app_data = data_message.add_app_data();
app_data->set_key(iter->first);
app_data->set_value(iter->second);
}
return MCSMessage(kDataMessageStanzaTag, data_message);
}
class FakeMCSClient : public MCSClient {
public:
FakeMCSClient(base::Clock* clock,
ConnectionFactory* connection_factory);
virtual ~FakeMCSClient();
virtual void Login(uint64 android_id,
uint64 security_token,
const std::vector<int64>& user_serial_numbers) OVERRIDE;
virtual void SendMessage(const MCSMessage& message) OVERRIDE;
void set_gcm_store(GCMStore* gcm_store);
uint64 last_android_id() const { return last_android_id_; }
uint64 last_security_token() const { return last_security_token_; }
uint8 last_message_tag() const { return last_message_tag_; }
const mcs_proto::DataMessageStanza& last_data_message_stanza() const {
return last_data_message_stanza_;
}
private:
uint64 last_android_id_;
uint64 last_security_token_;
uint8 last_message_tag_;
mcs_proto::DataMessageStanza last_data_message_stanza_;
};
FakeMCSClient::FakeMCSClient(base::Clock* clock,
ConnectionFactory* connection_factory)
: MCSClient(clock, connection_factory, NULL),
last_android_id_(0u),
last_security_token_(0u),
last_message_tag_(kNumProtoTypes) {
}
FakeMCSClient::~FakeMCSClient() {
}
void FakeMCSClient::set_gcm_store(GCMStore* gcm_store) {
SetGCMStoreForTesting(gcm_store);
}
void FakeMCSClient::Login(uint64 android_id,
uint64 security_token,
const std::vector<int64>& user_serial_numbers) {
last_android_id_ = android_id;
last_security_token_ = security_token;
}
void FakeMCSClient::SendMessage(const MCSMessage& message) {
last_message_tag_ = message.tag();
if (last_message_tag_ == kDataMessageStanzaTag) {
last_data_message_stanza_.CopyFrom(
reinterpret_cast<const mcs_proto::DataMessageStanza&>(
message.GetProtobuf()));
}
}
} // namespace
class GCMClientImplTest : public testing::Test,
public GCMClient::Delegate {
public:
GCMClientImplTest();
virtual ~GCMClientImplTest();
virtual void SetUp() OVERRIDE;
void BuildGCMClient();
void InitializeGCMClient();
void ReceiveMessageFromMCS(const MCSMessage& message);
void CompleteCheckin(uint64 android_id, uint64 security_token);
void CompleteRegistration(const std::string& registration_id);
// GCMClient::Delegate overrides (for verification).
virtual void OnCheckInFinished(const GCMClient::CheckinInfo& checkin_info,
GCMClient::Result result) OVERRIDE;
virtual void OnRegisterFinished(const std::string& app_id,
const std::string& registration_id,
GCMClient::Result result) OVERRIDE;
virtual void OnSendFinished(const std::string& app_id,
const std::string& message_id,
GCMClient::Result result) OVERRIDE {}
virtual void OnMessageReceived(const std::string& registration_id,
const GCMClient::IncomingMessage& message)
OVERRIDE;
virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
virtual void OnMessageSendError(const std::string& app_id,
const std::string& message_id,
GCMClient::Result result) OVERRIDE;
virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE {
return checkin_info_;
}
virtual void OnGCMReady() OVERRIDE;
void SetCheckinInfo(uint64 android_id, uint64 security_token);
GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
FakeMCSClient* mcs_client() const {
return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
}
LastEvent last_event() const { return last_event_; }
const std::string& last_app_id() const { return last_app_id_; }
const std::string& last_registration_id() const {
return last_registration_id_;
}
const std::string& last_message_id() const { return last_message_id_; }
GCMClient::Result last_result() const { return last_result_; }
const GCMClient::CheckinInfo& last_checkin_info() const {
return last_checkin_info_;
}
const GCMClient::IncomingMessage& last_message() const {
return last_message_;
}
int64 CurrentTime();
private:
// Tooling.
void PumpLoop();
void PumpLoopUntilIdle();
void QuitLoop();
base::SimpleTestClock* clock() const {
return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get());
}
// Variables used for verification.
LastEvent last_event_;
std::string last_app_id_;
std::string last_registration_id_;
std::string last_message_id_;
GCMClient::Result last_result_;
GCMClient::CheckinInfo last_checkin_info_;
GCMClient::IncomingMessage last_message_;
scoped_ptr<GCMClientImpl> gcm_client_;
GCMClient::CheckinInfo checkin_info_;
scoped_ptr<FakeConnectionFactory> connection_factory_;
base::MessageLoop message_loop_;
scoped_ptr<base::RunLoop> run_loop_;
net::TestURLFetcherFactory url_fetcher_factory_;
// Injected to GCM client:
base::ScopedTempDir temp_directory_;
scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
};
GCMClientImplTest::GCMClientImplTest()
: last_event_(NONE),
last_result_(GCMClient::UNKNOWN_ERROR),
url_request_context_getter_(new net::TestURLRequestContextGetter(
message_loop_.message_loop_proxy())) {
}
GCMClientImplTest::~GCMClientImplTest() {}
void GCMClientImplTest::SetUp() {
ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
run_loop_.reset(new base::RunLoop);
BuildGCMClient();
InitializeGCMClient();
gcm_client()->SetUserDelegate(kUsername, this);
PumpLoop();
}
void GCMClientImplTest::PumpLoop() {
run_loop_->Run();
run_loop_.reset(new base::RunLoop());
}
void GCMClientImplTest::PumpLoopUntilIdle() {
run_loop_->RunUntilIdle();
run_loop_.reset(new base::RunLoop());
}
void GCMClientImplTest::QuitLoop() {
if (run_loop_ && run_loop_->running())
run_loop_->Quit();
}
void GCMClientImplTest::BuildGCMClient() {
gcm_client_.reset(new GCMClientImpl());
}
void GCMClientImplTest::CompleteCheckin(uint64 android_id,
uint64 security_token) {
checkin_proto::AndroidCheckinResponse response;
response.set_stats_ok(true);
response.set_android_id(android_id);
response.set_security_token(security_token);
std::string response_string;
response.SerializeToString(&response_string);
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(response_string);
fetcher->delegate()->OnURLFetchComplete(fetcher);
url_fetcher_factory_.RemoveFetcherFromMap(0);
}
void GCMClientImplTest::CompleteRegistration(
const std::string& registration_id) {
std::string response(kRegistrationResponsePrefix);
response.append(registration_id);
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
fetcher->SetResponseString(response);
fetcher->delegate()->OnURLFetchComplete(fetcher);
url_fetcher_factory_.RemoveFetcherFromMap(0);
}
void GCMClientImplTest::InitializeGCMClient() {
// Creating and advancing the clock.
gcm_client_->clock_.reset(new base::SimpleTestClock);
clock()->Advance(base::TimeDelta::FromMilliseconds(1));
// Creating and injecting the mcs_client.
connection_factory_.reset(new FakeConnectionFactory());
gcm_client_->SetMCSClientForTesting(scoped_ptr<MCSClient>(
new FakeMCSClient(clock(), connection_factory_.get())).Pass());
// Actual initialization.
checkin_proto::ChromeBuildProto chrome_build_proto;
gcm_client_->Initialize(chrome_build_proto,
temp_directory_.path(),
message_loop_.message_loop_proxy(),
url_request_context_getter_);
#if defined(OS_MACOSX)
// On OSX, prevent the Keychain permissions popup during unit tests.
Encryptor::UseMockKeychain(true); // Must be after Initialize.
#endif
// Ensuring that mcs_client is using the same gcm_store as gcm_client.
mcs_client()->set_gcm_store(gcm_client_->gcm_store_.get());
PumpLoopUntilIdle();
CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken);
PumpLoopUntilIdle();
}
void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
gcm_client_->OnMessageReceivedFromMCS(message);
}
void GCMClientImplTest::OnGCMReady() {
last_event_ = LOADING_COMPLETED;
QuitLoop();
}
void GCMClientImplTest::OnCheckInFinished(
const GCMClient::CheckinInfo& checkin_info,
GCMClient::Result result) {
last_event_ = CHECKIN_COMPLETED;
last_checkin_info_.android_id = checkin_info.android_id;
last_checkin_info_.secret = checkin_info.secret;
last_result_ = result;
}
void GCMClientImplTest::OnMessageReceived(
const std::string& registration_id,
const GCMClient::IncomingMessage& message) {
last_event_ = MESSAGE_RECEIVED;
last_app_id_ = registration_id;
last_message_ = message;
QuitLoop();
}
void GCMClientImplTest::OnRegisterFinished(const std::string& app_id,
const std::string& registration_id,
GCMClient::Result result) {
last_event_ = REGISTRATION_COMPLETED;
last_app_id_ = app_id;
last_registration_id_ = registration_id;
last_result_ = result;
}
void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
last_event_ = MESSAGES_DELETED;
last_app_id_ = app_id;
}
void GCMClientImplTest::OnMessageSendError(const std::string& app_id,
const std::string& message_id,
GCMClient::Result result) {
last_event_ = MESSAGE_SEND_ERROR;
last_app_id_ = app_id;
last_message_id_ = message_id;
last_result_ = result;
}
int64 GCMClientImplTest::CurrentTime() {
return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
}
void GCMClientImplTest::SetCheckinInfo(
uint64 android_id,
uint64 security_token) {
checkin_info_.android_id = android_id;
checkin_info_.secret = security_token;
}
TEST_F(GCMClientImplTest, LoadingCompleted) {
EXPECT_EQ(LOADING_COMPLETED, last_event());
EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
}
TEST_F(GCMClientImplTest, CheckInUser) {
gcm_client()->CheckIn(kUsername);
CompleteCheckin(kUserAndroidId, kUserSecurityToken);
EXPECT_EQ(CHECKIN_COMPLETED, last_event());
EXPECT_EQ(kUserAndroidId, last_checkin_info().android_id);
EXPECT_EQ(kUserSecurityToken, last_checkin_info().secret);
EXPECT_EQ(GCMClient::SUCCESS, last_result());
}
TEST_F(GCMClientImplTest, RegisterApp) {
std::vector<std::string> senders;
senders.push_back("sender");
SetCheckinInfo(kUserAndroidId, kUserSecurityToken);
gcm_client()->Register(kUsername, "app_id", "cert", senders);
CompleteRegistration("reg_id");
EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
EXPECT_EQ("app_id", last_app_id());
EXPECT_EQ("reg_id", last_registration_id());
EXPECT_EQ(GCMClient::SUCCESS, last_result());
}
TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
std::map<std::string, std::string> expected_data;
expected_data["message_type"] = "gcm";
expected_data["key"] = "value";
expected_data["key2"] = "value2";
MCSMessage message(BuildDownstreamMessage(
"project_id", "app_id", expected_data));
EXPECT_TRUE(message.IsValid());
ReceiveMessageFromMCS(message);
expected_data.erase(expected_data.find("message_type"));
EXPECT_EQ(MESSAGE_RECEIVED, last_event());
EXPECT_EQ("app_id", last_app_id());
EXPECT_EQ(expected_data.size(), last_message().data.size());
EXPECT_EQ(expected_data, last_message().data);
}
TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) {
std::map<std::string, std::string> expected_data;
expected_data["message_type"] = "send_error";
expected_data["google.message_id"] = "007";
MCSMessage message(BuildDownstreamMessage(
"project_id", "app_id", expected_data));
EXPECT_TRUE(message.IsValid());
ReceiveMessageFromMCS(message);
EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
EXPECT_EQ("app_id", last_app_id());
EXPECT_EQ("007", last_message_id());
}
TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
std::map<std::string, std::string> expected_data;
expected_data["message_type"] = "deleted_messages";
MCSMessage message(BuildDownstreamMessage(
"project_id", "app_id", expected_data));
EXPECT_TRUE(message.IsValid());
ReceiveMessageFromMCS(message);
EXPECT_EQ(MESSAGES_DELETED, last_event());
EXPECT_EQ("app_id", last_app_id());
}
TEST_F(GCMClientImplTest, SendMessage) {
mcs_proto::DataMessageStanza stanza;
stanza.set_ttl(500);
GCMClient::OutgoingMessage message;
message.id = "007";
message.time_to_live = 500;
message.data["key"] = "value";
gcm_client()->Send(kUsername, "app_id", "project_id", message);
EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
EXPECT_EQ("app_id", mcs_client()->last_data_message_stanza().category());
EXPECT_EQ("project_id", mcs_client()->last_data_message_stanza().to());
EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
EXPECT_EQ(1, mcs_client()->last_data_message_stanza().device_user_id());
EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from());
EXPECT_EQ("project_id", mcs_client()->last_data_message_stanza().to());
EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
EXPECT_EQ("value",
mcs_client()->last_data_message_stanza().app_data(0).value());
}
} // namespace gcm
|