summaryrefslogtreecommitdiffstats
path: root/tests/libknot/test_yptrafo.c
blob: 3014a27b2c89a2fbbd4134655f3e3cda31f80f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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>
#include <sys/socket.h>
#include <tap/basic.h>

#include "libknot/yparser/yptrafo.h"
#include "libknot/libknot.h"

static void int_test(const char *txt, int64_t num, yp_style_t s,
                     int64_t min, int64_t max)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TINT, YP_VINT = { min, max, YP_NIL, s } };

	diag("integer \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(yp_int(b) == num, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, s | YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void int_bad_test(const char *txt, int code, yp_style_t s,
                         int64_t min, int64_t max)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	yp_item_t i = { NULL, YP_TINT, YP_VINT = { min, max, YP_NIL, s } };

	diag("integer \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	ok(ret == code, "invalid txt to bin");
}

static void bool_test(const char *txt, bool val)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TBOOL, YP_VNONE };

	diag("boolean \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(yp_bool(b) == val, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void bool_bad_test(const char *txt, int code)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	yp_item_t i = { NULL, YP_TBOOL, YP_VNONE };

	diag("boolean \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	ok(ret == code, "invalid txt to bin");
}

static void opt_test(const char *txt, unsigned val, const knot_lookup_t *opts)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TOPT, YP_VOPT = { opts } };

	diag("option \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(b_len == 1, "compare length");
	ok(yp_opt(b) == val, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void opt_bad_test(const char *txt, int code, const knot_lookup_t *opts)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	yp_item_t i = { NULL, YP_TOPT, YP_VOPT = { opts } };

	diag("option \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	ok(ret == code, "invalid txt to bin");
}

static void str_test(const char *txt, const char *val)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TSTR, YP_VNONE };

	diag("string \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(b_len == strlen(txt) + 1, "compare length");
	ok(memcmp(yp_str(b), val, b_len) == 0, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void addr_test(const char *txt, bool port)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TADDR, YP_VNONE };

	diag("address \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	bool no_port;
	yp_addr(b, &no_port);
	ok(no_port == port, "compare port presence");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void addr_bad_test(const char *txt, int code)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	yp_item_t i = { NULL, YP_TADDR, YP_VNONE };

	diag("address \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	ok(ret == code, "invalid txt to bin");
}

static void addr_range_test(const char *txt)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TNET, YP_VNONE };

	diag("address range \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void addr_range_bad_test(const char *txt, int code)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	yp_item_t i = { NULL, YP_TNET, YP_VNONE };

	diag("address range \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	ok(ret == code, "invalid txt to bin");
}

static void dname_test(const char *txt, const char *val)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TDNAME, YP_VNONE };

	diag("dname \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(memcmp(yp_dname(b), val, b_len) == 0, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void hex_test(const char *txt, const char *val, const char *txt_out)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_THEX, YP_VNONE };

	if (txt_out == NULL) {
		txt_out = txt;
	}

	diag("hex \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(memcmp(yp_bin(b), val, yp_bin_len(b)) == 0, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt_out) == t_len, "txt length");
	ok(memcmp(txt_out, t, t_len) == 0, "compare");
}

static void hex_bad_test(const char *txt, int code)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	yp_item_t i = { NULL, YP_THEX, YP_VNONE };

	diag("hex \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	ok(ret == code, "invalid txt to bin");
}

static void base64_test(const char *txt, const char *val)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t i = { NULL, YP_TB64, YP_VNONE };

	diag("base64 \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(memcmp(yp_bin(b), val, yp_bin_len(b)) == 0, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

static void ref_test(const char *txt, bool val)
{
	int ret;
	uint8_t b[64];
	size_t b_len = sizeof(b);
	char t[64];
	size_t t_len = sizeof(t);
	yp_item_t id = { NULL, YP_TBOOL, YP_VNONE };
	yp_item_t ref = { NULL, YP_TGRP, YP_VNONE };
	yp_item_t i = { NULL, YP_TREF, YP_VNONE };
	ref.var.g.id = &id;
	i.var.r.ref = &ref;

	diag("reference to boolean \"%s\":", txt);
	ret = yp_item_to_bin(&i, txt, strlen(txt), b, &b_len);
	is_int(KNOT_EOK, ret, "txt to bin");
	ok(yp_bool(b) == val, "compare");
	ret = yp_item_to_txt(&i, b, b_len, t, &t_len, YP_SNOQUOTE);
	is_int(KNOT_EOK, ret, "bin to txt");
	ok(strlen(t) == t_len, "txt ret length");
	ok(strlen(txt) == t_len, "txt length");
	ok(memcmp(txt, t, t_len) == 0, "compare");
}

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

	/* Integer tests. */
	int64_t min = -20000000000, max = 20000000000;
	int_test("5", 5, YP_SNONE, min, max);
	int_test("0", 0, YP_SNONE, min, max);
	int_test("-5", -5, YP_SNONE, min, max);
	int_test("20000000000", max, YP_SNONE, min, max);
	int_test("-20000000000", min, YP_SNONE, min, max);
	int_test("11B", 11LL * 1, YP_SSIZE, min, max);
	int_test("11K", 11LL * 1024, YP_SSIZE, min, max);
	int_test("11M", 11LL * 1024 * 1024, YP_SSIZE, min, max);
	int_test("11G", 11LL * 1024 * 1024 * 1024, YP_SSIZE, min, max);
	int_test("11s", 11LL * 1, YP_STIME, min, max);
	int_test("11m", 11LL * 60, YP_STIME, min, max);
	int_test("11h", 11LL * 3600, YP_STIME, min, max);
	int_test("11d", 11LL * 24 * 3600, YP_STIME, min, max);
	int_test("1025B", 1025LL, YP_SSIZE, min, max);
	int_test("61s", 61LL, YP_STIME, min, max);
	int_bad_test("20000000001", KNOT_ERANGE, YP_SNONE, min, max);
	int_bad_test("-20000000001", KNOT_ERANGE, YP_SNONE, min, max);
	int_bad_test("1x", KNOT_EINVAL, YP_SNONE, min, max);
	int_bad_test("1sx", KNOT_EINVAL, YP_STIME, min, max);

	/* Boolean tests. */
	bool_test("on", true);
	bool_test("off", false);
	bool_bad_test("onx", KNOT_EINVAL);
	bool_bad_test("enable", KNOT_EINVAL);

	/* Option tests. */
	static const knot_lookup_t opts[] = {
		{ 1,   "one" },
		{ 10,  "ten" },
		{ 255, "max" },
		{ 0, NULL }
	};
	opt_test("one", 1, opts);
	opt_test("ten", 10, opts);
	opt_test("max", 255, opts);
	opt_bad_test("onex", KNOT_EINVAL, opts);
	opt_bad_test("word", KNOT_EINVAL, opts);

	/* String tests. */
	str_test("Test string!", "Test string!");

	/* Address tests. */
	addr_test("192.168.123.1", true);
	addr_test("192.168.123.1@12345", false);
	addr_test("2001:db8::1", true);
	addr_test("::1@12345", false);
	addr_test("/tmp/test.sock", true);
	addr_bad_test("192.168.123.x", KNOT_EINVAL);
	addr_bad_test("192.168.123.1@", KNOT_EINVAL);
	addr_bad_test("192.168.123.1@1x", KNOT_EINVAL);
	addr_bad_test("192.168.123.1@65536", KNOT_ERANGE);

	/* Address range tests. */
	addr_range_test("1.1.1.1");
	addr_range_test("1.1.1.1/0");
	addr_range_test("1.1.1.1/32");
	addr_range_test("1.1.1.1-1.2.3.4");
	addr_range_test("::1");
	addr_range_test("::1/0");
	addr_range_test("::1/32");
	addr_range_test("1::-5::");
	addr_range_bad_test("unix", KNOT_EINVAL);
	addr_range_bad_test("1.1.1", KNOT_EINVAL);
	addr_range_bad_test("1.1.1.1/", KNOT_EINVAL);
	addr_range_bad_test("1.1.1.1/33", KNOT_ERANGE);
	addr_range_bad_test("1.1.1.1-", KNOT_EINVAL);
	addr_range_bad_test("1.1.1.1-::1", KNOT_EINVAL);

	/* Dname tests. */
	dname_test("example.com.", "\x07""example""\x03""com""\x00");

	/* Hex tests. */
	hex_test("", "", NULL);
	hex_test("0x", "", "");
	hex_test("Hello World!", "Hello World!", NULL);
	hex_test("0x0155FF", "\x01\x55\xFF", NULL);
	hex_bad_test("0xA", KNOT_EINVAL);

	/* Base64 tests. */
	base64_test("Zm9vYmFy", "foobar");

	/* Ref tests. */
	ref_test("on", true);

	return 0;
}