From: Aki Tuomi Date: Fri, 11 Nov 2016 11:36:23 +0000 (+0200) Subject: lib-dns: Add tests for dns-util X-Git-Tag: 2.2.27~142 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=41bee81223f49e83d922314ca93d841de831e931;p=thirdparty%2Fdovecot%2Fcore.git lib-dns: Add tests for dns-util --- diff --git a/src/lib-dns/Makefile.am b/src/lib-dns/Makefile.am index 4a0e8bc46f..302e37cf3d 100644 --- a/src/lib-dns/Makefile.am +++ b/src/lib-dns/Makefile.am @@ -11,5 +11,31 @@ headers = \ dns-lookup.h \ dns-util.h +test_programs = \ + test-dns-util + +noinst_PROGRAMS = $(test_programs) + +test_libs = \ + libdns.la \ + ../lib-test/libtest.la \ + ../lib/liblib.la + +test_dns_util_SOURCE = test-dns-util.c +test_dns_util_LDADD = $(test_libs) +test_dns_util_CFLAGS = $(AM_CPPFLAGS) \ + -I$(top_srcdir)/src/lib-test + +check: check-am check-test +check-test: all-am + for bin in $(test_programs); do \ + if test "$$bin" = "test-program-client-local"; then \ + if ! env NOVALGRIND=yes $(RUN_TEST) ./$$bin; then exit 1; fi; \ + else \ + if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \ + fi \ + done + + pkginc_libdir=$(pkgincludedir) pkginc_lib_HEADERS = $(headers) diff --git a/src/lib-dns/test-dns-util.c b/src/lib-dns/test-dns-util.c new file mode 100644 index 0000000000..01c043facc --- /dev/null +++ b/src/lib-dns/test-dns-util.c @@ -0,0 +1,121 @@ +#include "lib.h" +#include "test-lib.h" +#include "dns-util.h" +#include "array.h" + +static void test_dns_compare(void) +{ + struct { + const char *a; + const char *b; + int res; + } tests[] = + { + { NULL, NULL, 0 }, + { NULL, "", 1 }, + { "", NULL, -1 }, + { "", "", 0 }, + { "a", "a", 0 }, + { "a", "b", -1 }, + { "b", "a", 1 }, + { "A", "A", 0 }, + { "A", "B", -1 }, + { "B", "A", 1 }, + { "A", "a", 0 }, + { "a", "B", -1 }, + { "B", "a", 1 }, + { "test.invalid", "TeSt.InVaLid", 0 }, + { "alphabet.com", "alpha.com", 52 }, + { "com.alphabet", "com.alpha", 98 }, + { "com.com", "com.comcom", -99 }, + }; + + test_begin("test_dns_compare"); + + for(size_t i = 0; i < N_ELEMENTS(tests); i++) { + test_assert_idx(dns_compare(tests[i].a, tests[i].b) == tests[i].res, i); + test_assert_idx(dns_compare_labels(tests[i].a, tests[i].b) == tests[i].res, i); + } + + test_end(); +} + +static void test_dns_match(void) +{ + struct { + const char *name; + const char *mask; + int res; + } tests[] = + { + { "", "", 0 }, + { "", "*", 0 }, + { "*", "", -1 }, + { "TeSt.InVaLid", "test.invalid", 0 }, + { "contoso.com", "test.invalid", -1 }, + { "test.invalid", "test.unvalid", -1 }, + { "name.test.invalid", "*.test.invalid", 0 }, + { "real.name.test.invalid", "*.test.invalid", -1 }, + { "real.name.test.invalid", "*.*.test.invalid", 0 }, + { "name.test.invalid", "*name*.test.invalid", -1 }, + { "name.invalid", "name.*", -1 }, + }; + + test_begin("test_dns_match"); + + for(size_t i = 0; i < N_ELEMENTS(tests); i++) { + test_assert_idx(dns_match_wildcard(tests[i].name, tests[i].mask) == tests[i].res, i); + } + + test_end(); +} + +static int +arr_dns_compare(const char *const *a, const char *const *b) +{ + return dns_compare_labels(*a,*b); +} + +static void test_dns_sort(void) +{ + const char *input[] = { + "test.invalid", + "test.com", + "test.contoso.com", + "test.alphabet.com", + "test.xxx", + }; + + const char *output[] = { + "test.alphabet.com", + "test.contoso.com", + "test.com", + "test.invalid", + "test.xxx", + }; + + test_begin("test_dns_sort"); + + ARRAY_TYPE(const_string) arr; + t_array_init(&arr, 8); + + array_append(&arr, input, N_ELEMENTS(input)); + + array_sort(&arr, arr_dns_compare); + + for(size_t i = 0; i < N_ELEMENTS(output); i++) { + test_assert_idx(dns_compare(*array_idx(&arr, i), output[i]) == 0, i); + } + + test_end(); +} + +int main(void) { + void (*test_functions[])(void) = { + test_dns_compare, + test_dns_match, + test_dns_sort, + NULL + }; + return test_run(test_functions); +}