OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/websockets/websocket_throttle.h" | 5 #include "net/websockets/websocket_throttle.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 69 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
70 AddressList addr; | 70 AddressList addr; |
71 addr.push_back(MakeAddr(1, 2, 3, 4)); | 71 addr.push_back(MakeAddr(1, 2, 3, 4)); |
72 addr.push_back(MakeAddr(1, 2, 3, 5)); | 72 addr.push_back(MakeAddr(1, 2, 3, 5)); |
73 addr.push_back(MakeAddr(1, 2, 3, 6)); | 73 addr.push_back(MakeAddr(1, 2, 3, 6)); |
74 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 74 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
75 scoped_refptr<SocketStream> s1( | 75 scoped_refptr<SocketStream> s1( |
76 new SocketStream(GURL("ws://host1/"), w1.get())); | 76 new SocketStream(GURL("ws://host1/"), w1.get())); |
77 s1->set_context(&context); | 77 s1->set_context(&context); |
78 w1->InitSocketStream(s1.get()); | 78 w1->InitSocketStream(s1.get()); |
79 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 79 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); |
80 | 80 |
81 DVLOG(1) << "socket1"; | 81 DVLOG(1) << "socket1"; |
82 TestCompletionCallback callback_s1; | 82 TestCompletionCallback callback_s1; |
83 // Trying to open connection to host1 will start without wait. | 83 // Trying to open connection to host1 will start without wait. |
84 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); | 84 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
85 | 85 |
86 // Now connecting to host1, so waiting queue looks like | 86 // Now connecting to host1, so waiting queue looks like |
87 // Address | head -> tail | 87 // Address | head -> tail |
88 // 1.2.3.4 | w1 | 88 // 1.2.3.4 | w1 |
89 // 1.2.3.5 | w1 | 89 // 1.2.3.5 | w1 |
90 // 1.2.3.6 | w1 | 90 // 1.2.3.6 | w1 |
91 | 91 |
92 // For host2: 1.2.3.4 | 92 // For host2: 1.2.3.4 |
93 addr.clear(); | 93 addr.clear(); |
94 addr.push_back(MakeAddr(1, 2, 3, 4)); | 94 addr.push_back(MakeAddr(1, 2, 3, 4)); |
95 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); | 95 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
96 scoped_refptr<SocketStream> s2( | 96 scoped_refptr<SocketStream> s2( |
97 new SocketStream(GURL("ws://host2/"), w2.get())); | 97 new SocketStream(GURL("ws://host2/"), w2.get())); |
98 s2->set_context(&context); | 98 s2->set_context(&context); |
99 w2->InitSocketStream(s2.get()); | 99 w2->InitSocketStream(s2.get()); |
100 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); | 100 WebSocketThrottleTest::MockSocketStreamConnect(s2.get(), addr); |
101 | 101 |
102 DVLOG(1) << "socket2"; | 102 DVLOG(1) << "socket2"; |
103 TestCompletionCallback callback_s2; | 103 TestCompletionCallback callback_s2; |
104 // Trying to open connection to host2 will wait for w1. | 104 // Trying to open connection to host2 will wait for w1. |
105 EXPECT_EQ(ERR_IO_PENDING, | 105 EXPECT_EQ(ERR_IO_PENDING, |
106 w2->OnStartOpenConnection(s2, callback_s2.callback())); | 106 w2->OnStartOpenConnection(s2.get(), callback_s2.callback())); |
107 // Now waiting queue looks like | 107 // Now waiting queue looks like |
108 // Address | head -> tail | 108 // Address | head -> tail |
109 // 1.2.3.4 | w1 w2 | 109 // 1.2.3.4 | w1 w2 |
110 // 1.2.3.5 | w1 | 110 // 1.2.3.5 | w1 |
111 // 1.2.3.6 | w1 | 111 // 1.2.3.6 | w1 |
112 | 112 |
113 // For host3: 1.2.3.5 | 113 // For host3: 1.2.3.5 |
114 addr.clear(); | 114 addr.clear(); |
115 addr.push_back(MakeAddr(1, 2, 3, 5)); | 115 addr.push_back(MakeAddr(1, 2, 3, 5)); |
116 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); | 116 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); |
117 scoped_refptr<SocketStream> s3( | 117 scoped_refptr<SocketStream> s3( |
118 new SocketStream(GURL("ws://host3/"), w3.get())); | 118 new SocketStream(GURL("ws://host3/"), w3.get())); |
119 s3->set_context(&context); | 119 s3->set_context(&context); |
120 w3->InitSocketStream(s3.get()); | 120 w3->InitSocketStream(s3.get()); |
121 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); | 121 WebSocketThrottleTest::MockSocketStreamConnect(s3.get(), addr); |
122 | 122 |
123 DVLOG(1) << "socket3"; | 123 DVLOG(1) << "socket3"; |
124 TestCompletionCallback callback_s3; | 124 TestCompletionCallback callback_s3; |
125 // Trying to open connection to host3 will wait for w1. | 125 // Trying to open connection to host3 will wait for w1. |
126 EXPECT_EQ(ERR_IO_PENDING, | 126 EXPECT_EQ(ERR_IO_PENDING, |
127 w3->OnStartOpenConnection(s3, callback_s3.callback())); | 127 w3->OnStartOpenConnection(s3.get(), callback_s3.callback())); |
128 // Address | head -> tail | 128 // Address | head -> tail |
129 // 1.2.3.4 | w1 w2 | 129 // 1.2.3.4 | w1 w2 |
130 // 1.2.3.5 | w1 w3 | 130 // 1.2.3.5 | w1 w3 |
131 // 1.2.3.6 | w1 | 131 // 1.2.3.6 | w1 |
132 | 132 |
133 // For host4: 1.2.3.4, 1.2.3.6 | 133 // For host4: 1.2.3.4, 1.2.3.6 |
134 addr.clear(); | 134 addr.clear(); |
135 addr.push_back(MakeAddr(1, 2, 3, 4)); | 135 addr.push_back(MakeAddr(1, 2, 3, 4)); |
136 addr.push_back(MakeAddr(1, 2, 3, 6)); | 136 addr.push_back(MakeAddr(1, 2, 3, 6)); |
137 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); | 137 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); |
138 scoped_refptr<SocketStream> s4( | 138 scoped_refptr<SocketStream> s4( |
139 new SocketStream(GURL("ws://host4/"), w4.get())); | 139 new SocketStream(GURL("ws://host4/"), w4.get())); |
140 s4->set_context(&context); | 140 s4->set_context(&context); |
141 w4->InitSocketStream(s4.get()); | 141 w4->InitSocketStream(s4.get()); |
142 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); | 142 WebSocketThrottleTest::MockSocketStreamConnect(s4.get(), addr); |
143 | 143 |
144 DVLOG(1) << "socket4"; | 144 DVLOG(1) << "socket4"; |
145 TestCompletionCallback callback_s4; | 145 TestCompletionCallback callback_s4; |
146 // Trying to open connection to host4 will wait for w1, w2. | 146 // Trying to open connection to host4 will wait for w1, w2. |
147 EXPECT_EQ(ERR_IO_PENDING, | 147 EXPECT_EQ(ERR_IO_PENDING, |
148 w4->OnStartOpenConnection(s4, callback_s4.callback())); | 148 w4->OnStartOpenConnection(s4.get(), callback_s4.callback())); |
149 // Address | head -> tail | 149 // Address | head -> tail |
150 // 1.2.3.4 | w1 w2 w4 | 150 // 1.2.3.4 | w1 w2 w4 |
151 // 1.2.3.5 | w1 w3 | 151 // 1.2.3.5 | w1 w3 |
152 // 1.2.3.6 | w1 w4 | 152 // 1.2.3.6 | w1 w4 |
153 | 153 |
154 // For host5: 1.2.3.6 | 154 // For host5: 1.2.3.6 |
155 addr.clear(); | 155 addr.clear(); |
156 addr.push_back(MakeAddr(1, 2, 3, 6)); | 156 addr.push_back(MakeAddr(1, 2, 3, 6)); |
157 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); | 157 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); |
158 scoped_refptr<SocketStream> s5( | 158 scoped_refptr<SocketStream> s5( |
159 new SocketStream(GURL("ws://host5/"), w5.get())); | 159 new SocketStream(GURL("ws://host5/"), w5.get())); |
160 s5->set_context(&context); | 160 s5->set_context(&context); |
161 w5->InitSocketStream(s5.get()); | 161 w5->InitSocketStream(s5.get()); |
162 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); | 162 WebSocketThrottleTest::MockSocketStreamConnect(s5.get(), addr); |
163 | 163 |
164 DVLOG(1) << "socket5"; | 164 DVLOG(1) << "socket5"; |
165 TestCompletionCallback callback_s5; | 165 TestCompletionCallback callback_s5; |
166 // Trying to open connection to host5 will wait for w1, w4 | 166 // Trying to open connection to host5 will wait for w1, w4 |
167 EXPECT_EQ(ERR_IO_PENDING, | 167 EXPECT_EQ(ERR_IO_PENDING, |
168 w5->OnStartOpenConnection(s5, callback_s5.callback())); | 168 w5->OnStartOpenConnection(s5.get(), callback_s5.callback())); |
169 // Address | head -> tail | 169 // Address | head -> tail |
170 // 1.2.3.4 | w1 w2 w4 | 170 // 1.2.3.4 | w1 w2 w4 |
171 // 1.2.3.5 | w1 w3 | 171 // 1.2.3.5 | w1 w3 |
172 // 1.2.3.6 | w1 w4 w5 | 172 // 1.2.3.6 | w1 w4 w5 |
173 | 173 |
174 // For host6: 1.2.3.6 | 174 // For host6: 1.2.3.6 |
175 addr.clear(); | 175 addr.clear(); |
176 addr.push_back(MakeAddr(1, 2, 3, 6)); | 176 addr.push_back(MakeAddr(1, 2, 3, 6)); |
177 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); | 177 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); |
178 scoped_refptr<SocketStream> s6( | 178 scoped_refptr<SocketStream> s6( |
179 new SocketStream(GURL("ws://host6/"), w6.get())); | 179 new SocketStream(GURL("ws://host6/"), w6.get())); |
180 s6->set_context(&context); | 180 s6->set_context(&context); |
181 w6->InitSocketStream(s6.get()); | 181 w6->InitSocketStream(s6.get()); |
182 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); | 182 WebSocketThrottleTest::MockSocketStreamConnect(s6.get(), addr); |
183 | 183 |
184 DVLOG(1) << "socket6"; | 184 DVLOG(1) << "socket6"; |
185 TestCompletionCallback callback_s6; | 185 TestCompletionCallback callback_s6; |
186 // Trying to open connection to host6 will wait for w1, w4, w5 | 186 // Trying to open connection to host6 will wait for w1, w4, w5 |
187 EXPECT_EQ(ERR_IO_PENDING, | 187 EXPECT_EQ(ERR_IO_PENDING, |
188 w6->OnStartOpenConnection(s6, callback_s6.callback())); | 188 w6->OnStartOpenConnection(s6.get(), callback_s6.callback())); |
189 // Address | head -> tail | 189 // Address | head -> tail |
190 // 1.2.3.4 | w1 w2 w4 | 190 // 1.2.3.4 | w1 w2 w4 |
191 // 1.2.3.5 | w1 w3 | 191 // 1.2.3.5 | w1 w3 |
192 // 1.2.3.6 | w1 w4 w5 w6 | 192 // 1.2.3.6 | w1 w4 w5 w6 |
193 | 193 |
194 // Receive partial response on w1, still connecting. | 194 // Receive partial response on w1, still connecting. |
195 DVLOG(1) << "socket1 1"; | 195 DVLOG(1) << "socket1 1"; |
196 static const char kHeader[] = "HTTP/1.1 101 WebSocket Protocol\r\n"; | 196 static const char kHeader[] = "HTTP/1.1 101 WebSocket Protocol\r\n"; |
197 w1->OnReceivedData(s1.get(), kHeader, sizeof(kHeader) - 1); | 197 w1->OnReceivedData(s1.get(), kHeader, sizeof(kHeader) - 1); |
198 EXPECT_FALSE(callback_s2.have_result()); | 198 EXPECT_FALSE(callback_s2.have_result()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 | 285 |
286 // For localhost: 127.0.0.1, 127.0.0.1 | 286 // For localhost: 127.0.0.1, 127.0.0.1 |
287 AddressList addr; | 287 AddressList addr; |
288 addr.push_back(MakeAddr(127, 0, 0, 1)); | 288 addr.push_back(MakeAddr(127, 0, 0, 1)); |
289 addr.push_back(MakeAddr(127, 0, 0, 1)); | 289 addr.push_back(MakeAddr(127, 0, 0, 1)); |
290 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 290 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
291 scoped_refptr<SocketStream> s1( | 291 scoped_refptr<SocketStream> s1( |
292 new SocketStream(GURL("ws://localhost/"), w1.get())); | 292 new SocketStream(GURL("ws://localhost/"), w1.get())); |
293 s1->set_context(&context); | 293 s1->set_context(&context); |
294 w1->InitSocketStream(s1.get()); | 294 w1->InitSocketStream(s1.get()); |
295 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 295 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); |
296 | 296 |
297 DVLOG(1) << "socket1"; | 297 DVLOG(1) << "socket1"; |
298 TestCompletionCallback callback_s1; | 298 TestCompletionCallback callback_s1; |
299 // Trying to open connection to localhost will start without wait. | 299 // Trying to open connection to localhost will start without wait. |
300 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); | 300 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
301 | 301 |
302 DVLOG(1) << "socket1 close"; | 302 DVLOG(1) << "socket1 close"; |
303 w1->OnClose(s1.get()); | 303 w1->OnClose(s1.get()); |
304 s1->DetachDelegate(); | 304 s1->DetachDelegate(); |
305 DVLOG(1) << "Done"; | 305 DVLOG(1) << "Done"; |
306 base::MessageLoopForIO::current()->RunUntilIdle(); | 306 base::MessageLoopForIO::current()->RunUntilIdle(); |
307 } | 307 } |
308 | 308 |
309 // A connection should not be blocked by another connection to the same IP | 309 // A connection should not be blocked by another connection to the same IP |
310 // with a different port. | 310 // with a different port. |
311 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { | 311 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { |
312 TestURLRequestContext context; | 312 TestURLRequestContext context; |
313 DummySocketStreamDelegate delegate; | 313 DummySocketStreamDelegate delegate; |
314 IPAddressNumber localhost; | 314 IPAddressNumber localhost; |
315 ParseIPLiteralToNumber("127.0.0.1", &localhost); | 315 ParseIPLiteralToNumber("127.0.0.1", &localhost); |
316 WebSocketJob::set_websocket_over_spdy_enabled(false); | 316 WebSocketJob::set_websocket_over_spdy_enabled(false); |
317 | 317 |
318 // socket1: 127.0.0.1:80 | 318 // socket1: 127.0.0.1:80 |
319 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 319 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
320 scoped_refptr<SocketStream> s1( | 320 scoped_refptr<SocketStream> s1( |
321 new SocketStream(GURL("ws://localhost:80/"), w1.get())); | 321 new SocketStream(GURL("ws://localhost:80/"), w1.get())); |
322 s1->set_context(&context); | 322 s1->set_context(&context); |
323 w1->InitSocketStream(s1.get()); | 323 w1->InitSocketStream(s1.get()); |
324 MockSocketStreamConnect(s1, AddressList::CreateFromIPAddress(localhost, 80)); | 324 MockSocketStreamConnect(s1.get(), |
| 325 AddressList::CreateFromIPAddress(localhost, 80)); |
325 | 326 |
326 DVLOG(1) << "connecting socket1"; | 327 DVLOG(1) << "connecting socket1"; |
327 TestCompletionCallback callback_s1; | 328 TestCompletionCallback callback_s1; |
328 // Trying to open connection to localhost:80 will start without waiting. | 329 // Trying to open connection to localhost:80 will start without waiting. |
329 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); | 330 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
330 | 331 |
331 // socket2: 127.0.0.1:81 | 332 // socket2: 127.0.0.1:81 |
332 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); | 333 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
333 scoped_refptr<SocketStream> s2( | 334 scoped_refptr<SocketStream> s2( |
334 new SocketStream(GURL("ws://localhost:81/"), w2.get())); | 335 new SocketStream(GURL("ws://localhost:81/"), w2.get())); |
335 s2->set_context(&context); | 336 s2->set_context(&context); |
336 w2->InitSocketStream(s2.get()); | 337 w2->InitSocketStream(s2.get()); |
337 MockSocketStreamConnect(s2, AddressList::CreateFromIPAddress(localhost, 81)); | 338 MockSocketStreamConnect(s2.get(), |
| 339 AddressList::CreateFromIPAddress(localhost, 81)); |
338 | 340 |
339 DVLOG(1) << "connecting socket2"; | 341 DVLOG(1) << "connecting socket2"; |
340 TestCompletionCallback callback_s2; | 342 TestCompletionCallback callback_s2; |
341 // Trying to open connection to localhost:81 will start without waiting. | 343 // Trying to open connection to localhost:81 will start without waiting. |
342 EXPECT_EQ(OK, w2->OnStartOpenConnection(s2, callback_s2.callback())); | 344 EXPECT_EQ(OK, w2->OnStartOpenConnection(s2.get(), callback_s2.callback())); |
343 | 345 |
344 DVLOG(1) << "closing socket1"; | 346 DVLOG(1) << "closing socket1"; |
345 w1->OnClose(s1.get()); | 347 w1->OnClose(s1.get()); |
346 s1->DetachDelegate(); | 348 s1->DetachDelegate(); |
347 | 349 |
348 DVLOG(1) << "closing socket2"; | 350 DVLOG(1) << "closing socket2"; |
349 w2->OnClose(s2.get()); | 351 w2->OnClose(s2.get()); |
350 s2->DetachDelegate(); | 352 s2->DetachDelegate(); |
351 DVLOG(1) << "Done"; | 353 DVLOG(1) << "Done"; |
352 base::MessageLoopForIO::current()->RunUntilIdle(); | 354 base::MessageLoopForIO::current()->RunUntilIdle(); |
353 } | 355 } |
354 | 356 |
355 } | 357 } |
OLD | NEW |