1 #include <test.h>
2 
3 #include <string.h>
4 #include <findhub.h>
5 #include <misc_lib.h>                                          /* xsnprintf */
6 
7 #include <avahi-client/client.h>
8 #include <avahi-client/lookup.h>
9 
10 #include <avahi-common/simple-watch.h>
11 #include <avahi-common/malloc.h>
12 #include <avahi-common/error.h>
13 #include <avahi-common/address.h>
14 
15 
16 void (*avahi_simple_poll_quit_ptr)(AvahiSimplePoll *);
17 char* (*avahi_address_snprint_ptr)(char *, size_t , const AvahiAddress *);
18 int (*avahi_service_resolver_free_ptr)(AvahiServiceResolver *);
19 int (*avahi_client_errno_ptr)(AvahiClient *);
20 const char* (*avahi_strerror_ptr)(int);
21 AvahiServiceResolver* (*avahi_service_resolver_new_ptr)(AvahiClient *, AvahiIfIndex, AvahiProtocol, const char *, const char *, const char *, AvahiProtocol, AvahiLookupFlags, AvahiServiceResolverCallback, void *);
22 AvahiClient* (*avahi_service_browser_get_client_ptr)(AvahiServiceBrowser *);
23 AvahiClient* (*avahi_service_resolver_get_client_ptr)(AvahiServiceResolver *);
24 AvahiSimplePoll* (*avahi_simple_poll_new_ptr)();
25 const AvahiPoll* (*avahi_simple_poll_get_ptr)(AvahiSimplePoll *s);
26 AvahiClient* (*avahi_client_new_ptr)(const AvahiPoll *, AvahiClientFlags, AvahiClientCallback, void *, int *);
27 int (*avahi_simple_poll_loop_ptr)(AvahiSimplePoll *);
28 int (*avahi_service_browser_free_ptr)(AvahiServiceBrowser *);
29 void (*avahi_client_free_ptr)(AvahiClient *client);
30 void (*avahi_simple_poll_free_ptr)(AvahiSimplePoll *);
31 AvahiServiceBrowser* (*avahi_service_browser_new_ptr)(AvahiClient *, AvahiIfIndex, AvahiProtocol, const char *, const char *, AvahiLookupFlags, AvahiServiceBrowserCallback, void*);
32 
33 int hostcount;
34 
dlopen(const char * filename,int flag)35 void *dlopen(const char *filename, int flag)
36 {
37     return (void*)1;
38 }
39 
dlsym(void * handle,const char * symbol)40 void *dlsym(void *handle, const char *symbol)
41 {
42     if (strcmp(symbol, "avahi_simple_poll_quit") == 0)
43     {
44         return &avahi_simple_poll_quit;
45     }
46     else if (strcmp(symbol, "avahi_address_snprint") == 0)
47     {
48         return &avahi_address_snprint;
49     }
50     else if (strcmp(symbol, "avahi_service_resolver_free") == 0)
51     {
52         return &avahi_service_resolver_free;
53     }
54     else if (strcmp(symbol, "avahi_client_errno") == 0)
55     {
56         return &avahi_client_errno;
57     }
58     else if (strcmp(symbol, "avahi_strerror") == 0)
59     {
60         return &avahi_strerror;
61     }
62     else if (strcmp(symbol, "avahi_service_resolver_new") == 0)
63     {
64         return &avahi_service_resolver_new;
65     }
66     else if (strcmp(symbol, "avahi_service_browser_get_client") == 0)
67     {
68         return &avahi_service_browser_get_client;
69     }
70     else if (strcmp(symbol, "avahi_service_resolver_get_client") == 0)
71     {
72         return &avahi_service_resolver_get_client;
73     }
74     else if (strcmp(symbol, "avahi_simple_poll_new") == 0)
75     {
76         return &avahi_simple_poll_new;
77     }
78     else if (strcmp(symbol, "avahi_simple_poll_get") == 0)
79     {
80         return &avahi_simple_poll_get;
81     }
82     else if (strcmp(symbol, "avahi_client_new") == 0)
83     {
84         return &avahi_client_new;
85     }
86     else if (strcmp(symbol, "avahi_simple_poll_loop") == 0)
87     {
88         return &avahi_simple_poll_loop;
89     }
90     else if (strcmp(symbol, "avahi_service_browser_free") == 0)
91     {
92         return &avahi_service_browser_free;
93     }
94     else if (strcmp(symbol, "avahi_client_free") == 0)
95     {
96         return &avahi_client_free;
97     }
98     else if (strcmp(symbol, "avahi_simple_poll_free") == 0)
99     {
100         return &avahi_simple_poll_free;
101     }
102     else if (strcmp(symbol, "avahi_service_browser_new") == 0)
103     {
104         return &avahi_service_browser_new;
105     }
106 
107     return NULL;
108 }
109 
dlclose(void * handle)110 int dlclose(void *handle)
111 {
112     return 0;
113 }
114 
avahi_simple_poll_new()115 AvahiSimplePoll *avahi_simple_poll_new()
116 {
117     AvahiSimplePoll *sp = (AvahiSimplePoll*)1;
118 
119     return sp;
120 }
121 
avahi_simple_poll_free(AvahiSimplePoll * poll)122 void avahi_simple_poll_free(AvahiSimplePoll *poll)
123 {
124 
125 }
126 
avahi_simple_poll_get(AvahiSimplePoll * s)127 const AvahiPoll *avahi_simple_poll_get(AvahiSimplePoll *s)
128 {
129     AvahiPoll *p = { 0 };
130 
131     return p;
132 }
133 
avahi_simple_poll_quit(AvahiSimplePoll * poll)134 void avahi_simple_poll_quit(AvahiSimplePoll *poll)
135 {
136 
137 }
138 
avahi_address_snprint(char * buffer,size_t size,const AvahiAddress * address)139 char *avahi_address_snprint(char *buffer, size_t size, const AvahiAddress *address)
140 {
141     xsnprintf(buffer, size, "10.0.0.100");
142 
143     return NULL;
144 }
145 
avahi_service_resolver_free(AvahiServiceResolver * resolver)146 int avahi_service_resolver_free(AvahiServiceResolver *resolver)
147 {
148    return 0;
149 }
150 
avahi_client_errno(AvahiClient * c)151 int avahi_client_errno(AvahiClient *c)
152 {
153     return 1;
154 }
155 
avahi_strerror(int error)156 const char *avahi_strerror(int error)
157 {
158     return "Avahi error occurred";
159 }
160 
avahi_service_resolver_new(AvahiClient * c,AvahiIfIndex index,AvahiProtocol protocol,const char * s1,const char * s2,const char * s3,AvahiProtocol protocol2,AvahiLookupFlags flags,AvahiServiceResolverCallback callback,void * data)161 AvahiServiceResolver *avahi_service_resolver_new(AvahiClient *c, AvahiIfIndex index, AvahiProtocol protocol, const char * s1, const char *s2, const char *s3,
162                                                  AvahiProtocol protocol2, AvahiLookupFlags flags, AvahiServiceResolverCallback callback, void *data)
163 {
164     return (AvahiServiceResolver *)1;
165 }
166 
avahi_service_browser_get_client(AvahiServiceBrowser * sb)167 AvahiClient *avahi_service_browser_get_client(AvahiServiceBrowser *sb)
168 {
169     return (AvahiClient *)1;
170 }
171 
avahi_service_resolver_get_client(AvahiServiceResolver * sr)172 AvahiClient *avahi_service_resolver_get_client(AvahiServiceResolver *sr)
173 {
174     return (AvahiClient *)1;
175 }
176 
avahi_client_new(const AvahiPoll * poll,AvahiClientFlags cf,AvahiClientCallback callback,void * data,int * stat)177 AvahiClient *avahi_client_new(const AvahiPoll *poll, AvahiClientFlags cf, AvahiClientCallback callback, void *data, int *stat)
178 {
179     if (hostcount == 4)
180     {
181         return NULL;
182     }
183 
184     return (AvahiClient *)1;
185 }
186 
avahi_simple_poll_loop(AvahiSimplePoll * sp)187 int avahi_simple_poll_loop(AvahiSimplePoll *sp)
188 {
189     AvahiAddress *addr = calloc(1, sizeof(AvahiAddress));
190     AvahiServiceResolver *sr = { (AvahiServiceResolver*)1 };
191     switch(hostcount)
192     {
193     case 1:
194         resolve_callback(sr, 0, 0, AVAHI_RESOLVER_FOUND, "cfenginehub", "tcp", "local", "host1", addr, 5308, NULL, 0, NULL);
195         return 0;
196 
197     case 2:
198         resolve_callback(sr, 0, 0, AVAHI_RESOLVER_FOUND, "cfenginehub", "tcp", "local", "host1", addr, 5308, NULL, 0, NULL);
199         resolve_callback(sr, 0, 0, AVAHI_RESOLVER_FOUND, "cfenginehub", "tcp", "local", "host2", addr, 1234, NULL, 0, NULL);
200         resolve_callback(sr, 0, 0, AVAHI_RESOLVER_FOUND, "cfenginehub", "tcp", "local", "host3", addr, 4321, NULL, 0, NULL);
201         return 0;
202 
203     default:
204         free(addr);
205     };
206 
207     return 0;
208 }
209 
avahi_service_browser_free(AvahiServiceBrowser * sb)210 int avahi_service_browser_free(AvahiServiceBrowser *sb)
211 {
212     return 0;
213 }
214 
avahi_client_free(AvahiClient * c)215 void avahi_client_free(AvahiClient *c)
216 {
217 
218 }
219 
avahi_service_browser_new(AvahiClient * c,AvahiIfIndex index,AvahiProtocol protocol,const char * s1,const char * s2,AvahiLookupFlags flags,AvahiServiceBrowserCallback callback,void * data)220 AvahiServiceBrowser *avahi_service_browser_new(AvahiClient *c, AvahiIfIndex index, AvahiProtocol protocol, const char *s1, const char *s2, AvahiLookupFlags flags,
221                                                AvahiServiceBrowserCallback callback, void *data)
222 {
223     AvahiServiceBrowser *browser = (AvahiServiceBrowser *)1;
224 
225     return browser;
226 }
227 
test_noHubsFound(void)228 static void test_noHubsFound(void)
229 {
230     List *list = NULL;
231 
232     hostcount = 0;
233 
234     assert_int_equal(ListHubs(&list), 0);
235     assert_int_not_equal(list, NULL);
236 
237     ListDestroy(&list);
238 }
239 
test_oneHubFound(void)240 static void test_oneHubFound(void)
241 {
242     List *list = NULL;
243 
244     hostcount = 1;
245 
246     assert_int_equal(ListHubs(&list), 1);
247     assert_int_not_equal(list, NULL);
248 
249     ListIterator *i = NULL;
250     i = ListIteratorGet(list);
251     assert_true(i != NULL);
252     HostProperties *host = (HostProperties *)ListIteratorData(i);
253 
254     assert_int_equal(host->Port,5308);
255     assert_string_equal(host->Hostname, "host1");
256     assert_string_equal(host->IPAddress, "10.0.0.100");
257 
258     ListIteratorDestroy(&i);
259     ListDestroy(&list);
260 }
261 
test_multipleHubsFound(void)262 static void test_multipleHubsFound(void)
263 {
264     List *list = NULL;
265 
266     hostcount = 2;
267 
268     assert_int_equal(ListHubs(&list), 3);
269     assert_int_not_equal(list, NULL);
270 
271     ListIterator *i = NULL;
272     i = ListIteratorGet(list);
273 
274     HostProperties *host1 = (HostProperties *)ListIteratorData(i);
275     assert_int_not_equal(ListIteratorNext(i), -1);
276     HostProperties *host2 = (HostProperties *)ListIteratorData(i);
277     assert_int_not_equal(ListIteratorNext(i), -1);
278     HostProperties *host3 = (HostProperties *)ListIteratorData(i);
279 
280     assert_int_equal(host1->Port, 5308);
281     assert_string_equal(host1->Hostname, "host1");
282     assert_string_equal(host1->IPAddress, "10.0.0.100");
283 
284     assert_int_equal(host2->Port, 1234);
285     assert_string_equal(host2->Hostname, "host2");
286     assert_string_equal(host2->IPAddress, "10.0.0.100");
287 
288     assert_int_equal(host3->Port, 4321);
289     assert_string_equal(host3->Hostname, "host3");
290     assert_string_equal(host3->IPAddress, "10.0.0.100");
291 
292     ListIteratorDestroy(&i);
293     ListDestroy(&list);
294 }
295 
test_errorOccurred(void)296 static void test_errorOccurred(void)
297 {
298     List *list = NULL;
299 
300     hostcount = 4;
301 
302     assert_int_equal(ListHubs(&list), -1);
303 }
304 
main()305 int main()
306 {
307     PRINT_TEST_BANNER();
308     const UnitTest tests[] =
309     {
310           unit_test(test_noHubsFound),
311           unit_test(test_oneHubFound),
312           unit_test(test_multipleHubsFound),
313           unit_test(test_errorOccurred)
314     };
315 
316     return run_tests(tests);
317 }
318