]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-74.escape.sh
Rework unit_name_mangle_with_suffix() to (very slightly) simplify the path
[thirdparty/systemd.git] / test / units / testsuite-74.escape.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -eux
4 set -o pipefail
5
6 # shellcheck source=test/units/util.sh
7 . "$(dirname "$0")"/util.sh
8
9 # Simple wrapper to check both escaping and unescaping of given strings
10 # Arguments:
11 # $1 - expected unescaped string
12 # $2 - expected escaped string
13 # $3 - optional arguments for systemd-escape
14 check_escape() {
15 unescaped="${1?}"
16 escaped="${2?}"
17 shift 2
18
19 assert_eq "$(systemd-escape "$@" -- "$unescaped")" "$escaped"
20 assert_eq "$(systemd-escape "$@" --unescape -- "$escaped")" "$unescaped"
21 }
22
23 systemd-escape --help
24 systemd-escape --version
25
26 check_escape '' ''
27 check_escape 'hello' 'hello'
28 check_escape 'hello-world' 'hello\x2dworld'
29 check_escape '-+ěščřž---🤔' '\x2d\x2b\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\x2d\x2d\x2d\xf0\x9f\xa4\x94'
30 check_escape '/this/is/a/path/a b c' '-this-is-a-path-a\x20b\x20c'
31
32 # Multiple strings to escape/unescape
33 assert_eq "$(systemd-escape 'hello-world' '/dev/loop1' 'template@🐍')" \
34 'hello\x2dworld -dev-loop1 template\x40\xf0\x9f\x90\x8d'
35 assert_eq "$(systemd-escape --unescape -- 'hello\x2dworld' '-dev-loop1' 'template\x40\xf0\x9f\x90\x8d')" \
36 'hello-world /dev/loop1 template@🐍'
37
38 # --suffix= is not compatible with --unescape
39 assert_eq "$(systemd-escape --suffix=mount -- '-+ěščřž---🤔')" \
40 '\x2d\x2b\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\x2d\x2d\x2d\xf0\x9f\xa4\x94.mount'
41 assert_eq "$(systemd-escape --suffix=timer 'this has spaces')" \
42 'this\x20has\x20spaces.timer'
43 assert_eq "$(systemd-escape --suffix=service 'trailing-spaces ')" \
44 'trailing\x2dspaces\x20\x20.service'
45 assert_eq "$(systemd-escape --suffix=automount ' leading-spaces')" \
46 '\x20\x20\x20leading\x2dspaces.automount'
47
48 # --template=
49 check_escape 'hello' 'hello@hello.service' --template=hello@.service
50 check_escape ' what - is _ love? 🤔 ¯\_(ツ)_/¯' \
51 'hello@\x20\x20what\x20\x2d\x20is\x20_\x20love\x3f\x20\xf0\x9f\xa4\x94\x20\xc2\xaf\x5c_\x28\xe3\x83\x84\x29_-\xc2\xaf.service' \
52 --template=hello@.service
53 check_escape '/this/is/where/my/stuff/is/ with spaces though ' \
54 'mount-my-stuff@-this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service' \
55 --template=mount-my-stuff@.service
56 check_escape '/this/is/where/my/stuff/is/ with spaces though ' \
57 'mount-my-stuff@this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service' \
58 --template=mount-my-stuff@.service --path
59
60 # --instance (must be used with --unescape)
61 assert_eq "$(systemd-escape --unescape --instance 'hello@\x20\x20what\x20\x2d\x20is\x20_\x20love\x3f\x20\xf0\x9f\xa4\x94\x20\xc2\xaf\x5c_\x28\xe3\x83\x84\x29_-\xc2\xaf.service')" \
62 ' what - is _ love? 🤔 ¯\_(ツ)_/¯'
63 assert_eq "$(systemd-escape --unescape --instance 'mount-my-stuff@-this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service')" \
64 '/this/is/where/my/stuff/is/ with spaces though '
65 assert_eq "$(systemd-escape --unescape --instance --path 'mount-my-stuff@this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service')" \
66 '/this/is/where/my/stuff/is/ with spaces though '
67
68 # --path, reversible cases
69 check_escape / '-' --path
70 check_escape '/hello/world' 'hello-world' --path
71 check_escape '/mnt/smb/おにぎり' \
72 'mnt-smb-\xe3\x81\x8a\xe3\x81\xab\xe3\x81\x8e\xe3\x82\x8a' \
73 --path
74
75 # --path, non-reversible cases
76 assert_eq "$(systemd-escape --path ///////////////)" '-'
77 assert_eq "$(systemd-escape --path /..)" '-'
78 assert_eq "$(systemd-escape --path /../.././../.././)" '-'
79 assert_eq "$(systemd-escape --path /../.././../.././foo)" 'foo'
80
81 # --mangle
82 assert_eq "$(systemd-escape --mangle 'hello-world')" 'hello-world.service'
83 assert_eq "$(systemd-escape --mangle '/mount/this')" 'mount-this.mount'
84 assert_eq "$(systemd-escape --mangle 'my-service@ 🐱 ')" 'my-service@\x20\xf0\x9f\x90\xb1\x20.service'
85 assert_eq "$(systemd-escape --mangle '/dev/disk/by-emoji/🍎')" 'dev-disk-by\x2demoji-\xf0\x9f\x8d\x8e.device'
86 assert_eq "$(systemd-escape --mangle 'daily-existential-crisis .timer')" 'daily-existential-crisis\x20.timer'
87 assert_eq "$(systemd-escape --mangle 'trailing-whitespace.mount ')" 'trailing-whitespace.mount\x20.service'
88
89 (! systemd-escape)
90 (! systemd-escape --suffix='' hello)
91 (! systemd-escape --suffix=invalid hello)
92 (! systemd-escape --suffix=mount --template=hello@.service hello)
93 (! systemd-escape --suffix=mount --mangle)
94 (! systemd-escape --template='')
95 (! systemd-escape --template=@)
96 (! systemd-escape --template='hello@.service' '')
97 (! systemd-escape --unescape --template='hello@.service' '@hello.service')
98 (! systemd-escape --unescape --template='hello@.service' 'hello@.service')
99 (! systemd-escape --mangle --template=hello@.service hello)
100 (! systemd-escape --instance 'hello@hello.service')
101 (! systemd-escape --instance --template=hello@.service 'hello@hello.service')
102 (! systemd-escape --unescape --instance --path 'mount-my-stuff@-this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service')
103 (! systemd-escape --path '/../hello/..')
104 (! systemd-escape --path '.')
105 (! systemd-escape --path '..')
106 (! systemd-escape --path "$(set +x; printf '%0.sa' {0..256})")
107 (! systemd-escape --unescape --path '')
108 (! systemd-escape --mangle '')