]>
Commit | Line | Data |
---|---|---|
d9daae55 | 1 | #!/usr/bin/env python3 |
d8a0bcfd | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
d9daae55 | 3 | # |
d8a0bcfd YW |
4 | # systemd is free software; you can redistribute it and/or modify it |
5 | # under the terms of the GNU Lesser General Public License as published by | |
6 | # the Free Software Foundation; either version 2.1 of the License, or | |
7 | # (at your option) any later version. | |
d9daae55 | 8 | |
5a8575ef | 9 | import os |
d9daae55 | 10 | import sys |
03025f46 | 11 | import socket |
d9daae55 | 12 | import subprocess |
03025f46 ZJS |
13 | import tempfile |
14 | import pwd | |
b75f0c69 | 15 | import grp |
03025f46 ZJS |
16 | |
17 | try: | |
18 | from systemd import id128 | |
19 | except ImportError: | |
20 | id128 = None | |
d9daae55 ZJS |
21 | |
22 | EX_DATAERR = 65 # from sysexits.h | |
7488492a ZJS |
23 | EXIT_TEST_SKIP = 77 |
24 | ||
25 | try: | |
26 | subprocess.run | |
27 | except AttributeError: | |
28 | sys.exit(EXIT_TEST_SKIP) | |
d9daae55 | 29 | |
59ca366c | 30 | exe_with_args = sys.argv[1:] |
d9daae55 | 31 | |
03025f46 | 32 | def test_line(line, *, user, returncode=EX_DATAERR, extra={}): |
5a8575ef | 33 | args = ['--user'] if user else [] |
59ca366c EV |
34 | print('Running {} on {!r}'.format(' '.join(exe_with_args + args), line)) |
35 | c = subprocess.run(exe_with_args + ['--create', '-'] + args, | |
7488492a ZJS |
36 | input=line, stdout=subprocess.PIPE, universal_newlines=True, |
37 | **extra) | |
5a8575ef ZJS |
38 | assert c.returncode == returncode, c |
39 | ||
40 | def test_invalids(*, user): | |
41 | test_line('asdfa', user=user) | |
42 | test_line('f "open quote', user=user) | |
43 | test_line('f closed quote""', user=user) | |
44 | test_line('Y /unknown/letter', user=user) | |
45 | test_line('w non/absolute/path', user=user) | |
46 | test_line('s', user=user) # s is for short | |
47 | test_line('f!! /too/many/bangs', user=user) | |
48 | test_line('f++ /too/many/plusses', user=user) | |
49 | test_line('f+!+ /too/many/plusses', user=user) | |
50 | test_line('f!+! /too/many/bangs', user=user) | |
51 | test_line('w /unresolved/argument - - - - "%Y"', user=user) | |
52 | test_line('w /unresolved/argument/sandwich - - - - "%v%Y%v"', user=user) | |
53 | test_line('w /unresolved/filename/%Y - - - - "whatever"', user=user) | |
54 | test_line('w /unresolved/filename/sandwich/%v%Y%v - - - - "whatever"', user=user) | |
5238e957 BB |
55 | test_line('w - - - - - "no file specified"', user=user) |
56 | test_line('C - - - - - "no file specified"', user=user) | |
5a8575ef ZJS |
57 | test_line('C non/absolute/path - - - - -', user=user) |
58 | test_line('b - - - - - -', user=user) | |
59 | test_line('b 1234 - - - - -', user=user) | |
60 | test_line('c - - - - - -', user=user) | |
61 | test_line('c 1234 - - - - -', user=user) | |
62 | test_line('t - - -', user=user) | |
63 | test_line('T - - -', user=user) | |
64 | test_line('a - - -', user=user) | |
65 | test_line('A - - -', user=user) | |
66 | test_line('h - - -', user=user) | |
67 | test_line('H - - -', user=user) | |
68 | ||
a6dffbb7 | 69 | def test_uninitialized_t(): |
5a8575ef ZJS |
70 | if os.getuid() == 0: |
71 | return | |
72 | ||
03025f46 ZJS |
73 | test_line('w /foo - - - - "specifier for --user %t"', |
74 | user=True, returncode=0, extra={'env':{}}) | |
75 | ||
76 | def test_content(line, expected, *, user, extra={}): | |
77 | d = tempfile.TemporaryDirectory(prefix='test-systemd-tmpfiles.') | |
78 | arg = d.name + '/arg' | |
79 | spec = line.format(arg) | |
80 | test_line(spec, user=user, returncode=0, extra=extra) | |
81 | content = open(arg).read() | |
82 | print('expect: {!r}\nactual: {!r}'.format(expected, content)) | |
83 | assert content == expected | |
84 | ||
85 | def test_valid_specifiers(*, user): | |
86 | test_content('f {} - - - - two words', 'two words', user=user) | |
87 | if id128: | |
df1172fe ZJS |
88 | try: |
89 | test_content('f {} - - - - %m', '{}'.format(id128.get_machine().hex), user=user) | |
90 | except AssertionError as e: | |
91 | print(e) | |
92 | print('/etc/machine-id: {!r}'.format(open('/etc/machine-id').read())) | |
93 | print('/proc/cmdline: {!r}'.format(open('/proc/cmdline').read())) | |
94 | print('skipping') | |
03025f46 ZJS |
95 | test_content('f {} - - - - %b', '{}'.format(id128.get_boot().hex), user=user) |
96 | test_content('f {} - - - - %H', '{}'.format(socket.gethostname()), user=user) | |
97 | test_content('f {} - - - - %v', '{}'.format(os.uname().release), user=user) | |
98 | test_content('f {} - - - - %U', '{}'.format(os.getuid()), user=user) | |
b75f0c69 | 99 | test_content('f {} - - - - %G', '{}'.format(os.getgid()), user=user) |
03025f46 | 100 | |
b75f0c69 DC |
101 | puser = pwd.getpwuid(os.getuid()) |
102 | test_content('f {} - - - - %u', '{}'.format(puser.pw_name), user=user) | |
103 | ||
104 | pgroup = grp.getgrgid(os.getgid()) | |
105 | test_content('f {} - - - - %g', '{}'.format(pgroup.gr_name), user=user) | |
2f813b8a ZJS |
106 | |
107 | # Note that %h is the only specifier in which we look the environment, | |
108 | # because we check $HOME. Should we even be doing that? | |
109 | home = os.path.expanduser("~") | |
110 | test_content('f {} - - - - %h', '{}'.format(home), user=user) | |
03025f46 ZJS |
111 | |
112 | xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR') | |
113 | if xdg_runtime_dir is not None or not user: | |
114 | test_content('f {} - - - - %t', | |
115 | xdg_runtime_dir if user else '/run', | |
116 | user=user) | |
117 | ||
118 | xdg_config_home = os.getenv('XDG_CONFIG_HOME') | |
119 | if xdg_config_home is not None or not user: | |
120 | test_content('f {} - - - - %S', | |
121 | xdg_config_home if user else '/var/lib', | |
122 | user=user) | |
123 | ||
124 | xdg_cache_home = os.getenv('XDG_CACHE_HOME') | |
125 | if xdg_cache_home is not None or not user: | |
126 | test_content('f {} - - - - %C', | |
127 | xdg_cache_home if user else '/var/cache', | |
128 | user=user) | |
129 | ||
130 | if xdg_config_home is not None or not user: | |
131 | test_content('f {} - - - - %L', | |
132 | xdg_config_home + '/log' if user else '/var/log', | |
133 | user=user) | |
134 | ||
135 | test_content('f {} - - - - %%', '%', user=user) | |
d9daae55 ZJS |
136 | |
137 | if __name__ == '__main__': | |
5a8575ef ZJS |
138 | test_invalids(user=False) |
139 | test_invalids(user=True) | |
a6dffbb7 | 140 | test_uninitialized_t() |
03025f46 ZJS |
141 | |
142 | test_valid_specifiers(user=False) | |
143 | test_valid_specifiers(user=True) |