test 2.39 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 12 13 14
import serial.tools.list_ports
from test_common import (
    LogicMultiplexerTimeMeasurements,
    parse_nist_aead_test_vectors,
    DeviceUnderTestAeadUARTP,
    eprint,
    run_nist_aead_test_line,
)
Enrico Pozzobon committed
15 16


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


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

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

lwc-tester committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        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', 'sipeed-maixduino']
        cmd.extend(['--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
Enrico Pozzobon committed
52 53 54


def main(argv):
lwc-tester committed
55 56
    if len(argv) != 3:
        print("Usage: test LWC_AEAD_KAT.txt build_dir")
Enrico Pozzobon committed
57
        return 1
lwc-tester committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

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

    dut = Maixduino(build_dir)

    try:
        tool = LogicMultiplexerTimeMeasurements(0x00c0)
        tool.begin_measurement()

        dut.flash()

        ser = serial.Serial(
            get_serial(),
            baudrate=1500000,
            timeout=5)

        ser.setRTS(True)
        time.sleep(0.1)
        ser.setRTS(False)
        time.sleep(0.1)
        ser.setRTS(True)
        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))
Enrico Pozzobon committed
105 106 107 108


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