Skip to content

Commit

Permalink
Merge pull request #70 from kytos-ng/fix/intf_early_ret
Browse files Browse the repository at this point in the history
[Fix] intf early return
  • Loading branch information
viniarck authored Nov 28, 2022
2 parents 9938813 + 4f7a9d5 commit c0bf2bb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Removed

Fixed
=====
- Added early return when trying to process a loop or consume liveness in case interfaces haven't been created yet.

Security
========
Expand Down
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ async def on_ofpt_packet_in(self, event):

interface_a = switch_a.get_interface_by_port_no(port_a.value)
interface_b = switch_b.get_interface_by_port_no(port_b.value)
if not interface_a or not interface_b:
return

await self.loop_manager.process_if_looped(interface_a, interface_b)
await self.liveness_manager.consume_hello_if_enabled(interface_a,
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,61 @@ async def test_on_ofpt_packet_in(*args):
assert controller.buffers.app.aput.call_count == 1


@patch('kytos.core.controller.Controller.get_switch_by_dpid')
@patch('napps.kytos.of_lldp.main.Main._unpack_non_empty')
@patch('napps.kytos.of_lldp.main.UBInt32')
@patch('napps.kytos.of_lldp.main.DPID')
@patch('napps.kytos.of_lldp.main.LLDP')
@patch('napps.kytos.of_lldp.main.Ethernet')
async def test_on_ofpt_packet_in_early_intf(*args):
"""Test on_ofpt_packet_in early intf return."""
(mock_ethernet, mock_lldp, mock_dpid, mock_ubint32,
mock_unpack_non_empty, mock_get_switch_by_dpid) = args

# pylint: disable=bad-option-value, import-outside-toplevel
from napps.kytos.of_lldp.main import Main
Main.get_liveness_controller = MagicMock()
topology = get_topology_mock()
controller = get_controller_mock()
controller.buffers.app.aput = AsyncMock()
controller.switches = topology.switches
napp = Main(controller)
napp.loop_manager.process_if_looped = AsyncMock()
napp.liveness_manager.consume_hello_if_enabled = AsyncMock()

switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
message = MagicMock(in_port=1, data='data')
event = KytosEvent('ofpt_packet_in', content={'source': switch.connection,
'message': message})

mocked, ethernet, lldp, dpid, port_b = [MagicMock() for _ in range(5)]
mocked.value = 1
mock_ubint32.return_value = mocked
ethernet.ether_type = 0x88CC
ethernet.data = 'eth_data'
lldp.chassis_id.sub_value = 'chassis_id'
lldp.port_id.sub_value = 'port_id'
dpid.value = "00:00:00:00:00:00:00:02"
port_b.value = 2

mock_unpack_non_empty.side_effect = [ethernet, lldp, dpid, port_b]
mock_get_switch_by_dpid.return_value = get_switch_mock(dpid.value,
0x04)
switch.get_interface_by_port_no = MagicMock(return_value=None)
await napp.on_ofpt_packet_in(event)

calls = [call(mock_ethernet, message.data),
call(mock_lldp, ethernet.data),
call(mock_dpid, lldp.chassis_id.sub_value),
call(mock_ubint32, lldp.port_id.sub_value)]
mock_unpack_non_empty.assert_has_calls(calls)
switch.get_interface_by_port_no.assert_called()
# early return shouldn't allow these to get called
assert napp.loop_manager.process_if_looped.call_count == 0
assert napp.liveness_manager.consume_hello_if_enabled.call_count == 0
assert controller.buffers.app.aput.call_count == 0


# pylint: disable=protected-access,too-many-public-methods
class TestMain(TestCase):
"""Tests for the Main class."""
Expand Down

0 comments on commit c0bf2bb

Please sign in to comment.