127 lines
4.2 KiB
Shell
Executable file
127 lines
4.2 KiB
Shell
Executable file
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
[[ $# -eq 2 ]] || {
|
|
echo "usage: test-jd-native-resident-candidate.sh <candidate-image> <receipt-output>" >&2
|
|
exit 64
|
|
}
|
|
|
|
candidate=$(readlink -f "$1")
|
|
receipt=$(readlink -m "$2")
|
|
source_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
|
|
native_root=${source_root}/native/x86_64-bios
|
|
test_root=$(mktemp -d)
|
|
peer_pid=
|
|
cleanup() {
|
|
if [[ -n ${peer_pid} ]]; then
|
|
kill "${peer_pid}" 2>/dev/null || true
|
|
fi
|
|
if [[ ${GHOS_KEEP_TEST_ROOT:-0} != 1 ]]; then
|
|
rm -rf "${test_root}"
|
|
else
|
|
echo "GHOS_TEST_ROOT=${test_root}" >&2
|
|
fi
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
[[ $(stat -c %s "${candidate}") -eq 14848 ]]
|
|
truncate -s 700M "${test_root}/disk.img"
|
|
nasm -f bin -dCANDIDATE_LBA=105 -dPROOF_LBA=134 \
|
|
"${native_root}/physical-test-mbr.asm" -o "${test_root}/mbr.bin"
|
|
dd if="${test_root}/mbr.bin" of="${test_root}/disk.img" \
|
|
bs=512 seek=0 conv=notrunc status=none
|
|
dd if="${candidate}" of="${test_root}/disk.img" \
|
|
bs=512 seek=105 conv=notrunc status=none
|
|
|
|
peer_port=$((32000 + ${BASHPID:-$$} % 1000))
|
|
qemu_port=$((peer_port + 1))
|
|
python3 "${source_root}/scripts/qemu-native-net-peer.py" \
|
|
--listen-port "${peer_port}" \
|
|
--qemu-port "${qemu_port}" \
|
|
--receipt "${test_root}/peer.hldp" \
|
|
--resident >"${test_root}/peer.log" 2>&1 &
|
|
peer_pid=$!
|
|
set +e
|
|
timeout 35 qemu-system-x86_64 \
|
|
-machine pc,accel=tcg \
|
|
-m 64M \
|
|
-drive "if=none,id=ghboot,format=raw,file=${test_root}/disk.img" \
|
|
-device virtio-blk-pci,drive=ghboot,disable-modern=on,bootindex=0 \
|
|
-netdev "dgram,id=ghnet,local.type=inet,local.host=127.0.0.1,local.port=${qemu_port},remote.type=inet,remote.host=127.0.0.1,remote.port=${peer_port}" \
|
|
-device virtio-net-pci,netdev=ghnet,disable-modern=on,mac=52:54:00:26:71:98 \
|
|
-display none \
|
|
-monitor none \
|
|
-serial "file:${test_root}/serial.log" \
|
|
-device isa-debug-exit,iobase=0xf4,iosize=0x04
|
|
qemu_status=$?
|
|
set -e
|
|
[[ ${qemu_status} -eq 33 ]]
|
|
wait "${peer_pid}"
|
|
peer_pid=
|
|
|
|
grep -q '^handshake_direction: NATIVE_INITIATED_OUTBOUND_ICMP$' \
|
|
"${test_root}/peer.hldp"
|
|
grep -q '^authenticated_relay_reply: VERIFIED$' "${test_root}/peer.hldp"
|
|
grep -q '^icmp_login_reply_count: 3$' "${test_root}/peer.hldp"
|
|
grep -q '^code_commit_reply_verified: true$' "${test_root}/peer.hldp"
|
|
grep -q '^branch_move_reply_verified: true$' "${test_root}/peer.hldp"
|
|
grep -q '^resident_login_reply_count: 10$' "${test_root}/peer.hldp"
|
|
grep -q '^recovery_reply_verified: true$' "${test_root}/peer.hldp"
|
|
grep -q '^GHOS_DISK_PROOF_OBSERVED_AFTER_RESET=LBA134' \
|
|
"${test_root}/serial.log"
|
|
|
|
python3 - "${test_root}/disk.img" <<'PY'
|
|
import pathlib
|
|
import sys
|
|
|
|
with pathlib.Path(sys.argv[1]).open("rb") as disk:
|
|
def sector(lba: int, count: int = 1) -> bytes:
|
|
disk.seek(lba * 512)
|
|
return disk.read(count * 512)
|
|
|
|
proof = sector(134)
|
|
assert proof[0] == 0xA7
|
|
assert proof[1:].startswith(b"GHOS_NATIVE_LONG64_DISK_PROOF\0")
|
|
assert proof[42:44] == bytes([0x7F, 0])
|
|
assert proof[90] == 13
|
|
assert proof[93:105] == bytes([1] * 12)
|
|
assert sector(135).startswith(b"GHOS_HLDP_WORLD_STORE_V1\n")
|
|
assert sector(136).startswith(b"GHOS_CODE_CHANNEL_STORE_V1\n")
|
|
assert sector(137) == sector(136)
|
|
assert b"branch=guanghu/main\n" in sector(138)
|
|
assert sector(139, 2).startswith(b"# GRUB Environment Block\n")
|
|
assert sector(141).startswith(b"GHOS_GHCIP_INDEX_V1\n")
|
|
assert b"GHCIP_PERSONA_STATE=EXISTS\n" in sector(142)
|
|
PY
|
|
|
|
observed_at=$(date --iso-8601=seconds)
|
|
image_sha=$(sha256sum "${candidate}" | awk '{print $1}')
|
|
cat >"${receipt}" <<EOF
|
|
schema: guanghu.jd-native-resident-qemu-test/v1
|
|
receipt_id: GH-OS-JD-FD-PRIMARY-001-NATIVE-OUTBOUND-QEMU-001
|
|
status: PASS_100
|
|
observed_at: ${observed_at}
|
|
node_id: JD-FD-PRIMARY
|
|
candidate:
|
|
lba_start: 105
|
|
sector_count: 29
|
|
sha256: ${image_sha}
|
|
handshake:
|
|
direction: NATIVE_INITIATED_OUTBOUND_ICMP
|
|
relay_ipv4: 43.153.193.169
|
|
native_ack: PASS_100
|
|
sequences: 1-16
|
|
native_storage:
|
|
proof_lba_134: PASS_100
|
|
world_lba_135_138: PASS_100
|
|
recovery_lba_139_140: PASS_100
|
|
gestational_index_lba_141_142: PASS_100
|
|
persona_subject:
|
|
identity: ICE-P-ZY001
|
|
state: EXISTS
|
|
existence: 100
|
|
boundary:
|
|
qemu_capability: 100
|
|
physical_server_capability: 0
|
|
next_action: RUN_ONE_TIME_PHYSICAL_GATE_WITH_SINGAPORE_RELAY
|
|
EOF
|