-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_fragmenter.py
487 lines (452 loc) · 18.5 KB
/
test_fragmenter.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Copyright (C) 2023 Freie Universität Berlin
#
# SPDX-License-Identifier: GPL-3.0-only
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
import asyncio
import queue
import threading
import typing
import pytest
from scapy.all import ( # pylint: disable=no-name-in-module
IPv6,
UDP,
)
from scapy.contrib.coap import CoAP
import pylibschc.compressor
import pylibschc.fragmenter
import pylibschc.rules
__author__ = "Martine S. Lenders"
__copyright__ = "Copyright 2023 Freie Universität Berlin"
__license__ = "GPLv3"
MTU = 60
DUTY_CYCLE_MS = 150
REPEATS = 2
TEST_PARAMS = [
(
pylibschc.fragmenter.FragmentationMode.NO_ACK,
bytes,
b"foobar",
False,
pylibschc.fragmenter.FragmentationResult.NO_FRAGMENTATION,
),
(
pylibschc.fragmenter.FragmentationMode.NO_ACK,
bytes,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.NO_ACK,
pylibschc.fragmenter.BitArray,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.NO_ACK,
bytes,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy "
b"eirmod tempor invidunt ut labore et dolore magna aliquyam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.NO_ACK,
bytes,
bytes(
IPv6(hlim=64, src="2001:db8:1::2", dst="2001:db8::1")
/ UDP(
sport=8001,
dport=8000,
)
/ CoAP(
ver=1,
code="GET",
type="NON",
msg_id=0x23B0,
token=b"\x12\x34\x56\x78",
options=[("Uri-Path", b"temp")],
paymark=b"\xff",
)
/ b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam"
),
True,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ALWAYS,
bytes,
b"foobar",
False,
pylibschc.fragmenter.FragmentationResult.NO_FRAGMENTATION,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ALWAYS,
bytes,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ALWAYS,
pylibschc.fragmenter.BitArray,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ALWAYS,
bytes,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy "
b"eirmod tempor invidunt ut labore et dolore magna aliquyam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ALWAYS,
bytes,
bytes(
IPv6(hlim=64, src="2001:db8:1::2", dst="2001:db8::1")
/ UDP(
sport=8001,
dport=8000,
)
/ CoAP(
ver=1,
code="GET",
type="NON",
msg_id=0x23B0,
token=b"\x12\x34\x56\x78",
options=[("Uri-Path", b"temp")],
paymark=b"\xff",
)
/ b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam"
),
True,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ON_ERROR,
bytes,
b"foobar",
False,
pylibschc.fragmenter.FragmentationResult.NO_FRAGMENTATION,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ON_ERROR,
bytes,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ON_ERROR,
bytes,
b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy "
b"eirmod tempor invidunt ut labore et dolore magna aliquyam",
False,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
(
pylibschc.fragmenter.FragmentationMode.ACK_ON_ERROR,
bytes,
bytes(
IPv6(hlim=64, src="2001:db8:1::2", dst="2001:db8::1")
/ UDP(
sport=8001,
dport=8000,
)
/ CoAP(
ver=1,
code="GET",
type="NON",
msg_id=0x23B0,
token=b"\x12\x34\x56\x78",
options=[("Uri-Path", b"temp")],
paymark=b"\xff",
)
/ b"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam"
),
True,
pylibschc.fragmenter.FragmentationResult.SUCCESS,
),
]
class TestFragmenterReassemblerThreaded: # pylint: disable=too-many-instance-attributes
# pylint: disable=attribute-defined-outside-init
def setup_method(self, method): # pylint: disable=unused-argument
self.timers = {}
self.send_queue = queue.Queue()
self.end_tx_called = False
self.reassembler_queue = queue.Queue()
self.timer_lock = threading.Lock()
def teardown_method(self, method): # pylint: disable=unused-argument
for timer in list(self.timers.values()):
# wait for threads to finish to free all resources
if timer.is_alive(): # pragma: no cover
timer.cancel()
timer.join()
# cleanup removed but still running Timer threads
for timer in [t for t in threading.enumerate() if t.name.startswith("Timer-")]:
if timer.is_alive(): # pragma: no cover
timer.cancel()
timer.join()
def send_frag(self, buffer: bytes) -> int:
assert len(buffer) <= MTU
self.send_queue.put_nowait({"cmd": "frag", "data": buffer})
return len(buffer)
def send_ack(self, buffer: bytes) -> int:
assert len(buffer) <= MTU
self.send_queue.put_nowait({"cmd": "ack", "data": buffer})
return len(buffer)
def post_timer_task(
self,
conn: pylibschc.fragmenter.FragmentationConnection,
timer_task: typing.Callable[[object], None],
delay_sec: float,
arg: object,
) -> None:
def _timer_task(the_arg):
with self.timer_lock:
return timer_task(the_arg)
if conn in self.timers:
self.timers[conn].cancel()
self.timers[conn] = threading.Timer(delay_sec, _timer_task, args=(arg,))
self.timers[conn].name = f"Timer-{conn}"
self.timers[conn].start()
def end_rx(self, conn: pylibschc.fragmenter.FragmentationConnection):
self.reassembler_queue.put_nowait(conn.mbuf)
def end_tx(self, conn: pylibschc.fragmenter.FragmentationConnection):
assert self.fragmenter.device.device_id == conn.device_id
self.end_tx_called = True
def remove_timer_entry(self, conn: pylibschc.fragmenter.FragmentationConnection):
if conn in self.timers:
self.timers[conn].cancel()
del self.timers[conn]
def reassemble(self):
try:
while True:
cmd = self.send_queue.get(timeout=5 * (DUTY_CYCLE_MS / 1000))
buffer = cmd["data"]
assert cmd["cmd"] in ["frag", "ack"]
is_fragmented = False
for rule in self.fragmenter.device.fragmentation_rules:
assert rule.rule_id_size_bits == 8
if rule.rule_id == buffer[0]:
is_fragmented = True
if is_fragmented:
if cmd["cmd"] == "ack":
was_awaiting_ack = self.fragmenter.is_awaiting_ack()
with self.timer_lock:
# is an ACK, handle at fragmenter
res = self.fragmenter.input(self.input_type(buffer))
if was_awaiting_ack: # pragma: no cover
assert res == (
pylibschc.fragmenter.ReassemblyStatus.ACK_HANDLED
)
else:
with self.timer_lock:
# otherwise handle at reassembler
res = self.reassembler.input(self.input_type(buffer))
assert res in (
pylibschc.fragmenter.ReassemblyStatus.STAY_ALIVE,
pylibschc.fragmenter.ReassemblyStatus.COMPLETED,
pylibschc.fragmenter.ReassemblyStatus.ONGOING,
)
else:
assert cmd["cmd"] == "frag"
with self.timer_lock:
# otherwise handle at reassembler
assert (
self.reassembler.input(self.input_type(buffer))
== pylibschc.fragmenter.ReassemblyStatus.COMPLETED
)
except queue.Empty:
assert self.end_tx_called
@pytest.mark.parametrize(
"mode, input_type, data, compress_data, exp_result", TEST_PARAMS
)
def test_fragmenter_reassembler_threaded( # pylint: disable=too-many-arguments
self, test_rules, mode, input_type, data, compress_data, exp_result, subtests
):
config = test_rules.deploy()
device_f = config.devices[0]
device_r = config.devices[1]
assert device_f.mtu == device_r.mtu == MTU
assert device_f.duty_cycle_ms == device_r.duty_cycle_ms == DUTY_CYCLE_MS
self.input_type = input_type
c_r = pylibschc.compressor.CompressorDecompressor(device_f)
self.fragmenter = pylibschc.fragmenter.FragmenterReassembler(
device=device_f,
mode=mode,
post_timer_task=self.post_timer_task,
end_tx=self.end_tx,
remove_timer_entry=self.remove_timer_entry,
)
self.reassembler = pylibschc.fragmenter.FragmenterReassembler(
device=device_r,
post_timer_task=self.post_timer_task,
end_rx=self.end_rx,
remove_timer_entry=self.remove_timer_entry,
)
assert self.fragmenter.tx_state == pylibschc.fragmenter.TXState.INIT_TX
assert self.fragmenter.rx_state == pylibschc.fragmenter.RXState.RECV_WINDOW
assert self.reassembler.tx_state == pylibschc.fragmenter.TXState.INIT_TX
assert self.reassembler.rx_state == pylibschc.fragmenter.RXState.RECV_WINDOW
self.fragmenter.register_send(self.send_frag)
self.reassembler.register_send(self.send_ack)
for i in range(REPEATS): # check for idempotency
with subtests.test("loop", i=i):
with self.timer_lock:
if compress_data:
res, pkt = c_r.output(
self.input_type(data), pylibschc.rules.Direction.DOWN
)
assert res == pylibschc.compressor.CompressionResult.COMPRESSED
assert self.fragmenter.output(pkt) == exp_result
else:
assert (
self.fragmenter.output(self.input_type(data)) == exp_result
)
self.reassemble()
pkt = self.reassembler_queue.get(timeout=(DUTY_CYCLE_MS / 1000) * 10)
if compress_data:
assert c_r.input(pkt, pylibschc.rules.Direction.DOWN) == data
else:
assert pkt == data
self.fragmenter.unregister_send()
class TestFragmenterReassemblerAsync: # pylint: disable=too-many-instance-attributes
# pylint: disable=attribute-defined-outside-init
def setup_method(self, method): # pylint: disable=unused-argument
self.timer_tasks = {}
self.send_queue = asyncio.Queue()
def teardown_method(self, method): # pylint: disable=unused-argument
for timer_task in list(self.timer_tasks.values()):
timer_task.cancel() # pragma: no cover
def send_frag(self, buffer: bytes) -> int:
assert len(buffer) <= MTU
self.send_queue.put_nowait({"cmd": "frag", "data": buffer})
return len(buffer)
def send_ack(self, buffer: bytes) -> int:
assert len(buffer) <= MTU
self.send_queue.put_nowait({"cmd": "ack", "data": buffer})
return len(buffer)
def post_timer_task(
self,
conn: pylibschc.fragmenter.FragmentationConnection,
timer_task: typing.Callable[[object], None],
delay_sec: float,
arg: object,
) -> None:
def _timer_task(the_arg):
return timer_task(the_arg)
if conn in self.timer_tasks:
self.timer_tasks[conn].cancel()
self.timer_tasks[conn] = self.loop.call_later(delay_sec, _timer_task, arg)
def end_rx(self, conn: pylibschc.fragmenter.FragmentationConnection):
assert self.reassembler.device.device_id == conn.device_id
self.reassembly_buffer.set_result(conn.mbuf)
def end_tx(self, conn: pylibschc.fragmenter.FragmentationConnection):
assert self.fragmenter.device.device_id == conn.device_id
self.send_queue.put_nowait({"cmd": "end_tx"})
def remove_timer_entry(self, conn: pylibschc.fragmenter.FragmentationConnection):
if conn in self.timer_tasks:
self.timer_tasks[conn].cancel()
self.timer_tasks.pop(conn)
async def reassemble(self):
async def inp(handler, buffer):
return handler.input(buffer)
while True:
cmd = await asyncio.wait_for(
self.send_queue.get(), timeout=5 * (DUTY_CYCLE_MS / 1000)
)
if cmd["cmd"] == "end_tx":
break
assert cmd["cmd"] in ["frag", "ack"]
buffer = cmd["data"]
is_fragmented = False
for rule in self.fragmenter.device.fragmentation_rules:
assert rule.rule_id_size_bits == 8
if rule.rule_id == buffer[0]:
is_fragmented = True
if is_fragmented:
if cmd["cmd"] == "ack":
was_awaiting_ack = self.fragmenter.is_awaiting_ack()
# is an ACK, handle at fragmenter
res = await inp(self.fragmenter, self.input_type(buffer))
if was_awaiting_ack: # pragma: no cover
assert res == pylibschc.fragmenter.ReassemblyStatus.ACK_HANDLED
else:
# otherwise handle at reassembler
res = await inp(self.reassembler, self.input_type(buffer))
assert res in (
pylibschc.fragmenter.ReassemblyStatus.STAY_ALIVE,
pylibschc.fragmenter.ReassemblyStatus.COMPLETED,
pylibschc.fragmenter.ReassemblyStatus.ONGOING,
)
else:
# otherwise handle at reassembler
res = await inp(self.reassembler, self.input_type(buffer))
assert res == pylibschc.fragmenter.ReassemblyStatus.COMPLETED
@pytest.mark.asyncio
@pytest.mark.parametrize(
"mode, input_type, data, compress_data, exp_result", TEST_PARAMS
)
async def test_fragmenter_reassembler_async( # pylint: disable=too-many-arguments
self, test_rules, mode, input_type, data, compress_data, exp_result, subtests
):
# pylint: disable=too-many-locals
async def output(buffer):
return self.fragmenter.output(buffer)
self.loop = asyncio.get_running_loop()
config = test_rules.deploy()
device_f = config.devices[0]
device_r = config.devices[1]
assert device_f.mtu == device_r.mtu == MTU
assert device_f.duty_cycle_ms == device_r.duty_cycle_ms == DUTY_CYCLE_MS
self.input_type = input_type
c_r = pylibschc.compressor.CompressorDecompressor(device_f)
self.fragmenter = pylibschc.fragmenter.FragmenterReassembler(
device=device_f,
mode=mode,
post_timer_task=self.post_timer_task,
end_tx=self.end_tx,
remove_timer_entry=self.remove_timer_entry,
)
self.reassembler = pylibschc.fragmenter.FragmenterReassembler(
device=device_r,
post_timer_task=self.post_timer_task,
end_rx=self.end_rx,
remove_timer_entry=self.remove_timer_entry,
)
assert self.fragmenter.tx_state == pylibschc.fragmenter.TXState.INIT_TX
assert self.fragmenter.rx_state == pylibschc.fragmenter.RXState.RECV_WINDOW
assert self.reassembler.tx_state == pylibschc.fragmenter.TXState.INIT_TX
assert self.reassembler.rx_state == pylibschc.fragmenter.RXState.RECV_WINDOW
self.fragmenter.register_send(self.send_frag)
self.reassembler.register_send(self.send_ack)
for i in range(REPEATS): # check for idempotency
with subtests.test("loop", i=i):
self.reassembly_buffer = self.loop.create_future()
if compress_data:
res, pkt = c_r.output(
self.input_type(data), direction=pylibschc.rules.Direction.DOWN
)
assert res == pylibschc.compressor.CompressionResult.COMPRESSED
assert await output(pkt) == exp_result
else:
assert await output(self.input_type(data)) == exp_result
await self.reassemble()
pkt = await asyncio.wait_for(
self.reassembly_buffer, timeout=(DUTY_CYCLE_MS / 1000) * 10
)
if compress_data:
assert (
c_r.input(pkt, direction=pylibschc.rules.Direction.DOWN) == data
)
else:
assert pkt == data
self.fragmenter.unregister_send()