]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-systemd-tmpfiles.py
tmpfiles: also add %t/%S/%C/%L specifiers
[thirdparty/systemd.git] / src / test / test-systemd-tmpfiles.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
6 # systemd is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; either version 2.1 of the License, or
9 # (at your option) any later version.
10
11 import os
12 import sys
13 import subprocess
14
15 EX_DATAERR = 65 # from sysexits.h
16
17 exe = sys.argv[1]
18
19 def test_line(line, *, user, returncode=EX_DATAERR):
20 args = ['--user'] if user else []
21 print('Running {} {} on {!r}'.format(exe, ' '.join(args), line))
22 c = subprocess.run([exe, '--create', *args, '-'],
23 input=line, stdout=subprocess.PIPE, universal_newlines=True)
24 assert c.returncode == returncode, c
25
26 def test_invalids(*, user):
27 test_line('asdfa', user=user)
28 test_line('f "open quote', user=user)
29 test_line('f closed quote""', user=user)
30 test_line('Y /unknown/letter', user=user)
31 test_line('w non/absolute/path', user=user)
32 test_line('s', user=user) # s is for short
33 test_line('f!! /too/many/bangs', user=user)
34 test_line('f++ /too/many/plusses', user=user)
35 test_line('f+!+ /too/many/plusses', user=user)
36 test_line('f!+! /too/many/bangs', user=user)
37 test_line('w /unresolved/argument - - - - "%Y"', user=user)
38 test_line('w /unresolved/argument/sandwich - - - - "%v%Y%v"', user=user)
39 test_line('w /unresolved/filename/%Y - - - - "whatever"', user=user)
40 test_line('w /unresolved/filename/sandwich/%v%Y%v - - - - "whatever"', user=user)
41 test_line('w - - - - - "no file specfied"', user=user)
42 test_line('C - - - - - "no file specfied"', user=user)
43 test_line('C non/absolute/path - - - - -', user=user)
44 test_line('b - - - - - -', user=user)
45 test_line('b 1234 - - - - -', user=user)
46 test_line('c - - - - - -', user=user)
47 test_line('c 1234 - - - - -', user=user)
48 test_line('t - - -', user=user)
49 test_line('T - - -', user=user)
50 test_line('a - - -', user=user)
51 test_line('A - - -', user=user)
52 test_line('h - - -', user=user)
53 test_line('H - - -', user=user)
54
55 def test_unitialized_t():
56 if os.getuid() == 0:
57 return
58
59 try:
60 del os.environ['XDG_RUNTIME_DIR']
61 except KeyError:
62 pass
63 test_line('w /foo - - - - "specifier for --user %t"', user=True, returncode=0)
64
65 if __name__ == '__main__':
66 test_invalids(user=False)
67 test_invalids(user=True)
68 test_unitialized_t()