test.py raw

   1  #!/usr/bin/env python3
   2  
   3  import json
   4  import sys
   5  import subprocess
   6  from pathlib import Path
   7  
   8  
   9  def main():
  10      """Tests ordered roughly from faster to slower."""
  11      expect_code(run_verify("", "pub", '0.32'), 4, "Nonexistent version should fail")
  12      expect_code(run_verify("", "pub", '0.32.awefa.12f9h'), 11, "Malformed version should fail")
  13      expect_code(run_verify('--min-good-sigs 20', "pub", "22.0"), 9, "--min-good-sigs 20 should fail")
  14  
  15      print("- testing verification (22.0-x86_64-linux-gnu.tar.gz)", flush=True)
  16      _220_x86_64_linux_gnu = run_verify("--json", "pub", "22.0-x86_64-linux-gnu.tar.gz")
  17      try:
  18          result = json.loads(_220_x86_64_linux_gnu.stdout.decode())
  19      except Exception:
  20          print("failed on 22.0-x86_64-linux-gnu.tar.gz --json:")
  21          print_process_failure(_220_x86_64_linux_gnu)
  22          raise
  23  
  24      expect_code(_220_x86_64_linux_gnu, 0, "22.0-x86_64-linux-gnu.tar.gz should succeed")
  25      v = result['verified_binaries']
  26      assert result['good_trusted_sigs']
  27      assert len(v) == 1
  28      assert v['bitcoin-22.0-x86_64-linux-gnu.tar.gz'] == '59ebd25dd82a51638b7a6bb914586201e67db67b919b2a1ff08925a7936d1b16'
  29  
  30      print("- testing verification (22.0)", flush=True)
  31      _220 = run_verify("--json", "pub", "22.0")
  32      try:
  33          result = json.loads(_220.stdout.decode())
  34      except Exception:
  35          print("failed on 22.0 --json:")
  36          print_process_failure(_220)
  37          raise
  38  
  39      expect_code(_220, 0, "22.0 should succeed")
  40      v = result['verified_binaries']
  41      assert result['good_trusted_sigs']
  42      assert v['bitcoin-22.0-aarch64-linux-gnu.tar.gz'] == 'ac718fed08570a81b3587587872ad85a25173afa5f9fbbd0c03ba4d1714cfa3e'
  43      assert v['bitcoin-22.0-osx64.tar.gz'] == '2744d199c3343b2d94faffdfb2c94d75a630ba27301a70e47b0ad30a7e0155e9'
  44      assert v['bitcoin-22.0-x86_64-linux-gnu.tar.gz'] == '59ebd25dd82a51638b7a6bb914586201e67db67b919b2a1ff08925a7936d1b16'
  45  
  46  
  47  def run_verify(global_args: str, command: str, command_args: str) -> subprocess.CompletedProcess:
  48      maybe_here = Path.cwd() / 'verify.py'
  49      path = maybe_here if maybe_here.exists() else Path.cwd() / 'contrib' / 'verify-binaries' / 'verify.py'
  50  
  51      if command == "pub":
  52          command += " --cleanup"
  53  
  54      return subprocess.run(
  55          f"{path} {global_args} {command} {command_args}",
  56          stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  57  
  58  
  59  def expect_code(completed: subprocess.CompletedProcess, expected_code: int, msg: str):
  60      if completed.returncode != expected_code:
  61          print(f"{msg!r} failed: got code {completed.returncode}, expected {expected_code}")
  62          print_process_failure(completed)
  63          sys.exit(1)
  64      else:
  65          print(f"✓ {msg!r} passed")
  66  
  67  
  68  def print_process_failure(completed: subprocess.CompletedProcess):
  69      print(f"stdout:\n{completed.stdout.decode()}")
  70      print(f"stderr:\n{completed.stderr.decode()}")
  71  
  72  
  73  if __name__ == '__main__':
  74      main()
  75