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 <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/message_loop/message_loop.h" | |
15 #include "content/browser/renderer_host/websocket_host.h" | 14 #include "content/browser/renderer_host/websocket_host.h" |
16 #include "content/common/websocket.h" | 15 #include "content/common/websocket.h" |
17 #include "content/common/websocket_messages.h" | 16 #include "content/common/websocket_messages.h" |
18 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
19 #include "net/websockets/websocket_errors.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "url/gurl.h" | 19 #include "url/gurl.h" |
22 #include "url/origin.h" | 20 #include "url/origin.h" |
23 | 21 |
24 namespace content { | 22 namespace content { |
25 namespace { | 23 namespace { |
26 | 24 |
27 // This number is unlikely to occur by chance. | 25 // This number is unlikely to occur by chance. |
28 static const int kMagicRenderProcessId = 506116062; | 26 static const int kMagicRenderProcessId = 506116062; |
29 | 27 |
30 class WebSocketDispatcherHostTest; | 28 class WebSocketDispatcherHostTest; |
31 | 29 |
32 // A mock of WebsocketHost which records received messages. | 30 // A mock of WebsocketHost which records received messages. |
33 class MockWebSocketHost : public WebSocketHost { | 31 class MockWebSocketHost : public WebSocketHost { |
34 public: | 32 public: |
35 MockWebSocketHost(int routing_id, | 33 MockWebSocketHost(int routing_id, |
36 WebSocketDispatcherHost* dispatcher, | 34 WebSocketDispatcherHost* dispatcher, |
37 net::URLRequestContext* url_request_context, | 35 net::URLRequestContext* url_request_context, |
38 base::TimeDelta delay, | |
39 WebSocketDispatcherHostTest* owner); | 36 WebSocketDispatcherHostTest* owner); |
40 | 37 |
41 ~MockWebSocketHost() override {} | 38 ~MockWebSocketHost() override {} |
42 | 39 |
43 bool OnMessageReceived(const IPC::Message& message) override { | 40 bool OnMessageReceived(const IPC::Message& message) override { |
44 received_messages_.push_back(message); | 41 received_messages_.push_back(message); |
45 switch (message.type()) { | 42 return true; |
46 case WebSocketMsg_DropChannel::ID: | |
47 // Needed for PerRendererThrottlingFailedHandshakes, because without | |
48 // calling WebSocketHost::OnMessageReceived() (and thus | |
49 // WebSocketHost::OnDropChannel()), the connection stays pending and | |
50 // we cannot test per-renderer throttling with failed connections. | |
51 return WebSocketHost::OnMessageReceived(message); | |
52 | |
53 default: | |
54 return true; | |
55 } | |
56 } | 43 } |
57 | 44 |
58 void GoAway() override; | 45 void GoAway() override; |
59 | 46 |
60 std::vector<IPC::Message> received_messages_; | 47 std::vector<IPC::Message> received_messages_; |
61 base::WeakPtr<WebSocketDispatcherHostTest> owner_; | 48 base::WeakPtr<WebSocketDispatcherHostTest> owner_; |
62 base::TimeDelta delay_; | |
63 }; | |
64 | |
65 class TestingWebSocketDispatcherHost : public WebSocketDispatcherHost { | |
66 public: | |
67 TestingWebSocketDispatcherHost( | |
68 int process_id, | |
69 const GetRequestContextCallback& get_context_callback, | |
70 const WebSocketHostFactory& websocket_host_factory) | |
71 : WebSocketDispatcherHost(process_id, | |
72 get_context_callback, | |
73 websocket_host_factory) {} | |
74 | |
75 // This is needed because BrowserMessageFilter::Send() tries post the task to | |
76 // the IO thread, which doesn't exist in the context of these tests. | |
77 bool Send(IPC::Message* message) override { | |
78 delete message; | |
79 return true; | |
80 } | |
81 | |
82 using WebSocketDispatcherHost::num_pending_connections; | |
83 using WebSocketDispatcherHost::num_failed_connections; | |
84 using WebSocketDispatcherHost::num_succeeded_connections; | |
85 | |
86 private: | |
87 ~TestingWebSocketDispatcherHost() override {} | |
88 }; | 49 }; |
89 | 50 |
90 class WebSocketDispatcherHostTest : public ::testing::Test { | 51 class WebSocketDispatcherHostTest : public ::testing::Test { |
91 public: | 52 public: |
92 WebSocketDispatcherHostTest() | 53 WebSocketDispatcherHostTest() |
93 : next_routing_id_(123), | 54 : weak_ptr_factory_(this) { |
94 weak_ptr_factory_(this) { | 55 dispatcher_host_ = new WebSocketDispatcherHost( |
95 dispatcher_host_ = new TestingWebSocketDispatcherHost( | |
96 kMagicRenderProcessId, | 56 kMagicRenderProcessId, |
97 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 57 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
98 base::Unretained(this)), | 58 base::Unretained(this)), |
99 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 59 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
100 base::Unretained(this))); | 60 base::Unretained(this))); |
101 } | 61 } |
102 | 62 |
103 ~WebSocketDispatcherHostTest() override { | 63 ~WebSocketDispatcherHostTest() override { |
104 // We need to invalidate the issued WeakPtrs at the beginning of the | 64 // We need to invalidate the issued WeakPtrs at the beginning of the |
105 // destructor in order not to access destructed member variables. | 65 // destructor in order not to access destructed member variables. |
106 weak_ptr_factory_.InvalidateWeakPtrs(); | 66 weak_ptr_factory_.InvalidateWeakPtrs(); |
107 } | 67 } |
108 | 68 |
109 void GoAway(int routing_id) { | 69 void GoAway(int routing_id) { |
110 gone_hosts_.push_back(routing_id); | 70 gone_hosts_.push_back(routing_id); |
111 } | 71 } |
112 | 72 |
113 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { | 73 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { |
114 return weak_ptr_factory_.GetWeakPtr(); | 74 return weak_ptr_factory_.GetWeakPtr(); |
115 } | 75 } |
116 | 76 |
117 protected: | 77 protected: |
118 // Adds |n| connections. Returns true if succeeded. | 78 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
119 bool AddMultipleChannels(int number_of_channels) { | |
120 GURL socket_url("ws://example.com/test"); | |
121 std::vector<std::string> requested_protocols; | |
122 url::Origin origin("http://example.com"); | |
123 int render_frame_id = -3; | |
124 | |
125 for (int i = 0; i < number_of_channels; ++i) { | |
126 int routing_id = next_routing_id_++; | |
127 WebSocketHostMsg_AddChannelRequest message( | |
128 routing_id, | |
129 socket_url, | |
130 requested_protocols, | |
131 origin, | |
132 render_frame_id); | |
133 if (!dispatcher_host_->OnMessageReceived(message)) | |
134 return false; | |
135 } | |
136 | |
137 return true; | |
138 } | |
139 | |
140 // Adds and cancels |n| connections. Returns true if succeeded. | |
141 bool AddAndCancelMultipleChannels(int number_of_channels) { | |
142 GURL socket_url("ws://example.com/test"); | |
143 std::vector<std::string> requested_protocols; | |
144 url::Origin origin("http://example.com"); | |
145 int render_frame_id = -3; | |
146 | |
147 for (int i = 0; i < number_of_channels; ++i) { | |
148 int routing_id = next_routing_id_++; | |
149 WebSocketHostMsg_AddChannelRequest messageAddChannelRequest( | |
150 routing_id, | |
151 socket_url, | |
152 requested_protocols, | |
153 origin, | |
154 render_frame_id); | |
155 if (!dispatcher_host_->OnMessageReceived(messageAddChannelRequest)) | |
156 return false; | |
157 | |
158 WebSocketMsg_DropChannel messageDropChannel( | |
159 routing_id, false, net::kWebSocketErrorAbnormalClosure, ""); | |
160 if (!dispatcher_host_->OnMessageReceived(messageDropChannel)) | |
161 return false; | |
162 } | |
163 | |
164 return true; | |
165 } | |
166 | |
167 scoped_refptr<TestingWebSocketDispatcherHost> dispatcher_host_; | |
168 | 79 |
169 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 80 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
170 // them. | 81 // them. |
171 std::vector<MockWebSocketHost*> mock_hosts_; | 82 std::vector<MockWebSocketHost*> mock_hosts_; |
172 std::vector<int> gone_hosts_; | 83 std::vector<int> gone_hosts_; |
173 | 84 |
| 85 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; |
| 86 |
174 private: | 87 private: |
175 net::URLRequestContext* OnGetRequestContext() { | 88 net::URLRequestContext* OnGetRequestContext() { |
176 return NULL; | 89 return NULL; |
177 } | 90 } |
178 | 91 |
179 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay) { | 92 WebSocketHost* CreateWebSocketHost(int routing_id) { |
180 MockWebSocketHost* host = new MockWebSocketHost( | 93 MockWebSocketHost* host = |
181 routing_id, dispatcher_host_.get(), NULL, delay, this); | 94 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); |
182 mock_hosts_.push_back(host); | 95 mock_hosts_.push_back(host); |
183 return host; | 96 return host; |
184 } | 97 } |
185 | |
186 base::MessageLoop message_loop_; | |
187 | |
188 int next_routing_id_; | |
189 | |
190 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; | |
191 }; | 98 }; |
192 | 99 |
193 MockWebSocketHost::MockWebSocketHost( | 100 MockWebSocketHost::MockWebSocketHost( |
194 int routing_id, | 101 int routing_id, |
195 WebSocketDispatcherHost* dispatcher, | 102 WebSocketDispatcherHost* dispatcher, |
196 net::URLRequestContext* url_request_context, | 103 net::URLRequestContext* url_request_context, |
197 base::TimeDelta delay, | |
198 WebSocketDispatcherHostTest* owner) | 104 WebSocketDispatcherHostTest* owner) |
199 : WebSocketHost(routing_id, dispatcher, url_request_context, delay), | 105 : WebSocketHost(routing_id, dispatcher, url_request_context), |
200 owner_(owner->GetWeakPtr()), | 106 owner_(owner->GetWeakPtr()) {} |
201 delay_(delay) {} | |
202 | 107 |
203 void MockWebSocketHost::GoAway() { | 108 void MockWebSocketHost::GoAway() { |
204 if (owner_) | 109 if (owner_) |
205 owner_->GoAway(routing_id()); | 110 owner_->GoAway(routing_id()); |
206 } | 111 } |
207 | 112 |
208 TEST_F(WebSocketDispatcherHostTest, Construct) { | 113 TEST_F(WebSocketDispatcherHostTest, Construct) { |
209 // Do nothing. | 114 // Do nothing. |
210 } | 115 } |
211 | 116 |
212 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 117 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
213 IPC::Message message; | 118 IPC::Message message; |
214 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 119 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
215 } | 120 } |
216 | 121 |
217 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 122 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
218 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); | 123 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); |
219 } | 124 } |
220 | 125 |
221 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { | 126 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { |
222 int routing_id = 123; | 127 int routing_id = 123; |
223 GURL socket_url("ws://example.com/test"); | 128 GURL socket_url("ws://example.com/test"); |
224 std::vector<std::string> requested_protocols; | 129 std::vector<std::string> requested_protocols; |
225 requested_protocols.push_back("hello"); | 130 requested_protocols.push_back("hello"); |
226 url::Origin origin("http://example.com"); | 131 url::Origin origin("http://example.com/test"); |
227 int render_frame_id = -2; | 132 int render_frame_id = -2; |
228 WebSocketHostMsg_AddChannelRequest message( | 133 WebSocketHostMsg_AddChannelRequest message( |
229 routing_id, socket_url, requested_protocols, origin, render_frame_id); | 134 routing_id, socket_url, requested_protocols, origin, render_frame_id); |
230 | 135 |
231 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | 136 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); |
232 | 137 |
233 ASSERT_EQ(1U, mock_hosts_.size()); | 138 ASSERT_EQ(1U, mock_hosts_.size()); |
234 MockWebSocketHost* host = mock_hosts_[0]; | 139 MockWebSocketHost* host = mock_hosts_[0]; |
235 | 140 |
236 ASSERT_EQ(1U, host->received_messages_.size()); | 141 ASSERT_EQ(1U, host->received_messages_.size()); |
(...skipping 13 matching lines...) Expand all Loading... |
250 | 155 |
251 EXPECT_EQ(0U, mock_hosts_.size()); | 156 EXPECT_EQ(0U, mock_hosts_.size()); |
252 } | 157 } |
253 | 158 |
254 TEST_F(WebSocketDispatcherHostTest, SendFrame) { | 159 TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
255 int routing_id = 123; | 160 int routing_id = 123; |
256 | 161 |
257 GURL socket_url("ws://example.com/test"); | 162 GURL socket_url("ws://example.com/test"); |
258 std::vector<std::string> requested_protocols; | 163 std::vector<std::string> requested_protocols; |
259 requested_protocols.push_back("hello"); | 164 requested_protocols.push_back("hello"); |
260 url::Origin origin("http://example.com"); | 165 url::Origin origin("http://example.com/test"); |
261 int render_frame_id = -2; | 166 int render_frame_id = -2; |
262 WebSocketHostMsg_AddChannelRequest add_channel_message( | 167 WebSocketHostMsg_AddChannelRequest add_channel_message( |
263 routing_id, socket_url, requested_protocols, origin, render_frame_id); | 168 routing_id, socket_url, requested_protocols, origin, render_frame_id); |
264 | 169 |
265 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); | 170 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); |
266 | 171 |
267 std::vector<char> data; | 172 std::vector<char> data; |
268 WebSocketMsg_SendFrame send_frame_message( | 173 WebSocketMsg_SendFrame send_frame_message( |
269 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | 174 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); |
270 | 175 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 dispatcher_host_ = NULL; | 208 dispatcher_host_ = NULL; |
304 | 209 |
305 ASSERT_EQ(2u, gone_hosts_.size()); | 210 ASSERT_EQ(2u, gone_hosts_.size()); |
306 // The gone_hosts_ ordering is not predictable because it depends on the | 211 // The gone_hosts_ ordering is not predictable because it depends on the |
307 // hash_map ordering. | 212 // hash_map ordering. |
308 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | 213 std::sort(gone_hosts_.begin(), gone_hosts_.end()); |
309 EXPECT_EQ(123, gone_hosts_[0]); | 214 EXPECT_EQ(123, gone_hosts_[0]); |
310 EXPECT_EQ(456, gone_hosts_[1]); | 215 EXPECT_EQ(456, gone_hosts_[1]); |
311 } | 216 } |
312 | 217 |
313 TEST_F(WebSocketDispatcherHostTest, DelayFor4thPendingConnectionIsZero) { | |
314 ASSERT_TRUE(AddMultipleChannels(4)); | |
315 | |
316 EXPECT_EQ(4, dispatcher_host_->num_pending_connections()); | |
317 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
318 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
319 | |
320 ASSERT_EQ(4U, mock_hosts_.size()); | |
321 EXPECT_EQ(base::TimeDelta(), mock_hosts_[3]->delay_); | |
322 } | |
323 | |
324 TEST_F(WebSocketDispatcherHostTest, DelayFor8thPendingConnectionIsNonZero) { | |
325 ASSERT_TRUE(AddMultipleChannels(8)); | |
326 | |
327 EXPECT_EQ(8, dispatcher_host_->num_pending_connections()); | |
328 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
329 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
330 | |
331 ASSERT_EQ(8U, mock_hosts_.size()); | |
332 EXPECT_LT(base::TimeDelta(), mock_hosts_[7]->delay_); | |
333 } | |
334 | |
335 TEST_F(WebSocketDispatcherHostTest, DelayFor17thPendingConnection) { | |
336 ASSERT_TRUE(AddMultipleChannels(17)); | |
337 | |
338 EXPECT_EQ(17, dispatcher_host_->num_pending_connections()); | |
339 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
340 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
341 | |
342 ASSERT_EQ(17U, mock_hosts_.size()); | |
343 EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), mock_hosts_[16]->delay_); | |
344 EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), mock_hosts_[16]->delay_); | |
345 } | |
346 | |
347 // The 256th connection is rejected by per-renderer WebSocket throttling. | |
348 // This is not counted as a failure. | |
349 TEST_F(WebSocketDispatcherHostTest, Rejects256thPendingConnection) { | |
350 ASSERT_TRUE(AddMultipleChannels(256)); | |
351 | |
352 EXPECT_EQ(255, dispatcher_host_->num_pending_connections()); | |
353 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
354 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
355 | |
356 ASSERT_EQ(255U, mock_hosts_.size()); | |
357 } | |
358 | |
359 TEST_F(WebSocketDispatcherHostTest, DelayIsZeroAfter3FailedConnections) { | |
360 ASSERT_TRUE(AddAndCancelMultipleChannels(3)); | |
361 | |
362 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
363 EXPECT_EQ(3, dispatcher_host_->num_failed_connections()); | |
364 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
365 | |
366 ASSERT_TRUE(AddMultipleChannels(1)); | |
367 | |
368 ASSERT_EQ(4U, mock_hosts_.size()); | |
369 EXPECT_EQ(base::TimeDelta(), mock_hosts_[3]->delay_); | |
370 } | |
371 | |
372 TEST_F(WebSocketDispatcherHostTest, DelayIsNonZeroAfter7FailedConnections) { | |
373 ASSERT_TRUE(AddAndCancelMultipleChannels(7)); | |
374 | |
375 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
376 EXPECT_EQ(7, dispatcher_host_->num_failed_connections()); | |
377 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
378 | |
379 ASSERT_TRUE(AddMultipleChannels(1)); | |
380 | |
381 ASSERT_EQ(8U, mock_hosts_.size()); | |
382 EXPECT_LT(base::TimeDelta(), mock_hosts_[7]->delay_); | |
383 } | |
384 | |
385 TEST_F(WebSocketDispatcherHostTest, DelayAfter16FailedConnections) { | |
386 ASSERT_TRUE(AddAndCancelMultipleChannels(16)); | |
387 | |
388 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
389 EXPECT_EQ(16, dispatcher_host_->num_failed_connections()); | |
390 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
391 | |
392 ASSERT_TRUE(AddMultipleChannels(1)); | |
393 | |
394 ASSERT_EQ(17U, mock_hosts_.size()); | |
395 EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), mock_hosts_[16]->delay_); | |
396 EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), mock_hosts_[16]->delay_); | |
397 } | |
398 | |
399 TEST_F(WebSocketDispatcherHostTest, NotRejectedAfter255FailedConnections) { | |
400 ASSERT_TRUE(AddAndCancelMultipleChannels(255)); | |
401 | |
402 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
403 EXPECT_EQ(255, dispatcher_host_->num_failed_connections()); | |
404 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
405 | |
406 ASSERT_TRUE(AddMultipleChannels(1)); | |
407 | |
408 EXPECT_EQ(1, dispatcher_host_->num_pending_connections()); | |
409 EXPECT_EQ(255, dispatcher_host_->num_failed_connections()); | |
410 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
411 } | |
412 | |
413 } // namespace | 218 } // namespace |
414 } // namespace content | 219 } // namespace content |
OLD | NEW |