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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/gcm_driver/gcm_stats_recorder_impl.h"
#include <stdint.h>
#include <string>
#include "base/containers/circular_deque.h"
#include "components/gcm_driver/crypto/gcm_decryption_result.h"
#include "components/gcm_driver/crypto/gcm_encryption_provider.h"
#include "google_apis/gcm/engine/mcs_client.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gcm {
namespace {
static uint64_t kAndroidId = 4U;
static const char kCheckinStatus[] = "URL_FETCHING_FAILED";
static const char kHost[] = "www.example.com";
static const char kAppId[] = "app id 1";
static const char kFrom[] = "from";
static const char kSenderIds[] = "s1,s2";
static const char kReceiverId[] = "receiver 1";
static const char kMessageId[] = "message id 1";
static const int kQueuedSec = 5;
static const gcm::MCSClient::MessageSendStatus kMessageSendStatus =
gcm::MCSClient::QUEUED;
static const int kByteSize = 99;
static const int kTTL = 7;
static const int kRetries = 3;
static const int64_t kDelay = 15000;
static const ConnectionFactory::ConnectionResetReason kReason =
ConnectionFactory::NETWORK_CHANGE;
static const int kNetworkError = 1;
static const RegistrationRequest::Status kRegistrationStatus =
RegistrationRequest::SUCCESS;
static const UnregistrationRequest::Status kUnregistrationStatus =
UnregistrationRequest::SUCCESS;
static const char kCheckinInitiatedEvent[] = "Checkin initiated";
static const char kCheckinInitiatedDetails[] = "Android Id: 4";
static const char kCheckinDelayedDueToBackoffEvent[] = "Checkin backoff";
static const char kCheckinDelayedDueToBackoffDetails[] =
"Delayed for 15000 msec";
static const char kCheckinSuccessEvent[] = "Checkin succeeded";
static const char kCheckinSuccessDetails[] = "";
static const char kCheckinFailureEvent[] = "Checkin failed";
static const char kCheckinFailureDetails[] = "URL_FETCHING_FAILED. Will retry.";
static const char kConnectionInitiatedEvent[] = "Connection initiated";
static const char kConnectionInitiatedDetails[] = "www.example.com";
static const char kConnectionDelayedDueToBackoffEvent[] = "Connection backoff";
static const char kConnectionDelayedDueToBackoffDetails[] =
"Delayed for 15000 msec";
static const char kConnectionSuccessEvent[] = "Connection succeeded";
static const char kConnectionSuccessDetails[] = "";
static const char kConnectionFailureEvent[] = "Connection failed";
static const char kConnectionFailureDetails[] = "With network error 1";
static const char kConnectionResetSignaledEvent[] = "Connection reset";
static const char kConnectionResetSignaledDetails[] = "NETWORK_CHANGE";
static const char kRegistrationSentEvent[] = "Registration request sent";
static const char kRegistrationSentDetails[] = "";
static const char kRegistrationResponseEvent[] =
"Registration response received";
static const char kRegistrationResponseDetails[] = "SUCCESS";
static const char kRegistrationRetryDelayedEvent[] =
"Registration retry delayed";
static const char kRegistrationRetryDelayedDetails[] =
"Delayed for 15000 msec, retries left: 3";
static const char kUnregistrationSentEvent[] = "Unregistration request sent";
static const char kUnregistrationSentDetails[] = "";
static const char kUnregistrationResponseEvent[] =
"Unregistration response received";
static const char kUnregistrationResponseDetails[] = "SUCCESS";
static const char kUnregistrationRetryDelayedEvent[] =
"Unregistration retry delayed";
static const char kUnregistrationRetryDelayedDetails[] =
"Delayed for 15000 msec, retries left: 3";
static const char kDataReceivedEvent[] = "Data msg received";
static const char kDataReceivedDetails[] = "";
static const char kDataDeletedMessageEvent[] = "Data msg received";
static const char kDataDeletedMessageDetails[] =
"Message has been deleted on server";
static const char kDataSentToWireEvent[] = "Data msg sent to wire";
static const char kSentToWireDetails[] = "Msg queued for 5 seconds";
static const char kNotifySendStatusEvent[] = "SEND status: QUEUED";
static const char kNotifySendStatusDetails[] = "Msg size: 99 bytes, TTL: 7";
static const char kIncomingSendErrorEvent[] = "Received 'send error' msg";
static const char kIncomingSendErrorDetails[] = "";
static const GCMDecryptionResult kDecryptionResultFailure =
GCMDecryptionResult::INVALID_PAYLOAD;
} // namespace
class GCMStatsRecorderImplTest : public testing::Test {
public:
GCMStatsRecorderImplTest();
~GCMStatsRecorderImplTest() override;
void SetUp() override;
void VerifyRecordedCheckinCount(int expected_count) {
EXPECT_EQ(expected_count,
static_cast<int>(recorder_.checkin_activities().size()));
}
void VerifyRecordedConnectionCount(int expected_count) {
EXPECT_EQ(expected_count,
static_cast<int>(recorder_.connection_activities().size()));
}
void VerifyRecordedRegistrationCount(int expected_count) {
EXPECT_EQ(expected_count,
static_cast<int>(recorder_.registration_activities().size()));
}
void VerifyRecordedReceivingCount(int expected_count) {
EXPECT_EQ(expected_count,
static_cast<int>(recorder_.receiving_activities().size()));
}
void VerifyRecordedSendingCount(int expected_count) {
EXPECT_EQ(expected_count,
static_cast<int>(recorder_.sending_activities().size()));
}
void VerifyRecordedDecryptionFailureCount(int expected_count) {
EXPECT_EQ(
expected_count,
static_cast<int>(recorder_.decryption_failure_activities().size()));
}
void VerifyAllActivityQueueEmpty(const std::string& remark) {
EXPECT_TRUE(recorder_.checkin_activities().empty()) << remark;
EXPECT_TRUE(recorder_.connection_activities().empty()) << remark;
EXPECT_TRUE(recorder_.registration_activities().empty()) << remark;
EXPECT_TRUE(recorder_.receiving_activities().empty()) << remark;
EXPECT_TRUE(recorder_.sending_activities().empty()) << remark;
EXPECT_TRUE(recorder_.decryption_failure_activities().empty()) << remark;
}
void VerifyCheckinInitiated(const std::string& remark) {
VerifyCheckin(recorder_.checkin_activities(),
kCheckinInitiatedEvent,
kCheckinInitiatedDetails,
remark);
}
void VerifyCheckinDelayedDueToBackoff(const std::string& remark) {
VerifyCheckin(recorder_.checkin_activities(),
kCheckinDelayedDueToBackoffEvent,
kCheckinDelayedDueToBackoffDetails,
remark);
}
void VerifyCheckinSuccess(const std::string& remark) {
VerifyCheckin(recorder_.checkin_activities(),
kCheckinSuccessEvent,
kCheckinSuccessDetails,
remark);
}
void VerifyCheckinFailure(const std::string& remark) {
VerifyCheckin(recorder_.checkin_activities(),
kCheckinFailureEvent,
kCheckinFailureDetails,
remark);
}
void VerifyConnectionInitiated(const std::string& remark) {
VerifyConnection(recorder_.connection_activities(),
kConnectionInitiatedEvent,
kConnectionInitiatedDetails,
remark);
}
void VerifyConnectionDelayedDueToBackoff(const std::string& remark) {
VerifyConnection(recorder_.connection_activities(),
kConnectionDelayedDueToBackoffEvent,
kConnectionDelayedDueToBackoffDetails,
remark);
}
void VerifyConnectionSuccess(const std::string& remark) {
VerifyConnection(recorder_.connection_activities(),
kConnectionSuccessEvent,
kConnectionSuccessDetails,
remark);
}
void VerifyConnectionFailure(const std::string& remark) {
VerifyConnection(recorder_.connection_activities(),
kConnectionFailureEvent,
kConnectionFailureDetails,
remark);
}
void VerifyConnectionResetSignaled(const std::string& remark) {
VerifyConnection(recorder_.connection_activities(),
kConnectionResetSignaledEvent,
kConnectionResetSignaledDetails,
remark);
}
void VerifyRegistrationSent(const std::string& remark) {
VerifyRegistration(recorder_.registration_activities(),
kSenderIds,
kRegistrationSentEvent,
kRegistrationSentDetails,
remark);
}
void VerifyRegistrationResponse(const std::string& remark) {
VerifyRegistration(recorder_.registration_activities(),
kSenderIds,
kRegistrationResponseEvent,
kRegistrationResponseDetails,
remark);
}
void VerifyRegistrationRetryRequested(const std::string& remark) {
VerifyRegistration(recorder_.registration_activities(),
kSenderIds,
kRegistrationRetryDelayedEvent,
kRegistrationRetryDelayedDetails,
remark);
}
void VerifyUnregistrationSent(const std::string& remark) {
VerifyRegistration(recorder_.registration_activities(),
kSenderIds,
kUnregistrationSentEvent,
kUnregistrationSentDetails,
remark);
}
void VerifyUnregistrationResponse(const std::string& remark) {
VerifyRegistration(recorder_.registration_activities(),
kSenderIds,
kUnregistrationResponseEvent,
kUnregistrationResponseDetails,
remark);
}
void VerifyUnregistrationRetryDelayed(const std::string& remark) {
VerifyRegistration(recorder_.registration_activities(),
kSenderIds,
kUnregistrationRetryDelayedEvent,
kUnregistrationRetryDelayedDetails,
remark);
}
void VerifyDataMessageReceived(const std::string& remark) {
VerifyReceivingData(recorder_.receiving_activities(),
kDataReceivedEvent,
kDataReceivedDetails,
remark);
}
void VerifyDataDeletedMessage(const std::string& remark) {
VerifyReceivingData(recorder_.receiving_activities(),
kDataDeletedMessageEvent,
kDataDeletedMessageDetails,
remark);
}
void VerifyDataSentToWire(const std::string& remark) {
VerifySendingData(recorder_.sending_activities(),
kDataSentToWireEvent,
kSentToWireDetails,
remark);
}
void VerifyNotifySendStatus(const std::string& remark) {
VerifySendingData(recorder_.sending_activities(),
kNotifySendStatusEvent,
kNotifySendStatusDetails,
remark);
}
void VerifyIncomingSendError(const std::string& remark) {
VerifySendingData(recorder_.sending_activities(),
kIncomingSendErrorEvent,
kIncomingSendErrorDetails,
remark);
}
void VerifyRecordedDecryptionFailure(const std::string& remark) {
const auto& queue = recorder_.decryption_failure_activities();
EXPECT_EQ(kAppId, queue.front().app_id) << remark;
EXPECT_EQ(ToGCMDecryptionResultDetailsString(kDecryptionResultFailure),
queue.front().details)
<< remark;
}
protected:
void VerifyCheckin(const base::circular_deque<CheckinActivity>& queue,
const std::string& event,
const std::string& details,
const std::string& remark) {
EXPECT_EQ(event, queue.front().event) << remark;
EXPECT_EQ(details, queue.front().details) << remark;
}
void VerifyConnection(const base::circular_deque<ConnectionActivity>& queue,
const std::string& event,
const std::string& details,
const std::string& remark) {
EXPECT_EQ(event, queue.front().event) << remark;
EXPECT_EQ(details, queue.front().details) << remark;
}
void VerifyRegistration(
const base::circular_deque<RegistrationActivity>& queue,
const std::string& source,
const std::string& event,
const std::string& details,
const std::string& remark) {
EXPECT_EQ(kAppId, queue.front().app_id) << remark;
EXPECT_EQ(source, queue.front().source) << remark;
EXPECT_EQ(event, queue.front().event) << remark;
EXPECT_EQ(details, queue.front().details) << remark;
}
void VerifyReceivingData(const base::circular_deque<ReceivingActivity>& queue,
const std::string& event,
const std::string& details,
const std::string& remark) {
EXPECT_EQ(kAppId, queue.front().app_id) << remark;
EXPECT_EQ(kFrom, queue.front().from) << remark;
EXPECT_EQ(kByteSize, queue.front().message_byte_size) << remark;
EXPECT_EQ(event, queue.front().event) << remark;
EXPECT_EQ(details, queue.front().details) << remark;
}
void VerifySendingData(const base::circular_deque<SendingActivity>& queue,
const std::string& event,
const std::string& details,
const std::string& remark) {
EXPECT_EQ(kAppId, queue.front().app_id) << remark;
EXPECT_EQ(kReceiverId, queue.front().receiver_id) << remark;
EXPECT_EQ(kMessageId, queue.front().message_id) << remark;
EXPECT_EQ(event, queue.front().event) << remark;
EXPECT_EQ(details, queue.front().details) << remark;
}
std::string source_;
GCMStatsRecorderImpl recorder_;
};
GCMStatsRecorderImplTest::GCMStatsRecorderImplTest(){
}
GCMStatsRecorderImplTest::~GCMStatsRecorderImplTest() = default;
void GCMStatsRecorderImplTest::SetUp(){
source_ = "s1,s2";
recorder_.set_is_recording(true);
}
TEST_F(GCMStatsRecorderImplTest, StartStopRecordingTest) {
EXPECT_TRUE(recorder_.is_recording());
recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
VerifyRecordedSendingCount(1);
VerifyDataSentToWire("1st call");
recorder_.set_is_recording(false);
EXPECT_FALSE(recorder_.is_recording());
recorder_.Clear();
VerifyAllActivityQueueEmpty("all cleared");
// Exercise every recording method below and verify that nothing is recorded.
recorder_.RecordCheckinInitiated(kAndroidId);
recorder_.RecordCheckinDelayedDueToBackoff(kDelay);
recorder_.RecordCheckinSuccess();
recorder_.RecordCheckinFailure(kCheckinStatus, true);
VerifyAllActivityQueueEmpty("no checkin");
recorder_.RecordConnectionInitiated(kHost);
recorder_.RecordConnectionDelayedDueToBackoff(kDelay);
recorder_.RecordConnectionSuccess();
recorder_.RecordConnectionFailure(kNetworkError);
recorder_.RecordConnectionResetSignaled(kReason);
VerifyAllActivityQueueEmpty("no registration");
recorder_.RecordRegistrationSent(kAppId, kSenderIds);
recorder_.RecordRegistrationResponse(kAppId, source_,
kRegistrationStatus);
recorder_.RecordRegistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
recorder_.RecordUnregistrationSent(kAppId, source_);
recorder_.RecordUnregistrationResponse(
kAppId, source_, kUnregistrationStatus);
recorder_.RecordUnregistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
VerifyAllActivityQueueEmpty("no unregistration");
recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize,
GCMStatsRecorder::DATA_MESSAGE);
recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize,
GCMStatsRecorder::DELETED_MESSAGES);
VerifyAllActivityQueueEmpty("no receiving");
recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
kMessageSendStatus, kByteSize, kTTL);
recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId);
recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
VerifyAllActivityQueueEmpty("no sending");
}
TEST_F(GCMStatsRecorderImplTest, ClearLogTest) {
recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
VerifyRecordedSendingCount(1);
VerifyDataSentToWire("1st call");
recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
kMessageSendStatus, kByteSize, kTTL);
VerifyRecordedSendingCount(2);
VerifyNotifySendStatus("2nd call");
recorder_.Clear();
VerifyRecordedSendingCount(0);
}
TEST_F(GCMStatsRecorderImplTest, CheckinTest) {
recorder_.RecordCheckinInitiated(kAndroidId);
VerifyRecordedCheckinCount(1);
VerifyCheckinInitiated("1st call");
recorder_.RecordCheckinDelayedDueToBackoff(kDelay);
VerifyRecordedCheckinCount(2);
VerifyCheckinDelayedDueToBackoff("2nd call");
recorder_.RecordCheckinSuccess();
VerifyRecordedCheckinCount(3);
VerifyCheckinSuccess("3rd call");
recorder_.RecordCheckinFailure(kCheckinStatus, true);
VerifyRecordedCheckinCount(4);
VerifyCheckinFailure("4th call");
}
TEST_F(GCMStatsRecorderImplTest, ConnectionTest) {
recorder_.RecordConnectionInitiated(kHost);
VerifyRecordedConnectionCount(1);
VerifyConnectionInitiated("1st call");
recorder_.RecordConnectionDelayedDueToBackoff(kDelay);
VerifyRecordedConnectionCount(2);
VerifyConnectionDelayedDueToBackoff("2nd call");
recorder_.RecordConnectionSuccess();
VerifyRecordedConnectionCount(3);
VerifyConnectionSuccess("3rd call");
recorder_.RecordConnectionFailure(kNetworkError);
VerifyRecordedConnectionCount(4);
VerifyConnectionFailure("4th call");
recorder_.RecordConnectionResetSignaled(kReason);
VerifyRecordedConnectionCount(5);
VerifyConnectionResetSignaled("5th call");
}
TEST_F(GCMStatsRecorderImplTest, RegistrationTest) {
recorder_.RecordRegistrationSent(kAppId, kSenderIds);
VerifyRecordedRegistrationCount(1);
VerifyRegistrationSent("1st call");
recorder_.RecordRegistrationResponse(kAppId, source_,
kRegistrationStatus);
VerifyRecordedRegistrationCount(2);
VerifyRegistrationResponse("2nd call");
recorder_.RecordRegistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
VerifyRecordedRegistrationCount(3);
VerifyRegistrationRetryRequested("3rd call");
recorder_.RecordUnregistrationSent(kAppId, source_);
VerifyRecordedRegistrationCount(4);
VerifyUnregistrationSent("4th call");
recorder_.RecordUnregistrationResponse(
kAppId, source_, kUnregistrationStatus);
VerifyRecordedRegistrationCount(5);
VerifyUnregistrationResponse("5th call");
recorder_.RecordUnregistrationRetryDelayed(kAppId, source_, kDelay, kRetries);
VerifyRecordedRegistrationCount(6);
VerifyUnregistrationRetryDelayed("6th call");
}
TEST_F(GCMStatsRecorderImplTest, RecordReceivingTest) {
recorder_.RecordConnectionInitiated(std::string());
recorder_.RecordConnectionSuccess();
recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize,
GCMStatsRecorder::DATA_MESSAGE);
VerifyRecordedReceivingCount(1);
VerifyDataMessageReceived("1st call");
recorder_.RecordDataMessageReceived(kAppId, kFrom, kByteSize,
GCMStatsRecorder::DELETED_MESSAGES);
VerifyRecordedReceivingCount(2);
VerifyDataDeletedMessage("2nd call");
}
TEST_F(GCMStatsRecorderImplTest, RecordSendingTest) {
recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
VerifyRecordedSendingCount(1);
VerifyDataSentToWire("1st call");
recorder_.RecordNotifySendStatus(kAppId, kReceiverId, kMessageId,
kMessageSendStatus, kByteSize, kTTL);
VerifyRecordedSendingCount(2);
VerifyNotifySendStatus("2nd call");
recorder_.RecordIncomingSendError(kAppId, kReceiverId, kMessageId);
VerifyRecordedSendingCount(3);
VerifyIncomingSendError("3rd call");
recorder_.RecordDataSentToWire(kAppId, kReceiverId, kMessageId, kQueuedSec);
VerifyRecordedSendingCount(4);
VerifyDataSentToWire("4th call");
}
TEST_F(GCMStatsRecorderImplTest, RecordDecryptionFailureTest) {
recorder_.RecordDecryptionFailure(kAppId, kDecryptionResultFailure);
VerifyRecordedDecryptionFailureCount(1);
VerifyRecordedDecryptionFailure("1st call");
}
} // namespace gcm
|