#!/usr/bin/env bash # SPDX-License-Identifier: AGPL-3.0-or-later set -Eeuo pipefail repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd -P) bin=${1:-$repo_root/zupt} case "$bin" in /*) ;; *) bin="$(pwd -P)/${bin#./}" ;; esac fail() { printf 'FAIL: %s\n' "$*" >&2 exit 1 } test -x "$bin" || fail "$bin is not executable" command -v python3 >/dev/null 2>&1 || fail 'python3 is required' tmp=$(mktemp -d "${TMPDIR:-/tmp}/zupt-format-le.XXXXXX") trap 'rm -rf "$tmp"' EXIT printf 'little-endian format fixture\n' > "$tmp/input" printf 'format-test-password\n' > "$tmp/password" chmod 600 "$tmp/password" "$bin" compress --store --kdf pbkdf2 --pass-file "$tmp/password" \ "$tmp/format.zupt" "$tmp/input" >/dev/null 2>&1 || fail 'could not create PBKDF2 archive fixture' python3 - "$tmp/format.zupt" <<'PY' import pathlib import struct import sys path = pathlib.Path(sys.argv[1]) data = path.read_bytes() def reject(message): raise SystemExit(f"FAIL: {message}") def varint(offset): value = 0 shift = 0 start = offset while offset < len(data) and shift <= 63: byte = data[offset] offset += 1 value |= (byte & 0x7f) << shift if byte & 0x80 == 0: encoded = data[start:offset] canonical = bytearray() remaining = value while remaining >= 0x80: canonical.append((remaining & 0x7f) | 0x80) remaining >>= 7 canonical.append(remaining) if bytes(canonical) != encoded: reject("non-canonical varint in generated archive") return value, offset shift += 7 reject("unterminated varint") if len(data) < 64 + 32 + 32: reject("archive is too small") if data[:6] != b"ZUPT\x1a\x00" or data[6:8] != bytes((1, 6)): reject("header magic/version mismatch") flags = struct.unpack_from(" "$tmp/one-byte" "$bin" compress --store "$tmp/varint-base.zupt" "$tmp/one-byte" \ >/dev/null 2>&1 || fail 'could not create varint fixture' python3 - "$tmp/varint-base.zupt" "$tmp" <<'PY' import pathlib import struct import sys source = pathlib.Path(sys.argv[1]).read_bytes() out = pathlib.Path(sys.argv[2]) if len(source) < 128 or source[64:67] != b"\xbb\x01\x00": raise SystemExit("FAIL: unexpected varint fixture layout") footer = len(source) - 64 index_offset = struct.unpack_from("/dev/null 2>&1; then fail "non-canonical or overflowing varint was accepted: ${malformed##*/}" fi done printf 'non-canonical and overflowing uint64 varints: PASS\n'