mirror of
https://github.com/Dasharo/systemd.git
synced 2026-03-06 15:02:31 -08:00
Merge pull request #26716 from mrc0mmand/more-tests
test: add a couple of tests for systemd-escape and systemd-id128
This commit is contained in:
@@ -97,7 +97,7 @@ static CreditEntropy may_credit(int seed_fd) {
|
||||
}
|
||||
|
||||
/* Don't credit the random seed if we are in first-boot mode, because we are supposed to start from
|
||||
* scratch. This is a safety precaution for cases where we people ship "golden" images with empty
|
||||
* scratch. This is a safety precaution for cases where people ship "golden" images with empty
|
||||
* /etc but populated /var that contains a random seed. */
|
||||
r = RET_NERRNO(access("/run/systemd/first-boot", F_OK));
|
||||
if (r == -ENOENT)
|
||||
|
||||
102
test/units/testsuite-74.escape.sh
Executable file
102
test/units/testsuite-74.escape.sh
Executable file
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
set -eux
|
||||
set -o pipefail
|
||||
|
||||
# shellcheck source=test/units/assert.sh
|
||||
. "$(dirname "$0")"/assert.sh
|
||||
|
||||
# Simple wrapper to check both escaping and unescaping of given strings
|
||||
# Arguments:
|
||||
# $1 - expected unescaped string
|
||||
# $2 - expected escaped string
|
||||
# $3 - optional arguments for systemd-escape
|
||||
check_escape() {
|
||||
unescaped="${1?}"
|
||||
escaped="${2?}"
|
||||
shift 2
|
||||
|
||||
assert_eq "$(systemd-escape "$@" -- "$unescaped")" "$escaped"
|
||||
assert_eq "$(systemd-escape "$@" --unescape -- "$escaped")" "$unescaped"
|
||||
}
|
||||
|
||||
systemd-escape --help
|
||||
systemd-escape --version
|
||||
|
||||
check_escape '' ''
|
||||
check_escape 'hello' 'hello'
|
||||
check_escape 'hello-world' 'hello\x2dworld'
|
||||
check_escape '-+ěščřž---🤔' '\x2d\x2b\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\x2d\x2d\x2d\xf0\x9f\xa4\x94'
|
||||
check_escape '/this/is/a/path/a b c' '-this-is-a-path-a\x20b\x20c'
|
||||
|
||||
# Multiple strings to escape/unescape
|
||||
assert_eq "$(systemd-escape 'hello-world' '/dev/loop1' 'template@🐍')" \
|
||||
'hello\x2dworld -dev-loop1 template\x40\xf0\x9f\x90\x8d'
|
||||
assert_eq "$(systemd-escape --unescape -- 'hello\x2dworld' '-dev-loop1' 'template\x40\xf0\x9f\x90\x8d')" \
|
||||
'hello-world /dev/loop1 template@🐍'
|
||||
|
||||
# --suffix= is not compatible with --unescape
|
||||
assert_eq "$(systemd-escape --suffix=mount -- '-+ěščřž---🤔')" \
|
||||
'\x2d\x2b\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\x2d\x2d\x2d\xf0\x9f\xa4\x94.mount'
|
||||
assert_eq "$(systemd-escape --suffix=timer 'this has spaces')" \
|
||||
'this\x20has\x20spaces.timer'
|
||||
assert_eq "$(systemd-escape --suffix=service 'trailing-spaces ')" \
|
||||
'trailing\x2dspaces\x20\x20.service'
|
||||
assert_eq "$(systemd-escape --suffix=automount ' leading-spaces')" \
|
||||
'\x20\x20\x20leading\x2dspaces.automount'
|
||||
|
||||
# --template=
|
||||
check_escape 'hello' 'hello@hello.service' --template=hello@.service
|
||||
check_escape ' what - is _ love? 🤔 ¯\_(ツ)_/¯' \
|
||||
'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' \
|
||||
--template=hello@.service
|
||||
check_escape '/this/is/where/my/stuff/is/ with spaces though ' \
|
||||
'mount-my-stuff@-this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service' \
|
||||
--template=mount-my-stuff@.service
|
||||
check_escape '/this/is/where/my/stuff/is/ with spaces though ' \
|
||||
'mount-my-stuff@this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service' \
|
||||
--template=mount-my-stuff@.service --path
|
||||
|
||||
# --instance (must be used with --unescape)
|
||||
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')" \
|
||||
' what - is _ love? 🤔 ¯\_(ツ)_/¯'
|
||||
assert_eq "$(systemd-escape --unescape --instance 'mount-my-stuff@-this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service')" \
|
||||
'/this/is/where/my/stuff/is/ with spaces though '
|
||||
assert_eq "$(systemd-escape --unescape --instance --path 'mount-my-stuff@this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service')" \
|
||||
'/this/is/where/my/stuff/is/ with spaces though '
|
||||
|
||||
# --path
|
||||
check_escape / '-' --path
|
||||
check_escape '/hello/world' 'hello-world' --path
|
||||
check_escape '/mnt/smb/おにぎり' \
|
||||
'mnt-smb-\xe3\x81\x8a\xe3\x81\xab\xe3\x81\x8e\xe3\x82\x8a' \
|
||||
--path
|
||||
|
||||
# --mangle
|
||||
assert_eq "$(systemd-escape --mangle 'hello-world')" 'hello-world.service'
|
||||
assert_eq "$(systemd-escape --mangle '/mount/this')" 'mount-this.mount'
|
||||
assert_eq "$(systemd-escape --mangle 'my-service@ 🐱 ')" 'my-service@\x20\xf0\x9f\x90\xb1\x20.service'
|
||||
assert_eq "$(systemd-escape --mangle '/dev/disk/by-emoji/🍎')" 'dev-disk-by\x2demoji-\xf0\x9f\x8d\x8e.device'
|
||||
assert_eq "$(systemd-escape --mangle 'daily-existential-crisis .timer')" 'daily-existential-crisis\x20.timer'
|
||||
assert_eq "$(systemd-escape --mangle 'trailing-whitespace.mount ')" 'trailing-whitespace.mount\x20.service'
|
||||
|
||||
(! systemd-escape)
|
||||
(! systemd-escape --suffix='' hello)
|
||||
(! systemd-escape --suffix=invalid hello)
|
||||
(! systemd-escape --suffix=mount --template=hello@.service hello)
|
||||
(! systemd-escape --suffix=mount --mangle)
|
||||
(! systemd-escape --template='')
|
||||
(! systemd-escape --template=@)
|
||||
(! systemd-escape --template='hello@.service' '')
|
||||
(! systemd-escape --unescape --template='hello@.service' '@hello.service')
|
||||
(! systemd-escape --unescape --template='hello@.service' 'hello@.service')
|
||||
(! systemd-escape --mangle --template=hello@.service hello)
|
||||
(! systemd-escape --instance 'hello@hello.service')
|
||||
(! systemd-escape --instance --template=hello@.service 'hello@hello.service')
|
||||
(! systemd-escape --unescape --instance --path 'mount-my-stuff@-this-is-where-my-stuff-is-\x20with\x20spaces\x20though\x20.service')
|
||||
(! systemd-escape --path '/../hello')
|
||||
(! systemd-escape --path '.')
|
||||
(! systemd-escape --path '..')
|
||||
(! systemd-escape --path "$(set +x; printf '%0.sa' {0..256})")
|
||||
(! systemd-escape --unescape --path '')
|
||||
(! systemd-escape --mangle '')
|
||||
41
test/units/testsuite-74.id128.sh
Executable file
41
test/units/testsuite-74.id128.sh
Executable file
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
set -eux
|
||||
set -o pipefail
|
||||
|
||||
# shellcheck source=test/units/assert.sh
|
||||
. "$(dirname "$0")"/assert.sh
|
||||
|
||||
systemd-id128 --help
|
||||
systemd-id128 help
|
||||
systemd-id128 show
|
||||
systemd-id128 show --pretty | tail -n10
|
||||
systemd-id128 show 4f68bce3e8cd4db196e7fbcaf984b709 # root-x86-64
|
||||
systemd-id128 show --pretty 4f68bce3e8cd4db196e7fbcaf984b709
|
||||
|
||||
[[ "$(systemd-id128 new | wc -c)" -eq 33 ]]
|
||||
systemd-id128 new -p
|
||||
systemd-id128 new -u
|
||||
systemd-id128 new -a 4f68bce3e8cd4db196e7fbcaf984b709
|
||||
|
||||
systemd-id128 machine-id
|
||||
systemd-id128 machine-id --pretty
|
||||
systemd-id128 machine-id --uuid
|
||||
systemd-id128 machine-id --app-specific=4f68bce3e8cd4db196e7fbcaf984b709
|
||||
assert_eq "$(systemd-id128 machine-id)" "$(</etc/machine-id)"
|
||||
|
||||
systemd-id128 boot-id
|
||||
systemd-id128 boot-id --pretty
|
||||
systemd-id128 boot-id --uuid
|
||||
systemd-id128 boot-id --app-specific=4f68bce3e8cd4db196e7fbcaf984b709
|
||||
assert_eq "$(systemd-id128 boot-id --uuid)" "$(</proc/sys/kernel/random/boot_id)"
|
||||
|
||||
# shellcheck disable=SC2016
|
||||
systemd-run --wait --pipe bash -euxc '[[ $INVOCATION_ID == "$(systemd-id128 invocation-id)" ]]'
|
||||
|
||||
(! systemd-id128)
|
||||
(! systemd-id128 new -a '')
|
||||
(! systemd-id128 new -a '0')
|
||||
(! systemd-id128 invocation-id -a 4f68bce3e8cd4db196e7fbcaf984b709)
|
||||
(! systemd-id128 show '')
|
||||
(! systemd-id128 show "$(set +x; printf '%0.s0' {0..64})")
|
||||
Reference in New Issue
Block a user