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.

3.8 KB
Raw Blame
package opensuite.core;

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

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

/** Settings > Suite: temporary files, the memory limit for the launchers, the performance log. */
public final class LaunchTest {

    private static Path temp(String name) {
        try { return Files.createTempDirectory(name); } catch (java.io.IOException e) { throw new java.io.UncheckedIOException(e); }
    }

    private static String read(Path file) {
        try { return Files.readString(file); } catch (java.io.IOException e) { throw new java.io.UncheckedIOException(e); }
    }

    public void run() {
        Harness.suite("launch settings");

        Harness.test("a custom memory limit is left for the launchers and removed again", () -> {
            Settings s = new Settings();
            Path home = temp("ot-launch");
            try {
                Expect.isTrue(Launch.writeMemoryFile(s, home) == 0 && !Files.exists(home.resolve(Launch.MEMORY_FILE)), "half: no file");
                s.set(Launch.MEMORY_MODE, "custom");
                s.setInt(Launch.MEMORY_MB, 4096);
                Launch.writeMemoryFile(s, home);
                Expect.isTrue("4096".equals(read(home.resolve(Launch.MEMORY_FILE)).trim()), "the limit in MB");
                s.setInt(Launch.MEMORY_MB, 3);
                Expect.isTrue(Launch.writeMemoryFile(s, home) == 256, "never so little that the suite cannot start");
                s.set(Launch.MEMORY_MODE, "half");
                Launch.writeMemoryFile(s, home);
                Expect.isTrue(!Files.exists(home.resolve(Launch.MEMORY_FILE)), "back to half: file removed");
            } finally { s.remove(Launch.MEMORY_MODE); s.remove(Launch.MEMORY_MB); }
        });

        Harness.test("temporary files go to the chosen folder, or stay put if it cannot be used", () -> {
            Settings s = new Settings();
            String before = System.getProperty("java.io.tmpdir");
            Path dir = temp("ot-temp").resolve("made-on-demand");
            try {
                s.set(Launch.TEMP_DIR, dir.toString());
                Expect.isTrue(before.equals(Launch.applyTempDir(s)), "default mode ignores the folder");
                s.set(Launch.TEMP_MODE, "custom");
                Expect.isTrue(dir.toString().equals(Launch.applyTempDir(s)) && Files.isDirectory(dir), "custom folder, created");
                System.setProperty("java.io.tmpdir", before);
                // A folder cannot be made under a plain file, on any system.
                Path blocker = temp("ot-temp").resolve("a-file");
                try { Files.writeString(blocker, "x"); } catch (java.io.IOException e) { throw new java.io.UncheckedIOException(e); }
                s.set(Launch.TEMP_DIR, blocker.resolve("x").toString());
                Expect.isTrue(before.equals(Launch.applyTempDir(s)), "an unusable folder leaves the default");
            } finally { System.setProperty("java.io.tmpdir", before); s.remove(Launch.TEMP_DIR); s.remove(Launch.TEMP_MODE); }
        });

        Harness.test("the performance log writes only while it is switched on", () -> {
            SuiteContext ctx = new SuiteContext();
            Settings s = ctx.settings();
            Path dir = temp("ot-perf");
            try {
                s.set(Launch.LOG_DIR, dir.toString());
                Expect.isTrue(Launch.logPerformance(ctx) == null, "off: nothing written");
                s.setBool(Launch.LOG_LOCAL, true);
                Path file = Launch.logPerformance(ctx);
                Launch.logPerformance(ctx);
                Expect.isTrue(file != null && read(file).split("\n").length == 2, "one line each time");
                Expect.isTrue(read(file).contains("heap_used_mb="), "with the heap in it");
            } finally { s.remove(Launch.LOG_DIR); s.remove(Launch.LOG_LOCAL); HttpClient.setGate(null); }
        });
    }
}