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