Skip to content

Commit 9547082

Browse files
committed
show mock and lint
1 parent 6f5638b commit 9547082

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"request": "launch",
88
"program": "${workspaceFolder}/.venv/bin/pytest",
99
"args": [
10-
"tests/unit",
10+
"cpython-workspaces/flight-software-unit-tests/src",
1111
"--maxfail=1",
1212
"--disable-warnings"
1313
],

circuitpython-workspaces/flight-software/src/pysquared/hmac_auth.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@
2121
```
2222
"""
2323

24-
import circuitpython_hmac as hmac
25-
26-
try:
27-
# CircuitPython uses adafruit_hashlib
28-
import adafruit_hashlib as hashlib
29-
except ImportError:
30-
# Standard Python uses hashlib
31-
import hashlib
24+
import adafruit_hashlib as hashlib # interesting, this lib imports cpython stuff if it's available.... hmmm
25+
from circuitpython_hmac import HMAC
3226

3327

3428
class HMACAuthenticator:
@@ -58,26 +52,28 @@ def generate_hmac(self, message: str, counter: int) -> str:
5852
# Generate HMAC using SHA-256
5953
# Note: In CircuitPython, this uses the circuitpython_hmac library
6054
# In testing/CPython, this uses the standard hmac library
61-
h = hmac.new(self._secret_key, data, hashlib.sha256)
55+
h = HMAC(self._secret_key, data, hashlib.sha256)
6256
return h.hexdigest()
6357

6458
def compare_digest(expected_hmac: str, received_hmac: str):
6559
"""Compares two byte or str sequences in constant time.
6660
Returns True if expected_hmac == recieved_hmac, False otherwise.
6761
"""
68-
if not isinstance(expected_hmac, (bytes, bytearray, str)) or not isinstance(received_hmac, (bytes, bytearray, str)):
62+
if not isinstance(expected_hmac, (bytes, bytearray, str)) or not isinstance(
63+
received_hmac, (bytes, bytearray, str)
64+
):
6965
raise TypeError("compare_digest() expects two bytes or two str objects")
7066
# Convert strings to bytes if both are str
7167
if isinstance(expected_hmac, str) and isinstance(received_hmac, str):
72-
expected_hmac = expected_hmac.encode('utf-8')
73-
received_hmac = received_hmac.encode('utf-8')
68+
expected_hmac = expected_hmac.encode("utf-8")
69+
received_hmac = received_hmac.encode("utf-8")
7470
elif isinstance(expected_hmac, str) or isinstance(received_hmac, str):
7571
raise TypeError("Both inputs must be of the same type")
7672
# Ensure both are bytes/bytearray at this point
7773
if len(expected_hmac) != len(received_hmac):
7874
# Continue processing full length to keep timing consistent
7975
result = 0
80-
maxlen = max(len(expected_hmac), len(b))
76+
maxlen = max(len(expected_hmac), len(received_hmac))
8177
for i in range(maxlen):
8278
x = expected_hmac[i] if i < len(expected_hmac) else 0
8379
y = received_hmac[i] if i < len(received_hmac) else 0

cpython-workspaces/flight-software-unit-tests/src/unit-tests/test_hmac_integration.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import json
8+
from typing import Generator
89
from unittest.mock import MagicMock, patch
910

1011
import pytest
@@ -63,12 +64,25 @@ def ground_station_authenticator(mock_config):
6364
return HMACAuthenticator(mock_config.hmac_secret)
6465

6566

67+
@pytest.fixture
68+
def mock_hmac() -> Generator[MagicMock, None, None]:
69+
"""Mocks the HMAC class.
70+
71+
Yields:
72+
A MagicMock instance of HMAC.
73+
"""
74+
with patch("pysquared.hmac_auth.HMAC") as mock_class:
75+
mock_class.return_value = MagicMock()
76+
yield mock_class
77+
78+
6679
def test_hmac_integration_valid_command(
6780
flight_software_cdh,
6881
ground_station_authenticator,
6982
mock_config,
7083
mock_packet_manager,
7184
mock_counter16,
85+
mock_hmac,
7286
):
7387
"""Tests successful HMAC authentication flow from ground station to flight software.
7488
@@ -121,6 +135,7 @@ def test_hmac_integration_invalid_hmac(
121135
mock_config,
122136
mock_packet_manager,
123137
mock_logger,
138+
mock_hmac,
124139
):
125140
"""Tests that flight software rejects commands with invalid HMAC.
126141
@@ -170,6 +185,7 @@ def test_hmac_integration_replay_attack(
170185
mock_packet_manager,
171186
mock_counter16,
172187
mock_logger,
188+
mock_hmac,
173189
):
174190
"""Tests that flight software prevents replay attacks.
175191
@@ -226,6 +242,7 @@ def test_hmac_integration_counter_sequence(
226242
mock_config,
227243
mock_packet_manager,
228244
mock_counter16,
245+
mock_hmac,
229246
):
230247
"""Tests multiple commands with incrementing counters.
231248
@@ -271,6 +288,7 @@ def test_hmac_integration_counter_wraparound(
271288
mock_config,
272289
mock_packet_manager,
273290
mock_counter16,
291+
mock_hmac,
274292
):
275293
"""Tests counter wraparound handling in integration scenario.
276294
@@ -308,7 +326,11 @@ def test_hmac_integration_counter_wraparound(
308326

309327

310328
def test_hmac_integration_different_secrets(
311-
mock_logger, mock_config, mock_packet_manager, mock_counter16
329+
mock_logger,
330+
mock_config,
331+
mock_packet_manager,
332+
mock_counter16,
333+
mock_hmac,
312334
):
313335
"""Tests that flight software rejects commands with different HMAC secret.
314336
@@ -357,6 +379,7 @@ def test_hmac_integration_large_message(
357379
mock_config,
358380
mock_packet_manager,
359381
mock_counter16,
382+
mock_hmac,
360383
):
361384
"""Tests HMAC authentication with large message requiring packetization.
362385

0 commit comments

Comments
 (0)