1 /* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <assert.h>
18 #include <inttypes.h>
19 #include <tap/basic.h>
20
21 #include "libknot/rrset.h"
22 #include "libknot/descriptor.h"
23
check_rrset(const knot_rrset_t * rrset,const knot_dname_t * owner,uint16_t type,uint16_t rclass,uint32_t ttl)24 static bool check_rrset(const knot_rrset_t *rrset, const knot_dname_t *owner,
25 uint16_t type, uint16_t rclass, uint32_t ttl)
26 {
27 if (!rrset) {
28 return false;
29 }
30
31 const bool dname_cmp = owner == NULL ? rrset->owner == NULL :
32 knot_dname_is_equal(rrset->owner, owner);
33 return rrset->type == type && rrset->rclass == rclass && dname_cmp &&
34 rrset->ttl == ttl && rrset->rrs.count == 0; // We do not test rdataset here
35 }
36
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39 plan_lazy();
40
41 // Test new
42 knot_dname_t *dummy_owner = knot_dname_from_str_alloc("test.");
43 assert(dummy_owner);
44
45 knot_rrset_t *rrset = knot_rrset_new(dummy_owner, KNOT_RRTYPE_TXT,
46 KNOT_CLASS_IN, 3600, NULL);
47 ok(rrset != NULL, "rrset: create.");
48 assert(rrset);
49
50 ok(check_rrset(rrset, dummy_owner, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, 3600),
51 "rrset: set fields during create.");
52
53 // Test init
54 knot_dname_free(dummy_owner, NULL);
55 dummy_owner = knot_dname_from_str_alloc("test2.");
56 assert(dummy_owner);
57
58 knot_dname_free(rrset->owner, NULL);
59 knot_rrset_init(rrset, dummy_owner, KNOT_RRTYPE_A, KNOT_CLASS_CH, 7200);
60 ok(check_rrset(rrset, dummy_owner, KNOT_RRTYPE_A, KNOT_CLASS_CH, 7200),
61 "rrset: init.");
62
63 // Test copy
64 knot_rrset_t *copy = knot_rrset_copy(rrset, NULL);
65 ok(copy != NULL, "rrset: copy.");
66 ok(check_rrset(copy, rrset->owner, rrset->type, rrset->rclass, 7200),
67 "rrset: set fields during copy.");
68 ok(knot_rrset_copy(NULL, NULL) == NULL, "rrset: copy NULL.");
69 assert(copy);
70
71 // Test equal - same TTL
72 ok(knot_rrset_equal(rrset, copy, true), "rrset: cmp same TTL");
73
74 // Test equal - different TTL
75 copy->ttl++;
76 ok(!knot_rrset_equal(rrset, copy, true), "rrset: cmp different TTL");
77
78 // Test equal - ignore TTL
79 ok(knot_rrset_equal(rrset, copy, false), "rrset: cmp ignore TTL");
80
81 copy->ttl = rrset->ttl;
82
83 // Test equal - different type
84 copy->type++;
85 ok(!knot_rrset_equal(rrset, copy, true), "rrset: cmp different type");
86
87 copy->type = rrset->type;
88
89 // Test equal - owners
90 knot_dname_free(rrset->owner, NULL);
91 rrset->owner = NULL;
92 ok(!knot_rrset_equal(rrset, copy, true), "rrset: cmp NULL owner");
93
94 knot_dname_free(copy->owner, NULL);
95 copy->owner = NULL;
96 ok(knot_rrset_equal(rrset, copy, true), "rrset: cmp NULL owners");
97
98 // Test equal - different rdata
99 knot_rrset_add_rdata(copy, (const uint8_t *)"abc", 3, NULL);
100 ok(!knot_rrset_equal(rrset, copy, true), "rrset: cmp different rdata");
101
102 // Test clear
103 knot_rrset_clear(rrset, NULL);
104 ok(rrset->owner == NULL, "rrset: clear.");
105
106 // Test empty
107 ok(knot_rrset_empty(rrset), "rrset: empty.");
108 ok(knot_rrset_empty(NULL), "rrset: empty NULL.");
109 copy->rrs.count = 1;
110 ok(!knot_rrset_empty(copy), "rrset: not empty.");
111
112 // Test init empty
113 knot_rrset_init_empty(rrset);
114 ok(check_rrset(rrset, NULL, 0, KNOT_CLASS_IN, 0), "rrset: init empty.");
115
116 // "Test" freeing
117 knot_rrset_free(rrset, NULL);
118 knot_rrset_free(copy, NULL);
119
120 return 0;
121 }
122