Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

4.4 KB
Raw Blame
package opensuite.engine;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * Tests how the Live crawl view finds the engine's browsers and picks the
 * application pages out of what a browser has open. Nothing here starts a
 * browser or touches the network.
 */
public final class CrawlWatchTest {

    /** A browser's own folder, holding the port file when a port is given. */
    private static Path folder(String port) {
        try {
            Path dir = Files.createTempDirectory("cw");
            if (port != null) Files.writeString(dir.resolve("DevToolsActivePort"), port + "\n/devtools/browser/abc\n");
            return dir;
        } catch (java.io.IOException e) { throw new RuntimeException(e); }
    }

    public void run() {
        Harness.suite("live crawl view");

        Harness.test("a browser's port is read from the file it writes in its own folder", () -> {
            Path dir = folder("45595");
            String line = "/opt/chrome --headless=new " + CrawlWatch.PORT_ARG + " --user-data-dir=" + dir + " data:,";
            Expect.equal(CrawlWatch.portOf(line), Integer.valueOf(45595), "port");
        });

        Harness.test("a helper process of the browser is not taken for a browser", () -> {
            Path dir = folder("45595");
            String line = "/opt/chrome --type=renderer " + CrawlWatch.PORT_ARG + " --user-data-dir=" + dir;
            Expect.isTrue(CrawlWatch.portOf(line) == null, "no port");
        });

        Harness.test("a browser that was not started to be watched is left alone", () -> {
            Path dir = folder("45595");
            Expect.isTrue(CrawlWatch.portOf("/opt/chrome --remote-debugging-pipe --user-data-dir=" + dir) == null, "no port");
        });

        Harness.test("a browser that has not written its port yet gives none", () -> {
            Path dir = folder(null);
            Expect.isTrue(CrawlWatch.portOf("/opt/chrome " + CrawlWatch.PORT_ARG + " --user-data-dir=" + dir) == null, "no port");
        });

        Harness.test("only the application's pages are picked out of what a browser has open", () -> {
            String json = "[{\"id\":\"A\",\"title\":\"Mapper\",\"type\":\"other\",\"url\":\"about:blank\","
                    + "\"webSocketDebuggerUrl\":\"ws://127.0.0.1:1/devtools/page/A\"},"
                    + "{\"id\":\"B\",\"title\":\"Orders\",\"type\":\"page\",\"url\":\"http://app.test/orders\","
                    + "\"webSocketDebuggerUrl\":\"ws://127.0.0.1:1/devtools/page/B\"},"
                    + "{\"id\":\"C\",\"title\":\"Toolbar\",\"type\":\"browser_ui\",\"url\":\"chrome://toolbar/\","
                    + "\"webSocketDebuggerUrl\":\"ws://127.0.0.1:1/devtools/page/C\"},"
                    + "{\"id\":\"D\",\"title\":\"\",\"type\":\"page\",\"url\":\"data:,\","
                    + "\"webSocketDebuggerUrl\":\"ws://127.0.0.1:1/devtools/page/D\"}]";
            List<CrawlWatch.Page> pages = CrawlWatch.pagesIn(json);
            Expect.equal(pages.size(), 1, "one page");
            Expect.equal(pages.get(0).url(), "http://app.test/orders", "address");
            Expect.equal(pages.get(0).title(), "Orders", "title");
            Expect.equal(pages.get(0).socket(), "ws://127.0.0.1:1/devtools/page/B", "socket");
        });

        Harness.test("an answer that is not a list of pages gives none", () -> {
            Expect.equal(CrawlWatch.pagesIn("not json").size(), 0, "not json");
            Expect.equal(CrawlWatch.pagesIn("{\"error\":\"x\"}").size(), 0, "an object");
        });


        Harness.test("the view stays with one browser long enough to be read", () -> {
            java.util.Map<String, Long> moved = new java.util.HashMap<>();
            moved.put("a", 1000L);
            moved.put("b", 3000L);
            Expect.equal(CrawlWatch.choose(null, 0, 3000, moved), "b", "with nothing shown, the page that moved last");
            Expect.equal(CrawlWatch.choose("a", 2000, 3000, moved), "a", "the page on show is kept for a while");
            Expect.equal(CrawlWatch.choose("a", 2000, 2000 + CrawlWatch.DWELL_MS, moved), "b", "then the view may move on");
            moved.remove("a");
            Expect.equal(CrawlWatch.choose("a", 2000, 2100, moved), "b", "a page that has closed is left at once");
            Expect.equal(CrawlWatch.choose("a", 2000, 2100, new java.util.HashMap<>()), null, "no pages, nothing to show");
        });
    }
}