Skip to content

Commit 42238c5

Browse files
committed
Improve documentation and add comments to test_memio buffer utilities
1 parent 5efdc6b commit 42238c5

1 file changed

Lines changed: 77 additions & 7 deletions

File tree

tests/utils.c

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
* dump memory client.bin test_ctx.c_buff test_ctx.c_buff+test_ctx.c_len
3232
* dump memory server.bin test_ctx.s_buff test_ctx.s_buff+test_ctx.s_len
3333
* This can be imported into Wireshark by transforming the file with
34-
* od -Ax -tx1 -v client.bin > client.bin.hex
35-
* od -Ax -tx1 -v server.bin > server.bin.hex
36-
* And then loading test_output.dump.hex into Wireshark using the
37-
* "Import from Hex Dump..." option ion and selecting the TCP
38-
* encapsulation option.
34+
* od -Ax -tx1 -v client.bin > client.hex
35+
* od -Ax -tx1 -v server.bin > server.hex
36+
* Then transform the files into pcap (use -u instead of -T for UDP)
37+
* text2pcap -T 50,60 client.hex client.pcap
38+
* text2pcap -T 50,60 server.hex server.pcap
39+
* Then open in wireshark
40+
* wireshark client.pcap
41+
* wireshark server.pcap
3942
*/
4043

4144
int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz, void *ctx)
@@ -372,6 +375,7 @@ void test_memio_clear_buffer(struct test_memio_ctx *ctx, int is_client)
372375
}
373376
}
374377

378+
/* Inject a message into the buffer for client or server */
375379
int test_memio_inject_message(struct test_memio_ctx* ctx, int client,
376380
const char* data, int sz)
377381
{
@@ -380,6 +384,7 @@ int test_memio_inject_message(struct test_memio_ctx* ctx, int client,
380384
int* msg_sizes;
381385
byte* buff;
382386

387+
/* Select buffer and metadata for client or server */
383388
if (client) {
384389
buff = ctx->c_buff;
385390
len = &ctx->c_len;
@@ -392,34 +397,50 @@ int test_memio_inject_message(struct test_memio_ctx* ctx, int client,
392397
msg_count = &ctx->s_msg_count;
393398
msg_sizes = ctx->s_msg_sizes;
394399
}
400+
401+
/* Check if buffer has enough space for new message */
395402
if (*len + sz > TEST_MEMIO_BUF_SZ) {
396403
return -1;
397404
}
405+
/* Check if message count does not exceed maximum allowed */
398406
if (*msg_count >= TEST_MEMIO_MAX_MSGS) {
399407
return -1;
400408
}
409+
410+
/* Copy message data into buffer */
401411
XMEMCPY(buff + *len, data, (size_t)sz);
412+
413+
/* Record message size and increment message count */
402414
msg_sizes[*msg_count] = sz;
403415
(*msg_count)++;
404416
*len += sz;
417+
405418
return 0;
406419
}
407420

421+
/* Copy a message from the buffer to an output buffer */
408422
int test_memio_copy_message(const struct test_memio_ctx *ctx, int client,
409423
char *out, int *out_sz, int msg_pos)
410424
{
411425
const char* buff = NULL;
412426
int buff_sz = 0;
413427

428+
/* Retrieve message pointer and size for given position */
414429
if (test_memio_get_message(ctx, client, &buff, &buff_sz, msg_pos) != 0)
415430
return -1;
431+
432+
/* Ensure output buffer is large enough */
416433
if (*out_sz < buff_sz)
417434
return -1;
435+
436+
/* Copy message to output buffer */
418437
XMEMCPY(out, buff, (size_t)buff_sz);
419438
*out_sz = buff_sz;
439+
420440
return 0;
421441
}
422442

443+
/* Get a pointer and size to a message in the buffer */
423444
int test_memio_get_message(const struct test_memio_ctx *ctx, int client,
424445
const char **out, int *out_sz, int msg_pos)
425446
{
@@ -428,6 +449,7 @@ int test_memio_get_message(const struct test_memio_ctx *ctx, int client,
428449
int i;
429450
const byte* buff;
430451

452+
/* Select buffer and message metadata for client or server */
431453
if (client) {
432454
buff = ctx->c_buff;
433455
msg_count = ctx->c_msg_count;
@@ -438,14 +460,21 @@ int test_memio_get_message(const struct test_memio_ctx *ctx, int client,
438460
msg_count = ctx->s_msg_count;
439461
msg_sizes = ctx->s_msg_sizes;
440462
}
463+
464+
/* Validate message position */
441465
if (msg_pos < 0 || msg_pos >= msg_count) {
442466
return -1;
443467
}
468+
469+
/* Find start of the message in the buffer */
444470
for (i = 0; i < msg_pos; i++) {
445471
buff += msg_sizes[i];
446472
}
473+
474+
/* Set output pointers to message data and size */
447475
*out = (const char*)buff;
448476
*out_sz = msg_sizes[msg_pos];
477+
449478
return 0;
450479
}
451480

@@ -533,6 +562,7 @@ int test_memio_move_message(struct test_memio_ctx *ctx, int client,
533562
return 0;
534563
}
535564

565+
/* Drop (remove) a message from the buffer and update metadata */
536566
int test_memio_drop_message(struct test_memio_ctx *ctx, int client, int msg_pos)
537567
{
538568
int *len;
@@ -541,6 +571,8 @@ int test_memio_drop_message(struct test_memio_ctx *ctx, int client, int msg_pos)
541571
int msg_off, msg_sz;
542572
int i;
543573
byte *buff;
574+
575+
/* Select buffer and metadata for client or server */
544576
if (client) {
545577
buff = ctx->c_buff;
546578
len = &ctx->c_len;
@@ -552,26 +584,40 @@ int test_memio_drop_message(struct test_memio_ctx *ctx, int client, int msg_pos)
552584
msg_count = &ctx->s_msg_count;
553585
msg_sizes = ctx->s_msg_sizes;
554586
}
587+
588+
/* Check for empty message list */
555589
if (*msg_count == 0) {
556590
return -1;
557591
}
592+
558593
msg_off = 0;
594+
/* Validate message position */
559595
if (msg_pos >= *msg_count) {
560596
return -1;
561597
}
598+
599+
/* Find offset and size of message to drop */
562600
msg_sz = msg_sizes[msg_pos];
563601
for (i = 0; i < msg_pos; i++) {
564602
msg_off += msg_sizes[i];
565603
}
604+
605+
/* Remove message from buffer by shifting remaining data */
566606
XMEMMOVE(buff + msg_off, buff + msg_off + msg_sz, *len - msg_off - msg_sz);
607+
608+
/* Update message sizes array */
567609
for (i = msg_pos; i < *msg_count - 1; i++) {
568610
msg_sizes[i] = msg_sizes[i + 1];
569611
}
612+
613+
/* Update buffer length and message count */
570614
*len -= msg_sz;
571615
(*msg_count)--;
616+
572617
return 0;
573618
}
574619

620+
/* Remove a region from the buffer, possibly dropping or shrinking a message */
575621
int test_memio_remove_from_buffer(struct test_memio_ctx* ctx, int client,
576622
int off, int sz)
577623
{
@@ -582,6 +628,7 @@ int test_memio_remove_from_buffer(struct test_memio_ctx* ctx, int client,
582628
int i;
583629
byte* buff;
584630

631+
/* Select buffer and metadata for client or server */
585632
if (client) {
586633
buff = ctx->c_buff;
587634
len = &ctx->c_len;
@@ -594,6 +641,8 @@ int test_memio_remove_from_buffer(struct test_memio_ctx* ctx, int client,
594641
msg_count = &ctx->s_msg_count;
595642
msg_sizes = ctx->s_msg_sizes;
596643
}
644+
645+
/* Validate buffer and offset */
597646
if (*len == 0) {
598647
return -1;
599648
}
@@ -603,30 +652,38 @@ int test_memio_remove_from_buffer(struct test_memio_ctx* ctx, int client,
603652
if (off + sz > *len) {
604653
return -1;
605654
}
606-
/* find which message the offset is in */
655+
656+
/* Find which message the offset is in */
607657
msg_off = 0;
608658
for (i = 0; i < *msg_count; i++) {
609659
if (off >= msg_off && off < msg_off + msg_sizes[i]) {
610660
break;
611661
}
612662
msg_off += msg_sizes[i];
613663
}
614-
/* don't support records that are split across messages */
664+
665+
/* Don't support records split across messages */
615666
if (off + sz > msg_off + msg_sizes[i]) {
616667
return -1;
617668
}
618669
if (i == *msg_count) {
619670
return -1;
620671
}
672+
673+
/* If removing entire message, drop it */
621674
if (sz == msg_sizes[i]) {
622675
return test_memio_drop_message(ctx, client, i);
623676
}
677+
678+
/* Remove part of message by shifting buffer and updating size */
624679
XMEMMOVE(buff + off, buff + off + sz, *len - off - sz);
625680
msg_sizes[i] -= sz;
626681
*len -= sz;
682+
627683
return 0;
628684
}
629685

686+
/* Modify the length of a message in the buffer, shifting data as needed */
630687
int test_memio_modify_message_len(struct test_memio_ctx* ctx, int client,
631688
int msg_pos, int new_len)
632689
{
@@ -636,6 +693,8 @@ int test_memio_modify_message_len(struct test_memio_ctx* ctx, int client,
636693
int msg_off, msg_sz;
637694
int i;
638695
byte* buff;
696+
697+
/* Select buffer and metadata for client or server */
639698
if (client) {
640699
buff = ctx->c_buff;
641700
len = &ctx->c_len;
@@ -648,26 +707,37 @@ int test_memio_modify_message_len(struct test_memio_ctx* ctx, int client,
648707
msg_count = &ctx->s_msg_count;
649708
msg_sizes = ctx->s_msg_sizes;
650709
}
710+
711+
/* Validate message count and position */
651712
if (*msg_count == 0) {
652713
return -1;
653714
}
654715
if (msg_pos >= *msg_count) {
655716
return -1;
656717
}
718+
719+
/* Find offset and size of message to modify */
657720
msg_off = 0;
658721
for (i = 0; i < msg_pos; i++) {
659722
msg_off += msg_sizes[i];
660723
}
661724
msg_sz = msg_sizes[msg_pos];
725+
726+
/* Check if buffer has enough space for length increase */
662727
if (new_len > msg_sz) {
663728
if (*len + (new_len - msg_sz) > TEST_MEMIO_BUF_SZ) {
664729
return -1;
665730
}
666731
}
732+
733+
/* Shift buffer contents to accommodate new message length */
667734
XMEMMOVE(buff + msg_off + new_len, buff + msg_off + msg_sz,
668735
*len - msg_off - msg_sz);
736+
737+
/* Update message size and buffer length */
669738
msg_sizes[msg_pos] = new_len;
670739
*len = *len - msg_sz + new_len;
740+
671741
return 0;
672742
}
673743

0 commit comments

Comments
 (0)