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 <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <tap/basic.h>
21 
22 #include "libknot/descriptor.h"
23 
24 #define BUF_LEN 256
25 
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28 	plan_lazy();
29 
30 	const    knot_rdata_descriptor_t *descr;
31 	char     name[BUF_LEN] = "";
32 	int      ret;
33 	uint16_t num;
34 
35 	// Get descriptor, type num to string:
36 	// 1. TYPE0
37 	descr = knot_get_rdata_descriptor(0);
38 	ok(descr->type_name == 0, "get TYPE0 descriptor name");
39 	ok(descr->block_types[0] == KNOT_RDATA_WF_REMAINDER,
40 	   "get TYPE0 descriptor 1. item type");
41 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
42 	   "get TYPE0 descriptor 2. item type");
43 
44 	ret = knot_rrtype_to_string(0, name, BUF_LEN);
45 	ok(ret != -1, "get TYPE0 ret");
46 	ok(strcmp(name, "TYPE0") == 0, "get TYPE0 name");
47 
48 	// 2. A
49 	descr = knot_get_rdata_descriptor(1);
50 	ok(strcmp(descr->type_name, "A") == 0, "get A descriptor name");
51 	ok(descr->block_types[0] == 4,
52 	   "get A descriptor 1. item type");
53 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
54 	   "get A descriptor 2. item type");
55 
56 	ret = knot_rrtype_to_string(1, name, BUF_LEN);
57 	ok(ret != -1, "get A ret");
58 	ok(strcmp(name, "A") == 0, "get A name");
59 
60 	// 3. CNAME
61 	descr = knot_get_rdata_descriptor(5);
62 	ok(strcmp(descr->type_name, "CNAME") == 0, "get CNAME descriptor name");
63 	ok(descr->block_types[0] == KNOT_RDATA_WF_COMPRESSIBLE_DNAME,
64 	   "get CNAME descriptor 1. item type");
65 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
66 	   "get CNAME descriptor 2. item type");
67 
68 	ret = knot_rrtype_to_string(5, name, BUF_LEN);
69 	ok(ret != -1, "get CNAME ret");
70 	ok(strcmp(name, "CNAME") == 0, "get CNAME name");
71 
72 	// 4. TYPE38 (A6)
73 	descr = knot_get_rdata_descriptor(38);
74 	ok(descr->type_name == 0, "get TYPE38 descriptor name");
75 	ok(descr->block_types[0] == KNOT_RDATA_WF_REMAINDER,
76 	   "get TYPE38 descriptor 1. item type");
77 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
78 	   "get TYPE38 descriptor 2. item type");
79 
80 	ret = knot_rrtype_to_string(38, name, BUF_LEN);
81 	ok(ret != -1, "get TYPE38 ret");
82 	ok(strcmp(name, "TYPE38") == 0, "get TYPE38 name");
83 
84 	// 5. ANY
85 	descr = knot_get_rdata_descriptor(255);
86 	ok(strcmp(descr->type_name, "ANY") == 0, "get ANY descriptor name");
87 	ok(descr->block_types[0] == KNOT_RDATA_WF_REMAINDER,
88 	   "get ANY descriptor 1. item type");
89 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
90 	   "get ANY descriptor 2. item type");
91 
92 	ret = knot_rrtype_to_string(255, name, BUF_LEN);
93 	ok(ret != -1, "get ANY ret");
94 	ok(strcmp(name, "ANY") == 0, "get ANY name");
95 
96 	// 6. TYPE65535
97 	descr = knot_get_rdata_descriptor(65535);
98 	ok(descr->type_name == 0, "get TYPE65535 descriptor name");
99 	ok(descr->block_types[0] == KNOT_RDATA_WF_REMAINDER,
100 	   "get TYPE65535 descriptor 1. item type");
101 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
102 	   "get TYPE65535 descriptor 2. item type");
103 
104 	ret = knot_rrtype_to_string(65535, name, BUF_LEN);
105 	ok(ret != -1, "get TYPE65535 ret");
106 	ok(strcmp(name, "TYPE65535") == 0, "get TYPE65535 name");
107 
108 	// Class num to string:
109 	// 7. CLASS0
110 	ret = knot_rrclass_to_string(0, name, BUF_LEN);
111 	ok(ret != -1, "get CLASS0 ret");
112 	ok(strcmp(name, "CLASS0") == 0, "get CLASS0 name");
113 
114 	// 8. IN
115 	ret = knot_rrclass_to_string(1, name, BUF_LEN);
116 	ok(ret != -1, "get IN ret");
117 	ok(strcmp(name, "IN") == 0, "get IN name");
118 
119 	// 9. ANY
120 	ret = knot_rrclass_to_string(255, name, BUF_LEN);
121 	ok(ret != -1, "get ANY ret");
122 	ok(strcmp(name, "ANY") == 0, "get ANY name");
123 
124 	// 10. CLASS65535
125 	ret = knot_rrclass_to_string(65535, name, BUF_LEN);
126 	ok(ret != -1, "get CLASS65535 ret");
127 	ok(strcmp(name, "CLASS65535") == 0, "get CLASS65535 name");
128 
129 	// String to type num:
130 	// 11. A
131 	ret = knot_rrtype_from_string("A", &num);
132 	ok(ret != -1, "get A num ret");
133 	ok(num == 1, "get A num");
134 
135 	// 12. a
136 	ret = knot_rrtype_from_string("a", &num);
137 	ok(ret != -1, "get a num ret");
138 	ok(num == 1, "get a num");
139 
140 	// 13. AaAa
141 	ret = knot_rrtype_from_string("AaAa", &num);
142 	ok(ret != -1, "get AaAa num ret");
143 	ok(num == 28, "get AaAa num");
144 
145 	// 14. ""
146 	ret = knot_rrtype_from_string("", &num);
147 	ok(ret == -1, "get "" num ret");
148 
149 	// 15. DUMMY
150 	ret = knot_rrtype_from_string("DUMMY", &num);
151 	ok(ret == -1, "get DUMMY num ret");
152 
153 	// 16. TypE33
154 	ret = knot_rrtype_from_string("TypE33", &num);
155 	ok(ret != -1, "get TypE33 num ret");
156 	ok(num == 33, "get TypE33 num");
157 
158 	// 17. TYPE
159 	ret = knot_rrtype_from_string("TYPE", &num);
160 	ok(ret == -1, "get TYPE num ret");
161 
162 	// 18. TYPE0
163 	ret = knot_rrtype_from_string("TYPE0", &num);
164 	ok(ret != -1, "get TYPE0 num ret");
165 	ok(num == 0, "get TYPE0 num");
166 
167 	// 19. TYPE65535
168 	ret = knot_rrtype_from_string("TYPE65535", &num);
169 	ok(ret != -1, "get TYPE65535 num ret");
170 	ok(num == 65535, "get TYPE65535 num");
171 
172 	// 20. TYPE65536
173 	ret = knot_rrtype_from_string("TYPE65536", &num);
174 	ok(ret == -1, "get TYPE65536 num ret");
175 
176 	// String to class num:
177 	// 21. In
178 	ret = knot_rrclass_from_string("In", &num);
179 	ok(ret != -1, "get In num ret");
180 	ok(num == 1, "get In num");
181 
182 	// 22. ANY
183 	ret = knot_rrclass_from_string("ANY", &num);
184 	ok(ret != -1, "get ANY num ret");
185 	ok(num == 255, "get ANY num");
186 
187 	// 23. ""
188 	ret = knot_rrclass_from_string("", &num);
189 	ok(ret == -1, "get "" num ret");
190 
191 	// 24. DUMMY
192 	ret = knot_rrclass_from_string("DUMMY", &num);
193 	ok(ret == -1, "get DUMMY num ret");
194 
195 	// 25. CLass33
196 	ret = knot_rrclass_from_string("CLass33", &num);
197 	ok(ret != -1, "get CLass33 num ret");
198 	ok(num == 33, "get CLass33 num");
199 
200 	// 26. CLASS
201 	ret = knot_rrclass_from_string("CLASS", &num);
202 	ok(ret == -1, "get CLASS num ret");
203 
204 	// 27. CLASS0
205 	ret = knot_rrclass_from_string("CLASS0", &num);
206 	ok(ret != -1, "get CLASS0 num ret");
207 	ok(num == 0, "get CLASS0 num");
208 
209 	// 28. CLASS65535
210 	ret = knot_rrclass_from_string("CLASS65535", &num);
211 	ok(ret != -1, "get CLASS65535 num ret");
212 	ok(num == 65535, "get CLASS65535 num");
213 
214 	// 29. CLASS65536
215 	ret = knot_rrclass_from_string("CLASS65536", &num);
216 	ok(ret == -1, "get CLASS65536 num ret");
217 
218 	// Get obsolete descriptor:
219 	// 30. TYPE0
220 	descr = knot_get_obsolete_rdata_descriptor(0);
221 	ok(descr->type_name == 0, "get TYPE0 descriptor name");
222 	ok(descr->block_types[0] == KNOT_RDATA_WF_REMAINDER,
223 	   "get TYPE0 descriptor 1. item type");
224 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
225 	   "get TYPE0 descriptor 2. item type");
226 
227 	// 31. MD
228 	descr = knot_get_obsolete_rdata_descriptor(3);
229 	ok(strcmp(descr->type_name, "MD") == 0, "get MD descriptor name");
230 	ok(descr->block_types[0] == KNOT_RDATA_WF_DECOMPRESSIBLE_DNAME,
231 	   "get A descriptor 1. item type");
232 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
233 	   "get A descriptor 2. item type");
234 
235 	// 32. NXT
236 	descr = knot_get_obsolete_rdata_descriptor(30);
237 	ok(strcmp(descr->type_name, "NXT") == 0, "get NXT descriptor name");
238 	ok(descr->block_types[0] == KNOT_RDATA_WF_DECOMPRESSIBLE_DNAME,
239 	   "get CNAME descriptor 1. item type");
240 	ok(descr->block_types[1] == KNOT_RDATA_WF_REMAINDER,
241 	   "get CNAME descriptor 2. item type");
242 	ok(descr->block_types[2] == KNOT_RDATA_WF_END,
243 	   "get CNAME descriptor 3. item type");
244 
245 	// 33. TYPE38 (A6)
246 	descr = knot_get_obsolete_rdata_descriptor(38);
247 	ok(descr->type_name == 0, "get TYPE38 descriptor name");
248 	ok(descr->block_types[0] == KNOT_RDATA_WF_REMAINDER,
249 	   "get TYPE38 descriptor 1. item type");
250 	ok(descr->block_types[1] == KNOT_RDATA_WF_END,
251 	   "get TYPE38 descriptor 2. item type");
252 
253 	// knot_rrtype_to_string invalid output buffer size
254 	ret = knot_rrtype_to_string(1, NULL, 0);
255 	ok(ret == -1, "knot_rrtype_to_string: invalid output buffer size");
256 
257 	// knot_rrclass_to_string invalid output buffer size
258 	ret = knot_rrclass_to_string(1, NULL, 0);
259 	ok(ret == -1, "knot_rrclass_to_string: invalid output buffer size");
260 
261 	// knot_rrtype_is_metatype
262 	ok(knot_rrtype_is_metatype(0) == 0,
263 	   "rrtype is not metatype");
264 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_SIG) != 0,
265 	   "rrtype is SIG");
266 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_OPT) != 0,
267 	   "rrtype is OPT");
268 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_TKEY) != 0,
269 	   "rrtype is TKEY");
270 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_TSIG) != 0,
271 	   "rrtype is TSIG");
272 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_IXFR) != 0,
273 	   "rrtype is IXFR");
274 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_AXFR) != 0,
275 	   "rrtype is AXFR");
276 	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_ANY) != 0,
277 	   "rrtype is ANY");
278 
279 	// knot_rrtype_is_dnssec
280 	ok(knot_rrtype_is_dnssec(0) == 0,
281 	   "rrtype is not DNSSEC");
282 	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_DNSKEY) != 0,
283 	   "rrtype is DNSKEY");
284 	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_RRSIG) != 0,
285 	   "rrtype is RRSIG");
286 	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_NSEC) != 0,
287 	   "rrtype is NSEC");
288 	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_NSEC3) != 0,
289 	   "rrtype is NSEC3");
290 	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_NSEC3PARAM) != 0,
291 	   "rrtype is NSEC3PARAM");
292 	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_CDNSKEY) != 0,
293 	   "rrtype is CDNSKEY");
294 
295 	// knot_rrtype_additional_needed
296 	ok(knot_rrtype_additional_needed(0) == 0,
297 	   "rrtype is not additional needed");
298 	ok(knot_rrtype_additional_needed(KNOT_RRTYPE_NS) != 0,
299 	   "rrtype is NS");
300 	ok(knot_rrtype_additional_needed(KNOT_RRTYPE_MX) != 0,
301 	   "rrtype is MX");
302 	ok(knot_rrtype_additional_needed(KNOT_RRTYPE_SRV) != 0,
303 	   "rrtype is SRV");
304 
305 	// knot_rrtype_should_be_lowercased
306 	ok(knot_rrtype_should_be_lowercased(0) == 0,
307 	   "rrtype should not be lowercased");
308 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_NS) != 0,
309 	   "rrtype is NS");
310 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MD) != 0,
311 	   "rrtype is MD");
312 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MF) != 0,
313 	   "rrtype is MF");
314 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_CNAME) != 0,
315 	   "rrtype is CNAME");
316 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_SOA) != 0,
317 	   "rrtype is SOA");
318 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MB) != 0,
319 	   "rrtype is MB");
320 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MG) != 0,
321 	   "rrtype is MG");
322 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MR) != 0,
323 	   "rrtype is MR");
324 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_PTR) != 0,
325 	   "rrtype is PTR");
326 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MINFO) != 0,
327 	   "rrtype is MINFO");
328 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MX) != 0,
329 	   "rrtype is MX");
330 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_RP) != 0,
331 	   "rrtype is RP");
332 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_AFSDB) != 0,
333 	   "rrtype is AFSDB");
334 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_RT) != 0,
335 	   "rrtype is RT");
336 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_SIG) != 0,
337 	   "rrtype is SIG");
338 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_PX) != 0,
339 	   "rrtype is PX");
340 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_NXT) != 0,
341 	   "rrtype is NXT");
342 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_NAPTR) != 0,
343 	   "rrtype is NAPTR");
344 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_KX) != 0,
345 	   "rrtype is KX");
346 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_SRV) != 0,
347 	   "rrtype is SRV");
348 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_DNAME) != 0,
349 	   "rrtype is DNAME");
350 	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_RRSIG) != 0,
351 	   "rrtype is RRSIG");
352 
353 	ret = knot_opt_code_to_string(0, name, BUF_LEN);
354 	ok(ret != -1 && strcmp(name, "CODE0") == 0, "opt to str, code 0");
355 	ret = knot_opt_code_to_string(10, name, BUF_LEN);
356 	ok(ret != -1 && strcmp(name, "COOKIE") == 0, "opt to str, code 10");
357 	ret = knot_opt_code_to_string(65535, name, BUF_LEN);
358 	ok(ret != -1 && strcmp(name, "CODE65535") == 0, "opt to str, code 65535");
359 
360 	return 0;
361 }
362