1import ./make-test-python.nix (
2 { pkgs, ... }:
3
4 let
5 echoAll = pkgs.writeScript "echo-all" ''
6 #! ${pkgs.runtimeShell}
7 for s in "$@"; do
8 printf '%s\n' "$s"
9 done
10 '';
11 # deliberately using a local empty file instead of pkgs.emptyFile to have
12 # a non-store path in the test
13 args = [
14 "a%Nything"
15 "lang=\${LANG}"
16 ";"
17 "/bin/sh -c date"
18 ./empty-file
19 4.2
20 23
21 ];
22 in
23 {
24 name = "systemd-escaping";
25
26 nodes.machine =
27 {
28 pkgs,
29 lib,
30 utils,
31 ...
32 }:
33 {
34 systemd.services.echo =
35 assert !(builtins.tryEval (utils.escapeSystemdExecArgs [ [ ] ])).success;
36 assert !(builtins.tryEval (utils.escapeSystemdExecArgs [ { } ])).success;
37 assert !(builtins.tryEval (utils.escapeSystemdExecArgs [ null ])).success;
38 assert !(builtins.tryEval (utils.escapeSystemdExecArgs [ false ])).success;
39 assert !(builtins.tryEval (utils.escapeSystemdExecArgs [ (_: _) ])).success;
40 {
41 description = "Echo to the journal";
42 serviceConfig.Type = "oneshot";
43 serviceConfig.ExecStart = ''
44 ${echoAll} ${utils.escapeSystemdExecArgs args}
45 '';
46 };
47 };
48
49 testScript = ''
50 machine.wait_for_unit("multi-user.target")
51 machine.succeed("systemctl start echo.service")
52 # skip the first 'Starting <service> ...' line
53 logs = machine.succeed("journalctl -u echo.service -o cat").splitlines()[1:]
54 assert "a%Nything" == logs[0]
55 assert "lang=''${LANG}" == logs[1]
56 assert ";" == logs[2]
57 assert "/bin/sh -c date" == logs[3]
58 assert "/nix/store/ij3gw72f4n5z4dz6nnzl1731p9kmjbwr-empty-file" == logs[4]
59 assert "4.2" in logs[5] # toString produces extra fractional digits!
60 assert "23" == logs[6]
61 '';
62 }
63)