Enable some JsToCppTests.
[chromium-blink-merge.git] / net / websockets / websocket_throttle_test.cc
blob7b33883744a1c6b38ae9672b860ae2626c5f51e2
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.
5 #include "net/websockets/websocket_throttle.h"
7 #include <string>
9 #include "base/message_loop/message_loop.h"
10 #include "net/base/address_list.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/socket_stream/socket_stream.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "net/websockets/websocket_job.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17 #include "url/gurl.h"
19 class DummySocketStreamDelegate : public net::SocketStream::Delegate {
20 public:
21 DummySocketStreamDelegate() {}
22 virtual ~DummySocketStreamDelegate() {}
23 virtual void OnConnected(
24 net::SocketStream* socket, int max_pending_send_allowed) OVERRIDE {}
25 virtual void OnSentData(net::SocketStream* socket,
26 int amount_sent) OVERRIDE {}
27 virtual void OnReceivedData(net::SocketStream* socket,
28 const char* data, int len) OVERRIDE {}
29 virtual void OnClose(net::SocketStream* socket) OVERRIDE {}
32 namespace net {
34 class WebSocketThrottleTest : public PlatformTest {
35 protected:
36 static IPEndPoint MakeAddr(int a1, int a2, int a3, int a4) {
37 IPAddressNumber ip;
38 ip.push_back(a1);
39 ip.push_back(a2);
40 ip.push_back(a3);
41 ip.push_back(a4);
42 return IPEndPoint(ip, 0);
45 static void MockSocketStreamConnect(
46 SocketStream* socket, const AddressList& list) {
47 socket->set_addresses(list);
48 // TODO(toyoshim): We should introduce additional tests on cases via proxy.
49 socket->proxy_info_.UseDirect();
50 // In SocketStream::Connect(), it adds reference to socket, which is
51 // balanced with SocketStream::Finish() that is finally called from
52 // SocketStream::Close() or SocketStream::DetachDelegate(), when
53 // next_state_ is not STATE_NONE.
54 // If next_state_ is STATE_NONE, SocketStream::Close() or
55 // SocketStream::DetachDelegate() won't call SocketStream::Finish(),
56 // so Release() won't be called. Thus, we don't need socket->AddRef()
57 // here.
58 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE);
62 TEST_F(WebSocketThrottleTest, Throttle) {
63 TestURLRequestContext context;
64 DummySocketStreamDelegate delegate;
65 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled
66 // configuration.
67 WebSocketJob::set_websocket_over_spdy_enabled(true);
69 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6
70 AddressList addr;
71 addr.push_back(MakeAddr(1, 2, 3, 4));
72 addr.push_back(MakeAddr(1, 2, 3, 5));
73 addr.push_back(MakeAddr(1, 2, 3, 6));
74 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate));
75 scoped_refptr<SocketStream> s1(
76 new SocketStream(GURL("ws://host1/"), w1.get(), &context, NULL));
77 w1->InitSocketStream(s1.get());
78 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr);
80 DVLOG(1) << "socket1";
81 TestCompletionCallback callback_s1;
82 // Trying to open connection to host1 will start without wait.
83 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback()));
85 // Now connecting to host1, so waiting queue looks like
86 // Address | head -> tail
87 // 1.2.3.4 | w1
88 // 1.2.3.5 | w1
89 // 1.2.3.6 | w1
91 // For host2: 1.2.3.4
92 addr.clear();
93 addr.push_back(MakeAddr(1, 2, 3, 4));
94 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate));
95 scoped_refptr<SocketStream> s2(
96 new SocketStream(GURL("ws://host2/"), w2.get(), &context, NULL));
97 w2->InitSocketStream(s2.get());
98 WebSocketThrottleTest::MockSocketStreamConnect(s2.get(), addr);
100 DVLOG(1) << "socket2";
101 TestCompletionCallback callback_s2;
102 // Trying to open connection to host2 will wait for w1.
103 EXPECT_EQ(ERR_IO_PENDING,
104 w2->OnStartOpenConnection(s2.get(), callback_s2.callback()));
105 // Now waiting queue looks like
106 // Address | head -> tail
107 // 1.2.3.4 | w1 w2
108 // 1.2.3.5 | w1
109 // 1.2.3.6 | w1
111 // For host3: 1.2.3.5
112 addr.clear();
113 addr.push_back(MakeAddr(1, 2, 3, 5));
114 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate));
115 scoped_refptr<SocketStream> s3(
116 new SocketStream(GURL("ws://host3/"), w3.get(), &context, NULL));
117 w3->InitSocketStream(s3.get());
118 WebSocketThrottleTest::MockSocketStreamConnect(s3.get(), addr);
120 DVLOG(1) << "socket3";
121 TestCompletionCallback callback_s3;
122 // Trying to open connection to host3 will wait for w1.
123 EXPECT_EQ(ERR_IO_PENDING,
124 w3->OnStartOpenConnection(s3.get(), callback_s3.callback()));
125 // Address | head -> tail
126 // 1.2.3.4 | w1 w2
127 // 1.2.3.5 | w1 w3
128 // 1.2.3.6 | w1
130 // For host4: 1.2.3.4, 1.2.3.6
131 addr.clear();
132 addr.push_back(MakeAddr(1, 2, 3, 4));
133 addr.push_back(MakeAddr(1, 2, 3, 6));
134 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate));
135 scoped_refptr<SocketStream> s4(
136 new SocketStream(GURL("ws://host4/"), w4.get(), &context, NULL));
137 w4->InitSocketStream(s4.get());
138 WebSocketThrottleTest::MockSocketStreamConnect(s4.get(), addr);
140 DVLOG(1) << "socket4";
141 TestCompletionCallback callback_s4;
142 // Trying to open connection to host4 will wait for w1, w2.
143 EXPECT_EQ(ERR_IO_PENDING,
144 w4->OnStartOpenConnection(s4.get(), callback_s4.callback()));
145 // Address | head -> tail
146 // 1.2.3.4 | w1 w2 w4
147 // 1.2.3.5 | w1 w3
148 // 1.2.3.6 | w1 w4
150 // For host5: 1.2.3.6
151 addr.clear();
152 addr.push_back(MakeAddr(1, 2, 3, 6));
153 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate));
154 scoped_refptr<SocketStream> s5(
155 new SocketStream(GURL("ws://host5/"), w5.get(), &context, NULL));
156 w5->InitSocketStream(s5.get());
157 WebSocketThrottleTest::MockSocketStreamConnect(s5.get(), addr);
159 DVLOG(1) << "socket5";
160 TestCompletionCallback callback_s5;
161 // Trying to open connection to host5 will wait for w1, w4
162 EXPECT_EQ(ERR_IO_PENDING,
163 w5->OnStartOpenConnection(s5.get(), callback_s5.callback()));
164 // Address | head -> tail
165 // 1.2.3.4 | w1 w2 w4
166 // 1.2.3.5 | w1 w3
167 // 1.2.3.6 | w1 w4 w5
169 // For host6: 1.2.3.6
170 addr.clear();
171 addr.push_back(MakeAddr(1, 2, 3, 6));
172 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate));
173 scoped_refptr<SocketStream> s6(
174 new SocketStream(GURL("ws://host6/"), w6.get(), &context, NULL));
175 w6->InitSocketStream(s6.get());
176 WebSocketThrottleTest::MockSocketStreamConnect(s6.get(), addr);
178 DVLOG(1) << "socket6";
179 TestCompletionCallback callback_s6;
180 // Trying to open connection to host6 will wait for w1, w4, w5
181 EXPECT_EQ(ERR_IO_PENDING,
182 w6->OnStartOpenConnection(s6.get(), callback_s6.callback()));
183 // Address | head -> tail
184 // 1.2.3.4 | w1 w2 w4
185 // 1.2.3.5 | w1 w3
186 // 1.2.3.6 | w1 w4 w5 w6
188 // Receive partial response on w1, still connecting.
189 DVLOG(1) << "socket1 1";
190 static const char kHeader[] = "HTTP/1.1 101 WebSocket Protocol\r\n";
191 w1->OnReceivedData(s1.get(), kHeader, sizeof(kHeader) - 1);
192 EXPECT_FALSE(callback_s2.have_result());
193 EXPECT_FALSE(callback_s3.have_result());
194 EXPECT_FALSE(callback_s4.have_result());
195 EXPECT_FALSE(callback_s5.have_result());
196 EXPECT_FALSE(callback_s6.have_result());
198 // Receive rest of handshake response on w1.
199 DVLOG(1) << "socket1 2";
200 static const char kHeader2[] =
201 "Upgrade: WebSocket\r\n"
202 "Connection: Upgrade\r\n"
203 "Sec-WebSocket-Origin: http://www.google.com\r\n"
204 "Sec-WebSocket-Location: ws://websocket.chromium.org\r\n"
205 "\r\n"
206 "8jKS'y:G*Co,Wxa-";
207 w1->OnReceivedData(s1.get(), kHeader2, sizeof(kHeader2) - 1);
208 base::MessageLoopForIO::current()->RunUntilIdle();
209 // Now, w1 is open.
210 EXPECT_EQ(WebSocketJob::OPEN, w1->state());
211 // So, w2 and w3 can start connecting. w4 needs to wait w2 (1.2.3.4)
212 EXPECT_TRUE(callback_s2.have_result());
213 EXPECT_TRUE(callback_s3.have_result());
214 EXPECT_FALSE(callback_s4.have_result());
215 // Address | head -> tail
216 // 1.2.3.4 | w2 w4
217 // 1.2.3.5 | w3
218 // 1.2.3.6 | w4 w5 w6
220 // Closing s1 doesn't change waiting queue.
221 DVLOG(1) << "socket1 close";
222 w1->OnClose(s1.get());
223 base::MessageLoopForIO::current()->RunUntilIdle();
224 EXPECT_FALSE(callback_s4.have_result());
225 s1->DetachDelegate();
226 // Address | head -> tail
227 // 1.2.3.4 | w2 w4
228 // 1.2.3.5 | w3
229 // 1.2.3.6 | w4 w5 w6
231 // w5 can close while waiting in queue.
232 DVLOG(1) << "socket5 close";
233 // w5 close() closes SocketStream that change state to STATE_CLOSE, calls
234 // DoLoop(), so OnClose() callback will be called.
235 w5->OnClose(s5.get());
236 base::MessageLoopForIO::current()->RunUntilIdle();
237 EXPECT_FALSE(callback_s4.have_result());
238 // Address | head -> tail
239 // 1.2.3.4 | w2 w4
240 // 1.2.3.5 | w3
241 // 1.2.3.6 | w4 w6
242 s5->DetachDelegate();
244 // w6 close abnormally (e.g. renderer finishes) while waiting in queue.
245 DVLOG(1) << "socket6 close abnormally";
246 w6->DetachDelegate();
247 base::MessageLoopForIO::current()->RunUntilIdle();
248 EXPECT_FALSE(callback_s4.have_result());
249 // Address | head -> tail
250 // 1.2.3.4 | w2 w4
251 // 1.2.3.5 | w3
252 // 1.2.3.6 | w4
254 // Closing s2 kicks w4 to start connecting.
255 DVLOG(1) << "socket2 close";
256 w2->OnClose(s2.get());
257 base::MessageLoopForIO::current()->RunUntilIdle();
258 EXPECT_TRUE(callback_s4.have_result());
259 // Address | head -> tail
260 // 1.2.3.4 | w4
261 // 1.2.3.5 | w3
262 // 1.2.3.6 | w4
263 s2->DetachDelegate();
265 DVLOG(1) << "socket3 close";
266 w3->OnClose(s3.get());
267 base::MessageLoopForIO::current()->RunUntilIdle();
268 s3->DetachDelegate();
269 w4->OnClose(s4.get());
270 s4->DetachDelegate();
271 DVLOG(1) << "Done";
272 base::MessageLoopForIO::current()->RunUntilIdle();
275 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) {
276 TestURLRequestContext context;
277 DummySocketStreamDelegate delegate;
278 WebSocketJob::set_websocket_over_spdy_enabled(true);
280 // For localhost: 127.0.0.1, 127.0.0.1
281 AddressList addr;
282 addr.push_back(MakeAddr(127, 0, 0, 1));
283 addr.push_back(MakeAddr(127, 0, 0, 1));
284 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate));
285 scoped_refptr<SocketStream> s1(
286 new SocketStream(GURL("ws://localhost/"), w1.get(), &context, NULL));
287 w1->InitSocketStream(s1.get());
288 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr);
290 DVLOG(1) << "socket1";
291 TestCompletionCallback callback_s1;
292 // Trying to open connection to localhost will start without wait.
293 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback()));
295 DVLOG(1) << "socket1 close";
296 w1->OnClose(s1.get());
297 s1->DetachDelegate();
298 DVLOG(1) << "Done";
299 base::MessageLoopForIO::current()->RunUntilIdle();
302 // A connection should not be blocked by another connection to the same IP
303 // with a different port.
304 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) {
305 TestURLRequestContext context;
306 DummySocketStreamDelegate delegate;
307 IPAddressNumber localhost;
308 ParseIPLiteralToNumber("127.0.0.1", &localhost);
309 WebSocketJob::set_websocket_over_spdy_enabled(false);
311 // socket1: 127.0.0.1:80
312 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate));
313 scoped_refptr<SocketStream> s1(
314 new SocketStream(GURL("ws://localhost:80/"), w1.get(), &context, NULL));
315 w1->InitSocketStream(s1.get());
316 MockSocketStreamConnect(s1.get(),
317 AddressList::CreateFromIPAddress(localhost, 80));
319 DVLOG(1) << "connecting socket1";
320 TestCompletionCallback callback_s1;
321 // Trying to open connection to localhost:80 will start without waiting.
322 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback()));
324 // socket2: 127.0.0.1:81
325 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate));
326 scoped_refptr<SocketStream> s2(
327 new SocketStream(GURL("ws://localhost:81/"), w2.get(), &context, NULL));
328 w2->InitSocketStream(s2.get());
329 MockSocketStreamConnect(s2.get(),
330 AddressList::CreateFromIPAddress(localhost, 81));
332 DVLOG(1) << "connecting socket2";
333 TestCompletionCallback callback_s2;
334 // Trying to open connection to localhost:81 will start without waiting.
335 EXPECT_EQ(OK, w2->OnStartOpenConnection(s2.get(), callback_s2.callback()));
337 DVLOG(1) << "closing socket1";
338 w1->OnClose(s1.get());
339 s1->DetachDelegate();
341 DVLOG(1) << "closing socket2";
342 w2->OnClose(s2.get());
343 s2->DetachDelegate();
344 DVLOG(1) << "Done";
345 base::MessageLoopForIO::current()->RunUntilIdle();