test 2.22 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 43 44
    def firmware_size(self):
        return os.stat(self.firmware_path).st_size

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

53 54 55 56 57 58 59
        self.ser.setRTS(True)
        time.sleep(0.1)
        self.ser.setRTS(False)
        time.sleep(0.1)
        self.ser.setRTS(True)
        time.sleep(1)

lwc-tester committed
60
    def flash(self):
lwc-tester committed
61 62 63 64 65 66 67 68 69 70 71
        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
72 73
        eprint("Firmware flashed.")

74 75 76
        self.reset()
        self.reset()

lwc-tester committed
77 78
    def dump_ram(self):
        return None
Enrico Pozzobon committed
79 80 81


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

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

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


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