test_confio.c 31.3 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 20 21

    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 <tap/basic.h>

#include "test_conf.h"
#include "knot/conf/confio.h"
#include "knot/conf/tools.h"
22
#include "libknot/yparser/yptrafo.h"
23
#include "contrib/string.h"
24
#include "contrib/openbsd/strlcat.h"
25 26 27 28 29 30 31

#define SKIP_OPENBSD	skip("Nested transactions are not supported on OpenBSD");
#define OUT_LEN		1024
#define ZONE1		"zone1"
#define ZONE2		"zone2"
#define ZONE3		"zone3"

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
char *format_key(conf_io_t *io)
{
	char id[KNOT_DNAME_TXT_MAXLEN + 1] = "\0";
	size_t id_len = sizeof(id);

	// Get the textual item id.
	if (io->id_len > 0 && !io->id_as_data) {
		if (yp_item_to_txt(io->key0->var.g.id, io->id, io->id_len, id,
		                   &id_len, YP_SNOQUOTE) != KNOT_EOK) {
			return NULL;
		}
	}

	// Get the item prefix.
	const char *prefix = "";
	switch (io->type) {
	case NEW: prefix = "+"; break;
	case OLD: prefix = "-"; break;
	default: break;
	}

	// Format the item key.
	return sprintf_alloc(
		"%s%.*s%s%.*s%s%s%.*s",
		prefix, (int)io->key0->name[0], io->key0->name + 1,
		(io->id_len > 0 && !io->id_as_data ? "[" : ""),
		(io->id_len > 0 && !io->id_as_data ? (int)id_len : 0), id,
		(io->id_len > 0 && !io->id_as_data ? "]" : ""),
		(io->key1 != NULL ? "." : ""),
		(io->key1 != NULL ? (int)io->key1->name[0] : 0),
		(io->key1 != NULL ? io->key1->name + 1 : ""));
}

static int append_data(const yp_item_t *item, const uint8_t *bin, size_t bin_len,
                       char *out, size_t out_len)
{
	char buf[YP_MAX_TXT_DATA_LEN + 1] = "\0";
	size_t buf_len = sizeof(buf);

	int ret = yp_item_to_txt(item, bin, bin_len, buf, &buf_len, YP_SNONE);
	if (ret != KNOT_EOK) {
		return ret;
	}

	if (strlcat(out, buf, out_len) >= out_len) {
		return KNOT_ESPACE;
	}

	return KNOT_EOK;
}

char *format_data(conf_io_t *io)
{
	char out[YP_MAX_TXT_DATA_LEN + 1] = "\0";

	// Return the item identifier as the item data.
	if (io->id_as_data) {
		if (append_data(io->key0->var.g.id, io->id, io->id_len, out,
		                sizeof(out)) != KNOT_EOK) {
			return NULL;
		}

		return strdup(out);
	}

	// Check for no data.
	if (io->data.val == NULL && io->data.bin == NULL) {
		return NULL;
	}

	const yp_item_t *item = (io->key1 != NULL) ? io->key1 : io->key0;

	// Format explicit binary data value.
	if (io->data.bin != NULL) {
		if (append_data(item, io->data.bin, io->data.bin_len, out,
		                sizeof(out)) != KNOT_EOK) {
			return NULL;
		}
	// Format multivalued item data.
	} else if (item->flags & YP_FMULTI) {
		size_t values = conf_val_count(io->data.val);
		for (size_t i = 0; i < values; i++) {
			// Skip other values if known index (counted from 1).
			if (io->data.index > 0 &&
			    io->data.index != i + 1) {
				conf_val_next(io->data.val);
				continue;
			}

			if (i > 0) {
				if (strlcat(out, " ", sizeof(out)) >= sizeof(out)) {
					return NULL;
				}
			}

			conf_val(io->data.val);
			if (append_data(item, io->data.val->data, io->data.val->len,
			                out, sizeof(out)) != KNOT_EOK) {
				return NULL;
			}

			conf_val_next(io->data.val);
		}
	// Format singlevalued item data.
	} else {
		conf_val(io->data.val);
		if (append_data(item, io->data.val->data, io->data.val->len, out,
		                sizeof(out)) != KNOT_EOK) {
			return NULL;
		}
	}

	return strdup(out);
}

147 148 149 150 151
static int format_item(conf_io_t *io)
{
	char *out = (char *)io->misc;

	// Get the item key and data strings.
152 153
	char *key = format_key(io);
	char *data = format_data(io);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	// Format the item.
	char *item = sprintf_alloc(
		"%s%s%s%s",
		(*out != '\0' ? "\n" : ""),
		(key != NULL ? key : ""),
		(data != NULL ? " = " : ""),
		(data != NULL ? data : ""));
	free(key);
	free(data);
	if (item == NULL) {
		return KNOT_ENOMEM;
	}

	// Append the item.
	if (strlcat(out, item, OUT_LEN) >= OUT_LEN) {
170
		free(item);
171 172 173
		return KNOT_ESPACE;
	}

174 175
	free(item);

176 177 178
	return KNOT_EOK;
}

179
static void test_conf_io_begin(void)
180
{
181
	ok(conf_io_begin(true) == KNOT_TXN_ENOTEXISTS, "begin child txn with no parent");
182 183 184 185 186 187 188 189
	ok(conf()->io.txn == NULL, "check txn depth");

#if defined(__OpenBSD__)
	SKIP_OPENBSD
#else
	ok(conf_io_begin(false) == KNOT_EOK, "begin parent txn");
	ok(conf()->io.txn == &(conf()->io.txn_stack[0]), "check txn depth");

190
	ok(conf_io_begin(false) == KNOT_TXN_EEXISTS, "begin another parent txn");
191 192 193 194 195 196
	ok(conf()->io.txn == &(conf()->io.txn_stack[0]), "check txn depth");

	for (int i = 1; i < CONF_MAX_TXN_DEPTH; i++) {
		ok(conf_io_begin(true) == KNOT_EOK, "begin child txn");
		ok(conf()->io.txn == &(conf()->io.txn_stack[i]), "check txn depth");
	}
197
	ok(conf_io_begin(true) == KNOT_TXN_EEXISTS, "begin another child txn");
198 199 200
	ok(conf()->io.txn == &(conf()->io.txn_stack[CONF_MAX_TXN_DEPTH - 1]),
	   "check txn depth");

201
	conf_io_abort(false);
202 203 204 205
	ok(conf()->io.txn == NULL, "check txn depth");
#endif
}

206
static void test_conf_io_abort(void)
207 208 209 210
{
#if defined(__OpenBSD__)
	SKIP_OPENBSD
#else
211 212
	// Test child persistence after subchild abort.

213
	ok(conf_io_begin(false) == KNOT_EOK, "begin parent txn");
214
	char idx[2] = { '0' };
215
	ok(conf_io_set("server", "version", NULL, idx) ==
216
	   KNOT_EOK, "set single value '%s'", idx);
217 218

	for (int i = 1; i < CONF_MAX_TXN_DEPTH; i++) {
219 220
		char idx[2] = { '0' + i };
		ok(conf_io_begin(true) == KNOT_EOK, "begin child txn %s", idx);
221
		ok(conf_io_set("server", "version", NULL, idx) ==
222
		   KNOT_EOK, "set single value '%s'", idx);
223 224 225
	}

	for (int i = CONF_MAX_TXN_DEPTH - 1; i > 0; i--) {
226
		char idx[2] = { '0' + i };
227
		conf_io_abort(true);
228 229 230
		conf_val_t val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
		ok(val.code == KNOT_EOK, "check entry");
		const char *data = conf_str(&val);
231
		ok(*data == (idx[0] - 1), "compare txn data '%s'", data);
232 233
	}

234
	conf_io_abort(false);
235
	ok(conf()->io.txn == NULL, "check txn depth");
236

237
	// Test child abort with committed subchild.
238
	ok(conf_io_begin(false) == KNOT_EOK, "begin new parent txn");
239 240
	ok(conf_io_begin(true) == KNOT_EOK, "begin child txn");
	ok(conf_io_begin(true) == KNOT_EOK, "begin subchild txn");
241
	ok(conf_io_set("server", "version", NULL, "text") ==
242 243
	   KNOT_EOK, "set single value");
	ok(conf_io_commit(true) == KNOT_EOK, "commit subchild txn");
244
	conf_val_t val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
245
	ok(val.code == KNOT_EOK, "check entry");
246 247
	const char *data = conf_str(&val);
	ok(strcmp(data, "text") == 0, "compare subchild txn data '%s'", data);
248
	conf_io_abort(true);
249 250
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");
251
	conf_io_abort(false);
252

253
	// Test unchanged read_txn.
254 255 256 257 258
	val = conf_get_txn(conf(), &conf()->read_txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");
#endif
}

259
static void test_conf_io_commit(void)
260
{
261 262
	ok(conf_io_commit(false) == KNOT_TXN_ENOTEXISTS, "commit no txt txn");
	ok(conf_io_commit(true) == KNOT_TXN_ENOTEXISTS, "commit no txt txn");
263 264 265 266

#if defined(__OpenBSD__)
	SKIP_OPENBSD
#else
267 268
	// Test subchild persistence after commit.

269
	ok(conf_io_begin(false) == KNOT_EOK, "begin parent txn");
270
	char idx[2] = { '0' };
271
	ok(conf_io_set("server", "version", NULL, idx) ==
272
	   KNOT_EOK, "set single value '%s'", idx);
273 274

	for (int i = 1; i < CONF_MAX_TXN_DEPTH; i++) {
275 276
		char idx[2] = { '0' + i };
		ok(conf_io_begin(true) == KNOT_EOK, "begin child txn %s", idx);
277
		ok(conf_io_set("server", "version", NULL, idx) ==
278
		   KNOT_EOK, "set single value '%s'", idx);
279 280 281
	}

	for (int i = CONF_MAX_TXN_DEPTH - 1; i > 0; i--) {
282 283 284 285 286 287
		char idx[2] = { '0' + i };
		ok(conf_io_commit(true) == KNOT_EOK, "commit child txn %s", idx);
		conf_val_t val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
		ok(val.code == KNOT_EOK, "check entry");
		const char *data = conf_str(&val);
		ok(*data == ('0' + CONF_MAX_TXN_DEPTH - 1), "compare txn data '%s'", data);
288 289 290
	}

	ok(conf_io_commit(false) == KNOT_EOK, "commit parent txn");
291 292 293 294 295
	ok(conf()->io.txn == NULL, "check txn depth");

	// Test child persistence after parent commit.
	ok(conf_io_begin(false) == KNOT_EOK, "begin new parent txn");
	conf_val_t val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
296
	ok(val.code == KNOT_EOK, "check entry");
297
	idx[0] = '0' + CONF_MAX_TXN_DEPTH - 1;
298
	const char *data = conf_str(&val);
299
	ok(strcmp(data, idx) == 0, "compare final data '%s'", data);
300
	conf_io_abort(false);
301

302 303 304
	// Test unchanged read_txn.
	val = conf_get_txn(conf(), &conf()->read_txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");
305 306 307
#endif
}

308
static void test_conf_io_check(void)
309 310 311 312 313
{
	conf_io_t io = { NULL };

	// ERR no txn.
	ok(conf_io_check(&io) ==
314
	   KNOT_TXN_ENOTEXISTS, "check without active txn");
315 316 317 318

	ok(conf_io_begin(false) == KNOT_EOK, "begin txn");

	// Section check.
319
	ok(conf_io_set("remote", "id", NULL, "remote1") ==
320 321 322 323 324
	   KNOT_EOK, "set remote id");
	ok(conf_io_check(&io) ==
	   KNOT_EINVAL, "check missing remote address");
	ok(io.error.code == KNOT_EINVAL, "compare error code");

325
	ok(conf_io_set("remote", "address", "remote1", "1.1.1.1") ==
326 327 328 329 330 331
	   KNOT_EOK, "set remote address");
	ok(conf_io_check(&io) ==
	   KNOT_EOK, "check remote address");
	ok(io.error.code == KNOT_EOK, "compare error code");

	// Item check.
332
	ok(conf_io_set("zone", "domain", NULL, ZONE1) ==
333
	   KNOT_EOK, "set zone domain "ZONE1);
334
	ok(conf_io_set("zone", "master", ZONE1, "remote1") ==
335 336 337 338 339 340 341 342 343 344 345 346
	   KNOT_EOK, "set zone master");

	ok(conf_io_check(&io) ==
	   KNOT_EOK, "check all");

	ok(conf_io_unset("remote", NULL, NULL, NULL) ==
	   KNOT_EOK, "unset remotes");

	ok(conf_io_check(&io) ==
	   KNOT_ENOENT, "check missing master remote");
	ok(io.error.code == KNOT_ENOENT, "compare error code");

347
	conf_io_abort(false);
348 349
}

350
static void test_conf_io_set(void)
351 352
{
	// ERR no txn.
353
	ok(conf_io_set("server", "version", NULL, "text") ==
354
	   KNOT_TXN_ENOTEXISTS, "set without active txn");
355 356 357 358

	ok(conf_io_begin(false) == KNOT_EOK, "begin txn");

	// ERR.
359
	ok(conf_io_set(NULL, NULL, NULL, NULL) ==
360
	   KNOT_EINVAL, "set NULL key0");
361
	ok(conf_io_set("", NULL, NULL, NULL) ==
362
	   KNOT_YP_EINVAL_ITEM, "set empty key0");
363
	ok(conf_io_set("uknown", NULL, NULL, NULL) ==
364
	   KNOT_YP_EINVAL_ITEM, "set unknown key0");
365
	ok(conf_io_set("server", "unknown", NULL, NULL) ==
366
	   KNOT_YP_EINVAL_ITEM, "set unknown key1");
367
	ok(conf_io_set("include", NULL, NULL, NULL) ==
368
	   KNOT_YP_ENODATA, "set non-group without data");
369
	ok(conf_io_set("server", "background-workers", NULL, "x") ==
370 371 372
	   KNOT_EINVAL, "set invalid data");

	// ERR callback
373
	ok(conf_io_set("include", NULL, NULL, "invalid") ==
374 375
	   KNOT_EFILE, "set invalid callback value");

376 377 378 379 380 381 382 383 384 385
	// Single group, no item, no value.
	ok(conf_io_set("server", NULL, NULL, NULL) ==
	   KNOT_ENOTSUP, "set group no value");
	// Single group, no item, value.
	ok(conf_io_set("server", NULL, NULL, "text") ==
	   KNOT_YP_ENOTSUP_DATA, "set group value");
	// Single group, item, no value.
	ok(conf_io_set("server", "version", NULL, NULL) ==
	   KNOT_YP_ENODATA, "set group item no value");

386
	// Single group, single value.
387
	ok(conf_io_set("server", "version", NULL, "text") ==
388 389 390 391 392 393
	   KNOT_EOK, "set single value");
	conf_val_t val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_EOK, "check entry");
	ok(strcmp(conf_str(&val), "text") == 0, "check entry value");

	// Single group, multi value.
394
	ok(conf_io_set("server", "listen", NULL, "1.1.1.1") ==
395
	   KNOT_EOK, "set multivalue 1");
396
	ok(conf_io_set("server", "listen", NULL, "1.1.1.2") ==
397 398 399 400 401 402 403 404 405 406 407 408 409
	   KNOT_EOK, "set multivalue 2");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_LISTEN);
	ok(val.code == KNOT_EOK, "check entry");
	ok(conf_val_count(&val) == 2, "check entry value count");

	// Prepare dnames.
	knot_dname_t *zone1 = knot_dname_from_str_alloc(ZONE1);
	ok(zone1 != NULL, "create dname "ZONE1);
	knot_dname_t *zone2 = knot_dname_from_str_alloc(ZONE2);
	ok(zone2 != NULL, "create dname "ZONE2);
	knot_dname_t *zone3 = knot_dname_from_str_alloc(ZONE3);
	ok(zone3 != NULL, "create dname "ZONE3);

410 411 412 413
	// Multi group no id.
	ok(conf_io_set("zone", "domain", NULL, NULL) ==
	   KNOT_YP_ENOID, "set zone empty domain");

414
	// Multi group ids.
415
	ok(conf_io_set("zone", "domain", NULL, ZONE1) ==
416
	   KNOT_EOK, "set zone domain "ZONE1);
417
	ok(conf_io_set("zone", NULL, ZONE2, NULL) ==
418 419 420
	   KNOT_EOK, "set zone domain "ZONE2);

	// Multi group, single value.
421
	ok(conf_io_set("zone", "file", ZONE1, "name") ==
422 423 424 425 426 427
	   KNOT_EOK, "set zone file");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_FILE, zone1);
	ok(val.code == KNOT_EOK, "check entry");
	ok(strcmp(conf_str(&val), "name") == 0, "check entry value");

	// Multi group, single value, bad id.
428
	ok(conf_io_set("zone", "file", ZONE3, "name") ==
429 430 431
	   KNOT_YP_EINVAL_ID, "set zone file");

	// Multi group, single value, all ids.
432
	ok(conf_io_set("zone", "comment", NULL, "abc") ==
433 434 435 436 437 438 439 440 441
	   KNOT_EOK, "set zones comment");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
	ok(val.code == KNOT_EOK, "check entry");
	ok(strcmp(conf_str(&val), "abc") == 0, "check entry value");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
	ok(val.code == KNOT_EOK, "check entry");
	ok(strcmp(conf_str(&val), "abc") == 0, "check entry value");

	// Prepare different comment.
442
	ok(conf_io_set("zone", "domain", NULL, ZONE3) ==
443
	   KNOT_EOK, "set zone domain "ZONE3);
444
	ok(conf_io_set("zone", "comment", ZONE3, "xyz") ==
445 446 447 448 449 450 451 452 453 454
	   KNOT_EOK, "set zone comment");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone3);
	ok(val.code == KNOT_EOK, "check entry");
	ok(strcmp(conf_str(&val), "xyz") == 0, "check entry value");

	knot_dname_free(&zone1, NULL);
	knot_dname_free(&zone2, NULL);
	knot_dname_free(&zone3, NULL);

	ok(conf_io_commit(false) == KNOT_EOK, "commit txn");
455 456

	// Update read-only transaction.
457
	ok(conf_refresh_txn(conf()) == KNOT_EOK, "update read-only txn");
458 459
}

460
static void test_conf_io_unset(void)
461 462 463
{
	// ERR no txn.
	ok(conf_io_unset("server", "version", NULL, "text") ==
464
	   KNOT_TXN_ENOTEXISTS, "unset without active txn");
465 466 467 468 469 470 471 472 473 474 475 476

	ok(conf_io_begin(false) == KNOT_EOK, "begin txn");

	// ERR.
	ok(conf_io_unset("", NULL, NULL, NULL) ==
	   KNOT_YP_EINVAL_ITEM, "unset unknown key0");
	ok(conf_io_unset("uknown", NULL, NULL, NULL) ==
	   KNOT_YP_EINVAL_ITEM, "unset unknown key0");
	ok(conf_io_unset("server", "unknown", NULL, NULL) ==
	   KNOT_YP_EINVAL_ITEM, "unset unknown key1");
	ok(conf_io_unset("include", NULL, NULL, "file") ==
	   KNOT_ENOTSUP, "unset non-group item");
477
	ok(conf_io_unset("server", "background-workers", NULL, "x") ==
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	   KNOT_EINVAL, "unset invalid data");

	// Single group, single value.
	ok(conf_io_unset("server", "version", NULL, "") ==
	   KNOT_ENOENT, "unset zero length text value");
	conf_val_t val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_EOK, "check entry");

	ok(conf_io_unset("server", "version", NULL, "bad text") ==
	   KNOT_ENOENT, "unset bad value");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_EOK, "check entry");

	ok(conf_io_unset("server", "version", NULL, "text") ==
	   KNOT_EOK, "unset explicit value");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");

	// Restart transaction.
497 498
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

	ok(conf_io_unset("server", "version", NULL, NULL) ==
	   KNOT_EOK, "unset value");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");

	// Single group, multi value.
	ok(conf_io_unset("server", "listen", NULL, "9.9.9.9") ==
	   KNOT_ENOENT, "unset bad value");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_LISTEN);
	ok(val.code == KNOT_EOK, "check entry");

	ok(conf_io_unset("server", "listen", NULL, "1.1.1.1") ==
	   KNOT_EOK, "unset explicit value");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_LISTEN);
	ok(val.code == KNOT_EOK, "check entry");
	ok(conf_val_count(&val) == 1, "check entry value count");

	ok(conf_io_unset("server", "listen", NULL, NULL) ==
	   KNOT_EOK, "unset value");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_LISTEN);
	ok(val.code == KNOT_ENOENT, "check entry");

	// Restart transaction.
523 524
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
525 526 527 528 529 530 531 532 533 534

	// Whole section items.
	ok(conf_io_unset("server", NULL, NULL, NULL) ==
	   KNOT_EOK, "unset section");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_LISTEN);
	ok(val.code == KNOT_ENOENT, "check entry");

	// Restart transaction.
535 536
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
537 538 539 540 541 542 543 544 545 546 547 548 549

	// Prepare dnames.
	knot_dname_t *zone1 = knot_dname_from_str_alloc(ZONE1);
	ok(zone1 != NULL, "create dname "ZONE1);
	knot_dname_t *zone2 = knot_dname_from_str_alloc(ZONE2);
	ok(zone2 != NULL, "create dname "ZONE2);
	knot_dname_t *zone3 = knot_dname_from_str_alloc(ZONE3);
	ok(zone3 != NULL, "create dname "ZONE3);

	// Multi group, single value.
	ok(conf_io_unset("zone", "file", ZONE1, "name") ==
	   KNOT_EOK, "unset zone file");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_FILE, zone1);
550
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
551 552 553 554 555 556 557 558 559 560 561 562

	// Multi group, single bad value, all ids.
	ok(conf_io_unset("zone", "comment", NULL, "other") ==
	   KNOT_EOK, "unset zones comment");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
	ok(val.code == KNOT_EOK, "check entry");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
	ok(val.code == KNOT_EOK, "check entry");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone3);
	ok(val.code == KNOT_EOK, "check entry");

	// Restart transaction.
563 564
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
565 566 567 568 569

	// Multi group, single value (not all match), all ids.
	ok(conf_io_unset("zone", "comment", NULL, "abc") ==
	   KNOT_EOK, "unset some zones comment");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
570
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
571
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
572
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
573 574 575 576
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone3);
	ok(val.code == KNOT_EOK, "check entry");

	// Restart transaction.
577 578
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
579 580 581 582 583

	// Multi group, single value (all match), all ids.
	ok(conf_io_unset("zone", "comment", NULL, NULL) ==
	   KNOT_EOK, "unset all zones comment");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
584
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
585
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
586
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
587
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone3);
588
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
589 590

	// Restart transaction.
591 592
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
593 594 595 596 597

	// Multi group, all items, specific id.
	ok(conf_io_unset("zone", NULL, ZONE1, NULL) ==
	   KNOT_EOK, "unset zone items");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_FILE, zone1);
598
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
599
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
600
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
601 602 603 604
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
	ok(val.code == KNOT_EOK, "check entry");

	// Restart transaction.
605 606
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
607 608 609 610 611

	// Multi group, all items, all ids.
	ok(conf_io_unset("zone", NULL, NULL, NULL) ==
	   KNOT_EOK, "unset zone items");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_FILE, zone1);
612
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
613
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
614
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
615
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
616
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
617 618

	// Restart transaction.
619 620
	conf_io_abort(false);
	ok(conf_io_begin(false) == KNOT_EOK, "restart txn");
621 622 623 624 625 626 627 628 629

	// All groups.
	ok(conf_io_unset(NULL, NULL, NULL, NULL) ==
	   KNOT_EOK, "unset all");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_VERSION);
	ok(val.code == KNOT_ENOENT, "check entry");
	val = conf_get_txn(conf(), conf()->io.txn, C_SERVER, C_LISTEN);
	ok(val.code == KNOT_ENOENT, "check entry");
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_FILE, zone1);
630
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
631
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone1);
632
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
633
	val = conf_zone_get_txn(conf(), conf()->io.txn, C_COMMENT, zone2);
634
	ok(val.code == KNOT_YP_EINVAL_ID, "check entry");
635 636 637 638 639

	knot_dname_free(&zone1, NULL);
	knot_dname_free(&zone2, NULL);
	knot_dname_free(&zone3, NULL);

640
	conf_io_abort(false);
641 642
}

643
static void test_conf_io_get(void)
644 645 646 647 648 649 650 651 652 653 654
{
	const char *ref;
	char out[OUT_LEN];

	conf_io_t io = {
		.fcn = format_item,
		.misc = out
	};

	// ERR no txn.
	ok(conf_io_get("server", "version", NULL, false, &io) ==
655
	   KNOT_TXN_ENOTEXISTS, "get without active txn");
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

	// Get current, no active txn.
	*out = '\0';
	ok(conf_io_get("server", "version", NULL, true, &io) ==
	   KNOT_EOK, "get current without active txn");
	ref = "server.version = \"text\"";
	ok(strcmp(ref, out) == 0, "compare result");

	ok(conf_io_begin(false) == KNOT_EOK, "begin txn");

	// ERR.
	ok(conf_io_get("", NULL, NULL, true, &io) ==
	   KNOT_YP_EINVAL_ITEM, "get empty key0");
	ok(conf_io_get("uknown", NULL, NULL, true, &io) ==
	   KNOT_YP_EINVAL_ITEM, "get unknown key0");
	ok(conf_io_get("server", "unknown", NULL, true, &io) ==
	   KNOT_YP_EINVAL_ITEM, "get unknown key1");
	ok(conf_io_get("include", NULL, NULL, true, &io) ==
	   KNOT_ENOTSUP, "get non-group item");

	// Update item in the active txn.
677
	ok(conf_io_set("server", "version", NULL, "new text") ==
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	   KNOT_EOK, "set single value");

	// Get new, active txn.
	*out = '\0';
	ok(conf_io_get("server", "version", NULL, false, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "server.version = \"new text\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Get current, active txn.
	*out = '\0';
	ok(conf_io_get("server", "version", NULL, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "server.version = \"text\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Multi value.
	*out = '\0';
	ok(conf_io_get("server", "listen", NULL, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "server.listen = \"1.1.1.1\" \"1.1.1.2\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Single group.
	*out = '\0';
	ok(conf_io_get("server", NULL, NULL, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "server.version = \"text\"\n"
	      "server.listen = \"1.1.1.1\" \"1.1.1.2\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Prepare dnames.
	knot_dname_t *zone1 = knot_dname_from_str_alloc(ZONE1);
	ok(zone1 != NULL, "create dname "ZONE1);

	// Multi group, all values, all ids.
	*out = '\0';
	ok(conf_io_get("zone", NULL, NULL, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "zone.domain = \"zone1.\"\n"
	      "zone[zone1.].file = \"name\"\n"
	      "zone[zone1.].comment = \"abc\"\n"
	      "zone.domain = \"zone2.\"\n"
	      "zone[zone2.].comment = \"abc\"\n"
	      "zone.domain = \"zone3.\"\n"
	      "zone[zone3.].comment = \"xyz\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Multi group ids.
	*out = '\0';
	ok(conf_io_get("zone", "domain", NULL, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "zone.domain = \"zone1.\"\n"
	      "zone.domain = \"zone2.\"\n"
	      "zone.domain = \"zone3.\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Multi group, all values, single id.
	*out = '\0';
	ok(conf_io_get("zone", NULL, ZONE1, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "zone.domain = \"zone1.\"\n"
	      "zone[zone1.].file = \"name\"\n"
	      "zone[zone1.].comment = \"abc\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Multi group, single value, single id.
	*out = '\0';
	ok(conf_io_get("zone", "file", ZONE1, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "zone[zone1.].file = \"name\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// All groups.
	*out = '\0';
	ok(conf_io_get(NULL, NULL, NULL, true, &io) ==
	   KNOT_EOK, "get with active txn");
	ref = "server.version = \"text\"\n"
	      "server.listen = \"1.1.1.1\" \"1.1.1.2\"\n"
	      "zone.domain = \"zone1.\"\n"
	      "zone[zone1.].file = \"name\"\n"
	      "zone[zone1.].comment = \"abc\"\n"
	      "zone.domain = \"zone2.\"\n"
	      "zone[zone2.].comment = \"abc\"\n"
	      "zone.domain = \"zone3.\"\n"
	      "zone[zone3.].comment = \"xyz\"";
	ok(strcmp(ref, out) == 0, "compare result");

	knot_dname_free(&zone1, NULL);

768
	conf_io_abort(false);
769 770
}

771
static void test_conf_io_diff(void)
772 773 774 775 776 777 778 779 780 781 782
{
	const char *ref;
	char out[OUT_LEN];

	conf_io_t io = {
		.fcn = format_item,
		.misc = out
	};

	// ERR no txn.
	ok(conf_io_diff("server", "version", NULL, &io) ==
783
	   KNOT_TXN_ENOTEXISTS, "diff without active txn");
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

	ok(conf_io_begin(false) == KNOT_EOK, "begin txn");

	// ERR.
	ok(conf_io_diff("", NULL, NULL, &io) ==
	   KNOT_YP_EINVAL_ITEM, "diff empty key0");
	ok(conf_io_diff("uknown", NULL, NULL, &io) ==
	   KNOT_YP_EINVAL_ITEM, "diff unknown key0");
	ok(conf_io_diff("server", "unknown", NULL, &io) ==
	   KNOT_YP_EINVAL_ITEM, "diff unknown key1");
	ok(conf_io_diff("include", NULL, NULL, &io) ==
	   KNOT_ENOTSUP, "diff non-group item");

	*out = '\0';
	ok(conf_io_diff(NULL, NULL, NULL, &io) == KNOT_EOK, "diff no change");
	ref = "";
	ok(strcmp(ref, out) == 0, "compare result");

	// Update singlevalued item.
803
	ok(conf_io_set("server", "version", NULL, "new text") ==
804 805 806 807 808 809 810 811 812 813 814
	   KNOT_EOK, "set single value");

	*out = '\0';
	ok(conf_io_diff("server", "version", NULL, &io) == KNOT_EOK, "diff single item");
	ref = "-server.version = \"text\"\n"
	      "+server.version = \"new text\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Update multivalued item.
	ok(conf_io_unset("server", "listen", NULL, "1.1.1.1") ==
	   KNOT_EOK, "unset multivalue");
815
	ok(conf_io_set("server", "listen", NULL, "1.1.1.3") ==
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 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	   KNOT_EOK, "set multivalue");

	*out = '\0';
	ok(conf_io_diff("server", "listen", NULL, &io) == KNOT_EOK, "diff multi item");
	ref = "-server.listen = \"1.1.1.1\" \"1.1.1.2\"\n"
	      "+server.listen = \"1.1.1.2\" \"1.1.1.3\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Unset single item.
	ok(conf_io_unset("zone", "comment", ZONE3, NULL) ==
	   KNOT_EOK, "unset multivalue");

	*out = '\0';
	ok(conf_io_diff("zone", NULL, ZONE3, &io) == KNOT_EOK, "diff section");
	ref = "-zone[zone3.].comment = \"xyz\"";
	ok(strcmp(ref, out) == 0, "compare result");

	// Unset id.
	ok(conf_io_unset("zone", NULL, ZONE1, NULL) ==
	   KNOT_EOK, "unset id");
	ok(conf_io_unset("zone", NULL, ZONE2, NULL) ==
	   KNOT_EOK, "unset id");

	*out = '\0';
	ok(conf_io_diff("zone", NULL, ZONE2, &io) == KNOT_EOK, "diff id section");
	ref = "-zone.domain = \"zone2.\"\n"
	      "-zone[zone2.].comment = \"abc\"";
	ok(strcmp(ref, out) == 0, "compare result");

	*out = '\0';
	ok(conf_io_diff("zone", "domain", NULL, &io) == KNOT_EOK, "diff id");
	ref = "-zone.domain = \"zone1.\"\n"
	      "-zone.domain = \"zone2.\"";
	ok(strcmp(ref, out) == 0, "compare result");

	*out = '\0';
	ok(conf_io_diff(NULL, NULL, NULL, &io) == KNOT_EOK, "diff whole change");
	ref = "-server.version = \"text\"\n"
	      "+server.version = \"new text\"\n"
	      "-server.listen = \"1.1.1.1\" \"1.1.1.2\"\n"
	      "+server.listen = \"1.1.1.2\" \"1.1.1.3\"\n"
	      "-zone.domain = \"zone1.\"\n"
	      "-zone[zone1.].file = \"name\"\n"
	      "-zone[zone1.].comment = \"abc\"\n"
	      "-zone.domain = \"zone2.\"\n"
	      "-zone[zone2.].comment = \"abc\"\n"
	      "-zone[zone3.].comment = \"xyz\"";
	ok(strcmp(ref, out) == 0, "compare result");

865
	conf_io_abort(false);
866 867
}

868
static void test_conf_io_list(void)
869 870 871 872 873 874 875 876 877 878
{
	const char *ref;
	char out[OUT_LEN];

	conf_io_t io = {
		.fcn = format_item,
		.misc = out
	};

	// ERR.
879 880 881 882 883 884
	ok(conf_io_list("", &io) ==
	   KNOT_YP_EINVAL_ITEM, "list empty key0");
	ok(conf_io_list("uknown", &io) ==
	   KNOT_YP_EINVAL_ITEM, "list unknown key0");
	ok(conf_io_list("include", &io) ==
	   KNOT_ENOTSUP, "list non-group item");
885 886 887

	// Desc schema.
	*out = '\0';
888 889
	ok(conf_io_list(NULL, &io) ==
	   KNOT_EOK, "list schema");
890
	ref = "server\n"
891
	      "control\n"
892 893 894 895 896 897 898 899
	      "remote\n"
	      "template\n"
	      "zone\n"
	      "include";
	ok(strcmp(ref, out) == 0, "compare result");

	// Desc group.
	*out = '\0';
900 901
	ok(conf_io_list("server", &io) ==
	   KNOT_EOK, "list group");
902
	ref = "server.version\n"
903
	      "server.background-workers\n"
904
	      "server.listen\n"
905 906 907 908
	      "server.tcp-handshake-timeout\n"
	      "server.tcp-idle-timeout\n"
	      "server.tcp-reply-timeout\n"
	      "server.max-tcp-clients\n"
909 910
	      "server.max-udp-payload\n"
	      "server.max-ipv4-udp-payload\n"
911
	      "server.max-ipv6-udp-payload";
912 913 914 915
	ok(strcmp(ref, out) == 0, "compare result");
}

static const yp_item_t desc_server[] = {
916
	{ C_VERSION,              YP_TSTR,  YP_VNONE },
917
	{ C_BG_WORKERS,           YP_TINT,  YP_VNONE },
918 919
	{ C_LISTEN,               YP_TADDR, YP_VNONE, YP_FMULTI },
	// Required config cache items - assert fix.
920 921 922 923
	{ C_TCP_HSHAKE_TIMEOUT,   YP_TINT,  YP_VNONE },
	{ C_TCP_IDLE_TIMEOUT,	  YP_TINT,  YP_VNONE },
	{ C_TCP_REPLY_TIMEOUT,	  YP_TINT,  YP_VNONE },
	{ C_MAX_TCP_CLIENTS,	  YP_TINT,  YP_VNONE },
924 925 926
	{ C_MAX_UDP_PAYLOAD,      YP_TINT,  YP_VNONE },
	{ C_MAX_IPV4_UDP_PAYLOAD, YP_TINT,  YP_VNONE },
	{ C_MAX_IPV6_UDP_PAYLOAD, YP_TINT,  YP_VNONE },
927 928 929
	{ NULL }
};

930 931 932 933 934
static const yp_item_t desc_control[] = {
	{ C_TIMEOUT, YP_TINT, YP_VNONE },
	{ NULL }
};

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
static const yp_item_t desc_remote[] = {
	{ C_ID,   YP_TSTR,  YP_VNONE },
	{ C_ADDR, YP_TADDR, YP_VNONE, YP_FMULTI },
	{ NULL }
};

#define ZONE_ITEMS \
	{ C_FILE,           YP_TSTR,  YP_VNONE }, \
	{ C_MASTER,         YP_TREF,  YP_VREF = { C_RMT }, YP_FMULTI, { check_ref } }, \
	{ C_DNSSEC_SIGNING, YP_TBOOL, YP_VNONE }, \
	{ C_COMMENT,        YP_TSTR,  YP_VNONE },

static const yp_item_t desc_template[] = {
	{ C_ID, YP_TSTR, YP_VNONE },
	ZONE_ITEMS
	{ NULL }
};

static const yp_item_t desc_zone[] = {
	{ C_DOMAIN, YP_TDNAME, YP_VNONE },
	ZONE_ITEMS
	{ NULL }
};

959
const yp_item_t test_schema[] = {
960
	{ C_SRV,  YP_TGRP, YP_VGRP = { desc_server } },
961
	{ C_CTL,  YP_TGRP, YP_VGRP = { desc_control } },
962 963 964 965 966 967 968 969 970 971 972
	{ C_RMT,  YP_TGRP, YP_VGRP = { desc_remote }, YP_FMULTI, { check_remote } },
	{ C_TPL,  YP_TGRP, YP_VGRP = { desc_template }, YP_FMULTI, { check_template } },
	{ C_ZONE, YP_TGRP, YP_VGRP = { desc_zone }, YP_FMULTI, { check_zone } },
	{ C_INCL, YP_TSTR, YP_VNONE, YP_FNONE, { include_file } },
	{ NULL }
};

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

973
	ok(test_conf("", test_schema) == KNOT_EOK, "Prepare configuration");
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

	diag("conf_io_begin");
	test_conf_io_begin();

	diag("conf_io_abort");
	test_conf_io_abort();

	diag("conf_io_commit");
	test_conf_io_commit();

	diag("conf_io_check");
	test_conf_io_check();

	diag("conf_io_set");
	test_conf_io_set();

	diag("conf_io_unset");
	test_conf_io_unset();

	diag("conf_io_get");
	test_conf_io_get();

	diag("conf_io_diff");
	test_conf_io_diff();

999 1000
	diag("conf_io_list");
	test_conf_io_list();
1001

1002
	conf_free(conf());
1003 1004 1005

	return 0;
}