]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dns: Add tests for dns-util
authorAki Tuomi <aki.tuomi@dovecot.fi>
Fri, 11 Nov 2016 11:36:23 +0000 (13:36 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 18 Nov 2016 11:44:21 +0000 (13:44 +0200)
src/lib-dns/Makefile.am
src/lib-dns/test-dns-util.c [new file with mode: 0644]

index 4a0e8bc46f5fd99f3bafbc464b6f8e31c8a9447e..302e37cf3d4b30db3ce677eba59f68678b74e360 100644 (file)
@@ -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 (file)
index 0000000..01c043f
--- /dev/null
@@ -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);
+}