"""Tests for InboundMessageDistributor.""" from i2p_router.inbound_message_distributor import ( InboundMessageDistributor, DeliveryMode, I2NPType, ) def test_filter_allows_garlic_on_client(): dist = InboundMessageDistributor(client_hash=b"\x01" * 32) assert dist.is_allowed_message_type(I2NPType.GARLIC) is True def test_filter_blocks_tunnel_data_on_client(): dist = InboundMessageDistributor(client_hash=b"\x01" * 32) assert dist.is_allowed_message_type(I2NPType.TUNNEL_DATA) is False def test_filter_blocks_data_msg_on_client(): dist = InboundMessageDistributor(client_hash=b"\x01" * 32) assert dist.is_allowed_message_type(I2NPType.DATA) is False def test_filter_allows_delivery_status(): dist = InboundMessageDistributor(client_hash=b"\x01" * 32) assert dist.is_allowed_message_type(I2NPType.DELIVERY_STATUS) is True def test_distribute_garlic_to_handler(): received = [] dist = InboundMessageDistributor(client_hash=b"\x01" * 32) dist.set_garlic_handler(lambda data: received.append(data)) assert dist.distribute(I2NPType.GARLIC, b"garlic_data") is True assert received == [b"garlic_data"] def test_distribute_drops_disallowed(): dist = InboundMessageDistributor(client_hash=b"\x01" * 32) assert dist.distribute(I2NPType.TUNNEL_DATA, b"data") is False def test_handle_clove_destination_delivery(): delivered = [] dist = InboundMessageDistributor(client_hash=b"\x01" * 32) dist.set_client_manager(lambda dh, data: delivered.append((dh, data))) assert dist.handle_clove( DeliveryMode.DESTINATION, b"payload", dest_hash=b"\x01" * 32, ) is True assert len(delivered) == 1 def test_handle_clove_destination_wrong_hash(): dist = InboundMessageDistributor(client_hash=b"\x01" * 32) assert dist.handle_clove( DeliveryMode.DESTINATION, b"payload", dest_hash=b"\x02" * 32, ) is False def test_handle_clove_tunnel_delivery(): dist = InboundMessageDistributor() assert dist.handle_clove( DeliveryMode.TUNNEL, b"data", router_hash=b"\x03" * 32, tunnel_id=42, ) is True