yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 1 | // Copyright 2015 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 <string> |
| 6 | |
| 7 | #include "base/callback_forward.h" |
| 8 | #include "base/memory/weak_ptr.h" |
| 9 | #include "base/message_loop/message_loop.h" |
| 10 | #include "base/run_loop.h" |
| 11 | #include "base/strings/string_util.h" |
| 12 | #include "base/strings/stringprintf.h" |
| 13 | #include "base/thread_task_runner_handle.h" |
| 14 | #include "net/cookies/cookie_store.h" |
| 15 | #include "net/socket/socket_test_util.h" |
| 16 | #include "net/websockets/websocket_stream_create_test_base.h" |
| 17 | #include "net/websockets/websocket_test_util.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | #include "url/gurl.h" |
| 20 | |
| 21 | namespace net { |
| 22 | namespace { |
| 23 | |
| 24 | using ::testing::TestWithParam; |
| 25 | using ::testing::ValuesIn; |
| 26 | |
| 27 | const char kNoCookieHeader[] = ""; |
| 28 | |
| 29 | class TestBase : public WebSocketStreamCreateTestBase { |
| 30 | public: |
| 31 | void CreateAndConnect(const GURL& url, |
| 32 | const std::string& origin, |
| 33 | const std::string& cookie_header, |
| 34 | const std::string& response_body) { |
| 35 | // We assume cookie_header ends with CRLF if not empty, as |
| 36 | // WebSocketStandardRequestWithCookies requires. Use AddCRLFIfNotEmpty |
| 37 | // in a call site. |
brettw | a7ff1b29 | 2015-07-16 17:49:29 | [diff] [blame^] | 38 | CHECK(cookie_header.empty() || |
| 39 | base::EndsWith(cookie_header, "\r\n", base::CompareCase::SENSITIVE)); |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 40 | |
| 41 | url_request_context_host_.SetExpectations( |
| 42 | WebSocketStandardRequestWithCookies(url.path(), url.host(), origin, |
| 43 | cookie_header, std::string()), |
| 44 | response_body); |
| 45 | CreateAndConnectStream(url.spec(), NoSubProtocols(), origin, nullptr); |
| 46 | } |
| 47 | |
| 48 | std::string AddCRLFIfNotEmpty(const std::string& s) { |
| 49 | return s.empty() ? s : s + "\r\n"; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | struct ClientUseCookieParameter { |
| 54 | // The URL for the WebSocket connection. |
| 55 | const char* const url; |
| 56 | // The URL for the previously set cookies. |
| 57 | const char* const cookie_url; |
| 58 | // The previously set cookies contents. |
| 59 | const char* const cookie_line; |
| 60 | // The Cookie: HTTP header expected to appear in the WS request. An empty |
| 61 | // string means there is no Cookie: header. |
| 62 | const char* const cookie_header; |
| 63 | }; |
| 64 | |
| 65 | class WebSocketStreamClientUseCookieTest |
| 66 | : public TestBase, |
| 67 | public TestWithParam<ClientUseCookieParameter> { |
| 68 | public: |
| 69 | ~WebSocketStreamClientUseCookieTest() override { |
| 70 | // Permit any endpoint locks to be released. |
| 71 | stream_request_.reset(); |
| 72 | stream_.reset(); |
| 73 | base::RunLoop().RunUntilIdle(); |
| 74 | } |
| 75 | |
| 76 | static void SetCookieHelperFunction(const base::Closure& task, |
| 77 | base::WeakPtr<bool> weak_is_called, |
| 78 | base::WeakPtr<bool> weak_result, |
| 79 | bool success) { |
| 80 | *weak_is_called = true; |
| 81 | *weak_result = success; |
| 82 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | struct ServerSetCookieParameter { |
| 87 | // The URL for the WebSocket connection. |
| 88 | const char* const url; |
| 89 | // The URL used to query cookies after the response received. |
| 90 | const char* const cookie_url; |
| 91 | // The cookies expected to appear for |cookie_url| inquiry. |
| 92 | const char* const cookie_line; |
| 93 | // The Set-Cookie: HTTP header attached to the response. |
| 94 | const char* const cookie_header; |
| 95 | }; |
| 96 | |
| 97 | class WebSocketStreamServerSetCookieTest |
| 98 | : public TestBase, |
| 99 | public TestWithParam<ServerSetCookieParameter> { |
| 100 | public: |
| 101 | ~WebSocketStreamServerSetCookieTest() override { |
| 102 | // Permit any endpoint locks to be released. |
| 103 | stream_request_.reset(); |
| 104 | stream_.reset(); |
| 105 | base::RunLoop().RunUntilIdle(); |
| 106 | } |
| 107 | |
| 108 | static void GetCookiesHelperFunction(const base::Closure& task, |
| 109 | base::WeakPtr<bool> weak_is_called, |
| 110 | base::WeakPtr<std::string> weak_result, |
| 111 | const std::string& cookies) { |
| 112 | *weak_is_called = true; |
| 113 | *weak_result = cookies; |
| 114 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | TEST_P(WebSocketStreamClientUseCookieTest, ClientUseCookie) { |
| 119 | // For wss tests. |
| 120 | ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK)); |
| 121 | |
| 122 | CookieStore* store = |
| 123 | url_request_context_host_.GetURLRequestContext()->cookie_store(); |
| 124 | |
| 125 | const GURL url(GetParam().url); |
| 126 | const GURL cookie_url(GetParam().cookie_url); |
| 127 | const std::string origin("http://www.example.com"); |
| 128 | const std::string cookie_line(GetParam().cookie_line); |
| 129 | const std::string cookie_header(AddCRLFIfNotEmpty(GetParam().cookie_header)); |
| 130 | |
| 131 | bool is_called = false; |
| 132 | bool set_cookie_result = false; |
| 133 | base::WeakPtrFactory<bool> weak_is_called(&is_called); |
| 134 | base::WeakPtrFactory<bool> weak_set_cookie_result(&set_cookie_result); |
| 135 | |
| 136 | base::RunLoop run_loop; |
| 137 | store->SetCookieWithOptionsAsync( |
| 138 | cookie_url, cookie_line, CookieOptions(), |
| 139 | base::Bind(&SetCookieHelperFunction, run_loop.QuitClosure(), |
| 140 | weak_is_called.GetWeakPtr(), |
| 141 | weak_set_cookie_result.GetWeakPtr())); |
| 142 | run_loop.Run(); |
| 143 | ASSERT_TRUE(is_called); |
| 144 | ASSERT_TRUE(set_cookie_result); |
| 145 | |
| 146 | CreateAndConnect(url, origin, cookie_header, WebSocketStandardResponse("")); |
| 147 | WaitUntilConnectDone(); |
| 148 | EXPECT_FALSE(has_failed()); |
| 149 | } |
| 150 | |
| 151 | TEST_P(WebSocketStreamServerSetCookieTest, ServerSetCookie) { |
| 152 | // For wss tests. |
| 153 | ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK)); |
| 154 | |
| 155 | const GURL url(GetParam().url); |
| 156 | const GURL cookie_url(GetParam().cookie_url); |
| 157 | const std::string origin("http://www.example.com"); |
| 158 | const std::string cookie_line(GetParam().cookie_line); |
| 159 | const std::string cookie_header(AddCRLFIfNotEmpty(GetParam().cookie_header)); |
| 160 | |
| 161 | const std::string response = base::StringPrintf( |
| 162 | "HTTP/1.1 101 Switching Protocols\r\n" |
| 163 | "Upgrade: websocket\r\n" |
| 164 | "Connection: Upgrade\r\n" |
| 165 | "%s" |
| 166 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 167 | "\r\n", |
| 168 | cookie_header.c_str()); |
| 169 | |
| 170 | CookieStore* store = |
| 171 | url_request_context_host_.GetURLRequestContext()->cookie_store(); |
| 172 | |
| 173 | CreateAndConnect(url, origin, "", response); |
| 174 | WaitUntilConnectDone(); |
| 175 | EXPECT_FALSE(has_failed()); |
| 176 | |
| 177 | bool is_called = false; |
| 178 | std::string get_cookies_result; |
| 179 | base::WeakPtrFactory<bool> weak_is_called(&is_called); |
| 180 | base::WeakPtrFactory<std::string> weak_get_cookies_result( |
| 181 | &get_cookies_result); |
| 182 | base::RunLoop run_loop; |
| 183 | store->GetCookiesWithOptionsAsync( |
| 184 | cookie_url, CookieOptions(), |
| 185 | base::Bind(&GetCookiesHelperFunction, run_loop.QuitClosure(), |
| 186 | weak_is_called.GetWeakPtr(), |
| 187 | weak_get_cookies_result.GetWeakPtr())); |
| 188 | run_loop.Run(); |
| 189 | EXPECT_TRUE(is_called); |
| 190 | EXPECT_EQ(cookie_line, get_cookies_result); |
| 191 | } |
| 192 | |
| 193 | // Test parameters definitions follow... |
| 194 | |
| 195 | const ClientUseCookieParameter kClientUseCookieParameters[] = { |
| 196 | // Non-secure cookies for ws |
| 197 | {"ws://www.example.com", |
| 198 | "http://www.example.com", |
| 199 | "test-cookie", |
| 200 | "Cookie: test-cookie"}, |
| 201 | |
| 202 | {"ws://www.example.com", |
| 203 | "https://www.example.com", |
| 204 | "test-cookie", |
| 205 | "Cookie: test-cookie"}, |
| 206 | |
| 207 | {"ws://www.example.com", |
| 208 | "ws://www.example.com", |
| 209 | "test-cookie", |
| 210 | "Cookie: test-cookie"}, |
| 211 | |
| 212 | {"ws://www.example.com", |
| 213 | "wss://www.example.com", |
| 214 | "test-cookie", |
| 215 | "Cookie: test-cookie"}, |
| 216 | |
| 217 | // Non-secure cookies for wss |
| 218 | {"wss://www.example.com", |
| 219 | "http://www.example.com", |
| 220 | "test-cookie", |
| 221 | "Cookie: test-cookie"}, |
| 222 | |
| 223 | {"wss://www.example.com", |
| 224 | "https://www.example.com", |
| 225 | "test-cookie", |
| 226 | "Cookie: test-cookie"}, |
| 227 | |
| 228 | {"wss://www.example.com", |
| 229 | "ws://www.example.com", |
| 230 | "test-cookie", |
| 231 | "Cookie: test-cookie"}, |
| 232 | |
| 233 | {"wss://www.example.com", |
| 234 | "wss://www.example.com", |
| 235 | "test-cookie", |
| 236 | "Cookie: test-cookie"}, |
| 237 | |
| 238 | // Secure-cookies for ws |
| 239 | {"ws://www.example.com", |
| 240 | "https://www.example.com", |
| 241 | "test-cookie; secure", |
| 242 | kNoCookieHeader}, |
| 243 | |
| 244 | {"ws://www.example.com", |
| 245 | "wss://www.example.com", |
| 246 | "test-cookie; secure", |
| 247 | kNoCookieHeader}, |
| 248 | |
| 249 | // Secure-cookies for wss |
| 250 | {"wss://www.example.com", |
| 251 | "https://www.example.com", |
| 252 | "test-cookie; secure", |
| 253 | "Cookie: test-cookie"}, |
| 254 | |
| 255 | {"wss://www.example.com", |
| 256 | "wss://www.example.com", |
| 257 | "test-cookie; secure", |
| 258 | "Cookie: test-cookie"}, |
| 259 | |
| 260 | // Non-secure cookies for ws (sharing domain) |
| 261 | {"ws://www.example.com", |
| 262 | "http://www2.example.com", |
| 263 | "test-cookie; Domain=example.com", |
| 264 | "Cookie: test-cookie"}, |
| 265 | |
| 266 | {"ws://www.example.com", |
| 267 | "https://www2.example.com", |
| 268 | "test-cookie; Domain=example.com", |
| 269 | "Cookie: test-cookie"}, |
| 270 | |
| 271 | {"ws://www.example.com", |
| 272 | "ws://www2.example.com", |
| 273 | "test-cookie; Domain=example.com", |
| 274 | "Cookie: test-cookie"}, |
| 275 | |
| 276 | {"ws://www.example.com", |
| 277 | "wss://www2.example.com", |
| 278 | "test-cookie; Domain=example.com", |
| 279 | "Cookie: test-cookie"}, |
| 280 | |
| 281 | // Non-secure cookies for wss (sharing domain) |
| 282 | {"wss://www.example.com", |
| 283 | "http://www2.example.com", |
| 284 | "test-cookie; Domain=example.com", |
| 285 | "Cookie: test-cookie"}, |
| 286 | |
| 287 | {"wss://www.example.com", |
| 288 | "https://www2.example.com", |
| 289 | "test-cookie; Domain=example.com", |
| 290 | "Cookie: test-cookie"}, |
| 291 | |
| 292 | {"wss://www.example.com", |
| 293 | "ws://www2.example.com", |
| 294 | "test-cookie; Domain=example.com", |
| 295 | "Cookie: test-cookie"}, |
| 296 | |
| 297 | {"wss://www.example.com", |
| 298 | "wss://www2.example.com", |
| 299 | "test-cookie; Domain=example.com", |
| 300 | "Cookie: test-cookie"}, |
| 301 | |
| 302 | // Secure-cookies for ws (sharing domain) |
| 303 | {"ws://www.example.com", |
| 304 | "https://www2.example.com", |
| 305 | "test-cookie; Domain=example.com; secure", |
| 306 | kNoCookieHeader}, |
| 307 | |
| 308 | {"ws://www.example.com", |
| 309 | "wss://www2.example.com", |
| 310 | "test-cookie; Domain=example.com; secure", |
| 311 | kNoCookieHeader}, |
| 312 | |
| 313 | // Secure-cookies for wss (sharing domain) |
| 314 | {"wss://www.example.com", |
| 315 | "https://www2.example.com", |
| 316 | "test-cookie; Domain=example.com; secure", |
| 317 | "Cookie: test-cookie"}, |
| 318 | |
| 319 | {"wss://www.example.com", |
| 320 | "wss://www2.example.com", |
| 321 | "test-cookie; Domain=example.com; secure", |
| 322 | "Cookie: test-cookie"}, |
| 323 | |
| 324 | // Non-matching cookies for ws |
| 325 | {"ws://www.example.com", |
| 326 | "http://www2.example.com", |
| 327 | "test-cookie", |
| 328 | kNoCookieHeader}, |
| 329 | |
| 330 | {"ws://www.example.com", |
| 331 | "https://www2.example.com", |
| 332 | "test-cookie", |
| 333 | kNoCookieHeader}, |
| 334 | |
| 335 | {"ws://www.example.com", |
| 336 | "ws://www2.example.com", |
| 337 | "test-cookie", |
| 338 | kNoCookieHeader}, |
| 339 | |
| 340 | {"ws://www.example.com", |
| 341 | "wss://www2.example.com", |
| 342 | "test-cookie", |
| 343 | kNoCookieHeader}, |
| 344 | |
| 345 | // Non-matching cookies for wss |
| 346 | {"wss://www.example.com", |
| 347 | "http://www2.example.com", |
| 348 | "test-cookie", |
| 349 | kNoCookieHeader}, |
| 350 | |
| 351 | {"wss://www.example.com", |
| 352 | "https://www2.example.com", |
| 353 | "test-cookie", |
| 354 | kNoCookieHeader}, |
| 355 | |
| 356 | {"wss://www.example.com", |
| 357 | "ws://www2.example.com", |
| 358 | "test-cookie", |
| 359 | kNoCookieHeader}, |
| 360 | |
| 361 | {"wss://www.example.com", |
| 362 | "wss://www2.example.com", |
| 363 | "test-cookie", |
| 364 | kNoCookieHeader}, |
| 365 | }; |
| 366 | |
| 367 | INSTANTIATE_TEST_CASE_P(WebSocketStreamClientUseCookieTest, |
| 368 | WebSocketStreamClientUseCookieTest, |
| 369 | ValuesIn(kClientUseCookieParameters)); |
| 370 | |
| 371 | const ServerSetCookieParameter kServerSetCookieParameters[] = { |
| 372 | // Cookies coming from ws |
| 373 | {"ws://www.example.com", |
| 374 | "http://www.example.com", |
| 375 | "test-cookie", |
| 376 | "Set-Cookie: test-cookie"}, |
| 377 | |
| 378 | {"ws://www.example.com", |
| 379 | "https://www.example.com", |
| 380 | "test-cookie", |
| 381 | "Set-Cookie: test-cookie"}, |
| 382 | |
| 383 | {"ws://www.example.com", |
| 384 | "ws://www.example.com", |
| 385 | "test-cookie", |
| 386 | "Set-Cookie: test-cookie"}, |
| 387 | |
| 388 | {"ws://www.example.com", |
| 389 | "wss://www.example.com", |
| 390 | "test-cookie", |
| 391 | "Set-Cookie: test-cookie"}, |
| 392 | |
| 393 | // Cookies coming from wss |
| 394 | {"wss://www.example.com", |
| 395 | "http://www.example.com", |
| 396 | "test-cookie", |
| 397 | "Set-Cookie: test-cookie"}, |
| 398 | |
| 399 | {"wss://www.example.com", |
| 400 | "https://www.example.com", |
| 401 | "test-cookie", |
| 402 | "Set-Cookie: test-cookie"}, |
| 403 | |
| 404 | {"wss://www.example.com", |
| 405 | "ws://www.example.com", |
| 406 | "test-cookie", |
| 407 | "Set-Cookie: test-cookie"}, |
| 408 | |
| 409 | {"wss://www.example.com", |
| 410 | "wss://www.example.com", |
| 411 | "test-cookie", |
| 412 | "Set-Cookie: test-cookie"}, |
| 413 | |
| 414 | // cookies coming from ws (sharing domain) |
| 415 | {"ws://www.example.com", |
| 416 | "http://www2.example.com", |
| 417 | "test-cookie", |
| 418 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 419 | |
| 420 | {"ws://www.example.com", |
| 421 | "https://www2.example.com", |
| 422 | "test-cookie", |
| 423 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 424 | |
| 425 | {"ws://www.example.com", |
| 426 | "ws://www2.example.com", |
| 427 | "test-cookie", |
| 428 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 429 | |
| 430 | {"ws://www.example.com", |
| 431 | "wss://www2.example.com", |
| 432 | "test-cookie", |
| 433 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 434 | |
| 435 | // cookies coming from wss (sharing domain) |
| 436 | {"wss://www.example.com", |
| 437 | "http://www2.example.com", |
| 438 | "test-cookie", |
| 439 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 440 | |
| 441 | {"wss://www.example.com", |
| 442 | "https://www2.example.com", |
| 443 | "test-cookie", |
| 444 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 445 | |
| 446 | {"wss://www.example.com", |
| 447 | "ws://www2.example.com", |
| 448 | "test-cookie", |
| 449 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 450 | |
| 451 | {"wss://www.example.com", |
| 452 | "wss://www2.example.com", |
| 453 | "test-cookie", |
| 454 | "Set-Cookie: test-cookie; Domain=example.com"}, |
| 455 | |
| 456 | // Non-matching cookies coming from ws |
| 457 | {"ws://www.example.com", |
| 458 | "http://www2.example.com", |
| 459 | "", |
| 460 | "Set-Cookie: test-cookie"}, |
| 461 | |
| 462 | {"ws://www.example.com", |
| 463 | "https://www2.example.com", |
| 464 | "", |
| 465 | "Set-Cookie: test-cookie"}, |
| 466 | |
| 467 | {"ws://www.example.com", |
| 468 | "ws://www2.example.com", |
| 469 | "", |
| 470 | "Set-Cookie: test-cookie"}, |
| 471 | |
| 472 | {"ws://www.example.com", |
| 473 | "wss://www2.example.com", |
| 474 | "", |
| 475 | "Set-Cookie: test-cookie"}, |
| 476 | |
| 477 | // Non-matching cookies coming from wss |
| 478 | {"wss://www.example.com", |
| 479 | "http://www2.example.com", |
| 480 | "", |
| 481 | "Set-Cookie: test-cookie"}, |
| 482 | |
| 483 | {"wss://www.example.com", |
| 484 | "https://www2.example.com", |
| 485 | "", |
| 486 | "Set-Cookie: test-cookie"}, |
| 487 | |
| 488 | {"wss://www.example.com", |
| 489 | "ws://www2.example.com", |
| 490 | "", |
| 491 | "Set-Cookie: test-cookie"}, |
| 492 | |
| 493 | {"wss://www.example.com", |
| 494 | "wss://www2.example.com", |
| 495 | "", |
| 496 | "Set-Cookie: test-cookie"}, |
| 497 | }; |
| 498 | |
| 499 | INSTANTIATE_TEST_CASE_P(WebSocketStreamServerSetCookieTest, |
| 500 | WebSocketStreamServerSetCookieTest, |
| 501 | ValuesIn(kServerSetCookieParameters)); |
| 502 | |
| 503 | } // namespace |
| 504 | } // namespace net |