From 9adbed4be62ae67c76ea846f5981a571866784dd Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Thu, 30 Dec 2021 22:52:49 -0800 Subject: [PATCH] add unit tests for onTunnelReceive() --- meshtastic/tests/test_tunnel.py | 50 ++++++++++++++++++++++++++++++--- meshtastic/tunnel.py | 6 ++-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/meshtastic/tests/test_tunnel.py b/meshtastic/tests/test_tunnel.py index 867e8fc..05b53e9 100644 --- a/meshtastic/tests/test_tunnel.py +++ b/meshtastic/tests/test_tunnel.py @@ -1,13 +1,14 @@ """Meshtastic unit tests for tunnel.py""" import re +import sys import logging from unittest.mock import patch, MagicMock import pytest from ..tcp_interface import TCPInterface -from ..tunnel import Tunnel +from ..tunnel import Tunnel, onTunnelReceive from ..globals import Globals @@ -21,8 +22,7 @@ def test_Tunnel_on_non_linux_system(mock_platform_system, reset_globals): with patch('socket.socket') as mock_socket: with pytest.raises(Exception) as pytest_wrapped_e: iface = TCPInterface(hostname='localhost', noProto=True) - tun = Tunnel(iface) - assert tun == Globals.getInstance().get_tunnelInstance() + Tunnel(iface) assert pytest_wrapped_e.type == Exception assert mock_socket.called @@ -50,8 +50,50 @@ def test_Tunnel_with_interface(mock_platform_system, caplog, reset_globals, ifac mock_platform_system.side_effect = a_mock with caplog.at_level(logging.WARNING): with patch('socket.socket'): - Tunnel(iface) + tun = Tunnel(iface) + assert tun == Globals.getInstance().get_tunnelInstance() iface.close() assert re.search(r'Not creating a TapDevice()', caplog.text, re.MULTILINE) assert re.search(r'Not starting TUN reader', caplog.text, re.MULTILINE) assert re.search(r'Not sending packet', caplog.text, re.MULTILINE) + + +@pytest.mark.unit +@patch('platform.system') +def test_onTunnelReceive_from_ourselves(mock_platform_system, caplog, reset_globals, iface_with_nodes): + """Test onTunnelReceive""" + iface = iface_with_nodes + iface.myInfo.my_node_num = 2475227164 + sys.argv = [''] + Globals.getInstance().set_args(sys.argv) + packet = {'decoded': { 'payload': 'foo'}, 'from': 2475227164} + a_mock = MagicMock() + a_mock.return_value = 'Linux' + mock_platform_system.side_effect = a_mock + with caplog.at_level(logging.DEBUG): + with patch('socket.socket'): + tun = Tunnel(iface) + Globals.getInstance().set_tunnelInstance(tun) + onTunnelReceive(packet, iface) + assert re.search(r'in onTunnelReceive', caplog.text, re.MULTILINE) + assert re.search(r'Ignoring message we sent', caplog.text, re.MULTILINE) + + +@pytest.mark.unit +@patch('platform.system') +def test_onTunnelReceive_from_someone_else(mock_platform_system, caplog, reset_globals, iface_with_nodes): + """Test onTunnelReceive""" + iface = iface_with_nodes + iface.myInfo.my_node_num = 2475227164 + sys.argv = [''] + Globals.getInstance().set_args(sys.argv) + packet = {'decoded': { 'payload': 'foo'}, 'from': 123} + a_mock = MagicMock() + a_mock.return_value = 'Linux' + mock_platform_system.side_effect = a_mock + with caplog.at_level(logging.DEBUG): + with patch('socket.socket'): + tun = Tunnel(iface) + Globals.getInstance().set_tunnelInstance(tun) + onTunnelReceive(packet, iface) + assert re.search(r'in onTunnelReceive', caplog.text, re.MULTILINE) diff --git a/meshtastic/tunnel.py b/meshtastic/tunnel.py index d732d63..d78c2db 100644 --- a/meshtastic/tunnel.py +++ b/meshtastic/tunnel.py @@ -29,6 +29,7 @@ from .globals import Globals def onTunnelReceive(packet, interface): """Callback for received tunneled messages from mesh.""" + logging.debug(f'in onTunnelReceive()') our_globals = Globals.getInstance() tunnelInstance = our_globals.get_tunnelInstance() tunnelInstance.onReceive(packet) @@ -116,8 +117,9 @@ class Tunnel: logging.debug(f"Received mesh tunnel message type={type(p)} len={len(p)}") # we don't really need to check for filtering here (sender should have checked), # but this provides useful debug printing on types of packets received - if not self._shouldFilterPacket(p): - self.tun.write(p) + if not self.iface.noProto: # could move this one line down later + if not self._shouldFilterPacket(p): + self.tun.write(p) def _shouldFilterPacket(self, p): """Given a packet, decode it and return true if it should be ignored"""