OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "net/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 2109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2120 EXPECT_TRUE(sock->IsConnected()); | 2120 EXPECT_TRUE(sock->IsConnected()); |
2121 log.GetEntries(&entries); | 2121 log.GetEntries(&entries); |
2122 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 2122 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
2123 | 2123 |
2124 EXPECT_FALSE(sock->signed_cert_timestamps_received_); | 2124 EXPECT_FALSE(sock->signed_cert_timestamps_received_); |
2125 | 2125 |
2126 sock->Disconnect(); | 2126 sock->Disconnect(); |
2127 EXPECT_FALSE(sock->IsConnected()); | 2127 EXPECT_FALSE(sock->IsConnected()); |
2128 } | 2128 } |
2129 | 2129 |
| 2130 // Tests that IsConnectedAndIdle and WasEverUsed behave as expected. |
| 2131 TEST_F(SSLClientSocketTest, ReuseStates) { |
| 2132 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 2133 SpawnedTestServer::kLocalhost, |
| 2134 base::FilePath()); |
| 2135 ASSERT_TRUE(test_server.Start()); |
| 2136 |
| 2137 AddressList addr; |
| 2138 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 2139 |
| 2140 TestCompletionCallback callback; |
| 2141 scoped_ptr<StreamSocket> transport( |
| 2142 new TCPClientSocket(addr, NULL, NetLog::Source())); |
| 2143 int rv = transport->Connect(callback.callback()); |
| 2144 if (rv == ERR_IO_PENDING) |
| 2145 rv = callback.WaitForResult(); |
| 2146 EXPECT_EQ(OK, rv); |
| 2147 |
| 2148 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 2149 transport.Pass(), test_server.host_port_pair(), kDefaultSSLConfig)); |
| 2150 |
| 2151 rv = sock->Connect(callback.callback()); |
| 2152 if (rv == ERR_IO_PENDING) |
| 2153 rv = callback.WaitForResult(); |
| 2154 EXPECT_EQ(OK, rv); |
| 2155 |
| 2156 // The socket was just connected. It should be idle because it is speaking |
| 2157 // HTTP. Although the transport has been used for the handshake, WasEverUsed() |
| 2158 // returns false. |
| 2159 EXPECT_TRUE(sock->IsConnected()); |
| 2160 EXPECT_TRUE(sock->IsConnectedAndIdle()); |
| 2161 EXPECT_FALSE(sock->WasEverUsed()); |
| 2162 |
| 2163 const char kRequestText[] = "GET / HTTP/1.0\r\n\r\n"; |
| 2164 const size_t kRequestLen = arraysize(kRequestText) - 1; |
| 2165 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kRequestLen)); |
| 2166 memcpy(request_buffer->data(), kRequestText, kRequestLen); |
| 2167 |
| 2168 rv = sock->Write(request_buffer.get(), kRequestLen, callback.callback()); |
| 2169 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 2170 |
| 2171 if (rv == ERR_IO_PENDING) |
| 2172 rv = callback.WaitForResult(); |
| 2173 EXPECT_EQ(static_cast<int>(kRequestLen), rv); |
| 2174 |
| 2175 // The socket has now been used. |
| 2176 EXPECT_TRUE(sock->WasEverUsed()); |
| 2177 |
| 2178 // TODO(davidben): Read one byte to ensure the test server has responded and |
| 2179 // then assert IsConnectedAndIdle is false. This currently doesn't work |
| 2180 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their |
| 2181 // SSL implementation's internal buffers. Either call PR_Available and |
| 2182 // SSL_pending, although the former isn't actually implemented or perhaps |
| 2183 // attempt to read one byte extra. |
| 2184 } |
| 2185 |
2130 } // namespace net | 2186 } // namespace net |
OLD | NEW |