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

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: Added a large-write test, some spelling/doc fixes, and a stronger check for canonicalized names. 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 // MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
120 // ssl_socket_ is owned by socket_. TLSSocketTest keeps a pointer to it to
121 // expect invocations from TLSSocket to ssl_socket_.
122 scoped_ptr<MockSSLClientSocket> ssl_sock(new MockSSLClientSocket);
123 ssl_socket_ = ssl_sock.get();
124 socket_.reset(new TLSSocket(ssl_sock.PassAs<net::StreamSocket>(),
125 "test_extension_id"));
126 EXPECT_CALL(*ssl_socket_, Disconnect()).Times(1);
127 };
128
129 virtual void TearDown() {
130 ssl_socket_ = NULL;
131 socket_.reset();
132 };
133
134 protected:
135 MockSSLClientSocket* ssl_socket_;
136 scoped_ptr<TLSSocket> socket_;
137 };
138
139 // Verify that a Read() on TLSSocket will pass through into a Read() on
140 // ssl_socket_ and invoke its completion callback.
141 TEST_F(TLSSocketTest, TestTLSSocketRead) {
142 CompleteHandler handler;
143
144 EXPECT_CALL(*ssl_socket_, Read(_, _, _)).Times(1);
145 EXPECT_CALL(handler, OnReadComplete(_, _)).Times(1);
146
147 const int count = 512;
148 socket_->Read(
149 count,
150 base::Bind(&CompleteHandler::OnReadComplete, base::Unretained(&handler)));
151 }
152
153 // Verify that a Write() on a TLSSocket will pass through to Write()
154 // invocations on ssl_socket_, handling partial writes correctly, and calls
155 // the completion callback correctly.
156 TEST_F(TLSSocketTest, TestTLSSocketWrite) {
157 CompleteHandler handler;
158 net::CompletionCallback callback;
159
160 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly(
161 DoAll(SaveArg<2>(&callback), Return(128)));
162 EXPECT_CALL(handler, OnComplete(_)).Times(1);
163
164 scoped_refptr<net::IOBufferWithSize> io_buffer(
165 new net::IOBufferWithSize(256));
166 socket_->Write(
167 io_buffer.get(),
168 io_buffer->size(),
169 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
170 }
171
172 // Simulate a blocked Write, and verify that, when simulating the Write going
173 // through, the callback gets invoked.
174 TEST_F(TLSSocketTest, TestTLSSocketBlockedWrite) {
175 CompleteHandler handler;
176 net::CompletionCallback callback;
177
178 // Return ERR_IO_PENDING to say the Write()'s blocked. Save the |callback|
179 // Write()'s passed.
180 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(2).WillRepeatedly(
181 DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
182
183 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(42));
184 socket_->Write(
185 io_buffer.get(),
186 io_buffer->size(),
187 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
188
189 // After the simulated asynchronous writes come back (via calls to
190 // callback.Run()), hander's OnComplete should get invoked with the total
191 // amount written.
192 EXPECT_CALL(handler, OnComplete(42)).Times(1);
193 callback.Run(40);
194 callback.Run(2);
195 }
196
197 // Simulate multiple blocked Write()s.
198 TEST_F(TLSSocketTest, TestTLSSocketBlockedWriteReentry) {
199 const int kNumIOs = 5;
200 CompleteHandler handlers[kNumIOs];
201 net::CompletionCallback callback;
202 scoped_refptr<net::IOBufferWithSize> io_buffers[kNumIOs];
203
204 // The implementation of TLSSocket::Write() is inherited from
205 // Socket::Write(), which implements an internal write queue that wraps
206 // TLSSocket::WriteImpl(). Each call from TLSSocket::WriteImpl() will invoke
207 // ssl_socket_'s Write() (mocked here). Save the |callback| (assume they
208 // will all be equivalent), and return ERR_IO_PENDING, to indicate a blocked
209 // request. The mocked SSLClientSocket::Write() will get one request per
210 // TLSSocket::Write() request invoked on |socket_| below.
211 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(kNumIOs).WillRepeatedly(
212 DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
213
214 // Send out |kNuMIOs| requests, each with a different size.
215 for (int i = 0; i < kNumIOs; i++) {
216 io_buffers[i] = new net::IOBufferWithSize(128 + i * 50);
217 socket_->Write(io_buffers[i].get(),
218 io_buffers[i]->size(),
219 base::Bind(&CompleteHandler::OnComplete,
220 base::Unretained(&handlers[i])));
221
222 // Set up expectations on all |kNumIOs| handlers.
223 EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1);
224 }
225
226 // Finish each pending I/O. This should satisfy the expectations on the
227 // handlers.
228 for (int i = 0; i < kNumIOs; i++) {
229 callback.Run(128 + i * 50);
230 }
231 }
232
233 // Simulate Write()s above and below a SSLClientSocket size limit.
234 TEST_F(TLSSocketTest, TestTLSSocketLargeWrites) {
235 const int kSizeIncrement = 4096;
236 const int kNumIncrements = 10;
237 const int kStartFailingIncrement = 4;
238 const int kSizeLimit = kSizeIncrement * kStartFailingIncrement;
239 net::CompletionCallback callback;
240 CompleteHandler handler;
241 scoped_refptr<net::IOBufferWithSize> io_buffers[kNumIncrements];
242 std::vector<int> callback_args;
243
244 // Some implementations of SSLClientSocket may have write-size limits (e.g,
245 // max 1 TLS record, which is 16k). This test mocks a size limit at
246 // |kSizeIncrement| and calls Write() above and below that limit. It makes
247 // sure that (a) rejected-Write() errors are properly propagated back to
248 // the caller, and (b) those errors don't prevent future Write()s with
249 // below-limit sizes from working.
250
251 // The default case for Write() will be for the over-size case, returning
252 // ERR_INVALID_ARGUMENT. Socket::Write() will invoke the callback
253 // itself, so don't save that argument here.
254 EXPECT_CALL(*ssl_socket_, Write(_, _, _))
255 .WillRepeatedly(Return(net::ERR_INVALID_ARGUMENT));
Ryan Sleevi 2014/03/26 19:57:40 I don't entirely understand this. If the caller a
lally 2014/03/28 16:22:51 I misunderstood you when you said "SSLClientSocket
256
257 // Calls under the limit return ERR_IO_PENDING and save the callback for
258 // manual invocation (simulating a completed write).
259 EXPECT_CALL(*ssl_socket_, Write(_, Le(kSizeLimit), _)).WillRepeatedly(
260 DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
261
262 // Observe what comes back from Write() here. First, the over-size-limit
263 // errors, which return values < 0. The calls after the first
264 // |kStartFailingIncrement| are all over the size limit, except for the
265 // last call, which is for a below-limit amount.
266 EXPECT_CALL(handler, OnComplete(Lt(0)))
267 .Times(kNumIncrements - kStartFailingIncrement - 1);
268
269 // Write() returning successful. The first |kStartFailingIncrement| calls
270 // succeed, as well as the last one.
271 EXPECT_CALL(handler, OnComplete(Gt(0))).Times(kStartFailingIncrement + 1);
272
273 // Send out |kNumIncrements| requests, each with a different size. The
274 // last request is the same size as the first, and the ones in the middle
275 // are monotonically increasing from the first.
276 for (int i = 0; i < kNumIncrements; i++) {
277 const bool last = i == (kNumIncrements - 1);
278 const bool over_limit = (i >= kStartFailingIncrement) && !last;
279 io_buffers[i] = new net::IOBufferWithSize(last ? kSizeIncrement
280 : kSizeIncrement * (i + 1));
281
282 socket_->Write(
283 io_buffers[i].get(),
284 io_buffers[i]->size(),
285 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
286
287 // Do not save arguments for callbacks that don't need invocation -- they
288 // will already have been called by the time Write() returns.
289 if (!over_limit) {
290 callback_args.push_back(io_buffers[i]->size());
291 }
292 }
293
294 // Invoke the callback for the pendiong I/Os.
295 for (size_t i = 0; i < callback_args.size(); i++) {
296 callback.Run(callback_args[i]);
297 }
298 }
299
300 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/tls_socket.cc ('k') | chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698