test 2.47 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3

import os
import sys
5
import time
6
import subprocess
lwc-tester committed
7 8 9 10
import serial.tools.list_ports
from test_common import (
    DeviceUnderTestAeadUARTP,
    eprint,
11
    FileMutex,
lwc-tester committed
12
    run_nist_lws_aead_test,
lwc-tester committed
13
)
14

15

lwc-tester committed
16 17 18 19 20 21 22 23 24
def get_serial():
    ports = serial.tools.list_ports.comports()
    devices = [
        p.device
        for p in ports
        if (p.vid == 0x1A86 and p.pid == 0x7523)
    ]
    devices.sort()
    return devices[0]
25

26

lwc-tester committed
27
class Uno(DeviceUnderTestAeadUARTP):
28

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

32 33 34
        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
35
        self.build_dir = build_dir
lwc-tester committed
36 37 38 39
        self.template_path = os.path.dirname(sys.argv[0])

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

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

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

lwc-tester committed
55 56
    def flash(self):
        pipe = subprocess.PIPE
lwc-tester committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        avrdude_dir = '/home/tester/.platformio/packages/tool-avrdude/'

        cmd = [os.path.join(avrdude_dir, 'avrdude')]
        cmd += ['-C', os.path.join(avrdude_dir, 'avrdude.conf')]
        cmd += ['-v']
        cmd += ['-p', 'atmega328p']
        cmd += ['-c', 'arduino']
        cmd += ['-P', self.uart_device]
        cmd += ['-b', '115200']
        cmd += ['-D']
        cmd += ['-U', 'flash:w:%s:i' % os.path.abspath(self.firmware_path)]

        self.ser = serial.Serial(
            self.uart_device,
            baudrate=115200,
            timeout=5)
        self.ser.setDTR(True)
        time.sleep(0.01)
        self.ser.setDTR(False)
        self.ser.close()

lwc-tester committed
78 79 80
        p = subprocess.Popen(
            cmd, stdout=sys.stderr, stdin=pipe)
        stdout, stderr = p.communicate("")
81
        assert p.returncode == 0
lwc-tester committed
82 83 84 85
        eprint("Firmware flashed.")

    def dump_ram(self):
        return None
86 87


88
def main(argv):
lwc-tester committed
89 90
    if len(argv) != 2:
        print("Usage: test build_dir")
91
        return 1
lwc-tester committed
92

lwc-tester committed
93 94 95 96
    build_dir = argv[1]
    kat_path = os.path.join(build_dir, 'LWC_AEAD_KAT.txt')

    run_nist_lws_aead_test(kat_path, build_dir, 0x0800, Uno)
lwc-tester committed
97
    return 0
98

99 100 101

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