1 /*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 
3     This program is free software: you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation, either version 3 of the License, or
6     (at your option) any later version.
7 
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12 
13     You should have received a copy of the GNU General Public License
14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
15  */
16 
17 #include <stdbool.h>
18 #include <tap/basic.h>
19 
20 #include "libknot/rdata.h"
21 
main(int argc,char * argv[])22 int main(int argc, char *argv[])
23 {
24 	plan_lazy();
25 
26 	// Test array size
27 	ok(knot_rdata_size(1) == 2 + 1 + 1, "rdata: array size odd.");
28 	ok(knot_rdata_size(2) == 2 + 2, "rdata: array size even.");
29 
30 	// Test init
31 	const size_t data_size = 16;
32 	uint8_t buf1[knot_rdata_size(data_size)];
33 	knot_rdata_t *rdata = (knot_rdata_t *)buf1;
34 	uint8_t payload[] = "abcdefghijklmnop";
35 	knot_rdata_init(rdata, data_size, payload);
36 	const bool set_ok = rdata->len == data_size &&
37 	                    memcmp(rdata->data, payload, data_size) == 0;
38 	ok(set_ok, "rdata: init.");
39 
40 	// Test compare
41 	rdata->len = data_size;
42 	ok(knot_rdata_cmp(rdata, rdata) == 0, "rdata: cmp eq.");
43 
44 	knot_rdata_t *lower = rdata;
45 	uint8_t buf2[knot_rdata_size(data_size)];
46 	knot_rdata_t *greater = (knot_rdata_t *)buf2;
47 	knot_rdata_init(greater, data_size, (uint8_t *)"qrstuvwxyz123456");
48 	ok(knot_rdata_cmp(lower, greater) < 0, "rdata: cmp lower.");
49 	ok(knot_rdata_cmp(greater, lower) > 0, "rdata: cmp greater.");
50 
51 	// Payloads will be the same.
52 	memcpy(greater->data, lower->data, data_size);
53 	assert(knot_rdata_cmp(lower, greater) == 0);
54 
55 	lower->len = data_size - 1;
56 	ok(knot_rdata_cmp(lower, greater) < 0, "rdata: cmp lower size.");
57 	ok(knot_rdata_cmp(greater, lower) > 0, "rdata: cmp greater size.");
58 
59 	return 0;
60 }
61