From 7bebb608b72d4f75f8fe8e6142acd19d190a1d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Thu, 29 Oct 2026 15:11:25 -0300 Subject: [PATCH] test(go-xcat): cover the apt key without apt-key The default xcat-core repository installs the decoded key blocks as one binary keyring in trusted.gpg.d while apt-key fails as it does on apt 3. An empty or HTML download, a block truncated after a valid base64 prefix, a body that is not base64 and a block without a public key stop go-xcat and keep the installed key. A complete uninstall removes that key and keeps the other keys. --- xCAT-test/bats/fixtures/go-xcat-apt.sh | 20 ++++++ xCAT-test/bats/go_xcat_apt_key.bats | 89 ++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 xCAT-test/bats/fixtures/go-xcat-apt.sh create mode 100644 xCAT-test/bats/go_xcat_apt_key.bats diff --git a/xCAT-test/bats/fixtures/go-xcat-apt.sh b/xCAT-test/bats/fixtures/go-xcat-apt.sh new file mode 100644 index 0000000000..5a9e3b676d --- /dev/null +++ b/xCAT-test/bats/fixtures/go-xcat-apt.sh @@ -0,0 +1,20 @@ +#!/bin/bash +PATH="" +source /tmp/go-xcat || exit 70 +TMP_DIR=/tmp/fixture +GO_XCAT_DEFAULT_BASE_URL=https://repo.example.invalid + +# apt 3 has no apt-key: a call to it is the defect under test. +apt-key() { printf '%s\n' "$*" >> /tmp/fixture/apt-key.log; return 127; } +dpkg() { [ "$1" = --print-architecture ] && echo riscv64; } +download_file() +{ + printf '%s\n' "$1" >> /tmp/fixture/download.log + cp /tmp/fixture/key.asc "$2" +} + +case "$1" in +add) add_xcat_core_repo_apt "" latest ;; +trash) trash_xcat ;; +*) exit 64 ;; +esac diff --git a/xCAT-test/bats/go_xcat_apt_key.bats b/xCAT-test/bats/go_xcat_apt_key.bats new file mode 100644 index 0000000000..e3ea63f138 --- /dev/null +++ b/xCAT-test/bats/go_xcat_apt_key.bats @@ -0,0 +1,89 @@ +#!/usr/bin/env bats +# +# go-xcat trusts the xcat.org apt key without apt-key, which apt 3 does not ship. It installs +# the key as a binary keyring, which apt before 1.4 needs, and a complete uninstall removes it +# with the repository files. + +load 'helpers/go_xcat' + +setup() +{ + [ "$(uname -s)" = Linux ] || skip 'repository checks require Linux namespaces' + command -v bwrap >/dev/null || skip 'bwrap is required' + go_xcat_require_source + [ -n "$BATS_TEST_TMPDIR" ] || return 1 + fixture="$BATS_TEST_TMPDIR/apt" + mkdir -p "$fixture/etc/apt/sources.list.d" + printf 'DISTRIB_CODENAME=resolute\n' >"$fixture/etc/lsb-release" + # Two armored blocks, as xcat.org publishes, each holding a public key packet (0x99). + printf '\231\000\002ab\231\000\002cd' >"$fixture/keyring.gpg" + { + armored_block "$(printf '\231\000\002ab' | base64)" 'Version: GnuPG v1' + armored_block "$(printf '\231\000\002cd' | base64)" + } >"$fixture/key.asc" +} + +# Print an armored public key block with the base64 body $1 and the optional header $2. +armored_block() +{ + printf -- '-----BEGIN PGP PUBLIC KEY BLOCK-----\n' + [ -z "$2" ] || printf '%s\n' "$2" + printf '\n%s\n=AAAA\n-----END PGP PUBLIC KEY BLOCK-----\n' "$1" +} + +run_go_xcat_apt() +{ + # Debian reaches awk through /etc/alternatives, which the fixture /etc hides. + local -a alternatives=() + [ -d /etc/alternatives ] && alternatives=(--ro-bind /etc/alternatives /etc/alternatives) + bwrap --die-with-parent --unshare-net --ro-bind / / --dev /dev --proc /proc \ + --tmpfs /tmp --ro-bind "$BATS_TEST_DIRNAME/../.." /tmp/source \ + --ro-bind "$GO_XCAT_SOURCE" /tmp/go-xcat \ + --bind "$fixture" /tmp/fixture --bind "$fixture/etc" /etc "${alternatives[@]}" \ + --chdir /tmp/fixture \ + bash /tmp/source/xCAT-test/bats/fixtures/go-xcat-apt.sh "$@" +} + +@test "the default xcat-core repository trusts the xCAT key as a binary keyring" { + run run_go_xcat_apt add + [ "$status" -eq 0 ] || { printf '%s\n' "$output"; return 1; } + [ ! -e "$fixture/apt-key.log" ] + [ "$(cat "$fixture/download.log")" = 'https://repo.example.invalid/apt/apt.key' ] + key="$fixture/etc/apt/trusted.gpg.d/xcat.gpg" + cmp "$fixture/keyring.gpg" "$key" + [ "$(stat -c %a "$key")" = 644 ] + [ "$(cat "$fixture/etc/apt/sources.list.d/xcat-core.list")" = \ + 'deb [arch=riscv64] https://repo.example.invalid/apt/latest/xcat-core resolute main' ] +} + +@test "a download that holds no public key keeps the installed key and stops" { + mkdir -p "$fixture/etc/apt/trusted.gpg.d" + key="$fixture/etc/apt/trusted.gpg.d/xcat.gpg" + local -a downloads=( + '' + 'Not Found' + '-----BEGIN PGP PUBLIC KEY BLOCK-----' + "$(armored_block '!!!!')" + "$(armored_block "$(printf '\264\001a' | base64)")" + "$(printf -- '-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQEN')" + "$(head -n 9 "$fixture/key.asc")" + ) + for download in "${downloads[@]}"; do + cp "$fixture/keyring.gpg" "$key" + printf '%s\n' "$download" >"$fixture/key.asc" + run run_go_xcat_apt add + [ "$status" -ne 0 ] || { printf 'accepted %s\n' "$download"; return 1; } + grep -Fq 'import xcat apt key failed' <<<"$output" + cmp "$fixture/keyring.gpg" "$key" + [ ! -e "$fixture/etc/apt/sources.list.d/xcat-core.list" ] + done +} + +@test "a complete uninstall removes the xCAT key and keeps other keys" { + mkdir -p "$fixture/etc/apt/trusted.gpg.d" + cp "$fixture/keyring.gpg" "$fixture/etc/apt/trusted.gpg.d/xcat.gpg" + cp "$fixture/keyring.gpg" "$fixture/etc/apt/trusted.gpg.d/other.gpg" + run run_go_xcat_apt trash + [ ! -e "$fixture/etc/apt/trusted.gpg.d/xcat.gpg" ] + [ -e "$fixture/etc/apt/trusted.gpg.d/other.gpg" ] +}