test_node.c 5.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*  Copyright (C) 2011 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 <assert.h>
#include <tap/basic.h>

#include "knot/zone/node.h"
21
#include "libknot/libknot.h"
22
#include "contrib/wire.h"
23 24 25 26 27 28 29 30 31 32 33

static knot_rrset_t *create_dummy_rrset(const knot_dname_t *owner,
                                        uint16_t type)
{
	knot_rrset_t *r = knot_rrset_new(owner, type,
	                                 KNOT_CLASS_IN, NULL);
	assert(r);
	uint8_t wire[16] = { 0 };
	memcpy(wire, "testtest", strlen("testtest"));
	int ret = knot_rrset_add_rdata(r, wire, strlen("testtest"), 3600, NULL);
	assert(ret == KNOT_EOK);
34
	(void)ret;
35 36 37 38 39 40 41 42 43 44
	return r;
}

static knot_rrset_t *create_dummy_rrsig(const knot_dname_t *owner,
                                        uint16_t type)
{
	knot_rrset_t *r = knot_rrset_new(owner, KNOT_RRTYPE_RRSIG,
	                                 KNOT_CLASS_IN, NULL);
	assert(r);
	uint8_t wire[sizeof(uint16_t)];
45
	wire_write_u16(wire, type);
46 47
	int ret = knot_rrset_add_rdata(r, wire, sizeof(uint16_t), 3600, NULL);
	assert(ret == KNOT_EOK);
48
	(void)ret;
49 50 51 52 53 54
	return r;
}

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

56
	knot_dname_t *dummy_owner = knot_dname_from_str_alloc("test.");
57
	// Test new
Jan Kadlec's avatar
Jan Kadlec committed
58
	zone_node_t *node = node_new(dummy_owner, NULL);
59 60 61
	ok(node != NULL, "Node: new");
	assert(node);
	ok(knot_dname_is_equal(node->owner, dummy_owner), "Node: new - set fields");
62

63
	// Test parent setting
Jan Kadlec's avatar
Jan Kadlec committed
64
	zone_node_t *parent = node_new(dummy_owner, NULL);
65 66 67
	assert(parent);
	node_set_parent(node, parent);
	ok(node->parent == parent && parent->children == 1, "Node: set parent.");
68

69
	node_free(&parent, NULL);
70

71 72
	// Test RRSet addition
	knot_rrset_t *dummy_rrset = create_dummy_rrset(dummy_owner, KNOT_RRTYPE_TXT);
Jan Kadlec's avatar
Jan Kadlec committed
73
	int ret = node_add_rrset(node, dummy_rrset, NULL);
74 75
	ok(ret == KNOT_EOK && node->rrset_count == 1 &&
	   knot_rdataset_eq(&dummy_rrset->rrs, &node->rrs[0].rrs), "Node: add RRSet.");
76

77 78
	// Test shallow copy
	node->flags |= NODE_FLAGS_DELEG;
Jan Kadlec's avatar
Jan Kadlec committed
79
	zone_node_t *copy = node_shallow_copy(node, NULL);
80 81 82 83 84 85 86 87
	ok(copy != NULL, "Node: shallow copy.");
	assert(copy);
	const bool copy_ok = knot_dname_is_equal(copy->owner, node->owner) &&
	                     copy->rrset_count == node->rrset_count &&
	                     memcmp(copy->rrs, node->rrs,
	                            copy->rrset_count * sizeof(struct rr_data)) == 0 &&
	                     copy->flags == node->flags;
	ok(copy_ok, "Node: shallow copy - set fields.");
88

89
	node_free(&copy, NULL);
90

91 92 93 94
	// Test RRSet getters
	knot_rrset_t *n_rrset = node_create_rrset(node, KNOT_RRTYPE_TXT);
	ok(n_rrset && knot_rrset_equal(n_rrset, dummy_rrset, KNOT_RRSET_COMPARE_WHOLE),
	   "Node: create existing RRSet.");
95

96
	knot_rrset_free(&n_rrset, NULL);
97

98 99
	n_rrset = node_create_rrset(node, KNOT_RRTYPE_SOA);
	ok(n_rrset == NULL, "Node: create non-existing RRSet.");
100

101 102 103 104 105
	knot_rrset_t stack_rrset = node_rrset(node, KNOT_RRTYPE_TXT);
	ok(knot_rrset_equal(&stack_rrset, dummy_rrset,
	                    KNOT_RRSET_COMPARE_WHOLE), "Node: get existing RRSet.");
	stack_rrset = node_rrset(node, KNOT_RRTYPE_SOA);
	ok(knot_rrset_empty(&stack_rrset), "Node: get non-existent RRSet.");
106

107 108 109 110
	knot_rdataset_t *n_rdataset = node_rdataset(node, KNOT_RRTYPE_TXT);
	ok(n_rdataset && knot_rdataset_eq(n_rdataset, &dummy_rrset->rrs),
	   "Node: get existing rdataset.");
	n_rdataset = node_rdataset(node, KNOT_RRTYPE_SOA);
111
	ok(n_rdataset == NULL, "Node: get non-existing rdataset.");
112

113 114 115 116 117
	stack_rrset = node_rrset_at(node, 0);
	ok(knot_rrset_equal(&stack_rrset, dummy_rrset, KNOT_RRSET_COMPARE_WHOLE),
	   "Node: get existing position.");
	stack_rrset = node_rrset_at(node, 1);
	ok(knot_rrset_empty(&stack_rrset), "Node: get non-existent position.");
118

119 120 121
	// Test TTL mismatch
	knot_rdata_t *data = knot_rdataset_at(&dummy_rrset->rrs, 0);
	knot_rdata_set_ttl(data, 1800);
Jan Kadlec's avatar
Jan Kadlec committed
122
	ret = node_add_rrset(node, dummy_rrset, NULL);
123 124
	ok(ret == KNOT_ETTL && node->rrset_count == 1,
	   "Node: add RRSet, TTL mismatch.");
125

126
	knot_rrset_free(&dummy_rrset, NULL);
127

128 129 130 131
	// Test bool functions
	ok(node_rrtype_exists(node, KNOT_RRTYPE_TXT), "Node: type exists.");
	ok(!node_rrtype_exists(node, KNOT_RRTYPE_AAAA), "Node: type does not exist.");
	ok(!node_rrtype_is_signed(node, KNOT_RRTYPE_TXT), "Node: type is not signed.");
132

133
	dummy_rrset = create_dummy_rrsig(dummy_owner, KNOT_RRTYPE_TXT);
Jan Kadlec's avatar
Jan Kadlec committed
134
	ret = node_add_rrset(node, dummy_rrset, NULL);
135
	assert(ret == KNOT_EOK);
136

137
	ok(node_rrtype_is_signed(node, KNOT_RRTYPE_TXT), "Node: type is signed.");
138

139
	knot_rrset_free(&dummy_rrset, NULL);
140

141 142 143 144 145 146
	// Test remove RRset
	node_remove_rdataset(node, KNOT_RRTYPE_AAAA);
	ok(node->rrset_count == 2, "Node: remove non-existent rdataset.");
	void *to_free = node_rdataset(node, KNOT_RRTYPE_TXT)->data;
	node_remove_rdataset(node, KNOT_RRTYPE_TXT);
	ok(node->rrset_count == 1, "Node: remove existing rdataset.");
147

148
	free(to_free);
149

150
	// "Test" freeing
Jan Kadlec's avatar
Jan Kadlec committed
151
	node_free_rrsets(node, NULL);
152
	ok(node->rrset_count == 0, "Node: free RRSets.");
153

154
	node_free(&node, NULL);
155
	ok(node == NULL, "Node: free.");
156

157 158 159 160
	knot_dname_free(&dummy_owner, NULL);

	return 0;
}