about summary refs log tree commit diff
path: root/nixos/tests/ydotool.nix
blob: 45e3d27adeb4972c6a4a29ebaa0e9380994d3a4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
  system ? builtins.currentSystem,
  config ? { },
  pkgs ? import ../.. { inherit system config; },
  lib ? pkgs.lib,
}:
let
  makeTest = import ./make-test-python.nix;
  textInput = "This works.";
  inputBoxText = "Enter input";
  inputBox = pkgs.writeShellScript "zenity-input" ''
    ${lib.getExe pkgs.gnome.zenity} --entry --text '${inputBoxText}:' > /tmp/output &
  '';
  asUser = ''
    def as_user(cmd: str):
        """
        Return a shell command for running a shell command as a specific user.
        """
        return f"sudo -u alice -i {cmd}"
  '';
in
{
  headless = makeTest {
    name = "headless";

    enableOCR = true;

    nodes.machine = {
      imports = [ ./common/user-account.nix ];

      users.users.alice.extraGroups = [ "ydotool" ];

      programs.ydotool.enable = true;

      services.getty.autologinUser = "alice";
    };

    testScript =
      asUser
      + ''
        start_all()

        machine.wait_for_unit("multi-user.target")
        machine.wait_for_text("alice")
        machine.succeed(as_user("ydotool type 'echo ${textInput} > /tmp/output'")) # text input
        machine.succeed(as_user("ydotool key 28:1 28:0")) # text input
        machine.screenshot("headless_input")
        machine.wait_for_file("/tmp/output")
        machine.wait_until_succeeds("grep '${textInput}' /tmp/output") # text input
      '';

    meta.maintainers = with lib.maintainers; [
      OPNA2608
      quantenzitrone
    ];
  };

  x11 = makeTest {
    name = "x11";

    enableOCR = true;

    nodes.machine = {
      imports = [
        ./common/user-account.nix
        ./common/auto.nix
        ./common/x11.nix
      ];

      users.users.alice.extraGroups = [ "ydotool" ];

      programs.ydotool.enable = true;

      test-support.displayManager.auto = {
        enable = true;
        user = "alice";
      };

      services.xserver.windowManager.dwm.enable = true;
      services.displayManager.defaultSession = lib.mkForce "none+dwm";
    };

    testScript =
      asUser
      + ''
        start_all()

        machine.wait_for_x()
        machine.execute(as_user("${inputBox}"))
        machine.wait_for_text("${inputBoxText}")
        machine.succeed(as_user("ydotool type '${textInput}'")) # text input
        machine.screenshot("x11_input")
        machine.succeed(as_user("ydotool mousemove -a 400 110")) # mouse input
        machine.succeed(as_user("ydotool click 0xC0")) # mouse input
        machine.wait_for_file("/tmp/output")
        machine.wait_until_succeeds("grep '${textInput}' /tmp/output") # text input
      '';

    meta.maintainers = with lib.maintainers; [
      OPNA2608
      quantenzitrone
    ];
  };

  wayland = makeTest {
    name = "wayland";

    enableOCR = true;

    nodes.machine = {
      imports = [ ./common/user-account.nix ];

      services.cage = {
        enable = true;
        user = "alice";
      };

      programs.ydotool.enable = true;

      services.cage.program = inputBox;
    };

    testScript = ''
      start_all()

      machine.wait_for_unit("graphical.target")
      machine.wait_for_text("${inputBoxText}")
      machine.succeed("ydotool type '${textInput}'") # text input
      machine.screenshot("wayland_input")
      machine.succeed("ydotool mousemove -a 100 100") # mouse input
      machine.succeed("ydotool click 0xC0") # mouse input
      machine.wait_for_file("/tmp/output")
      machine.wait_until_succeeds("grep '${textInput}' /tmp/output") # text input
    '';

    meta.maintainers = with lib.maintainers; [
      OPNA2608
      quantenzitrone
    ];
  };

  customGroup =
    let
      name = "customGroup";
      nodeName = "${name}Node";
      insideGroupUsername = "ydotool-user";
      outsideGroupUsername = "other-user";
      groupName = "custom-group";
    in
    makeTest {
      inherit name;

      nodes."${nodeName}" = {
        programs.ydotool = {
          enable = true;
          group = groupName;
        };

        users.users = {
          "${insideGroupUsername}" = {
            isNormalUser = true;
            extraGroups = [ groupName ];
          };
          "${outsideGroupUsername}".isNormalUser = true;
        };
      };

      testScript = ''
        start_all()

        # Wait for service to start
        ${nodeName}.wait_for_unit("multi-user.target")
        ${nodeName}.wait_for_unit("ydotoold.service")

        # Verify that user with the configured group can use the service
        ${nodeName}.succeed("sudo --login --user=${insideGroupUsername} ydotool type 'Hello, World!'")

        # Verify that user without the configured group can't use the service
        ${nodeName}.fail("sudo --login --user=${outsideGroupUsername} ydotool type 'Hello, World!'")
      '';

      meta.maintainers = with lib.maintainers; [ l0b0 ];
    };
}