diff options
author | Valery Ivanov <ivalery111@gmail.com> | 2022-02-24 20:07:17 +0300 |
---|---|---|
committer | Valery Ivanov <ivalery111@gmail.com> | 2022-03-12 15:43:21 +0300 |
commit | 477b8ce8b397d52cc33e6aceaba1a3743554f779 (patch) | |
tree | 117c68ca6ad6f992bf378575e294339488c17cf2 /tests | |
parent | b20f5932781fd08f338f06c3ba5e17cc7d6d17d5 (diff) | |
download | libnet-477b8ce8b397d52cc33e6aceaba1a3743554f779.tar.gz |
tests: add libnet_build_ethernet unit test
Signed-off-by: Valery Ivanov <ivalery111@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/unit_tests.c | 106 |
2 files changed, 100 insertions, 8 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index fbd5972..a78c578 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,4 +4,4 @@ libnet_unit_tests_SOURCES = unit_tests.c libnet_unit_tests_LDFLAGS = -lcmocka -LDADD = $(top_builddir)/src/libnet.la
\ No newline at end of file +LDADD = $(top_builddir)/src/libnet.la diff --git a/tests/unit_tests.c b/tests/unit_tests.c index 45ad926..c225b37 100644 --- a/tests/unit_tests.c +++ b/tests/unit_tests.c @@ -1,21 +1,113 @@ +// clang-format off +#include <stddef.h> #include <stdio.h> #include <stdbool.h> +#include <stdlib.h> +#include <stdint.h> #include <setjmp.h> #include <cmocka.h> -/* A test case that does nothing and succeeds. */ -static void bool_always_success(void **state) { - (void) state; /* unused */ +#include <libnet.h> +// clang-format on - bool this_is_true = true; +/****************************************************************************** + * + * LOCAL HELPERS + * + *****************************************************************************/ - assert_true(this_is_true); +static void +print_err_buf(const char *err_buf) +{ + fprintf(stdout, "[ERROR]: %s\n", err_buf); + exit(EXIT_FAILURE); } -int main(void) { +/****************************************************************************** + * + * END OF LOCAL HELPERS + * + *****************************************************************************/ + +static void +test_libnet_build_ethernet(void **state) +{ + (void)state; /* unused */ + + libnet_ptag_t eth_ptag = (-1); + struct libnet_ethernet_hdr *eth_hdr = NULL; + char errbuf[LIBNET_ERRBUF_SIZE]; + uint8_t *header = NULL; + uint32_t header_size = 0; + uint8_t mac_dst[ETHER_ADDR_LEN] = { 0x11, 0x11, 0x11, 0x22, 0x22, 0x22 }; + uint8_t mac_src[ETHER_ADDR_LEN] = { 0x44, 0x44, 0x44, 0x55, 0x55, 0x55 }; + int rv = (-1); + + libnet_t *l = libnet_init(LIBNET_LINK_ADV, /* enable advanced mode */ + NULL, /* interface */ + errbuf); /* error buffer */ + + if (NULL == l) + { + print_err_buf(errbuf); + } + assert_non_null(l); + + eth_ptag = libnet_build_ethernet(mac_dst, /* destination ethernet address */ + mac_src, /* source ethernet address */ + ETHERTYPE_IP, /* upper layer protocol type */ + NULL, /* payload */ + 0, /* payload length */ + l, /* libnet context */ + 0); /* protocol tag */ + if ((-1) == eth_ptag) + { + print_err_buf(errbuf); + } + assert_int_not_equal(eth_ptag, (-1)); + + rv = libnet_adv_cull_header(l, /* libnet context */ + eth_ptag, /* protocol tag */ + &header, /* header */ + &header_size); /* header size */ + if ((-1) == rv) + { + print_err_buf(errbuf); + } + assert_int_not_equal(eth_ptag, (-1)); + assert_int_not_equal(header_size, 0); + + eth_hdr = (struct libnet_ethernet_hdr *)header; + assert_int_equal(eth_hdr->ether_type, htons(ETHERTYPE_IP)); + + // Compare source macs + for (uint8_t i = 0; i < ETHER_ADDR_LEN; i++) + { + assert_int_equal(eth_hdr->ether_shost[i], mac_src[i]); + } + + // Compare destination macs + for (uint8_t i = 0; i < ETHER_ADDR_LEN; i++) + { + assert_int_equal(eth_hdr->ether_dhost[i], mac_dst[i]); + } + + libnet_destroy(l); +} + +int +main(void) +{ const struct CMUnitTest tests[] = { - cmocka_unit_test(bool_always_success), + cmocka_unit_test(test_libnet_build_ethernet), }; return cmocka_run_group_tests(tests, NULL, NULL); } + +/** + * Local Variables: + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + */ |