Skip to content

Commit

Permalink
wip: test mdns
Browse files Browse the repository at this point in the history
  • Loading branch information
mereacre committed Dec 17, 2021
1 parent 073edb1 commit 4f5d386
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"streambuf": "cpp",
"variant": "cpp",
"mdns_decoder.h": "c",
"stdbool.h": "c"
"stdbool.h": "c",
"mdns_mapper.h": "c"
}
}
4 changes: 3 additions & 1 deletion src/capture/pcap_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ int capture_pcap_packet(struct pcap_context *ctx)
void close_pcap(struct pcap_context *ctx)
{
if (ctx != NULL) {
pcap_close(ctx->pd);
if (ctx->pd != NULL) {
pcap_close(ctx->pd);
}
os_free(ctx);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/dns/mdns_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ int close_mdns(struct mdns_context *context)
free_reflection_list(context->rif6);
context->rif6 = NULL;
}

free_mdns_mapper(&context->imap);
context->imap = NULL;

Expand Down
28 changes: 28 additions & 0 deletions tests/dns/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ target_link_libraries(test_mdns_list PRIVATE mdns_list log cmocka::cmocka)
add_executable(test_mdns_mapper test_mdns_mapper.c)
target_link_libraries(test_mdns_mapper PRIVATE mdns_mapper log cmocka::cmocka)

add_executable(test_command_mapper test_command_mapper.c)
target_link_libraries(test_command_mapper PRIVATE command_mapper log cmocka::cmocka)

add_executable(test_reflection_list test_reflection_list.c)
target_link_libraries(test_reflection_list PRIVATE reflection_list log cmocka::cmocka)

add_executable(test_mdns_service test_mdns_service.c)
target_link_libraries(test_mdns_service PRIVATE mdns_service log cmocka::cmocka)
set_target_properties(test_mdns_service
PROPERTIES
LINK_FLAGS "-Wl,--wrap=run_pcap -Wl,--wrap=eloop_register_read_sock -Wl,--wrap=eloop_init -Wl,--wrap=eloop_destroy -Wl,--wrap=eloop_run"
)

add_test(NAME test_mdns_list COMMAND test_mdns_list)
set_tests_properties(test_mdns_list
PROPERTIES
Expand All @@ -17,3 +30,18 @@ add_test(NAME test_mdns_mapper COMMAND test_mdns_mapper)
set_tests_properties(test_mdns_mapper
PROPERTIES
WILL_FAIL FALSE)

add_test(NAME test_command_mapper COMMAND test_command_mapper)
set_tests_properties(test_command_mapper
PROPERTIES
WILL_FAIL FALSE)

add_test(NAME test_reflection_list COMMAND test_reflection_list)
set_tests_properties(test_reflection_list
PROPERTIES
WILL_FAIL FALSE)

add_test(NAME test_mdns_service COMMAND test_mdns_service)
set_tests_properties(test_mdns_service
PROPERTIES
WILL_FAIL FALSE)
61 changes: 61 additions & 0 deletions tests/dns/test_command_mapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <errno.h>
#include <net/if.h>
#include <libgen.h>
#include <setjmp.h>
#include <cmocka.h>

#include "utils/log.h"
#include "utils/os.h"
#include "dns/command_mapper.h"

static void test_put_command_mapper(void **state)
{
(void) state;

hmap_command_conn *hmap = NULL;
char *command = "test";

assert_int_equal(put_command_mapper(&hmap, command), 0);

free_command_mapper(&hmap);
}

static void test_check_command_mapper(void **state)
{
(void) state;

hmap_command_conn *hmap = NULL;
char *command1 = "test1";
char *command2 = "test2";

put_command_mapper(&hmap, command1);
assert_int_equal(check_command_mapper(&hmap, command1), 1);
assert_int_equal(check_command_mapper(&hmap, command2), 0);

free_command_mapper(&hmap);
}

int main(int argc, char *argv[])
{
(void) argc; /* unused */
(void) argv; /* unused */

log_set_quiet(false);

const struct CMUnitTest tests[] = {
cmocka_unit_test(test_put_command_mapper),
cmocka_unit_test(test_check_command_mapper)
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
41 changes: 26 additions & 15 deletions tests/dns/test_mdns_mapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,11 @@ static void test_put_mdns_answer_mapper(void **state)
hmap_mdns_conn *imap = NULL;
uint8_t ip[IP_ALEN] = {10,0,0,23};
uint8_t ip1[IP_ALEN] = {10,0,0,24};
char *test1 = "test1";
char *test2 = "test2";

struct mdns_query_entry query = {};
struct mdns_answer_entry answer = {};
strcpy(query.qname, test1);
strcpy(answer.rrname, test2);

assert_int_equal(put_mdns_answer_mapper(&imap, ip, &answer), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_ANSWER), 1);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_QUERY), 0);
assert_int_equal(put_mdns_query_mapper(&imap, ip, &query), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_ANSWER), 1);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_QUERY), 1);
assert_int_equal(put_mdns_query_mapper(&imap, ip1, &query), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip1, MDNS_REQUEST_ANSWER), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip1, MDNS_REQUEST_QUERY), 1);
assert_int_equal(put_mdns_answer_mapper(&imap, ip1, &answer), 0);

free_mdns_mapper(&imap);
}
Expand All @@ -50,6 +38,22 @@ static void test_put_mdns_query_mapper(void **state)
{
(void) state;

hmap_mdns_conn *imap = NULL;
uint8_t ip[IP_ALEN] = {10,0,0,23};
uint8_t ip1[IP_ALEN] = {10,0,0,24};

struct mdns_query_entry query = {};

assert_int_equal(put_mdns_query_mapper(&imap, ip, &query), 0);
assert_int_equal(put_mdns_query_mapper(&imap, ip1, &query), 0);

free_mdns_mapper(&imap);
}

static void test_check_mdns_mapper_req(void **state)
{
(void) state; /* unused */

hmap_mdns_conn *imap = NULL;
uint8_t ip[IP_ALEN] = {10,0,0,23};
uint8_t ip1[IP_ALEN] = {10,0,0,24};
Expand All @@ -61,23 +65,30 @@ static void test_put_mdns_query_mapper(void **state)
strcpy(query.qname, test1);
strcpy(answer.rrname, test2);

assert_int_equal(put_mdns_answer_mapper(&imap, ip, &answer), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_ANSWER), 1);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_QUERY), 0);
assert_int_equal(put_mdns_query_mapper(&imap, ip, &query), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_ANSWER), 1);
assert_int_equal(check_mdns_mapper_req(&imap, ip, MDNS_REQUEST_QUERY), 1);
assert_int_equal(put_mdns_query_mapper(&imap, ip1, &query), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip1, MDNS_REQUEST_ANSWER), 0);
assert_int_equal(check_mdns_mapper_req(&imap, ip1, MDNS_REQUEST_QUERY), 1);

free_mdns_mapper(&imap);
free_mdns_mapper(&imap);
}

int main(int argc, char *argv[])
{
(void) argc; /* unused */
(void) argv; /* unused */

log_set_quiet(false);

const struct CMUnitTest tests[] = {
cmocka_unit_test(test_put_mdns_answer_mapper),
cmocka_unit_test(test_put_mdns_query_mapper)
cmocka_unit_test(test_put_mdns_query_mapper),
cmocka_unit_test(test_check_mdns_mapper_req)
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down
82 changes: 82 additions & 0 deletions tests/dns/test_mdns_service.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <errno.h>
#include <net/if.h>
#include <libgen.h>
#include <setjmp.h>
#include <cmocka.h>

#include "utils/log.h"
#include "utils/os.h"
#include "dns/mdns_service.h"
#include "capture/pcap_service.h"
#include "utils/eloop.h"

int __wrap_run_pcap(char *interface, bool immediate, bool promiscuous,
int timeout, char *filter, bool nonblock, capture_callback_fn pcap_fn,
void *fn_ctx, struct pcap_context** pctx)
{
(void) interface;
(void) immediate;
(void) promiscuous;
(void) timeout;
(void) filter;
(void) nonblock;
(void) pcap_fn;
(void) fn_ctx;

*pctx = (struct pcap_context*) os_zalloc(sizeof(struct pcap_context));
return 0;
}

int __wrap_eloop_register_read_sock(int sock, eloop_sock_handler handler,
void *eloop_data, void *user_data)
{
(void) sock;
(void) handler;
(void) eloop_data;
(void) user_data;

return 0;
}

int __wrap_eloop_init(void)
{
return 0;
}

void __wrap_eloop_destroy(void) {}
void __wrap_eloop_run(void) {}

static void test_run_mdns(void **state)
{
(void) state;

struct mdns_context context;
context.ifname = os_strdup("wlan0");
assert_int_equal(run_mdns(&context), 0);
os_free(context.ifname);
close_mdns(&context);
}

int main(int argc, char *argv[])
{
(void) argc; /* unused */
(void) argv; /* unused */

log_set_quiet(false);

const struct CMUnitTest tests[] = {
cmocka_unit_test(test_run_mdns),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
63 changes: 63 additions & 0 deletions tests/dns/test_reflection_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <errno.h>
#include <net/if.h>
#include <libgen.h>
#include <setjmp.h>
#include <cmocka.h>
#include <sys/socket.h>

#include "utils/log.h"
#include "utils/os.h"
#include "dns/reflection_list.h"

static void test_init_reflection_list(void **state)
{
(void) state;

struct reflection_list *list = NULL;

list = init_reflection_list();
assert_non_null(list);

free_reflection_list(list);
}

static void test_push_reflection_list(void **state)
{
(void) state;

struct reflection_list *el;
struct reflection_list *list = init_reflection_list();

assert_int_equal(dl_list_len(&list->list), 0);
assert_non_null(push_reflection_list(list, 0, "wlan0"));
assert_int_equal(dl_list_len(&list->list), 1);
el = dl_list_first(&list->list, struct reflection_list, list);
assert_non_null(el);
assert_string_equal(el->ifname, "wlan0");
free_reflection_list(list);
}

int main(int argc, char *argv[])
{
(void) argc; /* unused */
(void) argv; /* unused */

log_set_quiet(false);

const struct CMUnitTest tests[] = {
cmocka_unit_test(test_init_reflection_list),
cmocka_unit_test(test_push_reflection_list)
};

return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit 4f5d386

Please sign in to comment.