#!/usr/bin/env python3 import os import sys import time import subprocess import serial.tools.list_ports from test_common import ( LogicMultiplexerTimeMeasurements, parse_nist_aead_test_vectors, DeviceUnderTestAeadUARTP, eprint, run_nist_aead_test_line, ) def get_serial(): ports = serial.tools.list_ports.comports() devices = [ p.device for p in ports if (p.vid == 0x1A86 and p.pid == 0x7523) ] devices.sort() return devices[0] class ESP32(DeviceUnderTestAeadUARTP): def __init__(self, build_dir): DeviceUnderTestAeadUARTP.__init__(self) self.build_dir = build_dir def flash(self): pipe = subprocess.PIPE previous_dir = os.path.abspath(os.curdir) os.chdir(self.build_dir) cmd = ['platformio', 'run', '-e', 'uno', '--target', 'upload'] cmd.extend(['--upload-port', get_serial()]) cmd.extend(['--upload-port', get_serial()]) p = subprocess.Popen( cmd, stdout=sys.stderr, stdin=pipe) stdout, stderr = p.communicate("") eprint("Firmware flashed.") os.chdir(previous_dir) def dump_ram(self): return None def main(argv): if len(argv) != 3: print("Usage: test LWC_AEAD_KAT.txt build_dir") return 1 kat = list(parse_nist_aead_test_vectors(argv[1])) build_dir = argv[2] dut = ESP32(build_dir) try: tool = LogicMultiplexerTimeMeasurements(0x0c00) tool.begin_measurement() dut.flash() ser = serial.Serial( get_serial(), baudrate=115200, timeout=5) ser.setDTR(True) time.sleep(0.01) ser.setDTR(False) time.sleep(1) dut.ser = ser dut.prepare() sys.stdout.write("Board prepared\n") sys.stdout.flush() for i, m, ad, k, npub, c in kat: tool.arm() run_nist_aead_test_line(dut, i, m, ad, k, npub, c) tool.unarm() except Exception as ex: print("TEST FAILED") raise ex finally: tool.end_measurement() sys.stdout.flush() sys.stderr.flush() if __name__ == "__main__": sys.exit(main(sys.argv))