| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| 11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/socket_stream/socket_stream.h" | 12 #include "net/socket_stream/socket_stream.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "net/websockets/websocket_job.h" | 14 #include "net/websockets/websocket_job.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 class DummySocketStreamDelegate : public net::SocketStream::Delegate { | 19 namespace net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class DummySocketStreamDelegate : public SocketStream::Delegate { |
| 20 public: | 24 public: |
| 21 DummySocketStreamDelegate() {} | 25 DummySocketStreamDelegate() {} |
| 22 virtual ~DummySocketStreamDelegate() {} | 26 virtual ~DummySocketStreamDelegate() {} |
| 23 virtual void OnConnected( | 27 virtual void OnConnected( |
| 24 net::SocketStream* socket, int max_pending_send_allowed) OVERRIDE {} | 28 SocketStream* socket, int max_pending_send_allowed) OVERRIDE {} |
| 25 virtual void OnSentData(net::SocketStream* socket, | 29 virtual void OnSentData(SocketStream* socket, |
| 26 int amount_sent) OVERRIDE {} | 30 int amount_sent) OVERRIDE {} |
| 27 virtual void OnReceivedData(net::SocketStream* socket, | 31 virtual void OnReceivedData(SocketStream* socket, |
| 28 const char* data, int len) OVERRIDE {} | 32 const char* data, int len) OVERRIDE {} |
| 29 virtual void OnClose(net::SocketStream* socket) OVERRIDE {} | 33 virtual void OnClose(SocketStream* socket) OVERRIDE {} |
| 30 }; | 34 }; |
| 31 | 35 |
| 32 namespace net { | 36 class WebSocketThrottleTestContext : public TestURLRequestContext { |
| 37 public: |
| 38 WebSocketThrottleTestContext(bool enable_websocket_over_spdy) |
| 39 : TestURLRequestContext(true) { |
| 40 HttpNetworkSession::Params params; |
| 41 params.enable_websocket_over_spdy = enable_websocket_over_spdy; |
| 42 Init(); |
| 43 } |
| 44 }; |
| 45 |
| 46 } // namespace |
| 33 | 47 |
| 34 class WebSocketThrottleTest : public PlatformTest { | 48 class WebSocketThrottleTest : public PlatformTest { |
| 35 protected: | 49 protected: |
| 36 static IPEndPoint MakeAddr(int a1, int a2, int a3, int a4) { | 50 static IPEndPoint MakeAddr(int a1, int a2, int a3, int a4) { |
| 37 IPAddressNumber ip; | 51 IPAddressNumber ip; |
| 38 ip.push_back(a1); | 52 ip.push_back(a1); |
| 39 ip.push_back(a2); | 53 ip.push_back(a2); |
| 40 ip.push_back(a3); | 54 ip.push_back(a3); |
| 41 ip.push_back(a4); | 55 ip.push_back(a4); |
| 42 return IPEndPoint(ip, 0); | 56 return IPEndPoint(ip, 0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 // next_state_ is not STATE_NONE. | 67 // next_state_ is not STATE_NONE. |
| 54 // If next_state_ is STATE_NONE, SocketStream::Close() or | 68 // If next_state_ is STATE_NONE, SocketStream::Close() or |
| 55 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), | 69 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), |
| 56 // so Release() won't be called. Thus, we don't need socket->AddRef() | 70 // so Release() won't be called. Thus, we don't need socket->AddRef() |
| 57 // here. | 71 // here. |
| 58 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); | 72 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); |
| 59 } | 73 } |
| 60 }; | 74 }; |
| 61 | 75 |
| 62 TEST_F(WebSocketThrottleTest, Throttle) { | 76 TEST_F(WebSocketThrottleTest, Throttle) { |
| 63 TestURLRequestContext context; | |
| 64 DummySocketStreamDelegate delegate; | |
| 65 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled | 77 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled |
| 66 // configuration. | 78 // configuration. |
| 67 WebSocketJob::set_websocket_over_spdy_enabled(true); | 79 WebSocketThrottleTestContext context(true); |
| 80 DummySocketStreamDelegate delegate; |
| 68 | 81 |
| 69 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 82 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
| 70 AddressList addr; | 83 AddressList addr; |
| 71 addr.push_back(MakeAddr(1, 2, 3, 4)); | 84 addr.push_back(MakeAddr(1, 2, 3, 4)); |
| 72 addr.push_back(MakeAddr(1, 2, 3, 5)); | 85 addr.push_back(MakeAddr(1, 2, 3, 5)); |
| 73 addr.push_back(MakeAddr(1, 2, 3, 6)); | 86 addr.push_back(MakeAddr(1, 2, 3, 6)); |
| 74 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 87 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 75 scoped_refptr<SocketStream> s1( | 88 scoped_refptr<SocketStream> s1( |
| 76 new SocketStream(GURL("ws://host1/"), w1.get(), &context, NULL)); | 89 new SocketStream(GURL("ws://host1/"), w1.get(), &context, NULL)); |
| 77 w1->InitSocketStream(s1.get()); | 90 w1->InitSocketStream(s1.get()); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 w3->OnClose(s3.get()); | 279 w3->OnClose(s3.get()); |
| 267 base::MessageLoopForIO::current()->RunUntilIdle(); | 280 base::MessageLoopForIO::current()->RunUntilIdle(); |
| 268 s3->DetachDelegate(); | 281 s3->DetachDelegate(); |
| 269 w4->OnClose(s4.get()); | 282 w4->OnClose(s4.get()); |
| 270 s4->DetachDelegate(); | 283 s4->DetachDelegate(); |
| 271 DVLOG(1) << "Done"; | 284 DVLOG(1) << "Done"; |
| 272 base::MessageLoopForIO::current()->RunUntilIdle(); | 285 base::MessageLoopForIO::current()->RunUntilIdle(); |
| 273 } | 286 } |
| 274 | 287 |
| 275 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { | 288 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { |
| 276 TestURLRequestContext context; | 289 WebSocketThrottleTestContext context(true); |
| 277 DummySocketStreamDelegate delegate; | 290 DummySocketStreamDelegate delegate; |
| 278 WebSocketJob::set_websocket_over_spdy_enabled(true); | |
| 279 | 291 |
| 280 // For localhost: 127.0.0.1, 127.0.0.1 | 292 // For localhost: 127.0.0.1, 127.0.0.1 |
| 281 AddressList addr; | 293 AddressList addr; |
| 282 addr.push_back(MakeAddr(127, 0, 0, 1)); | 294 addr.push_back(MakeAddr(127, 0, 0, 1)); |
| 283 addr.push_back(MakeAddr(127, 0, 0, 1)); | 295 addr.push_back(MakeAddr(127, 0, 0, 1)); |
| 284 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 296 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 285 scoped_refptr<SocketStream> s1( | 297 scoped_refptr<SocketStream> s1( |
| 286 new SocketStream(GURL("ws://localhost/"), w1.get(), &context, NULL)); | 298 new SocketStream(GURL("ws://localhost/"), w1.get(), &context, NULL)); |
| 287 w1->InitSocketStream(s1.get()); | 299 w1->InitSocketStream(s1.get()); |
| 288 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); | 300 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); |
| 289 | 301 |
| 290 DVLOG(1) << "socket1"; | 302 DVLOG(1) << "socket1"; |
| 291 TestCompletionCallback callback_s1; | 303 TestCompletionCallback callback_s1; |
| 292 // Trying to open connection to localhost will start without wait. | 304 // Trying to open connection to localhost will start without wait. |
| 293 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); | 305 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
| 294 | 306 |
| 295 DVLOG(1) << "socket1 close"; | 307 DVLOG(1) << "socket1 close"; |
| 296 w1->OnClose(s1.get()); | 308 w1->OnClose(s1.get()); |
| 297 s1->DetachDelegate(); | 309 s1->DetachDelegate(); |
| 298 DVLOG(1) << "Done"; | 310 DVLOG(1) << "Done"; |
| 299 base::MessageLoopForIO::current()->RunUntilIdle(); | 311 base::MessageLoopForIO::current()->RunUntilIdle(); |
| 300 } | 312 } |
| 301 | 313 |
| 302 // A connection should not be blocked by another connection to the same IP | 314 // A connection should not be blocked by another connection to the same IP |
| 303 // with a different port. | 315 // with a different port. |
| 304 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { | 316 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { |
| 305 TestURLRequestContext context; | 317 WebSocketThrottleTestContext context(false); |
| 306 DummySocketStreamDelegate delegate; | 318 DummySocketStreamDelegate delegate; |
| 307 IPAddressNumber localhost; | 319 IPAddressNumber localhost; |
| 308 ParseIPLiteralToNumber("127.0.0.1", &localhost); | 320 ParseIPLiteralToNumber("127.0.0.1", &localhost); |
| 309 WebSocketJob::set_websocket_over_spdy_enabled(false); | |
| 310 | 321 |
| 311 // socket1: 127.0.0.1:80 | 322 // socket1: 127.0.0.1:80 |
| 312 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 323 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 313 scoped_refptr<SocketStream> s1( | 324 scoped_refptr<SocketStream> s1( |
| 314 new SocketStream(GURL("ws://localhost:80/"), w1.get(), &context, NULL)); | 325 new SocketStream(GURL("ws://localhost:80/"), w1.get(), &context, NULL)); |
| 315 w1->InitSocketStream(s1.get()); | 326 w1->InitSocketStream(s1.get()); |
| 316 MockSocketStreamConnect(s1.get(), | 327 MockSocketStreamConnect(s1.get(), |
| 317 AddressList::CreateFromIPAddress(localhost, 80)); | 328 AddressList::CreateFromIPAddress(localhost, 80)); |
| 318 | 329 |
| 319 DVLOG(1) << "connecting socket1"; | 330 DVLOG(1) << "connecting socket1"; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 338 w1->OnClose(s1.get()); | 349 w1->OnClose(s1.get()); |
| 339 s1->DetachDelegate(); | 350 s1->DetachDelegate(); |
| 340 | 351 |
| 341 DVLOG(1) << "closing socket2"; | 352 DVLOG(1) << "closing socket2"; |
| 342 w2->OnClose(s2.get()); | 353 w2->OnClose(s2.get()); |
| 343 s2->DetachDelegate(); | 354 s2->DetachDelegate(); |
| 344 DVLOG(1) << "Done"; | 355 DVLOG(1) << "Done"; |
| 345 base::MessageLoopForIO::current()->RunUntilIdle(); | 356 base::MessageLoopForIO::current()->RunUntilIdle(); |
| 346 } | 357 } |
| 347 | 358 |
| 348 } | 359 } // namespace net |
| OLD | NEW |