| 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 "content/browser/renderer_host/websocket_dispatcher_host.h" | 5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "content/browser/renderer_host/websocket_host.h" | 12 #include "content/browser/renderer_host/websocket_host.h" |
| 13 #include "content/common/websocket.h" | 13 #include "content/common/websocket.h" |
| 14 #include "content/common/websocket_messages.h" | 14 #include "content/common/websocket_messages.h" |
| 15 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 #include "url/origin.h" | 18 #include "url/origin.h" |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // This number is unlikely to occur by chance. |
| 24 static const int kMagicRenderProcessId = 506116062; |
| 25 |
| 23 // A mock of WebsocketHost which records received messages. | 26 // A mock of WebsocketHost which records received messages. |
| 24 class MockWebSocketHost : public WebSocketHost { | 27 class MockWebSocketHost : public WebSocketHost { |
| 25 public: | 28 public: |
| 26 MockWebSocketHost(int routing_id, | 29 MockWebSocketHost(int routing_id, |
| 27 WebSocketDispatcherHost* dispatcher, | 30 WebSocketDispatcherHost* dispatcher, |
| 28 net::URLRequestContext* url_request_context) | 31 net::URLRequestContext* url_request_context) |
| 29 : WebSocketHost(routing_id, dispatcher, url_request_context) { | 32 : WebSocketHost(routing_id, dispatcher, url_request_context) { |
| 30 } | 33 } |
| 31 | 34 |
| 32 virtual ~MockWebSocketHost() {} | 35 virtual ~MockWebSocketHost() {} |
| 33 | 36 |
| 34 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ | 37 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ |
| 35 received_messages_.push_back(message); | 38 received_messages_.push_back(message); |
| 36 return true; | 39 return true; |
| 37 } | 40 } |
| 38 | 41 |
| 39 std::vector<IPC::Message> received_messages_; | 42 std::vector<IPC::Message> received_messages_; |
| 40 }; | 43 }; |
| 41 | 44 |
| 42 class WebSocketDispatcherHostTest : public ::testing::Test { | 45 class WebSocketDispatcherHostTest : public ::testing::Test { |
| 43 public: | 46 public: |
| 44 WebSocketDispatcherHostTest() { | 47 WebSocketDispatcherHostTest() { |
| 45 dispatcher_host_ = new WebSocketDispatcherHost( | 48 dispatcher_host_ = new WebSocketDispatcherHost( |
| 46 0, | 49 kMagicRenderProcessId, |
| 47 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 50 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
| 48 base::Unretained(this)), | 51 base::Unretained(this)), |
| 49 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 52 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
| 50 base::Unretained(this))); | 53 base::Unretained(this))); |
| 51 } | 54 } |
| 52 | 55 |
| 53 virtual ~WebSocketDispatcherHostTest() {} | 56 virtual ~WebSocketDispatcherHostTest() {} |
| 54 | 57 |
| 55 protected: | 58 protected: |
| 56 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 59 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 74 | 77 |
| 75 TEST_F(WebSocketDispatcherHostTest, Construct) { | 78 TEST_F(WebSocketDispatcherHostTest, Construct) { |
| 76 // Do nothing. | 79 // Do nothing. |
| 77 } | 80 } |
| 78 | 81 |
| 79 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 82 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
| 80 IPC::Message message; | 83 IPC::Message message; |
| 81 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 84 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
| 82 } | 85 } |
| 83 | 86 |
| 87 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
| 88 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); |
| 89 } |
| 90 |
| 84 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { | 91 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { |
| 85 int routing_id = 123; | 92 int routing_id = 123; |
| 86 GURL socket_url("ws://example.com/test"); | 93 GURL socket_url("ws://example.com/test"); |
| 87 std::vector<std::string> requested_protocols; | 94 std::vector<std::string> requested_protocols; |
| 88 requested_protocols.push_back("hello"); | 95 requested_protocols.push_back("hello"); |
| 89 url::Origin origin("http://example.com/test"); | 96 url::Origin origin("http://example.com/test"); |
| 97 int render_frame_id = -2; |
| 90 WebSocketHostMsg_AddChannelRequest message( | 98 WebSocketHostMsg_AddChannelRequest message( |
| 91 routing_id, socket_url, requested_protocols, origin); | 99 routing_id, socket_url, requested_protocols, origin, render_frame_id); |
| 92 | 100 |
| 93 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | 101 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
| 94 | 102 |
| 95 ASSERT_EQ(1U, mock_hosts_.size()); | 103 ASSERT_EQ(1U, mock_hosts_.size()); |
| 96 MockWebSocketHost* host = mock_hosts_[0]; | 104 MockWebSocketHost* host = mock_hosts_[0]; |
| 97 | 105 |
| 98 ASSERT_EQ(1U, host->received_messages_.size()); | 106 ASSERT_EQ(1U, host->received_messages_.size()); |
| 99 const IPC::Message& forwarded_message = host->received_messages_[0]; | 107 const IPC::Message& forwarded_message = host->received_messages_[0]; |
| 100 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 108 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 101 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 109 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 113 EXPECT_EQ(0U, mock_hosts_.size()); | 121 EXPECT_EQ(0U, mock_hosts_.size()); |
| 114 } | 122 } |
| 115 | 123 |
| 116 TEST_F(WebSocketDispatcherHostTest, SendFrame) { | 124 TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
| 117 int routing_id = 123; | 125 int routing_id = 123; |
| 118 | 126 |
| 119 GURL socket_url("ws://example.com/test"); | 127 GURL socket_url("ws://example.com/test"); |
| 120 std::vector<std::string> requested_protocols; | 128 std::vector<std::string> requested_protocols; |
| 121 requested_protocols.push_back("hello"); | 129 requested_protocols.push_back("hello"); |
| 122 url::Origin origin("http://example.com/test"); | 130 url::Origin origin("http://example.com/test"); |
| 131 int render_frame_id = -2; |
| 123 WebSocketHostMsg_AddChannelRequest add_channel_message( | 132 WebSocketHostMsg_AddChannelRequest add_channel_message( |
| 124 routing_id, socket_url, requested_protocols, origin); | 133 routing_id, socket_url, requested_protocols, origin, render_frame_id); |
| 125 | 134 |
| 126 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); | 135 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); |
| 127 | 136 |
| 128 std::vector<char> data; | 137 std::vector<char> data; |
| 129 WebSocketMsg_SendFrame send_frame_message( | 138 WebSocketMsg_SendFrame send_frame_message( |
| 130 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | 139 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
| 131 | 140 |
| 132 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); | 141 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); |
| 133 | 142 |
| 134 ASSERT_EQ(1U, mock_hosts_.size()); | 143 ASSERT_EQ(1U, mock_hosts_.size()); |
| 135 MockWebSocketHost* host = mock_hosts_[0]; | 144 MockWebSocketHost* host = mock_hosts_[0]; |
| 136 | 145 |
| 137 ASSERT_EQ(2U, host->received_messages_.size()); | 146 ASSERT_EQ(2U, host->received_messages_.size()); |
| 138 { | 147 { |
| 139 const IPC::Message& forwarded_message = host->received_messages_[0]; | 148 const IPC::Message& forwarded_message = host->received_messages_[0]; |
| 140 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 149 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
| 141 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 150 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 142 } | 151 } |
| 143 { | 152 { |
| 144 const IPC::Message& forwarded_message = host->received_messages_[1]; | 153 const IPC::Message& forwarded_message = host->received_messages_[1]; |
| 145 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 154 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
| 146 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 155 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
| 147 } | 156 } |
| 148 } | 157 } |
| 149 | 158 |
| 150 } // namespace | 159 } // namespace |
| 151 } // namespace content | 160 } // namespace content |
| OLD | NEW |