HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/units/TEST-72-SYSUPDATE.sh

92,911 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: ba98dfce837b46f3d78a38ff55a45d19a167895f

Lines 1001–2000 of 2,397

rm -rf "$ROOT_CLEANUP"
mkdir -p "$ROOT_CLEANUP_ROOT/etc/sysupdate.d" \
         "$ROOT_CLEANUP_ROOT/target" \
         "$ROOT_CLEANUP_ROOT/var/lib/systemd/sysupdate/installdb"
 
cat >"$ROOT_CLEANUP_ROOT/etc/sysupdate.d/01-alpha.transfer" <<EOF
[Source]
Type=regular-file
Path=/source
MatchPattern=alpha-@v.bin
 
[Target]
Type=regular-file
Path=/target
MatchPattern=alpha-@v.bin
InstancesMax=2
EOF
 
echo "$RANDOM" >"$ROOT_CLEANUP_ROOT/target/alpha-v1.bin"
ln -s "$ROOT_CLEANUP_DB_VALUE" \
      "$ROOT_CLEANUP_ROOT/var/lib/systemd/sysupdate/installdb/$ROOT_CLEANUP_DB_KEY"
 
"$SYSUPDATE" --root="$ROOT_CLEANUP_ROOT" --verify=no cleanup
test -f "$ROOT_CLEANUP_ROOT/target/alpha-v1.bin"
test -L "$ROOT_CLEANUP_ROOT/var/lib/systemd/sysupdate/installdb/$ROOT_CLEANUP_DB_KEY"
 
rm -rf "$ROOT_CLEANUP"
 
# Briefly check the "--component-all" switch of the "cleanup" verb. Each component
# keeps its own install database (installdb.<component>), and "cleanup
# --component-all" must clean up orphaned resources across *all* of them in one
# go. Set up two components, install a resource into each, then drop both transfer
# files and run a single "cleanup --component-all" — it must remove both
# components' resources (and their install database entries).
COMPALL="$WORKDIR/component-all"
rm -rf "$COMPALL" /run/sysupdate.comp-a.d /run/sysupdate.comp-b.d \
    /var/lib/systemd/sysupdate/installdb.comp-a /var/lib/systemd/sysupdate/installdb.comp-b
mkdir -p "$COMPALL/source" "$COMPALL/target-a" "$COMPALL/target-b" \
    /run/sysupdate.comp-a.d /run/sysupdate.comp-b.d
 
echo "$RANDOM" >"$COMPALL/source/comp-a-v1.bin"
echo "$RANDOM" >"$COMPALL/source/comp-b-v1.bin"
(cd "$COMPALL/source" && sha256sum comp-* >SHA256SUMS)
 
cat >/run/sysupdate.comp-a.d/01-comp-a.transfer <<EOF
[Source]
Type=regular-file
Path=$COMPALL/source
MatchPattern=comp-a-@v.bin
 
[Target]
Type=regular-file
Path=$COMPALL/target-a
MatchPattern=comp-a-@v.bin
InstancesMax=1
EOF
 
cat >/run/sysupdate.comp-b.d/01-comp-b.transfer <<EOF
[Source]
Type=regular-file
Path=$COMPALL/source
MatchPattern=comp-b-@v.bin
 
[Target]
Type=regular-file
Path=$COMPALL/target-b
MatchPattern=comp-b-@v.bin
InstancesMax=1
EOF
 
"$SYSUPDATE" --component=comp-a --verify=no update
"$SYSUPDATE" --component=comp-b --verify=no update
test -f "$COMPALL/target-a/comp-a-v1.bin"
test -f "$COMPALL/target-b/comp-b-v1.bin"
test -d /var/lib/systemd/sysupdate/installdb.comp-a
test -d /var/lib/systemd/sysupdate/installdb.comp-b
 
# --component-all is not supported for every verb, so it must be refused where it isn't (e.g. "vacuum").
# (It *is* supported for update/acquire/cleanup/enable-*/disable-*, which is exercised further down.)
(! "$SYSUPDATE" --component-all --verify=no vacuum)
 
# With the transfer files still in place "cleanup --component-all" is a no-op:
# nothing is orphaned.
"$SYSUPDATE" --component-all cleanup
test -f "$COMPALL/target-a/comp-a-v1.bin"
test -f "$COMPALL/target-b/comp-b-v1.bin"
 
# Drop both transfer files and clean up all components at once. Both resources
# (and their install database entries) must now be gone.
rm /run/sysupdate.comp-a.d/01-comp-a.transfer /run/sysupdate.comp-b.d/01-comp-b.transfer
"$SYSUPDATE" --component-all cleanup
test ! -e "$COMPALL/target-a/comp-a-v1.bin"
test ! -e "$COMPALL/target-b/comp-b-v1.bin"
[[ "$(find /var/lib/systemd/sysupdate/installdb.comp-a -type l | wc -l)" -eq 0 ]]
[[ "$(find /var/lib/systemd/sysupdate/installdb.comp-b -type l | wc -l)" -eq 0 ]]
 
rm -rf "$COMPALL" /run/sysupdate.comp-a.d /run/sysupdate.comp-b.d \
    /var/lib/systemd/sysupdate/installdb.comp-a /var/lib/systemd/sysupdate/installdb.comp-b
 
# Check the "--cleanup=" switch of the "update" verb. With "--cleanup=yes" a
# successful update must, after installing the new version, run the equivalent of
# the "cleanup" verb and remove any resources that are no longer owned by a
# currently defined transfer file. Reuse the "alpha"/"beta" helpers from above.
rm -rf "$CONFIGDIR" "$INSTALLDB" "$CLEANUP"
mkdir -p "$CONFIGDIR" "$CLEANUP/source" "$CLEANUP/target"
 
cat >"$CONFIGDIR/01-alpha.transfer" <<EOF
[Source]
Type=regular-file
Path=$CLEANUP/source
MatchPattern=alpha-@v.bin
 
[Target]
Type=regular-file
Path=$CLEANUP/target
MatchPattern=alpha-@v.bin
InstancesMax=2
EOF
 
cat >"$CONFIGDIR/02-beta.transfer" <<EOF
[Source]
Type=directory
Path=$CLEANUP/source
MatchPattern=beta-@v
 
[Target]
Type=directory
Path=$CLEANUP/target
MatchPattern=beta-@v
InstancesMax=2
EOF
 
# Install a first version with both transfers in place.
cleanup_new_version v1
"$SYSUPDATE" --verify=no update --cleanup=yes
test -f "$CLEANUP/target/alpha-v1.bin"
verify_beta_synced v1
[[ "$(installdb_count)" -eq 2 ]]
assert_installdb_covers_target
 
# Now drop the "beta" transfer file and install a second version with
# "--cleanup=yes". The new alpha resource must be installed, and the now-orphaned
# beta directory (and its install database entry) must be removed as part of the
# same invocation, without a separate "cleanup" call.
rm "$CONFIGDIR/02-beta.transfer"
cleanup_new_version v2
"$SYSUPDATE" --verify=no update --cleanup=yes
test -f "$CLEANUP/target/alpha-v1.bin"
test -f "$CLEANUP/target/alpha-v2.bin"
test ! -e "$CLEANUP/target/beta-v1"
[[ "$(installdb_count)" -eq 1 ]]
assert_installdb_covers_target
 
# With "--cleanup=no" (the default) orphaned resources must be left in place.
# Redefine the "alpha" transfer so its patterns no longer match the already
# installed alpha files (turning them into orphans), while keeping a valid
# transfer definition in place. Updating with "--cleanup=no" must then install
# nothing new (there's no matching source) and leave the now-orphaned alpha files
# and their install database entry untouched.
cat >"$CONFIGDIR/01-alpha.transfer" <<EOF
[Source]
Type=regular-file
Path=$CLEANUP/source
MatchPattern=gamma-@v.bin
 
[Target]
Type=regular-file
Path=$CLEANUP/target
MatchPattern=gamma-@v.bin
InstancesMax=2
EOF
"$SYSUPDATE" --verify=no update --cleanup=no
test -f "$CLEANUP/target/alpha-v1.bin"
test -f "$CLEANUP/target/alpha-v2.bin"
[[ "$(installdb_count)" -eq 1 ]]
 
# Invoking the "cleanup" verb with "--cleanup=no" is contradictory and must be
# refused.
(! "$SYSUPDATE" --cleanup=no cleanup) |& grep "contradictory" >/dev/null
 
# A plain "cleanup" must still remove the orphaned alpha files.
"$SYSUPDATE" cleanup
test ! -f "$CLEANUP/target/alpha-v1.bin"
test ! -f "$CLEANUP/target/alpha-v2.bin"
[[ "$(installdb_count)" -eq 0 ]]
 
rm -rf "$CONFIGDIR" "$INSTALLDB" "$CLEANUP"
 
# Verify the notification callout: after a successful update, sysupdate must connect to every socket in
# /run/systemd/sysupdate/notify/ and invoke io.systemd.SysUpdate.Notify.OnCompletedUpdate(). We hook a tiny
# recorder socket into that directory that captures the request and replies with success.
NOTIFY_LOG="$WORKDIR/notify.log"
rm -f "$NOTIFY_LOG"
 
cat >"$WORKDIR/notify-recorder.py" <<EOF
#!/usr/bin/env python3
# Minimal Varlink server: read one NUL-terminated request, record it, reply with empty parameters.
import sys
buf = b""
while True:
    c = sys.stdin.buffer.read(1)
    if not c or c == b"\x00":
        break
    buf += c
with open("$NOTIFY_LOG", "ab") as f:
    f.write(buf + b"\n")
sys.stdout.buffer.write(b'{"parameters":{}}\x00')
sys.stdout.buffer.flush()
EOF
chmod +x "$WORKDIR/notify-recorder.py"
 
cat >/run/systemd/system/test-sysupdate-notify-recorder.socket <<EOF
[Socket]
ListenStream=/run/systemd/sysupdate/notify/io.test.SysUpdateRecorder
Accept=yes
EOF
 
cat >"/run/systemd/system/test-sysupdate-notify-recorder@.service" <<EOF
[Service]
ExecStart=$WORKDIR/notify-recorder.py
StandardInput=socket
StandardOutput=socket
EOF
 
systemctl daemon-reload
systemctl start test-sysupdate-notify-recorder.socket
 
rm -rf "$CONFIGDIR" "$WORKDIR/blobs"
mkdir -p "$CONFIGDIR" "$WORKDIR/blobs"
echo "hello" >"$WORKDIR/source/notifytest-v1.bin"
(cd "$WORKDIR/source" && sha256sum notifytest-v1.bin >SHA256SUMS)
cat >"$CONFIGDIR/01-notifytest.transfer" <<EOF
[Source]
Type=url-file
Path=file://$WORKDIR/source
MatchPattern=notifytest-@v.bin
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=notifytest-@v.bin
InstancesMax=1
EOF
 
# A real update must trigger exactly one notification carrying the version and the updated resources.
# The callout is synchronous (sysupdate blocks until the subscriber replied, which happens after the
# request was recorded), so the log is fully written by the time the update returns.
"$SYSUPDATE" --verify=no update
test -s "$NOTIFY_LOG"  # the notification must have been recorded
notify_line="$(tail -n1 "$NOTIFY_LOG")"
echo "Recorded notification: $notify_line"
jq -e '.method == "io.systemd.SysUpdate.Notify.OnCompletedUpdate"' <<<"$notify_line" >/dev/null
jq -e '.parameters.version == "v1"' <<<"$notify_line" >/dev/null
jq -e '.parameters.resources | length >= 1' <<<"$notify_line" >/dev/null
jq -e '.parameters.resources | all(has("transfer"))' <<<"$notify_line" >/dev/null
 
# A no-op update ("No update needed") must NOT emit a notification.
rm -f "$NOTIFY_LOG"
"$SYSUPDATE" --verify=no update
test ! -s "$NOTIFY_LOG"
 
systemctl stop test-sysupdate-notify-recorder.socket
rm -f /run/systemd/system/test-sysupdate-notify-recorder.socket \
      /run/systemd/system/test-sysupdate-notify-recorder@.service
systemctl daemon-reload
rm -rf "$CONFIGDIR" "$WORKDIR/blobs"
rm -f "$WORKDIR/source/notifytest-v1.bin" "$WORKDIR/source/SHA256SUMS" \
      "$WORKDIR/notify-recorder.py" "$NOTIFY_LOG"
 
verify_zero_fill_partition() {
    local blockdev="${1:?}"
    local sigfile="${2:?}"
    local version="${3:?}"
    local expect_zeroed="${4:?}"
    local start size dump tail
 
    start="$(sfdisk --json "$blockdev" | jq -r ".partitiontable.partitions[] | select(.name == \"sig-$version\") | .start")"
    test -n "$start"
    size="$(stat -c %s "$sigfile")"
    dump="$WORKDIR/zerofill-dump"
 
    dd if="$blockdev" of="$dump" bs=512 skip="$start" count=64 status=none
 
    # The partition must start with the payload file in any case
    cmp -n "$size" "$dump" "$sigfile"
 
    # Everything after it must be NUL bytes if zero filling is on, and must not be if it is off (the tail of
    # the longer payload of the version previously stored in the partition then survives)
    tail="$(tail -c +$((size + 1)) "$dump" | tr -d '\0' | wc -c)"
    if [[ "$expect_zeroed" == "1" ]]; then
        test "$tail" -eq 0
    else
        test "$tail" -gt 0
    fi
}
 
make_zero_fill_payload() {
    local size="${1:?}"
    local file="${2:?}"
 
    # Generates a JSON object resembling a verity signature of precisely the requested size: the fixed part of
    # the object is 94 bytes, the signature string makes up the rest
    test "$size" -gt 94
    printf '{"rootHash":"%s","signature":"%s"}' \
           "$(head -c 32 /dev/zero | od -An -v -tx1 | tr -d ' \n')" \
           "$(head -c $((size - 94)) /dev/zero | tr '\0' A)" >"$file"
    test "$(stat -c %s "$file")" -eq "$size"
}
 
test_zero_fill_partition() {
    local source_type="${1:?}"      # Source Type= to use (regular-file or url-file)
    local ptype_uuid="${2:?}"       # partition type UUID to create the partitions with
    local ptype_name="${3:?}"       # matching partition type identifier for MatchPartitionType=
    local setting="${4}"            # explicit PartitionZeroFill= line, or empty to test the default
    local expect_zeroed="${5:?}"    # whether the tail of the partition is expected to be zeroed out
    local in_use="${6:-no}"
    local sigdir="$WORKDIR/zerofill-source"
    local defdir="$WORKDIR/zerofill-defs"
    local backing="$WORKDIR/zerofill-disk.raw"
    local backing_size=4M
    local -a losetup_args=()
    local blockdev source_path
 
    rm -rf "$sigdir" "$defdir" "$backing"
    mkdir -p "$sigdir" "$defdir"
    if [[ "$in_use" == yes ]]; then
        backing_size=16M
        losetup_args+=(--partscan)
    fi
    truncate -s "$backing_size" "$backing"
 
    if [[ -e /dev/loop-control ]]; then
        blockdev="$(losetup --find --show "${losetup_args[@]}" "$backing")"
        ZEROFILL_LOOP="$blockdev"
    else
        blockdev="$backing"
    fi
 
    sfdisk "$blockdev" <<EOF
label: gpt
unit: sectors
 
size=64, type=$ptype_uuid, name=_empty
size=64, type=$ptype_uuid, name=_empty
EOF
 
    if [[ "$in_use" == yes ]]; then
        sfdisk --append "$blockdev" <<EOF
size=16384, type=0fc63daf-8483-4772-8e79-3d69d8477de4, name=mounted
EOF
        udevadm wait --timeout=30 --settle "${blockdev}p3"
        mkfs.ext4 -q "${blockdev}p3"
        mkdir -p "$WORKDIR/zerofill-mount"
        mount -o ro "${blockdev}p3" "$WORKDIR/zerofill-mount"
        ZEROFILL_MOUNT="$WORKDIR/zerofill-mount"
    fi
 
    if [[ "$source_type" == "url-file" ]]; then
        source_path="file://$sigdir"
    else
        source_path="$sigdir"
    fi
 
    cat >"$defdir/01-zerofill.transfer" <<EOF
[Source]
Type=$source_type
Path=$source_path
MatchPattern=sig-@v.raw
 
[Target]
Type=partition
Path=$blockdev
MatchPattern=sig-@v
MatchPartitionType=$ptype_name
$setting
EOF
 
    # The partition slots are 64 sectors (32768 bytes) each. Payload sizes and the tail state to expect in the
    # partition after each version, given that with two slots each version replaces the one two steps before:
    #
    #   v1: 3000 bytes into an empty slot, hence nothing can survive after it
    #   v2: exactly fills the slot, nothing is left to zero out at all
    #   v3: 130 bytes over the 3000 of v1, the tail of v1 survives unless zero filling is on
    #   v4: leaves less than a sector free after it over the full slot of v2, i.e. the remainder is too small
    #       for BLKZEROOUT, and the tail of v2 survives unless zero filling is on
    local -a versions=(v1 v2 v3 v4)
    local -A payload_size=([v1]=3000 [v2]=32768 [v3]=130 [v4]=32668)
    local -A zeroed=([v1]=1 [v2]=1 [v3]="$expect_zeroed" [v4]="$expect_zeroed")
    local v prev=
 
    for v in "${versions[@]}"; do
        make_zero_fill_payload "${payload_size[$v]}" "$sigdir/sig-$v.raw"
        (cd "$sigdir" && sha256sum sig-*.raw >SHA256SUMS)
 
        "$SYSUPDATE" --verify=no --definitions="$defdir" update
        verify_zero_fill_partition "$blockdev" "$sigdir/sig-$v.raw" "$v" "${zeroed[$v]}"
 
        # The version written in the previous step is in the other slot, and must be untouched, i.e. the zero
        # filling must not have overshot the partition
        if [[ -n "$prev" ]]; then
            verify_zero_fill_partition "$blockdev" "$sigdir/sig-$prev.raw" "$prev" "${zeroed[$prev]}"
        fi
        prev="$v"
    done
 
    # v3 and v4 must have replaced v1 and v2
    (! sfdisk --json "$blockdev" | jq -e '.partitiontable.partitions[] | select(.name == "sig-v1" or .name == "sig-v2")' >/dev/null)
 
    # And the partition table itself (including the backup GPT at the end of the device) must be intact
    sfdisk --verify "$blockdev"
 
    if [[ "$ZEROFILL_MOUNT" != "" ]]; then
        umount "$ZEROFILL_MOUNT"
        ZEROFILL_MOUNT=
    fi
    if [[ "$ZEROFILL_LOOP" != "" ]]; then
        losetup --detach "$ZEROFILL_LOOP"
        ZEROFILL_LOOP=
    fi
 
    rm -rf "$sigdir" "$defdir" "$backing" "$WORKDIR/zerofill-mount"
}
 
test_zero_fill() {
    # A verity signature partition carries a JSON object that must be followed by NUL bytes only. The split-out
    # artifact is not padded (so that it's a valid JSON text file), hence sysupdate must make sure that no tail
    # of a previously installed, longer signature survives when it installs a shorter one into a reused
    # partition, see https://github.com/systemd/systemd/issues/43567. This is controlled by the
    # PartitionZeroFill= setting, which defaults to on for verity signature partitions, and off otherwise.
    local vsig_uuid=41092b05-9fc8-4523-994f-2def0408b176    # root-x86-64-verity-sig
    local generic_uuid=0fc63daf-8483-4772-8e79-3d69d8477de4 # linux-generic
 
    # Default for verity signature partitions is on, both for local and URL sources
    test_zero_fill_partition regular-file "$vsig_uuid" root-x86-64-verity-sig "" 1
    test_zero_fill_partition url-file "$vsig_uuid" root-x86-64-verity-sig "" 1
 
    # ... and can be turned off explicitly
    test_zero_fill_partition regular-file "$vsig_uuid" root-x86-64-verity-sig "PartitionZeroFill=no" 0
 
    # Default for other partitions is off, but can be turned on explicitly
    test_zero_fill_partition regular-file "$generic_uuid" linux-generic "" 0
    test_zero_fill_partition regular-file "$generic_uuid" linux-generic "PartitionZeroFill=yes" 1
    test_zero_fill_partition url-file "$generic_uuid" linux-generic "PartitionZeroFill=yes" 1
 
    # A mounted partition prevents BLKZEROOUT from claiming the disk and invalidating dirty pages.
    # Updating other partitions must still work: https://github.com/systemd/systemd/issues/44317
    if [[ -e /dev/loop-control ]]; then
        test_zero_fill_partition regular-file "$vsig_uuid" root-x86-64-verity-sig "" 1 yes
        test_zero_fill_partition url-file "$vsig_uuid" root-x86-64-verity-sig "" 1 yes
    else
        echo "Skipping zero-fill tests with a mounted partition: no loopback device support."
    fi
}
 
test_signature_verification() {
    if ! command -v gpg >/dev/null; then
        echo "gpg not available, skipping signature verification test"
        return 0
    fi
 
    # Checking for --auto-key-import is not enough because the merge-only/import-clean guarantee we rely on
    # only works correctly with gpg 2.4
    local gpg_version gpg_rest
    gpg_version="$(gpg --version | sed -n '1p' | awk '{print $NF}')"
    gpg_rest="${gpg_version#*.}"
    if [ "${gpg_version%%.*}" -lt 2 ] || { [ "${gpg_version%%.*}" -eq 2 ] && [ "${gpg_rest%%.*}" -lt 4 ]; }; then
        echo "gpg $gpg_version too old (need >= 2.4), skipping signature verification test"
        return 0
    fi
 
    local sigdir="$WORKDIR/sigtest-source"
    local defdir="$WORKDIR/sigtest-defs"
    local gpghome="$WORKDIR/sigtest-gpghome"
    local other_home="$WORKDIR/sigtest-otherhome"
    local target="$WORKDIR/sigtest-target"
    local keyring="$WORKDIR/sigtest-keyring"
    local top_fpr keys
 
    SIGTEST_GPGHOME="$gpghome"
    SIGTEST_OTHERHOME="$other_home"
 
    mkdir -p "$sigdir" "$defdir" "$gpghome" "$other_home" "$target"
    chmod 700 "$gpghome" "$other_home"
 
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --quick-gen-key 'Test Key <test@example.com>' rsa2048 cert,sign never
    # Capture gpg output first, then grep from a here-string to avoid grep -m1 causing a SIGPIPE
    keys="$(GNUPGHOME="$gpghome" gpg --list-keys --with-colons)"
    top_fpr="$(grep -m1 '^fpr:' <<< "$keys" | cut -d: -f10)"
    test "$top_fpr" != ""
 
    GNUPGHOME="$gpghome" gpg --export --output "$keyring"
 
    dd if=/dev/urandom of="$sigdir/payload-v1.raw" bs=1024 count=8 status=none
    (cd "$sigdir" && sha256sum payload-v1.raw > SHA256SUMS)
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
 
    cat >"$defdir/01-sigtest.transfer" <<EOF
[Source]
Type=url-file
Path=file://$sigdir
MatchPattern=payload-@v.raw
 
[Target]
Type=regular-file
Path=$target
MatchPattern=payload-@v.raw
InstancesMax=3
EOF
 
    SYSTEMD_OPENPGP_KEYRING="$keyring" "$SYSUPDATE" --definitions="$defdir" check-new
    SYSTEMD_OPENPGP_KEYRING="$keyring" "$SYSUPDATE" --definitions="$defdir" update
    cmp "$sigdir/payload-v1.raw" "$target/payload-v1.raw"
 
    # Negative test: Sign with a key not in the keyring
    GNUPGHOME="$other_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --quick-gen-key 'Other Key <other@example.com>' rsa2048 cert,sign never
    dd if=/dev/urandom of="$sigdir/payload-v2.raw" bs=1024 count=8 status=none
    (cd "$sigdir" && sha256sum payload-v1.raw payload-v2.raw > SHA256SUMS)
    GNUPGHOME="$other_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
    if SYSTEMD_OPENPGP_KEYRING="$keyring" "$SYSUPDATE" --definitions="$defdir" update; then
        echo "ERROR: accepted an update signed by a key not in the keyring" >&2
        exit 1
    fi
    if [ -f "$target/payload-v2.raw" ]; then
        echo "ERROR: payload-v2 should not have been installed" >&2
        exit 1
    fi
 
    # Sub key test: Add a sub key the client does not have and rely on gpg
    # --auto-key-import to get it from the signature.
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --quick-add-key "$top_fpr" rsa2048 sign 1y
    # Make it so that only the sub key is available for signing to avoid having
    # to select it by fingerprint.
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --output "$WORKDIR/sigtest-subkey-secret.gpg" \
        --export-secret-subkeys
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --yes --delete-secret-keys "$top_fpr"
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --import "$WORKDIR/sigtest-subkey-secret.gpg"
    GNUPGHOME="$gpghome" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
    SYSTEMD_OPENPGP_KEYRING="$keyring" "$SYSUPDATE" --definitions="$defdir" update
    cmp "$sigdir/payload-v2.raw" "$target/payload-v2.raw"
}
 
backup_import_keyring() {
    local path="$1"
    local backup="$2"
 
    rm -f "$backup"
    if [ -e "$path" ] || [ -L "$path" ]; then
        mv "$path" "$backup"
    fi
}
 
restore_import_keyring() {
    local path="$1"
    local backup="$2"
 
    rm -f "$path"
    if [ -e "$backup" ] || [ -L "$backup" ]; then
        mv "$backup" "$path"
    fi
}
 
test_signature_keyring_overlay() (
    if ! command -v gpg >/dev/null; then
        echo "gpg not available, skipping signature overlay test"
        exit 0
    fi
 
    local gpg_version gpg_rest
    gpg_version="$(gpg --version | sed -n '1p' | awk '{print $NF}')"
    gpg_rest="${gpg_version#*.}"
    if [ "${gpg_version%%.*}" -lt 2 ] || { [ "${gpg_version%%.*}" -eq 2 ] && [ "${gpg_rest%%.*}" -lt 4 ]; }; then
        echo "gpg $gpg_version too old (need >= 2.4), skipping signature overlay test"
        exit 0
    fi
 
    local sigdir="$WORKDIR/sigtest-overlay-source"
    local defdir="$WORKDIR/sigtest-overlay-defs"
    local user_home="$WORKDIR/sigtest-overlay-userhome"
    local vendor_home="$WORKDIR/sigtest-overlay-vendorhome"
    local other_home="$WORKDIR/sigtest-overlay-otherhome"
    local target="$WORKDIR/sigtest-overlay-target"
    local user_keyring="$WORKDIR/sigtest-overlay-user.gpg"
    local vendor_keyring="$WORKDIR/sigtest-overlay-vendor.gpg"
    local other_keyring="$WORKDIR/sigtest-overlay-other.gpg"
    local etc_pgp_backup="$WORKDIR/import-pubring.etc.pgp.bak"
    local etc_gpg_backup="$WORKDIR/import-pubring.etc.gpg.bak"
    local usr_pgp_backup="$WORKDIR/import-pubring.usr.pgp.bak"
    # Called via trap on EXIT; shellcheck cannot see the indirect invocation.
    # shellcheck disable=SC2329,SC2317
    cleanup_overlay_keyrings() {
        set +e
        gpgconf --homedir "$user_home" --kill all 2>/dev/null || :
        gpgconf --homedir "$vendor_home" --kill all 2>/dev/null || :
        gpgconf --homedir "$other_home" --kill all 2>/dev/null || :
        restore_import_keyring /etc/systemd/import-pubring.pgp "$etc_pgp_backup"
        restore_import_keyring /etc/systemd/import-pubring.gpg "$etc_gpg_backup"
        restore_import_keyring /usr/lib/systemd/import-pubring.pgp "$usr_pgp_backup"
    }
 
    mkdir -p "$sigdir" "$defdir" "$user_home" "$vendor_home" "$other_home" "$target" /etc/systemd /usr/lib/systemd
    chmod 700 "$user_home" "$vendor_home" "$other_home"
 
    trap cleanup_overlay_keyrings EXIT
 
    backup_import_keyring /etc/systemd/import-pubring.pgp "$etc_pgp_backup"
    backup_import_keyring /etc/systemd/import-pubring.gpg "$etc_gpg_backup"
    backup_import_keyring /usr/lib/systemd/import-pubring.pgp "$usr_pgp_backup"
 
    GNUPGHOME="$user_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --quick-gen-key 'Overlay User <overlay-user@example.com>' rsa2048 cert,sign never
    GNUPGHOME="$user_home" gpg --export --output "$user_keyring"
 
    GNUPGHOME="$vendor_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --quick-gen-key 'Overlay Vendor <overlay-vendor@example.com>' rsa2048 cert,sign never
    GNUPGHOME="$vendor_home" gpg --export --output "$vendor_keyring"
 
    GNUPGHOME="$other_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --quick-gen-key 'Overlay Other <overlay-other@example.com>' rsa2048 cert,sign never
    GNUPGHOME="$other_home" gpg --export --output "$other_keyring"
 
    install -Dm0644 "$user_keyring" /etc/systemd/import-pubring.gpg
    install -Dm0644 "$vendor_keyring" /usr/lib/systemd/import-pubring.pgp
    rm -f /etc/systemd/import-pubring.pgp
 
    dd if=/dev/urandom of="$sigdir/payload-v1.raw" bs=1024 count=8 status=none
    (cd "$sigdir" && sha256sum payload-v1.raw > SHA256SUMS)
    GNUPGHOME="$vendor_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
 
    cat >"$defdir/01-sigtest.transfer" <<EOF
[Source]
Type=url-file
Path=file://$sigdir
MatchPattern=payload-@v.raw
 
[Target]
Type=regular-file
Path=$target
MatchPattern=payload-@v.raw
InstancesMax=3
EOF
 
    "$SYSUPDATE" --definitions="$defdir" check-new
    "$SYSUPDATE" --definitions="$defdir" update
    cmp "$sigdir/payload-v1.raw" "$target/payload-v1.raw"
 
    dd if=/dev/urandom of="$sigdir/payload-v2.raw" bs=1024 count=8 status=none
    (cd "$sigdir" && sha256sum payload-v1.raw payload-v2.raw > SHA256SUMS)
    GNUPGHOME="$other_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
    if "$SYSUPDATE" --definitions="$defdir" update; then
        echo "ERROR: accepted an update signed by a key not in the default keyrings" >&2
        exit 1
    fi
    if [ -f "$target/payload-v2.raw" ]; then
        echo "ERROR: payload-v2 should not have been installed" >&2
        exit 1
    fi
 
    dd if=/dev/urandom of="$sigdir/payload-v3.raw" bs=1024 count=8 status=none
    (cd "$sigdir" && sha256sum payload-v1.raw payload-v2.raw payload-v3.raw > SHA256SUMS)
    GNUPGHOME="$vendor_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
    if SYSTEMD_OPENPGP_KEYRING="$other_keyring" "$SYSUPDATE" --definitions="$defdir" update; then
        echo "ERROR: accepted an update with an override keyring that lacks the signing key" >&2
        exit 1
    fi
    if [ -f "$target/payload-v3.raw" ]; then
        echo "ERROR: payload-v3 should not have been installed" >&2
        exit 1
    fi
 
    install -Dm0644 "$user_keyring" /etc/systemd/import-pubring.pgp
    rm -f /etc/systemd/import-pubring.gpg
    dd if=/dev/urandom of="$sigdir/payload-v4.raw" bs=1024 count=8 status=none
    (cd "$sigdir" && sha256sum payload-v1.raw payload-v2.raw payload-v3.raw payload-v4.raw > SHA256SUMS)
    GNUPGHOME="$user_home" gpg --batch --pinentry-mode loopback --passphrase '' \
        --detach-sign --include-key-block --yes \
        --output "$sigdir/SHA256SUMS.gpg" "$sigdir/SHA256SUMS"
    "$SYSUPDATE" --definitions="$defdir" update
    cmp "$sigdir/payload-v4.raw" "$target/payload-v4.raw"
)
 
test_signature_verification
test_zero_fill
test_signature_keyring_overlay
 
# Test '**/' as prefix in MatchPattern= for subpaths in SHA256SUMS
rm -rf "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/sub"
mkdir -p "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/sub"
 
printf '1234567' >"$WORKDIR/source/sub/blob-v10.bin"
printf 'abcdefg' >"$WORKDIR/source/sub/blob-v11.bin"
(cd "$WORKDIR/source" && rm -f BEST-BEFORE-* && sha256sum sub/blob-*.bin >SHA256SUMS)
 
# A regular-file source where '**/' descends into sub/ to match against the basename
cat >"$CONFIGDIR/01-basename-dir.transfer" <<EOF
[Source]
Type=regular-file
Path=$WORKDIR/source
MatchPattern=**/blob-@v.bin
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=blob-@v.bin
InstancesMax=1
EOF
"$SYSUPDATE" --verify=no update
cmp "$WORKDIR/source/sub/blob-v11.bin" "$WORKDIR/blobs/blob-v11.bin"
[[ "$("$SYSUPDATE" --verify=no list v11 | grep -c "Version: v11")" == "1" ]]
rm "$CONFIGDIR/01-basename-dir.transfer"
 
# A url-file source pulled via file:// using SHA256SUMS with "sub/blob-v1x.bin" entries
rm -rf "$WORKDIR/blobs"
mkdir -p "$WORKDIR/blobs"
cat >"$CONFIGDIR/01-basename-url.transfer" <<EOF
[Source]
Type=url-file
Path=file://$WORKDIR/source
MatchPattern=**/blob-@v.bin
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=blob-@v.bin
InstancesMax=1
EOF
"$SYSUPDATE" --verify=no update
cmp "$WORKDIR/source/sub/blob-v11.bin" "$WORKDIR/blobs/blob-v11.bin"
rm "$CONFIGDIR/01-basename-url.transfer"
 
# A pattern that spells out the subdir literally should also work for url sources
# for parity with regular-file sources
rm -rf "$WORKDIR/blobs"
mkdir -p "$WORKDIR/blobs"
cat >"$CONFIGDIR/01-explicit-url.transfer" <<EOF
[Source]
Type=url-file
Path=file://$WORKDIR/source
MatchPattern=sub/blob-@v.bin
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=blob-@v.bin
InstancesMax=1
EOF
"$SYSUPDATE" --verify=no update
cmp "$WORKDIR/source/sub/blob-v11.bin" "$WORKDIR/blobs/blob-v11.bin"
rm "$CONFIGDIR/01-explicit-url.transfer"
 
# Rejection test for a manifest entry containing ".."
rm -rf "$WORKDIR/blobs"
mkdir -p "$WORKDIR/blobs"
mkdir -p "$WORKDIR/source/sub/nested"
cp "$WORKDIR/source/SHA256SUMS" "$WORKDIR/source/SHA256SUMS.bak"
sed -i 's,sub/,sub/nested/../,g' "$WORKDIR/source/SHA256SUMS"
cat >"$CONFIGDIR/01-basename-url.transfer" <<EOF
[Source]
Type=url-file
Path=file://$WORKDIR/source
MatchPattern=**/blob-@v.bin
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=blob-@v.bin
EOF
if "$SYSUPDATE" --verify=no update |& tee "$WORKDIR/traversal.log"; then
    echo "ERROR: accepted a manifest entry with a '..' path traversal" >&2
    exit 1
fi
grep "Invalid filename" "$WORKDIR/traversal.log" >/dev/null
mv "$WORKDIR/source/SHA256SUMS.bak" "$WORKDIR/source/SHA256SUMS"
rm "$CONFIGDIR/01-basename-url.transfer"
 
# Rejection test for a manifest entry with a percent-encoded ".." which curl would
# decode when pulling via file://, escaping the source dir past path_is_normalized()
rm -rf "$WORKDIR/blobs"
mkdir -p "$WORKDIR/blobs"
cp "$WORKDIR/source/SHA256SUMS" "$WORKDIR/source/SHA256SUMS.bak"
sed -i 's,sub/,sub/nested/%2e%2e/,g' "$WORKDIR/source/SHA256SUMS"
cat >"$CONFIGDIR/01-basename-url.transfer" <<EOF
[Source]
Type=url-file
Path=file://$WORKDIR/source
MatchPattern=**/blob-@v.bin
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=blob-@v.bin
EOF
if "$SYSUPDATE" --verify=no update |& tee "$WORKDIR/percent.log"; then
    echo "ERROR: accepted a manifest entry with a percent-encoded '..' path traversal" >&2
    exit 1
fi
grep "Invalid filename" "$WORKDIR/percent.log" >/dev/null
mv "$WORKDIR/source/SHA256SUMS.bak" "$WORKDIR/source/SHA256SUMS"
rm "$CONFIGDIR/01-basename-url.transfer"
 
# A MatchPattern= with two subdirectory levels must descend beyond the first level
rm -rf "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/depth-v3"
mkdir -p "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/depth-v3/contents"
printf 'version-three' >"$WORKDIR/source/depth-v3/contents/image.raw"
cat >"$CONFIGDIR/01-depth.transfer" <<EOF
[Source]
Type=regular-file
Path=$WORKDIR/source
MatchPattern=depth-@v/contents/image.raw
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=depth-@v.raw
InstancesMax=1
EOF
"$SYSUPDATE" --verify=no update
cmp "$WORKDIR/source/depth-v3/contents/image.raw" "$WORKDIR/blobs/depth-v3.raw"
rm -rf "$CONFIGDIR/01-depth.transfer" "$WORKDIR/source/depth-v3"
 
# Pattern matching should not depend on the pattern order. A transfer can list one pattern per repository
# layout, e.g., when a source switches from one directory per version (bundle-v1/image.raw) to flat files
# (bundle-v2.raw) and both layouts coexist during a migration.
# Check that after a subdirectory pattern which descends we still can match a top-level file through a
# '**/' pattern.
# Also check that after a non-matching '**/' pattern we still can match a plain top-level file.
# Then check that after a subdirectory pattern we can still match a plain top-level file.
# The first and last work because the wildcard field also captures the '.raw' suffix, so the top-level file
# matches the directory component of the subdirectory pattern and triggers the descend-retry logic.
# The fourth transfer checks the vice versa migration where one directory per version is the new layout and
# the old versions are flat files without a suffix: the directory name fully matches the flat pattern but
# must still be descended into for the subdirectory pattern.
rm -rf "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/bundle-v1" "$WORKDIR/source/pack-v2"
mkdir -p "$CONFIGDIR" "$WORKDIR/blobs"/{glob-last,glob-first,subdir-first,yes-and-retry} \
         "$WORKDIR/source/bundle-v1" "$WORKDIR/source/pack-v2"
echo 'version-one' >"$WORKDIR/source/bundle-v1/image.raw"
echo 'version-two' >"$WORKDIR/source/bundle-v2.raw"
echo 'version-v1' >"$WORKDIR/source/pack-v1"
echo 'version-v2' >"$WORKDIR/source/pack-v2/image.raw"
cat >"$CONFIGDIR/01-glob-last.transfer" <<EOF
[Source]
Type=regular-file
Path=$WORKDIR/source
MatchPattern=bundle-@v/image.raw **/bundle-@v.raw
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs/glob-last
MatchPattern=bundle-@v.raw
InstancesMax=1
EOF
cat >"$CONFIGDIR/02-glob-first.transfer" <<EOF
[Source]
Type=regular-file
Path=$WORKDIR/source
MatchPattern=**/bundle-@v.img bundle-@v.raw
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs/glob-first
MatchPattern=bundle-@v.raw
InstancesMax=1
EOF
cat >"$CONFIGDIR/03-subdir-first.transfer" <<EOF
[Source]
Type=regular-file
Path=$WORKDIR/source
MatchPattern=bundle-@v/image.raw bundle-@v.raw
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs/subdir-first
MatchPattern=bundle-@v.raw
InstancesMax=1
EOF
cat >"$CONFIGDIR/04-yes-and-retry.transfer" <<EOF
[Source]
Type=regular-file
Path=$WORKDIR/source
MatchPattern=pack-@v/image.raw pack-@v
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs/yes-and-retry
MatchPattern=pack-@v
InstancesMax=1
EOF
"$SYSUPDATE" --verify=no update
for target in glob-last glob-first subdir-first; do
    cmp "$WORKDIR/source/bundle-v2.raw" "$WORKDIR/blobs/$target/bundle-v2.raw"
done
cmp "$WORKDIR/source/pack-v2/image.raw" "$WORKDIR/blobs/yes-and-retry/pack-v2"
rm -rf "$CONFIGDIR"/{01-glob-last,02-glob-first,03-subdir-first,04-yes-and-retry}.transfer \
       "$WORKDIR/source/bundle-v2.raw" "$WORKDIR/source/bundle-v1" \
       "$WORKDIR/source/pack-v1" "$WORKDIR/source/pack-v2"
 
# A manifest entry that both directly matches a flat pattern and prefix-matches a subdirectory pattern
# must be downloaded, here the flat file is the newest version (this exercises YES_AND_RETRY)
rm -rf "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/pack-v1"
mkdir -p "$CONFIGDIR" "$WORKDIR/blobs" "$WORKDIR/source/pack-v1"
echo 'version-v1' >"$WORKDIR/source/pack-v1/image.raw"
echo 'version-v2' >"$WORKDIR/source/pack-v2"
(cd "$WORKDIR/source" && sha256sum pack-v1/image.raw pack-v2 >SHA256SUMS)
cat >"$CONFIGDIR/01-manifest-yes-and-retry.transfer" <<EOF
[Source]
Type=url-file
Path=file://$WORKDIR/source
MatchPattern=pack-@v/image.raw pack-@v
 
[Target]
Type=regular-file
Path=$WORKDIR/blobs
MatchPattern=pack-@v
InstancesMax=1
EOF
"$SYSUPDATE" --verify=no update
cmp "$WORKDIR/source/pack-v2" "$WORKDIR/blobs/pack-v2"
rm -rf "$CONFIGDIR/01-manifest-yes-and-retry.transfer" \
       "$WORKDIR/source/pack-v1" "$WORKDIR/source/pack-v2"
 
# Test that listing components/targets when none are configured gives an empty list.
"$SYSUPDATE" components |& grep "No components defined." >/dev/null
[[ $(varlinkctl call "$VARLINK_SOCKET" io.systemd.SysUpdate.ListTargets | jq -r '.targets') == "[]" ]]
 
# ============================================================================
# Components & features: enable/disable verbs, the --component-{all,suggested}
# and --feature-{all,suggested} switches, plus the Suggest=/SuggestOn…=
# settings. Everything below operates on freshly minted throw-away components
# and features, and cleans up after itself.
#
# Layout used throughout:
#   * a component <c> is defined by a transfer directory /run/sysupdate.<c>.d/
#     (which is what makes it show up in the 'components' list) plus an optional
#     metadata file /run/sysupdate.<c>.component carrying Description=/Suggest=/…
#   * the enable-component/disable-component verbs write their Enabled= override
#     into a drop-in below /etc/sysupdate.<c>.component.d/
#   * the enable-feature/disable-feature verbs write their Enabled= override into
#     a drop-in below /etc/sysupdate.d/<f>.feature.d/ (default component) or
#     /etc/sysupdate.<c>.d/<f>.feature.d/ (named component)
# ============================================================================
CF="$WORKDIR/compfeat"
 
# The Suggest…MachineTag= tests below drive the machine tags via /etc/machine-info
# (which the condition logic reads directly); back up any pre-existing file so we
# can restore it afterwards, mirroring TEST-74-AUX-UTILS.machine-tags.sh.
MI_BAK="$WORKDIR/machine-info.orig"
rm -f "$MI_BAK"
[[ -e /etc/machine-info ]] && cp -a /etc/machine-info "$MI_BAK"
 
set_machine_tags() {
    if [[ -n "${1:-}" ]]; then
        echo "TAGS=$1" >/etc/machine-info
    else
        rm -f /etc/machine-info
    fi
}
 
restore_machine_info() {
    if [[ -e "$MI_BAK" ]]; then
        cp -a "$MI_BAK" /etc/machine-info
    else
        rm -f /etc/machine-info
    fi
}
 
compfeat_cleanup() {
    rm -rf /run/sysupdate.d \
           /run/sysupdate.compx.d /run/sysupdate.compx.component /run/sysupdate.compx.component.d \
           /run/sysupdate.compy.d /run/sysupdate.compy.component /run/sysupdate.compy.component.d \
           /run/sysupdate.compz.d /run/sysupdate.compz.component /run/sysupdate.compz.component.d \
           /etc/sysupdate.d \
           /etc/sysupdate.compx.d /etc/sysupdate.compx.component.d \
           /etc/sysupdate.compy.d /etc/sysupdate.compy.component.d \
           /etc/sysupdate.compz.d /etc/sysupdate.compz.component.d
    rm -rf "$CF"
}
 
compfeat_reset() {
    compfeat_cleanup
    mkdir -p "$CF/source" \
             "$CF/target-default" "$CF/target-compx" "$CF/target-compy" "$CF/target-compz"
}