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