summaryrefslogtreecommitdiffstats
path: root/tests/knot/test_zonedb.c
blob: 3ef7632fea0033515bfabeb99b470b234ed75c08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>

#include "knot/zone/zone.h"
#include "knot/zone/zonedb.h"
#include "contrib/openbsd/strlcat.h"
#include "contrib/openbsd/strlcpy.h"

#define ZONE_COUNT 10
static const char *zone_list[ZONE_COUNT] = {
        ".",
        "com",
        "net",
        "c.com",
        "a.com",
        "a.net",
        "b.net",
        "c.a.com",
        "b.b.b.com",
        "b.b.b.b.net",
};

int main(int argc, char *argv[])
{
	plan_lazy();

	/* Create database. */
	knot_dname_txt_storage_t buf;
	const char *prefix = "zzz.";
	size_t nr_passed = 0;
	knot_dname_t *dname = NULL;
	zone_t *zones[ZONE_COUNT] = {0};
	knot_zonedb_t *db = knot_zonedb_new();
	ok(db != NULL, "zonedb: new");

	/* Populate. */
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
		knot_dname_t *zone_name = knot_dname_from_str_alloc(zone_list[i]);
		zones[i] = zone_new(zone_name);
		knot_dname_free(zone_name, NULL);

		if (zones[i] == NULL) {
			goto cleanup;
		}
		if (knot_zonedb_insert(db, zones[i]) == KNOT_EOK) {
			++nr_passed;
		} else {
			diag("knot_zonedb_add_zone(%s) failed", zone_list[i]);
		}
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: add zones");

	/* Lookup of exact names. */
	nr_passed = 0;
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
		dname = knot_dname_from_str_alloc(zone_list[i]);
		if (knot_zonedb_find(db, dname) == zones[i]) {
			++nr_passed;
		} else {
			diag("knot_zonedb_find(%s) failed", zone_list[i]);
		}
		knot_dname_free(dname, NULL);
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: find exact zones");

	/* Lookup of sub-names. */
	nr_passed = 0;
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
		strlcpy(buf, prefix, sizeof(buf));
		if (strcmp(zone_list[i], ".") != 0) {
			strlcat(buf, zone_list[i], sizeof(buf));
		}
		dname = knot_dname_from_str_alloc(buf);
		if (knot_zonedb_find_suffix(db, dname) == zones[i]) {
			++nr_passed;
		} else {
			diag("knot_zonedb_find_suffix(%s) failed", buf);
		}
		knot_dname_free(dname, NULL);
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: find zones for subnames");

	/* Remove all zones. */
	nr_passed = 0;
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
		dname = knot_dname_from_str_alloc(zone_list[i]);
		if (knot_zonedb_del(db, dname) == KNOT_EOK) {
			zone_free(&zones[i]);
			++nr_passed;
		} else {
			diag("knot_zonedb_remove_zone(%s) failed", zone_list[i]);
		}
		knot_dname_free(dname, NULL);
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: removed all zones");

cleanup:
	knot_zonedb_deep_free(&db, false);
	return 0;
}