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.

4.9 KB
Raw Blame
package opensuite.ui;

import opensuite.core.Settings;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

/** Hotkeys: what a key press is called, which action it is bound to, and how the action is carried out. */
public final class HotkeysTest {

    private static KeyEvent press(int code, int modifiers) {
        return new KeyEvent(new JLabel(), KeyEvent.KEY_PRESSED, 0, modifiers, code, KeyEvent.CHAR_UNDEFINED);
    }

    public void run() {
        Harness.suite("hotkeys");

        Harness.test("a key press is named as the settings page shows it", () -> {
            Expect.isTrue("Ctrl+R".equals(Hotkeys.text(press(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK))), "Ctrl+R");
            Expect.isTrue("Ctrl+Shift+B".equals(Hotkeys.text(press(KeyEvent.VK_B, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK))), "Ctrl+Shift+B");
            Expect.isTrue("Ctrl+Backspace".equals(Hotkeys.text(press(KeyEvent.VK_BACK_SPACE, KeyEvent.CTRL_DOWN_MASK))), "Ctrl+Backspace");
            Expect.isTrue(Hotkeys.text(press(KeyEvent.VK_R, 0)) == null, "a key without Control is not a hotkey");
            Expect.isTrue(Hotkeys.text(press(KeyEvent.VK_CONTROL, KeyEvent.CTRL_DOWN_MASK)) == null, "nor is Control on its own");
        });

        Harness.test("every default binding is a keystroke, and no two actions share one", () -> {
            Set<String> seen = new HashSet<>(), names = new HashSet<>();
            for (Hotkeys.Action a : Hotkeys.ACTIONS) {
                Expect.isTrue(names.add(a.name()), "listed twice: " + a.name());
                if (a.def().isEmpty()) continue;
                Expect.isTrue(Hotkeys.keyStroke(a.def()) != null, a.name() + " has a default that is not a keystroke: " + a.def());
                Expect.isTrue(seen.add(a.def()), a.def() + " is the default of two actions, the second being " + a.name());
            }
        });

        Harness.test("a rebound action answers to its new key and not its old one", () -> {
            Settings s = new Settings();
            Expect.isTrue("Send to Repeater".equals(Hotkeys.bound(s, "Ctrl+R").name()), "the default");
            s.set("hotkey.Send to Repeater", "Ctrl+Shift+Y");
            Expect.isTrue(Hotkeys.bound(s, "Ctrl+R") == null, "the old key is free");
            Expect.isTrue("Send to Repeater".equals(Hotkeys.bound(s, "Ctrl+Shift+Y").name()), "the new key");
            s.remove("hotkey.Send to Repeater");   // settings outlive the object, so leave them as they were
            Expect.isTrue("Send to Repeater".equals(Hotkeys.bound(s, "Ctrl+R").name()), "and back to the default once the binding is removed");
        });

        Harness.test("an action is carried out by the menu item of its name, wherever the focus is inside", () -> {
            int[] sent = new int[1];
            Supplier<JPopupMenu> menu = () -> {
                JPopupMenu m = new JPopupMenu();
                JMenu sub = new JMenu("Send");
                JMenuItem item = new JMenuItem("Send to repeater...");   // case and the ellipsis do not matter
                item.addActionListener(e -> sent[0]++);
                sub.add(item);
                m.add(new JMenuItem("Something else"));
                m.add(sub);
                JMenuItem off = new JMenuItem("Send to Intruder");
                off.setEnabled(false);
                off.addActionListener(e -> sent[0] += 100);
                m.add(off);
                return m;
            };
            JPanel host = new JPanel();
            host.putClientProperty(Hotkeys.MENU, menu);
            JTable inner = new JTable();
            host.add(inner);
            Hotkeys.Action repeater = Hotkeys.bound(new Settings(), "Ctrl+R"), intruder = Hotkeys.bound(new Settings(), "Ctrl+I");
            Expect.isTrue(Hotkeys.perform(repeater, inner, null) && sent[0] == 1, "the item ran once, got " + sent[0]);
            Expect.isTrue(!Hotkeys.perform(intruder, inner, null) && sent[0] == 1, "a disabled item is not run");
            Expect.isTrue(!Hotkeys.perform(repeater, new JTable(), null), "with no menu on offer nothing happens");
        });

        Harness.test("a text field carries out the editing actions itself", () -> {
            JTextField f = new JTextField("one two three");
            f.setCaretPosition(0);
            Hotkeys.Action next = null, all = null;
            for (Hotkeys.Action a : Hotkeys.ACTIONS) {
                if (a.name().equals("Go to next word")) next = a;
                if (a.name().equals("Select all")) all = a;
            }
            Expect.isTrue(Hotkeys.perform(next, f, null) && f.getCaretPosition() == 4, "the caret moved a word, to " + f.getCaretPosition());
            Expect.isTrue(Hotkeys.perform(all, f, null) && "one two three".equals(f.getSelectedText()), "everything is selected");
        });
    }
}