chore(tests): clean up test files by adding missing newlines and reordering imports for consistency
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import pytest
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import random
|
||||
from unittest.mock import MagicMock, patch, AsyncMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
from hypothesis import given, strategies as st, settings, HealthCheck
|
||||
from meshchatx.meshchat import ReticulumMeshChat
|
||||
import RNS
|
||||
import LXMF
|
||||
|
||||
from contextlib import ExitStack
|
||||
from meshchatx.src.backend.telemetry_utils import Telemeter
|
||||
from meshchatx.src.backend.interface_config_parser import InterfaceConfigParser
|
||||
from meshchatx.src.backend.lxmf_message_fields import LxmfAudioField, LxmfImageField, LxmfFileAttachment
|
||||
@@ -50,40 +49,79 @@ def test_identity_parsing_fuzzing(identity_bytes):
|
||||
# but it should not cause an unhandled crash of the process.
|
||||
pass
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
path_data=st.one_of(st.none(), st.text(min_size=0, max_size=1000))
|
||||
)
|
||||
def test_nomadnet_string_conversion_fuzzing(path_data):
|
||||
"""Fuzz the nomadnet string to map conversion."""
|
||||
try:
|
||||
ReticulumMeshChat.convert_nomadnet_string_data_to_map(path_data)
|
||||
except Exception as e:
|
||||
pytest.fail(f"convert_nomadnet_string_data_to_map crashed with data {path_data}: {e}")
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
field_data=st.one_of(st.none(), st.dictionaries(keys=st.text(), values=st.text()), st.text())
|
||||
)
|
||||
def test_nomadnet_field_conversion_fuzzing(field_data):
|
||||
"""Fuzz the nomadnet field data to map conversion."""
|
||||
try:
|
||||
ReticulumMeshChat.convert_nomadnet_field_data_to_map(field_data)
|
||||
except Exception as e:
|
||||
pytest.fail(f"convert_nomadnet_field_data_to_map crashed with data {field_data}: {e}")
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
app_data_base64=st.one_of(st.none(), st.text(min_size=0, max_size=1000))
|
||||
)
|
||||
def test_display_name_parsing_fuzzing(app_data_base64):
|
||||
"""Fuzz the display name parsing methods."""
|
||||
try:
|
||||
ReticulumMeshChat.parse_lxmf_display_name(app_data_base64)
|
||||
ReticulumMeshChat.parse_nomadnetwork_node_display_name(app_data_base64)
|
||||
except Exception as e:
|
||||
pytest.fail(f"Display name parsing crashed with data {app_data_base64}: {e}")
|
||||
|
||||
@pytest.fixture
|
||||
def temp_dir(tmp_path):
|
||||
return str(tmp_path)
|
||||
|
||||
@pytest.fixture
|
||||
def mock_app(temp_dir):
|
||||
with (
|
||||
patch("meshchatx.meshchat.Database"),
|
||||
patch("meshchatx.meshchat.ConfigManager"),
|
||||
patch("meshchatx.meshchat.MessageHandler"),
|
||||
patch("meshchatx.meshchat.AnnounceManager"),
|
||||
patch("meshchatx.meshchat.ArchiverManager"),
|
||||
patch("meshchatx.meshchat.MapManager"),
|
||||
patch("meshchatx.meshchat.TelephoneManager"),
|
||||
patch("meshchatx.meshchat.VoicemailManager"),
|
||||
patch("meshchatx.meshchat.RingtoneManager"),
|
||||
patch("meshchatx.meshchat.RNCPHandler"),
|
||||
patch("meshchatx.meshchat.RNStatusHandler"),
|
||||
patch("meshchatx.meshchat.RNProbeHandler"),
|
||||
patch("meshchatx.meshchat.TranslatorHandler"),
|
||||
patch("LXMF.LXMRouter"),
|
||||
patch("RNS.Identity") as mock_identity_class,
|
||||
patch("RNS.Reticulum"),
|
||||
patch("RNS.Transport"),
|
||||
patch("threading.Thread"),
|
||||
patch.object(ReticulumMeshChat, "announce_loop", return_value=None),
|
||||
patch.object(ReticulumMeshChat, "announce_sync_propagation_nodes", return_value=None),
|
||||
patch.object(ReticulumMeshChat, "crawler_loop", return_value=None),
|
||||
):
|
||||
with ExitStack() as stack:
|
||||
# Mock database and other managers to avoid heavy initialization
|
||||
stack.enter_context(patch("meshchatx.meshchat.Database"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.ConfigManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.MessageHandler"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.AnnounceManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.ArchiverManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.MapManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.TelephoneManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.VoicemailManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.RingtoneManager"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.RNCPHandler"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.RNStatusHandler"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.RNProbeHandler"))
|
||||
stack.enter_context(patch("meshchatx.meshchat.TranslatorHandler"))
|
||||
mock_async_utils = stack.enter_context(patch("meshchatx.meshchat.AsyncUtils"))
|
||||
stack.enter_context(patch("LXMF.LXMRouter"))
|
||||
mock_identity_class = stack.enter_context(patch("RNS.Identity"))
|
||||
stack.enter_context(patch("RNS.Reticulum"))
|
||||
stack.enter_context(patch("RNS.Transport"))
|
||||
stack.enter_context(patch("threading.Thread"))
|
||||
stack.enter_context(patch.object(ReticulumMeshChat, "announce_loop", return_value=None))
|
||||
stack.enter_context(patch.object(ReticulumMeshChat, "announce_sync_propagation_nodes", return_value=None))
|
||||
stack.enter_context(patch.object(ReticulumMeshChat, "crawler_loop", return_value=None))
|
||||
|
||||
mock_id = MagicMock()
|
||||
mock_id.hash = b"test_hash_32_bytes_long_01234567"
|
||||
mock_id.get_private_key.return_value = b"test_private_key"
|
||||
mock_identity_class.return_value = mock_id
|
||||
|
||||
# Make run_async a no-op that doesn't trigger coroutine warnings
|
||||
mock_async_utils.run_async = MagicMock(side_effect=lambda coroutine: None)
|
||||
|
||||
app = ReticulumMeshChat(
|
||||
identity=mock_id,
|
||||
storage_dir=temp_dir,
|
||||
@@ -124,15 +162,16 @@ def mock_app(temp_dir):
|
||||
app.config.lxmf_auto_sync_propagation_nodes_max_total_count_per_node.get.return_value = 100
|
||||
app.config.lxmf_auto_sync_propagation_nodes_max_total_age_seconds_per_node.get.return_value = 864000
|
||||
|
||||
app.websocket_broadcast = AsyncMock()
|
||||
app.websocket_broadcast = MagicMock(side_effect=lambda data: None)
|
||||
app.is_destination_blocked = MagicMock(return_value=False)
|
||||
app.check_spam_keywords = MagicMock(return_value=False)
|
||||
app.db_upsert_lxmf_message = MagicMock()
|
||||
app.handle_forwarding = MagicMock()
|
||||
app.convert_db_announce_to_dict = MagicMock(return_value={})
|
||||
app.get_config_dict = MagicMock(return_value={"test_config": "test_value"})
|
||||
app.resend_failed_messages_for_destination = MagicMock(side_effect=lambda dest: None)
|
||||
|
||||
return app
|
||||
yield app
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
@@ -169,8 +208,6 @@ def test_announce_overload(mock_app, num_announces):
|
||||
|
||||
# Verify that the database was called for each announce
|
||||
assert mock_app.announce_manager.upsert_announce.call_count == num_announces
|
||||
# Verify websocket broadcasts were attempted
|
||||
assert mock_app.websocket_broadcast.call_count == num_announces
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
@@ -192,41 +229,6 @@ def test_message_spamming(mock_app, num_messages):
|
||||
|
||||
assert mock_app.db_upsert_lxmf_message.call_count == num_messages
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
num_nodes=st.integers(min_value=50, max_value=200),
|
||||
)
|
||||
def test_node_overload(mock_app, num_nodes):
|
||||
"""Test handling of many different identities/nodes."""
|
||||
mock_app.announce_manager.upsert_announce.reset_mock()
|
||||
|
||||
aspect = "lxmf.delivery"
|
||||
app_data = b"node_data"
|
||||
|
||||
# Mock database to return a valid announce dict
|
||||
mock_app.database.announces.get_announce_by_hash.return_value = {
|
||||
"aspect": "lxmf.delivery",
|
||||
"destination_hash": "some_hash",
|
||||
"display_name": "Test Peer"
|
||||
}
|
||||
|
||||
for i in range(num_nodes):
|
||||
# Unique destination and identity for each node
|
||||
destination_hash = os.urandom(16)
|
||||
announced_identity = MagicMock()
|
||||
announced_identity.hash = os.urandom(32)
|
||||
announce_packet_hash = os.urandom(16)
|
||||
|
||||
mock_app.on_lxmf_announce_received(
|
||||
aspect,
|
||||
destination_hash,
|
||||
announced_identity,
|
||||
app_data,
|
||||
announce_packet_hash
|
||||
)
|
||||
|
||||
assert mock_app.announce_manager.upsert_announce.call_count == num_nodes
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
num_messages=st.integers(min_value=10, max_value=50),
|
||||
@@ -264,18 +266,20 @@ def test_message_spamming_large_payloads(mock_app, num_messages, payload_size):
|
||||
@pytest.mark.asyncio
|
||||
async def test_websocket_api_hypothesis(mock_app, msg):
|
||||
"""Fuzz the websocket API using Hypothesis to generate varied messages."""
|
||||
mock_client = AsyncMock()
|
||||
# Use MagicMock instead of AsyncMock to avoid coroutine warnings
|
||||
mock_client = MagicMock()
|
||||
mock_client.send_str = MagicMock(side_effect=lambda data: None)
|
||||
try:
|
||||
await mock_app.on_websocket_data_received(mock_client, msg)
|
||||
except Exception as e:
|
||||
# We expect some exceptions for malformed data if the handler isn't fully robust,
|
||||
# but we want to know what they are.
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_websocket_api_fuzzing(mock_app):
|
||||
"""Fuzz the websocket API with various message types and payloads."""
|
||||
mock_client = AsyncMock()
|
||||
# Use MagicMock instead of AsyncMock to avoid coroutine warnings
|
||||
mock_client = MagicMock()
|
||||
mock_client.send_str = MagicMock(side_effect=lambda data: None)
|
||||
|
||||
# Test cases with different message types and malformed/unexpected data
|
||||
fuzz_messages = [
|
||||
@@ -293,11 +297,9 @@ async def test_websocket_api_fuzzing(mock_app):
|
||||
|
||||
for msg in fuzz_messages:
|
||||
try:
|
||||
# We use await here because on_websocket_data_received is async
|
||||
await mock_app.on_websocket_data_received(mock_client, msg)
|
||||
except Exception as e:
|
||||
# We want to see if it crashes the whole app
|
||||
pytest.fail(f"Websocket API crashed with message {msg}: {e}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_fuzzing(mock_app):
|
||||
@@ -312,18 +314,17 @@ async def test_config_fuzzing(mock_app):
|
||||
|
||||
for config in fuzz_configs:
|
||||
try:
|
||||
# Mock update_config if it exists, or just let it run if it's safe
|
||||
if hasattr(mock_app, "update_config"):
|
||||
await mock_app.update_config(config)
|
||||
except Exception as e:
|
||||
pytest.fail(f"Config update crashed with config {config}: {e}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_malformed_announce_data(mock_app):
|
||||
"""Test handling of malformed or unexpected data in announces."""
|
||||
aspect = "lxmf.delivery"
|
||||
destination_hash = b"too_short" # Malformed hash
|
||||
|
||||
# Test with None identity - should be caught by my fix
|
||||
# Test with None identity
|
||||
mock_app.on_lxmf_announce_received(
|
||||
aspect,
|
||||
destination_hash,
|
||||
@@ -332,7 +333,7 @@ def test_malformed_announce_data(mock_app):
|
||||
b""
|
||||
)
|
||||
|
||||
# Test with identity having None hash - should be caught by my fix
|
||||
# Test with identity having None hash
|
||||
announced_identity = MagicMock()
|
||||
announced_identity.hash = None
|
||||
mock_app.on_lxmf_announce_received(
|
||||
@@ -351,7 +352,6 @@ def test_malformed_message_data(mock_app):
|
||||
|
||||
# This should be caught by the try-except in on_lxmf_delivery
|
||||
mock_app.on_lxmf_delivery(mock_message)
|
||||
# The call should return gracefully due to internal try-except
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
@@ -374,7 +374,6 @@ def test_database_dao_fuzzing(mock_app, weird_string, large_binary):
|
||||
try:
|
||||
mock_app.database.announces.upsert_announce(announce_data)
|
||||
except Exception:
|
||||
# Mock database might fail, but it shouldn't crash the test runner
|
||||
pass
|
||||
|
||||
# Test MessageDAO
|
||||
@@ -408,3 +407,79 @@ def test_lxmf_field_fuzzing(audio_bytes, image_bytes):
|
||||
LxmfFileAttachment(file_name="test.txt", file_bytes=audio_bytes)
|
||||
except Exception as e:
|
||||
pytest.fail(f"LXMF field classes crashed: {e}")
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
command_bytes=st.binary(min_size=1, max_size=100)
|
||||
)
|
||||
def test_sideband_command_fuzzing(mock_app, command_bytes):
|
||||
"""Fuzz the sideband command parsing in LXMF delivery."""
|
||||
mock_message = MagicMock()
|
||||
mock_message.source_hash = os.urandom(16)
|
||||
mock_message.hash = os.urandom(16)
|
||||
# 0x01 is SidebandCommands.TELEMETRY_REQUEST
|
||||
mock_message.get_fields.return_value = {LXMF.FIELD_COMMANDS: [command_bytes]}
|
||||
|
||||
try:
|
||||
mock_app.on_lxmf_delivery(mock_message)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
destination_hash=st.text(min_size=0, max_size=100),
|
||||
page_path=st.text(min_size=0, max_size=500),
|
||||
content=st.text(min_size=0, max_size=10000)
|
||||
)
|
||||
def test_archiver_manager_fuzzing(mock_app, destination_hash, page_path, content):
|
||||
"""Fuzz the archiver manager's page archiving logic."""
|
||||
try:
|
||||
mock_app.archive_page(destination_hash, page_path, content, is_manual=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
state=st.integers(min_value=-10, max_value=30)
|
||||
)
|
||||
def test_lxmf_state_conversion_fuzzing(mock_app, state):
|
||||
"""Fuzz LXMF state string conversion."""
|
||||
mock_message = MagicMock()
|
||||
mock_message.state = state
|
||||
try:
|
||||
ReticulumMeshChat.convert_lxmf_state_to_string(mock_message)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture], deadline=None)
|
||||
@given(
|
||||
method=st.integers(min_value=-10, max_value=30)
|
||||
)
|
||||
def test_lxmf_method_conversion_fuzzing(mock_app, method):
|
||||
"""Fuzz LXMF method string conversion."""
|
||||
mock_message = MagicMock()
|
||||
mock_message.method = method
|
||||
try:
|
||||
ReticulumMeshChat.convert_lxmf_method_to_string(mock_message)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def test_telephone_announce_fuzzing(mock_app):
|
||||
"""Fuzz telephone announce reception."""
|
||||
aspect = "telephone.call"
|
||||
destination_hash = os.urandom(16)
|
||||
announced_identity = MagicMock()
|
||||
announced_identity.hash = os.urandom(32)
|
||||
app_data = b"test_app_data"
|
||||
announce_packet_hash = os.urandom(16)
|
||||
|
||||
try:
|
||||
mock_app.on_telephone_announce_received(
|
||||
aspect,
|
||||
destination_hash,
|
||||
announced_identity,
|
||||
app_data,
|
||||
announce_packet_hash
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user