vinix-ports / gnupg / check.py
31 lines · 26 sloc · 1.46 KB · 59386468057b3b16ba4ae6abcedea7667e736c64
Raw
1"""Sign a fixture with a throwaway host key, then verify it with target gpgv."""
2
3from pathlib import Path
4import subprocess
5import tempfile
6
7
8def run(*args, **kwargs):
9 return subprocess.run(args, check=True, capture_output=True, timeout=30, **kwargs)
10
11
12# Keep the agent socket below the kernel's Unix-domain path limit even in
13# a deeply nested build tree. Only the exported fixture key enters the tree.
14with tempfile.TemporaryDirectory(prefix="gpg-", dir="/tmp") as home:
15 identity = "Vinix fixture <fixture@example.invalid>"
16 host = ["gpg", "--homedir", home, "--batch"]
17 try:
18 run(*host, "--pinentry-mode", "loopback", "--passphrase", "", "--quick-generate-key",
19 identity, "ed25519", "sign", "0")
20 Path("fixture.gpg").write_bytes(run(*host, "--export", identity).stdout)
21 package = Path("package-list")
22 package.write_text("Vinix packages\n")
23 run(*host, "--detach-sign", "--output", "package-list.sig", str(package))
24 verify = ["vinix-run", "g10/gpgv", "--keyring", str(Path("fixture.gpg").resolve()),
25 "--status-fd", "1", "package-list.sig", str(package)]
26 assert b"[GNUPG:] VALIDSIG " in run(*verify).stdout
27 package.write_text("changed\n")
28 bad = subprocess.run(verify, capture_output=True, timeout=15)
29 assert bad.returncode != 0 and b"[GNUPG:] BADSIG " in bad.stdout, bad.stderr
30 finally:
31 subprocess.run(["gpgconf", "--homedir", home, "--kill", "all"], timeout=10)
32