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.5 KB
Raw Blame
package opensuite.ui;

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

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/** The recent projects list keeps a project whose path holds a tab, a newline or a backslash. */
public final class RecentProjectsTest {

    public void run() {
        Harness.suite("recent projects");

        Harness.test("escaping a field and reading it back gives the same text", () -> {
            for (String s : List.of("plain", "a\tb", "a\nb", "a\r\nb", "C:\\temp\\new", "a\\tb", "ends\\", ""))
                Expect.equal(RecentProjects.unescape(RecentProjects.escape(s)), s, "round trip");
            Expect.isTrue(!RecentProjects.escape("a\tb\nc\rd").matches("(?s).*[\t\n\r].*"), "no raw tab or line break is written");
        });

        // Windows does not allow these characters in a file name.
        if (File.separatorChar == '/') Harness.test("projects whose paths hold a tab and a newline come back from the list", () -> {
            String home = System.getProperty("user.home");
            Path tmp = null;
            try {
                tmp = Files.createTempDirectory("recent");
                System.setProperty("user.home", tmp.toString());
                File tab = tmp.resolve("with\ttab.osuite").toFile(), line = tmp.resolve("with\nline.osuite").toFile(),
                        plain = tmp.resolve("plain.osuite").toFile();
                for (File f : List.of(tab, line, plain)) Files.writeString(f.toPath(), "x");
                RecentProjects.record("a", tab);
                RecentProjects.record("b", line);
                RecentProjects.record("c", plain);
                List<RecentProjects.Entry> got = RecentProjects.load();
                Expect.equal(got.size(), 3, "all three are listed");
                Expect.equal(got.get(0).file(), plain, "newest first");
                Expect.equal(got.get(1).file(), line, "the path with a newline");
                Expect.equal(got.get(2).file(), tab, "the path with a tab");
                Expect.equal(got.get(2).name(), "with\ttab.osuite", "its name");
            } catch (java.io.IOException e) {
                throw new RuntimeException(e);
            } finally {
                System.setProperty("user.home", home);
                if (tmp != null) {
                    try (var w = Files.walk(tmp)) { w.sorted(java.util.Comparator.reverseOrder()).forEach(q -> q.toFile().delete()); }
                    catch (java.io.IOException ignored) { }
                }
            }
        });
    }
}