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