]> git.meshlink.io Git - catta/blob - avahi-compat-libdns_sd/txt-test.c
cc220f31a71c3f37b12e4be3a7d175e7ee267c10
[catta] / avahi-compat-libdns_sd / txt-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 <sys/types.h>
27 #include <assert.h>
28 #include <stdio.h>
29
30 #include <avahi-common/gccmacro.h>
31 #include <dns_sd.h>
32
33 static void hexdump(const void* p, size_t size) {
34     const uint8_t *c = p;
35     assert(p);
36
37     printf("Dumping %zu bytes from %p:\n", size, p);
38
39     while (size > 0) {
40         unsigned i;
41
42         for (i = 0; i < 16; i++) {
43             if (i < size)
44                 printf("%02x ", c[i]);
45             else
46                 printf("   ");
47         }
48
49         for (i = 0; i < 16; i++) {
50             if (i < size)
51                 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
52             else
53                 printf(" ");
54         }
55
56         printf("\n");
57
58         c += 16;
59
60         if (size <= 16)
61             break;
62
63         size -= 16;
64     }
65 }
66
67
68 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
69     const char *r;
70     TXTRecordRef ref;
71     uint8_t l;
72     const void *p;
73     char k[256];
74
75     TXTRecordCreate(&ref, 0, NULL);
76
77     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
78
79     TXTRecordSetValue(&ref, "foo", 7, "lennart");
80     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
81
82     TXTRecordSetValue(&ref, "waldo", 5, "rocks");
83     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
84
85     TXTRecordSetValue(&ref, "quux", 9, "the_house");
86     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
87
88     TXTRecordSetValue(&ref, "yeah", 0, NULL);
89     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
90
91     TXTRecordSetValue(&ref, "waldo", 6, "rocked");
92     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
93
94     TXTRecordRemoveValue(&ref, "foo");
95     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
96
97     TXTRecordRemoveValue(&ref, "waldo");
98     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
99
100     TXTRecordSetValue(&ref, "kawumm", 6, "bloerb");
101     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
102
103     TXTRecordSetValue(&ref, "one", 1, "1");
104     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
105
106     TXTRecordSetValue(&ref, "two", 1, "2");
107     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
108
109     TXTRecordSetValue(&ref, "three", 1, "3");
110     hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref));
111
112     assert(TXTRecordContainsKey(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "two"));
113     assert(!TXTRecordContainsKey(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "four"));
114
115     r = TXTRecordGetValuePtr(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "kawumm", &l);
116
117     hexdump(r, l);
118
119     assert(TXTRecordGetCount(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref)) == 6);
120
121     TXTRecordGetItemAtIndex(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), 2, sizeof(k), k, &l, &p);
122
123     fprintf(stderr, "key=<%s>\n", k);
124
125     hexdump(p, l);
126
127     assert(TXTRecordGetItemAtIndex(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), 20, sizeof(k), k, &l, &p) == kDNSServiceErr_Invalid);
128
129     TXTRecordDeallocate(&ref);
130 }