Skip to content

Commit

Permalink
Fix PacketOut validation
Browse files Browse the repository at this point in the history
- Update packetout validation
- Fix tests
- Fix kytos#402
  • Loading branch information
macartur committed Sep 13, 2017
1 parent dd0164b commit aed24a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions pyof/v0x01/controller2switch/packet_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ def _update_actions_len(self):
self.actions_len = ListOfActions(self.actions).get_size()

def _validate_in_port(self):
port = self.in_port
valid = True
if isinstance(port, Port):
if port not in _VIRT_IN_PORTS:
valid = False
elif isinstance(port, int) and (port < 1 or port >=
Port.OFPP_MAX.value):
valid = False
if not valid:
raise ValidationError('{} is not a valid input port.'.format(port))
"""Validate in_port attribute.
Raises:
ValidationError: If in_port is a invalid port.
"""
is_valid_range = self.in_port > 0 and self.in_port <= Port.OFPP_MAX
is_valid_virtual_in_ports = self.in_port in _VIRT_IN_PORTS

if (is_valid_range or is_valid_virtual_in_ports) is False:
raise ValidationError(f'{self.in_port} is not a valid input port.')
4 changes: 2 additions & 2 deletions tests/v0x01/test_controller2switch/test_packet_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def test_invalid_virtual_in_ports(self):

def test_valid_physical_in_ports(self):
"""Physical port limits from 1.0.0 spec."""
max_valid = int(Port.OFPP_MAX.value) - 1
max_valid = int(Port.OFPP_MAX.value)
for in_port in (1, max_valid):
self.message.in_port = in_port
self.assertTrue(self.message.is_valid())

def test_invalid_physical_in_port(self):
"""Physical port limits from 1.0.0 spec."""
max_valid = int(Port.OFPP_MAX.value) - 1
max_valid = int(Port.OFPP_MAX.value)
for in_port in (-1, 0, max_valid + 1, max_valid + 2):
self.message.in_port = in_port
self.assertFalse(self.message.is_valid())
Expand Down

0 comments on commit aed24a3

Please sign in to comment.