test 1.97 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
import serial.tools.list_ports
from test_common import (
    DeviceUnderTestAeadUARTP,
10
    FileMutex,
lwc-tester committed
11
    eprint,
lwc-tester committed
12
    run_nist_lws_aead_test,
lwc-tester committed
13
)
Enrico Pozzobon committed
14 15


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


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

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

33 34 35
        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
36 37
        self.build_dir = build_dir

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    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
54 55 56 57 58 59
    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'])
60
        cmd.extend(['--upload-port', self.uart_device])
lwc-tester committed
61 62 63
        p = subprocess.Popen(
            cmd, stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
64
        assert p.returncode == 0
lwc-tester committed
65 66 67 68 69
        eprint("Firmware flashed.")
        os.chdir(previous_dir)

    def dump_ram(self):
        return None
Enrico Pozzobon committed
70 71 72


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

lwc-tester committed
77 78
    run_nist_lws_aead_test(argv[1], argv[2], 0x0080, Maixduino)
    return 0
Enrico Pozzobon committed
79 80 81 82


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