arch-system/tests/backup/mock-command.sh

143 lines
6.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Doublures strictes : aucune mutation ou lecture hors des fixtures /mnt.
set -euo pipefail
name="${0##*/}"
printf '%s' "$name" >> /mnt/calls
printf ' %q' "$@" >> /mnt/calls
printf '\n' >> /mnt/calls
if [[ "$name" == git ]]; then
[[ "$1" == -C && "$2" == /mnt/repo && "$3" == rev-parse && "$4" == HEAD && $# == 4 ]] ||
{ [[ "${SIM_LEGACY:-0}" == 1 && "$1" == -C && "$2" == /mnt/repo && "$3" == status ]] || exit 90; }
[[ "$3" == status ]] || printf '074d5dd0b2a1b362c8c0cd6413ab77f868461e25\n'
exit 0
fi
if [[ "$name" == sha256sum ]]; then
[[ $# == 0 ]] || exit 90
case "${SIM_HASH_MODE:-}" in
fail) exit 47 ;;
malformed) printf 'malformed -\n'; exit 0 ;;
'') exec /usr/bin/sha256sum ;;
*) exit 90 ;;
esac
fi
if [[ "$name" == mv ]]; then
if [[ "${1:-}" == -T ]]; then
[[ "$2" == -n && "$3" == -- && "$4" == /mnt/*/.incomplete.* && "$5" == /mnt/*/export-* ]] || exit 90
if [[ "${SIM_FINAL_COLLIDE:-0}" == 1 ]]; then
/usr/bin/mkdir -m 0700 -- "$5"
printf 'existing candidate\n' > "$5/keep"
fi
if [[ -n "${SIM_FINAL_SIGNAL_PHASE:-}" ]]; then
signal="${SIM_FINAL_SIGNAL:-TERM}"
[[ "$signal" == INT || "$signal" == TERM ]] || exit 90
[[ "$SIM_FINAL_SIGNAL_PHASE" == before || "$SIM_FINAL_SIGNAL_PHASE" == after ]] || exit 90
if [[ "$SIM_FINAL_SIGNAL_PHASE" == after ]]; then /usr/bin/mv "$@" || exit 91; fi
kill "-$signal" "$PPID"
[[ "$signal" == INT ]] && exit 130 || exit 143
fi
[[ "${SIM_FINAL_FAIL:-0}" != 1 ]] || exit 55
fi
exec /usr/bin/mv "$@"
fi
if [[ "$name" == sudo ]]; then
if [[ "${SIM_LEGACY:-0}" == 1 ]]; then
case "${1:-}" in
test)
[[ "$2" == -f && ( "$3" == /etc/* || "$3" == /boot/limine.conf || "$3" == /usr/local/sbin/* ) ]] || exit 90
[[ -f "$3" ]] ; exit ;;
install)
[[ "$2" == -D && "$3" == -o && "$5" == -g && "$7" == -m ]] || exit 90
[[ "$9" == /etc/* || "$9" == /usr/local/sbin/* ]] || exit 90
[[ "${10}" == /mnt/repo/* ]] || exit 90
/usr/bin/install -D -m "$8" "$9" "${10}"; exit ;;
awk)
[[ "${@: -1}" == /boot/limine.conf ]] || exit 90
exec /usr/bin/awk "${@:2}" ;;
esac
fi
[[ "${1:-}" == -n ]] || exit 90
shift
case "${1:-}" in
/usr/bin/bash)
[[ $# == 5 && "$2" == -c && "$4" == -- ]] || exit 90
digest="$(printf '%s' "$3" | /usr/bin/sha256sum)"
[[ "${digest%% *}" == 9fc82ae6938b308c551562c1106372ba19194e90651a107cf819d3744ede8e4d ]] || exit 90
target="$5"
[[ "$target" == /etc/* || "$target" == /boot/limine.conf || "$target" == /usr/local/sbin/* ]] || exit 90
if [[ "${SIM_INSPECT_TARGET:-}" == "$target" ]]; then
case "${SIM_INSPECT_MODE:-}" in
fail) exit 1 ;;
parent_fail) exit 3 ;;
empty) exit 0 ;;
unknown) printf 'UNKNOWN\n'; exit 0 ;;
valid_fail) printf 'EXISTING\n'; exit 1 ;;
esac
fi
parent="${target%/*}"
while [[ "$parent" != / ]]; do
if [[ -L "$parent" ]]; then printf 'LINK\n'; exit 0; fi
if [[ ! -e "$parent" ]]; then printf 'ABSENT\n'; exit 0; fi
if [[ ! -d "$parent" ]]; then printf 'INVALID\n'; exit 0; fi
parent="${parent%/*}"; [[ -n "$parent" ]] || parent=/
done
if [[ -L "$target" ]]; then printf 'LINK\n'
elif [[ -f "$target" ]]; then printf 'EXISTING\n'
elif [[ -e "$target" ]]; then printf 'INVALID\n'
else printf 'ABSENT\n'; fi
exit 0 ;;
/usr/bin/cat)
[[ $# == 3 && "$2" == -- ]] || exit 90
path="$3"
[[ "$path" == /etc/fstab || "$path" == /etc/pacman.conf ||
"$path" == /etc/makepkg.conf || "$path" == /etc/mkinitcpio.conf ||
"$path" == /etc/nftables.conf || "$path" == /etc/limine-snapper-sync.conf ||
"$path" == /etc/tlp.d/10-archasp.conf ||
"$path" == /etc/systemd/zram-generator.conf ||
"$path" == /etc/snapper/configs/root ||
"$path" == /usr/local/sbin/batlimit-set ||
"$path" == /usr/local/sbin/vmware-stack ||
"$path" == /boot/limine.conf ]] || exit 90
if [[ "${SIM_FAIL_KEY:-}" == "read:$path" ]]; then printf 'partial\n'; exit 47; fi
exec /usr/bin/cat -- "$path" ;;
/usr/bin/btrfs)
[[ "$2" == subvolume && "$3" == list && "$4" == / ||
"$2" == filesystem && "$3" == usage && "$4" == / ||
"$2" == scrub && "$3" == status && "$4" == / ]] || exit 90
key="btrfs_${2}_${3}"
if [[ "${SIM_SIGNAL_KEY:-}" == "$key" ]]; then kill "-${SIM_SIGNAL:-TERM}" "$PPID"; exit 143; fi
if [[ "${SIM_FAIL_KEY:-}" == "$key" ]]; then printf 'partial\n'; exit 47; fi
printf 'fixture %s\n' "$key"; exit 0 ;;
/usr/bin/nvme)
[[ "$2" == smart-log && "$3" == /dev/nvme0 ]] || exit 90
printf 'fixture nvme0\n'; exit 0 ;;
esac
exit 90
fi
case "$name" in
pacman)
[[ $# == 1 ]] || exit 90
case "$1" in
-Qqen) key=pacman_native; value=alpha ;;
-Qqem) key=pacman_foreign; value=foreign ;;
-Qdtq) key=pacman_orphans; value=orphan ;;
*) exit 90 ;;
esac ;;
systemctl)
if [[ "$*" == *--user* ]]; then key=user_state
else key=system_state; fi
value='fixture.service enabled' ;;
lsblk)
[[ "$1" == -e7 && "$2" == -o ]] || exit 90
key=lsblk; value='disk fixture' ;;
findmnt) [[ $# == 0 ]] || exit 90; key=findmnt; value='fixture mount' ;;
free) [[ "$1" == -h ]] || exit 90; key=memory; value='fixture memory' ;;
swapon) [[ "$1" == --show ]] || exit 90; key=swap; value='fixture swap' ;;
*) exit 90 ;;
esac
if [[ "$key" == pacman_orphans && "${SIM_ORPHANS_EMPTY_ERROR:-0}" == 1 ]]; then exit 1; fi
if [[ "${SIM_FAIL_KEY:-}" == "$key" ]]; then printf 'partial\n'; exit 47; fi
if [[ "${SIM_EMPTY_KEY:-}" == "$key" ]]; then exit 0; fi
if [[ "${SIM_SIGNAL_KEY:-}" == "$key" ]]; then kill "-${SIM_SIGNAL:-TERM}" "$PPID"; exit 143; fi
printf '%s\n' "$value"