]>
Commit | Line | Data |
---|---|---|
d9daae55 ZJS |
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 | ||
5a8575ef | 11 | import os |
d9daae55 | 12 | import sys |
03025f46 | 13 | import socket |
d9daae55 | 14 | import subprocess |
03025f46 ZJS |
15 | import tempfile |
16 | import pwd | |
17 | ||
18 | try: | |
19 | from systemd import id128 | |
20 | except ImportError: | |
21 | id128 = None | |
d9daae55 ZJS |
22 | |
23 | EX_DATAERR = 65 # from sysexits.h | |
24 | ||
25 | exe = sys.argv[1] | |
26 | ||
03025f46 | 27 | def test_line(line, *, user, returncode=EX_DATAERR, extra={}): |
5a8575ef ZJS |
28 | args = ['--user'] if user else [] |
29 | print('Running {} {} on {!r}'.format(exe, ' '.join(args), line)) | |
30 | c = subprocess.run([exe, '--create', *args, '-'], | |
03025f46 | 31 | **extra, |
d9daae55 | 32 | input=line, stdout=subprocess.PIPE, universal_newlines=True) |
5a8575ef ZJS |
33 | assert c.returncode == returncode, c |
34 | ||
35 | def test_invalids(*, user): | |
36 | test_line('asdfa', user=user) | |
37 | test_line('f "open quote', user=user) | |
38 | test_line('f closed quote""', user=user) | |
39 | test_line('Y /unknown/letter', user=user) | |
40 | test_line('w non/absolute/path', user=user) | |
41 | test_line('s', user=user) # s is for short | |
42 | test_line('f!! /too/many/bangs', user=user) | |
43 | test_line('f++ /too/many/plusses', user=user) | |
44 | test_line('f+!+ /too/many/plusses', user=user) | |
45 | test_line('f!+! /too/many/bangs', user=user) | |
46 | test_line('w /unresolved/argument - - - - "%Y"', user=user) | |
47 | test_line('w /unresolved/argument/sandwich - - - - "%v%Y%v"', user=user) | |
48 | test_line('w /unresolved/filename/%Y - - - - "whatever"', user=user) | |
49 | test_line('w /unresolved/filename/sandwich/%v%Y%v - - - - "whatever"', user=user) | |
50 | test_line('w - - - - - "no file specfied"', user=user) | |
51 | test_line('C - - - - - "no file specfied"', user=user) | |
52 | test_line('C non/absolute/path - - - - -', user=user) | |
53 | test_line('b - - - - - -', user=user) | |
54 | test_line('b 1234 - - - - -', user=user) | |
55 | test_line('c - - - - - -', user=user) | |
56 | test_line('c 1234 - - - - -', user=user) | |
57 | test_line('t - - -', user=user) | |
58 | test_line('T - - -', user=user) | |
59 | test_line('a - - -', user=user) | |
60 | test_line('A - - -', user=user) | |
61 | test_line('h - - -', user=user) | |
62 | test_line('H - - -', user=user) | |
63 | ||
64 | def test_unitialized_t(): | |
65 | if os.getuid() == 0: | |
66 | return | |
67 | ||
03025f46 ZJS |
68 | test_line('w /foo - - - - "specifier for --user %t"', |
69 | user=True, returncode=0, extra={'env':{}}) | |
70 | ||
71 | def test_content(line, expected, *, user, extra={}): | |
72 | d = tempfile.TemporaryDirectory(prefix='test-systemd-tmpfiles.') | |
73 | arg = d.name + '/arg' | |
74 | spec = line.format(arg) | |
75 | test_line(spec, user=user, returncode=0, extra=extra) | |
76 | content = open(arg).read() | |
77 | print('expect: {!r}\nactual: {!r}'.format(expected, content)) | |
78 | assert content == expected | |
79 | ||
80 | def test_valid_specifiers(*, user): | |
81 | test_content('f {} - - - - two words', 'two words', user=user) | |
82 | if id128: | |
83 | test_content('f {} - - - - %m', '{}'.format(id128.get_machine().hex), user=user) | |
84 | test_content('f {} - - - - %b', '{}'.format(id128.get_boot().hex), user=user) | |
85 | test_content('f {} - - - - %H', '{}'.format(socket.gethostname()), user=user) | |
86 | test_content('f {} - - - - %v', '{}'.format(os.uname().release), user=user) | |
87 | test_content('f {} - - - - %U', '{}'.format(os.getuid()), user=user) | |
88 | ||
89 | user = pwd.getpwuid(os.getuid()) | |
90 | test_content('f {} - - - - %u', '{}'.format(user.pw_name), user=user) | |
91 | test_content('f {} - - - - %h', '{}'.format(user.pw_dir), user=user) | |
92 | ||
93 | xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR') | |
94 | if xdg_runtime_dir is not None or not user: | |
95 | test_content('f {} - - - - %t', | |
96 | xdg_runtime_dir if user else '/run', | |
97 | user=user) | |
98 | ||
99 | xdg_config_home = os.getenv('XDG_CONFIG_HOME') | |
100 | if xdg_config_home is not None or not user: | |
101 | test_content('f {} - - - - %S', | |
102 | xdg_config_home if user else '/var/lib', | |
103 | user=user) | |
104 | ||
105 | xdg_cache_home = os.getenv('XDG_CACHE_HOME') | |
106 | if xdg_cache_home is not None or not user: | |
107 | test_content('f {} - - - - %C', | |
108 | xdg_cache_home if user else '/var/cache', | |
109 | user=user) | |
110 | ||
111 | if xdg_config_home is not None or not user: | |
112 | test_content('f {} - - - - %L', | |
113 | xdg_config_home + '/log' if user else '/var/log', | |
114 | user=user) | |
115 | ||
116 | test_content('f {} - - - - %%', '%', user=user) | |
d9daae55 ZJS |
117 | |
118 | if __name__ == '__main__': | |
5a8575ef ZJS |
119 | test_invalids(user=False) |
120 | test_invalids(user=True) | |
121 | test_unitialized_t() | |
03025f46 ZJS |
122 | |
123 | test_valid_specifiers(user=False) | |
124 | test_valid_specifiers(user=True) |