Passed
Pull Request — master (#577)
by Gleyberson
02:14
created

_get_data()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 0
1
"""Packet out message tests."""
2
from pyof.foundation.exceptions import ValidationError
3
from pyof.v0x04.common.action import ActionOutput
4
from pyof.v0x04.common.constants import OFP_NO_BUFFER
5
from pyof.v0x04.common.port import PortNo
6
from pyof.v0x04.controller2switch.packet_out import PacketOut
7
from tests.test_struct import TestStruct
8
9
NO_RAW = 'No raw dump file found.'
10
11
12
class TestPacketOut(TestStruct):
13
    """Packet out message tests (also those in :class:`.TestDump`).
14
15
    Attributes:
16
        message (PacketOut): The message configured in :meth:`setUpClass`.
17
18
    """
19
20
    @classmethod
21
    def setUpClass(cls):
22
        """Configure raw file and its object in parent class (TestDump)."""
23
        super().setUpClass()
24
        super().set_raw_dump_file('v0x04', 'ofpt_packet_out')
25
        super().set_raw_dump_object(PacketOut, xid=2544740805,
26
                                    buffer_id=OFP_NO_BUFFER,
27
                                    in_port=PortNo.OFPP_CONTROLLER,
28
                                    actions=_get_actions(), data=_get_data())
29
        super().set_minimum_size(24)
30
31
    def test_valid_virtual_in_ports(self):
32
        """Valid virtual ports as defined in 1.3.0 spec."""
33
        virtual_ports = (PortNo.OFPP_LOCAL, PortNo.OFPP_CONTROLLER,
34
                         PortNo.OFPP_ANY)
35
        for port in virtual_ports:
36
            with self.subTest(port=port):
37
                msg = PacketOut(in_port=port)
38
                self.assertTrue(msg.is_valid(),
39
                                f'{port.name} should be a valid in_port')
40
41
    def test_invalid_virtual_in_ports(self):
42
        """Invalid virtual ports as defined in 1.3.0 spec."""
43
        try:
44
            msg = self.get_raw_dump().read()
45
        except FileNotFoundError:
46
            raise self.skipTest(NO_RAW)
47
        else:
48
            invalid = (PortNo.OFPP_IN_PORT, PortNo.OFPP_TABLE,
49
                       PortNo.OFPP_NORMAL, PortNo.OFPP_FLOOD, PortNo.OFPP_ALL)
50
            msg = self.get_raw_object()
51
            for in_port in invalid:
52
                msg.in_port = in_port
53
                self.assertFalse(msg.is_valid())
54
                self.assertRaises(ValidationError, msg.validate)
55
56
    def test_valid_physical_in_ports(self):
57
        """Physical port limits from 1.3.0 spec."""
58
        try:
59
            msg = self.get_raw_dump().read()
60
        except FileNotFoundError:
61
            raise self.skipTest(NO_RAW)
62
        else:
63
            max_valid = int(PortNo.OFPP_MAX.value) - 1
64
            msg = self.get_raw_object()
65
            for in_port in (1, max_valid):
66
                msg.in_port = in_port
67
                self.assertTrue(msg.is_valid())
68
69
70
def _get_actions():
71
    """Return a list of actions used by packetout instance."""
72
    action = ActionOutput(port=1)
73
    return [action]
74
75
76
def _get_data():
77
    """Return a BinaryData used by packetout instance."""
78
    data = b'\x01\x80\xc2\x00\x00\x0e\x4e\xbf\xca\x27\x8e\xca\x81\x00\x0e'
79
    data += b'\xd7\x88\xcc\x02\x09\x07\x00\x00\x00\x00\x00\x00\x00\x01\x04'
80
    data += b'\x05\x07\x00\x00\x00\x01\x06\x02\x00\x78\x00\x00'
81
    return data
82