rdata_test.c 80.3 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11
 */

Evan Hunt's avatar
Evan Hunt committed
12 13
#if HAVE_CMOCKA

14
#include <sched.h> /* IWYU pragma: keep */
15 16
#include <setjmp.h>
#include <stdarg.h>
17
#include <stdbool.h>
18
#include <stddef.h>
Evan Hunt's avatar
Evan Hunt committed
19 20
#include <stdlib.h>
#include <string.h>
21 22
#include <unistd.h>

Evan Hunt's avatar
Evan Hunt committed
23 24 25
#define UNIT_TESTING
#include <cmocka.h>

26
#include <isc/hex.h>
27
#include <isc/lex.h>
28
#include <isc/print.h>
Evan Hunt's avatar
Evan Hunt committed
29
#include <isc/stdio.h>
30
#include <isc/types.h>
Evan Hunt's avatar
Evan Hunt committed
31
#include <isc/util.h>
32 33 34 35 36

#include <dns/rdata.h>

#include "dnstest.h"

Mark Andrews's avatar
Mark Andrews committed
37 38
static bool debug = false;

39 40 41 42
/*
 * An array of these structures is passed to compare_ok().
 */
struct compare_ok {
Evan Hunt's avatar
Evan Hunt committed
43 44 45 46
	const char *text1; /* text passed to fromtext_*() */
	const char *text2; /* text passed to fromtext_*() */
	int answer;	   /* -1, 0, 1 */
	int lineno;	   /* source line defining this RDATA */
47 48
};
typedef struct compare_ok compare_ok_t;
49

Evan Hunt's avatar
Evan Hunt committed
50
static int
Evan Hunt's avatar
Evan Hunt committed
51
_setup(void **state) {
Evan Hunt's avatar
Evan Hunt committed
52 53 54 55 56 57 58 59 60 61 62
	isc_result_t result;

	UNUSED(state);

	result = dns_test_begin(NULL, false);
	assert_int_equal(result, ISC_R_SUCCESS);

	return (0);
}

static int
Evan Hunt's avatar
Evan Hunt committed
63
_teardown(void **state) {
Evan Hunt's avatar
Evan Hunt committed
64 65 66 67 68 69 70
	UNUSED(state);

	dns_test_end();

	return (0);
}

71 72 73
/*
 * An array of these structures is passed to check_text_ok().
 */
Evan Hunt's avatar
Evan Hunt committed
74
typedef struct text_ok {
75 76
	const char *text_in;  /* text passed to fromtext_*() */
	const char *text_out; /* text expected from totext_*();
77
			       * NULL indicates text_in is invalid */
78
	unsigned int loop;
Evan Hunt's avatar
Evan Hunt committed
79
} text_ok_t;
80 81 82 83

/*
 * An array of these structures is passed to check_wire_ok().
 */
Evan Hunt's avatar
Evan Hunt committed
84
typedef struct wire_ok {
85
	unsigned char data[512]; /* RDATA in wire format */
Evan Hunt's avatar
Evan Hunt committed
86 87
	size_t len;		 /* octets of data to parse */
	bool ok;		 /* is this RDATA valid? */
88
	unsigned int loop;
Evan Hunt's avatar
Evan Hunt committed
89
} wire_ok_t;
90

91 92 93 94 95 96 97 98
#define COMPARE(r1, r2, answer)          \
	{                                \
		r1, r2, answer, __LINE__ \
	}
#define COMPARE_SENTINEL()              \
	{                               \
		NULL, NULL, 0, __LINE__ \
	}
99

100
#define TEXT_VALID_CHANGED(data_in, data_out) \
101
	{                                     \
102
		data_in, data_out, 0          \
103
	}
104 105 106
#define TEXT_VALID(data)      \
	{                     \
		data, data, 0 \
107
	}
108 109 110 111 112 113 114 115 116 117 118
#define TEXT_VALID_LOOP(loop, data) \
	{                           \
		data, data, loop    \
	}
#define TEXT_VALID_LOOPCHG(loop, data_in, data_out) \
	{                                           \
		data_in, data_out, loop             \
	}
#define TEXT_INVALID(data)    \
	{                     \
		data, NULL, 0 \
119 120 121 122
	}
#define TEXT_SENTINEL() TEXT_INVALID(NULL)

#define VARGC(...) (sizeof((unsigned char[]){ __VA_ARGS__ }))
123 124 125
#define WIRE_TEST(ok, loop, ...)                              \
	{                                                     \
		{ __VA_ARGS__ }, VARGC(__VA_ARGS__), ok, loop \
126
	}
127 128
#define WIRE_VALID(...)		   WIRE_TEST(true, 0, __VA_ARGS__)
#define WIRE_VALID_LOOP(loop, ...) WIRE_TEST(true, loop, __VA_ARGS__)
129 130 131 132 133 134
/*
 * WIRE_INVALID() test cases must always have at least one octet specified to
 * distinguish them from WIRE_SENTINEL().  Use the 'empty_ok' parameter passed
 * to check_wire_ok() for indicating whether empty RDATA is allowed for a given
 * RR type or not.
 */
135 136
#define WIRE_INVALID(FIRST, ...) WIRE_TEST(false, 0, FIRST, __VA_ARGS__)
#define WIRE_SENTINEL()		 WIRE_TEST(false, 0)
137

138 139 140 141 142 143 144
/*
 * Call dns_rdata_fromwire() for data in 'src', which is 'srclen' octets in
 * size and represents RDATA of given 'type' and 'class'.  Store the resulting
 * uncompressed wire form in 'dst', which is 'dstlen' octets in size, and make
 * 'rdata' refer to that uncompressed wire form.
 */
static isc_result_t
145 146
wire_to_rdata(const unsigned char *src, size_t srclen, dns_rdataclass_t rdclass,
	      dns_rdatatype_t type, unsigned char *dst, size_t dstlen,
Evan Hunt's avatar
Evan Hunt committed
147 148
	      dns_rdata_t *rdata) {
	isc_buffer_t source, target;
149
	dns_decompress_t dctx;
Evan Hunt's avatar
Evan Hunt committed
150
	isc_result_t result;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

	/*
	 * Set up len-octet buffer pointing at data.
	 */
	isc_buffer_constinit(&source, src, srclen);
	isc_buffer_add(&source, srclen);
	isc_buffer_setactive(&source, srclen);

	/*
	 * Initialize target buffer.
	 */
	isc_buffer_init(&target, dst, dstlen);

	/*
	 * Try converting input data into uncompressed wire form.
	 */
	dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY);
	result = dns_rdata_fromwire(rdata, rdclass, type, &source, &dctx, 0,
				    &target);
	dns_decompress_invalidate(&dctx);

	return (result);
}

175 176 177 178
/*
 * Call dns_rdata_towire() for rdata and write to result to dst.
 */
static isc_result_t
179
rdata_towire(dns_rdata_t *rdata, unsigned char *dst, size_t dstlen,
Evan Hunt's avatar
Evan Hunt committed
180 181
	     size_t *length) {
	isc_buffer_t target;
182
	dns_compress_t cctx;
Evan Hunt's avatar
Evan Hunt committed
183
	isc_result_t result;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

	/*
	 * Initialize target buffer.
	 */
	isc_buffer_init(&target, dst, dstlen);

	/*
	 * Try converting input data into uncompressed wire form.
	 */
	dns_compress_init(&cctx, -1, dt_mctx);
	result = dns_rdata_towire(rdata, &cctx, &target);
	dns_compress_invalidate(&cctx);

	*length = isc_buffer_usedlength(&target);

	return (result);
}

202
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
203
additionaldata_cb(void *arg, const dns_name_t *name, dns_rdatatype_t qtype) {
204 205 206 207 208 209 210 211 212 213
	UNUSED(arg);
	UNUSED(name);
	UNUSED(qtype);
	return (ISC_R_SUCCESS);
}

/*
 * call dns_rdata_additionaldata() for rdata.
 */
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
214
rdata_additionadata(dns_rdata_t *rdata) {
215 216 217
	return (dns_rdata_additionaldata(rdata, additionaldata_cb, NULL));
}

Mark Andrews's avatar
Mark Andrews committed
218 219 220 221 222 223 224
/*
 * Call dns_rdata_checknames() with various owner names chosen to
 * match well known forms.
 *
 * We are currently only checking that the calls do not trigger
 * assertion failures.
 *
225
 * XXXMPA A future extension could be to record the expected
Mark Andrews's avatar
Mark Andrews committed
226 227 228
 * result and the expected value of 'bad'.
 */
static void
Evan Hunt's avatar
Evan Hunt committed
229
rdata_checknames(dns_rdata_t *rdata) {
Mark Andrews's avatar
Mark Andrews committed
230
	dns_fixedname_t fixed, bfixed;
Evan Hunt's avatar
Evan Hunt committed
231 232
	dns_name_t *name, *bad;
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
233 234 235 236

	name = dns_fixedname_initname(&fixed);
	bad = dns_fixedname_initname(&bfixed);

237 238
	(void)dns_rdata_checknames(rdata, dns_rootname, NULL);
	(void)dns_rdata_checknames(rdata, dns_rootname, bad);
Mark Andrews's avatar
Mark Andrews committed
239 240 241

	result = dns_name_fromstring(name, "example.net", 0, NULL);
	assert_int_equal(result, ISC_R_SUCCESS);
242 243
	(void)dns_rdata_checknames(rdata, name, NULL);
	(void)dns_rdata_checknames(rdata, name, bad);
Mark Andrews's avatar
Mark Andrews committed
244 245 246

	result = dns_name_fromstring(name, "in-addr.arpa", 0, NULL);
	assert_int_equal(result, ISC_R_SUCCESS);
247 248
	(void)dns_rdata_checknames(rdata, name, NULL);
	(void)dns_rdata_checknames(rdata, name, bad);
Mark Andrews's avatar
Mark Andrews committed
249 250 251

	result = dns_name_fromstring(name, "ip6.arpa", 0, NULL);
	assert_int_equal(result, ISC_R_SUCCESS);
252 253
	(void)dns_rdata_checknames(rdata, name, NULL);
	(void)dns_rdata_checknames(rdata, name, bad);
Mark Andrews's avatar
Mark Andrews committed
254 255
}

256 257 258 259 260 261 262 263 264 265
/*
 * Test whether converting rdata to a type-specific struct and then back to
 * rdata results in the same uncompressed wire form.  This checks whether
 * tostruct_*() and fromstruct_*() routines for given RR class and type behave
 * consistently.
 *
 * This function is called for every correctly processed input RDATA, from both
 * check_text_ok_single() and check_wire_ok_single().
 */
static void
266 267
check_struct_conversions(dns_rdata_t *rdata, size_t structsize,
			 unsigned int loop) {
268
	dns_rdataclass_t rdclass = rdata->rdclass;
Evan Hunt's avatar
Evan Hunt committed
269 270 271 272 273
	dns_rdatatype_t type = rdata->type;
	isc_result_t result;
	isc_buffer_t target;
	void *rdata_struct;
	char buf[1024];
274
	unsigned int count = 0;
275

276
	rdata_struct = isc_mem_allocate(dt_mctx, structsize);
Evan Hunt's avatar
Evan Hunt committed
277
	assert_non_null(rdata_struct);
278 279 280 281 282

	/*
	 * Convert from uncompressed wire form into type-specific struct.
	 */
	result = dns_rdata_tostruct(rdata, rdata_struct, NULL);
Evan Hunt's avatar
Evan Hunt committed
283 284
	assert_int_equal(result, ISC_R_SUCCESS);

285 286 287 288 289 290
	/*
	 * Convert from type-specific struct into uncompressed wire form.
	 */
	isc_buffer_init(&target, buf, sizeof(buf));
	result = dns_rdata_fromstruct(NULL, rdclass, type, rdata_struct,
				      &target);
Evan Hunt's avatar
Evan Hunt committed
291 292
	assert_int_equal(result, ISC_R_SUCCESS);

293 294 295
	/*
	 * Ensure results are consistent.
	 */
Evan Hunt's avatar
Evan Hunt committed
296 297 298
	assert_int_equal(isc_buffer_usedlength(&target), rdata->length);

	assert_memory_equal(buf, rdata->data, rdata->length);
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	/*
	 * Check that one can walk hip rendezvous servers.
	 */
	switch (type) {
	case dns_rdatatype_hip: {
		dns_rdata_hip_t *hip = rdata_struct;

		for (result = dns_rdata_hip_first(hip); result == ISC_R_SUCCESS;
		     result = dns_rdata_hip_next(hip))
		{
			dns_name_t name;
			dns_name_init(&name, NULL);
			dns_rdata_hip_current(hip, &name);
			assert_int_not_equal(dns_name_countlabels(&name), 0);
			assert_true(dns_name_isabsolute(&name));
			count++;
		}
		assert_int_equal(result, ISC_R_NOMORE);
		assert_int_equal(count, loop);
		break;
	}
	}

323
	isc_mem_free(dt_mctx, rdata_struct);
324 325 326
}

/*
327 328 329
 * Check whether converting supplied text form RDATA into uncompressed wire
 * form succeeds (tests fromtext_*()).  If so, try converting it back into text
 * form and see if it results in the original text (tests totext_*()).
330 331 332
 */
static void
check_text_ok_single(const text_ok_t *text_ok, dns_rdataclass_t rdclass,
Evan Hunt's avatar
Evan Hunt committed
333
		     dns_rdatatype_t type, size_t structsize) {
334
	unsigned char buf_fromtext[1024], buf_fromwire[1024], buf_towire[1024];
Evan Hunt's avatar
Evan Hunt committed
335 336 337 338 339
	dns_rdata_t rdata = DNS_RDATA_INIT, rdata2 = DNS_RDATA_INIT;
	char buf_totext[1024] = { 0 };
	isc_buffer_t target;
	isc_result_t result;
	size_t length = 0;
340 341

	/*
342
	 * Try converting text form RDATA into uncompressed wire form.
343
	 */
344 345
	result = dns_test_rdatafromstring(&rdata, rdclass, type, buf_fromtext,
					  sizeof(buf_fromtext),
Evan Hunt's avatar
Evan Hunt committed
346
					  text_ok->text_in, false);
347 348 349
	/*
	 * Check whether result is as expected.
	 */
350
	if (text_ok->text_out != NULL) {
Mark Andrews's avatar
Mark Andrews committed
351 352 353
		if (debug && result != ISC_R_SUCCESS) {
			fprintf(stdout, "#'%s'\n", text_ok->text_in);
		}
Evan Hunt's avatar
Evan Hunt committed
354
		assert_int_equal(result, ISC_R_SUCCESS);
355
	} else {
Mark Andrews's avatar
Mark Andrews committed
356 357 358
		if (debug && result == ISC_R_SUCCESS) {
			fprintf(stdout, "#'%s'\n", text_ok->text_in);
		}
Evan Hunt's avatar
Evan Hunt committed
359
		assert_int_not_equal(result, ISC_R_SUCCESS);
360
	}
Evan Hunt's avatar
Evan Hunt committed
361

362
	/*
363 364
	 * If text form RDATA was not parsed correctly, performing any
	 * additional checks is pointless.
365
	 */
366 367 368
	if (result != ISC_R_SUCCESS) {
		return;
	}
369

370 371 372 373 374 375
	/*
	 * Try converting uncompressed wire form RDATA back into text form and
	 * check whether the resulting text is the same as the original one.
	 */
	isc_buffer_init(&target, buf_totext, sizeof(buf_totext));
	result = dns_rdata_totext(&rdata, NULL, &target);
Evan Hunt's avatar
Evan Hunt committed
376
	assert_int_equal(result, ISC_R_SUCCESS);
377 378 379 380 381 382
	/*
	 * Ensure buf_totext is properly NUL terminated as dns_rdata_totext()
	 * may attempt different output formats writing into the apparently
	 * unused part of the buffer.
	 */
	isc_buffer_putuint8(&target, 0);
Mark Andrews's avatar
Mark Andrews committed
383
	if (debug && strcmp(buf_totext, text_ok->text_out) != 0) {
384 385
		fprintf(stdout, "# '%s' != '%s'\n", buf_totext,
			text_ok->text_out);
Mark Andrews's avatar
Mark Andrews committed
386
	}
Evan Hunt's avatar
Evan Hunt committed
387 388
	assert_string_equal(buf_totext, text_ok->text_out);

389 390 391 392 393 394 395 396 397
	/*
	 * Ensure that fromtext_*() output is valid input for fromwire_*().
	 */
	result = wire_to_rdata(rdata.data, rdata.length, rdclass, type,
			       buf_fromwire, sizeof(buf_fromwire), &rdata2);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(rdata.length, rdata2.length);
	assert_memory_equal(rdata.data, buf_fromwire, rdata.length);

398 399 400 401 402 403 404 405
	/*
	 * Ensure that fromtext_*() output is valid input for towire_*().
	 */
	result = rdata_towire(&rdata, buf_towire, sizeof(buf_towire), &length);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(rdata.length, length);
	assert_memory_equal(rdata.data, buf_towire, length);

406 407 408 409 410 411
	/*
	 * Test that additionaldata_*() succeeded.
	 */
	result = rdata_additionadata(&rdata);
	assert_int_equal(result, ISC_R_SUCCESS);

Mark Andrews's avatar
Mark Andrews committed
412 413 414 415 416
	/*
	 * Exercise checknames_*().
	 */
	rdata_checknames(&rdata);

417 418 419 420
	/*
	 * Perform two-way conversion checks between uncompressed wire form and
	 * type-specific struct.
	 */
421
	check_struct_conversions(&rdata, structsize, text_ok->loop);
422 423
}

424 425 426 427 428 429 430 431 432 433
/*
 * Test whether converting rdata to text form and then parsing the result of
 * that conversion again results in the same uncompressed wire form.  This
 * checks whether totext_*() output is parsable by fromtext_*() for given RR
 * class and type.
 *
 * This function is called for every input RDATA which is successfully parsed
 * by check_wire_ok_single() and whose type is not a meta-type.
 */
static void
Evan Hunt's avatar
Evan Hunt committed
434 435
check_text_conversions(dns_rdata_t *rdata) {
	char buf_totext[1024] = { 0 };
436
	unsigned char buf_fromtext[1024];
Evan Hunt's avatar
Evan Hunt committed
437 438 439
	isc_result_t result;
	isc_buffer_t target;
	dns_rdata_t rdata2 = DNS_RDATA_INIT;
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

	/*
	 * Convert uncompressed wire form RDATA into text form.  This
	 * conversion must succeed since input RDATA was successfully
	 * parsed by check_wire_ok_single().
	 */
	isc_buffer_init(&target, buf_totext, sizeof(buf_totext));
	result = dns_rdata_totext(rdata, NULL, &target);
	assert_int_equal(result, ISC_R_SUCCESS);
	/*
	 * Ensure buf_totext is properly NUL terminated as dns_rdata_totext()
	 * may attempt different output formats writing into the apparently
	 * unused part of the buffer.
	 */
	isc_buffer_putuint8(&target, 0);
Mark Andrews's avatar
Mark Andrews committed
455 456 457
	if (debug) {
		fprintf(stdout, "#'%s'\n", buf_totext);
	}
458 459 460 461 462 463 464 465 466 467 468 469

	/*
	 * Try parsing text form RDATA output by dns_rdata_totext() again.
	 */
	result = dns_test_rdatafromstring(&rdata2, rdata->rdclass, rdata->type,
					  buf_fromtext, sizeof(buf_fromtext),
					  buf_totext, false);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(rdata2.length, rdata->length);
	assert_memory_equal(buf_fromtext, rdata->data, rdata->length);
}

470 471 472 473 474 475 476 477 478 479
/*
 * Test whether converting rdata to multi-line text form and then parsing the
 * result of that conversion again results in the same uncompressed wire form.
 * This checks whether multi-line totext_*() output is parsable by fromtext_*()
 * for given RR class and type.
 *
 * This function is called for every input RDATA which is successfully parsed
 * by check_wire_ok_single() and whose type is not a meta-type.
 */
static void
Evan Hunt's avatar
Evan Hunt committed
480 481
check_multiline_text_conversions(dns_rdata_t *rdata) {
	char buf_totext[1024] = { 0 };
482
	unsigned char buf_fromtext[1024];
Evan Hunt's avatar
Evan Hunt committed
483 484 485 486
	isc_result_t result;
	isc_buffer_t target;
	dns_rdata_t rdata2 = DNS_RDATA_INIT;
	unsigned int flags;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

	/*
	 * Convert uncompressed wire form RDATA into multi-line text form.
	 * This conversion must succeed since input RDATA was successfully
	 * parsed by check_wire_ok_single().
	 */
	isc_buffer_init(&target, buf_totext, sizeof(buf_totext));
	flags = dns_master_styleflags(&dns_master_style_default);
	result = dns_rdata_tofmttext(rdata, dns_rootname, flags, 80 - 32, 4,
				     "\n", &target);
	assert_int_equal(result, ISC_R_SUCCESS);
	/*
	 * Ensure buf_totext is properly NUL terminated as
	 * dns_rdata_tofmttext() may attempt different output formats
	 * writing into the apparently unused part of the buffer.
	 */
	isc_buffer_putuint8(&target, 0);
Mark Andrews's avatar
Mark Andrews committed
504 505 506
	if (debug) {
		fprintf(stdout, "#'%s'\n", buf_totext);
	}
507 508 509 510 511 512 513 514 515 516 517 518 519

	/*
	 * Try parsing multi-line text form RDATA output by
	 * dns_rdata_tofmttext() again.
	 */
	result = dns_test_rdatafromstring(&rdata2, rdata->rdclass, rdata->type,
					  buf_fromtext, sizeof(buf_fromtext),
					  buf_totext, false);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(rdata2.length, rdata->length);
	assert_memory_equal(buf_fromtext, rdata->data, rdata->length);
}

520
/*
521 522
 * Test whether supplied wire form RDATA is properly handled as being either
 * valid or invalid for an RR of given rdclass and type.
523 524 525
 */
static void
check_wire_ok_single(const wire_ok_t *wire_ok, dns_rdataclass_t rdclass,
Evan Hunt's avatar
Evan Hunt committed
526
		     dns_rdatatype_t type, size_t structsize) {
527
	unsigned char buf[1024], buf_towire[1024];
Evan Hunt's avatar
Evan Hunt committed
528 529 530
	isc_result_t result;
	dns_rdata_t rdata = DNS_RDATA_INIT;
	size_t length = 0;
531 532 533 534

	/*
	 * Try converting wire data into uncompressed wire form.
	 */
535 536
	result = wire_to_rdata(wire_ok->data, wire_ok->len, rdclass, type, buf,
			       sizeof(buf), &rdata);
537 538 539
	/*
	 * Check whether result is as expected.
	 */
540
	if (wire_ok->ok) {
Evan Hunt's avatar
Evan Hunt committed
541
		assert_int_equal(result, ISC_R_SUCCESS);
542
	} else {
Evan Hunt's avatar
Evan Hunt committed
543
		assert_int_not_equal(result, ISC_R_SUCCESS);
544
	}
545 546 547 548 549

	if (result != ISC_R_SUCCESS) {
		return;
	}

550
	/*
551 552
	 * If data was parsed correctly, perform two-way conversion checks
	 * between uncompressed wire form and type-specific struct.
553 554
	 *
	 * If the RR type is not a meta-type, additionally perform two-way
555 556 557 558
	 * conversion checks between:
	 *
	 *   - uncompressed wire form and text form,
	 *   - uncompressed wire form and multi-line text form.
559
	 */
560
	check_struct_conversions(&rdata, structsize, wire_ok->loop);
561 562 563
	if (!dns_rdatatype_ismeta(rdata.type)) {
		check_text_conversions(&rdata);
		check_multiline_text_conversions(&rdata);
564
	}
565 566 567 568 569 570 571 572

	/*
	 * Ensure that fromwire_*() output is valid input for towire_*().
	 */
	result = rdata_towire(&rdata, buf_towire, sizeof(buf_towire), &length);
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(rdata.length, length);
	assert_memory_equal(rdata.data, buf_towire, length);
573 574 575 576 577 578

	/*
	 * Test that additionaldata_*() succeeded.
	 */
	result = rdata_additionadata(&rdata);
	assert_int_equal(result, ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
579 580 581 582 583

	/*
	 * Exercise checknames_*().
	 */
	rdata_checknames(&rdata);
584 585
}

586
/*
587 588 589 590
 * Test fromtext_*() and totext_*() routines for given RR class and type for
 * each text form RDATA in the supplied array.  See the comment for
 * check_text_ok_single() for an explanation of how exactly these routines are
 * tested.
591
 */
592 593
static void
check_text_ok(const text_ok_t *text_ok, dns_rdataclass_t rdclass,
Evan Hunt's avatar
Evan Hunt committed
594
	      dns_rdatatype_t type, size_t structsize) {
595
	size_t i;
596

597 598 599
	/*
	 * Check all entries in the supplied array.
	 */
600
	for (i = 0; text_ok[i].text_in != NULL; i++) {
601
		check_text_ok_single(&text_ok[i], rdclass, type, structsize);
602
	}
603 604 605 606 607 608 609 610 611 612
}

/*
 * For each wire form RDATA in the supplied array, check whether it is properly
 * handled as being either valid or invalid for an RR of given rdclass and
 * type, then check whether trying to process a zero-length wire data buffer
 * yields the expected result.  This checks whether the fromwire_*() routine
 * for given RR class and type behaves as expected.
 */
static void
613
check_wire_ok(const wire_ok_t *wire_ok, bool empty_ok, dns_rdataclass_t rdclass,
Evan Hunt's avatar
Evan Hunt committed
614
	      dns_rdatatype_t type, size_t structsize) {
615
	wire_ok_t empty_wire = WIRE_TEST(empty_ok, 0);
Evan Hunt's avatar
Evan Hunt committed
616
	size_t i;
617 618 619 620

	/*
	 * Check all entries in the supplied array.
	 */
621
	for (i = 0; wire_ok[i].len != 0; i++) {
622
		check_wire_ok_single(&wire_ok[i], rdclass, type, structsize);
623
	}
624 625 626 627 628 629 630

	/*
	 * Check empty wire data.
	 */
	check_wire_ok_single(&empty_wire, rdclass, type, structsize);
}

631 632 633 634 635
/*
 * Check that two records compare as expected with dns_rdata_compare().
 */
static void
check_compare_ok_single(const compare_ok_t *compare_ok,
Evan Hunt's avatar
Evan Hunt committed
636 637
			dns_rdataclass_t rdclass, dns_rdatatype_t type) {
	dns_rdata_t rdata1 = DNS_RDATA_INIT, rdata2 = DNS_RDATA_INIT;
638
	unsigned char buf1[1024], buf2[1024];
Evan Hunt's avatar
Evan Hunt committed
639 640
	isc_result_t result;
	int answer;
641

642 643 644
	result = dns_test_rdatafromstring(&rdata1, rdclass, type, buf1,
					  sizeof(buf1), compare_ok->text1,
					  false);
Evan Hunt's avatar
Evan Hunt committed
645 646 647 648
	if (result != ISC_R_SUCCESS) {
		fail_msg("# line %d: '%s': expected success, got failure",
			 compare_ok->lineno, compare_ok->text1);
	}
649

650 651 652
	result = dns_test_rdatafromstring(&rdata2, rdclass, type, buf2,
					  sizeof(buf2), compare_ok->text2,
					  false);
653

Evan Hunt's avatar
Evan Hunt committed
654 655 656 657
	if (result != ISC_R_SUCCESS) {
		fail_msg("# line %d: '%s': expected success, got failure",
			 compare_ok->lineno, compare_ok->text2);
	}
658 659

	answer = dns_rdata_compare(&rdata1, &rdata2);
Evan Hunt's avatar
Evan Hunt committed
660 661 662
	if (compare_ok->answer == 0 && answer != 0) {
		fail_msg("# line %d: dns_rdata_compare('%s', '%s'): "
			 "expected equal, got %s",
663 664
			 compare_ok->lineno, compare_ok->text1,
			 compare_ok->text2,
Evan Hunt's avatar
Evan Hunt committed
665
			 (answer > 0) ? "greater than" : "less than");
666
	}
Evan Hunt's avatar
Evan Hunt committed
667 668 669
	if (compare_ok->answer < 0 && answer >= 0) {
		fail_msg("# line %d: dns_rdata_compare('%s', '%s'): "
			 "expected less than, got %s",
670 671
			 compare_ok->lineno, compare_ok->text1,
			 compare_ok->text2,
Evan Hunt's avatar
Evan Hunt committed
672
			 (answer == 0) ? "equal" : "greater than");
673
	}
Evan Hunt's avatar
Evan Hunt committed
674 675 676
	if (compare_ok->answer > 0 && answer <= 0) {
		fail_msg("line %d: dns_rdata_compare('%s', '%s'): "
			 "expected greater than, got %s",
677 678
			 compare_ok->lineno, compare_ok->text1,
			 compare_ok->text2,
Evan Hunt's avatar
Evan Hunt committed
679
			 (answer == 0) ? "equal" : "less than");
680 681 682 683 684 685 686 687
	}
}

/*
 * Check that all the records sets in compare_ok compare as expected
 * with dns_rdata_compare().
 */
static void
688
check_compare_ok(const compare_ok_t *compare_ok, dns_rdataclass_t rdclass,
Evan Hunt's avatar
Evan Hunt committed
689
		 dns_rdatatype_t type) {
690 691 692 693 694 695 696 697 698
	size_t i;
	/*
	 * Check all entries in the supplied array.
	 */
	for (i = 0; compare_ok[i].text1 != NULL; i++) {
		check_compare_ok_single(&compare_ok[i], rdclass, type);
	}
}

699
/*
700
 * Test whether supplied sets of text form and/or wire form RDATA are handled
Evan Hunt's avatar
Evan Hunt committed
701
 * as expected.
702 703 704
 *
 * The empty_ok argument denotes whether an attempt to parse a zero-length wire
 * data buffer should succeed or not (it is valid for some RR types).  There is
705
 * no point in performing a similar check for empty text form RDATA, because
706 707 708 709 710
 * dns_rdata_fromtext() returns ISC_R_UNEXPECTEDEND before calling fromtext_*()
 * for the given RR class and type.
 */
static void
check_rdata(const text_ok_t *text_ok, const wire_ok_t *wire_ok,
711
	    const compare_ok_t *compare_ok, bool empty_ok,
Evan Hunt's avatar
Evan Hunt committed
712
	    dns_rdataclass_t rdclass, dns_rdatatype_t type, size_t structsize) {
713
	if (text_ok != NULL) {
714
		check_text_ok(text_ok, rdclass, type, structsize);
715 716
	}
	if (wire_ok != NULL) {
717
		check_wire_ok(wire_ok, empty_ok, rdclass, type, structsize);
718
	}
719 720 721
	if (compare_ok != NULL) {
		check_compare_ok(compare_ok, rdclass, type);
	}
722 723
}

724 725 726 727 728 729 730 731
/*
 * Common tests for RR types based on KEY that require key data:
 *
 *   - CDNSKEY (RFC 7344)
 *   - DNSKEY (RFC 4034)
 *   - RKEY (draft-reid-dnsext-rkey-00)
 */
static void
Evan Hunt's avatar
Evan Hunt committed
732
key_required(void **state, dns_rdatatype_t type, size_t size) {
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	wire_ok_t wire_ok[] = { /*
				 * RDATA must be at least 5 octets in size:
				 *
				 *   - 2 octets for Flags,
				 *   - 1 octet for Protocol,
				 *   - 1 octet for Algorithm,
				 *   - Public Key must not be empty.
				 *
				 * RFC 2535 section 3.1.2 allows the Public Key
				 * to be empty if bits 0-1 of Flags are both
				 * set, but that only applies to KEY records:
				 * for the RR types tested here, the Public Key
				 * must not be empty.
				 */
				WIRE_INVALID(0x00),
				WIRE_INVALID(0x00, 0x00),
				WIRE_INVALID(0x00, 0x00, 0x00),
				WIRE_INVALID(0xc0, 0x00, 0x00, 0x00),
				WIRE_INVALID(0x00, 0x00, 0x00, 0x00),
				WIRE_VALID(0x00, 0x00, 0x00, 0x00, 0x00),
				WIRE_SENTINEL()
754 755 756 757 758 759 760
	};

	UNUSED(state);

	check_rdata(NULL, wire_ok, NULL, false, dns_rdataclass_in, type, size);
}

Evan Hunt's avatar
Evan Hunt committed
761 762
/* APL RDATA manipulations */
static void
Evan Hunt's avatar
Evan Hunt committed
763
apl(void **state) {
764 765 766 767
	text_ok_t text_ok[] = {
		/* empty list */
		TEXT_VALID(""),
		/* min,max prefix IPv4 */
768
		TEXT_VALID("1:0.0.0.0/0"), TEXT_VALID("1:127.0.0.1/32"),
769
		/* min,max prefix IPv6 */
770
		TEXT_VALID("2:::/0"), TEXT_VALID("2:::1/128"),
771
		/* negated */
772 773
		TEXT_VALID("!1:0.0.0.0/0"), TEXT_VALID("!1:127.0.0.1/32"),
		TEXT_VALID("!2:::/0"), TEXT_VALID("!2:::1/128"),
774
		/* bits set after prefix length - not disallowed */
775
		TEXT_VALID("1:127.0.0.0/0"), TEXT_VALID("2:8000::/0"),
776 777 778 779 780 781 782 783
		/* multiple */
		TEXT_VALID("1:0.0.0.0/0 1:127.0.0.1/32"),
		TEXT_VALID("1:0.0.0.0/0 !1:127.0.0.1/32"),
		/* family 0, prefix 0, positive */
		TEXT_VALID("\\# 4 00000000"),
		/* family 0, prefix 0, negative */
		TEXT_VALID("\\# 4 00000080"),
		/* prefix too long */
784
		TEXT_INVALID("1:0.0.0.0/33"), TEXT_INVALID("2:::/129"),
785 786 787 788 789
		/*
		 * Sentinel.
		 */
		TEXT_SENTINEL()
	};
790 791 792 793 794 795 796 797 798 799 800 801
	wire_ok_t wire_ok[] = { /* zero length */
				WIRE_VALID(),
				/* prefix too big IPv4 */
				WIRE_INVALID(0x00, 0x01, 33U, 0x00),
				/* prefix too big IPv6 */
				WIRE_INVALID(0x00, 0x02, 129U, 0x00),
				/* trailing zero octet in afdpart */
				WIRE_INVALID(0x00, 0x00, 0x00, 0x01, 0x00),
				/*
				 * Sentinel.
				 */
				WIRE_SENTINEL()
802 803
	};

Evan Hunt's avatar
Evan Hunt committed
804
	UNUSED(state);
805

806
	check_rdata(text_ok, wire_ok, NULL, true, dns_rdataclass_in,
807 808 809
		    dns_rdatatype_apl, sizeof(dns_rdata_in_apl_t));
}

Evan Hunt's avatar
Evan Hunt committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
/*
 * http://broadband-forum.org/ftp/pub/approved-specs/af-saa-0069.000.pdf
 *
 * ATMA RR’s have the following RDATA format:
 *
 *                                           1  1  1  1  1  1
 *             0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
 *          +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 *          |          FORMAT       |                       |
 *          +--+--+--+--+--+--+--+--+                       |
 *          /                    ADDRESS                    /
 *          |                                               |
 *          +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 *
 * The fields have the following meaning:
 *
 * * FORMAT: One octet that indicates the format of ADDRESS. The two
 *   possible values for FORMAT are value 0 indicating ATM End System Address
 *   (AESA) format and value 1 indicating E.164 format.
 *
 * * ADDRESS: Variable length string of octets containing the ATM address of
 *   the node to which this RR pertains.
 *
 * When the format value is 0, indicating that the address is in AESA format,
 * the address is coded as described in ISO 8348/AD 2 using the preferred
 * binary encoding of the ISO NSAP format. When the format value is 1,
 * indicating that the address is in E.164 format, the Address/Number Digits
 * appear in the order in which they would be entered on a numeric keypad.
 * Digits are coded in IA5 characters with the leftmost bit of each digit set
 * to 0.  This ATM address appears in ATM End System Address Octets field (AESA
 * format) or the Address/Number Digits field (E.164 format) of the Called
 * party number information element [ATMUNI3.1]. Subaddress information is
 * intentionally not included because E.164 subaddress information is used for
 * routing.
 *
 * ATMA RRs cause no additional section processing.
 */
static void
Evan Hunt's avatar
Evan Hunt committed
848
atma(void **state) {
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
	text_ok_t text_ok[] = { TEXT_VALID("00"),
				TEXT_VALID_CHANGED("0.0", "00"),
				/*
				 * multiple consecutive periods
				 */
				TEXT_INVALID("0..0"),
				/*
				 * trailing period
				 */
				TEXT_INVALID("00."),
				/*
				 * leading period
				 */
				TEXT_INVALID(".00"),
				/*
				 * Not full octets.
				 */
				TEXT_INVALID("000"),
				/*
				 * E.164
				 */
				TEXT_VALID("+61200000000"),
				/*
				 * E.164 with periods
				 */
				TEXT_VALID_CHANGED("+61.2.0000.0000", "+6120000"
								      "0000"),
				/*
				 * E.164 with period at end
				 */
				TEXT_INVALID("+61200000000."),
				/*
				 * E.164 with multiple consecutive periods
				 */
				TEXT_INVALID("+612..00000000"),
				/*
				 * E.164 with period before the leading digit.
				 */
				TEXT_INVALID("+.61200000000"),
				/*
				 * Sentinel.
				 */
				TEXT_SENTINEL() };
Mark Andrews's avatar
Mark Andrews committed
892 893 894 895
	wire_ok_t wire_ok[] = {
		/*
		 * Too short.
		 */
896
		WIRE_INVALID(0x00), WIRE_INVALID(0x01),
Mark Andrews's avatar
Mark Andrews committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910
		/*
		 * all digits
		 */
		WIRE_VALID(0x01, '6', '1', '2', '0', '0', '0'),
		/*
		 * non digit
		 */
		WIRE_INVALID(0x01, '+', '6', '1', '2', '0', '0', '0'),
		/*
		 * Sentinel.
		 */
		WIRE_SENTINEL()
	};

Evan Hunt's avatar
Evan Hunt committed
911
	UNUSED(state);
Mark Andrews's avatar
Mark Andrews committed
912

913
	check_rdata(text_ok, wire_ok, NULL, false, dns_rdataclass_in,
Mark Andrews's avatar
Mark Andrews committed
914 915 916
		    dns_rdatatype_atma, sizeof(dns_rdata_in_atma_t));
}

Mark Andrews's avatar
Mark Andrews committed
917
/* AMTRELAY RDATA manipulations */
Mark Andrews's avatar
Mark Andrews committed
918
static void
Evan Hunt's avatar
Evan Hunt committed
919
amtrelay(void **state) {
Mark Andrews's avatar
Mark Andrews committed
920
	text_ok_t text_ok[] = {
921
		TEXT_INVALID(""), TEXT_INVALID("0"), TEXT_INVALID("0 0"),
922
		/* gateway type 0 */
923 924
		TEXT_VALID("0 0 0"), TEXT_VALID("0 1 0"),
		TEXT_INVALID("0 2 0"),	 /* discovery out of range */
925
		TEXT_VALID("255 1 0"),	 /* max precedence */
926
		TEXT_INVALID("256 1 0"), /* precedence out of range */
Mark Andrews's avatar
Mark Andrews committed
927 928

		/* IPv4 gateway */
929
		TEXT_INVALID("0 0 1"), /* no address */
Mark Andrews's avatar
Mark Andrews committed
930 931
		TEXT_VALID("0 0 1 0.0.0.0"),
		TEXT_INVALID("0 0 1 0.0.0.0 x"), /* extra */
932 933 934
		TEXT_INVALID("0 0 1 0.0.0.0.0"), /* bad address */
		TEXT_INVALID("0 0 1 ::"),	 /* bad address */
		TEXT_INVALID("0 0 1 ."),	 /* bad address */
Mark Andrews's avatar
Mark Andrews committed
935 936

		/* IPv6 gateway */
937
		TEXT_INVALID("0 0 2"), /* no address */
938
		TEXT_VALID("0 0 2 ::"), TEXT_INVALID("0 0 2 :: xx"), /* extra */
939 940
		TEXT_INVALID("0 0 2 0.0.0.0"), /* bad address */
		TEXT_INVALID("0 0 2 ."),       /* bad address */
Mark Andrews's avatar
Mark Andrews committed
941 942

		/* hostname gateway */
943
		TEXT_INVALID("0 0 3"), /* no name */
Mark Andrews's avatar
Mark Andrews committed
944 945 946 947 948 949 950 951 952
		/* IPv4 is a valid name */
		TEXT_VALID_CHANGED("0 0 3 0.0.0.0", "0 0 3 0.0.0.0."),
		/* IPv6 is a valid name */
		TEXT_VALID_CHANGED("0 0 3 ::", "0 0 3 ::."),
		TEXT_VALID_CHANGED("0 0 3 example", "0 0 3 example."),
		TEXT_VALID("0 0 3 example."),
		TEXT_INVALID("0 0 3 example. x"), /* extra */

		/* unknown gateway */
953 954 955
		TEXT_VALID("\\# 2 0004"), TEXT_VALID("\\# 2 0084"),
		TEXT_VALID("\\# 2 007F"), TEXT_VALID("\\# 3 000400"),
		TEXT_VALID("\\# 3 008400"), TEXT_VALID("\\# 3 00FF00"),
Mark Andrews's avatar
Mark Andrews committed
956 957 958 959 960 961 962

		/*
		 * Sentinel.
		 */
		TEXT_SENTINEL()
	};
	wire_ok_t wire_ok[] = {
963 964
		WIRE_INVALID(0x00), WIRE_VALID(0x00, 0x00),
		WIRE_VALID(0x00, 0x80), WIRE_INVALID(0x00, 0x00, 0x00),
Mark Andrews's avatar
Mark Andrews committed
965 966
		WIRE_INVALID(0x00, 0x80, 0x00),

967
		WIRE_INVALID(0x00, 0x01), WIRE_INVALID(0x00, 0x01, 0x00),
Mark Andrews's avatar
Mark Andrews committed
968 969 970 971 972
		WIRE_INVALID(0x00, 0x01, 0x00, 0x00),
		WIRE_INVALID(0x00, 0x01, 0x00, 0x00, 0x00),
		WIRE_VALID(0x00, 0x01, 0x00, 0x00, 0x00, 0x00),
		WIRE_INVALID(0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00),

973 974 975 976 977 978 979
		WIRE_INVALID(0x00, 0x02), WIRE_INVALID(0x00, 0x02, 0x00),
		WIRE_VALID(0x00, 0x02, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
			   0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14,
			   0x15),
		WIRE_INVALID(0x00, 0x02, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
			     0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13,
			     0x14, 0x15, 0x16),
Mark Andrews's avatar
Mark Andrews committed
980

981 982
		WIRE_INVALID(0x00, 0x03), WIRE_VALID(0x00, 0x03, 0x00),
		WIRE_INVALID(0x00, 0x03, 0x00, 0x00), /* extra */
Mark Andrews's avatar
Mark Andrews committed
983

984
		WIRE_VALID(0x00, 0x04), WIRE_VALID(0x00, 0x04, 0x00),
Mark Andrews's avatar
Mark Andrews committed
985 986 987 988 989 990 991 992 993
		/*
		 * Sentinel.
		 */
		WIRE_SENTINEL()
	};

	UNUSED(state);

	check_rdata(text_ok, wire_ok, NULL, false, dns_rdataclass_in,
Mark Andrews's avatar
Mark Andrews committed
994
		    dns_rdatatype_amtrelay, sizeof(dns_rdata_amtrelay_t));
Mark Andrews's avatar
Mark Andrews committed
995 996
}

997
static void
Evan Hunt's avatar
Evan Hunt committed
998
cdnskey(void **state) {
999 1000 1001
	key_required(state, dns_rdatatype_cdnskey, sizeof(dns_rdata_cdnskey_t));
}

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/*
 * CSYNC tests.
 *
 * RFC 7477:
 *
 * 2.1.  The CSYNC Resource Record Format
 *
 * 2.1.1.  The CSYNC Resource Record Wire Format
 *
 *    The CSYNC RDATA consists of the following fields:
 *
 *                           1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
 *       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *      |                          SOA Serial                           |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *      |       Flags                   |            Type Bit Map       /
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *      /                     Type Bit Map (continued)                  /
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * 2.1.1.1.  The SOA Serial Field
 *
 *    The SOA Serial field contains a copy of the 32-bit SOA serial number
 *    from the child zone.  If the soaminimum flag is set, parental agents
 *    querying children's authoritative servers MUST NOT act on data from
 *    zones advertising an SOA serial number less than this value.  See
 *    [RFC1982] for properly implementing "less than" logic.  If the
 *    soaminimum flag is not set, parental agents MUST ignore the value in
 *    the SOA Serial field.  Clients can set the field to any value if the
 *    soaminimum flag is unset, such as the number zero.
 *
 * (...)
 *
 * 2.1.1.2.  The Flags Field
 *
 *    The Flags field contains 16 bits of boolean flags that define
 *    operations that affect the processing of the CSYNC record.  The flags
 *    defined in this document are as follows:
 *
 *       0x00 0x01: "immediate"
 *
 *       0x00 0x02: "soaminimum"
 *
 *    The definitions for how the flags are to be used can be found in
 *    Section 3.
 *
 *    The remaining flags are reserved for use by future specifications.
 *    Undefined flags MUST be set to 0 by CSYNC publishers.  Parental
 *    agents MUST NOT process a CSYNC record if it contains a 1 value for a
 *    flag that is unknown to or unsupported by the parental agent.
 *
 * 2.1.1.2.1.  The Type Bit Map Field
 *
 *    The Type Bit Map field indicates the record types to be processed by
 *    the parental agent, according to the procedures in Section 3.  The
 *    Type Bit Map field is encoded in the same way as the Type Bit Map
 *    field of the NSEC record, described in [RFC4034], Section 4.1.2.  If
 *    a bit has been set that a parental agent implementation does not
 *    understand, the parental agent MUST NOT act upon the record.
 *    Specifically, a parental agent must not simply copy the data, and it
 *    must understand the semantics associated with a bit in the Type Bit
 *    Map field that has been set to 1.
 */
Evan Hunt's avatar
Evan Hunt committed
1066
static void
Evan Hunt's avatar
Evan Hunt committed
1067
csync(void **state) {
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
	text_ok_t text_ok[] = { TEXT_INVALID(""),
				TEXT_INVALID("0"),
				TEXT_VALID("0 0"),
				TEXT_VALID("0 0 A"),
				TEXT_VALID("0 0 NS"),
				TEXT_VALID("0 0 AAAA"),
				TEXT_VALID("0 0 A AAAA"),
				TEXT_VALID("0 0 A NS AAAA"),
				TEXT_INVALID("0 0 A NS AAAA BOGUS"),
				TEXT_SENTINEL() };
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	wire_ok_t wire_ok[] = {
		/*
		 * Short.
		 */
		WIRE_INVALID(0x00),
		/*
		 * Short.
		 */
		WIRE_INVALID(0x00, 0x00),
		/*
		 * Short.
		 */
		WIRE_INVALID(0x00, 0x00, 0x00),
		/*
		 * Short.
		 */
		WIRE_INVALID(0x00, 0x00, 0x00, 0x00),
		/*
		 * Short.
		 */
		WIRE_INVALID(0x00, 0x00, 0x00, 0x00, 0x00),
		/*
		 * Serial + flags only.
		 */
		WIRE_VALID(0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
		/*
		 * Bad type map.
		 */
		WIRE_INVALID(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
		/*
		 * Bad type map.
		 */
		WIRE_INVALID(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
		/*
		 * Good type map.
		 */
		WIRE_VALID(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
			   0x02),
		/*
		 * Sentinel.
		 */
		WIRE_SENTINEL()
	};

Evan Hunt's avatar
Evan Hunt committed
1122
	UNUSED(state);
1123

1124
	check_rdata(text_ok, wire_ok, NULL, false, dns_rdataclass_in,
1125 1126 1127
		    dns_rdatatype_csync, sizeof(dns_rdata_csync_t));
}

1128
static void
Evan Hunt's avatar
Evan Hunt committed
1129
dnskey(void **state) {
1130 1131 1132
	key_required(state, dns_rdatatype_dnskey, sizeof(dns_rdata_dnskey_t));
}

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/*
 * DOA tests.
 *
 * draft-durand-doa-over-dns-03:
 *
 * 3.2.  DOA RDATA Wire Format
 *
 *        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 *     0: |                                                               |
 *        |                        DOA-ENTERPRISE                         |
 *        |                                                               |
 *        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 *     4: |                                                               |
 *        |                           DOA-TYPE                            |
 *        |                                                               |
 *        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 *     8: |         DOA-LOCATION          |         DOA-MEDIA-TYPE        /
 *        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 *    10: /                                                               /
 *        /                  DOA-MEDIA-TYPE (continued)                   /
 *        /                                                               /
 *        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 *        /                                                               /
 *        /                           DOA-DATA                            /
 *        /                                                               /
 *        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 *
 *    DOA-ENTERPRISE: a 32-bit unsigned integer in network order.
 *
 *    DOA-TYPE: a 32-bit unsigned integer in network order.
 *
 *    DOA-LOCATION: an 8-bit unsigned integer.
 *
 *    DOA-MEDIA-TYPE: A <character-string> (see [RFC1035]).  The first
 *    octet of the <character-string> contains the number of characters to
 *    follow.
 *
 *    DOA-DATA: A variable length blob of binary data.  The length of the
 *    DOA-DATA is not contained within the wire format of the RR and has to
 *    be computed from the RDLENGTH of the entire RR once other fields have
 *    been taken into account.
 *
 * 3.3.  DOA RDATA Presentation Format
 *
 *    The DOA-ENTERPRISE field is presented as an unsigned 32-bit decimal
 *    integer with range 0 - 4,294,967,295.
 *
 *    The DOA-TYPE field is presented as an unsigned 32-bit decimal integer
 *    with range 0 - 4,294,967,295.
 *
 *    The DOA-LOCATION field is presented as an unsigned 8-bit decimal
 *    integer with range 0 - 255.
 *
 *    The DOA-MEDIA-TYPE field is presented as a single <character-string>.
 *
1188
 *    The DOA-DATA is presented as Base64 encoded data [RFC4648] unless the
1189
 *    DOA-DATA is empty in which case it is presented as a single dash
1190 1191
 *    character ("-", ASCII 45).  White space is permitted within Base64
 *    data.
1192
 */
Evan Hunt's avatar
Evan Hunt committed
1193
static void
Evan Hunt's avatar
Evan Hunt committed
1194
doa(void **state) {
1195 1196 1197 1198 1199 1200 1201 1202
	text_ok_t text_ok[] = {
		/*
		 * Valid, non-empty DOA-DATA.
		 */
		TEXT_VALID("0 0 1 \"text/plain\" Zm9v"),
		/*
		 * Valid, non-empty DOA-DATA with whitespace in between.
		 */
1203 1204 1205 1206
		TEXT_VALID_CHANGED("0 0 1 \"text/plain\" Zm 9v", "0 0 1 "
								 "\"text/"
								 "plain\" "
								 "Zm9v"),
1207 1208 1209
		/*
		 * Valid, unquoted DOA-MEDIA-TYPE, non-empty DOA-DATA.
		 */
1210 1211 1212
		TEXT_VALID_CHANGED("0 0 1 text/plain Zm9v", "0 0 1 "
							    "\"text/plain\" "
							    "Zm9v"),
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		/*
		 * Invalid, quoted non-empty DOA-DATA.
		 */
		TEXT_INVALID("0 0 1 \"text/plain\" \"Zm9v\""),
		/*
		 * Valid, empty DOA-DATA.
		 */
		TEXT_VALID("0 0 1 \"text/plain\" -"),
		/*
		 * Invalid, quoted empty DOA-DATA.
		 */
		TEXT_INVALID("0 0 1 \"text/plain\" \"-\""),
		/*
		 * Invalid, missing "-" in empty DOA-DATA.
		 */
		TEXT_INVALID("0 0 1 \"text/plain\""),
		/*
		 * Valid, undefined DOA-LOCATION.
		 */
		TEXT_VALID("0 0 100 \"text/plain\" Zm9v"),
		/*
		 * Invalid, DOA-LOCATION too big.
		 */
		TEXT_INVALID("0 0 256 \"text/plain\" ZM9v"),
		/*
		 * Valid, empty DOA-MEDIA-TYPE, non-empty DOA-DATA.
		 */
		TEXT_VALID("0 0 2 \"\" aHR0cHM6Ly93d3cuaXNjLm9yZy8="),
		/*
		 * Valid, empty DOA-MEDIA-TYPE, empty DOA-DATA.
		 */
		TEXT_VALID("0 0 1 \"\" -"),
		/*
		 * Valid, DOA-MEDIA-TYPE with a space.
		 */
		TEXT_VALID("0 0 1 \"plain text\" Zm9v"),
		/*
		 * Invalid, missing DOA-MEDIA-TYPE.
		 */
		TEXT_INVALID("1234567890 1234567890 1"),
		/*
		 * Valid, DOA-DATA over 255 octets.
		 */
		TEXT_VALID("1234567890 1234567890 1 \"image/gif\" "
			   "R0lGODlhKAAZAOMCAGZmZgBmmf///zOZzMz//5nM/zNmmWbM"
			   "/5nMzMzMzACZ/////////////////////yH5BAEKAA8ALAAA"
			   "AAAoABkAAATH8IFJK5U2a4337F5ogRkpnoCJrly7PrCKyh8c"
			   "3HgAhzT35MDbbtO7/IJIHbGiOiaTxVTpSVWWLqNq1UVyapNS"
			   "1wd3OAxug0LhnCubcVhsxysQnOt4ATpvvzHlFzl1AwODhWeF"
			   "AgRpen5/UhheAYMFdUB4SFcpGEGGdQeCAqBBLTuSk30EeXd9"
			   "pEsAbKGxjHqDSE0Sp6ixN4N1BJmbc7lIhmsBich1awPAjkY1"
			   "SZR8bJWrz382SGqIBQQFQd4IsUTaX+ceuudPEQA7"),
		/*
		 * Invalid, bad Base64 in DOA-DATA.
		 */
		TEXT_INVALID("1234567890 1234567890 1 \"image/gif\" R0lGODl"),
		/*
		 * Sentinel.
		 */
		TEXT_SENTINEL()
	};
	wire_ok_t wire_ok[] = {
		/*
		 * Valid, empty DOA-MEDIA-TYPE, empty DOA-DATA.
		 */