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.1 KB
Raw Blame
package opensuite.core;

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

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

/** Bodies and snapshots are stored compressed, read back unchanged, and an unchanged snapshot is not written twice. */
public final class ProjectStoreCompressionTest {
    public void run() {
        Harness.suite("project file compression");
        try { go(); } catch (Exception e) { Harness.test("the project file can be written and read", () -> { throw new RuntimeException(e); }); }
    }

    private void go() throws Exception {

        Path dir = Files.createTempDirectory("ostest");
        try {
            Path file = dir.resolve("p.osuite");
            byte[] page = ("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + "<p>the same paragraph again</p>\n".repeat(2000)).getBytes();
            byte[] small = "GET / HTTP/1.1\r\n\r\n".getBytes();
            byte[] snap = ("settings " + "issue text ".repeat(5000)).getBytes();
            long offPage, offSmall;
            try (ProjectStore s = new ProjectStore(file, 0)) {
                offPage = s.append(ProjectStore.BODY, page);
                offSmall = s.append(ProjectStore.BODY, small);
                final long size = s.size();
                Harness.test("a text body takes far less room than its length", () ->
                        Expect.isTrue(size < page.length / 5, "stored " + size + " bytes for " + page.length));
                final boolean first = s.appendSnapshot(snap), again = s.appendSnapshot(snap.clone()), changed = s.appendSnapshot("changed".getBytes());
                Harness.test("an unchanged snapshot is written once", () -> {
                    Expect.isTrue(first, "the first is written");
                    Expect.isTrue(!again, "the same again is not");
                    Expect.isTrue(changed, "a changed one is");
                    Expect.isTrue(s.deadSnapshotBytes() > 0, "the replaced one is counted as dead space");
                });
                s.sync();
            }
            try (ProjectStore s = new ProjectStore(file, 0)) {
                final byte[][] lastSnap = { null };
                final int[] bodies = { 0 };
                s.scan((type, off, data) -> { if (type == ProjectStore.SNAPSHOT) lastSnap[0] = data; if (type == ProjectStore.BODY) bodies[0]++; });
                final byte[] gotPage = s.readBody(offPage, page.length), gotSmall = s.readBody(offSmall, small.length);
                Harness.test("everything reads back as it was written", () -> {
                    Expect.isTrue(java.util.Arrays.equals(gotPage, page), "the compressed body");
                    Expect.isTrue(java.util.Arrays.equals(gotSmall, small), "the small body, kept plain");
                    Expect.equal(bodies[0], 2, "both bodies are seen when the file is opened");
                    Expect.equal(new String(lastSnap[0]), "changed", "the latest snapshot");
                });
            }
        } finally {
            try (var w = Files.walk(dir)) { w.sorted(java.util.Comparator.reverseOrder()).forEach(p -> p.toFile().delete()); }
        }
    }
}