@MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyJavascriptCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      // This test again uses 127.0.0.1/localhost trick to simulate a third party.
      webServer = new TestWebServer(false);
      ThirdPartyCookiesTestHelper thirdParty = new ThirdPartyCookiesTestHelper(webServer);

      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      // When third party cookies are disabled...
      thirdParty.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(thirdParty.getSettings().getAcceptThirdPartyCookies());

      // ...we can't set third party cookies.
      thirdParty.assertThirdPartyIFrameCookieResult("1", false);

      // When third party cookies are enabled...
      thirdParty.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(thirdParty.getSettings().getAcceptThirdPartyCookies());

      // ...we can set third party cookies.
      thirdParty.assertThirdPartyIFrameCookieResult("2", true);
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
Example #2
0
 @Override
 public void tearDown() throws Exception {
   if (mWebServer != null) {
     mWebServer.shutdown();
   }
   super.tearDown();
 }
Example #3
0
  @Feature({"AndroidWebView", "Downloads"})
  @SmallTest
  public void testDownload() throws Throwable {
    AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
    AwContents awContents = testView.getAwContents();

    final String data = "download data";
    final String contentDisposition = "attachment;filename=\"download.txt\"";
    final String mimeType = "text/plain";

    List<Pair<String, String>> downloadHeaders = new ArrayList<Pair<String, String>>();
    downloadHeaders.add(Pair.create("Content-Disposition", contentDisposition));
    downloadHeaders.add(Pair.create("Content-Type", mimeType));
    downloadHeaders.add(Pair.create("Content-Length", Integer.toString(data.length())));

    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);
      final String pageUrl = webServer.setResponse("/download.txt", data, downloadHeaders);
      final OnDownloadStartHelper downloadStartHelper = mContentsClient.getOnDownloadStartHelper();
      final int callCount = downloadStartHelper.getCallCount();
      loadUrlAsync(awContents, pageUrl);
      downloadStartHelper.waitForCallback(callCount);

      assertEquals(pageUrl, downloadStartHelper.getUrl());
      assertEquals(contentDisposition, downloadStartHelper.getContentDisposition());
      assertEquals(mimeType, downloadStartHelper.getMimeType());
      assertEquals(data.length(), downloadStartHelper.getContentLength());
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      // In theory we need two servers to test this, one server ('the first party')
      // which returns a response with a link to a second server ('the third party')
      // at different origin. This second server attempts to set a cookie which should
      // fail if AcceptThirdPartyCookie() is false.
      // Strictly according to the letter of RFC6454 it should be possible to set this
      // situation up with two TestServers on different ports (these count as having
      // different origins) but Chrome is not strict about this and does not check the
      // port. Instead we cheat making some of the urls come from localhost and some
      // from 127.0.0.1 which count (both in theory and pratice) as having different
      // origins.
      webServer = new TestWebServer(false);

      // Turn global allow on.
      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      // When third party cookies are disabled...
      mAwContents.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(mAwContents.getSettings().getAcceptThirdPartyCookies());

      // ...we can't set third party cookies.
      // First on the third party server we create a url which tries to set a cookie.
      String cookieUrl =
          toThirdPartyUrl(makeCookieUrl(webServer, "/cookie_1.js", "test1", "value1"));
      // Then we create a url on the first party server which links to the first url.
      String url = makeScriptLinkUrl(webServer, "/content_1.html", cookieUrl);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      assertNull(mCookieManager.getCookie(cookieUrl));

      // When third party cookies are enabled...
      mAwContents.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(mAwContents.getSettings().getAcceptThirdPartyCookies());

      // ...we can set third party cookies.
      cookieUrl = toThirdPartyUrl(makeCookieUrl(webServer, "/cookie_2.js", "test2", "value2"));
      url = makeScriptLinkUrl(webServer, "/content_2.html", cookieUrl);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      waitForCookie(cookieUrl);
      String cookie = mCookieManager.getCookie(cookieUrl);
      assertNotNull(cookie);
      validateCookies(cookie, "test2");
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testAcceptCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);
      String path = "/cookie_test.html";
      String responseStr = "<html><head><title>TEST!</title></head><body>HELLO!</body></html>";
      String url = webServer.setResponse(path, responseStr, null);

      mCookieManager.setAcceptCookie(false);
      assertFalse(mCookieManager.acceptCookie());

      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      setCookieWithJavaScript("test1", "value1");
      assertNull(mCookieManager.getCookie(url));

      List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
      responseHeaders.add(Pair.create("Set-Cookie", "header-test1=header-value1; path=" + path));
      url = webServer.setResponse(path, responseStr, responseHeaders);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      assertNull(mCookieManager.getCookie(url));

      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      url = webServer.setResponse(path, responseStr, null);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      setCookieWithJavaScript("test2", "value2");
      waitForCookie(url);
      String cookie = mCookieManager.getCookie(url);
      assertNotNull(cookie);
      validateCookies(cookie, "test2");

      responseHeaders = new ArrayList<Pair<String, String>>();
      responseHeaders.add(Pair.create("Set-Cookie", "header-test2=header-value2 path=" + path));
      url = webServer.setResponse(path, responseStr, responseHeaders);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      waitForCookie(url);
      cookie = mCookieManager.getCookie(url);
      assertNotNull(cookie);
      validateCookies(cookie, "test2", "header-test2");
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
Example #6
0
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testGetFavicon() throws Throwable {
    final AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
    final AwContents awContents = testView.getAwContents();

    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);

      final String faviconUrl =
          webServer.setResponseBase64(
              "/" + CommonResources.FAVICON_FILENAME,
              CommonResources.FAVICON_DATA_BASE64,
              CommonResources.getImagePngHeaders(false));
      final String pageUrl =
          webServer.setResponse("/favicon.html", CommonResources.FAVICON_STATIC_HTML, null);

      // The getFavicon will return the right icon a certain time after
      // the page load completes which makes it slightly hard to test.
      final Bitmap defaultFavicon = awContents.getFavicon();

      getAwSettingsOnUiThread(awContents).setImagesEnabled(true);
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);

      pollOnUiThread(
          new Callable<Boolean>() {
            @Override
            public Boolean call() {
              return awContents.getFavicon() != null
                  && !awContents.getFavicon().sameAs(defaultFavicon);
            }
          });

      final Object originalFaviconSource = (new URL(faviconUrl)).getContent();
      final Bitmap originalFavicon =
          BitmapFactory.decodeStream((InputStream) originalFaviconSource);
      assertNotNull(originalFavicon);

      assertTrue(awContents.getFavicon().sameAs(originalFavicon));

    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyCookiesArePerWebview() throws Throwable {
    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);
      mCookieManager.setAcceptCookie(true);
      mCookieManager.removeAllCookie();
      assertTrue(mCookieManager.acceptCookie());
      assertFalse(mCookieManager.hasCookies());

      ThirdPartyCookiesTestHelper helperOne = new ThirdPartyCookiesTestHelper(webServer);
      ThirdPartyCookiesTestHelper helperTwo = new ThirdPartyCookiesTestHelper(webServer);

      helperOne.getSettings().setAcceptThirdPartyCookies(false);
      helperTwo.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertFalse(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("1", false);
      helperTwo.assertThirdPartyIFrameCookieResult("2", false);

      helperTwo.getSettings().setAcceptThirdPartyCookies(true);
      assertFalse(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertTrue(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("3", false);
      helperTwo.assertThirdPartyIFrameCookieResult("4", true);

      helperOne.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertTrue(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("5", true);
      helperTwo.assertThirdPartyIFrameCookieResult("6", true);

      helperTwo.getSettings().setAcceptThirdPartyCookies(false);
      assertTrue(helperOne.getSettings().getAcceptThirdPartyCookies());
      assertFalse(helperTwo.getSettings().getAcceptThirdPartyCookies());
      helperOne.assertThirdPartyIFrameCookieResult("7", true);
      helperTwo.assertThirdPartyIFrameCookieResult("8", false);
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
Example #8
0
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testClearCacheMemoryAndDisk() throws Throwable {
    final AwTestContainerView testContainer = createAwTestContainerViewOnMainSync(mContentsClient);
    final AwContents awContents = testContainer.getAwContents();

    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);
      final String pagePath = "/clear_cache_test.html";
      List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>();
      // Set Cache-Control headers to cache this request. One century should be long enough.
      headers.add(Pair.create("Cache-Control", "max-age=3153600000"));
      headers.add(Pair.create("Last-Modified", "Wed, 3 Oct 2012 00:00:00 GMT"));
      final String pageUrl =
          webServer.setResponse(pagePath, "<html><body>foo</body></html>", headers);

      // First load to populate cache.
      clearCacheOnUiThread(awContents, true);
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
      assertEquals(1, webServer.getRequestCount(pagePath));

      // Load about:blank so next load is not treated as reload by webkit and force
      // revalidate with the server.
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), "about:blank");

      // No clearCache call, so should be loaded from cache.
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
      assertEquals(1, webServer.getRequestCount(pagePath));

      // Same as above.
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), "about:blank");

      // Clear cache, so should hit server again.
      clearCacheOnUiThread(awContents, true);
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
      assertEquals(2, webServer.getRequestCount(pagePath));
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
 @Override
 protected void tearDown() throws Exception {
   mWebServer.shutdown();
   super.tearDown();
 }
 @Override
 public void tearDown() throws Exception {
   mServer.shutdown();
   super.tearDown();
 }
Example #11
0
  @DisabledTest // Fails on android-one: crbug.com/540723
  @MediumTest
  @Feature({"Navigation"})
  public void testWindowOpenUrlSpoof() throws Exception {
    TestWebServer webServer = TestWebServer.start();
    try {
      // Make sure that we start with one tab.
      final TabModel model = getActivity().getTabModelSelector().getModel(false);

      final Semaphore urlServedSemaphore = new Semaphore(0);
      Runnable checkAction =
          new Runnable() {
            @Override
            public void run() {
              final Tab tab = TabModelUtils.getCurrentTab(model);

              // Make sure that we are showing the spoofed data and a blank URL.
              String url = getTabUrlOnUIThread(tab);
              boolean spoofedUrl = "".equals(url) || "about:blank".equals(url);
              assertTrue("URL Spoofed", spoofedUrl);
              assertEquals("Not showing mocked content", "\"Spoofed\"", getTabBodyText(tab));
              urlServedSemaphore.release();
            }
          };

      // Mock out the test URL
      final String mockedUrl =
          webServer.setResponseWithRunnableAction(
              "/mockme.html",
              "<html>"
                  + "  <head>"
                  + "    <meta name=\"viewport\""
                  + "        content=\"initial-scale=0.75,maximum-scale=0.75,user-scalable=no\">"
                  + "  </head>"
                  + "  <body>Real</body>"
                  + "</html>",
              null,
              checkAction);

      // Navigate to the spoofable URL
      loadUrl(
          UrlUtils.encodeHtmlDataUri(
              "<head>"
                  + "  <meta name=\"viewport\""
                  + "      content=\"initial-scale=0.5,maximum-scale=0.5,user-scalable=no\">"
                  + "</head>"
                  + "<script>"
                  + "  function spoof() {"
                  + "    var w = open();"
                  + "    w.opener = null;"
                  + "    w.document.write('Spoofed');"
                  + "    w.location = '"
                  + mockedUrl
                  + "'"
                  + "  }"
                  + "</script>"
                  + "<body id='body' onclick='spoof()'></body>"));
      assertWaitForPageScaleFactorMatch(0.5f);

      // Click the page, which triggers the URL load.
      DOMUtils.clickNode(this, getActivity().getCurrentContentViewCore(), "body");

      // Wait for the proper URL to be served.
      assertTrue(urlServedSemaphore.tryAcquire(5, TimeUnit.SECONDS));

      // Wait for the url to change.
      final Tab tab = TabModelUtils.getCurrentTab(model);
      assertWaitForPageScaleFactorMatch(0.75f);
      CriteriaHelper.pollForCriteria(
          new Criteria("Page url didn't change") {
            @Override
            public boolean isSatisfied() {
              return mockedUrl.equals(getTabUrlOnUIThread(tab));
            }
          },
          5000,
          50);

      // Make sure that we're showing new content now.
      assertEquals("Still showing spoofed data", "\"Real\"", getTabBodyText(tab));
    } finally {
      webServer.shutdown();
    }
  }