Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grdddj/core unit tests cleanup #2196

Merged
merged 10 commits into from
Feb 22, 2024
12 changes: 9 additions & 3 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
model: [T2T1] # FIXME T2B1 https://github.com/trezor/trezor-firmware/issues/2724
model: [T2T1, T2B1]
asan: ${{ fromJSON(github.event_name == 'schedule' && '["noasan", "asan"]' || '["noasan"]') }}
env:
TREZOR_MODEL: ${{ matrix.model == 'T2T1' && 'T' || 'R' }}
Expand All @@ -123,8 +123,14 @@ jobs:
needs: core_emu
strategy:
matrix:
model: [T2T1] # FIXME: T2B1 https://github.com/trezor/trezor-firmware/issues/2724
model: [T2T1, T2B1]
asan: ${{ fromJSON(github.event_name == 'schedule' && '["noasan", "asan"]' || '["noasan"]') }}
# T2B1 fails due to rust clippy error which is hard to reproduce, see discussion here:
# https://github.com/trezor/trezor-firmware/pull/2196
# The problem might be in conflicting versions of clippy, let's see if this helps:
# https://github.com/trezor/trezor-firmware/issues/3337
exclude:
- model: T2B1
env:
TREZOR_MODEL: ${{ matrix.model == 'T2T1' && 'T' || 'R' }}
ADDRESS_SANITIZER: ${{ matrix.asan == 'asan' && '1' || '0' }}
Expand All @@ -146,7 +152,7 @@ jobs:
needs: core_emu
strategy:
matrix:
model: [T2T1] # FIXME: T2B1 https://github.com/trezor/trezor-firmware/issues/2724
model: [T2T1, T2B1]
steps:
- uses: actions/checkout@v4
with:
Expand Down
15 changes: 15 additions & 0 deletions ci/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ core unix regular build:
- core/build/unix # most of it needed by test_rust
expire_in: 1 week

# Non-frozen emulator build for model R.
core unix regular R build:
stage: build
<<: *gitlab_caching
needs: []
variables:
TREZOR_MODEL: "R"
script:
- $NIX_SHELL --run "poetry run make -C core build_unix"
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_SHORT_SHA"
paths:
- core/build/unix # most of it needed by test_rust
expire_in: 1 week

core unix regular asan build:
stage: build
<<: *gitlab_caching
Expand Down
9 changes: 9 additions & 0 deletions ci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ core unit python test:
script:
- $NIX_SHELL --run "poetry run make -C core test | ts -s"

# Python unit tests, checking core functionality. For model R.
core unit python R test:
stage: test
<<: *gitlab_caching
needs:
- core unix regular R build
script:
- $NIX_SHELL --run "poetry run make -C core test | ts -s"

# Rust unit tests.
core unit rust test:
stage: test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub const HOMESCREEN_IMAGE_WIDTH: i16 = WIDTH;
pub const HOMESCREEN_IMAGE_HEIGHT: i16 = HEIGHT;
pub const HOMESCREEN_TOIF_SIZE: i16 = 144;
pub const HOMESCREEN_TOIF_Y_OFFSET: i16 = 27;
pub const HOMESCREEN_TOIF_X_OFFSET: usize = ((WIDTH - HOMESCREEN_TOIF_SIZE) / 2) as usize;
pub const HOMESCREEN_TOIF_X_OFFSET: usize =
((WIDTH.saturating_sub(HOMESCREEN_TOIF_SIZE)) / 2) as usize;

const HOMESCREEN_MAX_ICON_SIZE: i16 = 20;
const NOTIFICATION_HEIGHT: i16 = 36;
Expand Down
12 changes: 3 additions & 9 deletions core/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@

sys.path.append("../src")

from ubinascii import hexlify, unhexlify # noqa: F401

import unittest # noqa: F401
from typing import Any, Awaitable
from ubinascii import hexlify, unhexlify # noqa: F401

from trezor import utils # noqa: F401

from apps.common.paths import HARDENED


def H_(x: int) -> int:
"""
Shortcut function that "hardens" a number in a BIP44 path.
"""
return x | HARDENED


def UH_(x: int) -> int:
"""
Shortcut function that "un-hardens" a number in a BIP44 path.
"""
return x & ~(HARDENED)


def await_result(task: Awaitable) -> Any:
value = None
while True:
Expand Down
2 changes: 1 addition & 1 deletion core/tests/ethereum_common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from ubinascii import unhexlify # noqa: F401

from trezor import messages, protobuf
from trezor.enums import EthereumDefinitionType
from trezor.crypto import cosi
from trezor.crypto.curve import ed25519
from trezor.crypto.hashlib import sha256
from trezor.enums import EthereumDefinitionType

PRIVATE_KEYS_DEV = [byte * 32 for byte in (b"\xdd", b"\xde", b"\xdf")]

Expand Down
2 changes: 1 addition & 1 deletion core/tests/mock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if False:
if False: # noqa
from typing import Any


Expand Down
10 changes: 5 additions & 5 deletions core/tests/mock_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import storage.common
from mock import patch

import storage.common

class MockStorage:
PATCH_METHODS = ("get", "set", "delete")
Expand All @@ -25,13 +25,13 @@ def delete(self, app: int, key: int, public: bool = False) -> None:
self.namespace[app].pop(key, None)

def __enter__(self):
for patch in self.patches:
patch.__enter__()
for self_patch in self.patches:
self_patch.__enter__()
return self

def __exit__(self, exc_type, exc_value, tb):
for patch in self.patches:
patch.__exit__(exc_type, exc_value, tb)
for self_patch in self.patches:
self_patch.__exit__(exc_type, exc_value, tb)


def mock_storage(func):
Expand Down
79 changes: 43 additions & 36 deletions core/tests/production_tests/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import utime

import trezorio as io
import trezorui as ui
import utime

usb_vcp = io.VCP(
iface_num=0x00,
Expand Down Expand Up @@ -34,17 +33,17 @@
def test_display(colors):
d.clear()
m = {
'R': 0xF800,
'G': 0x07E0,
'B': 0x001F,
'W': 0xFFFF,
"R": 0xF800,
"G": 0x07E0,
"B": 0x001F,
"W": 0xFFFF,
}
w = 240 // len(colors)
for i, c in enumerate(colors):
c = m.get(c, 0x0000)
d.bar(i * w, 0, i * w + w, 240, c)
d.refresh()
print('OK')
print("OK")


def test_touch(v):
Expand All @@ -68,14 +67,22 @@ def test_touch(v):
touch = False
while True:
if not touch:
if io.poll([io.TOUCH], r, 10000) and r[0] == io.TOUCH and r[1][0] == io.TOUCH_START:
if (
io.poll([io.TOUCH], r, 10000)
and r[0] == io.TOUCH
and r[1][0] == io.TOUCH_START
):
touch = True
else:
if io.poll([io.TOUCH], r, 10000) and r[0] == io.TOUCH and r[1][0] == io.TOUCH_END:
print(f'OK {r[1][1]} {r[1][2]}')
if (
io.poll([io.TOUCH], r, 10000)
and r[0] == io.TOUCH
and r[1][0] == io.TOUCH_END
):
print(f"OK {r[1][1]} {r[1][2]}")
break
if utime.ticks_us() > deadline:
print('ERROR TIMEOUT')
print("ERROR TIMEOUT")
break
# flush all events
while io.poll([io.TOUCH], r, 10000):
Expand All @@ -87,7 +94,7 @@ def test_touch(v):
def test_pwm(v):
d.backlight(int(v))
d.refresh()
print('OK')
print("OK")


def test_sd():
Expand All @@ -97,52 +104,52 @@ def test_sd():
try:
sd.read(0, buf1)
except OSError:
print('ERROR READING DATA')
print("ERROR READING DATA")
sd.power(False)
return
try:
sd.write(0, buf1)
except OSError:
print('ERROR WRITING DATA')
print("ERROR WRITING DATA")
sd.power(False)
return
buf2 = bytearray(8 * 1024)
try:
sd.read(0, buf2)
except OSError:
print('ERROR READING DATA')
print("ERROR READING DATA")
sd.power(False)
return
if buf1 == buf2:
print('OK')
print("OK")
else:
print('ERROR DATA MISMATCH')
print("ERROR DATA MISMATCH")
sd.power(False)
else:
print('ERROR NOCARD')
print("ERROR NOCARD")


def test_sbu(v):
sbu1 = (v[0] == '1')
sbu2 = (v[1] == '1')
sbu1 = v[0] == "1"
sbu2 = v[1] == "1"
sbu.set(sbu1, sbu2)
print('OK')
print("OK")


def test_otp_read():
data = bytearray(32)
otp.read(0, 0, data)
data = bytes(data).rstrip(b'\x00\xff').decode()
print('OK', data)
data = bytes(data).rstrip(b"\x00\xff").decode()
print("OK", data)


def test_otp_write(v):
if len(v) < 32:
v = v + '\x00' * (32 - len(v))
v = v + "\x00" * (32 - len(v))
data = v[:32].encode()
otp.write(0, 0, data)
otp.lock(0)
print('OK')
print("OK")


d.clear()
Expand All @@ -152,32 +159,32 @@ def test_otp_write(v):
try:
line = input()

if line == 'PING':
print('OK')
if line == "PING":
print("OK")

elif line.startswith('DISP '):
elif line.startswith("DISP "):
test_display(line[5:])

elif line.startswith('TOUCH '):
elif line.startswith("TOUCH "):
test_touch(line[6:])

elif line.startswith('PWM '):
elif line.startswith("PWM "):
test_pwm(line[4:])

elif line == 'SD':
elif line == "SD":
test_sd()

elif line.startswith('SBU '):
elif line.startswith("SBU "):
test_sbu(line[4:])

elif line.startswith('OTP READ'):
elif line.startswith("OTP READ"):
test_otp_read()

elif line.startswith('OTP WRITE '):
elif line.startswith("OTP WRITE "):
test_otp_write(line[10:])

else:
print('UNKNOWN')
print("UNKNOWN")

except Exception as ex:
print('ERROR', ex)
print("ERROR", ex)
Loading
Loading