test_descriptor.c 11.9 KB
Newer Older
1
/*  Copyright (C) 2018 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 17 18 19

    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 <stdlib.h>
#include <stdint.h>
#include <string.h>
20
#include <tap/basic.h>
21

22
#include "libknot/descriptor.h"
23 24 25 26 27

#define BUF_LEN 256

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

30
	const    knot_rdata_descriptor_t *descr;
31
	char     name[BUF_LEN] = { 0 };
32 33 34 35 36
	int      ret;
	uint16_t num;

	// Get descriptor, type num to string:
	// 1. TYPE0
37
	descr = knot_get_rdata_descriptor(0);
38 39
	ok(descr->type_name == 0, "get TYPE0 descriptor name");
	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 45 46 47 48

	ret = knot_rrtype_to_string(0, name, BUF_LEN);
	ok(ret != -1, "get TYPE0 ret");
	ok(strcmp(name, "TYPE0") == 0, "get TYPE0 name");

	// 2. A
49
	descr = knot_get_rdata_descriptor(1);
50 51
	ok(strcmp(descr->type_name, "A") == 0, "get A descriptor name");
	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 57 58 59 60

	ret = knot_rrtype_to_string(1, name, BUF_LEN);
	ok(ret != -1, "get A ret");
	ok(strcmp(name, "A") == 0, "get A name");

	// 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 69 70 71 72

	ret = knot_rrtype_to_string(5, name, BUF_LEN);
	ok(ret != -1, "get CNAME ret");
	ok(strcmp(name, "CNAME") == 0, "get CNAME name");

	// 4. TYPE38 (A6)
73
	descr = knot_get_rdata_descriptor(38);
74 75
	ok(descr->type_name == 0, "get TYPE38 descriptor name");
	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 81 82 83 84

	ret = knot_rrtype_to_string(38, name, BUF_LEN);
	ok(ret != -1, "get TYPE38 ret");
	ok(strcmp(name, "TYPE38") == 0, "get TYPE38 name");

	// 5. ANY
85
	descr = knot_get_rdata_descriptor(255);
86 87
	ok(strcmp(descr->type_name, "ANY") == 0, "get ANY descriptor name");
	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 93 94 95

	ret = knot_rrtype_to_string(255, name, BUF_LEN);
	ok(ret != -1, "get ANY ret");
	ok(strcmp(name, "ANY") == 0, "get ANY name");

96 97 98
	// 6. TYPE65535
	descr = knot_get_rdata_descriptor(65535);
	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 105 106
	ret = knot_rrtype_to_string(65535, name, BUF_LEN);
	ok(ret != -1, "get TYPE65535 ret");
	ok(strcmp(name, "TYPE65535") == 0, "get TYPE65535 name");
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

	// Class num to string:
	// 7. CLASS0
	ret = knot_rrclass_to_string(0, name, BUF_LEN);
	ok(ret != -1, "get CLASS0 ret");
	ok(strcmp(name, "CLASS0") == 0, "get CLASS0 name");

	// 8. IN
	ret = knot_rrclass_to_string(1, name, BUF_LEN);
	ok(ret != -1, "get IN ret");
	ok(strcmp(name, "IN") == 0, "get IN name");

	// 9. ANY
	ret = knot_rrclass_to_string(255, name, BUF_LEN);
	ok(ret != -1, "get ANY ret");
	ok(strcmp(name, "ANY") == 0, "get ANY name");

	// 10. CLASS65535
	ret = knot_rrclass_to_string(65535, name, BUF_LEN);
	ok(ret != -1, "get CLASS65535 ret");
	ok(strcmp(name, "CLASS65535") == 0, "get CLASS65535 name");

	// String to type num:
	// 11. A
	ret = knot_rrtype_from_string("A", &num);
	ok(ret != -1, "get A num ret");
	ok(num == 1, "get A num");

	// 12. a
	ret = knot_rrtype_from_string("a", &num);
	ok(ret != -1, "get a num ret");
	ok(num == 1, "get a num");

	// 13. AaAa
	ret = knot_rrtype_from_string("AaAa", &num);
	ok(ret != -1, "get AaAa num ret");
	ok(num == 28, "get AaAa num");

	// 14. ""
	ret = knot_rrtype_from_string("", &num);
	ok(ret == -1, "get "" num ret");

	// 15. DUMMY
	ret = knot_rrtype_from_string("DUMMY", &num);
	ok(ret == -1, "get DUMMY num ret");

	// 16. TypE33
	ret = knot_rrtype_from_string("TypE33", &num);
	ok(ret != -1, "get TypE33 num ret");
	ok(num == 33, "get TypE33 num");

	// 17. TYPE
	ret = knot_rrtype_from_string("TYPE", &num);
	ok(ret == -1, "get TYPE num ret");

	// 18. TYPE0
	ret = knot_rrtype_from_string("TYPE0", &num);
	ok(ret != -1, "get TYPE0 num ret");
	ok(num == 0, "get TYPE0 num");

	// 19. TYPE65535
	ret = knot_rrtype_from_string("TYPE65535", &num);
	ok(ret != -1, "get TYPE65535 num ret");
	ok(num == 65535, "get TYPE65535 num");

	// 20. TYPE65536
	ret = knot_rrtype_from_string("TYPE65536", &num);
	ok(ret == -1, "get TYPE65536 num ret");

	// String to class num:
	// 21. In
	ret = knot_rrclass_from_string("In", &num);
	ok(ret != -1, "get In num ret");
	ok(num == 1, "get In num");

	// 22. ANY
	ret = knot_rrclass_from_string("ANY", &num);
	ok(ret != -1, "get ANY num ret");
	ok(num == 255, "get ANY num");

	// 23. ""
	ret = knot_rrclass_from_string("", &num);
	ok(ret == -1, "get "" num ret");

	// 24. DUMMY
	ret = knot_rrclass_from_string("DUMMY", &num);
	ok(ret == -1, "get DUMMY num ret");

	// 25. CLass33
	ret = knot_rrclass_from_string("CLass33", &num);
	ok(ret != -1, "get CLass33 num ret");
	ok(num == 33, "get CLass33 num");

	// 26. CLASS
	ret = knot_rrclass_from_string("CLASS", &num);
	ok(ret == -1, "get CLASS num ret");

	// 27. CLASS0
	ret = knot_rrclass_from_string("CLASS0", &num);
	ok(ret != -1, "get CLASS0 num ret");
	ok(num == 0, "get CLASS0 num");

	// 28. CLASS65535
	ret = knot_rrclass_from_string("CLASS65535", &num);
	ok(ret != -1, "get CLASS65535 num ret");
	ok(num == 65535, "get CLASS65535 num");

	// 29. CLASS65536
	ret = knot_rrclass_from_string("CLASS65536", &num);
	ok(ret == -1, "get CLASS65536 num ret");

	// Get obsolete descriptor:
	// 30. TYPE0
220
	descr = knot_get_obsolete_rdata_descriptor(0);
221 222
	ok(descr->type_name == 0, "get TYPE0 descriptor name");
	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 248
	ok(descr->type_name == 0, "get TYPE38 descriptor name");
	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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	   "get TYPE38 descriptor 2. item type");

	// knot_rrtype_to_string invalid output buffer size
	ret = knot_rrtype_to_string(1, NULL, 0);
	ok(ret == -1, "knot_rrtype_to_string: invalid output buffer size");

	// knot_rrclass_to_string invalid output buffer size
	ret = knot_rrclass_to_string(1, NULL, 0);
	ok(ret == -1, "knot_rrclass_to_string: invalid output buffer size");

	// knot_rrtype_is_metatype
	ok(knot_rrtype_is_metatype(0) == 0,
	   "rrtype is not metatype");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_SIG) != 0,
	   "rrtype is SIG");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_OPT) != 0,
	   "rrtype is OPT");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_TKEY) != 0,
	   "rrtype is TKEY");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_TSIG) != 0,
	   "rrtype is TSIG");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_IXFR) != 0,
	   "rrtype is IXFR");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_AXFR) != 0,
	   "rrtype is AXFR");
	ok(knot_rrtype_is_metatype(KNOT_RRTYPE_ANY) != 0,
	   "rrtype is ANY");

279 280 281 282 283 284
	// knot_rrtype_is_dnssec
	ok(knot_rrtype_is_dnssec(0) == 0,
	   "rrtype is not DNSSEC");
	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_DNSKEY) != 0,
	   "rrtype is DNSKEY");
	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 291
	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_NSEC3PARAM) != 0,
	   "rrtype is NSEC3PARAM");
292 293
	ok(knot_rrtype_is_dnssec(KNOT_RRTYPE_CDNSKEY) != 0,
	   "rrtype is CDNSKEY");
294 295 296 297 298 299 300 301 302 303

	// knot_rrtype_additional_needed
	ok(knot_rrtype_additional_needed(0) == 0,
	   "rrtype is not additional needed");
	ok(knot_rrtype_additional_needed(KNOT_RRTYPE_NS) != 0,
	   "rrtype is NS");
	ok(knot_rrtype_additional_needed(KNOT_RRTYPE_MX) != 0,
	   "rrtype is MX");
	ok(knot_rrtype_additional_needed(KNOT_RRTYPE_SRV) != 0,
	   "rrtype is SRV");
304

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	// knot_rrtype_should_be_lowercased
	ok(knot_rrtype_should_be_lowercased(0) == 0,
	   "rrtype should not be lowercased");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_NS) != 0,
	   "rrtype is NS");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MD) != 0,
	   "rrtype is MD");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MF) != 0,
	   "rrtype is MF");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_CNAME) != 0,
	   "rrtype is CNAME");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_SOA) != 0,
	   "rrtype is SOA");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MB) != 0,
	   "rrtype is MB");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MG) != 0,
	   "rrtype is MG");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MR) != 0,
	   "rrtype is MR");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_PTR) != 0,
	   "rrtype is PTR");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MINFO) != 0,
	   "rrtype is MINFO");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_MX) != 0,
	   "rrtype is MX");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_RP) != 0,
	   "rrtype is RP");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_AFSDB) != 0,
	   "rrtype is AFSDB");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_RT) != 0,
	   "rrtype is RT");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_SIG) != 0,
	   "rrtype is SIG");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_PX) != 0,
	   "rrtype is PX");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_NXT) != 0,
	   "rrtype is NXT");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_NAPTR) != 0,
	   "rrtype is NAPTR");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_KX) != 0,
	   "rrtype is KX");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_SRV) != 0,
	   "rrtype is SRV");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_DNAME) != 0,
	   "rrtype is DNAME");
	ok(knot_rrtype_should_be_lowercased(KNOT_RRTYPE_RRSIG) != 0,
	   "rrtype is RRSIG");

353 354 355 356 357 358 359
	ret = knot_opt_code_to_string(0, name, BUF_LEN);
	ok(ret != -1 && strcmp(name, "CODE0") == 0, "opt to str, code 0");
	ret = knot_opt_code_to_string(10, name, BUF_LEN);
	ok(ret != -1 && strcmp(name, "COOKIE") == 0, "opt to str, code 10");
	ret = knot_opt_code_to_string(65535, name, BUF_LEN);
	ok(ret != -1 && strcmp(name, "CODE65535") == 0, "opt to str, code 65535");

360 361
	return 0;
}