forked from wolfSSL/wolfip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.c
More file actions
15703 lines (13358 loc) · 485 KB
/
Copy pathunit.c
File metadata and controls
15703 lines (13358 loc) · 485 KB
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* unit.c
*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfIP TCP/IP stack.
*
* wolfIP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfIP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include "check.h"
#include "../../../config.h"
#undef CONFIG_IPFILTER
#define CONFIG_IPFILTER 1
#undef WOLFIP_MAX_INTERFACES
#define WOLFIP_MAX_INTERFACES 3
#undef WOLFIP_ENABLE_LOOPBACK
#define WOLFIP_ENABLE_LOOPBACK 1
#undef WOLFIP_ENABLE_FORWARDING
#ifndef WOLFIP_ENABLE_FORWARDING
#define WOLFIP_ENABLE_FORWARDING 1
#endif
#if WOLFIP_ENABLE_LOOPBACK
#define TEST_LOOPBACK_IF 0U
#define TEST_PRIMARY_IF 1U
#define TEST_SECOND_IF 2U
#else
#define TEST_LOOPBACK_IF 0U
#define TEST_PRIMARY_IF 0U
#define TEST_SECOND_IF 1U
#endif
#include <stdio.h>
#include "../../wolfip.c"
#include <stdlib.h> /* for random() */
#include "mocks/wolfssl/wolfcrypt/settings.h"
#include "mocks/wolfssl/wolfcrypt/memory.h"
#include "mocks/wolfssl/ssl.h"
/* MOCKS */
/* pseudo random number generator to mock the random number generator */
static int test_rand_override_enabled;
static uint32_t test_rand_override_value;
uint32_t wolfIP_getrandom(void)
{
unsigned int seed = 0xDAC0FFEE;
if (test_rand_override_enabled)
return test_rand_override_value;
srandom(seed);
return random();
}
static uint8_t mem[8 * 1024];
static uint32_t memsz = 8 * 1024;
static const uint8_t ifmac[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
static uint8_t last_frame_sent[LINK_MTU];
static uint32_t last_frame_sent_size = 0;
static int mock_send(struct wolfIP_ll_dev *dev, void *frame, uint32_t len)
{
(void)dev;
memcpy(last_frame_sent, frame, len);
last_frame_sent_size = len;
return 0;
}
static int mock_poll(struct wolfIP_ll_dev *dev, void *frame, uint32_t len)
{
(void)dev;
(void)frame;
(void)len;
return 0;
}
static void mock_link_init_idx(struct wolfIP *s, unsigned int idx, const uint8_t *mac_override)
{
struct wolfIP_ll_dev *ll = wolfIP_getdev_ex(s, idx);
ck_assert_ptr_nonnull(ll);
memset(ll, 0, sizeof(*ll));
snprintf((char *)ll->ifname, sizeof(ll->ifname), "mock%u", idx);
if (mac_override) {
memcpy(ll->mac, mac_override, 6);
} else {
memcpy(ll->mac, ifmac, 6);
ll->mac[5] ^= (uint8_t)(idx + 1);
}
ll->poll = mock_poll;
ll->send = mock_send;
}
/* wolfSSL IO mocks (used by tests below) */
static CallbackIORecv g_ctx_recv_cb;
static CallbackIOSend g_ctx_send_cb;
static void *g_last_read_ctx;
static void *g_last_write_ctx;
static WOLFSSL_CTX *g_last_ctx;
static int test_recv_ret;
static int test_send_ret;
static uint8_t test_recv_fill[32];
static int test_recv_fill_len;
static uint8_t test_send_capture[32];
static int test_send_capture_len;
static int test_send_last_len;
static int test_recv_step;
static int test_recv_steps_len;
static int test_recv_steps[8];
static int test_recv_step_total;
static int filter_cb_calls;
static struct wolfIP_filter_event filter_last_event;
static enum wolfIP_filter_reason filter_block_reason;
static int filter_block_calls;
static int socket_cb_calls;
static int socket_cb_last_fd;
static uint16_t socket_cb_last_events;
static int timer_cb_calls;
static uint32_t dns_lookup_ip;
static int dns_lookup_calls;
struct tcp_seg_buf {
struct wolfIP_tcp_seg seg;
uint8_t pad[TCP_HEADER_LEN];
};
int wolfSSL_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv cb)
{
g_last_ctx = ctx;
g_ctx_recv_cb = cb;
return 0;
}
int wolfSSL_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend cb)
{
g_last_ctx = ctx;
g_ctx_send_cb = cb;
return 0;
}
int wolfSSL_SetIOReadCtx(WOLFSSL *ssl, void *ctx)
{
if (ssl)
ssl->rctx = ctx;
g_last_read_ctx = ctx;
return 0;
}
int wolfSSL_SetIOWriteCtx(WOLFSSL *ssl, void *ctx)
{
if (ssl)
ssl->wctx = ctx;
g_last_write_ctx = ctx;
return 0;
}
WOLFSSL_CTX *wolfSSL_get_SSL_CTX(WOLFSSL *ssl)
{
if (!ssl)
return NULL;
return ssl->ctx;
}
static int test_wolfIP_sock_recv(struct wolfIP *s, int fd, void *buf, int sz, int flags)
{
(void)s;
(void)fd;
(void)flags;
if (test_recv_steps_len > 0) {
int step = 0;
if (test_recv_step < test_recv_steps_len)
step = test_recv_steps[test_recv_step++];
if (step == -WOLFIP_EAGAIN) {
return -WOLFIP_EAGAIN;
} else if (step > 0) {
int offset = test_recv_step_total;
int copy_len = step;
if (copy_len > sz)
copy_len = sz;
if (offset + copy_len > (int)sizeof(test_recv_fill))
copy_len = (int)sizeof(test_recv_fill) - offset;
if (copy_len > 0) {
memcpy(buf, test_recv_fill + offset, (size_t)copy_len);
test_recv_step_total += copy_len;
}
return step;
} else {
return step;
}
}
if (test_recv_fill_len > 0 && test_recv_ret > 0) {
int copy_len = test_recv_fill_len;
if (copy_len > sz)
copy_len = sz;
memcpy(buf, test_recv_fill, (size_t)copy_len);
}
return test_recv_ret;
}
static int test_wolfIP_sock_send(struct wolfIP *s, int fd, const void *buf, int sz, int flags)
{
(void)s;
(void)fd;
(void)flags;
test_send_last_len = sz;
if (test_send_capture_len > 0) {
int copy_len = test_send_capture_len;
if (copy_len > sz)
copy_len = sz;
memcpy(test_send_capture, buf, (size_t)copy_len);
}
return test_send_ret;
}
#define wolfIP_sock_recv test_wolfIP_sock_recv
#define wolfIP_sock_send test_wolfIP_sock_send
#include "../../port/wolfssl_io.c"
#undef wolfIP_sock_recv
#undef wolfIP_sock_send
static void reset_wolfssl_io_state(void)
{
memset(ctx_map, 0, sizeof(ctx_map));
memset(io_descs, 0, sizeof(io_descs));
g_ctx_recv_cb = NULL;
g_ctx_send_cb = NULL;
g_last_read_ctx = NULL;
g_last_write_ctx = NULL;
g_last_ctx = NULL;
test_recv_ret = 0;
test_send_ret = 0;
test_recv_fill_len = 0;
test_send_capture_len = 0;
test_send_last_len = 0;
test_recv_step = 0;
test_recv_steps_len = 0;
test_recv_step_total = 0;
memset(test_recv_steps, 0, sizeof(test_recv_steps));
memset(test_recv_fill, 0, sizeof(test_recv_fill));
memset(test_send_capture, 0, sizeof(test_send_capture));
}
static int test_filter_cb(void *arg, const struct wolfIP_filter_event *event)
{
(void)arg;
if (event) {
filter_last_event = *event;
filter_cb_calls++;
}
return 0;
}
static int test_filter_cb_block(void *arg, const struct wolfIP_filter_event *event)
{
(void)arg;
if (event) {
filter_block_calls++;
if (event->reason == filter_block_reason)
return 1;
}
return 0;
}
static void test_socket_cb(int sock_fd, uint16_t events, void *arg)
{
(void)arg;
socket_cb_calls++;
socket_cb_last_fd = sock_fd;
socket_cb_last_events = events;
}
static void test_timer_cb(void *arg)
{
(void)arg;
timer_cb_calls++;
}
static void test_dns_lookup_cb(uint32_t ip)
{
dns_lookup_ip = ip;
dns_lookup_calls++;
}
static void test_dns_ptr_cb(const char *name)
{
(void)name;
}
void mock_link_init(struct wolfIP *s)
{
unsigned int idx = 0;
#if WOLFIP_ENABLE_LOOPBACK
idx = 1;
#endif
mock_link_init_idx(s, idx, NULL);
}
static struct timers_binheap heap;
static void reset_heap(void) {
heap.size = 0;
}
static void setup_stack_with_two_ifaces(struct wolfIP *s, ip4 primary_ip, ip4 secondary_ip)
{
wolfIP_init(s);
mock_link_init(s);
mock_link_init_idx(s, TEST_SECOND_IF, NULL);
wolfIP_ipconfig_set(s, primary_ip, 0xFFFFFF00U, 0);
wolfIP_ipconfig_set_ex(s, TEST_SECOND_IF, secondary_ip, 0xFFFFFF00U, 0);
}
static void inject_tcp_syn(struct wolfIP *s, unsigned int if_idx, ip4 dst_ip, uint16_t dst_port)
{
struct wolfIP_tcp_seg syn;
struct wolfIP_ll_dev *ll = wolfIP_getdev_ex(s, if_idx);
union transport_pseudo_header ph;
static const uint8_t src_mac[6] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60};
ck_assert_ptr_nonnull(ll);
memset(&syn, 0, sizeof(syn));
memcpy(syn.ip.eth.dst, ll->mac, 6);
memcpy(syn.ip.eth.src, src_mac, 6);
syn.ip.eth.type = ee16(ETH_TYPE_IP);
syn.ip.ver_ihl = 0x45;
syn.ip.ttl = 64;
syn.ip.proto = WI_IPPROTO_TCP;
syn.ip.len = ee16(IP_HEADER_LEN + TCP_HEADER_LEN);
syn.ip.src = ee32(0x0A0000A1U);
syn.ip.dst = ee32(dst_ip);
syn.ip.csum = 0;
iphdr_set_checksum(&syn.ip);
syn.src_port = ee16(40000);
syn.dst_port = ee16(dst_port);
syn.seq = ee32(1);
syn.ack = 0;
syn.hlen = TCP_HEADER_LEN << 2;
syn.flags = 0x02;
syn.win = ee16(65535);
syn.csum = 0;
syn.urg = 0;
memset(&ph, 0, sizeof(ph));
ph.ph.src = syn.ip.src;
ph.ph.dst = syn.ip.dst;
ph.ph.proto = WI_IPPROTO_TCP;
ph.ph.len = ee16(TCP_HEADER_LEN);
syn.csum = ee16(transport_checksum(&ph, &syn.src_port));
tcp_input(s, if_idx, &syn, sizeof(struct wolfIP_eth_frame) + IP_HEADER_LEN + TCP_HEADER_LEN);
}
static void inject_tcp_segment(struct wolfIP *s, unsigned int if_idx, ip4 src_ip, ip4 dst_ip,
uint16_t src_port, uint16_t dst_port, uint32_t seq, uint32_t ack, uint8_t flags)
{
struct wolfIP_tcp_seg seg;
struct wolfIP_ll_dev *ll = wolfIP_getdev_ex(s, if_idx);
union transport_pseudo_header ph;
static const uint8_t src_mac[6] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
ck_assert_ptr_nonnull(ll);
memset(&seg, 0, sizeof(seg));
memcpy(seg.ip.eth.dst, ll->mac, 6);
memcpy(seg.ip.eth.src, src_mac, 6);
seg.ip.eth.type = ee16(ETH_TYPE_IP);
seg.ip.ver_ihl = 0x45;
seg.ip.ttl = 64;
seg.ip.proto = WI_IPPROTO_TCP;
seg.ip.len = ee16(IP_HEADER_LEN + TCP_HEADER_LEN);
seg.ip.src = ee32(src_ip);
seg.ip.dst = ee32(dst_ip);
seg.ip.csum = 0;
iphdr_set_checksum(&seg.ip);
seg.src_port = ee16(src_port);
seg.dst_port = ee16(dst_port);
seg.seq = ee32(seq);
seg.ack = ee32(ack);
seg.hlen = TCP_HEADER_LEN << 2;
seg.flags = flags;
seg.win = ee16(65535);
seg.csum = 0;
seg.urg = 0;
memset(&ph, 0, sizeof(ph));
ph.ph.src = seg.ip.src;
ph.ph.dst = seg.ip.dst;
ph.ph.proto = WI_IPPROTO_TCP;
ph.ph.len = ee16(TCP_HEADER_LEN);
seg.csum = ee16(transport_checksum(&ph, &seg.src_port));
tcp_input(s, if_idx, &seg, sizeof(struct wolfIP_eth_frame) + IP_HEADER_LEN + TCP_HEADER_LEN);
}
static int tcp_option_find(const struct wolfIP_tcp_seg *tcp, uint8_t kind)
{
const uint8_t *opt = tcp->data;
int opt_len = (tcp->hlen >> 2) - TCP_HEADER_LEN;
int i = 0;
while (i < opt_len) {
if (opt[i] == TCP_OPTION_EOO)
break;
if (opt[i] == TCP_OPTION_NOP) {
i++;
continue;
}
if (i + 1 >= opt_len || opt[i + 1] < 2)
break;
if (opt[i] == kind)
return i;
i += opt[i + 1];
}
return -1;
}
static void inject_udp_datagram(struct wolfIP *s, unsigned int if_idx, ip4 src_ip, ip4 dst_ip,
uint16_t src_port, uint16_t dst_port, const uint8_t *payload, uint16_t payload_len)
{
uint8_t frame[LINK_MTU];
struct wolfIP_udp_datagram *udp = (struct wolfIP_udp_datagram *)frame;
struct wolfIP_ll_dev *ll = wolfIP_getdev_ex(s, if_idx);
static const uint8_t src_mac[6] = {0x90, 0x91, 0x92, 0x93, 0x94, 0x95};
ck_assert_ptr_nonnull(ll);
memset(udp, 0, sizeof(frame));
memcpy(udp->ip.eth.dst, ll->mac, 6);
memcpy(udp->ip.eth.src, src_mac, 6);
udp->ip.eth.type = ee16(ETH_TYPE_IP);
udp->ip.ver_ihl = 0x45;
udp->ip.ttl = 64;
udp->ip.proto = WI_IPPROTO_UDP;
udp->ip.len = ee16(IP_HEADER_LEN + UDP_HEADER_LEN + payload_len);
udp->ip.src = ee32(src_ip);
udp->ip.dst = ee32(dst_ip);
udp->ip.csum = 0;
iphdr_set_checksum(&udp->ip);
udp->src_port = ee16(src_port);
udp->dst_port = ee16(dst_port);
udp->len = ee16(UDP_HEADER_LEN + payload_len);
udp->csum = 0;
if (payload_len && payload) {
memcpy(udp->data, payload, payload_len);
}
udp_try_recv(s, if_idx, udp, (uint32_t)(ETH_HEADER_LEN + IP_HEADER_LEN + UDP_HEADER_LEN + payload_len));
}
static int enqueue_tcp_tx(struct tsocket *ts, uint32_t payload_len, uint8_t flags)
{
uint8_t buf[ETH_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN + 16];
struct wolfIP_tcp_seg *tcp = (struct wolfIP_tcp_seg *)buf;
uint32_t total_len = IP_HEADER_LEN + TCP_HEADER_LEN + payload_len;
uint32_t frame_len = ETH_HEADER_LEN + total_len;
ck_assert_uint_le(payload_len, 16);
memset(tcp, 0, sizeof(buf));
tcp->ip.len = ee16((uint16_t)total_len);
tcp->hlen = TCP_HEADER_LEN << 2;
tcp->flags = flags;
tcp->seq = ee32(ts->sock.tcp.seq);
tcp->ack = ee32(ts->sock.tcp.ack);
tcp->src_port = ee16(ts->src_port);
tcp->dst_port = ee16(ts->dst_port);
if (payload_len > 0) {
uint8_t *payload = (uint8_t *)tcp->ip.data + TCP_HEADER_LEN;
memset(payload, 0xAB, payload_len);
}
return fifo_push(&ts->sock.tcp.txbuf, tcp, frame_len);
}
static int enqueue_tcp_tx_with_payload(struct tsocket *ts, const uint8_t *payload_data,
uint32_t payload_len, uint8_t flags)
{
uint8_t buf[ETH_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN + 16];
struct wolfIP_tcp_seg *tcp = (struct wolfIP_tcp_seg *)buf;
uint32_t total_len = IP_HEADER_LEN + TCP_HEADER_LEN + payload_len;
uint32_t frame_len = ETH_HEADER_LEN + total_len;
uint8_t *payload;
ck_assert_uint_le(payload_len, 16);
memset(tcp, 0, sizeof(buf));
tcp->ip.len = ee16((uint16_t)total_len);
tcp->hlen = TCP_HEADER_LEN << 2;
tcp->flags = flags;
tcp->seq = ee32(ts->sock.tcp.seq);
tcp->ack = ee32(ts->sock.tcp.ack);
tcp->src_port = ee16(ts->src_port);
tcp->dst_port = ee16(ts->dst_port);
if (payload_len > 0) {
payload = (uint8_t *)tcp->ip.data + TCP_HEADER_LEN;
memcpy(payload, payload_data, payload_len);
}
return fifo_push(&ts->sock.tcp.txbuf, tcp, frame_len);
}
static void enqueue_udp_rx(struct tsocket *ts, const void *payload, uint16_t payload_len, uint16_t src_port)
{
uint8_t buf[sizeof(struct wolfIP_udp_datagram) + 1024];
struct wolfIP_udp_datagram *udp = (struct wolfIP_udp_datagram *)buf;
uint16_t total = UDP_HEADER_LEN + payload_len;
ck_assert_uint_le(payload_len, 1024);
memset(udp, 0, sizeof(buf));
udp->src_port = ee16(src_port);
udp->dst_port = ee16(ts->src_port);
udp->len = ee16(total);
memcpy(udp->data, payload, payload_len);
(void)fifo_push(&ts->sock.udp.rxbuf, udp, sizeof(struct wolfIP_udp_datagram) + payload_len);
}
START_TEST(test_fifo_init)
{
struct fifo f;
fifo_init(&f, mem, memsz);
ck_assert_int_eq(fifo_len(&f), 0);
ck_assert_int_eq(fifo_space(&f), memsz);
ck_assert_int_eq(fifo_len(&f), 0);
}
END_TEST
START_TEST(test_fifo_peek_wraps_tail_when_head_lt_tail)
{
struct fifo f;
uint8_t data[64];
struct pkt_desc *desc;
fifo_init(&f, data, sizeof(data));
f.head = 0;
f.tail = 4;
f.h_wrap = 0;
/* With head at 0 and tail aligned, peek should return the current tail
* descriptor without altering tail or wrap state. */
desc = fifo_peek(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(f.tail, 4);
}
END_TEST
START_TEST(test_fifo_peek_no_wrap_when_space_available)
{
struct fifo f;
uint8_t data[4096];
struct pkt_desc *desc;
fifo_init(&f, data, sizeof(data));
f.head = 0;
f.tail = 4;
f.h_wrap = 0;
/* When no wrap boundary is set, peek must not change tail. */
desc = fifo_peek(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(f.tail, 4);
}
END_TEST
START_TEST(test_fifo_next_wraps_on_hwrap)
{
struct fifo f;
uint8_t data[4096];
struct pkt_desc *desc0;
struct pkt_desc *desc1;
struct pkt_desc *next;
uint32_t len;
fifo_init(&f, data, sizeof(data));
desc0 = (struct pkt_desc *)data;
desc0->pos = 0;
desc0->len = 4;
len = sizeof(struct pkt_desc) + desc0->len;
while (len % 4)
len++;
desc1 = (struct pkt_desc *)(data + len);
desc1->pos = 0;
desc1->len = 0;
f.h_wrap = len;
f.head = len + 8;
next = fifo_next(&f, desc0);
ck_assert_ptr_eq(next, (struct pkt_desc *)data);
}
END_TEST
START_TEST(test_fifo_pop_aligns_tail_to_head_returns_null)
{
struct fifo f;
uint8_t data[64];
fifo_init(&f, data, sizeof(data));
f.head = 4;
f.tail = 1;
/* Aligning tail to head means the FIFO is empty; pop should return NULL. */
ck_assert_ptr_eq(fifo_pop(&f), NULL);
}
END_TEST
START_TEST(test_fifo_pop_wraps_tail_when_head_lt_tail)
{
struct fifo f;
uint8_t data[64];
struct pkt_desc *desc;
fifo_init(&f, data, sizeof(data));
f.head = 0;
f.tail = 4;
f.h_wrap = 0;
desc = (struct pkt_desc *)(data + 4);
desc->pos = 4;
desc->len = 0;
/* Popping a zero-length packet should advance tail past the descriptor. */
ck_assert_ptr_nonnull(fifo_pop(&f));
ck_assert_uint_eq(f.tail, 4 + sizeof(struct pkt_desc));
}
END_TEST
START_TEST(test_fifo_pop_no_wrap_when_space_available)
{
struct fifo f;
uint8_t data[4096];
struct pkt_desc *desc;
uint32_t expected_tail;
fifo_init(&f, data, sizeof(data));
f.head = 0;
f.tail = 4;
f.h_wrap = 0;
desc = (struct pkt_desc *)(data + 4);
desc->pos = 4;
desc->len = 0;
expected_tail = 4 + sizeof(struct pkt_desc);
/* With no wrap, pop should advance tail to the next descriptor. */
ck_assert_ptr_nonnull(fifo_pop(&f));
ck_assert_uint_eq(f.tail, expected_tail);
}
END_TEST
START_TEST(test_fifo_peek_empty_unaligned_tail)
{
struct fifo f;
uint8_t data[64];
struct pkt_desc *desc;
fifo_init(&f, data, sizeof(data));
f.head = 3;
f.tail = 3;
f.h_wrap = 0;
desc = fifo_peek(&f);
ck_assert_ptr_eq(desc, NULL);
ck_assert_uint_eq(f.tail, 3);
ck_assert_uint_eq(f.h_wrap, 0);
}
END_TEST
START_TEST(test_fifo_len_empty_unaligned_tail)
{
struct fifo f;
uint8_t data[64];
fifo_init(&f, data, sizeof(data));
f.head = 3;
f.tail = 3;
f.h_wrap = 0;
ck_assert_uint_eq(fifo_len(&f), 0);
ck_assert_uint_eq(f.tail, 3);
}
END_TEST
START_TEST(test_fifo_pop_empty_unaligned_tail)
{
struct fifo f;
uint8_t data[64];
fifo_init(&f, data, sizeof(data));
f.head = 3;
f.tail = 3;
f.h_wrap = 0;
ck_assert_ptr_eq(fifo_pop(&f), NULL);
ck_assert_uint_eq(f.tail, 3);
}
END_TEST
START_TEST(test_fifo_push_pop_odd_sizes_drains_cleanly)
{
struct fifo f;
uint8_t data[256];
struct pkt_desc *desc;
uint8_t p1[3] = {0x01, 0x02, 0x03};
uint8_t p2[5] = {0x11, 0x12, 0x13, 0x14, 0x15};
uint8_t p3[7] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};
fifo_init(&f, data, sizeof(data));
ck_assert_int_eq(fifo_push(&f, p1, sizeof(p1)), 0);
ck_assert_int_eq(fifo_push(&f, p2, sizeof(p2)), 0);
ck_assert_int_eq(fifo_push(&f, p3, sizeof(p3)), 0);
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_mem_eq((const uint8_t *)f.data + desc->pos + sizeof(struct pkt_desc), p1, sizeof(p1));
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_mem_eq((const uint8_t *)f.data + desc->pos + sizeof(struct pkt_desc), p2, sizeof(p2));
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_mem_eq((const uint8_t *)f.data + desc->pos + sizeof(struct pkt_desc), p3, sizeof(p3));
ck_assert_uint_eq(fifo_len(&f), 0);
ck_assert_ptr_eq(fifo_peek(&f), NULL);
}
END_TEST
START_TEST(test_fifo_full_wrap_does_not_appear_empty_or_discard_packets)
{
struct fifo f;
uint8_t data[120];
uint8_t payload[8];
struct pkt_desc *desc;
int i;
memset(payload, 0xAB, sizeof(payload));
fifo_init(&f, data, sizeof(data));
/* 5 * (sizeof(pkt_desc)=16 + payload=8) == 120: fills FIFO exactly and
* forces head == tail with wrap marker set (full, not empty). */
for (i = 0; i < 5; i++) {
payload[0] = (uint8_t)i;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
}
ck_assert_uint_eq(fifo_space(&f), 0);
ck_assert_uint_eq(f.head, f.tail);
ck_assert_uint_eq(f.h_wrap, sizeof(data));
/* Full FIFO must still expose packets. */
desc = fifo_peek(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(desc->len, sizeof(payload));
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 0);
for (i = 0; i < 5; i++) {
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(desc->len, sizeof(payload));
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), (uint8_t)i);
}
ck_assert_ptr_eq(fifo_peek(&f), NULL);
ck_assert_uint_eq(fifo_len(&f), 0);
}
END_TEST
START_TEST(test_fifo_next_stops_at_aligned_head_when_head_unaligned)
{
struct fifo f;
uint8_t data[128];
uint8_t p1[3] = {0x11, 0x12, 0x13};
uint8_t p2[5] = {0x21, 0x22, 0x23, 0x24, 0x25};
struct pkt_desc *d1;
struct pkt_desc *d2;
struct pkt_desc *d3;
fifo_init(&f, data, sizeof(data));
ck_assert_int_eq(fifo_push(&f, p1, sizeof(p1)), 0);
ck_assert_int_eq(fifo_push(&f, p2, sizeof(p2)), 0);
/* fifo_push aligns only on insertion boundaries; head can stay unaligned. */
ck_assert_uint_ne(f.head % 4, 0);
d1 = fifo_peek(&f);
ck_assert_ptr_nonnull(d1);
ck_assert_uint_eq(d1->len, sizeof(p1));
ck_assert_uint_eq(*((uint8_t *)f.data + d1->pos + sizeof(*d1)), p1[0]);
d2 = fifo_next(&f, d1);
ck_assert_ptr_nonnull(d2);
ck_assert_uint_eq(d2->len, sizeof(p2));
ck_assert_uint_eq(*((uint8_t *)f.data + d2->pos + sizeof(*d2)), p2[0]);
/* Must stop at aligned insertion cursor, not scan padding as descriptors. */
d3 = fifo_next(&f, d2);
ck_assert_ptr_eq(d3, NULL);
}
END_TEST
START_TEST(test_fifo_full_wrap_next_iterates_all_entries_without_loss)
{
struct fifo f;
uint8_t data[120];
uint8_t payload[8];
struct pkt_desc *desc;
int i;
memset(payload, 0xCD, sizeof(payload));
fifo_init(&f, data, sizeof(data));
for (i = 0; i < 5; i++) {
payload[0] = (uint8_t)i;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
}
ck_assert_uint_eq(f.head, f.tail);
ck_assert_uint_eq(f.h_wrap, sizeof(data));
ck_assert_uint_eq(fifo_space(&f), 0);
desc = fifo_peek(&f);
for (i = 0; i < 5; i++) {
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(desc->len, sizeof(payload));
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), (uint8_t)i);
desc = fifo_next(&f, desc);
}
ck_assert_ptr_eq(desc, NULL);
}
END_TEST
START_TEST(test_fifo_wrap_full_pop_then_refill_keeps_order_without_drops)
{
struct fifo f;
uint8_t data[120];
uint8_t payload[8];
struct pkt_desc *desc;
int i;
memset(payload, 0xEF, sizeof(payload));
fifo_init(&f, data, sizeof(data));
for (i = 0; i < 5; i++) {
payload[0] = (uint8_t)i;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
}
ck_assert_uint_eq(f.head, f.tail);
ck_assert_uint_eq(f.h_wrap, sizeof(data));
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 0);
payload[0] = 5;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
ck_assert_uint_eq(f.head, f.tail);
ck_assert_uint_eq(f.h_wrap, sizeof(data));
for (i = 1; i <= 5; i++) {
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(desc->len, sizeof(payload));
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), (uint8_t)i);
}
ck_assert_ptr_eq(fifo_peek(&f), NULL);
ck_assert_uint_eq(fifo_len(&f), 0);
}
END_TEST
START_TEST(test_fifo_wrap_flag_transitions_push_pop_around_boundary)
{
struct fifo f;
uint8_t data[100];
uint8_t payload[8];
struct pkt_desc *desc;
int i;
fifo_init(&f, data, sizeof(data));
memset(payload, 0, sizeof(payload));
/* Fill descriptors at offsets 0,24,48,72 (head=96, no wrap yet). */
for (i = 0; i < 4; i++) {
payload[0] = (uint8_t)i;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
}
ck_assert_uint_eq(f.h_wrap, 0);
ck_assert_uint_eq(f.head, 96);
ck_assert_uint_eq(f.tail, 0);
/* Drain first two packets so tail moves past "needed". */
for (i = 0; i < 2; i++) {
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), (uint8_t)i);
}
ck_assert_uint_eq(f.tail, 48);
ck_assert_uint_eq(f.h_wrap, 0);
/* Next push must wrap head to start and set h_wrap to old head (96). */
payload[0] = 4;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
ck_assert_uint_eq(f.h_wrap, 96);
ck_assert_uint_eq(f.head, 24);
ck_assert_uint_eq(f.tail, 48);
/* Pop packet at 48: still before wrap marker, flag remains set. */
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 2);
ck_assert_uint_eq(f.h_wrap, 96);
ck_assert_uint_eq(f.tail, 72);
/* Pop packet at 72 crosses wrap marker: tail wraps and h_wrap clears. */
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 3);
ck_assert_uint_eq(f.h_wrap, 0);
ck_assert_uint_eq(f.tail, 0);
/* Wrapped packet remains readable after flag clear. */
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 4);
ck_assert_ptr_eq(fifo_peek(&f), NULL);
}
END_TEST
START_TEST(test_fifo_wrap_flag_repeated_flips_keep_data_consistent)
{
struct fifo f;
uint8_t data[100];
uint8_t payload[8];
struct pkt_desc *desc;
int i;
fifo_init(&f, data, sizeof(data));
memset(payload, 0, sizeof(payload));
for (i = 0; i < 4; i++) {
payload[0] = (uint8_t)i;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
}
for (i = 0; i < 2; i++) {
desc = fifo_pop(&f);
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), (uint8_t)i);
}
payload[0] = 4;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
ck_assert_uint_eq(f.h_wrap, 96);
desc = fifo_pop(&f); /* 2 */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 2);
desc = fifo_pop(&f); /* 3, clears wrap */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 3);
ck_assert_uint_eq(f.h_wrap, 0);
desc = fifo_pop(&f); /* 4 */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 4);
/* Build another wrap cycle with new packets 5,6,7,8. */
for (i = 5; i <= 7; i++) {
payload[0] = (uint8_t)i;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
}
desc = fifo_pop(&f); /* 5 */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 5);
payload[0] = 8;
ck_assert_int_eq(fifo_push(&f, payload, sizeof(payload)), 0);
ck_assert_uint_eq(f.h_wrap, 96);
desc = fifo_pop(&f); /* 6 */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 6);
desc = fifo_pop(&f); /* 7, crosses wrap => clear */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 7);
ck_assert_uint_eq(f.h_wrap, 0);
desc = fifo_pop(&f); /* 8 */
ck_assert_ptr_nonnull(desc);
ck_assert_uint_eq(*((uint8_t *)f.data + desc->pos + sizeof(*desc)), 8);
ck_assert_ptr_eq(fifo_peek(&f), NULL);
ck_assert_uint_eq(fifo_len(&f), 0);
}
END_TEST
START_TEST(test_fifo_wrap_flag_transitions_with_odd_payload_sizes)
{
struct fifo f;