| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "net/websockets/websocket_handshake.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/string_split.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/stringprintf.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "testing/platform_test.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 class WebSocketHandshakeTest : public testing::Test { | |
| 21 public: | |
| 22 static void SetUpParameter(WebSocketHandshake* handshake, | |
| 23 uint32 number_1, uint32 number_2, | |
| 24 const std::string& key_1, const std::string& key_2, | |
| 25 const std::string& key_3) { | |
| 26 WebSocketHandshake::Parameter* parameter = | |
| 27 new WebSocketHandshake::Parameter; | |
| 28 parameter->number_1_ = number_1; | |
| 29 parameter->number_2_ = number_2; | |
| 30 parameter->key_1_ = key_1; | |
| 31 parameter->key_2_ = key_2; | |
| 32 parameter->key_3_ = key_3; | |
| 33 handshake->parameter_.reset(parameter); | |
| 34 } | |
| 35 | |
| 36 static void ExpectHeaderEquals(const std::string& expected, | |
| 37 const std::string& actual) { | |
| 38 std::vector<std::string> expected_lines; | |
| 39 Tokenize(expected, "\r\n", &expected_lines); | |
| 40 std::vector<std::string> actual_lines; | |
| 41 Tokenize(actual, "\r\n", &actual_lines); | |
| 42 // Request lines. | |
| 43 EXPECT_EQ(expected_lines[0], actual_lines[0]); | |
| 44 | |
| 45 std::vector<std::string> expected_headers; | |
| 46 for (size_t i = 1; i < expected_lines.size(); i++) { | |
| 47 // Finish at first CRLF CRLF. Note that /key_3/ might include CRLF. | |
| 48 if (expected_lines[i] == "") | |
| 49 break; | |
| 50 expected_headers.push_back(expected_lines[i]); | |
| 51 } | |
| 52 sort(expected_headers.begin(), expected_headers.end()); | |
| 53 | |
| 54 std::vector<std::string> actual_headers; | |
| 55 for (size_t i = 1; i < actual_lines.size(); i++) { | |
| 56 // Finish at first CRLF CRLF. Note that /key_3/ might include CRLF. | |
| 57 if (actual_lines[i] == "") | |
| 58 break; | |
| 59 actual_headers.push_back(actual_lines[i]); | |
| 60 } | |
| 61 sort(actual_headers.begin(), actual_headers.end()); | |
| 62 | |
| 63 EXPECT_EQ(expected_headers.size(), actual_headers.size()) | |
| 64 << "expected:" << expected | |
| 65 << "\nactual:" << actual; | |
| 66 for (size_t i = 0; i < expected_headers.size(); i++) { | |
| 67 EXPECT_EQ(expected_headers[i], actual_headers[i]); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 static void ExpectHandshakeMessageEquals(const std::string& expected, | |
| 72 const std::string& actual) { | |
| 73 // Headers. | |
| 74 ExpectHeaderEquals(expected, actual); | |
| 75 // Compare tailing \r\n\r\n<key3> (4 + 8 bytes). | |
| 76 ASSERT_GT(expected.size(), 12U); | |
| 77 const char* expected_key3 = expected.data() + expected.size() - 12; | |
| 78 EXPECT_GT(actual.size(), 12U); | |
| 79 if (actual.size() <= 12U) | |
| 80 return; | |
| 81 const char* actual_key3 = actual.data() + actual.size() - 12; | |
| 82 EXPECT_TRUE(memcmp(expected_key3, actual_key3, 12) == 0) | |
| 83 << "expected_key3:" << DumpKey(expected_key3, 12) | |
| 84 << ", actual_key3:" << DumpKey(actual_key3, 12); | |
| 85 } | |
| 86 | |
| 87 static std::string DumpKey(const char* buf, int len) { | |
| 88 std::string s; | |
| 89 for (int i = 0; i < len; i++) { | |
| 90 if (isprint(buf[i])) | |
| 91 s += base::StringPrintf("%c", buf[i]); | |
| 92 else | |
| 93 s += base::StringPrintf("\\x%02x", buf[i]); | |
| 94 } | |
| 95 return s; | |
| 96 } | |
| 97 | |
| 98 static std::string GetResourceName(WebSocketHandshake* handshake) { | |
| 99 return handshake->GetResourceName(); | |
| 100 } | |
| 101 static std::string GetHostFieldValue(WebSocketHandshake* handshake) { | |
| 102 return handshake->GetHostFieldValue(); | |
| 103 } | |
| 104 static std::string GetOriginFieldValue(WebSocketHandshake* handshake) { | |
| 105 return handshake->GetOriginFieldValue(); | |
| 106 } | |
| 107 }; | |
| 108 | |
| 109 | |
| 110 TEST_F(WebSocketHandshakeTest, Connect) { | |
| 111 const std::string kExpectedClientHandshakeMessage = | |
| 112 "GET /demo HTTP/1.1\r\n" | |
| 113 "Upgrade: WebSocket\r\n" | |
| 114 "Connection: Upgrade\r\n" | |
| 115 "Host: example.com\r\n" | |
| 116 "Origin: http://example.com\r\n" | |
| 117 "Sec-WebSocket-Protocol: sample\r\n" | |
| 118 "Sec-WebSocket-Key1: 388P O503D&ul7 {K%gX( %7 15\r\n" | |
| 119 "Sec-WebSocket-Key2: 1 N ?|k UT0or 3o 4 I97N 5-S3O 31\r\n" | |
| 120 "\r\n" | |
| 121 "\x47\x30\x22\x2D\x5A\x3F\x47\x58"; | |
| 122 | |
| 123 scoped_ptr<WebSocketHandshake> handshake( | |
| 124 new WebSocketHandshake(GURL("ws://example.com/demo"), | |
| 125 "http://example.com", | |
| 126 "ws://example.com/demo", | |
| 127 "sample")); | |
| 128 SetUpParameter(handshake.get(), 777007543U, 114997259U, | |
| 129 "388P O503D&ul7 {K%gX( %7 15", | |
| 130 "1 N ?|k UT0or 3o 4 I97N 5-S3O 31", | |
| 131 std::string("\x47\x30\x22\x2D\x5A\x3F\x47\x58", 8)); | |
| 132 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 133 ExpectHandshakeMessageEquals( | |
| 134 kExpectedClientHandshakeMessage, | |
| 135 handshake->CreateClientHandshakeMessage()); | |
| 136 | |
| 137 const char kResponse[] = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" | |
| 138 "Upgrade: WebSocket\r\n" | |
| 139 "Connection: Upgrade\r\n" | |
| 140 "Sec-WebSocket-Origin: http://example.com\r\n" | |
| 141 "Sec-WebSocket-Location: ws://example.com/demo\r\n" | |
| 142 "Sec-WebSocket-Protocol: sample\r\n" | |
| 143 "\r\n" | |
| 144 "\x30\x73\x74\x33\x52\x6C\x26\x71\x2D\x32\x5A\x55\x5E\x77\x65\x75"; | |
| 145 std::vector<std::string> response_lines; | |
| 146 base::SplitStringDontTrim(kResponse, '\n', &response_lines); | |
| 147 | |
| 148 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 149 // too short | |
| 150 EXPECT_EQ(-1, handshake->ReadServerHandshake(kResponse, 16)); | |
| 151 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 152 | |
| 153 // only status line | |
| 154 std::string response = response_lines[0]; | |
| 155 EXPECT_EQ(-1, handshake->ReadServerHandshake( | |
| 156 response.data(), response.size())); | |
| 157 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 158 // by upgrade header | |
| 159 response += response_lines[1]; | |
| 160 EXPECT_EQ(-1, handshake->ReadServerHandshake( | |
| 161 response.data(), response.size())); | |
| 162 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 163 // by connection header | |
| 164 response += response_lines[2]; | |
| 165 EXPECT_EQ(-1, handshake->ReadServerHandshake( | |
| 166 response.data(), response.size())); | |
| 167 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 168 | |
| 169 response += response_lines[3]; // Sec-WebSocket-Origin | |
| 170 response += response_lines[4]; // Sec-WebSocket-Location | |
| 171 response += response_lines[5]; // Sec-WebSocket-Protocol | |
| 172 EXPECT_EQ(-1, handshake->ReadServerHandshake( | |
| 173 response.data(), response.size())); | |
| 174 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 175 | |
| 176 response += response_lines[6]; // \r\n | |
| 177 EXPECT_EQ(-1, handshake->ReadServerHandshake( | |
| 178 response.data(), response.size())); | |
| 179 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 180 | |
| 181 int handshake_length = sizeof(kResponse) - 1; // -1 for terminating \0 | |
| 182 EXPECT_EQ(handshake_length, handshake->ReadServerHandshake( | |
| 183 kResponse, handshake_length)); // -1 for terminating \0 | |
| 184 EXPECT_EQ(WebSocketHandshake::MODE_CONNECTED, handshake->mode()); | |
| 185 } | |
| 186 | |
| 187 TEST_F(WebSocketHandshakeTest, ServerSentData) { | |
| 188 const std::string kExpectedClientHandshakeMessage = | |
| 189 "GET /demo HTTP/1.1\r\n" | |
| 190 "Upgrade: WebSocket\r\n" | |
| 191 "Connection: Upgrade\r\n" | |
| 192 "Host: example.com\r\n" | |
| 193 "Origin: http://example.com\r\n" | |
| 194 "Sec-WebSocket-Protocol: sample\r\n" | |
| 195 "Sec-WebSocket-Key1: 388P O503D&ul7 {K%gX( %7 15\r\n" | |
| 196 "Sec-WebSocket-Key2: 1 N ?|k UT0or 3o 4 I97N 5-S3O 31\r\n" | |
| 197 "\r\n" | |
| 198 "\x47\x30\x22\x2D\x5A\x3F\x47\x58"; | |
| 199 scoped_ptr<WebSocketHandshake> handshake( | |
| 200 new WebSocketHandshake(GURL("ws://example.com/demo"), | |
| 201 "http://example.com", | |
| 202 "ws://example.com/demo", | |
| 203 "sample")); | |
| 204 SetUpParameter(handshake.get(), 777007543U, 114997259U, | |
| 205 "388P O503D&ul7 {K%gX( %7 15", | |
| 206 "1 N ?|k UT0or 3o 4 I97N 5-S3O 31", | |
| 207 std::string("\x47\x30\x22\x2D\x5A\x3F\x47\x58", 8)); | |
| 208 EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode()); | |
| 209 ExpectHandshakeMessageEquals( | |
| 210 kExpectedClientHandshakeMessage, | |
| 211 handshake->CreateClientHandshakeMessage()); | |
| 212 | |
| 213 const char kResponse[] = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" | |
| 214 "Upgrade: WebSocket\r\n" | |
| 215 "Connection: Upgrade\r\n" | |
| 216 "Sec-WebSocket-Origin: http://example.com\r\n" | |
| 217 "Sec-WebSocket-Location: ws://example.com/demo\r\n" | |
| 218 "Sec-WebSocket-Protocol: sample\r\n" | |
| 219 "\r\n" | |
| 220 "\x30\x73\x74\x33\x52\x6C\x26\x71\x2D\x32\x5A\x55\x5E\x77\x65\x75" | |
| 221 "\0Hello\xff"; | |
| 222 | |
| 223 int handshake_length = strlen(kResponse); // key3 doesn't contain \0. | |
| 224 EXPECT_EQ(handshake_length, handshake->ReadServerHandshake( | |
| 225 kResponse, sizeof(kResponse) - 1)); // -1 for terminating \0 | |
| 226 EXPECT_EQ(WebSocketHandshake::MODE_CONNECTED, handshake->mode()); | |
| 227 } | |
| 228 | |
| 229 TEST_F(WebSocketHandshakeTest, is_secure_false) { | |
| 230 scoped_ptr<WebSocketHandshake> handshake( | |
| 231 new WebSocketHandshake(GURL("ws://example.com/demo"), | |
| 232 "http://example.com", | |
| 233 "ws://example.com/demo", | |
| 234 "sample")); | |
| 235 EXPECT_FALSE(handshake->is_secure()); | |
| 236 } | |
| 237 | |
| 238 TEST_F(WebSocketHandshakeTest, is_secure_true) { | |
| 239 // wss:// is secure. | |
| 240 scoped_ptr<WebSocketHandshake> handshake( | |
| 241 new WebSocketHandshake(GURL("wss://example.com/demo"), | |
| 242 "http://example.com", | |
| 243 "wss://example.com/demo", | |
| 244 "sample")); | |
| 245 EXPECT_TRUE(handshake->is_secure()); | |
| 246 } | |
| 247 | |
| 248 TEST_F(WebSocketHandshakeTest, CreateClientHandshakeMessage_ResourceName) { | |
| 249 scoped_ptr<WebSocketHandshake> handshake( | |
| 250 new WebSocketHandshake(GURL("ws://example.com/Test?q=xxx&p=%20"), | |
| 251 "http://example.com", | |
| 252 "ws://example.com/demo", | |
| 253 "sample")); | |
| 254 // Path and query should be preserved as-is. | |
| 255 EXPECT_EQ("/Test?q=xxx&p=%20", GetResourceName(handshake.get())); | |
| 256 } | |
| 257 | |
| 258 TEST_F(WebSocketHandshakeTest, CreateClientHandshakeMessage_Host) { | |
| 259 scoped_ptr<WebSocketHandshake> handshake( | |
| 260 new WebSocketHandshake(GURL("ws://Example.Com/demo"), | |
| 261 "http://Example.Com", | |
| 262 "ws://Example.Com/demo", | |
| 263 "sample")); | |
| 264 // Host should be lowercased | |
| 265 EXPECT_EQ("example.com", GetHostFieldValue(handshake.get())); | |
| 266 EXPECT_EQ("http://example.com", GetOriginFieldValue(handshake.get())); | |
| 267 } | |
| 268 | |
| 269 TEST_F(WebSocketHandshakeTest, CreateClientHandshakeMessage_TrimPort80) { | |
| 270 scoped_ptr<WebSocketHandshake> handshake( | |
| 271 new WebSocketHandshake(GURL("ws://example.com:80/demo"), | |
| 272 "http://example.com", | |
| 273 "ws://example.com/demo", | |
| 274 "sample")); | |
| 275 // :80 should be trimmed as it's the default port for ws://. | |
| 276 EXPECT_EQ("example.com", GetHostFieldValue(handshake.get())); | |
| 277 } | |
| 278 | |
| 279 TEST_F(WebSocketHandshakeTest, CreateClientHandshakeMessage_TrimPort443) { | |
| 280 scoped_ptr<WebSocketHandshake> handshake( | |
| 281 new WebSocketHandshake(GURL("wss://example.com:443/demo"), | |
| 282 "http://example.com", | |
| 283 "wss://example.com/demo", | |
| 284 "sample")); | |
| 285 // :443 should be trimmed as it's the default port for wss://. | |
| 286 EXPECT_EQ("example.com", GetHostFieldValue(handshake.get())); | |
| 287 } | |
| 288 | |
| 289 TEST_F(WebSocketHandshakeTest, | |
| 290 CreateClientHandshakeMessage_NonDefaultPortForWs) { | |
| 291 scoped_ptr<WebSocketHandshake> handshake( | |
| 292 new WebSocketHandshake(GURL("ws://example.com:8080/demo"), | |
| 293 "http://example.com", | |
| 294 "wss://example.com/demo", | |
| 295 "sample")); | |
| 296 // :8080 should be preserved as it's not the default port for ws://. | |
| 297 EXPECT_EQ("example.com:8080", GetHostFieldValue(handshake.get())); | |
| 298 } | |
| 299 | |
| 300 TEST_F(WebSocketHandshakeTest, | |
| 301 CreateClientHandshakeMessage_NonDefaultPortForWss) { | |
| 302 scoped_ptr<WebSocketHandshake> handshake( | |
| 303 new WebSocketHandshake(GURL("wss://example.com:4443/demo"), | |
| 304 "http://example.com", | |
| 305 "wss://example.com/demo", | |
| 306 "sample")); | |
| 307 // :4443 should be preserved as it's not the default port for wss://. | |
| 308 EXPECT_EQ("example.com:4443", GetHostFieldValue(handshake.get())); | |
| 309 } | |
| 310 | |
| 311 TEST_F(WebSocketHandshakeTest, CreateClientHandshakeMessage_WsBut443) { | |
| 312 scoped_ptr<WebSocketHandshake> handshake( | |
| 313 new WebSocketHandshake(GURL("ws://example.com:443/demo"), | |
| 314 "http://example.com", | |
| 315 "ws://example.com/demo", | |
| 316 "sample")); | |
| 317 // :443 should be preserved as it's not the default port for ws://. | |
| 318 EXPECT_EQ("example.com:443", GetHostFieldValue(handshake.get())); | |
| 319 } | |
| 320 | |
| 321 TEST_F(WebSocketHandshakeTest, CreateClientHandshakeMessage_WssBut80) { | |
| 322 scoped_ptr<WebSocketHandshake> handshake( | |
| 323 new WebSocketHandshake(GURL("wss://example.com:80/demo"), | |
| 324 "http://example.com", | |
| 325 "wss://example.com/demo", | |
| 326 "sample")); | |
| 327 // :80 should be preserved as it's not the default port for wss://. | |
| 328 EXPECT_EQ("example.com:80", GetHostFieldValue(handshake.get())); | |
| 329 } | |
| 330 | |
| 331 } // namespace net | |
| OLD | NEW |