Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(958)

Side by Side Diff: chrome/browser/extensions/api/socket/tls_socket_unittest.cc

Issue 76403004: An implementation of chrome.socket.secure(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed, @rsleevi's comments, added a new TLS test, further separated TLS and TCP tests, and reba… Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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::Invoke;
21 using testing::Gt;
22 using testing::Le;
23 using testing::Lt;
24 using testing::Return;
25 using testing::SaveArg;
26 using testing::WithArg;
27 using base::StringPiece;
28
29 namespace net {
30 class ServerBoundCertService;
31 }
32
33 namespace extensions {
34
35 class MockSSLClientSocket : public net::SSLClientSocket {
36 public:
37 MockSSLClientSocket() {}
38 MOCK_METHOD0(Disconnect, void());
39 MOCK_METHOD3(Read,
40 int(net::IOBuffer* buf,
41 int buf_len,
42 const net::CompletionCallback& callback));
43 MOCK_METHOD3(Write,
44 int(net::IOBuffer* buf,
45 int buf_len,
46 const net::CompletionCallback& callback));
47 MOCK_METHOD1(SetReceiveBufferSize, bool(int32));
48 MOCK_METHOD1(SetSendBufferSize, bool(int32));
49 MOCK_METHOD1(Connect, int(const CompletionCallback&));
50 MOCK_CONST_METHOD0(IsConnectedAndIdle, bool());
51 MOCK_CONST_METHOD1(GetPeerAddress, int(net::IPEndPoint*));
52 MOCK_CONST_METHOD1(GetLocalAddress, int(net::IPEndPoint*));
53 MOCK_CONST_METHOD0(NetLog, const net::BoundNetLog&());
54 MOCK_METHOD0(SetSubresourceSpeculation, void());
55 MOCK_METHOD0(SetOmniboxSpeculation, void());
56 MOCK_CONST_METHOD0(WasEverUsed, bool());
57 MOCK_CONST_METHOD0(UsingTCPFastOpen, bool());
58 MOCK_METHOD1(GetSSLInfo, bool(net::SSLInfo*));
59 MOCK_METHOD5(ExportKeyingMaterial,
60 int(const StringPiece&,
61 bool,
62 const StringPiece&,
63 unsigned char*,
64 unsigned int));
65 MOCK_METHOD1(GetTLSUniqueChannelBinding, int(std::string*));
66 MOCK_METHOD1(GetSSLCertRequestInfo, void(net::SSLCertRequestInfo*));
67 MOCK_METHOD2(GetNextProto,
68 net::SSLClientSocket::NextProtoStatus(std::string*,
69 std::string*));
70 MOCK_CONST_METHOD0(GetServerBoundCertService, net::ServerBoundCertService*());
71 virtual bool IsConnected() const OVERRIDE { return true; }
72
73 private:
74 DISALLOW_COPY_AND_ASSIGN(MockSSLClientSocket);
75 };
76
77 class MockTCPSocket : public net::TCPClientSocket {
78 public:
79 explicit MockTCPSocket(const net::AddressList& address_list)
80 : net::TCPClientSocket(address_list, NULL, net::NetLog::Source()) {}
81
82 MOCK_METHOD3(Read,
83 int(net::IOBuffer* buf,
84 int buf_len,
85 const net::CompletionCallback& callback));
86 MOCK_METHOD3(Write,
87 int(net::IOBuffer* buf,
88 int buf_len,
89 const net::CompletionCallback& callback));
90 MOCK_METHOD2(SetKeepAlive, bool(bool enable, int delay));
91 MOCK_METHOD1(SetNoDelay, bool(bool no_delay));
92
93 virtual bool IsConnected() const OVERRIDE { return true; }
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(MockTCPSocket);
97 };
98
99 class CompleteHandler {
100 public:
101 CompleteHandler() {}
102 MOCK_METHOD1(OnComplete, void(int result_code));
103 MOCK_METHOD2(OnReadComplete,
104 void(int result_code, scoped_refptr<net::IOBuffer> io_buffer));
105 MOCK_METHOD2(OnAccept, void(int, net::TCPClientSocket*));
106
107 private:
108 DISALLOW_COPY_AND_ASSIGN(CompleteHandler);
109 };
110
111 static const char FAKE_ID[] = "faktetesttlssocketunittest";
112
113 class TLSSocketTest : public ::testing::Test {
114 public:
115 TLSSocketTest() {}
116
117 virtual void SetUp() {
118 net::AddressList address_list;
119 // |ssl_socket_| is owned by |socket_|. TLSSocketTest keeps a pointer to
120 // it to expect invocations from TLSSocket to |ssl_socket_|.
121 scoped_ptr<MockSSLClientSocket> ssl_sock(new MockSSLClientSocket);
122 ssl_socket_ = ssl_sock.get();
123 socket_.reset(new TLSSocket(ssl_sock.PassAs<net::StreamSocket>(),
124 "test_extension_id"));
125 EXPECT_CALL(*ssl_socket_, Disconnect()).Times(1);
126 };
127
128 virtual void TearDown() {
129 ssl_socket_ = NULL;
130 socket_.reset();
131 };
132
133 protected:
134 MockSSLClientSocket* ssl_socket_;
135 scoped_ptr<TLSSocket> socket_;
136 };
137
138 // Verify that a Read() on TLSSocket will pass through into a Read() on
139 // |ssl_socket_| and invoke its completion callback.
140 TEST_F(TLSSocketTest, TestTLSSocketRead) {
141 CompleteHandler handler;
142
143 EXPECT_CALL(*ssl_socket_, Read(_, _, _)).Times(1);
144 EXPECT_CALL(handler, OnReadComplete(_, _)).Times(1);
145
146 const int count = 512;
147 socket_->Read(
148 count,
149 base::Bind(&CompleteHandler::OnReadComplete, base::Unretained(&handler)));
150 }
151
152 // Verify that a Write() on a TLSSocket will pass through to Write()
153 // invocations on |ssl_socket_|, handling partial writes correctly, and calls
154 // the completion callback correctly.
155 TEST_F(TLSSocketTest, TestTLSSocketWrite) {
156 CompleteHandler handler;
157 net::CompletionCallback callback;
158
159 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly(
160 DoAll(SaveArg<2>(&callback), Return(128)));
161 EXPECT_CALL(handler, OnComplete(_)).Times(1);
162
163 scoped_refptr<net::IOBufferWithSize> io_buffer(
164 new net::IOBufferWithSize(256));
165 socket_->Write(
166 io_buffer.get(),
167 io_buffer->size(),
168 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
169 }
170
171 // Simulate a blocked Write, and verify that, when simulating the Write going
172 // through, the callback gets invoked.
173 TEST_F(TLSSocketTest, TestTLSSocketBlockedWrite) {
174 CompleteHandler handler;
175 net::CompletionCallback callback;
176
177 // Return ERR_IO_PENDING to say the Write()'s blocked. Save the |callback|
178 // Write()'s passed.
179 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly(
180 DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
181
182 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(42));
183 socket_->Write(
184 io_buffer.get(),
185 io_buffer->size(),
186 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
187
188 // After the simulated asynchronous writes come back (via calls to
189 // callback.Run()), hander's OnComplete() should get invoked with the total
190 // amount written.
191 EXPECT_CALL(handler, OnComplete(42)).Times(1);
192 callback.Run(40);
193 callback.Run(2);
194 }
195
196 // Simulate multiple blocked Write()s.
197 TEST_F(TLSSocketTest, TestTLSSocketBlockedWriteReentry) {
198 const int kNumIOs = 5;
199 CompleteHandler handlers[kNumIOs];
200 net::CompletionCallback callback;
201 scoped_refptr<net::IOBufferWithSize> io_buffers[kNumIOs];
202
203 // The implementation of TLSSocket::Write() is inherited from
204 // Socket::Write(), which implements an internal write queue that wraps
205 // TLSSocket::WriteImpl(). Each call from TLSSocket::WriteImpl() will invoke
206 // |ssl_socket_|'s Write() (mocked here). Save the |callback| (assume they
207 // will all be equivalent), and return ERR_IO_PENDING, to indicate a blocked
208 // request. The mocked SSLClientSocket::Write() will get one request per
209 // TLSSocket::Write() request invoked on |socket_| below.
210 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(kNumIOs).WillRepeatedly(
211 DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
212
213 // Send out |kNuMIOs| requests, each with a different size.
214 for (int i = 0; i < kNumIOs; i++) {
215 io_buffers[i] = new net::IOBufferWithSize(128 + i * 50);
216 socket_->Write(io_buffers[i].get(),
217 io_buffers[i]->size(),
218 base::Bind(&CompleteHandler::OnComplete,
219 base::Unretained(&handlers[i])));
220
221 // Set up expectations on all |kNumIOs| handlers.
222 EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1);
223 }
224
225 // Finish each pending I/O. This should satisfy the expectations on the
226 // handlers.
227 for (int i = 0; i < kNumIOs; i++) {
228 callback.Run(128 + i * 50);
229 }
230 }
231
232 // Simulate Write()s above and below a SSLClientSocket size limit.
233 TEST_F(TLSSocketTest, TestTLSSocketLargeWrites) {
234 const int kSizeIncrement = 4096;
235 const int kNumIncrements = 10;
236 const int kStartFailingIncrement = 4;
237 const int kSizeLimit = kSizeIncrement * kStartFailingIncrement;
238 net::CompletionCallback callback;
239 CompleteHandler handler;
240 scoped_refptr<net::IOBufferWithSize> io_buffers[kNumIncrements];
241 std::vector<int> callback_args;
242
243 // Some implementations of SSLClientSocket may have write-size limits (e.g,
244 // max 1 TLS record, which is 16k). This test mocks a size limit at
245 // |kSizeIncrement| and calls Write() above and below that limit. It makes
246 // sure that (a) rejected-Write() errors are properly propagated back to
247 // the caller, and (b) those errors don't prevent future Write()s with
248 // below-limit sizes from working.
249
250 // The default case for Write() will be for the over-size case, returning
251 // ERR_INVALID_ARGUMENT. Socket::Write() will invoke the callback
252 // itself, so don't save that argument here.
253 EXPECT_CALL(*ssl_socket_, Write(_, _, _))
254 .WillRepeatedly(Return(net::ERR_INVALID_ARGUMENT));
255
256 // Calls under the limit return ERR_IO_PENDING and save the callback for
257 // manual invocation (simulating a completed Write()).
258 EXPECT_CALL(*ssl_socket_, Write(_, Le(kSizeLimit), _)).WillRepeatedly(
259 DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
260
261 // Observe what comes back from Write() here. First, the over-size-limit
262 // errors, which return values < 0. The calls after the first
263 // |kStartFailingIncrement| are all over the size limit, except for the
264 // last call, which is for a below-limit amount.
265 EXPECT_CALL(handler, OnComplete(Lt(0)))
266 .Times(kNumIncrements - kStartFailingIncrement - 1);
267
268 // Write() returning successful. The first |kStartFailingIncrement| calls
269 // succeed, as well as the last one.
270 EXPECT_CALL(handler, OnComplete(Gt(0))).Times(kStartFailingIncrement + 1);
271
272 // Send out |kNumIncrements| requests, each with a different size. The
273 // last request is the same size as the first, and the ones in the middle
274 // are monotonically increasing from the first.
275 for (int i = 0; i < kNumIncrements; i++) {
276 const bool last = i == (kNumIncrements - 1);
277 const bool over_limit = (i >= kStartFailingIncrement) && !last;
278 io_buffers[i] = new net::IOBufferWithSize(last ? kSizeIncrement
279 : kSizeIncrement * (i + 1));
280
281 socket_->Write(
282 io_buffers[i].get(),
283 io_buffers[i]->size(),
284 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
285
286 // Do not save arguments for callbacks that don't need invocation -- they
287 // will already have been called by the time Write() returns.
288 if (!over_limit) {
289 callback_args.push_back(io_buffers[i]->size());
290 }
291 }
292
293 // Invoke the callback for the pendiong I/Os.
294 for (size_t i = 0; i < callback_args.size(); i++) {
295 callback.Run(callback_args[i]);
296 }
297 }
298
299 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698