test_zonedb.c 3.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*  Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>

19 20
#include "knot/zone/zone.h"
#include "knot/zone/zonedb.h"
21 22
#include "contrib/openbsd/strlcat.h"
#include "contrib/openbsd/strlcpy.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#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(6);
41

42
	/* Create database. */
43
	char buf[KNOT_DNAME_MAXLEN];
44 45 46
	const char *prefix = "zzz.";
	size_t nr_passed = 0;
	knot_dname_t *dname = NULL;
47
	zone_t *zones[ZONE_COUNT] = {0};
48 49
	knot_zonedb_t *db = knot_zonedb_new(ZONE_COUNT);
	ok(db != NULL, "zonedb: new");
50

51 52
	/* Populate. */
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
53 54 55
		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);
56

57 58 59
		if (zones[i] == NULL) {
			goto cleanup;
		}
60
		if (knot_zonedb_insert(db, zones[i]) == KNOT_EOK) {
61 62 63 64 65 66
			++nr_passed;
		} else {
			diag("knot_zonedb_add_zone(%s) failed", zone_list[i]);
		}
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: add zones");
67

68 69
	/* Build search index. */
	ok(knot_zonedb_build_index(db) == KNOT_EOK, "zonedb: build search index");
70

71 72 73
	/* Lookup of exact names. */
	nr_passed = 0;
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
74
		dname = knot_dname_from_str_alloc(zone_list[i]);
75
		if (knot_zonedb_find(db, dname) == zones[i]) {
76 77
			++nr_passed;
		} else {
78
			diag("knot_zonedb_find(%s) failed", zone_list[i]);
79
		}
80
		knot_dname_free(&dname, NULL);
81 82
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: find exact zones");
83

84 85 86
	/* Lookup of sub-names. */
	nr_passed = 0;
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
87
		strlcpy(buf, prefix, sizeof(buf));
88
		if (strcmp(zone_list[i], ".") != 0) {
89
			strlcat(buf, zone_list[i], sizeof(buf));
90
		}
91
		dname = knot_dname_from_str_alloc(buf);
92
		if (knot_zonedb_find_suffix(db, dname) == zones[i]) {
93 94
			++nr_passed;
		} else {
Marek Vavrusa's avatar
Marek Vavrusa committed
95
			diag("knot_zonedb_find_suffix(%s) failed", buf);
96
		}
97
		knot_dname_free(&dname, NULL);
98 99
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: find zones for subnames");
100

101 102 103
	/* Remove all zones. */
	nr_passed = 0;
	for (unsigned i = 0; i < ZONE_COUNT; ++i) {
104
		dname = knot_dname_from_str_alloc(zone_list[i]);
105
		if (knot_zonedb_del(db, dname) == KNOT_EOK) {
106
			zone_free(&zones[i]);
107 108 109 110
			++nr_passed;
		} else {
			diag("knot_zonedb_remove_zone(%s) failed", zone_list[i]);
		}
111
		knot_dname_free(&dname, NULL);
112 113
	}
	ok(nr_passed == ZONE_COUNT, "zonedb: removed all zones");
114

115 116 117 118
cleanup:
	knot_zonedb_deep_free(&db);
	return 0;
}