Update ASan/Android runtime and setup script to LLVM r200682.
[chromium-blink-merge.git] / google_apis / gcm / engine / registration_request_unittest.cc
blobd3e06c8067dcaad1f187eacf6a857c807051ed43
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.
5 #include <map>
6 #include <string>
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "google_apis/gcm/engine/registration_request.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace gcm {
18 namespace {
19 const uint64 kAndroidId = 42UL;
20 const char kCert[] = "0DEADBEEF420";
21 const char kDeveloperId[] = "Project1";
22 const char kLoginHeader[] = "AidLogin";
23 const char kNoRegistrationIdReturned[] = "No Registration Id returned";
24 const char kAppId[] = "TestAppId";
25 const uint64 kSecurityToken = 77UL;
26 const uint64 kUserAndroidId = 1111;
27 const int64 kUserSerialNumber = 1;
28 } // namespace
30 class RegistrationRequestTest : public testing::Test {
31 public:
32 RegistrationRequestTest();
33 virtual ~RegistrationRequestTest();
35 void RegistrationCallback(const std::string& registration_id);
37 void CreateRequest(const std::string& sender_ids);
38 void SetResponseStatusAndString(net::HttpStatusCode status_code,
39 const std::string& response_body);
40 void CompleteFetch();
42 protected:
43 std::string registration_id_;
44 std::map<std::string, std::string> extras_;
45 scoped_ptr<RegistrationRequest> request_;
46 base::MessageLoop message_loop_;
47 net::TestURLFetcherFactory url_fetcher_factory_;
48 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
51 RegistrationRequestTest::RegistrationRequestTest()
52 : url_request_context_getter_(new net::TestURLRequestContextGetter(
53 message_loop_.message_loop_proxy())) {}
55 RegistrationRequestTest::~RegistrationRequestTest() {}
57 void RegistrationRequestTest::RegistrationCallback(
58 const std::string& registration_id) {
59 registration_id_ = registration_id;
62 void RegistrationRequestTest::CreateRequest(const std::string& sender_ids) {
63 std::vector<std::string> senders;
64 base::StringTokenizer tokenizer(sender_ids, ",");
65 while (tokenizer.GetNext())
66 senders.push_back(tokenizer.token());
68 request_.reset(new RegistrationRequest(
69 RegistrationRequest::RequestInfo(kAndroidId,
70 kSecurityToken,
71 kUserAndroidId,
72 kUserSerialNumber,
73 kAppId,
74 kCert,
75 senders),
76 base::Bind(&RegistrationRequestTest::RegistrationCallback,
77 base::Unretained(this)),
78 url_request_context_getter_.get()));
81 void RegistrationRequestTest::SetResponseStatusAndString(
82 net::HttpStatusCode status_code,
83 const std::string& response_body) {
84 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
85 ASSERT_TRUE(fetcher);
86 fetcher->set_response_code(status_code);
87 fetcher->SetResponseString(response_body);
90 void RegistrationRequestTest::CompleteFetch() {
91 registration_id_ = kNoRegistrationIdReturned;
92 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
93 ASSERT_TRUE(fetcher);
94 fetcher->delegate()->OnURLFetchComplete(fetcher);
97 TEST_F(RegistrationRequestTest, RequestDataPassedToFetcher) {
98 CreateRequest(kDeveloperId);
99 request_->Start();
101 // Get data sent by request.
102 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
103 ASSERT_TRUE(fetcher);
105 // Verify that authorization header was put together properly.
106 net::HttpRequestHeaders headers;
107 fetcher->GetExtraRequestHeaders(&headers);
108 std::string auth_header;
109 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header);
110 base::StringTokenizer auth_tokenizer(auth_header, " :");
111 ASSERT_TRUE(auth_tokenizer.GetNext());
112 EXPECT_EQ(kLoginHeader, auth_tokenizer.token());
113 ASSERT_TRUE(auth_tokenizer.GetNext());
114 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
115 ASSERT_TRUE(auth_tokenizer.GetNext());
116 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
118 std::map<std::string, std::string> expected_pairs;
119 expected_pairs["app"] = kAppId;
120 expected_pairs["sender"] = kDeveloperId;
121 expected_pairs["cert"] = kCert;
122 expected_pairs["device"] = base::Uint64ToString(kAndroidId);
123 expected_pairs["device_user_id"] = base::Int64ToString(kUserSerialNumber);
124 expected_pairs["X-GOOG.USER_AID"] = base::Uint64ToString(kUserAndroidId);
126 // Verify data was formatted properly.
127 std::string upload_data = fetcher->upload_data();
128 base::StringTokenizer data_tokenizer(upload_data, "&=");
129 while (data_tokenizer.GetNext()) {
130 std::map<std::string, std::string>::iterator iter =
131 expected_pairs.find(data_tokenizer.token());
132 ASSERT_TRUE(iter != expected_pairs.end());
133 ASSERT_TRUE(data_tokenizer.GetNext());
134 EXPECT_EQ(iter->second, data_tokenizer.token());
135 // Ensure that none of the keys appears twice.
136 expected_pairs.erase(iter);
139 EXPECT_EQ(0UL, expected_pairs.size());
142 TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) {
143 CreateRequest("sender1,sender2@gmail.com");
144 request_->Start();
146 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
147 ASSERT_TRUE(fetcher);
149 // Verify data was formatted properly.
150 std::string upload_data = fetcher->upload_data();
151 base::StringTokenizer data_tokenizer(upload_data, "&=");
153 // Skip all tokens until you hit entry for senders.
154 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender")
155 continue;
157 ASSERT_TRUE(data_tokenizer.GetNext());
158 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(),
159 net::UnescapeRule::URL_SPECIAL_CHARS));
160 base::StringTokenizer sender_tokenizer(senders, ",");
161 ASSERT_TRUE(sender_tokenizer.GetNext());
162 EXPECT_EQ("sender1", sender_tokenizer.token());
163 ASSERT_TRUE(sender_tokenizer.GetNext());
164 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token());
167 TEST_F(RegistrationRequestTest, ResponseParsing) {
168 CreateRequest("sender1,sender2");
169 request_->Start();
171 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
172 CompleteFetch();
174 EXPECT_EQ("2501", registration_id_);
177 TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) {
178 CreateRequest("sender1,sender2");
179 request_->Start();
181 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
182 CompleteFetch();
184 EXPECT_EQ("", registration_id_);
187 TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) {
188 CreateRequest("sender1,sender2");
189 request_->Start();
191 SetResponseStatusAndString(net::HTTP_OK, "");
192 CompleteFetch();
194 EXPECT_EQ("", registration_id_);
196 CreateRequest("sender1,sender2");
197 request_->Start();
199 SetResponseStatusAndString(net::HTTP_OK, "some error in response");
200 CompleteFetch();
202 EXPECT_EQ("", registration_id_);
205 } // namespace gcm