OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import android.test.MoreAsserts; | 7 import android.test.MoreAsserts; |
8 import android.test.suitebuilder.annotation.MediumTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
9 import android.util.Pair; | 9 import android.util.Pair; |
10 import android.webkit.ValueCallback; | 10 import android.webkit.ValueCallback; |
11 | 11 |
12 import static org.chromium.android_webview.test.util.CookieUtils.TestValueCallba ck; | 12 import static org.chromium.android_webview.test.util.CookieUtils.TestValueCallba ck; |
13 | 13 |
14 import org.chromium.android_webview.AwContents; | 14 import org.chromium.android_webview.AwContents; |
15 import org.chromium.android_webview.AwCookieManager; | 15 import org.chromium.android_webview.AwCookieManager; |
16 import org.chromium.android_webview.AwSettings; | 16 import org.chromium.android_webview.AwSettings; |
17 import org.chromium.android_webview.test.util.CookieUtils; | 17 import org.chromium.android_webview.test.util.CookieUtils; |
18 import org.chromium.android_webview.test.util.JSUtils; | 18 import org.chromium.android_webview.test.util.JSUtils; |
19 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
20 import org.chromium.content.browser.test.util.JavaScriptUtils; | |
21 import org.chromium.content_public.browser.WebContents; | |
20 import org.chromium.net.test.util.TestWebServer; | 22 import org.chromium.net.test.util.TestWebServer; |
21 | 23 |
22 import java.util.ArrayList; | 24 import java.util.ArrayList; |
23 import java.util.Arrays; | 25 import java.util.Arrays; |
24 import java.util.Date; | 26 import java.util.Date; |
25 import java.util.HashSet; | 27 import java.util.HashSet; |
26 import java.util.List; | 28 import java.util.List; |
27 import java.util.Set; | 29 import java.util.Set; |
28 import java.util.concurrent.Callable; | 30 import java.util.concurrent.Callable; |
29 | 31 |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); | 396 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); |
395 waitForCookie(cookieUrl); | 397 waitForCookie(cookieUrl); |
396 String cookie = mCookieManager.getCookie(cookieUrl); | 398 String cookie = mCookieManager.getCookie(cookieUrl); |
397 assertNotNull(cookie); | 399 assertNotNull(cookie); |
398 validateCookies(cookie, "test2"); | 400 validateCookies(cookie, "test2"); |
399 } finally { | 401 } finally { |
400 webServer.shutdown(); | 402 webServer.shutdown(); |
401 } | 403 } |
402 } | 404 } |
403 | 405 |
406 @MediumTest | |
Torne
2016/10/14 09:59:24
Can you put the common code for these tests in a s
yhirano
2016/10/19 11:32:13
Done.
| |
407 @Feature({"AndroidWebView", "Privacy"}) | |
408 public void testThirdPartyCookieForWebSocketDisabledCase() throws Throwable { | |
jbudorick
2016/10/18 17:27:30
If possible, new tests should use EmbeddedTestServ
yhirano
2016/10/19 14:45:00
We need to add a response header (Sec-WebSocket-Ac
jbudorick
2016/10/19 15:40:40
Ah. Yeah, we haven't implemented that yet. Given t
| |
409 TestWebServer webServer = TestWebServer.start(); | |
410 try { | |
411 // Turn global allow on. | |
412 mCookieManager.setAcceptCookie(true); | |
413 assertTrue(mCookieManager.acceptCookie()); | |
414 | |
415 // When third party cookies are disabled... | |
416 mAwContents.getSettings().setAcceptThirdPartyCookies(false); | |
417 assertFalse(mAwContents.getSettings().getAcceptThirdPartyCookies()); | |
418 | |
419 // |cookieUrl| is a third-party url that sets a cookie on response. | |
420 String cookieUrl = toThirdPartyUrl( | |
421 makeCookieWebSocketUrl(webServer, "/cookie_1", "test1", "val ue1")); | |
422 // This html file includes a script establishing a WebSocket connect ion to |cookieUrl|. | |
423 String url = makeWebSocketScriptUrl(webServer, "/content_1.html", co okieUrl); | |
424 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); | |
425 final String connecting = "0"; // WebSocket.CONNECTING | |
426 final String closed = "3"; // WebSocket.CLOSED | |
427 String readyState = connecting; | |
428 WebContents webContents = mAwContents.getWebContents(); | |
429 while (!readyState.equals(closed)) { | |
430 readyState = JavaScriptUtils.executeJavaScriptAndWaitForResult( | |
431 webContents, "ws.readyState"); | |
432 } | |
433 assertEquals("true", | |
434 JavaScriptUtils.executeJavaScriptAndWaitForResult(webContent s, "hasOpened")); | |
435 assertNull(mCookieManager.getCookie(cookieUrl)); | |
436 } finally { | |
437 webServer.shutdown(); | |
438 } | |
439 } | |
440 | |
441 @MediumTest | |
442 @Feature({"AndroidWebView", "Privacy"}) | |
443 public void testThirdPartyCookieForWebSocketEnabledCase() throws Throwable { | |
444 TestWebServer webServer = TestWebServer.start(); | |
445 try { | |
446 // Turn global allow on. | |
447 mCookieManager.setAcceptCookie(true); | |
448 assertTrue(mCookieManager.acceptCookie()); | |
449 | |
450 // When third party cookies are enabled... | |
451 mAwContents.getSettings().setAcceptThirdPartyCookies(true); | |
452 assertTrue(mAwContents.getSettings().getAcceptThirdPartyCookies()); | |
453 | |
454 // |cookieUrl| is a third-party url that sets a cookie on response. | |
455 String cookieUrl = toThirdPartyUrl( | |
456 makeCookieWebSocketUrl(webServer, "/cookie_1", "test1", "val ue1")); | |
457 // This html file includes a script establishing a WebSocket connect ion to |cookieUrl|. | |
458 String url = makeWebSocketScriptUrl(webServer, "/content_1.html", co okieUrl); | |
459 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); | |
460 WebContents webContents = mAwContents.getWebContents(); | |
461 final String connecting = "0"; // WebSocket.CONNECTING | |
462 final String closed = "3"; // WebSocket.CLOSED | |
463 String readyState = connecting; | |
464 while (!readyState.equals(closed)) { | |
465 readyState = JavaScriptUtils.executeJavaScriptAndWaitForResult( | |
466 webContents, "ws.readyState"); | |
467 } | |
468 assertEquals("true", | |
469 JavaScriptUtils.executeJavaScriptAndWaitForResult(webContent s, "hasOpened")); | |
470 assertEquals("test1=value1", mCookieManager.getCookie(cookieUrl)); | |
471 } finally { | |
472 webServer.shutdown(); | |
473 } | |
474 } | |
475 | |
404 /** | 476 /** |
405 * Creates a response on the TestWebServer which attempts to set a cookie wh en fetched. | 477 * Creates a response on the TestWebServer which attempts to set a cookie wh en fetched. |
406 * @param webServer the webServer on which to create the response | 478 * @param webServer the webServer on which to create the response |
407 * @param path the path component of the url (e.g "/cookie_test.html") | 479 * @param path the path component of the url (e.g "/cookie_test.html") |
408 * @param key the key of the cookie | 480 * @param key the key of the cookie |
409 * @param value the value of the cookie | 481 * @param value the value of the cookie |
410 * @return the url which gets the response | 482 * @return the url which gets the response |
411 */ | 483 */ |
412 private String makeCookieUrl(TestWebServer webServer, String path, String ke y, String value) { | 484 private String makeCookieUrl(TestWebServer webServer, String path, String ke y, String value) { |
413 String response = ""; | 485 String response = ""; |
414 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>(); | 486 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>(); |
415 responseHeaders.add( | 487 responseHeaders.add( |
416 Pair.create("Set-Cookie", key + "=" + value + "; path=" + path)) ; | 488 Pair.create("Set-Cookie", key + "=" + value + "; path=" + path)) ; |
417 return webServer.setResponse(path, response, responseHeaders); | 489 return webServer.setResponse(path, response, responseHeaders); |
418 } | 490 } |
419 | 491 |
420 /** | 492 /** |
493 * Creates a response on the TestWebServer which attempts to set a cookie wh en establishing a | |
494 * WebSocket connection. | |
495 * @param webServer the webServer on which to create the response | |
496 * @param path the path component of the url (e.g "/cookie_test.html") | |
497 * @param key the key of the cookie | |
498 * @param value the value of the cookie | |
499 * @return the url which gets the response | |
500 */ | |
501 private String makeCookieWebSocketUrl( | |
502 TestWebServer webServer, String path, String key, String value) { | |
503 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>(); | |
504 responseHeaders.add(Pair.create("Set-Cookie", key + "=" + value + "; pat h=" + path)); | |
505 return webServer.setResponseForWebSocket(path, responseHeaders); | |
506 } | |
507 | |
508 /** | |
421 * Creates a response on the TestWebServer which contains a script tag with an external src. | 509 * Creates a response on the TestWebServer which contains a script tag with an external src. |
422 * @param webServer the webServer on which to create the response | 510 * @param webServer the webServer on which to create the response |
423 * @param path the path component of the url (e.g "/my_thing_with_script.ht ml") | 511 * @param path the path component of the url (e.g "/my_thing_with_script.ht ml") |
424 * @param url the url which which should appear as the src of the script ta g. | 512 * @param url the url which which should appear as the src of the script ta g. |
425 * @return the url which gets the response | 513 * @return the url which gets the response |
426 */ | 514 */ |
427 private String makeScriptLinkUrl(TestWebServer webServer, String path, Strin g url) { | 515 private String makeScriptLinkUrl(TestWebServer webServer, String path, Strin g url) { |
428 String responseStr = "<html><head><title>Content!</title></head>" | 516 String responseStr = "<html><head><title>Content!</title></head>" |
429 + "<body><script src=" + url + "></script></body></html>"; | 517 + "<body><script src=" + url + "></script></body></html>"; |
430 return webServer.setResponse(path, responseStr, null); | 518 return webServer.setResponse(path, responseStr, null); |
431 } | 519 } |
432 | 520 |
521 /** | |
522 * Creates a response on the TestWebServer which contains a script establish ing a WebSocket | |
523 * connection. | |
524 * @param webServer the webServer on which to create the response | |
525 * @param path the path component of the url (e.g "/my_thing_with_script.ht ml") | |
526 * @param url the url which which should appear as the src of the script ta g. | |
527 * @return the url which gets the response | |
528 */ | |
529 private String makeWebSocketScriptUrl(TestWebServer webServer, String path, String url) { | |
530 String responseStr = "<html><head><title>Content!</title></head>" | |
531 + "<body><script>\n" | |
532 + "let ws = new WebSocket('" + url.replaceAll("^http", "ws") + " ');\n" | |
533 + "let hasOpened = false;\n" | |
534 + "ws.onopen = () => hasOpened = true;\n" | |
535 + "</script></body></html>"; | |
536 return webServer.setResponse(path, responseStr, null); | |
537 } | |
538 | |
433 @MediumTest | 539 @MediumTest |
434 @Feature({"AndroidWebView", "Privacy"}) | 540 @Feature({"AndroidWebView", "Privacy"}) |
435 public void testThirdPartyJavascriptCookie() throws Throwable { | 541 public void testThirdPartyJavascriptCookie() throws Throwable { |
436 TestWebServer webServer = TestWebServer.start(); | 542 TestWebServer webServer = TestWebServer.start(); |
437 try { | 543 try { |
438 // This test again uses 127.0.0.1/localhost trick to simulate a thir d party. | 544 // This test again uses 127.0.0.1/localhost trick to simulate a thir d party. |
439 ThirdPartyCookiesTestHelper thirdParty = | 545 ThirdPartyCookiesTestHelper thirdParty = |
440 new ThirdPartyCookiesTestHelper(webServer); | 546 new ThirdPartyCookiesTestHelper(webServer); |
441 | 547 |
442 mCookieManager.setAcceptCookie(true); | 548 mCookieManager.setAcceptCookie(true); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 return makeExpiringCookieMs(cookie, secondsTillExpiry * 1000); | 792 return makeExpiringCookieMs(cookie, secondsTillExpiry * 1000); |
687 } | 793 } |
688 | 794 |
689 @SuppressWarnings("deprecation") | 795 @SuppressWarnings("deprecation") |
690 private String makeExpiringCookieMs(String cookie, int millisecondsTillExpir y) { | 796 private String makeExpiringCookieMs(String cookie, int millisecondsTillExpir y) { |
691 Date date = new Date(); | 797 Date date = new Date(); |
692 date.setTime(date.getTime() + millisecondsTillExpiry); | 798 date.setTime(date.getTime() + millisecondsTillExpiry); |
693 return cookie + "; expires=" + date.toGMTString(); | 799 return cookie + "; expires=" + date.toGMTString(); |
694 } | 800 } |
695 } | 801 } |
OLD | NEW |