OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/memory/ref_counted.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" | |
16 #include "content/common/websocket.h" | |
17 #include "content/common/websocket_messages.h" | |
18 #include "ipc/ipc_message.h" | |
19 #include "net/websockets/websocket_errors.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 #include "url/gurl.h" | |
22 #include "url/origin.h" | |
23 | |
24 namespace content { | |
25 namespace { | |
26 | |
27 // This number is unlikely to occur by chance. | |
28 static const int kMagicRenderProcessId = 506116062; | |
29 | |
30 class WebSocketDispatcherHostTest; | |
31 | |
32 // A mock of WebsocketHost which records received messages. | |
33 class MockWebSocketHost : public WebSocketHost { | |
34 public: | |
35 MockWebSocketHost(int routing_id, | |
36 WebSocketDispatcherHost* dispatcher, | |
37 net::URLRequestContext* url_request_context, | |
38 base::TimeDelta delay, | |
39 WebSocketDispatcherHostTest* owner); | |
40 | |
41 ~MockWebSocketHost() override {} | |
42 | |
43 bool OnMessageReceived(const IPC::Message& message) override { | |
44 received_messages_.push_back(message); | |
45 switch (message.type()) { | |
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 } | |
57 | |
58 void GoAway() override; | |
59 | |
60 std::vector<IPC::Message> received_messages_; | |
61 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 }; | |
89 | |
90 class WebSocketDispatcherHostTest : public ::testing::Test { | |
91 public: | |
92 WebSocketDispatcherHostTest() | |
93 : next_routing_id_(123), | |
94 weak_ptr_factory_(this) { | |
95 dispatcher_host_ = new TestingWebSocketDispatcherHost( | |
96 kMagicRenderProcessId, | |
97 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | |
98 base::Unretained(this)), | |
99 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | |
100 base::Unretained(this))); | |
101 } | |
102 | |
103 ~WebSocketDispatcherHostTest() override { | |
104 // We need to invalidate the issued WeakPtrs at the beginning of the | |
105 // destructor in order not to access destructed member variables. | |
106 weak_ptr_factory_.InvalidateWeakPtrs(); | |
107 } | |
108 | |
109 void GoAway(int routing_id) { | |
110 gone_hosts_.push_back(routing_id); | |
111 } | |
112 | |
113 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { | |
114 return weak_ptr_factory_.GetWeakPtr(); | |
115 } | |
116 | |
117 protected: | |
118 // Adds |n| connections. Returns true if succeeded. | |
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(GURL("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 "", | |
133 render_frame_id); | |
134 if (!dispatcher_host_->OnMessageReceived(message)) | |
135 return false; | |
136 } | |
137 | |
138 return true; | |
139 } | |
140 | |
141 // Adds and cancels |n| connections. Returns true if succeeded. | |
142 bool AddAndCancelMultipleChannels(int number_of_channels) { | |
143 GURL socket_url("ws://example.com/test"); | |
144 std::vector<std::string> requested_protocols; | |
145 url::Origin origin(GURL("http://example.com")); | |
146 int render_frame_id = -3; | |
147 | |
148 for (int i = 0; i < number_of_channels; ++i) { | |
149 int routing_id = next_routing_id_++; | |
150 WebSocketHostMsg_AddChannelRequest messageAddChannelRequest( | |
151 routing_id, | |
152 socket_url, | |
153 requested_protocols, | |
154 origin, | |
155 "", | |
156 render_frame_id); | |
157 if (!dispatcher_host_->OnMessageReceived(messageAddChannelRequest)) | |
158 return false; | |
159 | |
160 WebSocketMsg_DropChannel messageDropChannel( | |
161 routing_id, false, net::kWebSocketErrorAbnormalClosure, ""); | |
162 if (!dispatcher_host_->OnMessageReceived(messageDropChannel)) | |
163 return false; | |
164 } | |
165 | |
166 return true; | |
167 } | |
168 | |
169 scoped_refptr<TestingWebSocketDispatcherHost> dispatcher_host_; | |
170 | |
171 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | |
172 // them. | |
173 std::vector<MockWebSocketHost*> mock_hosts_; | |
174 std::vector<int> gone_hosts_; | |
175 | |
176 private: | |
177 net::URLRequestContext* OnGetRequestContext() { | |
178 return NULL; | |
179 } | |
180 | |
181 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay) { | |
182 MockWebSocketHost* host = new MockWebSocketHost( | |
183 routing_id, dispatcher_host_.get(), NULL, delay, this); | |
184 mock_hosts_.push_back(host); | |
185 return host; | |
186 } | |
187 | |
188 base::MessageLoop message_loop_; | |
189 | |
190 int next_routing_id_; | |
191 | |
192 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; | |
193 }; | |
194 | |
195 MockWebSocketHost::MockWebSocketHost( | |
196 int routing_id, | |
197 WebSocketDispatcherHost* dispatcher, | |
198 net::URLRequestContext* url_request_context, | |
199 base::TimeDelta delay, | |
200 WebSocketDispatcherHostTest* owner) | |
201 : WebSocketHost(routing_id, dispatcher, url_request_context, delay), | |
202 owner_(owner->GetWeakPtr()), | |
203 delay_(delay) {} | |
204 | |
205 void MockWebSocketHost::GoAway() { | |
206 if (owner_) | |
207 owner_->GoAway(routing_id()); | |
208 } | |
209 | |
210 TEST_F(WebSocketDispatcherHostTest, Construct) { | |
211 // Do nothing. | |
212 } | |
213 | |
214 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | |
215 IPC::Message message; | |
216 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | |
217 } | |
218 | |
219 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | |
220 EXPECT_EQ(kMagicRenderProcessId, dispatcher_host_->render_process_id()); | |
221 } | |
222 | |
223 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) { | |
224 int routing_id = 123; | |
225 GURL socket_url("ws://example.com/test"); | |
226 std::vector<std::string> requested_protocols; | |
227 requested_protocols.push_back("hello"); | |
228 url::Origin origin(GURL("http://example.com")); | |
229 int render_frame_id = -2; | |
230 WebSocketHostMsg_AddChannelRequest message( | |
231 routing_id, socket_url, requested_protocols, origin, "", render_frame_id); | |
232 | |
233 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | |
234 | |
235 ASSERT_EQ(1U, mock_hosts_.size()); | |
236 MockWebSocketHost* host = mock_hosts_[0]; | |
237 | |
238 ASSERT_EQ(1U, host->received_messages_.size()); | |
239 const IPC::Message& forwarded_message = host->received_messages_[0]; | |
240 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | |
241 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | |
242 } | |
243 | |
244 TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) { | |
245 int routing_id = 123; | |
246 std::vector<char> data; | |
247 WebSocketMsg_SendFrame message( | |
248 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | |
249 | |
250 // Expected to be ignored. | |
251 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message)); | |
252 | |
253 EXPECT_EQ(0U, mock_hosts_.size()); | |
254 } | |
255 | |
256 TEST_F(WebSocketDispatcherHostTest, SendFrame) { | |
257 int routing_id = 123; | |
258 | |
259 GURL socket_url("ws://example.com/test"); | |
260 std::vector<std::string> requested_protocols; | |
261 requested_protocols.push_back("hello"); | |
262 url::Origin origin(GURL("http://example.com")); | |
263 int render_frame_id = -2; | |
264 WebSocketHostMsg_AddChannelRequest add_channel_message( | |
265 routing_id, socket_url, requested_protocols, origin, "", render_frame_id); | |
266 | |
267 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(add_channel_message)); | |
268 | |
269 std::vector<char> data; | |
270 WebSocketMsg_SendFrame send_frame_message( | |
271 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data); | |
272 | |
273 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(send_frame_message)); | |
274 | |
275 ASSERT_EQ(1U, mock_hosts_.size()); | |
276 MockWebSocketHost* host = mock_hosts_[0]; | |
277 | |
278 ASSERT_EQ(2U, host->received_messages_.size()); | |
279 { | |
280 const IPC::Message& forwarded_message = host->received_messages_[0]; | |
281 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | |
282 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | |
283 } | |
284 { | |
285 const IPC::Message& forwarded_message = host->received_messages_[1]; | |
286 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | |
287 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | |
288 } | |
289 } | |
290 | |
291 TEST_F(WebSocketDispatcherHostTest, Destruct) { | |
292 WebSocketHostMsg_AddChannelRequest message1( | |
293 123, GURL("ws://example.com/test"), std::vector<std::string>(), | |
294 url::Origin(GURL("http://example.com")), "", -1); | |
295 WebSocketHostMsg_AddChannelRequest message2( | |
296 456, GURL("ws://example.com/test2"), std::vector<std::string>(), | |
297 url::Origin(GURL("http://example.com")), "", -1); | |
298 | |
299 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); | |
300 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); | |
301 | |
302 ASSERT_EQ(2u, mock_hosts_.size()); | |
303 | |
304 mock_hosts_.clear(); | |
305 dispatcher_host_ = NULL; | |
306 | |
307 ASSERT_EQ(2u, gone_hosts_.size()); | |
308 // The gone_hosts_ ordering is not predictable because it depends on the | |
309 // hash_map ordering. | |
310 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | |
311 EXPECT_EQ(123, gone_hosts_[0]); | |
312 EXPECT_EQ(456, gone_hosts_[1]); | |
313 } | |
314 | |
315 TEST_F(WebSocketDispatcherHostTest, DelayFor4thPendingConnectionIsZero) { | |
316 ASSERT_TRUE(AddMultipleChannels(4)); | |
317 | |
318 EXPECT_EQ(4, dispatcher_host_->num_pending_connections()); | |
319 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
320 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
321 | |
322 ASSERT_EQ(4U, mock_hosts_.size()); | |
323 EXPECT_EQ(base::TimeDelta(), mock_hosts_[3]->delay_); | |
324 } | |
325 | |
326 TEST_F(WebSocketDispatcherHostTest, DelayFor8thPendingConnectionIsNonZero) { | |
327 ASSERT_TRUE(AddMultipleChannels(8)); | |
328 | |
329 EXPECT_EQ(8, dispatcher_host_->num_pending_connections()); | |
330 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
331 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
332 | |
333 ASSERT_EQ(8U, mock_hosts_.size()); | |
334 EXPECT_LT(base::TimeDelta(), mock_hosts_[7]->delay_); | |
335 } | |
336 | |
337 TEST_F(WebSocketDispatcherHostTest, DelayFor17thPendingConnection) { | |
338 ASSERT_TRUE(AddMultipleChannels(17)); | |
339 | |
340 EXPECT_EQ(17, dispatcher_host_->num_pending_connections()); | |
341 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
342 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
343 | |
344 ASSERT_EQ(17U, mock_hosts_.size()); | |
345 EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), mock_hosts_[16]->delay_); | |
346 EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), mock_hosts_[16]->delay_); | |
347 } | |
348 | |
349 // The 256th connection is rejected by per-renderer WebSocket throttling. | |
350 // This is not counted as a failure. | |
351 TEST_F(WebSocketDispatcherHostTest, Rejects256thPendingConnection) { | |
352 ASSERT_TRUE(AddMultipleChannels(256)); | |
353 | |
354 EXPECT_EQ(255, dispatcher_host_->num_pending_connections()); | |
355 EXPECT_EQ(0, dispatcher_host_->num_failed_connections()); | |
356 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
357 | |
358 ASSERT_EQ(255U, mock_hosts_.size()); | |
359 } | |
360 | |
361 TEST_F(WebSocketDispatcherHostTest, DelayIsZeroAfter3FailedConnections) { | |
362 ASSERT_TRUE(AddAndCancelMultipleChannels(3)); | |
363 | |
364 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
365 EXPECT_EQ(3, dispatcher_host_->num_failed_connections()); | |
366 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
367 | |
368 ASSERT_TRUE(AddMultipleChannels(1)); | |
369 | |
370 ASSERT_EQ(4U, mock_hosts_.size()); | |
371 EXPECT_EQ(base::TimeDelta(), mock_hosts_[3]->delay_); | |
372 } | |
373 | |
374 TEST_F(WebSocketDispatcherHostTest, DelayIsNonZeroAfter7FailedConnections) { | |
375 ASSERT_TRUE(AddAndCancelMultipleChannels(7)); | |
376 | |
377 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
378 EXPECT_EQ(7, dispatcher_host_->num_failed_connections()); | |
379 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
380 | |
381 ASSERT_TRUE(AddMultipleChannels(1)); | |
382 | |
383 ASSERT_EQ(8U, mock_hosts_.size()); | |
384 EXPECT_LT(base::TimeDelta(), mock_hosts_[7]->delay_); | |
385 } | |
386 | |
387 TEST_F(WebSocketDispatcherHostTest, DelayAfter16FailedConnections) { | |
388 ASSERT_TRUE(AddAndCancelMultipleChannels(16)); | |
389 | |
390 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
391 EXPECT_EQ(16, dispatcher_host_->num_failed_connections()); | |
392 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
393 | |
394 ASSERT_TRUE(AddMultipleChannels(1)); | |
395 | |
396 ASSERT_EQ(17U, mock_hosts_.size()); | |
397 EXPECT_LE(base::TimeDelta::FromMilliseconds(1000), mock_hosts_[16]->delay_); | |
398 EXPECT_GE(base::TimeDelta::FromMilliseconds(5000), mock_hosts_[16]->delay_); | |
399 } | |
400 | |
401 TEST_F(WebSocketDispatcherHostTest, NotRejectedAfter255FailedConnections) { | |
402 ASSERT_TRUE(AddAndCancelMultipleChannels(255)); | |
403 | |
404 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
405 EXPECT_EQ(255, dispatcher_host_->num_failed_connections()); | |
406 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
407 | |
408 ASSERT_TRUE(AddMultipleChannels(1)); | |
409 | |
410 EXPECT_EQ(1, dispatcher_host_->num_pending_connections()); | |
411 EXPECT_EQ(255, dispatcher_host_->num_failed_connections()); | |
412 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
413 } | |
414 | |
415 // This is a regression test for https://crrev.com/998173003/. | |
416 TEST_F(WebSocketDispatcherHostTest, InvalidScheme) { | |
417 int routing_id = 123; | |
418 GURL socket_url("http://example.com/test"); | |
419 std::vector<std::string> requested_protocols; | |
420 requested_protocols.push_back("hello"); | |
421 url::Origin origin(GURL("http://example.com")); | |
422 int render_frame_id = -2; | |
423 WebSocketHostMsg_AddChannelRequest message( | |
424 routing_id, socket_url, requested_protocols, origin, "", render_frame_id); | |
425 | |
426 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message)); | |
427 | |
428 ASSERT_EQ(1U, mock_hosts_.size()); | |
429 MockWebSocketHost* host = mock_hosts_[0]; | |
430 | |
431 // Tests that WebSocketHost::OnMessageReceived() doesn't cause a crash and | |
432 // the connection with an invalid scheme fails here. | |
433 // We call WebSocketHost::OnMessageReceived() here explicitly because | |
434 // MockWebSocketHost does not call WebSocketHost::OnMessageReceived() for | |
435 // WebSocketHostMsg_AddChannelRequest. | |
436 host->WebSocketHost::OnMessageReceived(message); | |
437 | |
438 EXPECT_EQ(0, dispatcher_host_->num_pending_connections()); | |
439 EXPECT_EQ(1, dispatcher_host_->num_failed_connections()); | |
440 EXPECT_EQ(0, dispatcher_host_->num_succeeded_connections()); | |
441 } | |
442 | |
443 } // namespace | |
444 } // namespace content | |
OLD | NEW |