1 /*  Copyright (C) 2021 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 <tap/basic.h>
18 #include <tap/files.h>
19 
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 
23 #include "contrib/sockaddr.h"
24 #include "libknot/packet/pkt.c"
25 #include "libknot/probe/probe.h"
26 
main(int argc,char * argv[])27 int main(int argc, char *argv[])
28 {
29 	plan_lazy();
30 
31 	knot_probe_t *probe_out = knot_probe_alloc();
32 	ok(probe_out != NULL, "probe: initialize output probe");
33 	knot_probe_t *probe_in = knot_probe_alloc();
34 	ok(probe_in != NULL, "probe: initialize input probe");
35 
36 	int fd = knot_probe_fd(probe_out);
37 	ok(fd < 0, "probe: unavailable fd");
38 
39 	char *workdir = test_mkdtemp();
40 	ok(workdir != NULL, "probe: create temporary workdir");
41 
42 	int ret = knot_probe_set_producer(probe_out, workdir, 1);
43 	ok(ret == KNOT_ECONN, "probe: connect producer");
44 
45 	ret = knot_probe_set_consumer(probe_in, workdir, 1);
46 	ok(ret == KNOT_EOK, "probe: connect consumer");
47 	fd = knot_probe_fd(probe_in);
48 	ok(fd >= 0, "probe: get input probe fd");
49 
50 	ret = knot_probe_set_producer(probe_out, workdir, 1);
51 	ok(ret == KNOT_EOK, "probe: reconnect producer");
52 	fd = knot_probe_fd(probe_out);
53 	ok(fd >= 0, "probe: get output probe fd");
54 
55 	struct sockaddr_storage addr;
56 	ret = sockaddr_set(&addr, AF_INET, "192.168.0.1", 53);
57 	ok(ret == KNOT_EOK, "probe: set address");
58 
59 	knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, NULL);
60 	ok(query != NULL, "probe: create query");
61 	knot_pkt_t *reply = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, NULL);
62 	ok(reply != NULL, "probe: create reply");
63 
64 	ret = knot_pkt_put_question(query, (const uint8_t *)"\x04test\x00",
65 	                            KNOT_CLASS_IN, KNOT_RRTYPE_SOA);
66 	ok(ret == KNOT_EOK, "probe: put query");
67 
68 	knot_probe_data_t data_out;
69 	ret = knot_probe_data_set(&data_out, KNOT_PROBE_PROTO_UDP,
70 	                          &addr, &addr, query, reply, KNOT_RCODE_NXDOMAIN);
71 	ok(ret == KNOT_EOK, "probe: connect producer");
72 
73 	knot_pkt_free(query);
74 	knot_pkt_free(reply);
75 
76 	ret = knot_probe_produce(probe_out, &data_out, 1);
77 	ok(ret == KNOT_EOK, "probe: produce datagram");
78 
79 	knot_probe_data_t data_in;
80 	ret = knot_probe_consume(probe_in, &data_in, 1, 20);
81 	ok(ret == 1, "probe: consume datagram");
82 
83 	ret = memcmp(&data_in, &data_out, offsetof(knot_probe_data_t, query.qname));
84 	ok(ret == 0, "probe: data comparsion");
85 
86 	ret = knot_dname_cmp(data_in.query.qname, data_out.query.qname);
87 	ok(ret == 0, "probe: qname comparison");
88 
89 	knot_probe_free(probe_in);
90 	knot_probe_free(probe_out);
91 
92 	test_rm_rf(workdir);
93 	free(workdir);
94 
95 	return 0;
96 }
97