http_network_layer_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright (c) 2012 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. #include "net/http/http_network_layer.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/strings/stringprintf.h"
  8. #include "net/cert/ct_policy_enforcer.h"
  9. #include "net/cert/mock_cert_verifier.h"
  10. #include "net/dns/mock_host_resolver.h"
  11. #include "net/http/http_network_session.h"
  12. #include "net/http/http_server_properties.h"
  13. #include "net/http/http_transaction_test_util.h"
  14. #include "net/http/transport_security_state.h"
  15. #include "net/log/net_log_with_source.h"
  16. #include "net/proxy_resolution/configured_proxy_resolution_service.h"
  17. #include "net/quic/quic_context.h"
  18. #include "net/socket/socket_test_util.h"
  19. #include "net/spdy/spdy_session_pool.h"
  20. #include "net/ssl/ssl_config_service_defaults.h"
  21. #include "net/test/gtest_util.h"
  22. #include "net/test/test_with_task_environment.h"
  23. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  24. #include "testing/gmock/include/gmock/gmock.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. #include "testing/platform_test.h"
  27. using net::test::IsError;
  28. using net::test::IsOk;
  29. namespace net {
  30. namespace {
  31. class HttpNetworkLayerTest : public PlatformTest, public WithTaskEnvironment {
  32. protected:
  33. HttpNetworkLayerTest()
  34. : ssl_config_service_(std::make_unique<SSLConfigServiceDefaults>()) {}
  35. void SetUp() override {
  36. ConfigureTestDependencies(ConfiguredProxyResolutionService::CreateDirect());
  37. }
  38. void ConfigureTestDependencies(
  39. std::unique_ptr<ConfiguredProxyResolutionService>
  40. proxy_resolution_service) {
  41. cert_verifier_ = std::make_unique<MockCertVerifier>();
  42. transport_security_state_ = std::make_unique<TransportSecurityState>();
  43. proxy_resolution_service_ = std::move(proxy_resolution_service);
  44. HttpNetworkSessionContext session_context;
  45. session_context.client_socket_factory = &mock_socket_factory_;
  46. session_context.host_resolver = &host_resolver_;
  47. session_context.cert_verifier = cert_verifier_.get();
  48. session_context.transport_security_state = transport_security_state_.get();
  49. session_context.ct_policy_enforcer = &ct_policy_enforcer_;
  50. session_context.proxy_resolution_service = proxy_resolution_service_.get();
  51. session_context.ssl_config_service = ssl_config_service_.get();
  52. session_context.http_server_properties = &http_server_properties_;
  53. session_context.quic_context = &quic_context_;
  54. network_session_ = std::make_unique<HttpNetworkSession>(
  55. HttpNetworkSessionParams(), session_context);
  56. factory_ = std::make_unique<HttpNetworkLayer>(network_session_.get());
  57. }
  58. void ExecuteRequestExpectingContentAndHeader(const std::string& method,
  59. const std::string& content,
  60. const std::string& header,
  61. const std::string& value) {
  62. TestCompletionCallback callback;
  63. HttpRequestInfo request_info;
  64. request_info.url = GURL("http://www.google.com/");
  65. request_info.method = method;
  66. request_info.load_flags = LOAD_NORMAL;
  67. request_info.traffic_annotation =
  68. net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
  69. std::unique_ptr<HttpTransaction> trans;
  70. int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  71. EXPECT_THAT(rv, IsOk());
  72. rv = trans->Start(&request_info, callback.callback(), NetLogWithSource());
  73. if (rv == ERR_IO_PENDING)
  74. rv = callback.WaitForResult();
  75. ASSERT_THAT(rv, IsOk());
  76. std::string contents;
  77. rv = ReadTransaction(trans.get(), &contents);
  78. EXPECT_THAT(rv, IsOk());
  79. EXPECT_EQ(content, contents);
  80. if (!header.empty()) {
  81. // We also have a server header here that isn't set by the proxy.
  82. EXPECT_TRUE(trans->GetResponseInfo()->headers->HasHeaderValue(
  83. header, value));
  84. }
  85. }
  86. // Check that |proxy_count| proxies are in the retry list.
  87. // These will be, in order, |bad_proxy| and |bad_proxy2|".
  88. void TestBadProxies(unsigned int proxy_count, const std::string& bad_proxy,
  89. const std::string& bad_proxy2) {
  90. const ProxyRetryInfoMap& retry_info =
  91. proxy_resolution_service_->proxy_retry_info();
  92. ASSERT_EQ(proxy_count, retry_info.size());
  93. if (proxy_count > 0)
  94. ASSERT_TRUE(retry_info.find(bad_proxy) != retry_info.end());
  95. if (proxy_count > 1)
  96. ASSERT_TRUE(retry_info.find(bad_proxy2) != retry_info.end());
  97. }
  98. // Simulates a request through a proxy which returns a bypass, which is then
  99. // retried through a second proxy that doesn't bypass.
  100. // Checks that the expected requests were issued, the expected content was
  101. // received, and the first proxy |bad_proxy| was marked as bad.
  102. void TestProxyFallback(const std::string& bad_proxy) {
  103. MockRead data_reads[] = {
  104. MockRead("HTTP/1.1 200 OK\r\n"
  105. "Chrome-Proxy: bypass=0\r\n\r\n"),
  106. MockRead("Bypass message"),
  107. MockRead(SYNCHRONOUS, OK),
  108. };
  109. TestProxyFallbackWithMockReads(bad_proxy, "", data_reads, 1u);
  110. }
  111. void TestProxyFallbackWithMockReads(const std::string& bad_proxy,
  112. const std::string& bad_proxy2,
  113. base::span<const MockRead> data_reads,
  114. unsigned int expected_retry_info_size) {
  115. TestProxyFallbackByMethodWithMockReads(bad_proxy, bad_proxy2, data_reads,
  116. "GET", "content", true,
  117. expected_retry_info_size);
  118. }
  119. void TestProxyFallbackByMethodWithMockReads(
  120. const std::string& bad_proxy,
  121. const std::string& bad_proxy2,
  122. base::span<const MockRead> data_reads,
  123. std::string method,
  124. std::string content,
  125. bool retry_expected,
  126. unsigned int expected_retry_info_size) {
  127. std::string trailer =
  128. (method == "HEAD" || method == "PUT" || method == "POST") ?
  129. "Content-Length: 0\r\n\r\n" : "\r\n";
  130. std::string request =
  131. base::StringPrintf("%s http://www.google.com/ HTTP/1.1\r\n"
  132. "Host: www.google.com\r\n"
  133. "Proxy-Connection: keep-alive\r\n"
  134. "%s", method.c_str(), trailer.c_str());
  135. MockWrite data_writes[] = {
  136. MockWrite(request.c_str()),
  137. };
  138. StaticSocketDataProvider data1(data_reads, data_writes);
  139. mock_socket_factory_.AddSocketDataProvider(&data1);
  140. // Second data provider returns the expected content.
  141. MockRead data_reads2[3];
  142. size_t data_reads2_index = 0;
  143. data_reads2[data_reads2_index++] = MockRead("HTTP/1.0 200 OK\r\n"
  144. "Server: not-proxy\r\n\r\n");
  145. if (!content.empty())
  146. data_reads2[data_reads2_index++] = MockRead(content.c_str());
  147. data_reads2[data_reads2_index++] = MockRead(SYNCHRONOUS, OK);
  148. MockWrite data_writes2[] = {
  149. MockWrite(request.c_str()),
  150. };
  151. StaticSocketDataProvider data2(
  152. base::make_span(data_reads2, data_reads2_index), data_writes2);
  153. mock_socket_factory_.AddSocketDataProvider(&data2);
  154. // Expect that we get "content" and not "Bypass message", and that there's
  155. // a "not-proxy" "Server:" header in the final response.
  156. if (retry_expected) {
  157. ExecuteRequestExpectingContentAndHeader(method, content,
  158. "server", "not-proxy");
  159. } else {
  160. ExecuteRequestExpectingContentAndHeader(method, content, "", "");
  161. }
  162. // We should also observe the bad proxy in the retry list.
  163. TestBadProxies(expected_retry_info_size, bad_proxy, bad_proxy2);
  164. }
  165. // Simulates a request through a proxy which returns a bypass, which is then
  166. // retried through a direct connection to the origin site.
  167. // Checks that the expected requests were issued, the expected content was
  168. // received, and the proxy |bad_proxy| was marked as bad.
  169. void TestProxyFallbackToDirect(const std::string& bad_proxy) {
  170. MockRead data_reads[] = {
  171. MockRead("HTTP/1.1 200 OK\r\n"
  172. "Chrome-Proxy: bypass=0\r\n\r\n"),
  173. MockRead("Bypass message"),
  174. MockRead(SYNCHRONOUS, OK),
  175. };
  176. MockWrite data_writes[] = {
  177. MockWrite("GET http://www.google.com/ HTTP/1.1\r\n"
  178. "Host: www.google.com\r\n"
  179. "Proxy-Connection: keep-alive\r\n\r\n"),
  180. };
  181. StaticSocketDataProvider data1(data_reads, data_writes);
  182. mock_socket_factory_.AddSocketDataProvider(&data1);
  183. // Second data provider returns the expected content.
  184. MockRead data_reads2[] = {
  185. MockRead("HTTP/1.0 200 OK\r\n"
  186. "Server: not-proxy\r\n\r\n"),
  187. MockRead("content"),
  188. MockRead(SYNCHRONOUS, OK),
  189. };
  190. MockWrite data_writes2[] = {
  191. MockWrite("GET / HTTP/1.1\r\n"
  192. "Host: www.google.com\r\n"
  193. "Connection: keep-alive\r\n\r\n"),
  194. };
  195. StaticSocketDataProvider data2(data_reads2, data_writes2);
  196. mock_socket_factory_.AddSocketDataProvider(&data2);
  197. // Expect that we get "content" and not "Bypass message", and that there's
  198. // a "not-proxy" "Server:" header in the final response.
  199. ExecuteRequestExpectingContentAndHeader("GET", "content",
  200. "server", "not-proxy");
  201. // We should also observe the bad proxy in the retry list.
  202. TestBadProxies(1u, bad_proxy, "");
  203. }
  204. // Simulates a request through a proxy which returns a bypass, under a
  205. // configuration where there is no valid bypass. |proxy_count| proxies
  206. // are expected to be configured.
  207. // Checks that the expected requests were issued, the bypass message was the
  208. // final received content, and all proxies were marked as bad.
  209. void TestProxyFallbackFail(unsigned int proxy_count,
  210. const std::string& bad_proxy,
  211. const std::string& bad_proxy2) {
  212. MockRead data_reads[] = {
  213. MockRead("HTTP/1.1 200 OK\r\n"
  214. "Chrome-Proxy: bypass=0\r\n\r\n"),
  215. MockRead("Bypass message"),
  216. MockRead(SYNCHRONOUS, OK),
  217. };
  218. MockWrite data_writes[] = {
  219. MockWrite("GET http://www.google.com/ HTTP/1.1\r\n"
  220. "Host: www.google.com\r\n"
  221. "Proxy-Connection: keep-alive\r\n\r\n"),
  222. };
  223. StaticSocketDataProvider data1(data_reads, data_writes);
  224. StaticSocketDataProvider data2(data_reads, data_writes);
  225. mock_socket_factory_.AddSocketDataProvider(&data1);
  226. if (proxy_count > 1)
  227. mock_socket_factory_.AddSocketDataProvider(&data2);
  228. // Expect that we get "Bypass message", and not "content"..
  229. ExecuteRequestExpectingContentAndHeader("GET", "Bypass message", "", "");
  230. // We should also observe the bad proxy or proxies in the retry list.
  231. TestBadProxies(proxy_count, bad_proxy, bad_proxy2);
  232. }
  233. MockClientSocketFactory mock_socket_factory_;
  234. MockHostResolver host_resolver_{
  235. /*default_result=*/
  236. MockHostResolverBase::RuleResolver::GetLocalhostResult()};
  237. std::unique_ptr<CertVerifier> cert_verifier_;
  238. std::unique_ptr<TransportSecurityState> transport_security_state_;
  239. DefaultCTPolicyEnforcer ct_policy_enforcer_;
  240. std::unique_ptr<ProxyResolutionService> proxy_resolution_service_;
  241. std::unique_ptr<SSLConfigService> ssl_config_service_;
  242. QuicContext quic_context_;
  243. std::unique_ptr<HttpNetworkSession> network_session_;
  244. std::unique_ptr<HttpNetworkLayer> factory_;
  245. private:
  246. HttpServerProperties http_server_properties_;
  247. };
  248. TEST_F(HttpNetworkLayerTest, CreateAndDestroy) {
  249. std::unique_ptr<HttpTransaction> trans;
  250. int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  251. EXPECT_THAT(rv, IsOk());
  252. EXPECT_TRUE(trans.get() != nullptr);
  253. }
  254. TEST_F(HttpNetworkLayerTest, Suspend) {
  255. std::unique_ptr<HttpTransaction> trans;
  256. int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  257. EXPECT_THAT(rv, IsOk());
  258. trans.reset();
  259. factory_->OnSuspend();
  260. rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  261. EXPECT_THAT(rv, IsError(ERR_NETWORK_IO_SUSPENDED));
  262. ASSERT_TRUE(trans == nullptr);
  263. factory_->OnResume();
  264. rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  265. EXPECT_THAT(rv, IsOk());
  266. }
  267. TEST_F(HttpNetworkLayerTest, GET) {
  268. MockRead data_reads[] = {
  269. MockRead("HTTP/1.0 200 OK\r\n\r\n"),
  270. MockRead("hello world"),
  271. MockRead(SYNCHRONOUS, OK),
  272. };
  273. MockWrite data_writes[] = {
  274. MockWrite("GET / HTTP/1.1\r\n"
  275. "Host: www.google.com\r\n"
  276. "Connection: keep-alive\r\n"
  277. "User-Agent: Foo/1.0\r\n\r\n"),
  278. };
  279. StaticSocketDataProvider data(data_reads, data_writes);
  280. mock_socket_factory_.AddSocketDataProvider(&data);
  281. TestCompletionCallback callback;
  282. HttpRequestInfo request_info;
  283. request_info.url = GURL("http://www.google.com/");
  284. request_info.method = "GET";
  285. request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent,
  286. "Foo/1.0");
  287. request_info.load_flags = LOAD_NORMAL;
  288. request_info.traffic_annotation =
  289. net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
  290. std::unique_ptr<HttpTransaction> trans;
  291. int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  292. EXPECT_THAT(rv, IsOk());
  293. rv = trans->Start(&request_info, callback.callback(), NetLogWithSource());
  294. rv = callback.GetResult(rv);
  295. ASSERT_THAT(rv, IsOk());
  296. std::string contents;
  297. rv = ReadTransaction(trans.get(), &contents);
  298. EXPECT_THAT(rv, IsOk());
  299. EXPECT_EQ("hello world", contents);
  300. }
  301. TEST_F(HttpNetworkLayerTest, NetworkVerified) {
  302. MockRead data_reads[] = {
  303. MockRead("HTTP/1.0 200 OK\r\n\r\n"),
  304. MockRead("hello world"),
  305. MockRead(SYNCHRONOUS, OK),
  306. };
  307. MockWrite data_writes[] = {
  308. MockWrite("GET / HTTP/1.1\r\n"
  309. "Host: www.google.com\r\n"
  310. "Connection: keep-alive\r\n"
  311. "User-Agent: Foo/1.0\r\n\r\n"),
  312. };
  313. StaticSocketDataProvider data(data_reads, data_writes);
  314. mock_socket_factory_.AddSocketDataProvider(&data);
  315. TestCompletionCallback callback;
  316. HttpRequestInfo request_info;
  317. request_info.url = GURL("http://www.google.com/");
  318. request_info.method = "GET";
  319. request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent,
  320. "Foo/1.0");
  321. request_info.load_flags = LOAD_NORMAL;
  322. request_info.traffic_annotation =
  323. net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
  324. std::unique_ptr<HttpTransaction> trans;
  325. int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  326. EXPECT_THAT(rv, IsOk());
  327. rv = trans->Start(&request_info, callback.callback(), NetLogWithSource());
  328. ASSERT_THAT(callback.GetResult(rv), IsOk());
  329. EXPECT_TRUE(trans->GetResponseInfo()->network_accessed);
  330. }
  331. TEST_F(HttpNetworkLayerTest, NetworkUnVerified) {
  332. MockRead data_reads[] = {
  333. MockRead(ASYNC, ERR_CONNECTION_RESET),
  334. };
  335. MockWrite data_writes[] = {
  336. MockWrite("GET / HTTP/1.1\r\n"
  337. "Host: www.google.com\r\n"
  338. "Connection: keep-alive\r\n"
  339. "User-Agent: Foo/1.0\r\n\r\n"),
  340. };
  341. StaticSocketDataProvider data(data_reads, data_writes);
  342. mock_socket_factory_.AddSocketDataProvider(&data);
  343. TestCompletionCallback callback;
  344. HttpRequestInfo request_info;
  345. request_info.url = GURL("http://www.google.com/");
  346. request_info.method = "GET";
  347. request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent,
  348. "Foo/1.0");
  349. request_info.load_flags = LOAD_NORMAL;
  350. request_info.traffic_annotation =
  351. net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
  352. std::unique_ptr<HttpTransaction> trans;
  353. int rv = factory_->CreateTransaction(DEFAULT_PRIORITY, &trans);
  354. EXPECT_THAT(rv, IsOk());
  355. rv = trans->Start(&request_info, callback.callback(), NetLogWithSource());
  356. ASSERT_THAT(callback.GetResult(rv), IsError(ERR_CONNECTION_RESET));
  357. // network_accessed is true; the HTTP stack did try to make a connection.
  358. EXPECT_TRUE(trans->GetResponseInfo()->network_accessed);
  359. }
  360. } // namespace
  361. } // namespace net