This source file includes following definitions.
- url_request_context_getter_
- FetcherCallback
- CreateRequest
- SetResponseStatusAndString
- CompleteFetch
- SetResponse
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include <vector>
#include "google_apis/gcm/engine/checkin_request.h"
#include "google_apis/gcm/protocol/checkin.pb.h"
#include "net/base/backoff_entry.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gcm {
namespace {
const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
1,
15000,
2,
0.5,
1000 * 60 * 5,
-1,
false,
};
}
const uint64 kAndroidId = 42UL;
const uint64 kBlankAndroidId = 999999UL;
const uint64 kBlankSecurityToken = 999999UL;
const char kChromeVersion[] = "Version String";
const uint64 kSecurityToken = 77;
class CheckinRequestTest : public testing::Test {
public:
enum ResponseScenario {
VALID_RESPONSE,
MISSING_ANDROID_ID,
MISSING_SECURITY_TOKEN,
ANDROID_ID_IS_ZER0,
SECURITY_TOKEN_IS_ZERO
};
CheckinRequestTest();
virtual ~CheckinRequestTest();
void FetcherCallback(uint64 android_id, uint64 security_token);
void CreateRequest(uint64 android_id, uint64 security_token);
void SetResponseStatusAndString(
net::HttpStatusCode status_code,
const std::string& response_data);
void CompleteFetch();
void SetResponse(ResponseScenario response_scenario);
protected:
bool callback_called_;
uint64 android_id_;
uint64 security_token_;
int checkin_device_type_;
base::MessageLoop message_loop_;
net::TestURLFetcherFactory url_fetcher_factory_;
scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
checkin_proto::ChromeBuildProto chrome_build_proto_;
std::vector<std::string> account_ids_;
scoped_ptr<CheckinRequest> request_;
};
CheckinRequestTest::CheckinRequestTest()
: callback_called_(false),
android_id_(kBlankAndroidId),
security_token_(kBlankSecurityToken),
checkin_device_type_(0),
url_request_context_getter_(new net::TestURLRequestContextGetter(
message_loop_.message_loop_proxy())) {
account_ids_.push_back("account_id");
}
CheckinRequestTest::~CheckinRequestTest() {}
void CheckinRequestTest::FetcherCallback(uint64 android_id,
uint64 security_token) {
callback_called_ = true;
android_id_ = android_id;
security_token_ = security_token;
}
void CheckinRequestTest::CreateRequest(uint64 android_id,
uint64 security_token) {
chrome_build_proto_.set_platform(
checkin_proto::ChromeBuildProto::PLATFORM_LINUX);
chrome_build_proto_.set_channel(
checkin_proto::ChromeBuildProto::CHANNEL_CANARY);
chrome_build_proto_.set_chrome_version(kChromeVersion);
request_.reset(new CheckinRequest(
base::Bind(&CheckinRequestTest::FetcherCallback, base::Unretained(this)),
kDefaultBackoffPolicy,
chrome_build_proto_,
android_id,
security_token,
account_ids_,
url_request_context_getter_.get()));
callback_called_ = false;
android_id_ = kBlankAndroidId;
security_token_ = kBlankSecurityToken;
}
void CheckinRequestTest::SetResponseStatusAndString(
net::HttpStatusCode status_code,
const std::string& response_data) {
net::TestURLFetcher* fetcher =
url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_response_code(status_code);
fetcher->SetResponseString(response_data);
}
void CheckinRequestTest::CompleteFetch() {
net::TestURLFetcher* fetcher =
url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
void CheckinRequestTest::SetResponse(ResponseScenario response_scenario) {
checkin_proto::AndroidCheckinResponse response;
response.set_stats_ok(true);
uint64 android_id = response_scenario == ANDROID_ID_IS_ZER0 ? 0 : kAndroidId;
uint64 security_token =
response_scenario == SECURITY_TOKEN_IS_ZERO ? 0 : kSecurityToken;
if (response_scenario != MISSING_ANDROID_ID)
response.set_android_id(android_id);
if (response_scenario != MISSING_SECURITY_TOKEN)
response.set_security_token(security_token);
std::string response_string;
response.SerializeToString(&response_string);
SetResponseStatusAndString(net::HTTP_OK, response_string);
}
TEST_F(CheckinRequestTest, FetcherData) {
CreateRequest(kAndroidId, kSecurityToken);
request_->Start();
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_response_code(net::HTTP_OK);
checkin_proto::AndroidCheckinRequest request_proto;
request_proto.ParseFromString(fetcher->upload_data());
EXPECT_EQ(kAndroidId, static_cast<uint64>(request_proto.id()));
EXPECT_EQ(kSecurityToken, request_proto.security_token());
EXPECT_EQ(chrome_build_proto_.platform(),
request_proto.checkin().chrome_build().platform());
EXPECT_EQ(chrome_build_proto_.chrome_version(),
request_proto.checkin().chrome_build().chrome_version());
EXPECT_EQ(chrome_build_proto_.channel(),
request_proto.checkin().chrome_build().channel());
#if defined(CHROME_OS)
EXPECT_EQ(checkin_proto::DEVICE_CHROME_OS, request_proto.checkin().type());
#else
EXPECT_EQ(checkin_proto::DEVICE_CHROME_BROWSER,
request_proto.checkin().type());
#endif
EXPECT_EQ(1, request_proto.account_cookie_size());
EXPECT_EQ("[account_id]", request_proto.account_cookie(0));
}
TEST_F(CheckinRequestTest, ResponseBodyEmpty) {
CreateRequest(0u, 0u);
request_->Start();
SetResponseStatusAndString(net::HTTP_OK, std::string());
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, ResponseBodyCorrupted) {
CreateRequest(0u, 0u);
request_->Start();
SetResponseStatusAndString(net::HTTP_OK, "Corrupted response body");
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, ResponseHttpStatusUnauthorized) {
CreateRequest(0u, 0u);
request_->Start();
SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, std::string());
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(0u, android_id_);
EXPECT_EQ(0u, security_token_);
}
TEST_F(CheckinRequestTest, ResponseHttpStatusBadRequest) {
CreateRequest(0u, 0u);
request_->Start();
SetResponseStatusAndString(net::HTTP_BAD_REQUEST, std::string());
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(0u, android_id_);
EXPECT_EQ(0u, security_token_);
}
TEST_F(CheckinRequestTest, ResponseHttpStatusNotOK) {
CreateRequest(0u, 0u);
request_->Start();
SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, std::string());
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, ResponseMissingAndroidId) {
CreateRequest(0u, 0u);
request_->Start();
SetResponse(MISSING_ANDROID_ID);
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, ResponseMissingSecurityToken) {
CreateRequest(0u, 0u);
request_->Start();
SetResponse(MISSING_SECURITY_TOKEN);
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, AndroidIdEqualsZeroInResponse) {
CreateRequest(0u, 0u);
request_->Start();
SetResponse(ANDROID_ID_IS_ZER0);
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, SecurityTokenEqualsZeroInResponse) {
CreateRequest(0u, 0u);
request_->Start();
SetResponse(SECURITY_TOKEN_IS_ZERO);
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, SuccessfulFirstTimeCheckin) {
CreateRequest(0u, 0u);
request_->Start();
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
TEST_F(CheckinRequestTest, SuccessfulSubsequentCheckin) {
CreateRequest(kAndroidId, kSecurityToken);
request_->Start();
SetResponse(VALID_RESPONSE);
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(kAndroidId, android_id_);
EXPECT_EQ(kSecurityToken, security_token_);
}
}