Skip to content

Commit

Permalink
Show test as skipped when not supported by the device
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Sep 29, 2023
1 parent 0b1035e commit 6d17acb
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions pynitrokey/cli/nk3/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
from struct import unpack
from threading import Thread
from types import TracebackType
from typing import Any, Callable, Iterable, Optional, Tuple, Type, Union
from typing import Any, Callable, Iterable, Optional, Tuple, Type, Union, cast

from fido2.ctap import CtapError
from tqdm import tqdm

from pynitrokey.cli.exceptions import CliException
Expand Down Expand Up @@ -354,6 +355,10 @@ def select(conn: CardConnection, aid: list[int]) -> bool:
]


class NotSupported:
pass


@test_case("se050", "SE050")
def test_se050(ctx: TestContext, device: Nitrokey3Base) -> TestResult:
from queue import Queue
Expand All @@ -368,10 +373,18 @@ def test_se050(ctx: TestContext, device: Nitrokey3Base) -> TestResult:
):
return TestResult(TestStatus.SKIPPED)

que: Queue[Optional[bytes]] = Queue()
que: Queue[Union[bytes, None, type[NotSupported]]] = Queue()

def internal_se050_run(q: Queue[Optional[bytes]]) -> None:
q.put(AdminApp(device).se050_tests())
def internal_se050_run(
q: Queue[Union[bytes, None, type[NotSupported]]],
) -> None:
try:
q.put(AdminApp(device).se050_tests())
except CtapError as e:
if e.code == CtapError.ERR.INVALID_LENGTH:
q.put(NotSupported())
else:
q.put(None)

t = Thread(target=internal_se050_run, args=[que])
t.start()
Expand All @@ -395,8 +408,18 @@ def internal_se050_run(q: Queue[Optional[bytes]]) -> None:
bar.close()
result = que.get()

if result is None or len(result) < 11:
return TestResult(TestStatus.FAILURE, "Did not get test run data")
if isinstance(result, NotSupported):
# This means that the device responded with `NotAvailable`, so either it is a version that doesn't support this feature or it was disabled at compile time
return TestResult(
TestStatus.SKIPPED,
"Testing SE050 functionality is not supported by the device",
)
elif result is None:
return TestResult(TestStatus.FAILURE, "Failed to get test run data")

result = cast(bytes, result)
if len(result) < 11:
return TestResult(TestStatus.FAILURE, "Did not get full test run data")
major = result[0]
minor = result[1]
patch = result[2]
Expand Down

0 comments on commit 6d17acb

Please sign in to comment.