Chromium Code Reviews| Index: net/websockets/websocket_stream_test.cc |
| diff --git a/net/websockets/websocket_stream_test.cc b/net/websockets/websocket_stream_test.cc |
| index 03a593de0e9a4b7c5f98f4fed36b65ed793a7554..e55ac6e60bd9c6dd410d2325c69ff67d8dbde7c2 100644 |
| --- a/net/websockets/websocket_stream_test.cc |
| +++ b/net/websockets/websocket_stream_test.cc |
| @@ -124,6 +124,23 @@ class WebSocketStreamCreateTest : public ::testing::Test { |
| CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); |
| } |
| + void CreateAndConnectCustomResponseWithCookies( |
| + const std::string& socket_url, |
| + const std::string& socket_host, |
| + const std::string& socket_path, |
| + const std::vector<std::string>& sub_protocols, |
| + const std::string& origin, |
| + const std::string& cookies, |
| + const std::string& extra_request_headers, |
| + const std::string& response_body, |
| + scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
| + url_request_context_host_.SetExpectations( |
| + WebSocketStandardRequestWithCookies(socket_path, socket_host, origin, |
| + cookies, extra_request_headers), |
| + response_body); |
| + CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); |
| + } |
| + |
| // |extra_request_headers| and |extra_response_headers| must end in "\r\n" or |
| // errors like "Unable to perform synchronous IO while stopped" will occur. |
| void CreateAndConnectStandard( |
| @@ -141,6 +158,25 @@ class WebSocketStreamCreateTest : public ::testing::Test { |
| WebSocketStandardResponse(extra_response_headers), timer.Pass()); |
| } |
| + // |cookies| must be empty or end with "\r\n". |
| + // |extra_request_headers| and |extra_response_headers| must end in "\r\n" or |
| + // errors like "Unable to perform synchronous IO while stopped" will occur. |
| + void CreateAndConnectStandardWithCookies( |
| + const std::string& socket_url, |
| + const std::string& socket_host, |
| + const std::string& socket_path, |
| + const std::vector<std::string>& sub_protocols, |
| + const std::string& origin, |
| + const std::string& cookies, |
| + const std::string& extra_request_headers, |
| + const std::string& extra_response_headers, |
| + scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
| + CreateAndConnectCustomResponseWithCookies( |
| + socket_url, socket_host, socket_path, sub_protocols, origin, cookies, |
| + extra_request_headers, |
| + WebSocketStandardResponse(extra_response_headers), timer.Pass()); |
| + } |
| + |
| void CreateAndConnectRawExpectations( |
| const std::string& socket_url, |
| const std::vector<std::string>& sub_protocols, |
| @@ -416,6 +452,82 @@ class WebSocketStreamCreateUMATest : public ::testing::Test { |
| } |
| }; |
| +struct WebSocketStreamUseCookieTestParameter { |
| + WebSocketStreamUseCookieTestParameter(const GURL& url, |
| + const GURL& cookie_url, |
| + const std::string& origin, |
| + const std::string& cookie_line, |
| + const std::string& cookies) |
| + : url(url), |
| + cookie_url(cookie_url), |
| + origin(origin), |
| + cookie_line(cookie_line), |
| + cookies(cookies) {} |
| + |
| + const GURL url; |
|
Adam Rice
2015/01/23 11:14:02
Personally I would make all these types const char
yhirano
2015/01/23 12:19:00
I would like to leave it as is, because of type ch
|
| + const GURL cookie_url; |
| + const std::string origin; |
| + const std::string cookie_line; |
| + const std::string cookies; |
| +}; |
| + |
| +class WebSocketStreamUseCookieTest |
| + : public WebSocketStreamCreateTest, |
| + public ::testing::WithParamInterface< |
| + WebSocketStreamUseCookieTestParameter> { |
| + public: |
| + void SetCookie(CookieStore* store, |
| + const GURL& url, |
| + const std::string& cookie_line) { |
| + class RunLoop { |
| + public: |
| + void Run() { run_loop_.Run(); } |
| + void Quit(bool) { run_loop_.Quit(); } |
| + |
| + private: |
| + base::RunLoop run_loop_; |
| + }; |
| + |
| + RunLoop run_loop; |
| + store->SetCookieWithOptionsAsync( |
| + url, cookie_line, CookieOptions(), |
| + base::Bind(&RunLoop::Quit, base::Unretained(&run_loop))); |
| + run_loop.Run(); |
| + } |
| +}; |
| + |
| +class WebSocketStreamSetCookieTest |
| + : public WebSocketStreamCreateTest, |
| + public ::testing::WithParamInterface< |
| + WebSocketStreamUseCookieTestParameter> { |
| + public: |
| + std::string GetCookies(CookieStore* store, const GURL& url) { |
| + struct GetCookies { |
|
Adam Rice
2015/01/23 11:14:02
I find having a struct called GetCookies inside a
yhirano
2015/01/23 12:19:00
Done.
|
| + void Run() { |
| + run_loop_.Run(); |
| + ; |
|
Adam Rice
2015/01/23 11:14:02
This semi-colon does nothing!
yhirano
2015/01/23 12:19:00
Done.
|
| + } |
| + void Quit(const std::string& cookies) { |
| + cookies_ = cookies; |
| + run_loop_.Quit(); |
| + } |
| + |
| + const std::string& cookies() const { return cookies_; } |
| + |
| + private: |
| + base::RunLoop run_loop_; |
| + std::string cookies_; |
| + }; |
| + GetCookies callback; |
| + |
| + store->GetCookiesWithOptionsAsync( |
| + url, CookieOptions(), |
| + base::Bind(&GetCookies::Quit, base::Unretained(&callback))); |
| + callback.Run(); |
| + return callback.cookies(); |
| + } |
| +}; |
| + |
| // Confirm that the basic case works as expected. |
| TEST_F(WebSocketStreamCreateTest, SimpleSuccess) { |
| CreateAndConnectStandard("ws://localhost/", "localhost", "/", |
| @@ -1381,5 +1493,431 @@ TEST_F(WebSocketStreamCreateUMATest, Failed) { |
| EXPECT_EQ(0, samples->GetCount(FAILED)); |
| } |
| +TEST_P(WebSocketStreamUseCookieTest, UseCookie) { |
| + // In case we use a TLS connection. |
| + ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK)); |
| + |
| + CookieStore* store = |
| + url_request_context_host_.GetURLRequestContext()->cookie_store(); |
| + |
| + const GURL& url = GetParam().url; |
| + const GURL& cookie_url = GetParam().cookie_url; |
| + const std::string& origin = GetParam().origin; |
| + const std::string& cookie_line = GetParam().cookie_line; |
| + const std::string& cookies = GetParam().cookies; |
| + |
| + SetCookie(store, cookie_url, cookie_line); |
| + |
| + CreateAndConnectStandardWithCookies(url.spec(), url.host(), url.path(), |
| + NoSubProtocols(), origin, cookies, "", |
| + ""); |
| + |
| + RunUntilIdle(); |
| + EXPECT_FALSE(has_failed()); |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P( |
| + WebSocketStreamUseCookieTest, |
| + WebSocketStreamUseCookieTest, |
| + ::testing::Values( |
| + // Non-secure cookies for ws |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("http://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("https://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("ws://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("wss://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + // Non-secure cookies for wss |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("http://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("https://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("ws://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("wss://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + // Secure-cookies for ws |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("https://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; secure", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("wss://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; secure", |
| + ""), |
| + |
| + // Secure-cookies for wss |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("https://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; secure", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("wss://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; secure", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + // Non-secure cookies for ws (sharing domain) |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + // Non-secure cookies for wss (sharing domain) |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; Domain=example.com", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + // Secure-cookies for ws (sharing domain) |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("ws://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; Domain=example.com; secure", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("ws://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie; Domain=example.com; secure", |
| + ""), |
| + |
| + // Secure-cookies for wss (sharing domain) |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("wss://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; Domain=example.com; secure", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("wss://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie; Domain=example.com; secure", |
| + "Cookie: test-cookie\r\n"), |
| + |
| + // Non-maching cookies for ws |
|
Adam Rice
2015/01/23 11:14:03
"matching" (and below).
yhirano
2015/01/23 12:19:00
Done.
|
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + // Non-maching cookies for wss |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + ""), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + ""))); |
| + |
| +TEST_P(WebSocketStreamSetCookieTest, SetCookie) { |
| + // In case we use a TLS connection. |
| + ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK)); |
| + |
| + const GURL& url = GetParam().url; |
| + const GURL& cookie_url = GetParam().cookie_url; |
| + const std::string& origin = GetParam().origin; |
| + const std::string& cookie_line = GetParam().cookie_line; |
| + const std::string& cookies = GetParam().cookies; |
| + |
| + const std::string response = base::StringPrintf( |
| + "HTTP/1.1 101 Switching Protocols\r\n" |
| + "Upgrade: websocket\r\n" |
| + "Connection: Upgrade\r\n" |
| + "%s" |
| + "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| + "\r\n", |
| + cookies.c_str()); |
| + |
| + CookieStore* store = |
| + url_request_context_host_.GetURLRequestContext()->cookie_store(); |
| + |
| + CreateAndConnectCustomResponse(url.spec(), url.host(), url.path(), |
| + NoSubProtocols(), origin, "", response); |
| + |
| + RunUntilIdle(); |
| + EXPECT_FALSE(has_failed()); |
| + |
| + EXPECT_EQ(cookie_line, GetCookies(store, cookie_url)); |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P( |
| + WebSocketStreamSetCookieTest, |
| + WebSocketStreamSetCookieTest, |
| + ::testing::Values( |
| + // Cookies for ws |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("http://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("https://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("ws://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("wss://www.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + // Cookies for wss |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("http://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("https://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("ws://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("wss://www.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + // cookies for ws (sharing domain) |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("ws://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("ws://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("ws://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("ws://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "http://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + // cookies for wss (sharing domain) |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("wss://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("wss://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("wss://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter( |
| + GURL("wss://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "https://www.example.com", |
| + "test-cookie", |
| + "Set-Cookie: test-cookie; Domain=example.com\r\n"), |
| + |
| + // Non-maching cookies for ws |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "http://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "http://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "http://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("ws://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "http://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + // Non-maching cookies for wss |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("http://www2.example.com"), |
| + "https://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("https://www2.example.com"), |
| + "https://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("ws://www2.example.com"), |
| + "https://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"), |
| + |
| + WebSocketStreamUseCookieTestParameter(GURL("wss://www.example.com"), |
| + GURL("wss://www2.example.com"), |
| + "https://www.example.com", |
| + "", |
| + "Set-Cookie: test-cookie\r\n"))); |
| + |
| } // namespace |
| } // namespace net |