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.

6.9 KB
Raw Blame
package opensuite.core;

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

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * A tool loaded at run time can leave: what it registered with the context comes
 * off cleanly, and the lists it shares with the threads that save and restore a
 * project are safe to walk while the window thread changes them.
 */
public final class SuiteContextRegistryTest {

    /** A tool with nothing to it, named as given. */
    private static ToolModule tool(String name) {
        return new ToolModule() {
            @Override public String name() { return name; }
            @Override public Tab[] tabs() { return new Tab[0]; }
        };
    }

    private static Issue issue() {
        return new Issue("A finding", Issue.Severity.HIGH, Issue.Confidence.FIRM,
                new HttpService("example.com", 443, true), "/", "", "d", "", "", null, null);
    }

    private static LogEntry entry() {
        return new LogEntry("proxy", new HttpService("example.com", 443, true),
                new HttpMessage(Bytes.bytes("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")));
    }

    public void run() {
        Harness.suite("suite context registry");

        Harness.test("a tool entered twice is entered once, and taking it away takes every copy", () -> {
            SuiteContext ctx = new SuiteContext();
            ToolModule a = tool("a");
            ctx.registerTool(a);
            ctx.registerTool(a);
            Expect.equal(ctx.tools().size(), 1, "entered once");
            ctx.unregisterTool(a);
            Expect.isTrue(ctx.tools().isEmpty(), "nothing left to ask for a saved state, so nothing of it is held");
        });

        Harness.test("a tool can be taken away", () -> {
            SuiteContext ctx = new SuiteContext();
            ToolModule a = tool("a"), b = tool("b");
            ctx.registerTool(a);
            ctx.registerTool(b);
            Expect.equal(ctx.tools().size(), 2, "tools registered");
            ctx.unregisterTool(a);
            Expect.equal(ctx.tools().size(), 1, "tools after taking one away");
            Expect.equal(ctx.tools().get(0).name(), "b", "the other tool stays");
            ctx.unregisterTool(a);   // twice is harmless
            Expect.equal(ctx.tools().size(), 1, "taking it away again changes nothing");
        });

        Harness.test("the list of tools is safe to walk while another thread changes it", () -> {
            SuiteContext ctx = new SuiteContext();
            ctx.registerTool(tool("a"));
            final Throwable[] failure = { null };
            Thread walker = new Thread(() -> {
                try {
                    for (int i = 0; i < 2000; i++) for (ToolModule t : ctx.tools()) Expect.notNull(t.name(), "a name");
                } catch (Throwable t) { failure[0] = t; }
            }, "registry-walker");
            walker.start();
            try {
                for (int i = 0; i < 2000; i++) {
                    ToolModule t = tool("t" + i);
                    ctx.registerTool(t);
                    ctx.unregisterTool(t);
                }
                walker.join(10_000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            Expect.isNull(failure[0], "no failure while walking the list: " + failure[0]);
            Expect.isFalse(walker.isAlive(), "the walk finished");
        });

        Harness.test("a sink can be taken away", () -> {
            SuiteContext ctx = new SuiteContext();
            AtomicInteger received = new AtomicInteger();
            ctx.registerSink("example", (svc, req, resp, cap) -> received.incrementAndGet());
            Expect.isTrue(ctx.hasSink("example") && ctx.sinkNames().contains("example"), "registered");
            ctx.unregisterSink("example");
            Expect.isFalse(ctx.hasSink("example"), "no longer a destination");
            Expect.isFalse(ctx.sinkNames().contains("example"), "no longer in the Send to menus");
            HttpService svc = new HttpService("example.com", 443, true);
            ctx.sendTo("example", svc, null, null, null);
            Expect.equal(received.get(), 0, "a send to it reaches nothing");
        });

        Harness.test("every kind of listener can be taken away", () -> {
            SuiteContext ctx = new SuiteContext();
            AtomicInteger calls = new AtomicInteger();
            SuiteContext.AlertListener alert = a -> calls.incrementAndGet();
            SuiteContext.IssueListener issue = i -> calls.incrementAndGet();
            SuiteContext.SiteMapListener siteMap = e -> calls.incrementAndGet();
            SuiteContext.ScopeListener scope = () -> calls.incrementAndGet();
            SuiteContext.TaskListener task = t -> calls.incrementAndGet();
            java.util.function.Consumer<String> send = t -> calls.incrementAndGet();
            ctx.addAlertListener(alert);
            ctx.addIssueListener(issue);
            ctx.addSiteMapListener(siteMap);
            ctx.addScopeListener(scope);
            ctx.addTaskListener(task);
            ctx.addSendListener(send);
            ctx.removeAlertListener(alert);
            ctx.removeIssueListener(issue);
            ctx.removeSiteMapListener(siteMap);
            ctx.removeScopeListener(scope);
            ctx.removeTaskListener(task);
            ctx.removeSendListener(send);
            ctx.alert("suite", "an event");
            ctx.addIssue(issue());
            ctx.siteMapChanged(entry());
            ctx.scopeChanged();
            ctx.taskChanged(null);
            ctx.sendTo("nowhere", null, null, null, null);
            Expect.equal(calls.get(), 0, "a listener taken away is not called");
        });

        Harness.test("state held for a tool is kept until it is taken, once", () -> {
            SuiteContext ctx = new SuiteContext();
            Expect.isNull(ctx.takeHeldToolState("late"), "nothing held to begin with");
            ctx.holdToolState("late", "saved");
            ctx.holdToolState(null, "ignored");
            ctx.holdToolState("ignored", null);
            Expect.equal(ctx.takeHeldToolState("late"), "saved", "the held state");
            Expect.isNull(ctx.takeHeldToolState("late"), "taken once");
            Expect.isNull(ctx.takeHeldToolState(null), "a null name holds nothing");
            Expect.isNull(ctx.takeHeldToolState("ignored"), "a null state is not held");
        });

        Harness.test("sinks keep their order and a name registers once", () -> {
            SuiteContext ctx = new SuiteContext();
            List<String> order = new ArrayList<>();
            ctx.registerSink("first", (svc, req, resp, cap) -> { });
            ctx.registerSink("second", (svc, req, resp, cap) -> { });
            ctx.registerSink("first", (svc, req, resp, cap) -> { });
            Expect.equal(String.join(",", ctx.sinkNames()), "first,second", "registration order, a name once");
        });
    }
}