| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bit_cast.h" | 8 #include "base/bit_cast.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/sync/base/cancelation_signal.h" |
| 16 #include "components/sync/core/http_bridge.h" |
| 17 #include "components/sync/core/http_post_provider_factory.h" |
| 15 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" | 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | 20 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
| 20 #include "sync/internal_api/public/base/cancelation_signal.h" | |
| 21 #include "sync/internal_api/public/http_bridge.h" | |
| 22 #include "sync/internal_api/public/http_post_provider_factory.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace syncer { | 25 namespace syncer { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. | 29 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. |
| 30 const base::FilePath::CharType kDocRoot[] = | 30 const base::FilePath::CharType kDocRoot[] = |
| 31 FILE_PATH_LITERAL("chrome/test/data"); | 31 FILE_PATH_LITERAL("chrome/test/data"); |
| 32 | 32 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 fake_default_request_context_getter_ = | 68 fake_default_request_context_getter_ = |
| 69 new net::TestURLRequestContextGetter(io_thread_.task_runner()); | 69 new net::TestURLRequestContextGetter(io_thread_.task_runner()); |
| 70 fake_default_request_context_getter_->AddRef(); | 70 fake_default_request_context_getter_->AddRef(); |
| 71 } | 71 } |
| 72 HttpBridge* bridge = | 72 HttpBridge* bridge = |
| 73 new HttpBridge(kUserAgent, fake_default_request_context_getter_, | 73 new HttpBridge(kUserAgent, fake_default_request_context_getter_, |
| 74 NetworkTimeUpdateCallback(), BindToTrackerCallback()); | 74 NetworkTimeUpdateCallback(), BindToTrackerCallback()); |
| 75 return bridge; | 75 return bridge; |
| 76 } | 76 } |
| 77 | 77 |
| 78 static void Abort(HttpBridge* bridge) { | 78 static void Abort(HttpBridge* bridge) { bridge->Abort(); } |
| 79 bridge->Abort(); | |
| 80 } | |
| 81 | 79 |
| 82 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race | 80 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race |
| 83 // condition. | 81 // condition. |
| 84 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, | 82 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, |
| 85 base::WaitableEvent* signal_when_released); | 83 base::WaitableEvent* signal_when_released); |
| 86 | 84 |
| 87 static void TestSameHttpNetworkSession(base::MessageLoop* main_message_loop, | 85 static void TestSameHttpNetworkSession(base::MessageLoop* main_message_loop, |
| 88 MAYBE_SyncHttpBridgeTest* test) { | 86 MAYBE_SyncHttpBridgeTest* test) { |
| 89 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); | 87 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); |
| 90 EXPECT_TRUE(test->GetTestRequestContextGetter()); | 88 EXPECT_TRUE(test->GetTestRequestContextGetter()); |
| 91 net::HttpNetworkSession* test_session = | 89 net::HttpNetworkSession* test_session = test->GetTestRequestContextGetter() |
| 92 test->GetTestRequestContextGetter()->GetURLRequestContext()-> | 90 ->GetURLRequestContext() |
| 93 http_transaction_factory()->GetSession(); | 91 ->http_transaction_factory() |
| 94 EXPECT_EQ(test_session, | 92 ->GetSession(); |
| 95 http_bridge->GetRequestContextGetterForTest()-> | 93 EXPECT_EQ(test_session, http_bridge->GetRequestContextGetterForTest() |
| 96 GetURLRequestContext()-> | 94 ->GetURLRequestContext() |
| 97 http_transaction_factory()->GetSession()); | 95 ->http_transaction_factory() |
| 96 ->GetSession()); |
| 98 main_message_loop->task_runner()->PostTask( | 97 main_message_loop->task_runner()->PostTask( |
| 99 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 98 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 100 } | 99 } |
| 101 | 100 |
| 102 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } | 101 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } |
| 103 | 102 |
| 104 // Note this is lazy created, so don't call this before your bridge. | 103 // Note this is lazy created, so don't call this before your bridge. |
| 105 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { | 104 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { |
| 106 return fake_default_request_context_getter_; | 105 return fake_default_request_context_getter_; |
| 107 } | 106 } |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 int response_code = 0; | 301 int response_code = 0; |
| 303 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 302 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 304 EXPECT_TRUE(success); | 303 EXPECT_TRUE(success); |
| 305 EXPECT_EQ(200, response_code); | 304 EXPECT_EQ(200, response_code); |
| 306 EXPECT_EQ(0, os_error); | 305 EXPECT_EQ(0, os_error); |
| 307 | 306 |
| 308 std::string response(http_bridge->GetResponseContent(), | 307 std::string response(http_bridge->GetResponseContent(), |
| 309 http_bridge->GetResponseContentLength()); | 308 http_bridge->GetResponseContentLength()); |
| 310 EXPECT_EQ(std::string::npos, response.find("Cookie:")); | 309 EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
| 311 EXPECT_NE(std::string::npos, | 310 EXPECT_NE(std::string::npos, |
| 312 response.find(base::StringPrintf("%s: %s", | 311 response.find(base::StringPrintf( |
| 313 net::HttpRequestHeaders::kUserAgent, kUserAgent))); | 312 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); |
| 314 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 313 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 315 } | 314 } |
| 316 | 315 |
| 317 TEST_F(MAYBE_SyncHttpBridgeTest, TestExtraRequestHeaders) { | 316 TEST_F(MAYBE_SyncHttpBridgeTest, TestExtraRequestHeaders) { |
| 318 ASSERT_TRUE(test_server_.Start()); | 317 ASSERT_TRUE(test_server_.Start()); |
| 319 | 318 |
| 320 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 319 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 321 | 320 |
| 322 GURL echo_header = test_server_.GetURL("/echoall"); | 321 GURL echo_header = test_server_.GetURL("/echoall"); |
| 323 | 322 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 434 |
| 436 // Schedule the fetch completion callback (but don't run it yet). Don't take | 435 // Schedule the fetch completion callback (but don't run it yet). Don't take |
| 437 // a reference to the bridge to mimic URLFetcher's handling of the delegate. | 436 // a reference to the bridge to mimic URLFetcher's handling of the delegate. |
| 438 net::URLFetcherDelegate* delegate = | 437 net::URLFetcherDelegate* delegate = |
| 439 static_cast<net::URLFetcherDelegate*>(bridge_for_race_test()); | 438 static_cast<net::URLFetcherDelegate*>(bridge_for_race_test()); |
| 440 std::string response_content = "success!"; | 439 std::string response_content = "success!"; |
| 441 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); | 440 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); |
| 442 fetcher.set_response_code(200); | 441 fetcher.set_response_code(200); |
| 443 fetcher.SetResponseString(response_content); | 442 fetcher.SetResponseString(response_content); |
| 444 ASSERT_TRUE(io_thread()->task_runner()->PostTask( | 443 ASSERT_TRUE(io_thread()->task_runner()->PostTask( |
| 445 FROM_HERE, | 444 FROM_HERE, base::Bind(&net::URLFetcherDelegate::OnURLFetchComplete, |
| 446 base::Bind(&net::URLFetcherDelegate::OnURLFetchComplete, | 445 base::Unretained(delegate), &fetcher))); |
| 447 base::Unretained(delegate), &fetcher))); | |
| 448 | 446 |
| 449 // Abort the fetch. This should be smart enough to handle the case where | 447 // Abort the fetch. This should be smart enough to handle the case where |
| 450 // the bridge is destroyed before the callback scheduled above completes. | 448 // the bridge is destroyed before the callback scheduled above completes. |
| 451 bridge_for_race_test()->Abort(); | 449 bridge_for_race_test()->Abort(); |
| 452 | 450 |
| 453 // Wait until the sync thread releases its ref on the bridge. | 451 // Wait until the sync thread releases its ref on the bridge. |
| 454 signal_when_released.Wait(); | 452 signal_when_released.Wait(); |
| 455 ASSERT_FALSE(bridge_for_race_test()); | 453 ASSERT_FALSE(bridge_for_race_test()); |
| 456 | 454 |
| 457 // Unleash the hounds. The fetch completion callback should fire first, and | 455 // Unleash the hounds. The fetch completion callback should fire first, and |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 | 573 |
| 576 // Sync thread: Finally run the posted task, only to find that our | 574 // Sync thread: Finally run the posted task, only to find that our |
| 577 // HttpBridgeFactory has been neutered. Should not crash. | 575 // HttpBridgeFactory has been neutered. Should not crash. |
| 578 factory->Init("TestUserAgent", BindToTrackerCallback()); | 576 factory->Init("TestUserAgent", BindToTrackerCallback()); |
| 579 | 577 |
| 580 // At this point, attempting to use the factory would trigger a crash. Both | 578 // At this point, attempting to use the factory would trigger a crash. Both |
| 581 // this test and the real world code should make sure this never happens. | 579 // this test and the real world code should make sure this never happens. |
| 582 } | 580 } |
| 583 | 581 |
| 584 } // namespace syncer | 582 } // namespace syncer |
| OLD | NEW |