OLD | NEW |
(Empty) | |
| 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 <map> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_tokenizer.h" |
| 11 #include "google_apis/gcm/engine/unregistration_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" |
| 15 |
| 16 namespace gcm { |
| 17 |
| 18 namespace { |
| 19 const uint64 kAndroidId = 42UL; |
| 20 const char kLoginHeader[] = "AidLogin"; |
| 21 const char kAppId[] = "TestAppId"; |
| 22 const char kDeletedAppId[] = "deleted=TestAppId"; |
| 23 const uint64 kSecurityToken = 77UL; |
| 24 |
| 25 // Backoff policy for testing registration request. |
| 26 const net::BackoffEntry::Policy kDefaultBackoffPolicy = { |
| 27 // Number of initial errors (in sequence) to ignore before applying |
| 28 // exponential back-off rules. |
| 29 // Explicitly set to 2 to skip the delay on the first retry, as we are not |
| 30 // trying to test the backoff itself, but rather the fact that retry happens. |
| 31 1, |
| 32 |
| 33 // Initial delay for exponential back-off in ms. |
| 34 15000, // 15 seconds. |
| 35 |
| 36 // Factor by which the waiting time will be multiplied. |
| 37 2, |
| 38 |
| 39 // Fuzzing percentage. ex: 10% will spread requests randomly |
| 40 // between 90%-100% of the calculated time. |
| 41 0.5, // 50%. |
| 42 |
| 43 // Maximum amount of time we are willing to delay our request in ms. |
| 44 1000 * 60 * 5, // 5 minutes. |
| 45 |
| 46 // Time to keep an entry from being discarded even when it |
| 47 // has no significant state, -1 to never discard. |
| 48 -1, |
| 49 |
| 50 // Don't use initial delay unless the last request was an error. |
| 51 false, |
| 52 }; |
| 53 } // namespace |
| 54 |
| 55 class UnregistrationRequestTest : public testing::Test { |
| 56 public: |
| 57 UnregistrationRequestTest(); |
| 58 virtual ~UnregistrationRequestTest(); |
| 59 |
| 60 void UnregistrationCallback(bool success); |
| 61 |
| 62 void CreateRequest(); |
| 63 void SetResponseStatusAndString(net::HttpStatusCode status_code, |
| 64 const std::string& response_body); |
| 65 void CompleteFetch(); |
| 66 |
| 67 protected: |
| 68 bool callback_called_; |
| 69 bool unregistration_successful_; |
| 70 scoped_ptr<UnregistrationRequest> request_; |
| 71 base::MessageLoop message_loop_; |
| 72 net::TestURLFetcherFactory url_fetcher_factory_; |
| 73 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 74 }; |
| 75 |
| 76 UnregistrationRequestTest::UnregistrationRequestTest() |
| 77 : callback_called_(false), |
| 78 unregistration_successful_(false), |
| 79 url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 80 message_loop_.message_loop_proxy())) {} |
| 81 |
| 82 UnregistrationRequestTest::~UnregistrationRequestTest() {} |
| 83 |
| 84 void UnregistrationRequestTest::UnregistrationCallback(bool success) { |
| 85 callback_called_ = true; |
| 86 unregistration_successful_ = success; |
| 87 } |
| 88 |
| 89 void UnregistrationRequestTest::CreateRequest() { |
| 90 request_.reset(new UnregistrationRequest( |
| 91 UnregistrationRequest::RequestInfo(kAndroidId, |
| 92 kSecurityToken, |
| 93 kAppId), |
| 94 kDefaultBackoffPolicy, |
| 95 base::Bind(&UnregistrationRequestTest::UnregistrationCallback, |
| 96 base::Unretained(this)), |
| 97 url_request_context_getter_.get())); |
| 98 } |
| 99 |
| 100 void UnregistrationRequestTest::SetResponseStatusAndString( |
| 101 net::HttpStatusCode status_code, |
| 102 const std::string& response_body) { |
| 103 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 104 ASSERT_TRUE(fetcher); |
| 105 fetcher->set_response_code(status_code); |
| 106 fetcher->SetResponseString(response_body); |
| 107 } |
| 108 |
| 109 void UnregistrationRequestTest::CompleteFetch() { |
| 110 unregistration_successful_ = false; |
| 111 callback_called_ = false; |
| 112 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 113 ASSERT_TRUE(fetcher); |
| 114 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 115 } |
| 116 |
| 117 TEST_F(UnregistrationRequestTest, RequestDataPassedToFetcher) { |
| 118 CreateRequest(); |
| 119 request_->Start(); |
| 120 |
| 121 // Get data sent by request. |
| 122 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 123 ASSERT_TRUE(fetcher); |
| 124 |
| 125 // Verify that authorization header was put together properly. |
| 126 net::HttpRequestHeaders headers; |
| 127 fetcher->GetExtraRequestHeaders(&headers); |
| 128 std::string auth_header; |
| 129 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header); |
| 130 base::StringTokenizer auth_tokenizer(auth_header, " :"); |
| 131 ASSERT_TRUE(auth_tokenizer.GetNext()); |
| 132 EXPECT_EQ(kLoginHeader, auth_tokenizer.token()); |
| 133 ASSERT_TRUE(auth_tokenizer.GetNext()); |
| 134 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token()); |
| 135 ASSERT_TRUE(auth_tokenizer.GetNext()); |
| 136 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token()); |
| 137 std::string app_id_header; |
| 138 headers.GetHeader("app", &app_id_header); |
| 139 EXPECT_EQ(kAppId, app_id_header); |
| 140 |
| 141 std::map<std::string, std::string> expected_pairs; |
| 142 expected_pairs["app"] = kAppId; |
| 143 expected_pairs["device"] = base::Uint64ToString(kAndroidId); |
| 144 expected_pairs["delete"] = "true"; |
| 145 expected_pairs["gcm_unreg_caller"] = "false"; |
| 146 |
| 147 // Verify data was formatted properly. |
| 148 std::string upload_data = fetcher->upload_data(); |
| 149 base::StringTokenizer data_tokenizer(upload_data, "&="); |
| 150 while (data_tokenizer.GetNext()) { |
| 151 std::map<std::string, std::string>::iterator iter = |
| 152 expected_pairs.find(data_tokenizer.token()); |
| 153 ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token(); |
| 154 ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token(); |
| 155 EXPECT_EQ(iter->second, data_tokenizer.token()); |
| 156 // Ensure that none of the keys appears twice. |
| 157 expected_pairs.erase(iter); |
| 158 } |
| 159 |
| 160 EXPECT_EQ(0UL, expected_pairs.size()); |
| 161 } |
| 162 |
| 163 TEST_F(UnregistrationRequestTest, SuccessfulUnregistration) { |
| 164 CreateRequest(); |
| 165 request_->Start(); |
| 166 |
| 167 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); |
| 168 CompleteFetch(); |
| 169 |
| 170 EXPECT_TRUE(callback_called_); |
| 171 EXPECT_TRUE(unregistration_successful_); |
| 172 } |
| 173 |
| 174 TEST_F(UnregistrationRequestTest, ResponseHttpStatusNotOK) { |
| 175 CreateRequest(); |
| 176 request_->Start(); |
| 177 |
| 178 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); |
| 179 CompleteFetch(); |
| 180 |
| 181 EXPECT_TRUE(callback_called_); |
| 182 EXPECT_FALSE(unregistration_successful_); |
| 183 } |
| 184 |
| 185 TEST_F(UnregistrationRequestTest, ResponseEmpty) { |
| 186 CreateRequest(); |
| 187 request_->Start(); |
| 188 |
| 189 SetResponseStatusAndString(net::HTTP_OK, ""); |
| 190 CompleteFetch(); |
| 191 |
| 192 EXPECT_FALSE(callback_called_); |
| 193 EXPECT_FALSE(unregistration_successful_); |
| 194 |
| 195 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); |
| 196 CompleteFetch(); |
| 197 |
| 198 EXPECT_TRUE(callback_called_); |
| 199 EXPECT_TRUE(unregistration_successful_); |
| 200 } |
| 201 |
| 202 TEST_F(UnregistrationRequestTest, InvalidParametersError) { |
| 203 CreateRequest(); |
| 204 request_->Start(); |
| 205 |
| 206 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); |
| 207 CompleteFetch(); |
| 208 |
| 209 EXPECT_TRUE(callback_called_); |
| 210 EXPECT_FALSE(unregistration_successful_); |
| 211 } |
| 212 |
| 213 TEST_F(UnregistrationRequestTest, UnkwnownError) { |
| 214 CreateRequest(); |
| 215 request_->Start(); |
| 216 |
| 217 SetResponseStatusAndString(net::HTTP_OK, "Error=XXX"); |
| 218 CompleteFetch(); |
| 219 |
| 220 EXPECT_TRUE(callback_called_); |
| 221 EXPECT_FALSE(unregistration_successful_); |
| 222 } |
| 223 |
| 224 TEST_F(UnregistrationRequestTest, ServiceUnavailable) { |
| 225 CreateRequest(); |
| 226 request_->Start(); |
| 227 |
| 228 SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE, ""); |
| 229 CompleteFetch(); |
| 230 |
| 231 EXPECT_FALSE(callback_called_); |
| 232 |
| 233 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); |
| 234 CompleteFetch(); |
| 235 |
| 236 EXPECT_TRUE(callback_called_); |
| 237 EXPECT_TRUE(unregistration_successful_); |
| 238 } |
| 239 |
| 240 TEST_F(UnregistrationRequestTest, InternalServerError) { |
| 241 CreateRequest(); |
| 242 request_->Start(); |
| 243 |
| 244 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, ""); |
| 245 CompleteFetch(); |
| 246 |
| 247 EXPECT_FALSE(callback_called_); |
| 248 |
| 249 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); |
| 250 CompleteFetch(); |
| 251 |
| 252 EXPECT_TRUE(callback_called_); |
| 253 EXPECT_TRUE(unregistration_successful_); |
| 254 } |
| 255 |
| 256 TEST_F(UnregistrationRequestTest, IncorrectAppId) { |
| 257 CreateRequest(); |
| 258 request_->Start(); |
| 259 |
| 260 SetResponseStatusAndString(net::HTTP_OK, "deleted=OtherTestAppId"); |
| 261 CompleteFetch(); |
| 262 |
| 263 EXPECT_FALSE(callback_called_); |
| 264 |
| 265 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); |
| 266 CompleteFetch(); |
| 267 |
| 268 EXPECT_TRUE(callback_called_); |
| 269 EXPECT_TRUE(unregistration_successful_); |
| 270 } |
| 271 |
| 272 TEST_F(UnregistrationRequestTest, ResponseParsingFailed) { |
| 273 CreateRequest(); |
| 274 request_->Start(); |
| 275 |
| 276 SetResponseStatusAndString(net::HTTP_OK, "some malformed response"); |
| 277 CompleteFetch(); |
| 278 |
| 279 EXPECT_FALSE(callback_called_); |
| 280 |
| 281 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); |
| 282 CompleteFetch(); |
| 283 |
| 284 EXPECT_TRUE(callback_called_); |
| 285 EXPECT_TRUE(unregistration_successful_); |
| 286 } |
| 287 |
| 288 } // namespace gcm |
OLD | NEW |