Index: content/browser/websockets/websocket_manager_unittest.cc |
diff --git a/content/browser/websockets/websocket_manager_unittest.cc b/content/browser/websockets/websocket_manager_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6f7a11b50c2f72929b16eceab9f0c45595b3757c |
--- /dev/null |
+++ b/content/browser/websockets/websocket_manager_unittest.cc |
@@ -0,0 +1,377 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <algorithm> |
+#include <memory> |
+#include <vector> |
+ |
+#include "content/browser/websockets/websocket_manager.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+#include "url/origin.h" |
+ |
+namespace content { |
+namespace { |
+ |
+// This number is unlikely to occur by chance. |
+static const int kMagicRenderProcessId = 506116062; |
+ |
+class TestWebSocketImpl : public WebSocketImpl { |
+ public: |
+ TestWebSocketImpl(Delegate* delegate, |
+ mojom::WebSocketRequest request, |
+ base::TimeDelta delay) |
+ : WebSocketImpl(delegate, std::move(request), delay) {} |
+ |
+ base::TimeDelta delay() const { return delay_; } |
+ |
+ void SimulateConnectionError() { |
+ OnConnectionError(); |
+ } |
+}; |
+ |
+class TestWebSocketManager : public WebSocketManager { |
+ public: |
+ TestWebSocketManager() |
+ : WebSocketManager(kMagicRenderProcessId, nullptr) {} |
+ |
+ const std::vector<TestWebSocketImpl*>& sockets() const { |
+ return sockets_; |
+ } |
+ |
+ int num_pending_connections() const { |
+ return num_pending_connections_; |
+ } |
+ int64_t num_failed_connections() const { |
+ return num_current_failed_connections_ + num_previous_failed_connections_; |
+ } |
+ int64_t num_succeeded_connections() const { |
+ return num_current_succeeded_connections_ + |
+ num_previous_succeeded_connections_; |
+ } |
+ |
+ void DoCreateWebSocket(mojom::WebSocketRequest request) { |
+ WebSocketManager::DoCreateWebSocket(std::move(request)); |
+ } |
+ |
+ private: |
+ WebSocketImpl* CreateWebSocketImpl(WebSocketImpl::Delegate* delegate, |
+ mojom::WebSocketRequest request, |
+ base::TimeDelta delay) override { |
+ TestWebSocketImpl* impl = |
+ new TestWebSocketImpl(delegate, std::move(request), delay); |
+ // We keep a vector of sockets here to track their creation order. |
+ sockets_.push_back(impl); |
+ return impl; |
+ } |
+ |
+ void OnLostConnectionToClient(WebSocketImpl* impl) override { |
+ auto it = std::find(sockets_.begin(), sockets_.end(), |
+ static_cast<TestWebSocketImpl*>(impl)); |
+ ASSERT_TRUE(it != sockets_.end()); |
+ sockets_.erase(it); |
+ |
+ WebSocketManager::OnLostConnectionToClient(impl); |
+ } |
+ |
+ std::vector<TestWebSocketImpl*> sockets_; |
+}; |
+ |
+class WebSocketManagerTest : public ::testing::Test { |
+ public: |
+ WebSocketManagerTest() |
+ : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
+ websocket_manager_.reset(new TestWebSocketManager()); |
+ } |
+ |
+ void AddMultipleChannels(int number_of_channels) { |
+ for (int i = 0; i < number_of_channels; ++i) { |
+ mojom::WebSocketPtr websocket; |
+ websocket_manager_->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
+ } |
+ } |
+ |
+ void AddAndCancelMultipleChannels(int number_of_channels) { |
+ for (int i = 0; i < number_of_channels; ++i) { |
+ mojom::WebSocketPtr websocket; |
+ websocket_manager_->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
+ websocket_manager_->sockets().back()->SimulateConnectionError(); |
+ } |
+ } |
+ |
+ TestWebSocketManager* websocket_manager() { return websocket_manager_.get(); } |
+ |
+ private: |
+ TestBrowserThreadBundle thread_bundle_; |
+ std::unique_ptr<TestWebSocketManager> websocket_manager_; |
+}; |
+ |
+TEST_F(WebSocketManagerTest, Construct) { |
+ // Do nothing. |
+} |
+ |
+#if 0 |
Adam Rice
2016/07/27 02:38:54
Please delete all the #if 0s before checking in.
|
+TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
+ IPC::Message message; |
+ EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
+} |
+ |
+TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
+ EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); |
+} |
+#endif |
+ |
+TEST_F(WebSocketManagerTest, CreateWebSocket) { |
+ mojom::WebSocketPtr websocket; |
+ |
+ websocket_manager()->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
+ |
+ EXPECT_EQ(1U, websocket_manager()->sockets().size()); |
+} |
+ |
+#if 0 |
+TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { |
+ int routing_id = 123; |
+ GURL socket_url("ws://example.com/test"); |
+ std::vector<std::string> requested_protocols; |
+ requested_protocols.push_back("hello"); |
+ url::Origin origin(GURL("http://example.com")); |
+ WebSocketHostMsg_AddChannelRequest message( |
+ routing_id, socket_url, requested_protocols, origin, ""); |
+ |
+ ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
+ |
+ ASSERT_EQ(1U, mock_hosts_.size()); |
+ MockWebSocketHost* host = mock_hosts_[0]; |
+ |
+ ASSERT_EQ(1U, host->received_messages_.size()); |
+ const IPC::Message& forwarded_message = host->received_messages_[0]; |
+ EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
+ EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
+} |
+#endif |
+ |
+TEST_F(WebSocketManagerTest, SendFrameButNotConnectedYet) { |
+ mojom::WebSocketPtr websocket; |
+ |
+ websocket_manager()->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
+ |
+ // This should not crash. |
+ mojo::Array<uint8_t> data; |
+ websocket->SendFrame( |
+ true, mojom::WebSocketMessageType::TEXT, std::move(data)); |
+} |
+ |
+#if 0 |
+TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) { |
+ int routing_id = 123; |
+ std::vector<char> data; |
+ WebSocketMsg_SendFrame message( |
+ routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
+ |
+ // Expected to be ignored. |
+ EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
+ |
+ EXPECT_EQ(0U, mock_hosts_.size()); |
+} |
+#endif |
+ |
+#if 0 |
+TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
+ int routing_id = 123; |
+ |
+ GURL socket_url("ws://example.com/test"); |
+ std::vector<std::string> requested_protocols; |
+ requested_protocols.push_back("hello"); |
+ url::Origin origin(GURL("http://example.com")); |
+ int render_frame_id = -2; |
+ WebSocketHostMsg_AddChannelRequest add_channel_message( |
+ routing_id, socket_url, requested_protocols, origin, "", render_frame_id); |
+ |
+ ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); |
+ |
+ std::vector<char> data; |
+ WebSocketMsg_SendFrame send_frame_message( |
+ routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
+ |
+ EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); |
+ |
+ ASSERT_EQ(1U, mock_hosts_.size()); |
+ MockWebSocketHost* host = mock_hosts_[0]; |
+ |
+ ASSERT_EQ(2U, host->received_messages_.size()); |
+ { |
+ const IPC::Message& forwarded_message = host->received_messages_[0]; |
+ EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
+ EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
+ } |
+ { |
+ const IPC::Message& forwarded_message = host->received_messages_[1]; |
+ EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
+ EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
+ } |
+} |
+ |
+TEST_F(WebSocketDispatcherHostTest, Destruct) { |
+ WebSocketHostMsg_AddChannelRequest message1( |
+ 123, GURL("ws://example.com/test"), std::vector<std::string>(), |
+ url::Origin(GURL("http://example.com")), "", -1); |
+ WebSocketHostMsg_AddChannelRequest message2( |
+ 456, GURL("ws://example.com/test2"), std::vector<std::string>(), |
+ url::Origin(GURL("http://example.com")), "", -1); |
+ |
+ ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); |
+ ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); |
+ |
+ ASSERT_EQ(2u, mock_hosts_.size()); |
+ |
+ mock_hosts_.clear(); |
+ dispatcher_host_ = NULL; |
+ |
+ ASSERT_EQ(2u, gone_hosts_.size()); |
+ // The gone_hosts_ ordering is not predictable because it depends on the |
+ // hash_map ordering. |
+ std::sort(gone_hosts_.begin(), gone_hosts_.end()); |
+ EXPECT_EQ(123, gone_hosts_[0]); |
+ EXPECT_EQ(456, gone_hosts_[1]); |
+} |
+#endif |
+ |
+TEST_F(WebSocketManagerTest, DelayFor4thPendingConnectionIsZero) { |
+ AddMultipleChannels(4); |
+ |
+ EXPECT_EQ(4, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ ASSERT_EQ(4U, websocket_manager()->sockets().size()); |
+ EXPECT_EQ(base::TimeDelta(), websocket_manager()->sockets()[3]->delay()); |
+} |
+ |
+TEST_F(WebSocketManagerTest, DelayFor8thPendingConnectionIsNonZero) { |
+ AddMultipleChannels(8); |
+ |
+ EXPECT_EQ(8, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ ASSERT_EQ(8U, websocket_manager()->sockets().size()); |
+ EXPECT_LT(base::TimeDelta(), websocket_manager()->sockets()[7]->delay()); |
+} |
+ |
+TEST_F(WebSocketManagerTest, DelayFor17thPendingConnection) { |
+ AddMultipleChannels(17); |
+ |
+ EXPECT_EQ(17, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ ASSERT_EQ(17U, websocket_manager()->sockets().size()); |
+ EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), |
+ websocket_manager()->sockets()[16]->delay()); |
+ EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), |
+ websocket_manager()->sockets()[16]->delay()); |
+} |
+ |
+// The 256th connection is rejected by per-renderer WebSocket throttling. |
+// This is not counted as a failure. |
+TEST_F(WebSocketManagerTest, Rejects256thPendingConnection) { |
+ AddMultipleChannels(256); |
+ |
+ EXPECT_EQ(255, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ ASSERT_EQ(255U, websocket_manager()->sockets().size()); |
+} |
+ |
+TEST_F(WebSocketManagerTest, DelayIsZeroAfter3FailedConnections) { |
+ AddAndCancelMultipleChannels(3); |
+ |
+ EXPECT_EQ(0, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(3, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ AddMultipleChannels(1); |
+ |
+ ASSERT_EQ(1U, websocket_manager()->sockets().size()); |
+ EXPECT_EQ(base::TimeDelta(), websocket_manager()->sockets()[0]->delay()); |
+} |
+ |
+TEST_F(WebSocketManagerTest, DelayIsNonZeroAfter7FailedConnections) { |
+ AddAndCancelMultipleChannels(7); |
+ |
+ EXPECT_EQ(0, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(7, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ AddMultipleChannels(1); |
+ |
+ ASSERT_EQ(1U, websocket_manager()->sockets().size()); |
+ EXPECT_LT(base::TimeDelta(), websocket_manager()->sockets()[0]->delay()); |
+} |
+ |
+TEST_F(WebSocketManagerTest, DelayAfter16FailedConnections) { |
+ AddAndCancelMultipleChannels(16); |
+ |
+ EXPECT_EQ(0, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(16, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ AddMultipleChannels(1); |
+ |
+ ASSERT_EQ(1U, websocket_manager()->sockets().size()); |
+ EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), |
+ websocket_manager()->sockets()[0]->delay()); |
+ EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), |
+ websocket_manager()->sockets()[0]->delay()); |
+} |
+ |
+TEST_F(WebSocketManagerTest, NotRejectedAfter255FailedConnections) { |
+ AddAndCancelMultipleChannels(255); |
+ |
+ EXPECT_EQ(0, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(255, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+ |
+ AddMultipleChannels(1); |
+ |
+ EXPECT_EQ(1, websocket_manager()->num_pending_connections()); |
+ EXPECT_EQ(255, websocket_manager()->num_failed_connections()); |
+ EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
+} |
+ |
+#if 0 |
+// This is a regression test for https://crrev.com/998173003/. |
+TEST_F(WebSocketDispatcherHostTest, InvalidScheme) { |
+ int routing_id = 123; |
+ GURL socket_url("http://example.com/test"); |
+ std::vector<std::string> requested_protocols; |
+ requested_protocols.push_back("hello"); |
+ url::Origin origin(GURL("http://example.com")); |
+ int render_frame_id = -2; |
+ WebSocketHostMsg_AddChannelRequest message( |
+ routing_id, socket_url, requested_protocols, origin, "", render_frame_id); |
+ |
+ ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
+ |
+ ASSERT_EQ(1U, mock_hosts_.size()); |
+ MockWebSocketHost* host = mock_hosts_[0]; |
+ |
+ // Tests that WebSocketHost::OnMessageReceived() doesn't cause a crash and |
+ // the connection with an invalid scheme fails here. |
+ // We call WebSocketHost::OnMessageReceived() here explicitly because |
+ // MockWebSocketHost does not call WebSocketHost::OnMessageReceived() for |
+ // WebSocketHostMsg_AddChannelRequest. |
+ host->WebSocketHost::OnMessageReceived(message); |
+ |
+ EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); |
+ EXPECT_EQ(1, dispatcher_host_->num_failed_connections()); |
+ EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); |
+} |
+#endif |
+ |
+} // namespace |
+} // namespace content |