1
|
|
|
"""Packet in message tests.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
|
4
|
|
|
from pyof.v0x04.asynchronous.packet_in import PacketIn, PacketInReason |
5
|
|
|
from pyof.v0x04.common.flow_match import Match, OxmTLV |
6
|
|
|
from pyof.v0x04.common.header import Header |
7
|
|
|
from tests.test_struct import TestStruct |
8
|
|
|
|
9
|
|
|
oxmtlv = OxmTLV(oxm_class=32768, |
10
|
|
|
oxm_field=0, |
11
|
|
|
oxm_hasmask=0, |
12
|
|
|
oxm_value=b'\x00\x00\x00\x16') |
13
|
|
|
|
14
|
|
|
match = Match(match_type=1, |
15
|
|
|
oxm_match_fields=[oxmtlv]) |
16
|
|
|
|
17
|
|
|
packetin = PacketIn(xid=0, |
18
|
|
|
buffer_id=257, |
19
|
|
|
total_len=81, |
20
|
|
|
reason=1, |
21
|
|
|
table_id=0, |
22
|
|
|
cookie=18446744073709551615, |
23
|
|
|
match=match, |
24
|
|
|
data=81 * b'\x00') |
25
|
|
|
|
26
|
|
|
dump = b'\x04\n\x00{\x00\x00\x00\x00\x00\x00\x01\x01\x00Q\x01\x00\xff\xff' |
27
|
|
|
dump += b'\xff\xff\xff\xff\xff\xff\x00\x01\x00\x0c\x80\x00\x00\x04\x00\x00' |
28
|
|
|
dump += b'\x00\x16\x00\x00\x00\x00\x00\x00' |
29
|
|
|
dump += 81 * b'\x00' |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class TestPacketIn(TestCase): |
33
|
|
|
"""Test PacketIn class.""" |
34
|
|
|
|
35
|
|
|
def test_pack(self): |
36
|
|
|
"""Assert pack method returns a known dump.""" |
37
|
|
|
self.assertEqual(dump, packetin.pack()) |
38
|
|
|
|
39
|
|
|
def test_unpack(self): |
40
|
|
|
"""Assert the known dump is unpacked correctly.""" |
41
|
|
|
unpacked_header = Header() |
42
|
|
|
unpacked_header.unpack(dump[:8]) |
43
|
|
|
packetin.update_header_length() |
44
|
|
|
self.assertEqual(packetin.header, unpacked_header) |
45
|
|
|
|
46
|
|
|
unpacked_packetin = PacketIn() |
47
|
|
|
unpacked_packetin.unpack(dump[8:]) |
48
|
|
|
unpacked_packetin.header = unpacked_header |
49
|
|
|
self.assertEqual(packetin, unpacked_packetin) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class TestPacketInRaw(TestStruct): |
53
|
|
|
"""Test PacketIn using a dump file.""" |
54
|
|
|
|
55
|
|
|
@classmethod |
56
|
|
|
def setUpClass(cls): |
57
|
|
|
"""Configure raw file and its object in parent class (TestDump).""" |
58
|
|
|
super().setUpClass() |
59
|
|
|
super().set_raw_dump_file('v0x04', 'ofpt_packet_in') |
60
|
|
|
super().set_raw_dump_object(PacketIn, xid=1, buffer_id=1, total_len=1, |
61
|
|
|
reason=PacketInReason.OFPR_ACTION, |
62
|
|
|
table_id=1, cookie=1, data=b'') |
63
|
|
|
super().set_minimum_size(34) |
64
|
|
|
|