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.

2.7 KB
Raw Blame
package opensuite.core;

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

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

/** The memory figure counts everything Open Suite runs, not the application's heap alone. */
public final class MemoryUseTest {
    public void run() {
        Harness.suite("MemoryUse");

        Harness.test("a reading's total is all four parts", () -> {
            MemoryUse.Reading r = new MemoryUse.Reading(800, 2100, 150, 370, 64000, 58000, true);
            Expect.equal(r.total(), 3420L, "application, engine, other programs and data held in memory");
        });

        Harness.test("a value is read from a /proc file in bytes", () -> { try {
            Path f = Files.createTempFile("memuse", ".txt");
            try {
                Files.writeString(f, "Rss:              2048 kB\nPss:               1024 kB\nPss_Dirty:          512 kB\n");
                Expect.equal(MemoryUse.field(f, "Pss:"), 1024L * 1024, "the named line, not one that starts the same");
                Expect.equal(MemoryUse.field(f, "Swap:"), -1L, "a line that is not there");
                Expect.equal(MemoryUse.field(Path.of("/nonexistent/none"), "Pss:"), -1L, "a file that is not there");
            } finally {
                Files.deleteIfExists(f);
            }
        } catch (java.io.IOException e) { throw new RuntimeException(e); } });

        Harness.test("a folder's size is the sum of its files", () -> { try {
            Path d = Files.createTempDirectory("memuse");
            try {
                Files.write(d.resolve("a"), new byte[1000]);
                Files.createDirectory(d.resolve("sub"));
                Files.write(d.resolve("sub").resolve("b"), new byte[500]);
                Expect.equal(MemoryUse.size(d), 1500L, "files in the folder and below it");
            } finally {
                Files.deleteIfExists(d.resolve("sub").resolve("b"));
                Files.deleteIfExists(d.resolve("sub"));
                Files.deleteIfExists(d.resolve("a"));
                Files.deleteIfExists(d);
            }
        } catch (java.io.IOException e) { throw new RuntimeException(e); } });

        Harness.test("on Linux, macOS and Windows this process has a share and the machine has a size", () -> {
            if (!Files.isReadable(Path.of("/proc/self/status")) && !Os.MAC && !Os.WINDOWS) return;
            MemoryUse.Reading r = MemoryUse.read();
            Expect.isTrue(r.whole(), "the whole figure is available");
            Expect.isTrue(r.app() > 10L * 1024 * 1024, "this process uses more than 10 MB");
            Expect.isTrue(r.machine() >= r.available() && r.machine() > 0, "the machine's memory and what is available of it");
        });
    }
}