tests.unit.v0x01.test_controller2switch.test_packet_out   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 47
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A TestPacketOut.test_valid_physical_in_ports() 0 6 2
A TestPacketOut.setUpClass() 0 9 1
A TestPacketOut.setUp() 0 3 1
A TestPacketOut.test_valid_virtual_in_ports() 0 6 2
A TestPacketOut.test_invalid_virtual_in_ports() 0 8 2
A TestPacketOut.test_invalid_physical_in_port() 0 7 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_actions() 0 4 1
A _get_data() 0 5 1
1
"""Packet out message tests."""
2
from pyof.foundation.exceptions import ValidationError
3
from pyof.v0x01.common.action import ActionOutput
4
from pyof.v0x01.common.phy_port import Port
5
from pyof.v0x01.controller2switch.packet_out import PacketOut
6
from tests.unit.test_struct import TestStruct
7
8
9
class TestPacketOut(TestStruct):
10
    """Packet out message tests (also those in :class:`.TestDump`).
11
12
    Attributes:
13
        message (PacketOut): The message configured in :meth:`setUpClass`.
14
    """
15
16
    @classmethod
17
    def setUpClass(cls):
18
        """Configure raw file and its object in parent class (TestDump)."""
19
        super().setUpClass()
20
        super().set_raw_dump_file('v0x01', 'ofpt_packet_out')
21
        super().set_raw_dump_object(PacketOut, xid=8, buffer_id=4294967295,
22
                                    in_port=Port.OFPP_NONE, data=_get_data(),
23
                                    actions=_get_actions())
24
        super().set_minimum_size(16)
25
26
    def setUp(self):
27
        """Run before every test."""
28
        self.message = self.get_raw_object()
29
30
    def test_valid_virtual_in_ports(self):
31
        """Valid virtual ports as defined in 1.0.1 spec."""
32
        valid = (Port.OFPP_LOCAL, Port.OFPP_CONTROLLER, Port.OFPP_NONE)
33
        for in_port in valid:
34
            self.message.in_port = in_port
35
            self.assertTrue(self.message.is_valid())
36
37
    def test_invalid_virtual_in_ports(self):
38
        """Invalid virtual ports as defined in 1.0.1 spec."""
39
        invalid = (Port.OFPP_IN_PORT, Port.OFPP_TABLE, Port.OFPP_NORMAL,
40
                   Port.OFPP_FLOOD, Port.OFPP_ALL)
41
        for in_port in invalid:
42
            self.message.in_port = in_port
43
            self.assertFalse(self.message.is_valid())
44
            self.assertRaises(ValidationError, self.message.validate)
45
46
    def test_valid_physical_in_ports(self):
47
        """Physical port limits from 1.0.0 spec."""
48
        max_valid = int(Port.OFPP_MAX.value)
49
        for in_port in (1, max_valid):
50
            self.message.in_port = in_port
51
            self.assertTrue(self.message.is_valid())
52
53
    def test_invalid_physical_in_port(self):
54
        """Physical port limits from 1.0.0 spec."""
55
        max_valid = int(Port.OFPP_MAX.value)
56
        for in_port in (-1, 0, max_valid + 1, max_valid + 2):
57
            self.message.in_port = in_port
58
            self.assertFalse(self.message.is_valid())
59
            self.assertRaises(ValidationError, self.message.validate)
60
61
62
def _get_actions():
63
    """Function used to return a list of actions used by packetout instance."""
64
    action = ActionOutput(port=1, max_length=0)
65
    return [action]
66
67
68
def _get_data():
69
    """Function used to return a BinaryData used by packetout instance."""
70
    data = b'\x01# \x00\x00\x01\xd2A\xc6.*@\x88\xcc\x02\x07\x07dpi'
71
    data += b'd:1\x04\x02\x021\x06\x02\x00x\x0c\x06dpid:1\x00\x00'
72
    return data
73