Files

40 lines
2.0 KiB
Bash

#!/usr/bin/env bash
# KeePassXC interop check (docs/KDBX.md, "Tests"). Needs keepassxc-cli (2.7), cargo and python3; on the dev
# machine run it in WSL inside `nix shell nixpkgs#keepassxc nixpkgs#cargo nixpkgs#rustc nixpkgs#stdenv.cc
# nixpkgs#python3`. Works in a fresh temporary folder; the test password is not a secret.
#
# 1. KeePassXC imports sample.xml (every feature in the mapping table) → KDBX 3.1 with a recycle bin.
# 2. Custos writes an empty KDBX 4 file; KeePassXC merges (1) into it and saves it → KDBX 4 written by KeePassXC.
# 3. Custos reads (1) and (2), maps them to items and back, and writes KDBX 4 again.
# 4. KeePassXC opens what Custos wrote; compare.py checks the XML exports and attachment-export the bytes.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
export KDBX_PASSWORD=custos-test
K=${KEEPASSXC_CLI:-keepassxc-cli}
pw() { printf '%s\n' "$KDBX_PASSWORD"; }
cargo build -q --release --manifest-path "$repo/Cargo.toml" -p custos-kdbx --example kdbx_tool \
--target-dir "${CARGO_TARGET_DIR:-$work/target}"
tool="${CARGO_TARGET_DIR:-$work/target}/release/examples/kdbx_tool"
cd "$work"
{ pw; pw; } | $K import -q -p "$here/sample.xml" v3.kdbx
"$tool" new v4.kdbx fast
{ pw; pw; } | $K merge -q v4.kdbx v3.kdbx
for v in v3 v4; do
printf 'KDBX version of %s: ' "$v"; od -A n -t u2 -j 8 -N 4 "$v.kdbx" | awk '{print $2 "." $1}'
pw | $K export -q -f xml "$v.kdbx" > "$v.xml"
"$tool" roundtrip "$v.kdbx" "$v-custos.kdbx"
pw | $K export -q -f xml "$v-custos.kdbx" > "$v-custos.xml"
python3 "$here/compare.py" "$v.xml" "$v-custos.xml"
for db in "$v.kdbx" "$v-custos.kdbx"; do
pw | $K attachment-export -q --stdout "$db" Internet/GitHub codes.txt | sha256sum
done | uniq | wc -l | grep -qx 1 || { echo "$v: the attachment changed"; exit 1; }
pw | $K show -q --totp "$v-custos.kdbx" Internet/Mail/Mail | grep -qE '^[0-9]{8}$' \
|| { echo "$v: no 8-digit TOTP after the round trip"; exit 1; }
done
echo "interop: ok"