Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 "chrome/browser/extensions/api/socket/tls_socket.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/strings/string_piece.h" | |
| 9 #include "net/base/address_list.h" | |
| 10 #include "net/base/completion_callback.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/base/rand_callback.h" | |
| 14 #include "net/socket/ssl_client_socket.h" | |
| 15 #include "net/socket/tcp_client_socket.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 | |
| 18 using testing::_; | |
| 19 using testing::DoAll; | |
| 20 using testing::Return; | |
| 21 using testing::SaveArg; | |
| 22 using base::StringPiece; | |
| 23 | |
| 24 namespace net { | |
| 25 class ServerBoundCertService; | |
| 26 } | |
| 27 | |
| 28 namespace extensions { | |
| 29 | |
| 30 class MockSSLClientSocket : public net::SSLClientSocket { | |
| 31 public: | |
| 32 MockSSLClientSocket() {} | |
| 33 MOCK_METHOD0(Disconnect, void()); | |
| 34 MOCK_METHOD3(Read, | |
| 35 int(net::IOBuffer* buf, | |
| 36 int buf_len, | |
| 37 const net::CompletionCallback& callback)); | |
| 38 MOCK_METHOD3(Write, | |
| 39 int(net::IOBuffer* buf, | |
| 40 int buf_len, | |
| 41 const net::CompletionCallback& callback)); | |
| 42 MOCK_METHOD1(SetReceiveBufferSize, bool(int32)); | |
| 43 MOCK_METHOD1(SetSendBufferSize, bool(int32)); | |
| 44 MOCK_METHOD1(Connect, int(const CompletionCallback&)); | |
| 45 MOCK_CONST_METHOD0(IsConnectedAndIdle, bool()); | |
| 46 MOCK_CONST_METHOD1(GetPeerAddress, int(net::IPEndPoint*)); | |
| 47 MOCK_CONST_METHOD1(GetLocalAddress, int(net::IPEndPoint*)); | |
| 48 MOCK_CONST_METHOD0(NetLog, const net::BoundNetLog&()); | |
| 49 MOCK_METHOD0(SetSubresourceSpeculation, void()); | |
| 50 MOCK_METHOD0(SetOmniboxSpeculation, void()); | |
| 51 MOCK_CONST_METHOD0(WasEverUsed, bool()); | |
| 52 MOCK_CONST_METHOD0(UsingTCPFastOpen, bool()); | |
| 53 MOCK_METHOD1(GetSSLInfo, bool(net::SSLInfo*)); | |
| 54 MOCK_METHOD5(ExportKeyingMaterial, | |
| 55 int(const StringPiece&, | |
| 56 bool, | |
| 57 const StringPiece&, | |
| 58 unsigned char*, | |
| 59 unsigned int)); | |
| 60 MOCK_METHOD1(GetTLSUniqueChannelBinding, int(std::string*)); | |
| 61 MOCK_METHOD1(GetSSLCertRequestInfo, void(net::SSLCertRequestInfo*)); | |
| 62 MOCK_METHOD2(GetNextProto, | |
| 63 net::SSLClientSocket::NextProtoStatus(std::string*, | |
| 64 std::string*)); | |
| 65 MOCK_CONST_METHOD0(GetServerBoundCertService, net::ServerBoundCertService*()); | |
| 66 virtual bool IsConnected() const OVERRIDE { return true; } | |
| 67 | |
| 68 private: | |
| 69 DISALLOW_COPY_AND_ASSIGN(MockSSLClientSocket); | |
| 70 }; | |
| 71 | |
| 72 class MockTCPSocket : public net::TCPClientSocket { | |
| 73 public: | |
| 74 explicit MockTCPSocket(const net::AddressList& address_list) | |
| 75 : net::TCPClientSocket(address_list, NULL, net::NetLog::Source()) {} | |
| 76 | |
| 77 MOCK_METHOD3(Read, | |
| 78 int(net::IOBuffer* buf, | |
| 79 int buf_len, | |
| 80 const net::CompletionCallback& callback)); | |
| 81 MOCK_METHOD3(Write, | |
| 82 int(net::IOBuffer* buf, | |
| 83 int buf_len, | |
| 84 const net::CompletionCallback& callback)); | |
| 85 MOCK_METHOD2(SetKeepAlive, bool(bool enable, int delay)); | |
| 86 MOCK_METHOD1(SetNoDelay, bool(bool no_delay)); | |
| 87 | |
| 88 virtual bool IsConnected() const OVERRIDE { return true; } | |
| 89 | |
| 90 private: | |
| 91 DISALLOW_COPY_AND_ASSIGN(MockTCPSocket); | |
| 92 }; | |
| 93 | |
| 94 class CompleteHandler { | |
| 95 public: | |
| 96 CompleteHandler() {} | |
| 97 MOCK_METHOD1(OnComplete, void(int result_code)); | |
| 98 MOCK_METHOD2(OnReadComplete, | |
| 99 void(int result_code, scoped_refptr<net::IOBuffer> io_buffer)); | |
| 100 MOCK_METHOD2(OnAccept, void(int, net::TCPClientSocket*)); | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(CompleteHandler); | |
| 104 }; | |
| 105 | |
| 106 static const char FAKE_ID[] = "faktetesttlssocketunittest"; | |
| 107 | |
| 108 class SocketTest : public ::testing::Test { | |
| 109 pubic: | |
|
Ryan Sleevi
2014/03/18 01:05:13
typo: public != pubic
lally
2014/03/26 03:17:51
That's just embarrassing.
| |
| 110 SocketTest() {} | |
| 111 | |
| 112 virtual void Setup() { | |
| 113 net::AddressList address_list; | |
| 114 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); | |
| 115 // ssl_socket_ is owned by socket_. SocketTest keeps a pointer to it to | |
| 116 // expect invocations from TLSSocket to ssl_socket_. | |
| 117 ssl_socket_ = new MockSSLClientSocket; | |
| 118 socket_.reset(new TLSSocket(ssl_socket_, tcp_client_socket_ID)); | |
| 119 }; | |
| 120 | |
| 121 virtual void TearDown() { | |
| 122 ssl_socket_ = NULL; | |
| 123 socket_.reset(); | |
| 124 }; | |
| 125 | |
| 126 protected: | |
| 127 MockSSLClientSocket* ssl_socket_; | |
| 128 scoped_ptr<TLSSocket> socket_; | |
| 129 }; | |
| 130 | |
| 131 // Verify that a Read() on TLSSocket will pass through into a Read() on | |
| 132 // ssl_socket_ and invoke its completion callback. | |
| 133 TEST_F(SocketTest, TestTLSSocketRead) { | |
| 134 CompleteHandler handler; | |
| 135 | |
| 136 EXPECT_CALL(*ssl_socket_, Read(_, _, _)).Times(1); | |
| 137 EXPECT_CALL(handler, OnReadComplete(_, _)).Times(1); | |
| 138 | |
| 139 const int count = 512; | |
| 140 socket_->Read( | |
| 141 count, | |
| 142 base::Bind(&CompleteHandler::OnReadComplete, base::Unretained(&handler))); | |
| 143 } | |
| 144 | |
| 145 // Verify that a Write() on a TLSSocket will pass through to Write() | |
| 146 // invocations on ssl_socket_, handling partial writes correctly, and calls | |
| 147 // the completion callback correctly. | |
| 148 TEST_F(SocketTest, TestTLSSocketWrite) { | |
| 149 CompleteHandler handler; | |
| 150 net::CompletionCallback callback; | |
| 151 | |
| 152 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly( | |
| 153 testing::DoAll(SaveArg<2>(&callback), Return(128))); | |
| 154 EXPECT_CALL(handler, OnComplete(_)).Times(1); | |
| 155 | |
| 156 scoped_refptr<net::IOBufferWithSize> io_buffer( | |
| 157 new net::IOBufferWithSize(256)); | |
| 158 socket->Write( | |
| 159 io_buffer.get(), | |
| 160 io_buffer->size(), | |
| 161 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler))); | |
| 162 } | |
| 163 | |
| 164 // Simulate a blocked Write, and verify that, when we simulate the Write going | |
| 165 // through, the callback gets invoked. | |
| 166 TEST_F(SocketTest, TestTLSSocketBlockedWrite) { | |
| 167 CompleteHandler handler; | |
| 168 net::CompletionCallback callback; | |
| 169 | |
| 170 // Return ERR_IO_PENDING to say the Write()'s blocked. Save the |callback| | |
| 171 // Write()'s passed. | |
| 172 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly( | |
| 173 testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING))); | |
| 174 | |
| 175 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(42)); | |
| 176 socket_->Write( | |
| 177 io_buffer.get(), | |
| 178 io_buffer->size(), | |
| 179 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler))); | |
| 180 | |
| 181 // After the simulated asynchronous writes come back (via calls to | |
| 182 // callback.Run()), hander's OnComplete should get invoked with the total | |
| 183 // amount written. | |
| 184 EXPECT_CALL(handler, OnComplete(42)).Times(1); | |
| 185 callback.Run(40); | |
| 186 callback.Run(2); | |
| 187 } | |
| 188 | |
| 189 // Simulate multiple blocked Write()s. | |
| 190 TEST_F(SocketTest, TestTLSSocketBlockedWriteReentry) { | |
| 191 const int kNumIOs = 5; | |
| 192 CompleteHandler handlers[kNumIOs]; | |
| 193 net::CompletionCallback callback; | |
| 194 scoped_refptr<net::IOBufferWithSize> io_buffers[kNumIOs]; | |
| 195 | |
| 196 // The implemntation of TLSSocket::Write() is inherited from | |
|
Ryan Sleevi
2014/03/18 01:05:13
typo: implementation
lally
2014/03/26 03:17:51
Done.
| |
| 197 // Socket::Write(), which implements an internal write queue that wraps | |
| 198 // TLSSocket::WriteImpl(). Each call from TLSSocket::WriteImpl() will invoke | |
| 199 // ssl_socket_'s Write(), (mocked here). Save the |callback| (we assume | |
| 200 // they will all be equivalent), and return ERR_IO_PENDING, to indicate a | |
| 201 // blocked request. The mocked Write() will get one request per Write() | |
|
Ryan Sleevi
2014/03/18 01:05:13
the mocked SSLClientSocket::Write() will get one r
| |
| 202 // request we invoke on |socket_| below. | |
| 203 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(kNumIOs).WillRepeatedly( | |
| 204 testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING))); | |
| 205 | |
| 206 // Send out |kNuMIOs| requests, each with a different size. | |
| 207 for (int i = 0; i < kNumIOs; i++) { | |
| 208 io_buffers[i] = new net::IOBufferWithSize(128 + i * 50); | |
| 209 scoped_refptr<net::IOBufferWithSize> io_buffer1( | |
| 210 new net::IOBufferWithSize(42)); | |
| 211 socket_->Write(io_buffers[i].get(), | |
| 212 io_buffers[i]->size(), | |
| 213 base::Bind(&CompleteHandler::OnComplete, | |
| 214 base::Unretained(&handlers[i]))); | |
| 215 | |
| 216 // Set up expectations on all |kNumIOs| handlers. | |
| 217 EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1); | |
| 218 } | |
| 219 | |
| 220 // Finish each pending I/O. This should satisfy the expectations on the | |
| 221 // handlers. | |
| 222 for (int i = 0; i < kNumIOs; i++) { | |
| 223 callback.Run(128 + i * 50); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 } // namespace extensions | |
| OLD | NEW |