[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1 | // Copyright (c) 2009 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 <math.h> // ceil |
| 6 | |
| 7 | #include "base/compiler_specific.h" |
| 8 | #include "net/base/completion_callback.h" |
| 9 | #include "net/base/mock_host_resolver.h" |
| 10 | #include "net/base/ssl_config_service_defaults.h" |
| 11 | #include "net/base/ssl_info.h" |
| 12 | #include "net/base/test_completion_callback.h" |
| 13 | #include "net/base/upload_data.h" |
| 14 | #include "net/flip/flip_network_transaction.h" |
| 15 | #include "net/flip/flip_protocol.h" |
| 16 | #include "net/http/http_auth_handler_ntlm.h" |
| 17 | #include "net/http/http_network_session.h" |
| 18 | #include "net/http/http_network_transaction.h" |
| 19 | #include "net/http/http_transaction_unittest.h" |
| 20 | #include "net/proxy/proxy_config_service_fixed.h" |
| 21 | #include "net/socket/client_socket_factory.h" |
| 22 | #include "net/socket/socket_test_util.h" |
| 23 | #include "net/socket/ssl_client_socket.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 24 | #include "testing/platform_test.h" |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 28 | namespace { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 29 | |
| 30 | // Create a proxy service which fails on all requests (falls back to direct). |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 31 | net::ProxyService* CreateNullProxyService() { |
| 32 | return net::ProxyService::CreateNull(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Helper to manage the lifetimes of the dependencies for a |
| 36 | // HttpNetworkTransaction. |
| 37 | class SessionDependencies { |
| 38 | public: |
| 39 | // Default set of dependencies -- "null" proxy service. |
| 40 | SessionDependencies() |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 41 | : host_resolver(new net::MockHostResolver), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 42 | proxy_service(CreateNullProxyService()), |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 43 | ssl_config_service(new net::SSLConfigServiceDefaults) {} |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 44 | |
| 45 | // Custom proxy service dependency. |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 46 | explicit SessionDependencies(net::ProxyService* proxy_service) |
| 47 | : host_resolver(new net::MockHostResolver), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 48 | proxy_service(proxy_service), |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 49 | ssl_config_service(new net::SSLConfigServiceDefaults) {} |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 50 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 51 | scoped_refptr<net::MockHostResolverBase> host_resolver; |
| 52 | scoped_refptr<net::ProxyService> proxy_service; |
| 53 | scoped_refptr<net::SSLConfigService> ssl_config_service; |
| 54 | net::MockClientSocketFactory socket_factory; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 55 | }; |
| 56 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 57 | net::ProxyService* CreateFixedProxyService(const std::string& proxy) { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 58 | net::ProxyConfig proxy_config; |
| 59 | proxy_config.proxy_rules.ParseFromString(proxy); |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 60 | return net::ProxyService::CreateFixed(proxy_config); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 64 | net::HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
| 65 | return new net::HttpNetworkSession(session_deps->host_resolver, |
| 66 | session_deps->proxy_service, |
| 67 | &session_deps->socket_factory, |
| 68 | session_deps->ssl_config_service); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 71 | } // namespace |
| 72 | |
| 73 | namespace net { |
| 74 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 75 | class FlipNetworkTransactionTest : public PlatformTest { |
| 76 | public: |
| 77 | virtual void SetUp() { |
| 78 | // Disable compression on this test. |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 79 | flip::FlipFramer::set_enable_compression_default(false); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | virtual void TearDown() { |
| 83 | // Empty the current queue. |
| 84 | MessageLoop::current()->RunAllPending(); |
| 85 | PlatformTest::TearDown(); |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); |
| 90 | |
| 91 | struct SimpleGetHelperResult { |
| 92 | int rv; |
| 93 | std::string status_line; |
| 94 | std::string response_data; |
| 95 | }; |
| 96 | |
| 97 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[]) { |
| 98 | SimpleGetHelperResult out; |
| 99 | |
| 100 | SessionDependencies session_deps; |
| 101 | scoped_ptr<FlipNetworkTransaction> trans( |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 102 | new FlipNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 103 | |
| 104 | HttpRequestInfo request; |
| 105 | request.method = "GET"; |
| 106 | request.url = GURL("http://www.google.com/"); |
| 107 | request.load_flags = 0; |
| 108 | |
| 109 | StaticMockSocket data(data_reads, NULL); |
| 110 | session_deps.socket_factory.AddMockSocket(&data); |
| 111 | |
| 112 | TestCompletionCallback callback; |
| 113 | |
| 114 | int rv = trans->Start(&request, &callback, NULL); |
| 115 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 116 | |
| 117 | out.rv = callback.WaitForResult(); |
| 118 | if (out.rv != OK) |
| 119 | return out; |
| 120 | |
| 121 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 122 | EXPECT_TRUE(response->headers != NULL); |
| 123 | out.status_line = response->headers->GetStatusLine(); |
| 124 | |
| 125 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 126 | EXPECT_EQ(OK, rv); |
| 127 | |
| 128 | return out; |
| 129 | } |
| 130 | |
| 131 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 132 | int expected_status); |
| 133 | |
| 134 | void ConnectStatusHelper(const MockRead& status); |
| 135 | }; |
| 136 | |
| 137 | //----------------------------------------------------------------------------- |
| 138 | |
| 139 | // Verify FlipNetworkTransaction constructor. |
| 140 | TEST_F(FlipNetworkTransactionTest, Constructor) { |
| 141 | SessionDependencies session_deps; |
| 142 | scoped_refptr<net::HttpNetworkSession> session = |
| 143 | CreateSession(&session_deps); |
| 144 | scoped_ptr<HttpTransaction> trans(new FlipNetworkTransaction(session)); |
| 145 | } |
| 146 | |
| 147 | TEST_F(FlipNetworkTransactionTest, Connect) { |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame^] | 148 | static const unsigned char syn_reply[] = { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 149 | 0x80, 0x01, 0x00, 0x02, // header |
| 150 | 0x00, 0x00, 0x00, 0x45, |
| 151 | 0x00, 0x00, 0x00, 0x01, |
| 152 | 0x00, 0x00, 0x00, 0x04, // 4 headers |
| 153 | 0x00, 0x05, 'h', 'e', 'l', 'l', 'o', // "hello" |
| 154 | 0x00, 0x03, 'b', 'y', 'e', // "bye" |
| 155 | 0x00, 0x06, 's', 't', 'a', 't', 'u', 's', // "status" |
| 156 | 0x00, 0x03, '2', '0', '0', // "200" |
| 157 | 0x00, 0x03, 'u', 'r', 'l', // "url" |
| 158 | 0x00, 0x0a, '/', 'i', 'n', 'd', 'e', 'x', '.', 'p', 'h', 'p', // "HTTP/1.1" |
| 159 | 0x00, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n', // "version" |
| 160 | 0x00, 0x08, 'H', 'T', 'T', 'P', '/', '1', '.', '1', // "HTTP/1.1" |
| 161 | }; |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame^] | 162 | static const unsigned char body_frame[] = { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 163 | 0x00, 0x00, 0x00, 0x01, // header |
| 164 | 0x00, 0x00, 0x00, 0x06, |
| 165 | 'h', 'e', 'l', 'l', 'o', '!', // "hello" |
| 166 | }; |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame^] | 167 | static const unsigned char fin_frame[] = { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 168 | 0x80, 0x01, 0x00, 0x03, // header |
| 169 | 0x00, 0x00, 0x00, 0x08, |
| 170 | 0x00, 0x00, 0x00, 0x01, |
| 171 | 0x00, 0x00, 0x00, 0x00, |
| 172 | }; |
| 173 | |
| 174 | MockRead data_reads[] = { |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame^] | 175 | MockRead(true, reinterpret_cast<const char*>(syn_reply), sizeof(syn_reply)), |
| 176 | MockRead(true, reinterpret_cast<const char*>(body_frame), |
| 177 | sizeof(body_frame)), |
| 178 | MockRead(true, reinterpret_cast<const char*>(fin_frame), sizeof(fin_frame)), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 179 | MockRead(true, 0, 0), // EOF |
| 180 | }; |
| 181 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 182 | // We disable SSL for this test. |
| 183 | FlipSession::SetSSLMode(false); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 184 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
| 185 | EXPECT_EQ(OK, out.rv); |
| 186 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 187 | EXPECT_EQ("hello!", out.response_data); |
| 188 | } |
| 189 | |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame^] | 190 | // Test that the transaction doesn't crash when we don't have a reply. |
| 191 | TEST_F(FlipNetworkTransactionTest, ResponseWithoutSynReply) { |
| 192 | static const unsigned char body_frame[] = { |
| 193 | 0x00, 0x00, 0x00, 0x01, // header |
| 194 | 0x00, 0x00, 0x00, 0x06, |
| 195 | 'h', 'e', 'l', 'l', 'o', '!', // "hello" |
| 196 | }; |
| 197 | static const unsigned char fin_frame[] = { |
| 198 | 0x80, 0x01, 0x00, 0x03, // header |
| 199 | 0x00, 0x00, 0x00, 0x08, |
| 200 | 0x00, 0x00, 0x00, 0x01, |
| 201 | 0x00, 0x00, 0x00, 0x00, |
| 202 | }; |
| 203 | |
| 204 | MockRead data_reads[] = { |
| 205 | MockRead(true, reinterpret_cast<const char*>(body_frame), |
| 206 | sizeof(body_frame)), |
| 207 | MockRead(true, reinterpret_cast<const char*>(fin_frame), sizeof(fin_frame)), |
| 208 | MockRead(true, 0, 0), // EOF |
| 209 | }; |
| 210 | |
| 211 | // We disable SSL for this test. |
| 212 | FlipSession::SetSSLMode(false); |
| 213 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
| 214 | EXPECT_EQ(ERR_SYN_REPLY_NOT_RECEIVED, out.rv); |
| 215 | } |
| 216 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 217 | } // namespace net |
| 218 | |