bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
a81d5c3f5a4ad5d100b258d10d4c75f4a02ab1f6Stephan Bosch
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi#include "lib.h"
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi#include "test-lib.h"
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi#include "dns-util.h"
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi#include "array.h"
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomistatic void test_dns_compare(void)
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi{
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi const char *a;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi const char *b;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi int res;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi } tests[] =
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { NULL, NULL, 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { NULL, "", 1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "", NULL, -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "", "", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "a", "a", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "a", "b", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "b", "a", 1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "A", "A", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "A", "B", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "B", "A", 1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "A", "a", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "a", "B", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "B", "a", 1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "test.invalid", "TeSt.InVaLid", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "alphabet.com", "alpha.com", 52 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "com.alphabet", "com.alpha", 98 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "com.com", "com.comcom", -99 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi };
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_begin("test_dns_compare");
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi for(size_t i = 0; i < N_ELEMENTS(tests); i++) {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_assert_idx(dns_compare(tests[i].a, tests[i].b) == tests[i].res, i);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_assert_idx(dns_compare_labels(tests[i].a, tests[i].b) == tests[i].res, i);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi }
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_end();
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi}
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomistatic void test_dns_match(void)
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi{
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi const char *name;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi const char *mask;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi int res;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi } tests[] =
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "", "", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "", "*", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "*", "", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "TeSt.InVaLid", "test.invalid", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "contoso.com", "test.invalid", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "test.invalid", "test.unvalid", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "name.test.invalid", "*.test.invalid", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "real.name.test.invalid", "*.test.invalid", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "real.name.test.invalid", "*.*.test.invalid", 0 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "name.test.invalid", "*name*.test.invalid", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi { "name.invalid", "name.*", -1 },
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi };
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_begin("test_dns_match");
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi for(size_t i = 0; i < N_ELEMENTS(tests); i++) {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_assert_idx(dns_match_wildcard(tests[i].name, tests[i].mask) == tests[i].res, i);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi }
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_end();
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi}
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomistatic int
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomiarr_dns_compare(const char *const *a, const char *const *b)
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi{
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi return dns_compare_labels(*a,*b);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi}
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomistatic void test_dns_sort(void)
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi{
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi const char *input[] = {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.invalid",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.com",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.contoso.com",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.alphabet.com",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.xxx",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi };
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi const char *output[] = {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.alphabet.com",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.contoso.com",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.com",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.invalid",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi "test.xxx",
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi };
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_begin("test_dns_sort");
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi ARRAY_TYPE(const_string) arr;
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi t_array_init(&arr, 8);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi array_append(&arr, input, N_ELEMENTS(input));
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi array_sort(&arr, arr_dns_compare);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi for(size_t i = 0; i < N_ELEMENTS(output); i++) {
00a15fe7ccadda399a86fe67ad2ed22488ff0c05Timo Sirainen const char *const *strp = array_idx(&arr, i);
00a15fe7ccadda399a86fe67ad2ed22488ff0c05Timo Sirainen test_assert_idx(dns_compare(*strp, output[i]) == 0, i);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi }
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_end();
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi}
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomiint main(void) {
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen void (*const test_functions[])(void) = {
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_dns_compare,
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_dns_match,
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi test_dns_sort,
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi NULL
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi };
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi return test_run(test_functions);
a7f58d28d4ccceaf03e2032ec8e855e1ba965bfeAki Tuomi}