]> git.meshlink.io Git - catta/blob - tests/dns-test.c
Fix compilation error caused by ACX_THREAD
[catta] / tests / dns-test.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <assert.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29
30 #include <catta/domain.h>
31 #include <catta/defs.h>
32 #include <catta/malloc.h>
33 #include <catta/log.h>
34
35 #include "../src/dns.h"
36 #include "../src/util.h"
37
38 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char *argv[]) {
39     char t[CATTA_DOMAIN_NAME_MAX], *m;
40     const char *a, *b, *c, *d;
41     CattaDnsPacket *p;
42     CattaRecord *r, *r2;
43     uint8_t rdata[CATTA_DNS_RDATA_MAX];
44     size_t l;
45
46     p = catta_dns_packet_new(0);
47
48     assert(catta_dns_packet_append_name(p, a = "Ahello.hello.hello.de."));
49     assert(catta_dns_packet_append_name(p, b = "Bthis is a test.hello.de."));
50     assert(catta_dns_packet_append_name(p, c = "Cthis\\.is\\.a\\.test\\.with\\.dots.hello.de."));
51     assert(catta_dns_packet_append_name(p, d = "Dthis\\\\is another test.hello.de."));
52
53     catta_hexdump(CATTA_DNS_PACKET_DATA(p), p->size);
54
55     assert(catta_dns_packet_consume_name(p, t, sizeof(t)) == 0);
56     catta_log_debug(">%s<", t);
57     assert(catta_domain_equal(a, t));
58
59     assert(catta_dns_packet_consume_name(p, t, sizeof(t)) == 0);
60     catta_log_debug(">%s<", t);
61     assert(catta_domain_equal(b, t));
62
63     assert(catta_dns_packet_consume_name(p, t, sizeof(t)) == 0);
64     catta_log_debug(">%s<", t);
65     assert(catta_domain_equal(c, t));
66
67     assert(catta_dns_packet_consume_name(p, t, sizeof(t)) == 0);
68     catta_log_debug(">%s<", t);
69     assert(catta_domain_equal(d, t));
70
71     catta_dns_packet_free(p);
72
73     /* RDATA PARSING AND SERIALIZATION */
74
75     /* Create an CattaRecord with some usful data */
76     r = catta_record_new_full("foobar.local", CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_HINFO, CATTA_DEFAULT_TTL);
77     assert(r);
78     r->data.hinfo.cpu = catta_strdup("FOO");
79     r->data.hinfo.os = catta_strdup("BAR");
80
81     /* Serialize it into a blob */
82     assert((l = catta_rdata_serialize(r, rdata, sizeof(rdata))) != (size_t) -1);
83
84     /* Print it */
85     catta_hexdump(rdata, l);
86
87     /* Create a new record and fill in the data from the blob */
88     r2 = catta_record_new(r->key, CATTA_DEFAULT_TTL);
89     assert(r2);
90     assert(catta_rdata_parse(r2, rdata, l) >= 0);
91
92     /* Compare both versions */
93     assert(catta_record_equal_no_ttl(r, r2));
94
95     /* Free the records */
96     catta_record_unref(r);
97     catta_record_unref(r2);
98
99     r = catta_record_new_full("foobar", 77, 77, CATTA_DEFAULT_TTL);
100     assert(r);
101
102     assert(r->data.generic.data = catta_memdup("HALLO", r->data.generic.size = 5));
103
104     m = catta_record_to_string(r);
105     assert(m);
106
107     catta_log_debug(">%s<", m);
108
109     catta_free(m);
110     catta_record_unref(r);
111
112     return 0;
113 }