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 18, 2017
1 parent 871b0f5 commit dffe126
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 18 additions & 10 deletions pyof/v0x01/controller2switch/packet_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,21 @@ 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.
This method will verify the port number and raise a exception if:
1 - The port number is less than 0 and more than Port.OFPP_MAX
2 - The port number is a virtual port number is not equal to
Port.OFPP_LOCAL, Port.OFPP_CONTROLLER, Port.OFPP_NONE
Raises:
ValidationError: If in_port is an 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 dffe126

Please sign in to comment.