Skip to content

Commit

Permalink
Add unit test for reliable_get_num_output_sequenced_available
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
  • Loading branch information
schwabe committed May 7, 2022
1 parent d90d420 commit 6013396
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tests/unit_tests/openvpn/Makefile.am
Expand Up @@ -61,7 +61,10 @@ packet_id_testdriver_SOURCES = test_packet_id.c mock_msg.c mock_msg.h \
$(openvpn_srcdir)/buffer.c \
$(openvpn_srcdir)/otime.c \
$(openvpn_srcdir)/packet_id.c \
$(openvpn_srcdir)/platform.c
$(openvpn_srcdir)/platform.c \
$(openvpn_srcdir)/reliable.c \
$(openvpn_srcdir)/session_id.c


pkt_testdriver_CFLAGS = @TEST_CFLAGS@ \
-I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_tests/openvpn/mock_get_random.c
Expand Up @@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>

unsigned long
Expand All @@ -34,3 +35,12 @@ get_random(void)
/* rand() is not very random, but it's C99 and this is just for testing */
return rand();
}

void
prng_bytes(uint8_t *output, int len)
{
for (int i = 0; i < len; i++)
{
output[i] = rand();
}
}
55 changes: 55 additions & 0 deletions tests/unit_tests/openvpn/test_packet_id.c
Expand Up @@ -36,6 +36,7 @@
#include <cmocka.h>

#include "packet_id.h"
#include "reliable.h"

#include "mock_msg.h"

Expand Down Expand Up @@ -156,6 +157,59 @@ test_packet_id_write_long_wrap(void **state)
assert_true(data->test_buf_data.buf_time == htonl(now));
}

static void
test_get_num_output_sequenced_available(void **state)
{

struct reliable *rel = malloc(sizeof(struct reliable));
reliable_init(rel, 100, 50, 8, false);

rel->array[5].active = true;
rel->array[5].packet_id = 100;

rel->packet_id = 103;

assert_int_equal(5, reliable_get_num_output_sequenced_available(rel));

rel->array[6].active = true;
rel->array[6].packet_id = 97;
assert_int_equal(2, reliable_get_num_output_sequenced_available(rel));

/* test ids close to int/usigned int barrier */

rel->array[5].active = true;
rel->array[5].packet_id = (0x80000000u -3);
rel->array[6].active = false;
rel->packet_id = (0x80000000u -1);

assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));

rel->array[5].active = true;
rel->array[5].packet_id = (0x80000000u -3);
rel->packet_id = 0x80000001u;

assert_int_equal(4, reliable_get_num_output_sequenced_available(rel));


/* test wrapping */
rel->array[5].active = true;
rel->array[5].packet_id = (0xffffffffu -3);
rel->array[6].active = false;
rel->packet_id = (0xffffffffu - 1);

assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));

rel->array[2].packet_id = 0;
rel->array[2].active = true;

assert_int_equal(6, reliable_get_num_output_sequenced_available(rel));

rel->packet_id = 3;
assert_int_equal(1, reliable_get_num_output_sequenced_available(rel));

reliable_free(rel);
}

int
main(void)
{
Expand All @@ -178,6 +232,7 @@ main(void)
cmocka_unit_test_setup_teardown(test_packet_id_write_long_wrap,
test_packet_id_write_setup,
test_packet_id_write_teardown),
cmocka_unit_test(test_get_num_output_sequenced_available)
};

return cmocka_run_group_tests_name("packet_id tests", tests, NULL, NULL);
Expand Down

0 comments on commit 6013396

Please sign in to comment.