Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/metrics/field_trial.h" | |
| 5 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 6 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "base/test/mock_entropy_provider.h" | |
| 7 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 8 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
| 9 #include "net/test/spawned_test_server/spawned_test_server.h" | 11 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 10 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 11 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 13 #include "sync/internal_api/public/base/cancelation_signal.h" | 15 #include "sync/internal_api/public/base/cancelation_signal.h" |
| 14 #include "sync/internal_api/public/http_bridge.h" | 16 #include "sync/internal_api/public/http_bridge.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace syncer { | 19 namespace syncer { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. | 22 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. |
| 21 const base::FilePath::CharType kDocRoot[] = | 23 const base::FilePath::CharType kDocRoot[] = |
| 22 FILE_PATH_LITERAL("chrome/test/data"); | 24 FILE_PATH_LITERAL("chrome/test/data"); |
| 23 } | 25 } |
| 24 | 26 |
| 25 const char kUserAgent[] = "user-agent"; | 27 const char kUserAgent[] = "user-agent"; |
| 26 | 28 |
| 29 bool GzipUncompress(const char* input, int length, std::string* output) { | |
|
Alexei Svitkine (slow)
2015/07/25 15:50:36
Ditto here:
https://code.google.com/p/chromium/co
Gang Wu
2015/07/27 22:35:06
same as previous one.
| |
| 30 z_stream stream = {0}; | |
| 31 | |
| 32 stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(input)); | |
| 33 stream.avail_in = length; | |
| 34 stream.next_out = reinterpret_cast<uint8*>(&(*output)[0]); | |
| 35 stream.avail_out = output->size(); | |
| 36 | |
| 37 bool success = false; | |
| 38 while (stream.avail_in > 0 && stream.avail_out > 0) { | |
| 39 // 16 is added to read in gzip format. | |
| 40 int result = inflateInit2(&stream, MAX_WBITS + 16); | |
| 41 DCHECK_EQ(Z_OK, result); | |
| 42 | |
| 43 result = inflate(&stream, Z_FINISH); | |
| 44 success = (result == Z_STREAM_END); | |
| 45 if (!success) { | |
| 46 DVLOG(2) << "inflate() failed. Result: " << result; | |
| 47 break; | |
| 48 } | |
| 49 | |
| 50 result = inflateEnd(&stream); | |
| 51 DCHECK(result == Z_OK); | |
| 52 } | |
| 53 | |
| 54 if (stream.avail_in == 0) { | |
| 55 success = true; | |
| 56 output->resize(output->size() - stream.avail_out); | |
| 57 } | |
| 58 return success; | |
| 59 } | |
| 60 | |
| 27 class SyncHttpBridgeTest : public testing::Test { | 61 class SyncHttpBridgeTest : public testing::Test { |
| 28 public: | 62 public: |
| 29 SyncHttpBridgeTest() | 63 SyncHttpBridgeTest() |
| 30 : test_server_(net::SpawnedTestServer::TYPE_HTTP, | 64 : test_server_(net::SpawnedTestServer::TYPE_HTTP, |
| 31 net::SpawnedTestServer::kLocalhost, | 65 net::SpawnedTestServer::kLocalhost, |
| 32 base::FilePath(kDocRoot)), | 66 base::FilePath(kDocRoot)), |
| 33 fake_default_request_context_getter_(NULL), | 67 fake_default_request_context_getter_(NULL), |
| 34 bridge_for_race_test_(NULL), | 68 bridge_for_race_test_(NULL), |
| 35 io_thread_("IO thread") { | 69 io_thread_("IO thread") { |
| 36 } | 70 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 private: | 173 private: |
| 140 ~ShuntedHttpBridge() override {} | 174 ~ShuntedHttpBridge() override {} |
| 141 | 175 |
| 142 void CallOnURLFetchComplete() { | 176 void CallOnURLFetchComplete() { |
| 143 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); | 177 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); |
| 144 // We return no cookies and a dummy content response. | 178 // We return no cookies and a dummy content response. |
| 145 net::ResponseCookies cookies; | 179 net::ResponseCookies cookies; |
| 146 | 180 |
| 147 std::string response_content = "success!"; | 181 std::string response_content = "success!"; |
| 148 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); | 182 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); |
| 183 scoped_refptr<net::HttpResponseHeaders> response_headers( | |
| 184 new net::HttpResponseHeaders("")); | |
| 149 fetcher.set_response_code(200); | 185 fetcher.set_response_code(200); |
| 150 fetcher.set_cookies(cookies); | 186 fetcher.set_cookies(cookies); |
| 151 fetcher.SetResponseString(response_content); | 187 fetcher.SetResponseString(response_content); |
| 188 fetcher.set_response_headers(response_headers); | |
| 152 OnURLFetchComplete(&fetcher); | 189 OnURLFetchComplete(&fetcher); |
| 153 } | 190 } |
| 154 SyncHttpBridgeTest* test_; | 191 SyncHttpBridgeTest* test_; |
| 155 bool never_finishes_; | 192 bool never_finishes_; |
| 156 }; | 193 }; |
| 157 | 194 |
| 158 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( | 195 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( |
| 159 base::WaitableEvent* signal_when_created, | 196 base::WaitableEvent* signal_when_created, |
| 160 base::WaitableEvent* signal_when_released) { | 197 base::WaitableEvent* signal_when_released) { |
| 161 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 198 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 262 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 226 EXPECT_TRUE(success); | 263 EXPECT_TRUE(success); |
| 227 EXPECT_EQ(200, response_code); | 264 EXPECT_EQ(200, response_code); |
| 228 EXPECT_EQ(0, os_error); | 265 EXPECT_EQ(0, os_error); |
| 229 | 266 |
| 230 EXPECT_EQ(payload.length() + 1, | 267 EXPECT_EQ(payload.length() + 1, |
| 231 static_cast<size_t>(http_bridge->GetResponseContentLength())); | 268 static_cast<size_t>(http_bridge->GetResponseContentLength())); |
| 232 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); | 269 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
| 233 } | 270 } |
| 234 | 271 |
| 272 // Full round-trip test of the HttpBridge with compress data, using default | |
| 273 // UA string and no request cookies. | |
| 274 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveWithCompressedPayload) { | |
| 275 ASSERT_TRUE(test_server_.Start()); | |
| 276 | |
| 277 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | |
| 278 | |
| 279 std::string payload = "this should be echoed back"; | |
| 280 GURL echo = test_server_.GetURL("echo"); | |
| 281 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); | |
| 282 http_bridge->SetPostPayload("application/x-www-form-urlencoded", | |
| 283 payload.length() + 1, payload.c_str()); | |
| 284 int os_error = 0; | |
| 285 int response_code = 0; | |
| 286 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | |
| 287 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", | |
| 288 "Enabled"); | |
| 289 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | |
| 290 EXPECT_TRUE(success); | |
| 291 EXPECT_EQ(200, response_code); | |
| 292 EXPECT_EQ(0, os_error); | |
| 293 | |
| 294 EXPECT_NE(payload.length() + 1, | |
| 295 static_cast<size_t>(http_bridge->GetResponseContentLength())); | |
| 296 std::string uncompressed_payload(payload.size() * 10, ' '); | |
| 297 GzipUncompress(http_bridge->GetResponseContent(), | |
| 298 http_bridge->GetResponseContentLength(), | |
| 299 &uncompressed_payload); | |
| 300 EXPECT_EQ(payload.length() + 1, uncompressed_payload.size()); | |
| 301 EXPECT_EQ(payload + '\0', uncompressed_payload); | |
| 302 } | |
| 303 | |
| 304 // Full round-trip test of the HttpBridge with compression. | |
| 305 TEST_F(SyncHttpBridgeTest, | |
| 306 TestMakeSynchronousPostLiveComprehensiveWithCompression) { | |
| 307 ASSERT_TRUE(test_server_.Start()); | |
| 308 | |
| 309 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | |
| 310 | |
| 311 GURL echo_header = test_server_.GetURL("echoall"); | |
| 312 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | |
| 313 | |
| 314 std::string test_payload = "###TEST PAYLOAD###"; | |
| 315 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | |
| 316 test_payload.c_str()); | |
| 317 | |
| 318 int os_error = 0; | |
| 319 int response_code = 0; | |
| 320 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | |
| 321 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", | |
| 322 "Enabled"); | |
| 323 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | |
| 324 EXPECT_TRUE(success); | |
| 325 EXPECT_EQ(200, response_code); | |
| 326 EXPECT_EQ(0, os_error); | |
| 327 | |
| 328 std::string response(http_bridge->GetResponseContent(), | |
| 329 http_bridge->GetResponseContentLength()); | |
| 330 EXPECT_NE(std::string::npos, response.find("Content-Encoding: gzip")); | |
| 331 EXPECT_NE(std::string::npos, response.find("Accept-Encoding: gzip, deflate")); | |
| 332 EXPECT_NE(std::string::npos, | |
| 333 response.find(base::StringPrintf( | |
| 334 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); | |
| 335 } | |
| 336 | |
| 235 // Full round-trip test of the HttpBridge. | 337 // Full round-trip test of the HttpBridge. |
| 236 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { | 338 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
| 237 ASSERT_TRUE(test_server_.Start()); | 339 ASSERT_TRUE(test_server_.Start()); |
| 238 | 340 |
| 239 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 341 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 240 | 342 |
| 241 GURL echo_header = test_server_.GetURL("echoall"); | 343 GURL echo_header = test_server_.GetURL("echoall"); |
| 242 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 344 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 243 | 345 |
| 244 std::string test_payload = "###TEST PAYLOAD###"; | 346 std::string test_payload = "###TEST PAYLOAD###"; |
| 245 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 347 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 246 test_payload.c_str()); | 348 test_payload.c_str()); |
| 247 | 349 |
| 248 int os_error = 0; | 350 int os_error = 0; |
| 249 int response_code = 0; | 351 int response_code = 0; |
| 250 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 352 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 251 EXPECT_TRUE(success); | 353 EXPECT_TRUE(success); |
| 252 EXPECT_EQ(200, response_code); | 354 EXPECT_EQ(200, response_code); |
| 253 EXPECT_EQ(0, os_error); | 355 EXPECT_EQ(0, os_error); |
| 254 | 356 |
| 255 std::string response(http_bridge->GetResponseContent(), | 357 std::string response(http_bridge->GetResponseContent(), |
| 256 http_bridge->GetResponseContentLength()); | 358 http_bridge->GetResponseContentLength()); |
| 257 EXPECT_EQ(std::string::npos, response.find("Cookie:")); | 359 EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
| 360 EXPECT_NE(std::string::npos, response.find("Accept-Encoding: deflate")); | |
| 258 EXPECT_NE(std::string::npos, | 361 EXPECT_NE(std::string::npos, |
| 259 response.find(base::StringPrintf("%s: %s", | 362 response.find(base::StringPrintf("%s: %s", |
| 260 net::HttpRequestHeaders::kUserAgent, kUserAgent))); | 363 net::HttpRequestHeaders::kUserAgent, kUserAgent))); |
| 261 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 364 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 262 } | 365 } |
| 263 | 366 |
| 264 TEST_F(SyncHttpBridgeTest, TestExtraRequestHeaders) { | 367 TEST_F(SyncHttpBridgeTest, TestExtraRequestHeaders) { |
| 265 ASSERT_TRUE(test_server_.Start()); | 368 ASSERT_TRUE(test_server_.Start()); |
| 266 | 369 |
| 267 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 370 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 | 615 |
| 513 // Sync thread: Finally run the posted task, only to find that our | 616 // Sync thread: Finally run the posted task, only to find that our |
| 514 // HttpBridgeFactory has been neutered. Should not crash. | 617 // HttpBridgeFactory has been neutered. Should not crash. |
| 515 factory->Init("TestUserAgent"); | 618 factory->Init("TestUserAgent"); |
| 516 | 619 |
| 517 // At this point, attempting to use the factory would trigger a crash. Both | 620 // At this point, attempting to use the factory would trigger a crash. Both |
| 518 // this test and the real world code should make sure this never happens. | 621 // this test and the real world code should make sure this never happens. |
| 519 }; | 622 }; |
| 520 | 623 |
| 521 } // namespace syncer | 624 } // namespace syncer |
| OLD | NEW |