#!/usr/bin/env python3 import re import os import sys import struct from subprocess import Popen, PIPE def main(argv): speed_test = True if len(argv) < 3: print("Usage: test.py LWC_AEAD_KAT.txt program [arguments]") cmd = argv[2:] for attempt in range(3): print("beginning test %d of '%s' using test vectors '%s'" % (attempt, ' '.join(cmd), argv[1])) try: if speed_test: measurements = begin_measurement() try: test(argv[1], cmd) finally: if speed_test: end_measurement(measurements) print("TEST SUCCESSFUL") return 0 except Exception as ex: print(str(ex)) print("TEST FAILED") finally: sys.stdout.flush() sys.stderr.flush() return 1 def test(test_file, cmd, ram_test=False): test_file = open(test_file, 'r') p = Popen(cmd, bufsize=0, stdin=PIPE, stdout=PIPE) def write(data): l = p.stdin.write(data) if len(data) != l: raise Exception("could not write %d bytes of data (put %d)" % (len(data), l)) def read(l): if l == 0: return b"" data = p.stdout.read(l) if len(data) == 0: print("Unexpected end of stream", file=sys.stderr) #sys.exit(1) if len(data) != l: raise Exception("could not read %d bytes of data (got %d)" % (l, len(data))) return data def submit(action, data): h = struct.pack(" %s\n" % (' '.join(sys.argv), outfile)) mdbfile.close() return 0 raise Exception("Capture didn't complete successfully") if __name__ == "__main__": sys.exit(main(sys.argv))