Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: net/websockets/websocket_stream_cookie_test.cc

Issue 869073002: Add WebSocket cookie tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "net/websockets/websocket_stream.h"
6
7 #include <string>
8 #include "base/run_loop.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/websockets/websocket_stream_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
14
15 namespace net {
16 namespace {
17
18 using ::testing::TestWithParam;
19 using ::testing::ValuesIn;
20
21 const char* const kNoCookieHeader = "";
22
23 class TestBase : public WebSocketStreamCreateTestBase {
24 public:
25 void CreateAndConnect(const GURL& url,
26 const std::string& origin,
27 const std::string& cookie_header,
28 const std::string& response_body) {
29 CHECK(cookie_header.empty() || EndsWith(cookie_header, "\r\n", true));
Ryan Sleevi 2015/02/02 20:14:41 Comment explaining why?
yhirano 2015/02/09 10:50:14 Done.
30
31 url_request_context_host_.SetExpectations(
32 WebSocketStandardRequestWithCookies(url.path(), url.host(), origin,
33 cookie_header, std::string()),
34 response_body);
35 CreateAndConnectStream(url.spec(), NoSubProtocols(), origin, nullptr);
36 }
37
38 static std::string AddCRLFIfNotEmpty(const std::string& s) {
Ryan Sleevi 2015/02/02 20:14:41 No need to make this static.
yhirano 2015/02/09 10:50:14 Done.
39 return s.empty() ? s : s + "\r\n";
40 }
41 };
42
43 class ClientUseCookieParameter {
yhirano 2015/02/02 05:39:57 I made this (and the other) class POD.
Ryan Sleevi 2015/02/02 20:14:41 Make this a struct then, since you're following st
yhirano 2015/02/09 10:50:14 Done.
44 public:
45 // The URL for the WebSocket connection.
yhirano 2015/02/02 05:29:56 Comment: done
46 const char* const url;
yhirano 2015/02/02 05:29:56 I don't see any harm from constness.
47 // The URL for the previously set cookies.
48 const char* const cookie_url;
49 // The previously set cookies contents.
50 const char* const cookie_line;
51 // The Cookie: HTTP header expected to appear in the WS request. An empty
52 // string means there is no Cookie: header.
53 const char* const cookie_header;
54 };
55
56 class WebSocketStreamClientUseCookieTest
57 : public TestBase,
58 public TestWithParam<ClientUseCookieParameter> {
59 public:
60 ~WebSocketStreamClientUseCookieTest() override {
61 // Permit any endpoint locks to be released.
62 stream_request_.reset();
63 stream_.reset();
64 RunUntilIdle();
65 }
66
67 void SetCookie(CookieStore* store,
68 const GURL& url,
69 const std::string& cookie_line) {
70 class RunLoop {
yhirano 2015/02/02 05:29:56 I will fix this helper if necessary after the GetC
yhirano 2015/02/09 10:50:14 Done.
71 public:
72 void Run() { run_loop_.Run(); }
73 void Quit(bool unused) { run_loop_.Quit(); }
74
75 private:
76 base::RunLoop run_loop_;
77 };
78
79 RunLoop run_loop;
80 store->SetCookieWithOptionsAsync(
81 url, cookie_line, CookieOptions(),
82 base::Bind(&RunLoop::Quit, base::Unretained(&run_loop)));
83 run_loop.Run();
84 }
85 };
86
87 class ServerSetCookieParameter {
Ryan Sleevi 2015/02/02 20:14:41 ditto struct
yhirano 2015/02/09 10:50:14 Done.
88 public:
89 // The URL for the WebSocket connection.
90 const char* const url;
91 // The URL used to query cookies after the response received.
92 const char* const cookie_url;
93 // The cookies expected to appear for |cookie_url| inquiry.
94 const char* const cookie_line;
95 // The Set-Cookie: HTTP header attached to the response.
96 const char* const cookie_header;
97 };
98
99 class WebSocketStreamServerSetCookieTest
100 : public TestBase,
101 public TestWithParam<ServerSetCookieParameter> {
102 public:
103 ~WebSocketStreamServerSetCookieTest() override {
104 // Permit any endpoint locks to be released.
105 stream_request_.reset();
106 stream_.reset();
107 RunUntilIdle();
108 }
109
110 std::string GetCookies(CookieStore* store, const GURL& url) {
111 struct GetCookiesHelper {
yhirano 2015/02/02 05:29:56 Ryan's comment (https://codereview.chromium.org/86
yhirano 2015/02/02 05:39:57 I'm not sure what you presume: If you are worrying
Ryan Sleevi 2015/02/02 20:14:41 The concern here is that the message loop is quit
yhirano 2015/02/03 01:26:32 When the message loop is quit without calling Stas
Ryan Sleevi 2015/02/03 02:16:27 I still find this code not up to the degree that w
yhirano 2015/02/03 03:41:10 Sorry, I still don't understand. I understand you
yhirano 2015/02/09 10:50:14 Although I am not 100% sure what you suggested, bu
112 void Run() { run_loop_.Run(); }
113 void Quit(const std::string& cookies) {
114 cookies_ = cookies;
115 run_loop_.Quit();
116 }
117
118 const std::string& cookies() const { return cookies_; }
119
120 private:
121 base::RunLoop run_loop_;
122 std::string cookies_;
123 };
124 GetCookiesHelper helper;
125
126 store->GetCookiesWithOptionsAsync(
127 url, CookieOptions(),
128 base::Bind(&GetCookiesHelper::Quit, base::Unretained(&helper)));
129 helper.Run();
130 return helper.cookies();
131 }
132 };
133
134 TEST_P(WebSocketStreamClientUseCookieTest, ClientUseCookie) {
135 // For wss tests.
136 ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK));
137
138 CookieStore* store =
139 url_request_context_host_.GetURLRequestContext()->cookie_store();
140
141 const GURL url(GetParam().url);
142 const GURL cookie_url(GetParam().cookie_url);
143 const std::string origin("http://www.example.com");
144 const std::string cookie_line(GetParam().cookie_line);
145 const std::string cookie_header(AddCRLFIfNotEmpty(GetParam().cookie_header));
146
147 SetCookie(store, cookie_url, cookie_line);
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.
yhirano 2015/02/02 05:29:56 Ryan's comment: This comment doesn't make sense. I
yhirano 2015/02/02 05:39:57 I'm not certain because it depends parameters. Fix
yhirano 2015/02/02 06:11:58 Sorry, I meant 'it depends on parameters.'
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));
yhirano 2015/02/02 05:29:56 I don't have a strong opinion whether we enforce "
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 EXPECT_EQ(cookie_line, GetCookies(store, cookie_url));
183 }
184
185 // Test parameters definitions follow...
186
187 const ClientUseCookieParameter kClientUseCookieParameters[] = {
188 // Non-secure cookies for ws
189 {"ws://www.example.com",
190 "http://www.example.com",
191 "test-cookie",
192 "Cookie: test-cookie"},
193
194 {"ws://www.example.com",
195 "https://www.example.com",
196 "test-cookie",
197 "Cookie: test-cookie"},
198
199 {"ws://www.example.com",
200 "ws://www.example.com",
201 "test-cookie",
202 "Cookie: test-cookie"},
203
204 {"ws://www.example.com",
205 "wss://www.example.com",
206 "test-cookie",
207 "Cookie: test-cookie"},
208
209 // Non-secure cookies for wss
210 {"wss://www.example.com",
211 "http://www.example.com",
212 "test-cookie",
213 "Cookie: test-cookie"},
214
215 {"wss://www.example.com",
216 "https://www.example.com",
217 "test-cookie",
218 "Cookie: test-cookie"},
219
220 {"wss://www.example.com",
221 "ws://www.example.com",
222 "test-cookie",
223 "Cookie: test-cookie"},
224
225 {"wss://www.example.com",
226 "wss://www.example.com",
227 "test-cookie",
228 "Cookie: test-cookie"},
229
230 // Secure-cookies for ws
231 {"ws://www.example.com",
232 "https://www.example.com",
233 "test-cookie; secure",
234 kNoCookieHeader},
235
236 {"ws://www.example.com",
237 "wss://www.example.com",
238 "test-cookie; secure",
239 kNoCookieHeader},
240
241 // Secure-cookies for wss
242 {"wss://www.example.com",
243 "https://www.example.com",
244 "test-cookie; secure",
245 "Cookie: test-cookie"},
246
247 {"wss://www.example.com",
248 "wss://www.example.com",
249 "test-cookie; secure",
250 "Cookie: test-cookie"},
251
252 // Non-secure cookies for ws (sharing domain)
253 {"ws://www.example.com",
254 "http://www2.example.com",
255 "test-cookie; Domain=example.com",
256 "Cookie: test-cookie"},
257
258 {"ws://www.example.com",
259 "https://www2.example.com",
260 "test-cookie; Domain=example.com",
261 "Cookie: test-cookie"},
262
263 {"ws://www.example.com",
264 "ws://www2.example.com",
265 "test-cookie; Domain=example.com",
266 "Cookie: test-cookie"},
267
268 {"ws://www.example.com",
269 "wss://www2.example.com",
270 "test-cookie; Domain=example.com",
271 "Cookie: test-cookie"},
272
273 // Non-secure cookies for wss (sharing domain)
274 {"wss://www.example.com",
275 "http://www2.example.com",
276 "test-cookie; Domain=example.com",
277 "Cookie: test-cookie"},
278
279 {"wss://www.example.com",
280 "https://www2.example.com",
281 "test-cookie; Domain=example.com",
282 "Cookie: test-cookie"},
283
284 {"wss://www.example.com",
285 "ws://www2.example.com",
286 "test-cookie; Domain=example.com",
287 "Cookie: test-cookie"},
288
289 {"wss://www.example.com",
290 "wss://www2.example.com",
291 "test-cookie; Domain=example.com",
292 "Cookie: test-cookie"},
293
294 // Secure-cookies for ws (sharing domain)
295 {"ws://www.example.com",
296 "https://www2.example.com",
297 "test-cookie; Domain=example.com; secure",
298 kNoCookieHeader},
299
300 {"ws://www.example.com",
301 "wss://www2.example.com",
302 "test-cookie; Domain=example.com; secure",
303 kNoCookieHeader},
304
305 // Secure-cookies for wss (sharing domain)
306 {"wss://www.example.com",
307 "https://www2.example.com",
308 "test-cookie; Domain=example.com; secure",
309 "Cookie: test-cookie"},
310
311 {"wss://www.example.com",
312 "wss://www2.example.com",
313 "test-cookie; Domain=example.com; secure",
314 "Cookie: test-cookie"},
315
316 // Non-matching cookies for ws
317 {"ws://www.example.com",
318 "http://www2.example.com",
319 "test-cookie",
320 kNoCookieHeader},
321
322 {"ws://www.example.com",
323 "https://www2.example.com",
324 "test-cookie",
325 kNoCookieHeader},
326
327 {"ws://www.example.com",
328 "ws://www2.example.com",
329 "test-cookie",
330 kNoCookieHeader},
331
332 {"ws://www.example.com",
333 "wss://www2.example.com",
334 "test-cookie",
335 kNoCookieHeader},
336
337 // Non-matching cookies for wss
338 {"wss://www.example.com",
339 "http://www2.example.com",
340 "test-cookie",
341 kNoCookieHeader},
342
343 {"wss://www.example.com",
344 "https://www2.example.com",
345 "test-cookie",
346 kNoCookieHeader},
347
348 {"wss://www.example.com",
349 "ws://www2.example.com",
350 "test-cookie",
351 kNoCookieHeader},
352
353 {"wss://www.example.com",
354 "wss://www2.example.com",
355 "test-cookie",
356 kNoCookieHeader},
357 };
358
359 const ServerSetCookieParameter kServerSetCookieParameters[] = {
360 // Cookies coming from ws
361 {"ws://www.example.com",
362 "http://www.example.com",
363 "test-cookie",
364 "Set-Cookie: test-cookie"},
365
366 {"ws://www.example.com",
367 "https://www.example.com",
368 "test-cookie",
369 "Set-Cookie: test-cookie"},
370
371 {"ws://www.example.com",
372 "ws://www.example.com",
373 "test-cookie",
374 "Set-Cookie: test-cookie"},
375
376 {"ws://www.example.com",
377 "wss://www.example.com",
378 "test-cookie",
379 "Set-Cookie: test-cookie"},
380
381 // Cookies coming from wss
382 {"wss://www.example.com",
383 "http://www.example.com",
384 "test-cookie",
385 "Set-Cookie: test-cookie"},
386
387 {"wss://www.example.com",
388 "https://www.example.com",
389 "test-cookie",
390 "Set-Cookie: test-cookie"},
391
392 {"wss://www.example.com",
393 "ws://www.example.com",
394 "test-cookie",
395 "Set-Cookie: test-cookie"},
396
397 {"wss://www.example.com",
398 "wss://www.example.com",
399 "test-cookie",
400 "Set-Cookie: test-cookie"},
401
402 // cookies coming from ws (sharing domain)
403 {"ws://www.example.com",
404 "http://www2.example.com",
405 "test-cookie",
406 "Set-Cookie: test-cookie; Domain=example.com"},
407
408 {"ws://www.example.com",
409 "https://www2.example.com",
410 "test-cookie",
411 "Set-Cookie: test-cookie; Domain=example.com"},
412
413 {"ws://www.example.com",
414 "ws://www2.example.com",
415 "test-cookie",
416 "Set-Cookie: test-cookie; Domain=example.com"},
417
418 {"ws://www.example.com",
419 "wss://www2.example.com",
420 "test-cookie",
421 "Set-Cookie: test-cookie; Domain=example.com"},
422
423 // cookies coming from wss (sharing domain)
424 {"wss://www.example.com",
425 "http://www2.example.com",
426 "test-cookie",
427 "Set-Cookie: test-cookie; Domain=example.com"},
428
429 {"wss://www.example.com",
430 "https://www2.example.com",
431 "test-cookie",
432 "Set-Cookie: test-cookie; Domain=example.com"},
433
434 {"wss://www.example.com",
435 "ws://www2.example.com",
436 "test-cookie",
437 "Set-Cookie: test-cookie; Domain=example.com"},
438
439 {"wss://www.example.com",
440 "wss://www2.example.com",
441 "test-cookie",
442 "Set-Cookie: test-cookie; Domain=example.com"},
443
444 // Non-matching cookies coming from ws
445 {"ws://www.example.com",
446 "http://www2.example.com",
447 "",
448 "Set-Cookie: test-cookie"},
449
450 {"ws://www.example.com",
451 "https://www2.example.com",
452 "",
453 "Set-Cookie: test-cookie"},
454
455 {"ws://www.example.com",
456 "ws://www2.example.com",
457 "",
458 "Set-Cookie: test-cookie"},
459
460 {"ws://www.example.com",
461 "wss://www2.example.com",
462 "",
463 "Set-Cookie: test-cookie"},
464
465 // Non-matching cookies coming from wss
466 {"wss://www.example.com",
467 "http://www2.example.com",
468 "",
469 "Set-Cookie: test-cookie"},
470
471 {"wss://www.example.com",
472 "https://www2.example.com",
473 "",
474 "Set-Cookie: test-cookie"},
475
476 {"wss://www.example.com",
477 "ws://www2.example.com",
478 "",
479 "Set-Cookie: test-cookie"},
480
481 {"wss://www.example.com",
482 "wss://www2.example.com",
483 "",
484 "Set-Cookie: test-cookie"},
485 };
486
487 INSTANTIATE_TEST_CASE_P(WebSocketStreamClientUseCookieTest,
488 WebSocketStreamClientUseCookieTest,
489 ValuesIn(kClientUseCookieParameters));
Ryan Sleevi 2015/02/02 20:14:41 Move this to line 358 - keep your code & instantia
yhirano 2015/02/09 10:50:14 Done.
490
491 INSTANTIATE_TEST_CASE_P(WebSocketStreamServerSetCookieTest,
492 WebSocketStreamServerSetCookieTest,
493 ValuesIn(kServerSetCookieParameters));
494
495 } // namespace
496 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698