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

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 check on whether the socket to be TLS'd has a pending read. 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::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:
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
Ryan Sleevi 2014/03/12 23:35:27 nit
lally 2014/03/17 01:58:06 Done.
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 // Each call from TLSSocket::Write() will invoke ssl_socket_'s Write().
Ryan Sleevi 2014/03/12 23:35:27 ssl_socket_ (or at least, SSLClientSocket) only al
lally 2014/03/17 01:58:06 Clarified here and in the documentation of TLSSock
197 // Save the |callback| (we assume they will all be equivalent), and return
198 // ERR_IO_PENDING, to indicate a blocked request. The mocked Write() will
199 // get one request per Write() request we invoke on |socket_| below.
200 EXPECT_CALL(*ssl_socket_, Write(_, _, _)).Times(kNumIOs).WillRepeatedly(
201 testing::DoAll(SaveArg<2>(&callback), Return(net::ERR_IO_PENDING)));
202
203 // Send out |kNuMIOs| requests, each with a different size.
204 for (int i = 0; i < kNumIOs; i++) {
205 io_buffers[i] = new net::IOBufferWithSize(128 + i * 50);
206 scoped_refptr<net::IOBufferWithSize> io_buffer1(
207 new net::IOBufferWithSize(42));
208 socket_->Write(io_buffers[i].get(),
209 io_buffers[i]->size(),
210 base::Bind(&CompleteHandler::OnComplete,
211 base::Unretained(&handlers[i])));
212
213 // Set up expectations on all |kNumIOs| handlers.
214 EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size())).Times(1);
215 }
216
217 // Finish each pending I/O. This should satisfy the expectations on the
218 // handlers.
219 for (int i = 0; i < kNumIOs; i++) {
220 callback.Run(128 + i * 50);
221 }
222 }
223
224 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698