summaryrefslogtreecommitdiffstats
path: root/tests/knot/test_changeset.c
blob: 6775b76bd415562b1ac40f166e2b5d10fca63231 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*  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 <config.h>
#include <assert.h>
#include <tap/basic.h>

#include "libknot/errcode.h"
#include "libknot/error.h"
#include "knot/updates/changesets.h"

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

	// Test with NULL changeset
	ok(changeset_size(NULL) == 0, "changeset: NULL size");
	ok(changeset_empty(NULL), "changeset: NULL empty");

	// Test creation.
	knot_dname_t *d = knot_dname_from_str_alloc("test.");
	assert(d);
	changeset_t *ch = changeset_new(d);
	knot_dname_free(d, NULL);
	ok(ch != NULL, "changeset: new");
	if (!ch) {
		return 1;
	}
	ok(changeset_empty(ch), "changeset: empty");
	ch->soa_to = (knot_rrset_t *)0xdeadbeef;
	ok(!changeset_empty(ch), "changeset: empty SOA");
	ch->soa_to = NULL;
	ok(changeset_size(ch) == 0, "changeset: empty size");

	// Test additions.
	d = knot_dname_from_str_alloc("non.terminals.test.");
	assert(d);
	knot_rrset_t *apex_txt_rr = knot_rrset_new(d, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, 3600, NULL);
	assert(apex_txt_rr);
	uint8_t data[8] = "\7teststr";
	knot_rrset_add_rdata(apex_txt_rr, data, sizeof(data), NULL);

	int ret = changeset_add_addition(ch, apex_txt_rr, CHANGESET_CHECK);
	is_int(KNOT_EOK, ret, "changeset: add RRSet");
	ok(changeset_size(ch) == 1, "changeset: size add");
	ret = changeset_add_removal(ch, apex_txt_rr, CHANGESET_CHECK);
	is_int(KNOT_EOK, ret, "changeset: rem RRSet");
	ok(changeset_size(ch) == 0, "changeset: size remove");
	ok(changeset_empty(ch), "changeset: empty");
	changeset_add_addition(ch, apex_txt_rr, CHANGESET_CHECK);

	// Add another RR to node.
	knot_rrset_t *apex_spf_rr = knot_rrset_new(d, KNOT_RRTYPE_SPF, KNOT_CLASS_IN, 3600, NULL);
	assert(apex_spf_rr);
	knot_rrset_add_rdata(apex_spf_rr, data, sizeof(data), NULL);
	ret = changeset_add_addition(ch, apex_spf_rr, CHANGESET_CHECK);
	is_int(KNOT_EOK, ret, "changeset: add multiple");

	// Add another node.
	knot_dname_free(d, NULL);
	d = knot_dname_from_str_alloc("here.come.more.non.terminals.test");
	assert(d);
	knot_rrset_t *other_rr = knot_rrset_new(d, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, 3600, NULL);
	assert(other_rr);
	knot_rrset_add_rdata(other_rr, data, sizeof(data), NULL);
	ret = changeset_add_addition(ch, other_rr, CHANGESET_CHECK);
	is_int(KNOT_EOK, ret, "changeset: remove multiple");

	// Test add traversal.
	changeset_iter_t it;
	ret = changeset_iter_add(&it, ch);
	is_int(KNOT_EOK, ret, "changeset: create iter add");
	// Order: non.terminals.test. TXT, SPF, here.come.more.non.terminals.test. TXT.
	knot_rrset_t iter = changeset_iter_next(&it);
	bool trav_ok = knot_rrset_equal(&iter, apex_txt_rr, true);
	iter = changeset_iter_next(&it);
	trav_ok = trav_ok && knot_rrset_equal(&iter, apex_spf_rr, true);
	iter = changeset_iter_next(&it);
	trav_ok = trav_ok && knot_rrset_equal(&iter, other_rr, true);

	ok(trav_ok, "changeset: add traversal");

	iter = changeset_iter_next(&it);
	changeset_iter_clear(&it);
	ok(knot_rrset_empty(&iter), "changeset: traversal: skip non-terminals");

	changeset_add_removal(ch, apex_txt_rr, CHANGESET_CHECK);
	changeset_add_removal(ch, apex_txt_rr, CHANGESET_CHECK);

	// Test remove traversal.
	ret = changeset_iter_rem(&it, ch);
	is_int(KNOT_EOK, ret, "changeset: create iter rem");
	iter = changeset_iter_next(&it);
	ok(knot_rrset_equal(&iter, apex_txt_rr, true),
	   "changeset: rem traversal");
	changeset_iter_clear(&it);

	// Test all traversal - just count.
	ret = changeset_iter_all(&it, ch);
	is_int(KNOT_EOK, ret, "changeset: create iter all");
	size_t size = 0;
	iter = changeset_iter_next(&it);
	while (!knot_rrset_empty(&iter)) {
		++size;
		iter = changeset_iter_next(&it);
	}
	changeset_iter_clear(&it);
	ok(size == 3, "changeset: iter all");

	// Create new changeset.
	knot_dname_free(d, NULL);
	d = knot_dname_from_str_alloc("test.");
	assert(d);
	changeset_t *ch2 = changeset_new(d);
	knot_dname_free(d, NULL);
	assert(ch2);
	// Add something to add section.
	knot_dname_free(apex_txt_rr->owner, NULL);
	apex_txt_rr->owner = knot_dname_from_str_alloc("something.test.");
	assert(apex_txt_rr->owner);
	ret = changeset_add_addition(ch2, apex_txt_rr, CHANGESET_CHECK);
	assert(ret == KNOT_EOK);

	// Add something to remove section.
	knot_dname_free(apex_txt_rr->owner, NULL);
	apex_txt_rr->owner =
		knot_dname_from_str_alloc("and.now.for.something.completely.different.test.");
	assert(apex_txt_rr->owner);
	ret = changeset_add_removal(ch2, apex_txt_rr, CHANGESET_CHECK);
	assert(ret == KNOT_EOK);

	// Test merge.
	ret = changeset_merge(ch, ch2, 0);
	ok(ret == KNOT_EOK && changeset_size(ch) == 5, "changeset: merge");

	// Test cleanup.
	changeset_clear(ch);
	ok(changeset_empty(ch), "changeset: clear");
	free(ch);

	list_t chgs;
	init_list(&chgs);
	add_head(&chgs, &ch2->n);
	changesets_clear(&chgs);
	ok(changeset_empty(ch2), "changeset: clear list");
	free(ch2);

	knot_rrset_free(apex_txt_rr, NULL);
	knot_rrset_free(apex_spf_rr, NULL);
	knot_rrset_free(other_rr, NULL);

	return 0;
}