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;
import java.util.prefs.Preferences;

/** Data kept under the product's former name comes across on the first start under the new one. */
public final class MigrationTest {

    private interface Throwing<T> { T get() throws Exception; }
    private static <T> T unchecked(Throwing<T> t) {
        try { return t.get(); } catch (Exception e) { throw new IllegalStateException(e); }
    }

    public void run() {
        Harness.suite("migration from the former name");

        Harness.test("the settings folder moves once and is never overwritten", () -> {
            Path dir = unchecked(() -> Files.createTempDirectory("otmig"));
            Path from = dir.resolve("old"), to = dir.resolve("new");
            unchecked(() -> Files.createDirectories(from.resolve("logs")));
            unchecked(() -> Files.writeString(from.resolve("ca.pem"), "certificate"));
            Expect.isTrue(unchecked(() -> Migration.moveFolder(from, to)), "moved");
            Expect.equal(unchecked(() -> Files.readString(to.resolve("ca.pem"))), "certificate", "contents kept");
            Expect.isFalse(Files.exists(from), "old folder gone");
            unchecked(() -> Files.createDirectories(from));
            unchecked(() -> Files.writeString(from.resolve("ca.pem"), "older"));
            Expect.isFalse(unchecked(() -> Migration.moveFolder(from, to)), "not moved over existing data");
            Expect.equal(unchecked(() -> Files.readString(to.resolve("ca.pem"))), "certificate", "new data untouched");
            Expect.isFalse(unchecked(() -> Migration.moveFolder(dir.resolve("absent"), dir.resolve("x"))), "nothing to move");
        });

        Harness.test("saved preferences are copied, children included, unless new ones exist", () -> {
            Preferences root = Preferences.userRoot().node("opensuite-test-" + System.nanoTime());
            try {
                Preferences from = root.node("from"), to = root.node("to");
                from.put("ui.theme", "dark");
                from.node("child").put("k", "v");
                Expect.isTrue(unchecked(() -> Migration.copyPreferences(from, to)), "copied");
                Expect.equal(to.get("ui.theme", ""), "dark", "key");
                Expect.equal(to.node("child").get("k", ""), "v", "child key");
                from.put("ui.theme", "light");
                Expect.isFalse(unchecked(() -> Migration.copyPreferences(from, to)), "not copied twice");
                Expect.equal(to.get("ui.theme", ""), "dark", "newer settings kept");
            } finally {
                unchecked(() -> { root.removeNode(); return null; });
            }
        });
    }
}