Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "remoting/host/websocket_connection.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "net/base/ip_endpoint.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/base/net_util.h" | |
| 12 #include "net/socket/tcp_client_socket.h" | |
| 13 #include "remoting/base/socket_reader.h" | |
| 14 #include "remoting/host/websocket_listener.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace remoting { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int kPortRangeMin = 12800; | |
| 23 const int kPortRangeMax = 12810; | |
| 24 const char kHeaderEndMarker[] = "\r\n\r\n"; | |
| 25 | |
| 26 const char kHelloMessage[] = { 0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, | |
| 27 0x9f, 0x4d, 0x51, 0x58 }; | |
| 28 const char kHelloText[] = "Hello"; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class WebsocketTestReader { | |
| 33 public: | |
| 34 WebsocketTestReader(net::Socket* socket, const base::Closure& on_data) | |
| 35 : on_data_(on_data), | |
| 36 closed_(false), | |
| 37 reading_header_(true) { | |
| 38 reader_.Init(socket, base::Bind(&WebsocketTestReader::OnReadResult, | |
| 39 base::Unretained(this))); | |
| 40 } | |
| 41 ~WebsocketTestReader() { | |
| 42 } | |
| 43 | |
| 44 bool closed() { return closed_; } | |
| 45 | |
| 46 bool reading_header() { return reading_header_; } | |
| 47 const std::string& header() { return header_; } | |
| 48 const std::string& data_receved() { return data_receved_; } | |
| 49 | |
| 50 protected: | |
| 51 void OnReadResult(scoped_refptr<net::IOBuffer> buffer, int result) { | |
| 52 if (result <= 0) { | |
| 53 closed_ = true; | |
| 54 } else if (reading_header_) { | |
| 55 header_.append(buffer->data(), buffer->data() + result); | |
| 56 size_t end_pos = header_.find(kHeaderEndMarker); | |
| 57 if (end_pos != std::string::npos) { | |
| 58 data_receved_ = header_.substr(end_pos + strlen(kHeaderEndMarker)); | |
| 59 header_ = header_.substr(0, end_pos); | |
| 60 reading_header_ = false; | |
| 61 } | |
| 62 } else { | |
| 63 data_receved_.append(buffer->data(), buffer->data() + result); | |
| 64 } | |
| 65 on_data_.Run(); | |
| 66 } | |
| 67 | |
| 68 private: | |
| 69 SocketReader reader_; | |
| 70 base::Closure on_data_; | |
| 71 bool closed_; | |
| 72 bool reading_header_; | |
| 73 std::string header_; | |
| 74 std::string data_receved_; | |
| 75 }; | |
| 76 | |
| 77 class WebsocketConnectionTest : public testing::Test, | |
| 78 public WebsocketConnection::Delegate { | |
| 79 public: | |
| 80 virtual void OnWebsocketMessage(const std::string& message) OVERRIDE { | |
| 81 last_message_ = message; | |
| 82 if (message_run_loop_.get()) { | |
| 83 message_run_loop_->Quit(); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 virtual void OnWebsocketClosed() OVERRIDE { | |
| 88 connection_.reset(); | |
| 89 if (closed_run_loop_.get()) { | |
| 90 closed_run_loop_->Quit(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 protected: | |
| 95 void Initialize() { | |
| 96 listener_.reset(new WebsocketListener()); | |
| 97 net::IPAddressNumber localhost; | |
| 98 ASSERT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &localhost)); | |
| 99 for (int port = kPortRangeMin; port < kPortRangeMax; ++port) { | |
| 100 endpoint_ = net::IPEndPoint(localhost, port); | |
| 101 if (listener_->Listen( | |
| 102 endpoint_, base::Bind(&WebsocketConnectionTest::OnNewConnection, | |
|
Wez
2012/11/20 05:44:09
nit: Indentation.
Sergey Ulanov
2012/11/21 01:40:24
Done.
| |
| 103 base::Unretained(this))) == net::OK) { | |
| 104 return; | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void OnNewConnection(scoped_ptr<WebsocketConnection> connection) { | |
| 110 EXPECT_TRUE(connection_.get() == NULL); | |
| 111 connection_ = connection.Pass(); | |
| 112 connection_->Accept(this); | |
| 113 } | |
| 114 | |
| 115 void ConnectSocket() { | |
| 116 client_.reset(new net::TCPClientSocket( | |
| 117 net::AddressList(endpoint_), NULL, net::NetLog::Source())); | |
| 118 client_connect_result_ = -1; | |
| 119 client_->Connect(base::Bind(&WebsocketConnectionTest::OnClientConnected, | |
| 120 base::Unretained(this))); | |
| 121 connect_run_loop_.reset(new base::RunLoop()); | |
| 122 connect_run_loop_->Run(); | |
| 123 ASSERT_EQ(client_connect_result_, 0); | |
| 124 client_writer_.reset(new protocol::BufferedSocketWriter()); | |
| 125 client_writer_->Init( | |
| 126 client_.get(), protocol::BufferedSocketWriter::WriteFailedCallback()); | |
| 127 client_reader_.reset(new WebsocketTestReader( | |
| 128 client_.get(), | |
| 129 base::Bind(&WebsocketConnectionTest::OnClientDataReceived, | |
| 130 base::Unretained(this)))); | |
| 131 } | |
| 132 | |
| 133 void OnClientConnected(int result) { | |
| 134 client_connect_result_ = result; | |
| 135 connect_run_loop_->Quit(); | |
| 136 } | |
| 137 | |
| 138 void OnClientDataReceived() { | |
| 139 if (handshake_run_loop_.get() && !client_reader_->reading_header()) { | |
| 140 handshake_run_loop_->Quit(); | |
| 141 } | |
| 142 if (closed_run_loop_.get() && client_reader_->closed()) { | |
| 143 closed_run_loop_->Quit(); | |
| 144 } | |
| 145 if (data_received_run_loop_.get()) { | |
| 146 data_received_run_loop_->Quit(); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void Send(const std::string& data) { | |
| 151 scoped_refptr<net::IOBufferWithSize> buffer = | |
| 152 new net::IOBufferWithSize(data.size()); | |
| 153 memcpy(buffer->data(), data.data(), data.size()); | |
| 154 client_writer_->Write(buffer, base::Closure()); | |
| 155 } | |
| 156 | |
| 157 void Handshake() { | |
| 158 Send("GET /chat HTTP/1.1\r\n" | |
| 159 "Host: server.example.com\r\n" | |
| 160 "Upgrade: websocket\r\n" | |
| 161 "Connection: Upgrade\r\n" | |
| 162 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" | |
| 163 "Origin: http://example.com\r\n" | |
| 164 "Sec-WebSocket-Version: 13\r\n\r\n"); | |
| 165 handshake_run_loop_.reset(new base::RunLoop()); | |
| 166 handshake_run_loop_->Run(); | |
| 167 EXPECT_EQ("http://example.com", connection_->origin()); | |
| 168 EXPECT_EQ("server.example.com", connection_->request_host()); | |
| 169 EXPECT_EQ("/chat", connection_->request_path()); | |
| 170 EXPECT_EQ("HTTP/1.1 101 Switching Protocol\r\n" | |
| 171 "Upgrade: websocket\r\n" | |
| 172 "Connection: Upgrade\r\n" | |
| 173 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", | |
| 174 client_reader_->header()); | |
| 175 } | |
| 176 | |
| 177 void ReceiveFrame(std::string expected_frame) { | |
| 178 while (client_reader_->data_receved().size() < expected_frame.size()) { | |
| 179 data_received_run_loop_.reset(new base::RunLoop()); | |
| 180 data_received_run_loop_->Run(); | |
| 181 } | |
| 182 EXPECT_EQ(expected_frame, client_reader_->data_receved()); | |
| 183 } | |
| 184 | |
| 185 MessageLoopForIO message_loop_; | |
| 186 | |
| 187 scoped_ptr<base::RunLoop> connect_run_loop_; | |
| 188 scoped_ptr<base::RunLoop> handshake_run_loop_; | |
| 189 scoped_ptr<base::RunLoop> closed_run_loop_; | |
| 190 scoped_ptr<base::RunLoop> data_received_run_loop_; | |
| 191 scoped_ptr<base::RunLoop> message_run_loop_; | |
| 192 | |
| 193 scoped_ptr<WebsocketListener> listener_; | |
| 194 net::IPEndPoint endpoint_; | |
| 195 scoped_ptr<WebsocketConnection> connection_; | |
| 196 scoped_ptr<net::TCPClientSocket> client_; | |
| 197 scoped_ptr<protocol::BufferedSocketWriter> client_writer_; | |
| 198 scoped_ptr<WebsocketTestReader> client_reader_; | |
| 199 int client_connect_result_; | |
| 200 std::string last_message_; | |
| 201 }; | |
| 202 | |
| 203 TEST_F(WebsocketConnectionTest, ConnectSocket) { | |
| 204 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 205 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
|
Wez
2012/11/20 05:44:09
Why are these ASSERTs rather than EXPECTs?
Sergey Ulanov
2012/11/21 01:40:24
Because we don't want to call ConnectSocket() if I
| |
| 206 } | |
| 207 | |
| 208 TEST_F(WebsocketConnectionTest, SuccessfulHandshake) { | |
| 209 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 210 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 211 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 212 ASSERT_TRUE(connection_.get() != NULL); | |
| 213 } | |
| 214 | |
| 215 TEST_F(WebsocketConnectionTest, DisconnectAndConnect) { | |
|
Wez
2012/11/20 05:44:09
nit: ConnectAndReconnect?
Sergey Ulanov
2012/11/21 01:40:24
Done.
| |
| 216 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 217 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 218 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 219 ASSERT_TRUE(connection_.get() != NULL); | |
| 220 | |
| 221 client_.reset(); | |
| 222 closed_run_loop_.reset(new base::RunLoop()); | |
| 223 closed_run_loop_->Run(); | |
| 224 EXPECT_TRUE(connection_.get() == NULL); | |
|
Wez
2012/11/20 05:44:09
Why is this EXPECT, not ASSERT as the rest of thes
Sergey Ulanov
2012/11/21 01:40:24
ASSERT is preferred when failed condition may cras
| |
| 225 | |
| 226 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 227 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 228 ASSERT_TRUE(connection_.get() != NULL); | |
| 229 } | |
| 230 | |
| 231 TEST_F(WebsocketConnectionTest, CloseFrame) { | |
|
Wez
2012/11/20 05:44:09
nit: CloseFrameIsSent
Sergey Ulanov
2012/11/21 01:40:24
Done.
| |
| 232 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 233 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 234 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 235 | |
| 236 connection_->Close(); | |
| 237 | |
| 238 // Expect to receive Close frame. | |
| 239 char expected_frame[] = { 0x88, 0x00 }; | |
| 240 ReceiveFrame(std::string(expected_frame, | |
| 241 expected_frame + sizeof(expected_frame))); | |
| 242 } | |
| 243 | |
| 244 TEST_F(WebsocketConnectionTest, NonWebsocketHeader) { | |
|
Wez
2012/11/20 05:44:09
nit: ConnectFailsOnNonWebSocketHeader?
Sergey Ulanov
2012/11/21 01:40:24
Done.
| |
| 245 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 246 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 247 EXPECT_TRUE(connection_.get() == NULL); | |
| 248 Send("GET /chat HTTP/1.1\r\n" | |
| 249 "Host: server.example.com\r\n\r\n"); | |
| 250 closed_run_loop_.reset(new base::RunLoop()); | |
| 251 closed_run_loop_->Run(); | |
| 252 EXPECT_TRUE(connection_.get() == NULL); | |
| 253 } | |
| 254 | |
| 255 TEST_F(WebsocketConnectionTest, SendMessage) { | |
| 256 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 257 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 258 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 259 connection_->SendText(kHelloText); | |
| 260 char expected_frame[] = { 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f }; | |
| 261 ReceiveFrame(std::string(expected_frame, | |
| 262 expected_frame + sizeof(expected_frame))); | |
| 263 } | |
| 264 | |
| 265 TEST_F(WebsocketConnectionTest, ReceiveMessage) { | |
| 266 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 267 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 268 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 269 char kHelloMessage[] = { 0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, | |
| 270 0x9f, 0x4d, 0x51, 0x58 }; | |
| 271 Send(std::string(kHelloMessage, kHelloMessage + sizeof(kHelloMessage))); | |
| 272 | |
| 273 message_run_loop_.reset(new base::RunLoop()); | |
| 274 message_run_loop_->Run(); | |
| 275 | |
| 276 EXPECT_EQ(kHelloText, last_message_); | |
| 277 } | |
| 278 | |
| 279 TEST_F(WebsocketConnectionTest, FragmentedMessage) { | |
| 280 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 281 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 282 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 283 | |
| 284 char fragment1[] = { 0x01, 0x83, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d }; | |
| 285 Send(std::string(fragment1, fragment1 + sizeof(fragment1))); | |
| 286 char fragment2[] = { 0x80, 0x82, 0x3d, 0x37, 0x12, 0x42, 0x51, 0x58 }; | |
| 287 Send(std::string(fragment2, fragment2 + sizeof(fragment2))); | |
| 288 | |
| 289 message_run_loop_.reset(new base::RunLoop()); | |
| 290 message_run_loop_->Run(); | |
| 291 | |
| 292 EXPECT_EQ(kHelloText, last_message_); | |
| 293 } | |
| 294 | |
| 295 TEST_F(WebsocketConnectionTest, PingResponse) { | |
| 296 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 297 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 298 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 299 | |
| 300 char ping_frame[] = { 0x89, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, | |
| 301 0x4d, 0x51, 0x58 }; | |
| 302 Send(std::string(ping_frame, ping_frame + sizeof(ping_frame))); | |
| 303 | |
| 304 char expected_frame[] = { 0x8a, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f }; | |
| 305 ReceiveFrame(std::string(expected_frame, | |
| 306 expected_frame + sizeof(expected_frame))); | |
| 307 } | |
| 308 | |
| 309 TEST_F(WebsocketConnectionTest, MessageSizeLimit) { | |
| 310 ASSERT_NO_FATAL_FAILURE(Initialize()); | |
| 311 ASSERT_NO_FATAL_FAILURE(ConnectSocket()); | |
| 312 ASSERT_NO_FATAL_FAILURE(Handshake()); | |
| 313 | |
| 314 connection_->set_maximum_message_size(strlen(kHelloText)); | |
| 315 Send(std::string(kHelloMessage, kHelloMessage + sizeof(kHelloMessage))); | |
| 316 | |
| 317 message_run_loop_.reset(new base::RunLoop()); | |
| 318 message_run_loop_->Run(); | |
| 319 | |
| 320 EXPECT_EQ(kHelloText, last_message_); | |
| 321 | |
| 322 // Set lower message size limit and try sending the same message again. | |
| 323 connection_->set_maximum_message_size(strlen(kHelloText) - 1); | |
| 324 | |
| 325 Send(std::string(kHelloMessage, kHelloMessage + sizeof(kHelloMessage))); | |
| 326 | |
| 327 // Expect to receive Close frame. | |
| 328 char expected_frame[] = { 0x88, 0x00 }; | |
| 329 ReceiveFrame(std::string(expected_frame, | |
| 330 expected_frame + sizeof(expected_frame))); | |
| 331 } | |
| 332 | |
| 333 } // namespace remoting | |
| OLD | NEW |