Skip to content

Commit

Permalink
Allow testing in MicroPython
Browse files Browse the repository at this point in the history
  • Loading branch information
argilo committed Mar 4, 2024
1 parent c7bfe40 commit 2c2da2d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ jobs:
- name: Test
env:
TEST_CYCLES: 50000
run: ./test_secplus.py
run: |
./test_secplus.py
TEST_MODE=c ./test_secplus.py
TEST_MODE=avr ./test_secplus.py
50 changes: 23 additions & 27 deletions test_secplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
#

import os
import platform
import random
import unittest
import secplus
import struct
import subprocess
import sys
from ctypes import *

Expand Down Expand Up @@ -137,6 +135,22 @@ class TestSecplus(unittest.TestCase):
0x360e281, 0x260f281, 0x360e281, 0x260f281, 0x8193, 0x8193, 0x8193, 0x8193,
]

@classmethod
def setUpClass(cls):
if os.getenv("TEST_MODE") == "c":
print("Testing C implementation.", file=sys.stderr)
substitute_c()
elif os.getenv("TEST_MODE") == "avr":
print("Testing C implementation in AVR simulator.", file=sys.stderr)
cls._process = substitute_avr()
else:
print("Testing Python implementation.", file=sys.stderr)

@classmethod
def tearDownClass(cls):
if os.getenv("TEST_MODE") == "avr":
shutdown_avr(cls._process)

def test_encode_decode(self):
for _ in range(self.test_cycles):
rolling = random.randrange(2**32) & 0xfffffffe
Expand Down Expand Up @@ -574,6 +588,8 @@ def test_decode_wireline(self):


def substitute_c():
import platform

if platform.system() == "Linux":
libsecplus = cdll.LoadLibrary("./libsecplus.so")
elif platform.system() == "Darwin":
Expand Down Expand Up @@ -692,6 +708,8 @@ def decode_wireline(code):


def substitute_avr():
import subprocess

sim = subprocess.Popen(["simulavr", "-d", "attiny85", "-f", "test/avr_test.elf", "-W", "0x20,-", "-R", "0x22,-", "-T", "exit"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Expand Down Expand Up @@ -815,28 +833,6 @@ def shutdown_avr(sim):


if __name__ == '__main__':
status = 0

print("Testing Python:", file=sys.stderr)
result = unittest.main(exit=False)
if not result.result.wasSuccessful():
status = 1

substitute_c()

print("Testing C:", file=sys.stderr)
result = unittest.main(exit=False)
if not result.result.wasSuccessful():
status = 1

process = substitute_avr()

print("Testing C in AVR simulator:", file=sys.stderr)
TestSecplus.test_cycles //= 50
result = unittest.main(exit=False)
if not result.result.wasSuccessful():
status = 1

shutdown_avr(process)

exit(status)
result = unittest.main()
if not result.wasSuccessful():
sys.exit(1)

0 comments on commit 2c2da2d

Please sign in to comment.