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 private String thirdPartyCookieForWebSocket(boolean acceptCookie) throws Thr
owable { |
| 407 TestWebServer webServer = TestWebServer.start(); |
| 408 try { |
| 409 // Turn global allow on. |
| 410 mCookieManager.setAcceptCookie(true); |
| 411 assertTrue(mCookieManager.acceptCookie()); |
| 412 |
| 413 // Sets the per-WebView value. |
| 414 mAwContents.getSettings().setAcceptThirdPartyCookies(acceptCookie); |
| 415 assertEquals(acceptCookie, mAwContents.getSettings().getAcceptThirdP
artyCookies()); |
| 416 |
| 417 // |cookieUrl| is a third-party url that sets a cookie on response. |
| 418 String cookieUrl = toThirdPartyUrl( |
| 419 makeCookieWebSocketUrl(webServer, "/cookie_1", "test1", "val
ue1")); |
| 420 // This html file includes a script establishing a WebSocket connect
ion to |cookieUrl|. |
| 421 String url = makeWebSocketScriptUrl(webServer, "/content_1.html", co
okieUrl); |
| 422 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
url); |
| 423 final String connecting = "0"; // WebSocket.CONNECTING |
| 424 final String closed = "3"; // WebSocket.CLOSED |
| 425 String readyState = connecting; |
| 426 WebContents webContents = mAwContents.getWebContents(); |
| 427 while (!readyState.equals(closed)) { |
| 428 readyState = JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 429 webContents, "ws.readyState"); |
| 430 } |
| 431 assertEquals("true", |
| 432 JavaScriptUtils.executeJavaScriptAndWaitForResult(webContent
s, "hasOpened")); |
| 433 return mCookieManager.getCookie(cookieUrl); |
| 434 } finally { |
| 435 webServer.shutdown(); |
| 436 } |
| 437 } |
| 438 |
| 439 @MediumTest |
| 440 @Feature({"AndroidWebView", "Privacy"}) |
| 441 public void testThirdPartyCookieForWebSocketDisabledCase() throws Throwable
{ |
| 442 assertNull(thirdPartyCookieForWebSocket(false)); |
| 443 } |
| 444 |
| 445 @MediumTest |
| 446 @Feature({"AndroidWebView", "Privacy"}) |
| 447 public void testThirdPartyCookieForWebSocketEnabledCase() throws Throwable { |
| 448 assertEquals("test1=value1", thirdPartyCookieForWebSocket(true)); |
| 449 } |
| 450 |
404 /** | 451 /** |
405 * Creates a response on the TestWebServer which attempts to set a cookie wh
en fetched. | 452 * 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 | 453 * @param webServer the webServer on which to create the response |
407 * @param path the path component of the url (e.g "/cookie_test.html") | 454 * @param path the path component of the url (e.g "/cookie_test.html") |
408 * @param key the key of the cookie | 455 * @param key the key of the cookie |
409 * @param value the value of the cookie | 456 * @param value the value of the cookie |
410 * @return the url which gets the response | 457 * @return the url which gets the response |
411 */ | 458 */ |
412 private String makeCookieUrl(TestWebServer webServer, String path, String ke
y, String value) { | 459 private String makeCookieUrl(TestWebServer webServer, String path, String ke
y, String value) { |
413 String response = ""; | 460 String response = ""; |
414 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String,
String>>(); | 461 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String,
String>>(); |
415 responseHeaders.add( | 462 responseHeaders.add( |
416 Pair.create("Set-Cookie", key + "=" + value + "; path=" + path))
; | 463 Pair.create("Set-Cookie", key + "=" + value + "; path=" + path))
; |
417 return webServer.setResponse(path, response, responseHeaders); | 464 return webServer.setResponse(path, response, responseHeaders); |
418 } | 465 } |
419 | 466 |
420 /** | 467 /** |
| 468 * Creates a response on the TestWebServer which attempts to set a cookie wh
en establishing a |
| 469 * WebSocket connection. |
| 470 * @param webServer the webServer on which to create the response |
| 471 * @param path the path component of the url (e.g "/cookie_test.html") |
| 472 * @param key the key of the cookie |
| 473 * @param value the value of the cookie |
| 474 * @return the url which gets the response |
| 475 */ |
| 476 private String makeCookieWebSocketUrl( |
| 477 TestWebServer webServer, String path, String key, String value) { |
| 478 List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String,
String>>(); |
| 479 responseHeaders.add(Pair.create("Set-Cookie", key + "=" + value + "; pat
h=" + path)); |
| 480 return webServer.setResponseForWebSocket(path, responseHeaders); |
| 481 } |
| 482 |
| 483 /** |
421 * Creates a response on the TestWebServer which contains a script tag with
an external src. | 484 * 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 | 485 * @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") | 486 * @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. | 487 * @param url the url which which should appear as the src of the script ta
g. |
425 * @return the url which gets the response | 488 * @return the url which gets the response |
426 */ | 489 */ |
427 private String makeScriptLinkUrl(TestWebServer webServer, String path, Strin
g url) { | 490 private String makeScriptLinkUrl(TestWebServer webServer, String path, Strin
g url) { |
428 String responseStr = "<html><head><title>Content!</title></head>" | 491 String responseStr = "<html><head><title>Content!</title></head>" |
429 + "<body><script src=" + url + "></script></body></html>"; | 492 + "<body><script src=" + url + "></script></body></html>"; |
430 return webServer.setResponse(path, responseStr, null); | 493 return webServer.setResponse(path, responseStr, null); |
431 } | 494 } |
432 | 495 |
| 496 /** |
| 497 * Creates a response on the TestWebServer which contains a script establish
ing a WebSocket |
| 498 * connection. |
| 499 * @param webServer the webServer on which to create the response |
| 500 * @param path the path component of the url (e.g "/my_thing_with_script.ht
ml") |
| 501 * @param url the url which which should appear as the src of the script ta
g. |
| 502 * @return the url which gets the response |
| 503 */ |
| 504 private String makeWebSocketScriptUrl(TestWebServer webServer, String path,
String url) { |
| 505 String responseStr = "<html><head><title>Content!</title></head>" |
| 506 + "<body><script>\n" |
| 507 + "let ws = new WebSocket('" + url.replaceAll("^http", "ws") + "
');\n" |
| 508 + "let hasOpened = false;\n" |
| 509 + "ws.onopen = () => hasOpened = true;\n" |
| 510 + "</script></body></html>"; |
| 511 return webServer.setResponse(path, responseStr, null); |
| 512 } |
| 513 |
433 @MediumTest | 514 @MediumTest |
434 @Feature({"AndroidWebView", "Privacy"}) | 515 @Feature({"AndroidWebView", "Privacy"}) |
435 public void testThirdPartyJavascriptCookie() throws Throwable { | 516 public void testThirdPartyJavascriptCookie() throws Throwable { |
436 TestWebServer webServer = TestWebServer.start(); | 517 TestWebServer webServer = TestWebServer.start(); |
437 try { | 518 try { |
438 // This test again uses 127.0.0.1/localhost trick to simulate a thir
d party. | 519 // This test again uses 127.0.0.1/localhost trick to simulate a thir
d party. |
439 ThirdPartyCookiesTestHelper thirdParty = | 520 ThirdPartyCookiesTestHelper thirdParty = |
440 new ThirdPartyCookiesTestHelper(webServer); | 521 new ThirdPartyCookiesTestHelper(webServer); |
441 | 522 |
442 mCookieManager.setAcceptCookie(true); | 523 mCookieManager.setAcceptCookie(true); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 return makeExpiringCookieMs(cookie, secondsTillExpiry * 1000); | 767 return makeExpiringCookieMs(cookie, secondsTillExpiry * 1000); |
687 } | 768 } |
688 | 769 |
689 @SuppressWarnings("deprecation") | 770 @SuppressWarnings("deprecation") |
690 private String makeExpiringCookieMs(String cookie, int millisecondsTillExpir
y) { | 771 private String makeExpiringCookieMs(String cookie, int millisecondsTillExpir
y) { |
691 Date date = new Date(); | 772 Date date = new Date(); |
692 date.setTime(date.getTime() + millisecondsTillExpiry); | 773 date.setTime(date.getTime() + millisecondsTillExpiry); |
693 return cookie + "; expires=" + date.toGMTString(); | 774 return cookie + "; expires=" + date.toGMTString(); |
694 } | 775 } |
695 } | 776 } |
OLD | NEW |