• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of avahi.
3 
4   avahi 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   avahi 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 avahi; 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 <stdio.h>
25 #include <assert.h>
26 
27 #include "strlst.h"
28 #include "avahi-malloc.h"
29 
main(AVAHI_GCC_UNUSED int argc,AVAHI_GCC_UNUSED char * argv[])30 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
31     char *t, *v;
32     uint8_t data[1024];
33     AvahiStringList *a = NULL, *b, *p;
34     size_t size, n;
35     int r;
36 
37     a = avahi_string_list_new("prefix", "a", "b", NULL);
38 
39     a = avahi_string_list_add(a, "start");
40     a = avahi_string_list_add(a, "foo=99");
41     a = avahi_string_list_add(a, "bar");
42     a = avahi_string_list_add(a, "");
43     a = avahi_string_list_add(a, "");
44     a = avahi_string_list_add(a, "quux");
45     a = avahi_string_list_add(a, "");
46     a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9);
47     a = avahi_string_list_add_printf(a, "seven=%i %c", 7, 'x');
48     a = avahi_string_list_add_pair(a, "blubb", "blaa");
49     a = avahi_string_list_add_pair(a, "uxknurz", NULL);
50     a = avahi_string_list_add_pair_arbitrary(a, "uxknurz2", (const uint8_t*) "blafasel\0oerks", 14);
51 
52     a = avahi_string_list_add(a, "end");
53 
54     t = avahi_string_list_to_string(a);
55     printf("--%s--\n", t);
56     avahi_free(t);
57 
58     n = avahi_string_list_serialize(a, NULL, 0);
59     size = avahi_string_list_serialize(a, data, sizeof(data));
60     assert(size == n);
61 
62     printf("%zu\n", size);
63 
64     for (t = (char*) data, n = 0; n < size; n++, t++) {
65         if (*t <= 32)
66             printf("(%u)", *t);
67         else
68             printf("%c", *t);
69     }
70 
71     printf("\n");
72 
73     assert(avahi_string_list_parse(data, size, &b) == 0);
74 
75     printf("equal: %i\n", avahi_string_list_equal(a, b));
76 
77     t = avahi_string_list_to_string(b);
78     printf("--%s--\n", t);
79     avahi_free(t);
80 
81     avahi_string_list_free(b);
82 
83     b = avahi_string_list_copy(a);
84 
85     assert(avahi_string_list_equal(a, b));
86 
87     t = avahi_string_list_to_string(b);
88     printf("--%s--\n", t);
89     avahi_free(t);
90 
91     p = avahi_string_list_find(a, "seven");
92     assert(p);
93 
94     r = avahi_string_list_get_pair(p, &t, &v, NULL);
95     assert(r >= 0);
96     assert(t);
97     assert(v);
98 
99     printf("<%s>=<%s>\n", t, v);
100     avahi_free(t);
101     avahi_free(v);
102 
103     p = avahi_string_list_find(a, "quux");
104     assert(p);
105 
106     r = avahi_string_list_get_pair(p, &t, &v, NULL);
107     assert(r >= 0);
108     assert(t);
109     assert(!v);
110 
111     printf("<%s>=<%s>\n", t, v);
112     avahi_free(t);
113     avahi_free(v);
114 
115     avahi_string_list_free(a);
116     avahi_string_list_free(b);
117 
118     n = avahi_string_list_serialize(NULL, NULL, 0);
119     size = avahi_string_list_serialize(NULL, data, sizeof(data));
120     assert(size == 1);
121     assert(size == n);
122 
123     assert(avahi_string_list_parse(data, size, &a) == 0);
124     assert(!a);
125 
126     return 0;
127 }
128