-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_test.py
293 lines (246 loc) · 9.99 KB
/
packet_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from socket import AF_INET
from unittest import TestCase, main
from unittest.mock import Mock, patch
import routerbase
from packet import (
ResponsePacket,
construct_packets,
read_packet,
validate_packet,
)
from routeentry import RouteEntry
from routingtable import RoutingTable
def get_single_packet():
packet = bytearray(24)
packet[0] = 2
packet[1] = 2
packet[2:4] = (0).to_bytes(2, "big")
packet[4:6] = AF_INET.to_bytes(2, "big")
packet[8:12] = (1).to_bytes(4, "big")
packet[20:24] = (1).to_bytes(4, "big")
return packet
def get_two_packets():
packet1 = bytearray(504)
packet1[0] = 2
packet1[1] = 2
packet1[2:4] = (0).to_bytes(2, "big")
diff = 1
for i in range(25):
# if i + diff == 2:
# diff += 1
packet1[i * 20 + 4 : i * 20 + 6] = AF_INET.to_bytes(2, "big")
packet1[i * 20 + 8 : i * 20 + 12] = (i + diff).to_bytes(4, "big")
packet1[i * 20 + 20 : i * 20 + 24] = (1).to_bytes(4, "big")
packet2 = bytearray(144)
packet2[0] = 2
packet2[1] = 2
packet2[2:4] = (0).to_bytes(2, "big")
for i in range(7):
packet2[i * 20 + 4 : i * 20 + 6] = AF_INET.to_bytes(2, "big")
packet2[i * 20 + 8 : i * 20 + 12] = (i + 26).to_bytes(4, "big")
packet2[i * 20 + 20 : i * 20 + 24] = (1).to_bytes(4, "big")
return packet1, packet2
class TestPacketConstruction(TestCase):
def setUp(self):
routerbase.DEBUG_MODE = True
def _test_single_packet(self, packet, expected_packet, iteration=0):
for i, val in enumerate(expected_packet):
self.assertEqual(
packet[i],
val,
f"Iteration {iteration}. Failed on byte {i}. Expected: {val}. "
f"Received: {packet[i]}",
)
def test_single_entry(self):
"""
Tests a routing table where the single entry does not match the
given router_id.
"""
table = RoutingTable(0, 0, 0, 0)
# metric: 1, next_hop: 2
table.add_route(1, RouteEntry(0, 1, 0, 2))
packets = construct_packets(table, 3)
expected_packet = get_single_packet()
self.assertEqual(len(packets), 1)
packet = packets[0]
self.assertEqual(len(packet), len(expected_packet))
self._test_single_packet(packet, expected_packet)
def test_two_entries_router_id_clash(self):
"""
The routing table has two entries, where one entry was learnt from the
router that the packet is going to be sent to. The packet being
produced should contain two entries, however the entry with the id
clash should have a metric of 16.
"""
table_router_id = 1
table = RoutingTable(table_router_id, 0, 0, 0)
table.add_route(1, RouteEntry(0, 1, 0, 0))
table.add_route(2, RouteEntry(0, 2, 0, 3))
packets = construct_packets(table, 3)
expected_packet = bytearray(44)
expected_packet[0] = 2
expected_packet[1] = 2
expected_packet[2:4] = (1).to_bytes(2, "big")
expected_packet[4:6] = AF_INET.to_bytes(2, "big")
expected_packet[8:12] = (1).to_bytes(4, "big")
expected_packet[20:24] = (1).to_bytes(4, "big")
expected_packet[20 + 4 : 20 + 6] = AF_INET.to_bytes(2, "big")
expected_packet[20 + 8 : 20 + 12] = (2).to_bytes(4, "big")
expected_packet[20 + 20 : 20 + 24] = (16).to_bytes(4, "big")
self.assertEqual(len(packets), 1)
packet = packets[0]
self.assertEqual(len(packet), len(expected_packet))
self._test_single_packet(packet, expected_packet)
def test_two_entries_infinity(self):
"""
Tests that a routing table with two entries, where one entry has a
metric of infinity, [OLD: but is flagged], produces a packet with
the two entries inside.
"""
table = RoutingTable(0, 0, 0, 0)
table.add_route(1, RouteEntry(0, 1, 0, 0))
table.add_route(2, RouteEntry(0, 16, 0, 0))
# table[2].flag = True
packets = construct_packets(table, 1)
expected_packet = bytearray(44)
expected_packet[0] = 2
expected_packet[1] = 2
expected_packet[2:4] = (0).to_bytes(2, "big")
expected_packet[4:6] = AF_INET.to_bytes(2, "big")
expected_packet[8:12] = (1).to_bytes(4, "big")
expected_packet[20:24] = (1).to_bytes(4, "big")
expected_packet[20 + 4 : 20 + 6] = AF_INET.to_bytes(2, "big")
expected_packet[20 + 8 : 20 + 12] = (2).to_bytes(4, "big")
expected_packet[20 + 20 : 20 + 24] = (16).to_bytes(4, "big")
self.assertEqual(len(packets), 1)
packet = packets[0]
self.assertEqual(len(packet), len(expected_packet))
self._test_single_packet(packet, expected_packet)
# def test_two_entries_flag(self):
# """
# Tests that a routing table with two entries, where one entry has a
# metric of infinity, and is not flagged, produces a packet with the
# entry which isn't infinity inside.
# """
# table = RoutingTable(0, 0, 0, 0)
# table.add_route(1, RouteEntry(0, 1, 0, 0))
# table.add_route(2, RouteEntry(0, 16, 0, 0))
# table[2].flag = False
# packets = construct_packets(table, 3)
# expected_packet = bytearray(24)
# expected_packet[0] = 2
# expected_packet[1] = 2
# expected_packet[2:4] = (0).to_bytes(2, "big")
# expected_packet[4:6] = AF_INET.to_bytes(2, "big")
# expected_packet[8:12] = (1).to_bytes(4, "big")
# expected_packet[20:24] = (1).to_bytes(4, "big")
# self.assertEqual(len(packets), 1)
# packet = packets[0]
# self.assertEqual(len(packet), len(expected_packet))
# self._test_single_packet(packet, expected_packet)
def test_multiple_packets(self):
"""
Tests that multiple packets are returned, when the number of routes is
greater than 25.
"""
# Number of entries inside the table: 32
# Number of entries being sent out: 31
table = RoutingTable(0, 0, 0, 0)
table.add_route(1, RouteEntry(0, 1, 0, 0))
table.add_route(2, RouteEntry(0, 1, 0, 0))
# table.add_route(2, RouteEntry(0, 16, 3, 0, 0))
# table[2].flag = False
for i in range(30):
table.add_route(i + 3, RouteEntry(0, 1, 0, 0))
packets = construct_packets(table, 3)
expected_packets = get_two_packets()
self.assertEqual(len(packets), 2)
for i, packet in enumerate(packets):
expected_packet = expected_packets[i]
self.assertEqual(len(packet), len(expected_packet))
self._test_single_packet(packet, expected_packet, i)
class TestPacketReading(TestCase):
def setUp(self):
routerbase.DEBUG_MODE = True
def test_single_entry(self):
"""Tests that a packet with a single entry can be read correctly."""
packet = get_single_packet()
command, version, sender_router_id, entries = read_packet(packet)
self.assertEqual(command, 2)
self.assertEqual(version, 2)
self.assertEqual(sender_router_id, 0)
self.assertEqual(len(entries), 1)
for afi, router_id, metric in entries:
self.assertEqual(afi, AF_INET)
self.assertEqual(router_id, 1)
self.assertEqual(metric, 1)
def test_multiple_entries(self):
"""
Tests that a packet with multiple entries (in this case 25
entries) can be correctly read.
"""
packet, _ = get_two_packets()
command, version, sender_router_id, entries = read_packet(packet)
self.assertEqual(command, 2)
self.assertEqual(version, 2)
self.assertEqual(sender_router_id, 0)
self.assertEqual(len(entries), 25)
for i, (afi, router_id, metric) in enumerate(entries):
expected_router_id = i + 1
self.assertEqual(afi, AF_INET)
self.assertEqual(router_id, expected_router_id)
self.assertEqual(metric, 1)
class TestValidatePacket(TestCase):
table: RoutingTable
def setUp(self):
self.table = RoutingTable(
router_id=1, update_delta=10, timeout_delta=60, gc_delta=40
)
self.table.add_route(
router_id=2,
route=RouteEntry(port=4000, metric=2, timeout_time=60, next_hop=3),
)
def assertOutputEqual(self, expected: str, logger: Mock):
actual = ""
for item in logger.call_args:
if isinstance(item, tuple):
for i in item:
actual += i
self.assertEqual(expected, actual)
@patch("packet.logger")
def test_valid_packet(self, logger):
packet = ResponsePacket(
command=2, version=2, sender_router_id=2, entries=[]
)
self.assertEqual(validate_packet(self.table, packet), True)
@patch("packet.logger")
def test_sender_is_self(self, logger):
packet = ResponsePacket(
command=2, version=2, sender_router_id=1, entries=[]
)
self.assertEqual(validate_packet(self.table, packet), False)
self.assertOutputEqual(
"The packet's router_id of 1 illegally matches the router_id of "
"this router.",
logger,
)
@patch("packet.logger")
def test_invalid_command(self, logger):
packet = ResponsePacket(
command=1, version=2, sender_router_id=2, entries=[]
)
self.assertEqual(validate_packet(self.table, packet), False)
self.assertOutputEqual(
"The packet has a command value of 1, instead of 2.", logger
)
@patch("packet.logger")
def test_invalid_version(self, logger):
packet = ResponsePacket(
command=2, version=1, sender_router_id=2, entries=[]
)
self.assertEqual(validate_packet(self.table, packet), False)
self.assertOutputEqual(
"The packet has a version value of 1, instead of 2.", logger
)
if __name__ == "__main__":
main()