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
|
// 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 "base/run_loop.h"
#include "base/strings/string_util.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "sync/notifier/gcm_network_channel.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
class TestGCMNetworkChannelDelegate : public GCMNetworkChannelDelegate {
public:
TestGCMNetworkChannelDelegate()
: register_call_count_(0) {}
virtual void Initialize() OVERRIDE {}
virtual void RequestToken(RequestTokenCallback callback) OVERRIDE {
request_token_callback = callback;
}
virtual void InvalidateToken(const std::string& token) OVERRIDE {
invalidated_token = token;
}
virtual void Register(RegisterCallback callback) OVERRIDE {
++register_call_count_;
register_callback = callback;
}
RequestTokenCallback request_token_callback;
std::string invalidated_token;
RegisterCallback register_callback;
int register_call_count_;
};
// Backoff policy for test. Run first 5 retries without delay.
const net::BackoffEntry::Policy kTestBackoffPolicy = {
// Number of initial errors (in sequence) to ignore before applying
// exponential back-off rules.
5,
// Initial delay for exponential back-off in ms.
2000, // 2 seconds.
// Factor by which the waiting time will be multiplied.
2,
// Fuzzing percentage. ex: 10% will spread requests randomly
// between 90%-100% of the calculated time.
0.2, // 20%.
// Maximum amount of time we are willing to delay our request in ms.
1000 * 3600 * 4, // 4 hours.
// Time to keep an entry from being discarded even when it
// has no significant state, -1 to never discard.
-1,
// Don't use initial delay unless the last request was an error.
false,
};
class TestGCMNetworkChannel : public GCMNetworkChannel {
public:
TestGCMNetworkChannel(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
scoped_ptr<GCMNetworkChannelDelegate> delegate)
: GCMNetworkChannel(request_context_getter, delegate.Pass()) {
ResetRegisterBackoffEntryForTest(&kTestBackoffPolicy);
}
protected:
// On Android GCMNetworkChannel::BuildUrl hits NOTREACHED(). I still want
// tests to run.
virtual GURL BuildUrl(const std::string& registration_id) OVERRIDE {
return GURL("http://test.url.com");
}
};
class GCMNetworkChannelTest
: public ::testing::Test,
public SyncNetworkChannel::Observer {
protected:
GCMNetworkChannelTest()
: delegate_(NULL),
url_fetchers_created_count_(0) {
}
virtual ~GCMNetworkChannelTest() {
}
virtual void SetUp() {
request_context_getter_ = new net::TestURLRequestContextGetter(
base::MessageLoopProxy::current());
// Ownership of delegate goes to GCNMentworkChannel but test needs pointer
// to it.
delegate_ = new TestGCMNetworkChannelDelegate();
scoped_ptr<GCMNetworkChannelDelegate> delegate(delegate_);
gcm_network_channel_.reset(new TestGCMNetworkChannel(
request_context_getter_,
delegate.Pass()));
gcm_network_channel_->AddObserver(this);
gcm_network_channel_->SetMessageReceiver(
invalidation::NewPermanentCallback(
this, &GCMNetworkChannelTest::OnIncomingMessage));
url_fetcher_factory_.reset(new net::FakeURLFetcherFactory(NULL,
base::Bind(&GCMNetworkChannelTest::CreateURLFetcher,
base::Unretained(this))));
}
virtual void TearDown() {
gcm_network_channel_->RemoveObserver(this);
}
// Helper functions to call private methods from test
GURL BuildUrl(const std::string& registration_id) {
return gcm_network_channel_->GCMNetworkChannel::BuildUrl(registration_id);
}
static void Base64EncodeURLSafe(const std::string& input,
std::string* output) {
GCMNetworkChannel::Base64EncodeURLSafe(input, output);
}
static bool Base64DecodeURLSafe(const std::string& input,
std::string* output) {
return GCMNetworkChannel::Base64DecodeURLSafe(input, output);
}
virtual void OnNetworkChannelStateChanged(
InvalidatorState invalidator_state) OVERRIDE {
}
void OnIncomingMessage(std::string incoming_message) {
}
GCMNetworkChannel* network_channel() {
return gcm_network_channel_.get();
}
TestGCMNetworkChannelDelegate* delegate() {
return delegate_;
}
int url_fetchers_created_count() {
return url_fetchers_created_count_;
}
net::FakeURLFetcherFactory* url_fetcher_factory() {
return url_fetcher_factory_.get();
}
scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
const GURL& url,
net::URLFetcherDelegate* delegate,
const std::string& response_data,
net::HttpStatusCode response_code,
net::URLRequestStatus::Status status) {
++url_fetchers_created_count_;
return scoped_ptr<net::FakeURLFetcher>(new net::FakeURLFetcher(
url, delegate, response_data, response_code, status));
}
void RunLoopUntilIdle() {
base::RunLoop run_loop;
run_loop.RunUntilIdle();
}
private:
base::MessageLoop message_loop_;
TestGCMNetworkChannelDelegate* delegate_;
scoped_ptr<GCMNetworkChannel> gcm_network_channel_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
int url_fetchers_created_count_;
};
TEST_F(GCMNetworkChannelTest, HappyCase) {
url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
std::string(),
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
// After construction GCMNetworkChannel should have called Register.
EXPECT_FALSE(delegate()->register_callback.is_null());
// Return valid registration id.
delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
network_channel()->SendMessage("abra.cadabra");
// SendMessage should have triggered RequestToken. No HTTP request should be
// started yet.
EXPECT_FALSE(delegate()->request_token_callback.is_null());
EXPECT_EQ(url_fetchers_created_count(), 0);
// Return valid access token. This should trigger HTTP request.
delegate()->request_token_callback.Run(
GoogleServiceAuthError::AuthErrorNone(), "access.token");
RunLoopUntilIdle();
EXPECT_EQ(url_fetchers_created_count(), 1);
// Return another access token. Message should be cleared by now and shouldn't
// be sent.
delegate()->request_token_callback.Run(
GoogleServiceAuthError::AuthErrorNone(), "access.token2");
RunLoopUntilIdle();
EXPECT_EQ(url_fetchers_created_count(), 1);
}
TEST_F(GCMNetworkChannelTest, FailedRegister) {
// After construction GCMNetworkChannel should have called Register.
EXPECT_FALSE(delegate()->register_callback.is_null());
EXPECT_EQ(1, delegate()->register_call_count_);
// Return transient error from Register call.
delegate()->register_callback.Run("", gcm::GCMClient::NETWORK_ERROR);
RunLoopUntilIdle();
// GcmNetworkChannel should have scheduled Register retry.
EXPECT_EQ(2, delegate()->register_call_count_);
// Return persistent error from Register call.
delegate()->register_callback.Run("", gcm::GCMClient::NOT_SIGNED_IN);
RunLoopUntilIdle();
// GcmNetworkChannel should give up trying.
EXPECT_EQ(2, delegate()->register_call_count_);
network_channel()->SendMessage("abra.cadabra");
// SendMessage shouldn't trigger RequestToken.
EXPECT_TRUE(delegate()->request_token_callback.is_null());
EXPECT_EQ(0, url_fetchers_created_count());
}
TEST_F(GCMNetworkChannelTest, RegisterFinishesAfterSendMessage) {
url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
"",
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
// After construction GCMNetworkChannel should have called Register.
EXPECT_FALSE(delegate()->register_callback.is_null());
network_channel()->SendMessage("abra.cadabra");
// SendMessage shouldn't trigger RequestToken.
EXPECT_TRUE(delegate()->request_token_callback.is_null());
EXPECT_EQ(url_fetchers_created_count(), 0);
// Return valid registration id.
delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
EXPECT_FALSE(delegate()->request_token_callback.is_null());
EXPECT_EQ(url_fetchers_created_count(), 0);
// Return valid access token. This should trigger HTTP request.
delegate()->request_token_callback.Run(
GoogleServiceAuthError::AuthErrorNone(), "access.token");
RunLoopUntilIdle();
EXPECT_EQ(url_fetchers_created_count(), 1);
}
TEST_F(GCMNetworkChannelTest, RequestTokenFailure) {
// After construction GCMNetworkChannel should have called Register.
EXPECT_FALSE(delegate()->register_callback.is_null());
// Return valid registration id.
delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
network_channel()->SendMessage("abra.cadabra");
// SendMessage should have triggered RequestToken. No HTTP request should be
// started yet.
EXPECT_FALSE(delegate()->request_token_callback.is_null());
EXPECT_EQ(url_fetchers_created_count(), 0);
// RequestToken returns failure.
delegate()->request_token_callback.Run(
GoogleServiceAuthError::FromConnectionError(1), "");
// Should be no HTTP requests.
EXPECT_EQ(url_fetchers_created_count(), 0);
}
TEST_F(GCMNetworkChannelTest, AuthErrorFromServer) {
// Setup fake response to return AUTH_ERROR.
url_fetcher_factory()->SetFakeResponse(GURL("http://test.url.com"),
"",
net::HTTP_UNAUTHORIZED,
net::URLRequestStatus::SUCCESS);
// After construction GCMNetworkChannel should have called Register.
EXPECT_FALSE(delegate()->register_callback.is_null());
// Return valid registration id.
delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
network_channel()->SendMessage("abra.cadabra");
// SendMessage should have triggered RequestToken. No HTTP request should be
// started yet.
EXPECT_FALSE(delegate()->request_token_callback.is_null());
EXPECT_EQ(url_fetchers_created_count(), 0);
// Return valid access token. This should trigger HTTP request.
delegate()->request_token_callback.Run(
GoogleServiceAuthError::AuthErrorNone(), "access.token");
RunLoopUntilIdle();
EXPECT_EQ(url_fetchers_created_count(), 1);
EXPECT_EQ(delegate()->invalidated_token, "access.token");
}
// Following two tests are to check for memory leaks/crashes when Register and
// RequestToken don't complete by the teardown.
TEST_F(GCMNetworkChannelTest, RegisterNeverCompletes) {
network_channel()->SendMessage("abra.cadabra");
// Register should be called by now. Let's not complete and see what happens.
EXPECT_FALSE(delegate()->register_callback.is_null());
}
TEST_F(GCMNetworkChannelTest, RequestTokenNeverCompletes) {
network_channel()->SendMessage("abra.cadabra");
// Return valid registration id.
delegate()->register_callback.Run("registration.id", gcm::GCMClient::SUCCESS);
// RequestToken should be called by now. Let's not complete and see what
// happens.
EXPECT_FALSE(delegate()->request_token_callback.is_null());
}
#if !defined(ANDROID)
TEST_F(GCMNetworkChannelTest, BuildUrl) {
GURL url = BuildUrl("registration.id");
EXPECT_TRUE(url.SchemeIsHTTPOrHTTPS());
EXPECT_FALSE(url.host().empty());
EXPECT_FALSE(url.path().empty());
std::vector<std::string> parts;
Tokenize(url.path(), "/", &parts);
std::string buffer;
EXPECT_TRUE(Base64DecodeURLSafe(parts[parts.size() - 1], &buffer));
}
#endif
TEST_F(GCMNetworkChannelTest, Base64EncodeDecode) {
std::string input;
std::string plain;
std::string base64;
// Empty string.
Base64EncodeURLSafe(input, &base64);
EXPECT_TRUE(base64.empty());
EXPECT_TRUE(Base64DecodeURLSafe(base64, &plain));
EXPECT_EQ(input, plain);
// String length: 1..7.
for (int length = 1; length < 8; length++) {
input = "abra.cadabra";
input.resize(length);
Base64EncodeURLSafe(input, &base64);
// Ensure no padding at the end.
EXPECT_NE(base64[base64.size() - 1], '=');
EXPECT_TRUE(Base64DecodeURLSafe(base64, &plain));
EXPECT_EQ(input, plain);
}
// Presence of '-', '_'.
input = "\xfb\xff";
Base64EncodeURLSafe(input, &base64);
EXPECT_EQ("-_8", base64);
EXPECT_TRUE(Base64DecodeURLSafe(base64, &plain));
EXPECT_EQ(input, plain);
}
} // namespace syncer
|