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 3 // found in the LICENSE file. 4 5 #include "components/gcm_driver/gcm_stats_recorder_impl.h" 6 7 #include <deque> 8 #include <string> 9 #include <vector> 10 11 #include "google_apis/gcm/engine/mcs_client.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace gcm { 15 16 namespace { 17 18 static uint64 kAndroidId = 4U; 19 static const char kCheckinStatus[] = "URL_FETCHING_FAILED"; 20 static const char kHost[] = "www.example.com"; 21 static const char kAppId[] = "app id 1"; 22 static const char kFrom[] = "from"; 23 static const char kSenderIds[] = "s1,s2"; 24 static const char kReceiverId[] = "receiver 1"; 25 static const char kMessageId[] = "message id 1"; 26 static const int kQueuedSec = 5; 27 static const gcm::MCSClient::MessageSendStatus kMessageSendStatus = 28 gcm::MCSClient::QUEUED; 29 static const int kByteSize = 99; 30 static const int kTTL = 7; 31 static const int kRetries = 3; 32 static const int64 kDelay = 15000; 33 static const ConnectionFactory::ConnectionResetReason kReason = 34 ConnectionFactory::NETWORK_CHANGE; 35 static const int kNetworkError = 1; 36 37 static const RegistrationRequest::Status kRegistrationStatus = 38 RegistrationRequest::SUCCESS; 39 static const UnregistrationRequest::Status kUnregistrationStatus = 40 UnregistrationRequest::SUCCESS; 41 42 static const char kCheckinInitiatedEvent[] = "Checkin initiated"; 43 static const char kCheckinInitiatedDetails[] = "Android Id: 4"; 44 static const char kCheckinDelayedDueToBackoffEvent[] = "Checkin backoff"; 45 static const char kCheckinDelayedDueToBackoffDetails[] = 46 "Delayed for 15000 msec"; 47 static const char kCheckinSuccessEvent[] = "Checkin succeeded"; 48 static const char kCheckinSuccessDetails[] = ""; 49 static const char kCheckinFailureEvent[] = "Checkin failed"; 50 static const char kCheckinFailureDetails[] = "URL_FETCHING_FAILED. Will retry."; 51 52 static const char kConnectionInitiatedEvent[] = "Connection initiated"; 53 static const char kConnectionInitiatedDetails[] = "www.example.com"; 54 static const char kConnectionDelayedDueToBackoffEvent[] = "Connection backoff"; 55 static const char kConnectionDelayedDueToBackoffDetails[] = 56 "Delayed for 15000 msec"; 57 static const char kConnectionSuccessEvent[] = "Connection succeeded"; 58 static const char kConnectionSuccessDetails[] = ""; 59 static const char kConnectionFailureEvent[] = "Connection failed"; 60 static const char kConnectionFailureDetails[] = "With network error 1"; 61 static const char kConnectionResetSignaledEvent[] = "Connection reset"; 62 static const char kConnectionResetSignaledDetails[] = "NETWORK_CHANGE"; 63 64 static const char kRegistrationSentEvent[] = "Registration request sent"; 65 static const char kRegistrationSentDetails[] = ""; 66 static const char kRegistrationResponseEvent[] = 67 "Registration response received"; 68 static const char kRegistrationResponseDetails[] = "SUCCESS"; 69 static const char kRegistrationRetryRequestedEvent[] = 70 "Registration retry requested"; 71 static const char kRegistrationRetryRequestedDetails[] = "Retries left: 3"; 72 static const char kUnregistrationSentEvent[] = "Unregistration request sent"; 73 static const char kUnregistrationSentDetails[] = ""; 74 static const char kUnregistrationResponseEvent[] = 75 "Unregistration response received"; 76 static const char kUnregistrationResponseDetails[] = "SUCCESS"; 77 static const char kUnregistrationRetryDelayedEvent[] = 78 "Unregistration retry delayed"; 79 static const char kUnregistrationRetryDelayedDetails[] = 80 "Delayed for 15000 msec"; 81 82 static const char kDataReceivedEvent[] = "Data msg received"; 83 static const char kDataReceivedDetails[] = ""; 84 static const char kDataReceivedNotRegisteredEvent[] = "Data msg received"; 85 static const char kDataReceivedNotRegisteredDetails[] = 86 "No such registered app found"; 87 static const char kDataDeletedMessageEvent[] = "Data msg received"; 88 static const char kDataDeletedMessageDetails[] = 89 "Message has been deleted on server"; 90 91 static const char kDataSentToWireEvent[] = "Data msg sent to wire"; 92 static const char kSentToWireDetails[] = "Msg queued for 5 seconds"; 93 static const char kNotifySendStatusEvent[] = "SEND status: QUEUED"; 94 static const char kNotifySendStatusDetails[] = "Msg size: 99 bytes, TTL: 7"; 95 static const char kIncomingSendErrorEvent[] = "Received 'send error' msg"; 96 static const char kIncomingSendErrorDetails[] = ""; 97 98 } // namespace 99 100 class GCMStatsRecorderImplTest : public testing::Test { 101 public: 102 GCMStatsRecorderImplTest(); 103 virtual ~GCMStatsRecorderImplTest(); 104 virtual void SetUp() OVERRIDE; 105 106 void VerifyRecordedCheckinCount(int expected_count) { 107 EXPECT_EQ(expected_count, 108 static_cast<int>(recorder_.checkin_activities().size())); 109 } 110 void VerifyRecordedConnectionCount(int expected_count) { 111 EXPECT_EQ(expected_count, 112 static_cast<int>(recorder_.connection_activities().size())); 113 } 114 void VerifyRecordedRegistrationCount(int expected_count) { 115 EXPECT_EQ(expected_count, 116 static_cast<int>(recorder_.registration_activities().size())); 117 } 118 void VerifyRecordedReceivingCount(int expected_count) { 119 EXPECT_EQ(expected_count, 120 static_cast<int>(recorder_.receiving_activities().size())); 121 } 122 void VerifyRecordedSendingCount(int expected_count) { 123 EXPECT_EQ(expected_count, 124 static_cast<int>(recorder_.sending_activities().size())); 125 } 126 void VerifyAllActivityQueueEmpty(const std::string& remark) { 127 EXPECT_TRUE(recorder_.checkin_activities().empty()) << remark; 128 EXPECT_TRUE(recorder_.connection_activities().empty()) << remark; 129 EXPECT_TRUE(recorder_.registration_activities().empty()) << remark; 130 EXPECT_TRUE(recorder_.receiving_activities().empty()) << remark; 131 EXPECT_TRUE(recorder_.sending_activities().empty()) << remark; 132 } 133 134 void VerifyCheckinInitiated(const std::string& remark) { 135 VerifyCheckin(recorder_.checkin_activities(), 136 kCheckinInitiatedEvent, 137 kCheckinInitiatedDetails, 138 remark); 139 } 140 141 void VerifyCheckinDelayedDueToBackoff(const std::string& remark) { 142 VerifyCheckin(recorder_.checkin_activities(), 143 kCheckinDelayedDueToBackoffEvent, 144 kCheckinDelayedDueToBackoffDetails, 145 remark); 146 } 147 148 void VerifyCheckinSuccess(const std::string& remark) { 149 VerifyCheckin(recorder_.checkin_activities(), 150 kCheckinSuccessEvent, 151 kCheckinSuccessDetails, 152 remark); 153 } 154 155 void VerifyCheckinFailure(const std::string& remark) { 156 VerifyCheckin(recorder_.checkin_activities(), 157 kCheckinFailureEvent, 158 kCheckinFailureDetails, 159 remark); 160 } 161 162 void VerifyConnectionInitiated(const std::string& remark) { 163 VerifyConnection(recorder_.connection_activities(), 164 kConnectionInitiatedEvent, 165 kConnectionInitiatedDetails, 166 remark); 167 } 168 169 void VerifyConnectionDelayedDueToBackoff(const std::string& remark) { 170 VerifyConnection(recorder_.connection_activities(), 171 kConnectionDelayedDueToBackoffEvent, 172 kConnectionDelayedDueToBackoffDetails, 173 remark); 174 } 175 176 void VerifyConnectionSuccess(const std::string& remark) { 177 VerifyConnection(recorder_.connection_activities(), 178 kConnectionSuccessEvent, 179 kConnectionSuccessDetails, 180 remark); 181 } 182 183 void VerifyConnectionFailure(const std::string& remark) { 184 VerifyConnection(recorder_.connection_activities(), 185 kConnectionFailureEvent, 186 kConnectionFailureDetails, 187 remark); 188 } 189 190 void VerifyConnectionResetSignaled(const std::string& remark) { 191 VerifyConnection(recorder_.connection_activities(), 192 kConnectionResetSignaledEvent, 193 kConnectionResetSignaledDetails, 194 remark); 195 } 196 197 void VerifyRegistrationSent(const std::string& remark) { 198 VerifyRegistration(recorder_.registration_activities(), 199 kSenderIds, 200 kRegistrationSentEvent, 201 kRegistrationSentDetails, 202 remark); 203 } 204 205 void VerifyRegistrationResponse(const std::string& remark) { 206 VerifyRegistration(recorder_.registration_activities(), 207 kSenderIds, 208 kRegistrationResponseEvent, 209 kRegistrationResponseDetails, 210 remark); 211 } 212 213 void VerifyRegistrationRetryRequested(const std::string& remark) { 214 VerifyRegistration(recorder_.registration_activities(), 215 kSenderIds, 216 kRegistrationRetryRequestedEvent, 217 kRegistrationRetryRequestedDetails, 218 remark); 219 } 220 221 void VerifyUnregistrationSent(const std::string& remark) { 222 VerifyRegistration(recorder_.registration_activities(), 223 std::string(), 224 kUnregistrationSentEvent, 225 kUnregistrationSentDetails, 226 remark); 227 } 228 229 void VerifyUnregistrationResponse(const std::string& remark) { 230 VerifyRegistration(recorder_.registration_activities(), 231 std::string(), 232 kUnregistrationResponseEvent, 233 kUnregistrationResponseDetails, 234 remark); 235 } 236 237 void VerifyUnregistrationRetryDelayed(const std::string& remark) { 238 VerifyRegistration(recorder_.registration_activities(), 239 std::string(), 240 kUnregistrationRetryDelayedEvent, 241 kUnregistrationRetryDelayedDetails, 242 remark); 243 } 244 245 void VerifyDataMessageReceived(const std::string& remark) { 246 VerifyReceivingData(recorder_.receiving_activities(), 247 kDataReceivedEvent, 248 kDataReceivedDetails, 249 remark); 250 } 251 252 void VerifyDataDeletedMessage(const std::string& remark) { 253 VerifyReceivingData(recorder_.receiving_activities(), 254 kDataDeletedMessageEvent, 255 kDataDeletedMessageDetails, 256 remark); 257 } 258 259 void VerifyDataMessageReceivedNotRegistered(const std::string& remark) { 260 VerifyReceivingData(recorder_.receiving_activities(), 261 kDataReceivedNotRegisteredEvent, 262 kDataReceivedNotRegisteredDetails, 263 remark); 264 } 265 266 void VerifyDataSentToWire(const std::string& remark) { 267 VerifySendingData(recorder_.sending_activities(), 268 kDataSentToWireEvent, 269 kSentToWireDetails, 270 remark); 271 } 272 273 void VerifyNotifySendStatus(const std::string& remark) { 274 VerifySendingData(recorder_.sending_activities(), 275 kNotifySendStatusEvent, 276 kNotifySendStatusDetails, 277 remark); 278 } 279 280 void VerifyIncomingSendError(const std::string& remark) { 281 VerifySendingData(recorder_.sending_activities(), 282 kIncomingSendErrorEvent, 283 kIncomingSendErrorDetails, 284 remark); 285 } 286 287 protected: 288 void VerifyCheckin( 289 const std::deque<CheckinActivity>& queue, 290 const std::string& event, 291 const std::string& details, 292 const std::string& remark) { 293 EXPECT_EQ(event, queue.front().event) << remark; 294 EXPECT_EQ(details, queue.front().details) << remark; 295 } 296 297 void VerifyConnection( 298 const std::deque<ConnectionActivity>& queue, 299 const std::string& event, 300 const std::string& details, 301 const std::string& remark) { 302 EXPECT_EQ(event, queue.front().event) << remark; 303 EXPECT_EQ(details, queue.front().details) << remark; 304 } 305 306 void VerifyRegistration( 307 const std::deque<RegistrationActivity>& queue, 308 const std::string& sender_ids, 309 const std::string& event, 310 const std::string& details, 311 const std::string& remark) { 312 EXPECT_EQ(kAppId, queue.front().app_id) << remark; 313 EXPECT_EQ(sender_ids, queue.front().sender_ids) << remark; 314 EXPECT_EQ(event, queue.front().event) << remark; 315 EXPECT_EQ(details, queue.front().details) << remark; 316 } 317 318 void VerifyReceivingData( 319 const std::deque<ReceivingActivity>& queue, 320 const std::string& event, 321 const std::string& details, 322 const std::string& remark) { 323 EXPECT_EQ(kAppId, queue.front().app_id) << remark; 324 EXPECT_EQ(kFrom, queue.front().from) << remark; 325 EXPECT_EQ(kByteSize, queue.front().message_byte_size) << remark; 326 EXPECT_EQ(event, queue.front().event) << remark; 327 EXPECT_EQ(details, queue.front().details) << remark; 328 } 329 330 void VerifySendingData( 331 const std::deque<SendingActivity>& queue, 332 const std::string& event, const std::string& details, 333 const std::string& remark) { 334 EXPECT_EQ(kAppId, queue.front().app_id) << remark; 335 EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark; 336 EXPECT_EQ(kMessageId, queue.front().message_id) << remark; 337 EXPECT_EQ(event, queue.front().event) << remark; 338 EXPECT_EQ(details, queue.front().details) << remark; 339 } 340 341 std::vector<std::string> sender_ids_; 342 GCMStatsRecorderImpl recorder_; 343 }; 344 345 GCMStatsRecorderImplTest::GCMStatsRecorderImplTest(){ 346 } 347 348 GCMStatsRecorderImplTest::~GCMStatsRecorderImplTest() {} 349 350 void GCMStatsRecorderImplTest::SetUp(){ 351 sender_ids_.push_back("s1"); 352 sender_ids_.push_back("s2"); 353 recorder_.SetRecording(true); 354 } 355 356 TEST_F(GCMStatsRecorderImplTest, StartStopRecordingTest) { 357 EXPECT_TRUE(recorder_.is_recording()); 358 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); 359 VerifyRecordedSendingCount(1); 360 VerifyDataSentToWire("1st call"); 361 362 recorder_.SetRecording(false); 363 EXPECT_FALSE(recorder_.is_recording()); 364 recorder_.Clear(); 365 VerifyAllActivityQueueEmpty("all cleared"); 366 367 // Exercise every recording method below and verify that nothing is recorded. 368 recorder_.RecordCheckinInitiated(kAndroidId); 369 recorder_.RecordCheckinDelayedDueToBackoff(kDelay); 370 recorder_.RecordCheckinSuccess(); 371 recorder_.RecordCheckinFailure(kCheckinStatus, true); 372 VerifyAllActivityQueueEmpty("no checkin"); 373 374 recorder_.RecordConnectionInitiated(kHost); 375 recorder_.RecordConnectionDelayedDueToBackoff(kDelay); 376 recorder_.RecordConnectionSuccess(); 377 recorder_.RecordConnectionFailure(kNetworkError); 378 recorder_.RecordConnectionResetSignaled(kReason); 379 VerifyAllActivityQueueEmpty("no registration"); 380 381 recorder_.RecordRegistrationSent(kAppId, kSenderIds); 382 recorder_.RecordRegistrationResponse(kAppId, sender_ids_, 383 kRegistrationStatus); 384 recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries); 385 recorder_.RecordUnregistrationSent(kAppId); 386 recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus); 387 recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay); 388 VerifyAllActivityQueueEmpty("no unregistration"); 389 390 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, 391 GCMStatsRecorder::DATA_MESSAGE); 392 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, 393 GCMStatsRecorder::DELETED_MESSAGES); 394 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false, 395 GCMStatsRecorder::DATA_MESSAGE); 396 VerifyAllActivityQueueEmpty("no receiving"); 397 398 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); 399 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, 400 kMessageSendStatus, kByteSize, kTTL); 401 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); 402 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); 403 VerifyAllActivityQueueEmpty("no sending"); 404 } 405 406 TEST_F(GCMStatsRecorderImplTest, ClearLogTest) { 407 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); 408 VerifyRecordedSendingCount(1); 409 VerifyDataSentToWire("1st call"); 410 411 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, 412 kMessageSendStatus, kByteSize, kTTL); 413 VerifyRecordedSendingCount(2); 414 VerifyNotifySendStatus("2nd call"); 415 416 recorder_.Clear(); 417 VerifyRecordedSendingCount(0); 418 } 419 420 TEST_F(GCMStatsRecorderImplTest, CheckinTest) { 421 recorder_.RecordCheckinInitiated(kAndroidId); 422 VerifyRecordedCheckinCount(1); 423 VerifyCheckinInitiated("1st call"); 424 425 recorder_.RecordCheckinDelayedDueToBackoff(kDelay); 426 VerifyRecordedCheckinCount(2); 427 VerifyCheckinDelayedDueToBackoff("2nd call"); 428 429 recorder_.RecordCheckinSuccess(); 430 VerifyRecordedCheckinCount(3); 431 VerifyCheckinSuccess("3rd call"); 432 433 recorder_.RecordCheckinFailure(kCheckinStatus, true); 434 VerifyRecordedCheckinCount(4); 435 VerifyCheckinFailure("4th call"); 436 } 437 438 TEST_F(GCMStatsRecorderImplTest, ConnectionTest) { 439 recorder_.RecordConnectionInitiated(kHost); 440 VerifyRecordedConnectionCount(1); 441 VerifyConnectionInitiated("1st call"); 442 443 recorder_.RecordConnectionDelayedDueToBackoff(kDelay); 444 VerifyRecordedConnectionCount(2); 445 VerifyConnectionDelayedDueToBackoff("2nd call"); 446 447 recorder_.RecordConnectionSuccess(); 448 VerifyRecordedConnectionCount(3); 449 VerifyConnectionSuccess("3rd call"); 450 451 recorder_.RecordConnectionFailure(kNetworkError); 452 VerifyRecordedConnectionCount(4); 453 VerifyConnectionFailure("4th call"); 454 455 recorder_.RecordConnectionResetSignaled(kReason); 456 VerifyRecordedConnectionCount(5); 457 VerifyConnectionResetSignaled("5th call"); 458 } 459 460 TEST_F(GCMStatsRecorderImplTest, RegistrationTest) { 461 recorder_.RecordRegistrationSent(kAppId, kSenderIds); 462 VerifyRecordedRegistrationCount(1); 463 VerifyRegistrationSent("1st call"); 464 465 recorder_.RecordRegistrationResponse(kAppId, sender_ids_, 466 kRegistrationStatus); 467 VerifyRecordedRegistrationCount(2); 468 VerifyRegistrationResponse("2nd call"); 469 470 recorder_.RecordRegistrationRetryRequested(kAppId, sender_ids_, kRetries); 471 VerifyRecordedRegistrationCount(3); 472 VerifyRegistrationRetryRequested("3rd call"); 473 474 recorder_.RecordUnregistrationSent(kAppId); 475 VerifyRecordedRegistrationCount(4); 476 VerifyUnregistrationSent("4th call"); 477 478 recorder_.RecordUnregistrationResponse(kAppId, kUnregistrationStatus); 479 VerifyRecordedRegistrationCount(5); 480 VerifyUnregistrationResponse("5th call"); 481 482 recorder_.RecordUnregistrationRetryDelayed(kAppId, kDelay); 483 VerifyRecordedRegistrationCount(6); 484 VerifyUnregistrationRetryDelayed("6th call"); 485 } 486 487 TEST_F(GCMStatsRecorderImplTest, RecordReceivingTest) { 488 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, 489 GCMStatsRecorder::DATA_MESSAGE); 490 VerifyRecordedReceivingCount(1); 491 VerifyDataMessageReceived("1st call"); 492 493 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, true, 494 GCMStatsRecorder::DELETED_MESSAGES); 495 VerifyRecordedReceivingCount(2); 496 VerifyDataDeletedMessage("2nd call"); 497 498 recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize, false, 499 GCMStatsRecorder::DATA_MESSAGE); 500 VerifyRecordedReceivingCount(3); 501 VerifyDataMessageReceivedNotRegistered("3rd call"); 502 } 503 504 TEST_F(GCMStatsRecorderImplTest, RecordSendingTest) { 505 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); 506 VerifyRecordedSendingCount(1); 507 VerifyDataSentToWire("1st call"); 508 509 recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId, 510 kMessageSendStatus, kByteSize, kTTL); 511 VerifyRecordedSendingCount(2); 512 VerifyNotifySendStatus("2nd call"); 513 514 recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId); 515 VerifyRecordedSendingCount(3); 516 VerifyIncomingSendError("3rd call"); 517 518 recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec); 519 VerifyRecordedSendingCount(4); 520 VerifyDataSentToWire("4th call"); 521 } 522 523 } // namespace gcm 524