test 2.53 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6
#!/usr/bin/env python3

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


lwc-tester committed
18 19 20 21 22 23 24 25 26 27
def get_serial():
    import serial.tools.list_ports
    ports = serial.tools.list_ports.comports()
    ports = [
        c
        for c in ports
        if c.product == 'Sipeed-Debug'
    ]
    ports.sort(key=lambda d: d.location)
    return ports[0].device
Enrico Pozzobon committed
28 29


lwc-tester committed
30
class Maixduino(DeviceUnderTestAeadUARTP):
Enrico Pozzobon committed
31

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

35 36 37
        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
38 39
        self.build_dir = build_dir

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    def reset(self):
        self.ser.setRTS(True)
        time.sleep(0.1)
        self.ser.setRTS(False)
        time.sleep(0.1)
        self.ser.setRTS(True)
        time.sleep(1)

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

lwc-tester committed
56 57 58 59 60 61
    def flash(self):
        pipe = subprocess.PIPE
        previous_dir = os.path.abspath(os.curdir)
        os.chdir(self.build_dir)
        cmd = ['platformio', 'run', '-e', 'sipeed-maixduino']
        cmd.extend(['--target', 'upload'])
62
        cmd.extend(['--upload-port', self.uart_device])
lwc-tester committed
63 64 65
        p = subprocess.Popen(
            cmd, stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
66
        assert p.returncode == 0
lwc-tester committed
67 68 69 70 71
        eprint("Firmware flashed.")
        os.chdir(previous_dir)

    def dump_ram(self):
        return None
Enrico Pozzobon committed
72 73 74


def main(argv):
lwc-tester committed
75 76
    if len(argv) != 3:
        print("Usage: test LWC_AEAD_KAT.txt build_dir")
Enrico Pozzobon committed
77
        return 1
lwc-tester committed
78 79 80 81 82

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

    dut = Maixduino(build_dir)
83 84 85 86
    dut.flash()
    dut.prepare()
    sys.stdout.write("Board prepared\n")
    sys.stdout.flush()
lwc-tester committed
87 88

    try:
89
        tool = LogicMultiplexerTimeMeasurements(0x0080)
lwc-tester committed
90 91 92 93 94 95 96 97 98 99 100 101 102
        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()
Enrico Pozzobon committed
103 104 105 106


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