Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: google_apis/gcm/engine/registration_request_unittest.cc

Issue 183923006: [GCM] API update to allow only a single sender in registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updates based on CR. Changing how the senders/reg_ids are stored to avoid upgrade to multiple sedne… Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_tokenizer.h" 10 #include "base/strings/string_tokenizer.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 RegistrationRequestTest::~RegistrationRequestTest() {} 93 RegistrationRequestTest::~RegistrationRequestTest() {}
94 94
95 void RegistrationRequestTest::RegistrationCallback( 95 void RegistrationRequestTest::RegistrationCallback(
96 RegistrationRequest::Status status, 96 RegistrationRequest::Status status,
97 const std::string& registration_id) { 97 const std::string& registration_id) {
98 status_ = status; 98 status_ = status;
99 registration_id_ = registration_id; 99 registration_id_ = registration_id;
100 callback_called_ = true; 100 callback_called_ = true;
101 } 101 }
102 102
103 void RegistrationRequestTest::CreateRequest(const std::string& sender_ids) { 103 void RegistrationRequestTest::CreateRequest(const std::string& sender_id) {
104 std::vector<std::string> senders;
105 base::StringTokenizer tokenizer(sender_ids, ",");
106 while (tokenizer.GetNext())
107 senders.push_back(tokenizer.token());
108
109 request_.reset(new RegistrationRequest( 104 request_.reset(new RegistrationRequest(
110 RegistrationRequest::RequestInfo(kAndroidId, 105 RegistrationRequest::RequestInfo(kAndroidId,
111 kSecurityToken, 106 kSecurityToken,
112 kAppId, 107 kAppId,
113 kCert, 108 kCert,
114 senders), 109 sender_id),
115 kDefaultBackoffPolicy, 110 kDefaultBackoffPolicy,
116 base::Bind(&RegistrationRequestTest::RegistrationCallback, 111 base::Bind(&RegistrationRequestTest::RegistrationCallback,
117 base::Unretained(this)), 112 base::Unretained(this)),
118 max_retry_count_, 113 max_retry_count_,
119 url_request_context_getter_.get())); 114 url_request_context_getter_.get()));
120 } 115 }
121 116
122 void RegistrationRequestTest::SetResponseStatusAndString( 117 void RegistrationRequestTest::SetResponseStatusAndString(
123 net::HttpStatusCode status_code, 118 net::HttpStatusCode status_code,
124 const std::string& response_body) { 119 const std::string& response_body) {
125 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 120 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
126 ASSERT_TRUE(fetcher); 121 ASSERT_TRUE(fetcher);
127 fetcher->set_response_code(status_code); 122 fetcher->set_response_code(status_code);
128 fetcher->SetResponseString(response_body); 123 fetcher->SetResponseString(response_body);
129 } 124 }
130 125
131 void RegistrationRequestTest::CompleteFetch() { 126 void RegistrationRequestTest::CompleteFetch() {
132 registration_id_.clear(); 127 registration_id_.clear();
133 status_ = RegistrationRequest::SUCCESS; 128 status_ = RegistrationRequest::SUCCESS;
134 callback_called_ = false; 129 callback_called_ = false;
135 130
136 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 131 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
137 ASSERT_TRUE(fetcher); 132 ASSERT_TRUE(fetcher);
138 fetcher->delegate()->OnURLFetchComplete(fetcher); 133 fetcher->delegate()->OnURLFetchComplete(fetcher);
139 } 134 }
140 135
141 TEST_F(RegistrationRequestTest, RequestSuccessful) { 136 TEST_F(RegistrationRequestTest, RequestSuccessful) {
142 set_max_retry_count(0); 137 set_max_retry_count(0);
143 CreateRequest("sender1,sender2"); 138 CreateRequest(kDeveloperId);
144 request_->Start(); 139 request_->Start();
145 140
146 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 141 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
147 CompleteFetch(); 142 CompleteFetch();
148 143
149 EXPECT_TRUE(callback_called_); 144 EXPECT_TRUE(callback_called_);
150 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 145 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
151 EXPECT_EQ("2501", registration_id_); 146 EXPECT_EQ("2501", registration_id_);
152 } 147 }
153 148
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 ASSERT_TRUE(iter != expected_pairs.end()); 182 ASSERT_TRUE(iter != expected_pairs.end());
188 ASSERT_TRUE(data_tokenizer.GetNext()); 183 ASSERT_TRUE(data_tokenizer.GetNext());
189 EXPECT_EQ(iter->second, data_tokenizer.token()); 184 EXPECT_EQ(iter->second, data_tokenizer.token());
190 // Ensure that none of the keys appears twice. 185 // Ensure that none of the keys appears twice.
191 expected_pairs.erase(iter); 186 expected_pairs.erase(iter);
192 } 187 }
193 188
194 EXPECT_EQ(0UL, expected_pairs.size()); 189 EXPECT_EQ(0UL, expected_pairs.size());
195 } 190 }
196 191
197 TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) {
198 CreateRequest("sender1,sender2@gmail.com");
199 request_->Start();
200
201 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
202 ASSERT_TRUE(fetcher);
203
204 // Verify data was formatted properly.
205 std::string upload_data = fetcher->upload_data();
206 base::StringTokenizer data_tokenizer(upload_data, "&=");
207
208 // Skip all tokens until you hit entry for senders.
209 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender")
210 continue;
211
212 ASSERT_TRUE(data_tokenizer.GetNext());
213 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(),
214 net::UnescapeRule::URL_SPECIAL_CHARS));
215 base::StringTokenizer sender_tokenizer(senders, ",");
216 ASSERT_TRUE(sender_tokenizer.GetNext());
217 EXPECT_EQ("sender1", sender_tokenizer.token());
218 ASSERT_TRUE(sender_tokenizer.GetNext());
219 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token());
220 }
221
222 TEST_F(RegistrationRequestTest, ResponseParsing) { 192 TEST_F(RegistrationRequestTest, ResponseParsing) {
223 CreateRequest("sender1,sender2"); 193 CreateRequest(kDeveloperId);
224 request_->Start(); 194 request_->Start();
225 195
226 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 196 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
227 CompleteFetch(); 197 CompleteFetch();
228 198
229 EXPECT_TRUE(callback_called_); 199 EXPECT_TRUE(callback_called_);
230 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 200 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
231 EXPECT_EQ("2501", registration_id_); 201 EXPECT_EQ("2501", registration_id_);
232 } 202 }
233 203
234 TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) { 204 TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) {
235 CreateRequest("sender1,sender2"); 205 CreateRequest(kDeveloperId);
236 request_->Start(); 206 request_->Start();
237 207
238 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); 208 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
239 CompleteFetch(); 209 CompleteFetch();
240 210
241 EXPECT_FALSE(callback_called_); 211 EXPECT_FALSE(callback_called_);
242 212
243 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 213 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
244 CompleteFetch(); 214 CompleteFetch();
245 215
246 EXPECT_TRUE(callback_called_); 216 EXPECT_TRUE(callback_called_);
247 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 217 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
248 EXPECT_EQ("2501", registration_id_); 218 EXPECT_EQ("2501", registration_id_);
249 } 219 }
250 220
251 TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) { 221 TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) {
252 CreateRequest("sender1,sender2"); 222 CreateRequest(kDeveloperId);
253 request_->Start(); 223 request_->Start();
254 224
255 SetResponseStatusAndString(net::HTTP_OK, ""); 225 SetResponseStatusAndString(net::HTTP_OK, "");
256 CompleteFetch(); 226 CompleteFetch();
257 227
258 EXPECT_FALSE(callback_called_); 228 EXPECT_FALSE(callback_called_);
259 229
260 SetResponseStatusAndString(net::HTTP_OK, "some error in response"); 230 SetResponseStatusAndString(net::HTTP_OK, "some error in response");
261 CompleteFetch(); 231 CompleteFetch();
262 232
263 EXPECT_FALSE(callback_called_); 233 EXPECT_FALSE(callback_called_);
264 234
265 // Ensuring a retry happened and succeeds. 235 // Ensuring a retry happened and succeeds.
266 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 236 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
267 CompleteFetch(); 237 CompleteFetch();
268 238
269 EXPECT_TRUE(callback_called_); 239 EXPECT_TRUE(callback_called_);
270 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 240 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
271 EXPECT_EQ("2501", registration_id_); 241 EXPECT_EQ("2501", registration_id_);
272 } 242 }
273 243
274 TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) { 244 TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) {
275 CreateRequest("sender1,sender2"); 245 CreateRequest(kDeveloperId);
276 request_->Start(); 246 request_->Start();
277 247
278 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR"); 248 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR");
279 CompleteFetch(); 249 CompleteFetch();
280 250
281 EXPECT_FALSE(callback_called_); 251 EXPECT_FALSE(callback_called_);
282 252
283 // Ensuring a retry happened and succeeds. 253 // Ensuring a retry happened and succeeds.
284 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 254 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
285 CompleteFetch(); 255 CompleteFetch();
286 256
287 EXPECT_TRUE(callback_called_); 257 EXPECT_TRUE(callback_called_);
288 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 258 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
289 EXPECT_EQ("2501", registration_id_); 259 EXPECT_EQ("2501", registration_id_);
290 } 260 }
291 261
292 TEST_F(RegistrationRequestTest, ResponseAuthenticationError) { 262 TEST_F(RegistrationRequestTest, ResponseAuthenticationError) {
293 CreateRequest("sender1,sender2"); 263 CreateRequest(kDeveloperId);
294 request_->Start(); 264 request_->Start();
295 265
296 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, 266 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED,
297 "Error=AUTHENTICATION_FAILED"); 267 "Error=AUTHENTICATION_FAILED");
298 CompleteFetch(); 268 CompleteFetch();
299 269
300 EXPECT_FALSE(callback_called_); 270 EXPECT_FALSE(callback_called_);
301 271
302 // Ensuring a retry happened and succeeds. 272 // Ensuring a retry happened and succeeds.
303 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 273 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
304 CompleteFetch(); 274 CompleteFetch();
305 275
306 EXPECT_TRUE(callback_called_); 276 EXPECT_TRUE(callback_called_);
307 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 277 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
308 EXPECT_EQ("2501", registration_id_); 278 EXPECT_EQ("2501", registration_id_);
309 } 279 }
310 280
311 TEST_F(RegistrationRequestTest, ResponseInvalidParameters) { 281 TEST_F(RegistrationRequestTest, ResponseInvalidParameters) {
312 CreateRequest("sender1,sender2"); 282 CreateRequest(kDeveloperId);
313 request_->Start(); 283 request_->Start();
314 284
315 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); 285 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
316 CompleteFetch(); 286 CompleteFetch();
317 287
318 EXPECT_TRUE(callback_called_); 288 EXPECT_TRUE(callback_called_);
319 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_); 289 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_);
320 EXPECT_EQ(std::string(), registration_id_); 290 EXPECT_EQ(std::string(), registration_id_);
321 } 291 }
322 292
323 TEST_F(RegistrationRequestTest, ResponseInvalidSender) { 293 TEST_F(RegistrationRequestTest, ResponseInvalidSender) {
324 CreateRequest("sender1,sender2"); 294 CreateRequest(kDeveloperId);
325 request_->Start(); 295 request_->Start();
326 296
327 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); 297 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER");
328 CompleteFetch(); 298 CompleteFetch();
329 299
330 EXPECT_TRUE(callback_called_); 300 EXPECT_TRUE(callback_called_);
331 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); 301 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
332 EXPECT_EQ(std::string(), registration_id_); 302 EXPECT_EQ(std::string(), registration_id_);
333 } 303 }
334 304
335 TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) { 305 TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) {
336 CreateRequest("sender1"); 306 CreateRequest("sender1");
337 request_->Start(); 307 request_->Start();
338 308
339 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER"); 309 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER");
340 CompleteFetch(); 310 CompleteFetch();
341 311
342 EXPECT_TRUE(callback_called_); 312 EXPECT_TRUE(callback_called_);
343 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); 313 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
344 EXPECT_EQ(std::string(), registration_id_); 314 EXPECT_EQ(std::string(), registration_id_);
345 } 315 }
346 316
347 TEST_F(RegistrationRequestTest, RequestNotSuccessful) { 317 TEST_F(RegistrationRequestTest, RequestNotSuccessful) {
348 CreateRequest("sender1,sender2"); 318 CreateRequest(kDeveloperId);
349 request_->Start(); 319 request_->Start();
350 320
351 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1); 321 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1);
352 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 322 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
353 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 323 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
354 ASSERT_TRUE(fetcher); 324 ASSERT_TRUE(fetcher);
355 fetcher->set_status(request_status); 325 fetcher->set_status(request_status);
356 326
357 CompleteFetch(); 327 CompleteFetch();
358 328
359 EXPECT_FALSE(callback_called_); 329 EXPECT_FALSE(callback_called_);
360 330
361 // Ensuring a retry happened and succeeded. 331 // Ensuring a retry happened and succeeded.
362 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 332 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
363 CompleteFetch(); 333 CompleteFetch();
364 334
365 EXPECT_TRUE(callback_called_); 335 EXPECT_TRUE(callback_called_);
366 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 336 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
367 EXPECT_EQ("2501", registration_id_); 337 EXPECT_EQ("2501", registration_id_);
368 } 338 }
369 339
370 TEST_F(RegistrationRequestTest, ResponseHttpNotOk) { 340 TEST_F(RegistrationRequestTest, ResponseHttpNotOk) {
371 CreateRequest("sender1,sender2"); 341 CreateRequest(kDeveloperId);
372 request_->Start(); 342 request_->Start();
373 343
374 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 344 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
375 CompleteFetch(); 345 CompleteFetch();
376 346
377 EXPECT_FALSE(callback_called_); 347 EXPECT_FALSE(callback_called_);
378 348
379 // Ensuring a retry happened and succeeded. 349 // Ensuring a retry happened and succeeded.
380 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 350 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
381 CompleteFetch(); 351 CompleteFetch();
382 352
383 EXPECT_TRUE(callback_called_); 353 EXPECT_TRUE(callback_called_);
384 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 354 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
385 EXPECT_EQ("2501", registration_id_); 355 EXPECT_EQ("2501", registration_id_);
386 } 356 }
387 357
388 TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { 358 TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) {
389 set_max_retry_count(0); 359 set_max_retry_count(0);
390 CreateRequest("sender1,sender2"); 360 CreateRequest(kDeveloperId);
391 request_->Start(); 361 request_->Start();
392 362
393 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 363 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
394 CompleteFetch(); 364 CompleteFetch();
395 365
396 EXPECT_TRUE(callback_called_); 366 EXPECT_TRUE(callback_called_);
397 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); 367 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
398 EXPECT_EQ(std::string(), registration_id_); 368 EXPECT_EQ(std::string(), registration_id_);
399 } 369 }
400 370
401 TEST_F(RegistrationRequestTest, MaximumAttemptsReached) { 371 TEST_F(RegistrationRequestTest, MaximumAttemptsReached) {
402 CreateRequest("sender1,sender2"); 372 CreateRequest(kDeveloperId);
403 request_->Start(); 373 request_->Start();
404 374
405 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 375 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
406 CompleteFetch(); 376 CompleteFetch();
407 377
408 EXPECT_FALSE(callback_called_); 378 EXPECT_FALSE(callback_called_);
409 379
410 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 380 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
411 CompleteFetch(); 381 CompleteFetch();
412 382
413 EXPECT_FALSE(callback_called_); 383 EXPECT_FALSE(callback_called_);
414 384
415 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 385 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
416 CompleteFetch(); 386 CompleteFetch();
417 387
418 EXPECT_TRUE(callback_called_); 388 EXPECT_TRUE(callback_called_);
419 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); 389 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
420 EXPECT_EQ(std::string(), registration_id_); 390 EXPECT_EQ(std::string(), registration_id_);
421 } 391 }
422 392
423 } // namespace gcm 393 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698