From b19e727ab52d4cd1c54f725c7d30e0c3db669c36 Mon Sep 17 00:00:00 2001 From: Valery Ivanov Date: Sun, 16 Apr 2023 13:11:44 +0300 Subject: tests: fetch header/packet without advanced mode Sometimes it's necessary to obtain a header or packet for unit testing. This possible only in advanced mode. The advanced mode requires sudo. This limitation is not necessary for unit testing. Signed-off-by: Valery Ivanov --- configure.ac | 3 +++ src/libnet_advanced.c | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/configure.ac b/configure.ac index f1c592e..4f54428 100644 --- a/configure.ac +++ b/configure.ac @@ -185,6 +185,9 @@ AC_ARG_ENABLE([tests], ) AC_MSG_RESULT([$enable_tests]) AM_CONDITIONAL([ENABLE_TESTS], [test "$enable_tests" = "yes"]) +AS_IF([test "$enable_tests" = "yes"], [ + AC_DEFINE(LIBNET_ENABLE_TESTS, 1, [Useful define for testing purposes.]) +]) # what (not) to do if the user disables shared libraries AM_CONDITIONAL([COND_SHARED], [test "x$enable_shared" != xno]) diff --git a/src/libnet_advanced.c b/src/libnet_advanced.c index fb22ec6..5a3a610 100644 --- a/src/libnet_advanced.c +++ b/src/libnet_advanced.c @@ -38,12 +38,18 @@ libnet_adv_cull_packet(libnet_t *l, uint8_t **packet, uint32_t *packet_s) *packet = NULL; *packet_s = 0; +#ifdef LIBNET_ENABLE_TESTS + /* + * Allow to fetch the packet without advanced mode. Useful for unit tests. + */ +#else if (l->injection_type != LIBNET_LINK_ADV) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): advanced link mode not enabled", __func__); return (-1); } +#endif /* checksums will be written in */ return (libnet_pblock_coalesce(l, packet, packet_s)); @@ -58,12 +64,18 @@ libnet_adv_cull_header(libnet_t *l, libnet_ptag_t ptag, uint8_t **header, *header = NULL; *header_s = 0; +#ifdef LIBNET_ENABLE_TESTS + /* + * Allow to fetch the packet's header without advanced mode. Useful for unit tests. + */ +#else if (l->injection_type != LIBNET_LINK_ADV) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): advanced link mode not enabled", __func__); return (-1); } +#endif p = libnet_pblock_find(l, ptag); if (p == NULL) -- cgit v1.2.1