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