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