mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-08-06 08:43:36 +02:00
Drop out channel hash helper
This commit is contained in:
+9
-1
@@ -127,6 +127,13 @@ class TestDebugEndpoint:
|
||||
channel_key = "CD" * 16
|
||||
await _insert_contact(contact_key, "Alice", last_contacted=1700000000)
|
||||
await ChannelRepository.upsert(key=channel_key, name="#flightless", on_radio=False)
|
||||
await MessageRepository.create(
|
||||
msg_type="CHAN",
|
||||
text="Alice: hello",
|
||||
received_at=1700000001,
|
||||
conversation_key=channel_key,
|
||||
sender_timestamp=1700000001,
|
||||
)
|
||||
|
||||
radio_manager.max_channels = 2
|
||||
radio_manager.path_hash_mode = 1
|
||||
@@ -187,6 +194,7 @@ class TestDebugEndpoint:
|
||||
|
||||
assert payload["application"]["commit_hash"] == "deadbeef"
|
||||
assert payload["runtime"]["channel_slot_reuse_enabled"] is True
|
||||
assert payload["runtime"]["channels_with_incoming_messages"] == 1
|
||||
assert any("support snapshot marker" in line for line in payload["logs"])
|
||||
|
||||
radio_probe = payload["radio_probe"]
|
||||
@@ -226,6 +234,7 @@ class TestDebugEndpoint:
|
||||
payload = response.json()
|
||||
assert payload["radio_probe"]["performed"] is False
|
||||
assert payload["radio_probe"]["errors"] == ["Radio not connected"]
|
||||
assert payload["runtime"]["channels_with_incoming_messages"] == 0
|
||||
|
||||
|
||||
class TestRadioDisconnectedHandler:
|
||||
@@ -328,7 +337,6 @@ class TestMessagesEndpoint:
|
||||
radio_manager._meshcore = mock_mc
|
||||
with (
|
||||
_patch_require_connected(mock_mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event") as mock_broadcast,
|
||||
):
|
||||
response = await client.post(
|
||||
|
||||
@@ -14,7 +14,6 @@ from app.decoder import (
|
||||
PayloadType,
|
||||
RouteType,
|
||||
_clamp_scalar,
|
||||
calculate_channel_hash,
|
||||
decrypt_direct_message,
|
||||
decrypt_group_text,
|
||||
derive_public_key,
|
||||
@@ -34,22 +33,8 @@ class TestChannelKeyDerivation:
|
||||
channel_name = "#test"
|
||||
expected_key = hashlib.sha256(channel_name.encode("utf-8")).digest()[:16]
|
||||
|
||||
# Verify the derived key produces the expected channel hash
|
||||
result_hash = calculate_channel_hash(expected_key)
|
||||
expected_hash = format(hashlib.sha256(expected_key).digest()[0], "02x")
|
||||
assert result_hash == expected_hash
|
||||
assert len(expected_key) == 16
|
||||
|
||||
def test_channel_hash_calculation(self):
|
||||
"""Channel hash is the first byte of SHA256(key) as hex."""
|
||||
key = bytes(16) # All zeros
|
||||
expected_hash = format(hashlib.sha256(key).digest()[0], "02x")
|
||||
|
||||
result = calculate_channel_hash(key)
|
||||
|
||||
assert result == expected_hash
|
||||
assert len(result) == 2 # Two hex chars
|
||||
|
||||
|
||||
class TestPacketParsing:
|
||||
"""Test raw packet header parsing."""
|
||||
|
||||
@@ -210,12 +210,10 @@ class TestChannelDecryption:
|
||||
|
||||
def test_channel_hash_matches_packet(self):
|
||||
"""Channel hash in packet matches hash computed from key."""
|
||||
from app.decoder import calculate_channel_hash
|
||||
|
||||
info = parse_packet(CHANNEL_PACKET)
|
||||
assert info is not None
|
||||
packet_hash = format(info.payload[0], "02x")
|
||||
expected_hash = calculate_channel_hash(CHANNEL_KEY)
|
||||
expected_hash = format(sha256(CHANNEL_KEY).digest()[0], "02x")
|
||||
assert packet_hash == expected_hash
|
||||
|
||||
def test_wrong_channel_key_fails(self):
|
||||
|
||||
@@ -209,7 +209,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event", side_effect=capture_broadcast),
|
||||
):
|
||||
request = SendChannelMessageRequest(channel_key=chan_key, text="!lasttime5 someone")
|
||||
@@ -234,7 +233,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
):
|
||||
request = SendChannelMessageRequest(channel_key=chan_key, text="acked now")
|
||||
@@ -262,7 +260,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event", side_effect=capture_broadcast),
|
||||
):
|
||||
request = SendChannelMessageRequest(channel_key=chan_key, text="hello")
|
||||
@@ -293,7 +290,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
):
|
||||
request = SendChannelMessageRequest(channel_key=chan_key, text="hello")
|
||||
@@ -317,7 +313,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
):
|
||||
request = SendChannelMessageRequest(channel_key=chan_key, text="hello")
|
||||
@@ -339,7 +334,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
pytest.raises(HTTPException) as exc_info,
|
||||
):
|
||||
@@ -362,7 +356,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
):
|
||||
await send_channel_message(
|
||||
@@ -391,7 +384,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
):
|
||||
await send_channel_message(
|
||||
@@ -433,7 +425,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
):
|
||||
await send_channel_message(
|
||||
@@ -458,7 +449,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
patch("app.radio.settings.force_channel_slot_reconfigure", True),
|
||||
):
|
||||
@@ -487,7 +477,6 @@ class TestOutgoingChannelBroadcast:
|
||||
with (
|
||||
patch("app.routers.messages.require_connected", return_value=mc),
|
||||
patch.object(radio_manager, "_meshcore", mc),
|
||||
patch("app.decoder.calculate_channel_hash", return_value="abcd"),
|
||||
patch("app.routers.messages.broadcast_event"),
|
||||
pytest.raises(HTTPException) as exc_info,
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user