test 2.19 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
        self.build_dir = build_dir
lwc-tester committed
37 38 39 40
        self.template_path = os.path.dirname(sys.argv[0])

        self.firmware_path = os.path.join(
            build_dir, 'firmware.bin')
lwc-tester committed
41

42
    def reset(self):
lwc-tester committed
43 44 45 46 47 48 49
        if self.ser is not None:
            self.ser.close()
        self.ser = serial.Serial(
            self.uart_device,
            baudrate=1500000,
            timeout=5)

50 51 52 53 54 55 56 57
        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):
lwc-tester committed
58
        self.reset()
59 60 61
        self.reset()
        DeviceUnderTestAeadUARTP.prepare(self)

lwc-tester committed
62
    def flash(self):
lwc-tester committed
63 64 65 66 67 68 69 70 71 72 73
        pio_packages_path = "/home/tester/.platformio/packages/"
        kflash_path = os.path.join(
            pio_packages_path, "tool-kflash-kendryte210/kflash.py")

        cmd = ['python3', kflash_path]
        cmd += ['-b', '750000']
        cmd += ['-p', self.uart_device]
        cmd += ['-B', 'maixduino']
        cmd += [self.firmware_path]
        subprocess.check_call(cmd)

lwc-tester committed
74 75 76 77
        eprint("Firmware flashed.")

    def dump_ram(self):
        return None
Enrico Pozzobon committed
78 79 80


def main(argv):
lwc-tester committed
81 82
    if len(argv) != 2:
        print("Usage: test build_dir")
Enrico Pozzobon committed
83
        return 1
lwc-tester committed
84

lwc-tester committed
85 86 87 88
    build_dir = argv[1]
    kat_path = os.path.join(build_dir, 'LWC_AEAD_KAT.txt')

    run_nist_lws_aead_test(kat_path, build_dir, 0x0080, Maixduino)
lwc-tester committed
89
    return 0
Enrico Pozzobon committed
90 91 92 93


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