Search in sources :

Example 1 with UnexpectedPage

use of com.gargoylesoftware.htmlunit.UnexpectedPage in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method click_unexpectedPageAttachmentHandlerHandleContentType.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_unexpectedPageAttachmentHandlerHandleContentType() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n" + "  <a href='" + URL_SECOND + "' id='link'>link</a>\n" + "</body>\n" + "</html>";
    getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
    final LinkedList<Page> pages = new LinkedList<Page>();
    getWebClient().setAttachmentHandler(new AttachmentHandler() {

        @Override
        public boolean isAttachment(final WebResponse response) {
            return MimeType.APPLICATION_JSON.equalsIgnoreCase(response.getResponseHeaderValue(HttpHeader.CONTENT_TYPE));
        }

        @Override
        public void handleAttachment(final Page page) {
            pages.add(page);
        }
    });
    try {
        final HtmlPage page = loadPage(html);
        assertEquals(1, getWebClient().getWebWindows().size());
        page.getElementById("link").click();
        final WebWindow newWindow = getWebClient().getWebWindows().get(getWebClient().getWebWindows().size() - 1);
        assertTrue(newWindow.getEnclosedPage() instanceof UnexpectedPage);
        assertEquals(1, pages.size());
    } finally {
        getWebClient().setAttachmentHandler(null);
    }
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 2 with UnexpectedPage

use of com.gargoylesoftware.htmlunit.UnexpectedPage in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method click_unexpectedPageAttachmentHandlerDoesNotHandleContentType.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_unexpectedPageAttachmentHandlerDoesNotHandleContentType() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n" + "  <a href='" + URL_SECOND + "' id='link'>link</a>\n" + "</body>\n" + "</html>";
    getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
    final LinkedList<Page> pages = new LinkedList<Page>();
    getWebClient().setAttachmentHandler(new AttachmentHandler() {

        @Override
        public void handleAttachment(final Page page) {
            pages.add(page);
        }
    });
    try {
        final HtmlPage page = loadPage(html);
        assertEquals(1, getWebClient().getWebWindows().size());
        page.getElementById("link").click();
        assertEquals(1, getWebClient().getWebWindows().size());
        assertTrue(getWebClient().getCurrentWindow().getEnclosedPage() instanceof UnexpectedPage);
        assertEquals(0, pages.size());
    } finally {
        getWebClient().setAttachmentHandler(null);
    }
}
Also used : UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with UnexpectedPage

use of com.gargoylesoftware.htmlunit.UnexpectedPage in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method clickWithDownloadAttributeDataUrl.

/**
 * Not testable with Selenium.
 *
 * @exception Exception If the test fails
 */
@Test
@Alerts(DEFAULT = { "1", "First", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAAL" + "GPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1l" + "bnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C" + "7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI" + "97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" }, IE = { "0", "First", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAAL" + "GPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1l" + "bnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C" + "7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI" + "97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" })
@HtmlUnitNYI(IE = { "1", "First", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAAL" + "GPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1l" + "bnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C" + "7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI" + "97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" })
public void clickWithDownloadAttributeDataUrl() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>First</title>\n" + "</head>\n" + "<body>\n" + "  <a id='clickMe' href='data:image/png;base64," + "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP" + "C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA" + "AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J" + "REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq" + "ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0" + "vr4MkhoXe0rZigAAAABJRU5ErkJggg==' download='lora.html'>Click Me</a>\n" + "</body></html>";
    getMockWebConnection().setResponse(URL_SECOND, "<head><title>Second</title>");
    final int windowsSize = getWebClient().getWebWindows().size();
    final HtmlPage page = loadPage(html);
    page.getElementById("clickMe").click();
    assertEquals("Should have opened a new window", windowsSize + Integer.parseInt(getExpectedAlerts()[0]), getWebClient().getWebWindows().size());
    assertEquals("Should not have navigated away", getExpectedAlerts()[1], page.getTitleText());
    final WebWindow dataWindow = getWebClient().getWebWindows().get(getWebClient().getWebWindows().size() - 1);
    final Page dataPage = dataWindow.getEnclosedPage();
    assertTrue("Should be an UnexpectedPage", dataPage instanceof UnexpectedPage);
    try (InputStream resultInputStream = ((UnexpectedPage) dataPage).getInputStream()) {
        final ImageInputStream resultImageIS = ImageIO.createImageInputStream(resultInputStream);
        final BufferedImage resultBufferedIIS = ImageIO.read(resultImageIS);
        compareImages(getExpectedAlerts()[2], null, resultBufferedIIS);
    }
}
Also used : UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) BufferedImage(java.awt.image.BufferedImage) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts) HtmlUnitNYI(com.gargoylesoftware.htmlunit.junit.BrowserRunner.HtmlUnitNYI)

Example 4 with UnexpectedPage

use of com.gargoylesoftware.htmlunit.UnexpectedPage in project FunctionalTests by VEuPathDB.

the class SmokeTests method assertJsonStatusMessageIsOK.

/**
 * Assert request returns 200 OK for the given json url
 */
private void assertJsonStatusMessageIsOK(String url) throws Exception {
    try {
        WebRequest request = new WebRequest(new URL(url), HttpMethod.HEAD);
        request.setAdditionalHeader("Content-type", "application/json");
        UnexpectedPage page = (UnexpectedPage) browser.getPage(request);
        WebResponse response = page.getWebResponse();
        assertEquals(response.getStatusCode(), HTTP_RESPONSE_OK, "Wrong Status " + response.getStatusMessage() + " for " + url + " .");
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) URL(java.net.URL)

Example 5 with UnexpectedPage

use of com.gargoylesoftware.htmlunit.UnexpectedPage in project jenkins by jenkinsci.

the class DirectoryBrowserSupportTest method junctionAndSymlink_outsideWorkspace_areNotAllowed_windowsJunction.

// The hard links (mklink /H) to file are impossible to be detected and will allow a user to retrieve any file in the system
// to achieve that they should already have access to the system or the Script Console.
@Test
@Issue("SECURITY-904")
public void junctionAndSymlink_outsideWorkspace_areNotAllowed_windowsJunction() throws Exception {
    Assume.assumeTrue(Functions.isWindows());
    FreeStyleProject p = j.createFreeStyleProject();
    File secretsFolder = new File(j.jenkins.getRootDir(), "secrets");
    File secretTarget = new File(secretsFolder, "goal.txt");
    String secretContent = "secret";
    FileUtils.write(secretTarget, secretContent, StandardCharsets.UTF_8);
    /*
         *  secrets/
         *      goal.txt
         *  workspace/
         *      intermediateFolder/
         *          public2.key
         *          otherFolder/
         *              to_secret3s -> symlink ../../../../secrets/
         *              to_secret3j -> junction ../../../../secrets/
         *          to_secret2s -> symlink ../../../secrets/
         *          to_secret2j -> junction ../../../secrets/
         *          to_secret_goal2 -> symlink ../../../secrets/goal.txt
         *      public1.key
         *      to_secret1s -> symlink ../../secrets/
         *      to_secret1j -> junction ../../secrets/
         *      to_secret_goal1 -> symlink ../../secrets/goal.txt
         *
         */
    String script = loadContentFromResource("outsideWorkspaceStructureWithJunctions.bat");
    p.getBuildersList().add(new BatchFile(script));
    j.buildAndAssertSuccess(p);
    JenkinsRule.WebClient wc = j.createWebClient();
    wc.getOptions().setThrowExceptionOnFailingStatusCode(false);
    {
        // workspace root must be reachable (regular case)
        Page page = wc.goTo(p.getUrl() + "ws/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_OK));
        String workspaceContent = page.getWebResponse().getContentAsString();
        assertThat(workspaceContent, allOf(containsString("public1.key"), containsString("intermediateFolder"), not(containsString("to_secrets1j")), not(containsString("to_secrets1s")), not(containsString("to_secrets_goal1")), not(containsString("to_secrets2")), not(containsString("to_secrets_goal2"))));
    }
    {
        // to_secrets1s not reachable
        Page page = wc.goTo(p.getUrl() + "ws/to_secrets1s/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // to_secrets1j not reachable
        Page page = wc.goTo(p.getUrl() + "ws/to_secrets1j/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // to_secrets_goal1 not reachable
        Page page = wc.goTo(p.getUrl() + "ws/to_secrets_goal1/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // intermediateFolder must be reachable (regular case)
        Page page = wc.goTo(p.getUrl() + "ws/intermediateFolder/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_OK));
        String workspaceContent = page.getWebResponse().getContentAsString();
        assertThat(workspaceContent, allOf(not(containsString("to_secrets1")), not(containsString("to_secrets_goal1")), not(containsString("to_secrets2s")), not(containsString("to_secrets2j")), not(containsString("to_secrets_goal2"))));
    }
    {
        // to_secrets2s not reachable
        Page page = wc.goTo(p.getUrl() + "ws/intermediateFolder/to_secrets2s/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // to_secrets2j not reachable
        Page page = wc.goTo(p.getUrl() + "ws/intermediateFolder/to_secrets2j/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // using symbolic in the intermediate path
        Page page = wc.goTo(p.getUrl() + "ws/intermediateFolder/to_secrets2s/master.key", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // using symbolic in the intermediate path
        Page page = wc.goTo(p.getUrl() + "ws/intermediateFolder/to_secrets2j/master.key", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    {
        // to_secrets_goal2 not reachable
        Page page = wc.goTo(p.getUrl() + "ws/intermediateFolder/to_secrets_goal2/", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_NOT_FOUND));
    }
    // pattern search feature
    {
        // the pattern allow us to search inside the files / folders,
        // without the patch the master.key from inside the outside symlinks would have been linked
        Page page = wc.goTo(p.getUrl() + "ws/**/*.key", null);
        assertThat(page.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_OK));
        String workspaceContent = page.getWebResponse().getContentAsString();
        assertThat(workspaceContent, allOf(not(containsString("master.key")), containsString("public1.key"), containsString("public2.key"), containsString("intermediateFolder"), not(containsString("otherFolder")), not(containsString("to_secrets3j")), not(containsString("to_secrets3s")), not(containsString("to_secrets2j")), not(containsString("to_secrets2s")), not(containsString("to_secrets1j")), not(containsString("to_secrets1s"))));
    }
    // zip feature
    {
        // all the outside folders / files are not included in the zip
        Page zipPage = wc.goTo(p.getUrl() + "ws/*zip*/ws.zip", null);
        assertThat(zipPage.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_OK));
        List<String> entryNames = getListOfEntriesInDownloadedZip((UnexpectedPage) zipPage);
        assertThat(entryNames, containsInAnyOrder(p.getName() + "/intermediateFolder/public2.key", p.getName() + "/public1.key"));
    }
    {
        // all the outside folders / files are not included in the zip
        Page zipPage = wc.goTo(p.getUrl() + "ws/intermediateFolder/*zip*/intermediateFolder.zip", null);
        assertThat(zipPage.getWebResponse().getStatusCode(), equalTo(HttpURLConnection.HTTP_OK));
        List<String> entryNames = getListOfEntriesInDownloadedZip((UnexpectedPage) zipPage);
        assertThat(entryNames, contains("intermediateFolder/public2.key"));
    }
}
Also used : BatchFile(hudson.tasks.BatchFile) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) List(java.util.List) ArrayList(java.util.ArrayList) ExtensionList(hudson.ExtensionList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) ZipFile(java.util.zip.ZipFile) VirtualFile(jenkins.util.VirtualFile) BatchFile(hudson.tasks.BatchFile) File(java.io.File) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

UnexpectedPage (com.gargoylesoftware.htmlunit.UnexpectedPage)11 Test (org.junit.Test)10 Page (com.gargoylesoftware.htmlunit.Page)9 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)4 AttachmentHandler (com.gargoylesoftware.htmlunit.attachment.AttachmentHandler)4 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 BatchFile (hudson.tasks.BatchFile)4 File (java.io.File)4 LinkedList (java.util.LinkedList)4 ZipFile (java.util.zip.ZipFile)4 VirtualFile (jenkins.util.VirtualFile)4 Issue (org.jvnet.hudson.test.Issue)4 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)3 ExtensionList (hudson.ExtensionList)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)3 InputStream (java.io.InputStream)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1