test_zone-tree.c 3.09 KB
Newer Older
1
/*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    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/>.
 */

17
#include <tap/basic.h>
18

19
#include "libknot/errcode.h"
20
#include "knot/zone/zone-tree.h"
21 22 23

#define NCOUNT 4
static knot_dname_t* NAME[NCOUNT];
24
static zone_node_t NODE[NCOUNT];
25
static knot_dname_t* ORDER[NCOUNT];
26
static void ztree_init_data(void)
27
{
28 29 30 31
	NAME[0] = knot_dname_from_str_alloc(".");
	NAME[1] = knot_dname_from_str_alloc("master.ac.");
	NAME[2] = knot_dname_from_str_alloc("ac.");
	NAME[3] = knot_dname_from_str_alloc("ns.");
32 33 34 35 36 37 38

	knot_dname_t *order[NCOUNT] = {
	        NAME[0], NAME[2], NAME[1], NAME[3]
	};
	memcpy(ORDER, order, NCOUNT * sizeof(knot_dname_t*));

	for (unsigned i = 0; i < NCOUNT; ++i) {
39
		memset(NODE + i, 0, sizeof(zone_node_t));
40 41 42 43 44 45
		NODE[i].owner = NAME[i];
		NODE[i].prev = NODE + ((NCOUNT + i - 1) % NCOUNT);
		NODE[i].rrset_count = 1; /* required for ordered search */
	}
}

46
static void ztree_free_data(void)
47 48
{
	for (unsigned i = 0; i < NCOUNT; ++i)
49
		knot_dname_free(NAME + i, NULL);
50 51
}

52
static int ztree_iter_data(zone_node_t **node, void *data)
53
{
54
	unsigned *i = (unsigned *)data;
55
	knot_dname_t *owner = (*node)->owner;
56 57 58
	int result = KNOT_EOK;
	if (owner != ORDER[*i]) {
		result = KNOT_ERROR;
59 60
		char *exp_s = knot_dname_to_str_alloc(ORDER[*i]);
		char *owner_s = knot_dname_to_str_alloc(owner);
61
		diag("ztree: at index: %u expected '%s' got '%s'\n", *i, exp_s, owner_s);
62 63 64
		free(exp_s);
		free(owner_s);
	}
65 66
	++(*i);
	return result;
67 68 69 70 71 72 73 74 75
}

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

	ztree_init_data();

	/* 1. create test */
76
	zone_tree_t* t = zone_tree_create();
77 78 79 80 81
	ok(t != NULL, "ztree: created");

	/* 2. insert test */
	unsigned passed = 1;
	for (unsigned i = 0; i < NCOUNT; ++i) {
82
		if (zone_tree_insert(t, NODE + i) != KNOT_EOK) {
83 84 85 86 87 88 89 90
			passed = 0;
			break;
		}
	}
	ok(passed, "ztree: insertion");

	/* 3. check data test */
	passed = 1;
91
	zone_node_t *node = NULL;
92
	for (unsigned i = 0; i < NCOUNT; ++i) {
93
		int r = zone_tree_get(t, NAME[i], &node);
94 95 96 97 98 99 100 101 102
		if (r != KNOT_EOK || node != NODE + i) {
			passed = 0;
			break;
		}
	}
	ok(passed, "ztree: lookup");

	/* 4. ordered lookup */
	node = NULL;
103
	zone_node_t *prev = NULL;
104
	knot_dname_t *tmp_dn = knot_dname_from_str_alloc("z.ac.");
105
	zone_tree_get_less_or_equal(t, tmp_dn, &node, &prev);
106
	knot_dname_free(&tmp_dn, NULL);
107 108 109
	ok(prev == NODE + 1, "ztree: ordered lookup");

	/* 5. ordered traversal */
110
	unsigned i = 0;
111
	int ret = zone_tree_apply(t, ztree_iter_data, &i);
112
	ok (ret == KNOT_EOK, "ztree: ordered traversal");
113

114
	zone_tree_free(&t);
115 116 117
	ztree_free_data();
	return 0;
}