Commit 3a376339 by lwc-tester

maixduino new test

parent 6e1b5c82
...@@ -3,191 +3,105 @@ ...@@ -3,191 +3,105 @@
import os import os
import sys import sys
import time import time
import struct
import serial
import subprocess import subprocess
import serial.tools.list_ports
from test_common import (
LogicMultiplexerTimeMeasurements,
parse_nist_aead_test_vectors,
DeviceUnderTestAeadUARTP,
eprint,
run_nist_aead_test_line,
)
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
def eprint(*args, **kargs):
print(*args, file=sys.stderr, **kargs)
class Maixduino(DeviceUnderTestAeadUARTP):
def flash(tty=None): def __init__(self, build_dir):
pipe = subprocess.PIPE DeviceUnderTestAeadUARTP.__init__(self)
cmd = ['platformio', 'run', '--target', 'upload']
if tty is not None:
cmd.extend(['--upload-port', tty])
p = subprocess.Popen(cmd,
stdout=sys.stderr, stdin=pipe)
stdout, stderr = p.communicate("")
def get_serial(): self.build_dir = build_dir
import serial.tools.list_ports
ports = serial.tools.list_ports.comports() def flash(self):
sipeed_devices = [ c.device pipe = subprocess.PIPE
for c in ports previous_dir = os.path.abspath(os.curdir)
if c.product == 'Sipeed-Debug'] os.chdir(self.build_dir)
sipeed_devices.sort() cmd = ['platformio', 'run', '-e', 'sipeed-maixduino']
return sipeed_devices[0] cmd.extend(['--target', 'upload'])
cmd.extend(['--upload-port', get_serial()])
cmd.extend(['--upload-port', get_serial()])
class UARTP: p = subprocess.Popen(
def __init__(self, ser): cmd, stdout=sys.stderr, stdin=pipe)
UARTP.SYN = 0xf9 stdout, stderr = p.communicate("")
UARTP.FIN = 0xf3 eprint("Firmware flashed.")
self.ser = ser os.chdir(previous_dir)
def uart_read(self): def dump_ram(self):
r = self.ser.read(1) return None
if len(r) != 1:
raise Exception("Serial read error")
return r[0]
def uart_write(self, c):
b = struct.pack("B", c)
r = self.ser.write(b)
if r != len(b):
raise Exception("Serial write error")
return r
def send(self, buf):
self.uart_write(UARTP.SYN)
len_ind_0 = 0xff & len(buf)
len_ind_1 = 0xff & (len(buf) >> 7)
if len(buf) < 128:
self.uart_write(len_ind_0)
else:
self.uart_write(len_ind_0 | 0x80)
self.uart_write(len_ind_1)
fcs = 0
for i in range(len(buf)):
info = buf[i]
fcs = (fcs + info) & 0xff
self.uart_write(buf[i])
fcs = (0xff - fcs) & 0xff
self.uart_write(fcs)
self.uart_write(UARTP.FIN)
eprint("sent frame '%s'" % buf.hex())
def recv(self):
tag_old = UARTP.FIN
while 1:
tag = tag_old
while 1:
if tag_old == UARTP.FIN:
if tag == UARTP.SYN:
break
tag_old = tag
tag = self.uart_read()
tag_old = tag
l = self.uart_read()
if l & 0x80:
l &= 0x7f
l |= self.uart_read() << 7
fcs = 0
buf = []
for i in range(l):
info = self.uart_read()
buf.append(info)
fcs = (fcs + info) & 0xff
fcs = (fcs + self.uart_read()) & 0xff
tag = self.uart_read()
if fcs == 0xff:
if tag == UARTP.FIN:
buf = bytes(buf)
eprint("rcvd frame '%s'" % buf.hex())
if len(buf) >= 1 and buf[0] == 0xde:
sys.stderr.buffer.write(buf[1:])
sys.stderr.flush()
else:
return buf
def stdin_read(n):
b = sys.stdin.buffer.read(n)
if len(b) != n:
sys.exit(1)
return b
def stdin_readvar():
l = stdin_read(4)
(l, ) = struct.unpack("<I", l)
v = stdin_read(l)
return v
def main(argv): def main(argv):
eprint(argv[0]) if len(argv) != 3:
script_dir = os.path.split(argv[0])[0] print("Usage: test LWC_AEAD_KAT.txt build_dir")
if len(script_dir) > 0:
os.chdir(script_dir)
dev = get_serial()
flash(dev)
eprint("Flashed")
time.sleep(0.1)
ser = serial.Serial(dev, baudrate=1500000, timeout=5)
uartp = UARTP(ser)
ser.setRTS(True)
time.sleep(0.1)
ser.setRTS(False)
time.sleep(0.1)
ser.setRTS(True)
time.sleep(1)
exp_hello = b"Hello, World!"
hello = ser.read(len(exp_hello))
if hello != exp_hello:
eprint("Improper board initialization message: ")
return 1 return 1
eprint("Board initialized properly")
sys.stdout.write("Hello, World!\n") kat = list(parse_nist_aead_test_vectors(argv[1]))
sys.stdout.flush() build_dir = argv[2]
while 1: dut = Maixduino(build_dir)
action = stdin_read(1)[0]
eprint("Command %c from stdin" % action) try:
tool = LogicMultiplexerTimeMeasurements(0x00c0)
if action in b"ackmps": tool.begin_measurement()
v = stdin_readvar()
uartp.send(struct.pack("B", action) + v) dut.flash()
ack = uartp.recv()
if len(ack) != 1 or ack[0] != action: ser = serial.Serial(
raise Exception("Unacknowledged variable transfer") get_serial(),
eprint("Var %c successfully sent to board" % action) baudrate=1500000,
timeout=5)
elif action in b"ACKMPS":
c = struct.pack("B", action) ser.setRTS(True)
uartp.send(c) time.sleep(0.1)
v = uartp.recv() ser.setRTS(False)
if len(v) < 1 or v[0] != action: time.sleep(0.1)
raise Exception("Could not obtain variable from board") ser.setRTS(True)
v = v[1:] time.sleep(1)
eprint("Var %c received from board: %s" % (action, v.hex()))
l = struct.pack("<I", len(v)) dut.ser = ser
sys.stdout.buffer.write(l + v)
sys.stdout.flush() dut.prepare()
sys.stdout.write("Board prepared\n")
elif action in b"ed": sys.stdout.flush()
c = struct.pack("B", action)
uartp.send(c) for i, m, ad, k, npub, c in kat:
ack = uartp.recv() tool.arm()
if len(ack) < 1 or ack[0] != action: run_nist_aead_test_line(dut, i, m, ad, k, npub, c)
raise Exception("Unacknowledged variable transfer") tool.unarm()
eprint("Operation %c completed successfully" % action)
except Exception as ex:
else: print("TEST FAILED")
raise Exception("Unknown action %c" % action) raise ex
finally:
return 0 tool.end_measurement()
sys.stdout.flush()
sys.stderr.flush()
if __name__ == "__main__":
sys.exit(main(sys.argv))
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -25,7 +25,7 @@ def get_serial(): ...@@ -25,7 +25,7 @@ def get_serial():
return devices[0] return devices[0]
class ESP32(DeviceUnderTestAeadUARTP): class Uno(DeviceUnderTestAeadUARTP):
def __init__(self, build_dir): def __init__(self, build_dir):
DeviceUnderTestAeadUARTP.__init__(self) DeviceUnderTestAeadUARTP.__init__(self)
...@@ -57,7 +57,7 @@ def main(argv): ...@@ -57,7 +57,7 @@ def main(argv):
kat = list(parse_nist_aead_test_vectors(argv[1])) kat = list(parse_nist_aead_test_vectors(argv[1]))
build_dir = argv[2] build_dir = argv[2]
dut = ESP32(build_dir) dut = Uno(build_dir)
try: try:
tool = LogicMultiplexerTimeMeasurements(0x0c00) tool = LogicMultiplexerTimeMeasurements(0x0c00)
...@@ -98,4 +98,3 @@ def main(argv): ...@@ -98,4 +98,3 @@ def main(argv):
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main(sys.argv)) sys.exit(main(sys.argv))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment