test 2.39 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3

import os
import sys
5
import time
6
import subprocess
lwc-tester committed
7 8 9 10 11 12
import serial.tools.list_ports
from test_common import (
    LogicMultiplexerTimeMeasurements,
    parse_nist_aead_test_vectors,
    DeviceUnderTestAeadUARTP,
    eprint,
13
    FileMutex,
lwc-tester committed
14 15
    run_nist_aead_test_line,
)
16

17

lwc-tester committed
18 19 20 21 22 23 24 25 26
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]
27

28

lwc-tester committed
29
class Uno(DeviceUnderTestAeadUARTP):
30

lwc-tester committed
31 32
    def __init__(self, build_dir):
        DeviceUnderTestAeadUARTP.__init__(self)
33

34 35 36
        self.uart_device = get_serial()
        devname = os.path.basename(self.uart_device)
        self.lock = FileMutex('/var/lock/lwc-compare.%s.lock' % devname)
lwc-tester committed
37 38
        self.build_dir = build_dir

39 40 41 42 43 44 45 46 47 48 49 50 51 52
    def reset(self):
        self.ser.setDTR(True)
        time.sleep(0.01)
        self.ser.setDTR(False)
        time.sleep(1)

    def prepare(self):
        self.ser = serial.Serial(
            self.uart_device,
            baudrate=115200,
            timeout=5)
        self.reset()
        DeviceUnderTestAeadUARTP.prepare(self)

lwc-tester committed
53 54 55 56 57
    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']
58
        cmd.extend(['--upload-port', self.uart_device])
lwc-tester committed
59 60 61
        p = subprocess.Popen(
            cmd, stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
62
        assert p.returncode == 0
lwc-tester committed
63 64 65 66 67
        eprint("Firmware flashed.")
        os.chdir(previous_dir)

    def dump_ram(self):
        return None
68 69


70
def main(argv):
lwc-tester committed
71 72
    if len(argv) != 3:
        print("Usage: test LWC_AEAD_KAT.txt build_dir")
73
        return 1
lwc-tester committed
74 75 76 77

    kat = list(parse_nist_aead_test_vectors(argv[1]))
    build_dir = argv[2]

lwc-tester committed
78
    dut = Uno(build_dir)
lwc-tester committed
79

80 81 82 83 84
    dut.flash()
    dut.prepare()
    sys.stdout.write("Board prepared\n")
    sys.stdout.flush()

lwc-tester committed
85
    try:
86
        tool = LogicMultiplexerTimeMeasurements(0x0800)
lwc-tester committed
87 88 89 90 91 92 93 94 95 96 97 98 99
        tool.begin_measurement()

        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()
100

101 102 103

if __name__ == "__main__":
    sys.exit(main(sys.argv))