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 TEST(SocketTest, TestTLSSocketRead) { | |
Ryan Sleevi
2014/02/24 20:06:49
Can you add comments to each of these (eg: on line
lally
2014/02/27 17:05:59
Done.
| |
109 net::AddressList address_list; | |
110 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); | |
111 MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; | |
112 CompleteHandler handler; | |
113 | |
114 scoped_ptr<TLSSocket> socket( | |
115 new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); | |
116 | |
117 EXPECT_CALL(*ssl_socket, Read(_, _, _)).Times(1); | |
118 EXPECT_CALL(handler, OnReadComplete(_, _)).Times(1); | |
119 | |
120 const int count = 512; | |
121 socket->Read( | |
122 count, | |
123 base::Bind(&CompleteHandler::OnReadComplete, base::Unretained(&handler))); | |
124 } | |
125 | |
126 TEST(SocketTest, TestTLSSocketWrite) { | |
127 net::AddressList address_list; | |
128 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); | |
129 MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; | |
130 CompleteHandler handler; | |
131 | |
132 scoped_ptr<TLSSocket> socket( | |
133 new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); | |
134 | |
135 net::CompletionCallback callback; | |
136 EXPECT_CALL(*ssl_socket, Write(_, _, _)).Times(2).WillRepeatedly( | |
137 testing::DoAll(SaveArg<2>(&callback), Return(128))); | |
138 EXPECT_CALL(handler, OnComplete(_)).Times(1); | |
139 | |
140 scoped_refptr<net::IOBufferWithSize> io_buffer( | |
141 new net::IOBufferWithSize(256)); | |
142 socket->Write( | |
143 io_buffer.get(), | |
144 io_buffer->size(), | |
145 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler))); | |
146 } | |
147 | |
148 TEST(SocketTest, TestTLSSocketBlockedWrite) { | |
149 net::AddressList address_list; | |
150 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); | |
151 MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; | |
152 CompleteHandler handler; | |
153 | |
154 scoped_ptr<TLSSocket> socket( | |
155 new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); | |
Ryan Sleevi
2014/02/24 20:06:49
Given the redundancy of this setup logic, does it
lally
2014/02/27 17:05:59
Good point. Done.
| |
156 | |
157 net::CompletionCallback callback; | |
158 EXPECT_CALL(*ssl_socket, Write(_, _, _)).Times(2).WillRepeatedly( | |
159 testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING))); | |
160 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(42)); | |
161 socket->Write( | |
162 io_buffer.get(), | |
163 io_buffer->size(), | |
164 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler))); | |
165 | |
166 // Good. Original call came back unable to complete. Now pretend the socket | |
167 // finished, and confirm that we passed the error back. | |
168 EXPECT_CALL(handler, OnComplete(42)).Times(1); | |
169 callback.Run(40); | |
170 callback.Run(2); | |
171 } | |
172 | |
173 TEST(SocketTest, TestTLSSocketBlockedWriteReentry) { | |
174 net::AddressList address_list; | |
175 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); | |
176 MockSSLClientSocket* ssl_socket = new MockSSLClientSocket; | |
177 CompleteHandler handlers[5]; | |
Ryan Sleevi
2014/02/24 20:06:49
magic numbers: What exists to guarantee that this
lally
2014/02/27 17:05:59
Documentation added, and the 5 has been replaced w
| |
178 | |
179 scoped_ptr<TLSSocket> socket( | |
180 new TLSSocket(ssl_socket, tcp_client_socket, FAKE_ID)); | |
181 | |
182 net::CompletionCallback callback; | |
183 EXPECT_CALL(*ssl_socket, Write(_, _, _)).Times(5).WillRepeatedly( | |
184 testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING))); | |
185 scoped_refptr<net::IOBufferWithSize> io_buffers[5]; | |
186 int i; | |
187 for (i = 0; i < 5; i++) { | |
188 io_buffers[i] = new net::IOBufferWithSize(128 + i * 50); | |
189 scoped_refptr<net::IOBufferWithSize> io_buffer1( | |
190 new net::IOBufferWithSize(42)); | |
191 socket->Write(io_buffers[i].get(), | |
192 io_buffers[i]->size(), | |
193 base::Bind(&CompleteHandler::OnComplete, | |
194 base::Unretained(&handlers[i]))); | |
195 | |
196 EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1); | |
197 } | |
198 | |
199 for (i = 0; i < 5; i++) { | |
200 callback.Run(128 + i * 50); | |
201 } | |
202 } | |
203 | |
204 } // namespace extensions | |
OLD | NEW |