1 /*
2  * Copyright (c) 2013, NLNet Labs, Verisign, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  *   notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  *   notice, this list of conditions and the following disclaimer in the
11  *   documentation and/or other materials provided with the distribution.
12  * * Neither the names of the copyright holders nor the
13  *   names of its contributors may be used to endorse or promote products
14  *   derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef _check_getdns_context_set_upstream_recursive_servers_h_
28 #define _check_getdns_context_set_upstream_recursive_servers_h_
29 
30     /*
31      ******************************************************************************************
32      *                                                                                        *
33      *  T E S T S  F O R  G E T D N S _ C O N T E X T _ S E T _ S T U B _ R E S O L U T I O N *
34      *                                                                                        *
35      ******************************************************************************************
36     */
37 
START_TEST(getdns_context_set_upstream_recursive_servers_1)38     START_TEST (getdns_context_set_upstream_recursive_servers_1)
39     {
40      /*
41       *  context is NULL
42       *  expect:  GETDNS_RETURN_INVALID_PARAMETER
43       */
44 
45       struct getdns_context *context = NULL;
46       struct getdns_list    *upstream_list = NULL;
47 
48       LIST_CREATE(upstream_list);
49 
50       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
51         GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_context_set_upstream_recursive_servers()");
52 
53             LIST_DESTROY(upstream_list);
54     }
55     END_TEST
56 
START_TEST(getdns_context_set_upstream_recursive_servers_2)57     START_TEST (getdns_context_set_upstream_recursive_servers_2)
58     {
59      /*
60       *  upstream_list  is NULL
61       *  expect: GETDNS_RETURN_INVALID_PARAMETER
62       */
63 
64       struct getdns_context *context = NULL;
65       CONTEXT_CREATE(TRUE);
66 
67       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, NULL),
68         GETDNS_RETURN_GOOD, "Return code from getdns_context_set_upstream_recursive_servers()");
69 
70 
71       CONTEXT_DESTROY;
72     }
73     END_TEST
74 
START_TEST(getdns_context_set_upstream_recursive_servers_3)75     START_TEST (getdns_context_set_upstream_recursive_servers_3)
76     {
77      /*
78       *  create upstream_list
79       *  create context
80       * a dict in upstream_list does not contain getdns_bindata
81       */
82 
83       struct getdns_context *context = NULL;
84       struct getdns_list    *upstream_list = NULL;
85       struct getdns_dict *dict = NULL;
86       size_t index = 0;
87 
88       CONTEXT_CREATE(TRUE);
89       LIST_CREATE(upstream_list);
90       DICT_CREATE(dict);
91 
92       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
93         "Return code from getdns_list_set_dict()");
94 
95 
96       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
97         GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Return code from getdns_context_set_upstream_recursive_servers()");
98 
99       CONTEXT_DESTROY;
100       LIST_DESTROY(upstream_list);
101       DICT_DESTROY(dict);
102     }
103     END_TEST
104 
START_TEST(getdns_context_set_upstream_recursive_servers_4)105     START_TEST (getdns_context_set_upstream_recursive_servers_4)
106     {
107      /*
108       *  create upstream_list
109       *  create context
110       * a dict in upstream_list does not contain two names
111       */
112 
113       struct getdns_context *context = NULL;
114       struct getdns_list    *upstream_list = NULL;
115       struct getdns_dict *dict = NULL;
116       struct getdns_bindata address_type = { 5, (void *) "IPv4" };
117       size_t index = 0;
118 
119       CONTEXT_CREATE(TRUE);
120       LIST_CREATE(upstream_list);
121       DICT_CREATE(dict);
122 
123       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
124         "Return code from getdns_list_set_dict()");
125 
126       ASSERT_RC(getdns_dict_set_bindata(dict, "address_type", &address_type),
127          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
128 
129 
130       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
131         GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Return code from getdns_context_set_upstream_recursive_servers()");
132 
133       CONTEXT_DESTROY;
134       LIST_DESTROY(upstream_list);
135       DICT_DESTROY(dict);
136     }
137     END_TEST
138 
START_TEST(getdns_context_set_upstream_recursive_servers_5)139     START_TEST (getdns_context_set_upstream_recursive_servers_5)
140     {
141      /*
142       *  create upstream_list
143       *  create context
144       * a dict in upstream_list contains names other than address_type ,
145          address_data, and port
146       */
147 
148       struct getdns_context *context = NULL;
149       struct getdns_list    *upstream_list = NULL;
150       struct getdns_dict *dict = NULL;
151       struct getdns_bindata address_data = { 4, (void *)"\x08\x08\x08\x08" };
152       size_t index = 0;
153 
154       CONTEXT_CREATE(TRUE);
155       LIST_CREATE(upstream_list);
156       DICT_CREATE(dict);
157 
158       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
159         "Return code from getdns_list_set_dict()");
160 
161      ASSERT_RC(getdns_dict_set_int(dict, "not_address_type", 100),
162          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()");
163 
164        ASSERT_RC(getdns_dict_set_bindata(dict, "not_address_data", &address_data),
165          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
166 
167       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
168         GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Return code from getdns_context_set_upstream_recursive_servers()");
169 
170       CONTEXT_DESTROY;
171       LIST_DESTROY(upstream_list);
172       DICT_DESTROY(dict);
173     }
174     END_TEST
175 
START_TEST(getdns_context_set_upstream_recursive_servers_6)176     START_TEST (getdns_context_set_upstream_recursive_servers_6)
177     {
178      /*
179       *  create upstream_list
180       *  create context
181       * a dict in upstream_list contains invalid address_ type (not “IPv4” or “IPv6”)
182       */
183 
184       struct getdns_context *context = NULL;
185       struct getdns_list    *upstream_list = NULL;
186       struct getdns_dict *dict = NULL;
187       struct getdns_bindata address_type = { 5, (void *)"IPv5" };
188        struct getdns_bindata address_data = { 4, (void *)"\x08\x08\x08\x08" };
189       size_t index = 0;
190 
191       CONTEXT_CREATE(TRUE);
192       LIST_CREATE(upstream_list);
193       DICT_CREATE(dict);
194 
195       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
196         "Return code from getdns_list_set_dict()");
197 
198      ASSERT_RC(getdns_dict_set_bindata(dict, "address_type", &address_type),
199          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
200        ASSERT_RC(getdns_dict_set_bindata(dict, "address_data", &address_data),
201          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
202 
203       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
204         GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Return code from getdns_context_set_upstream_recursive_servers()");
205 
206       CONTEXT_DESTROY;
207       LIST_DESTROY(upstream_list);
208       DICT_DESTROY(dict);
209     }
210     END_TEST
211 
START_TEST(getdns_context_set_upstream_recursive_servers_7)212     START_TEST (getdns_context_set_upstream_recursive_servers_7)
213     {
214      /*
215       *  create upstream_list
216       *  create context
217       * a dict in upstream_list contains named address_type and
218          address_data but the data type isn’t bindata
219       */
220 
221       struct getdns_context *context = NULL;
222       struct getdns_list    *upstream_list = NULL;
223       struct getdns_dict *dict = NULL;
224       size_t index = 0;
225 
226       CONTEXT_CREATE(TRUE);
227       LIST_CREATE(upstream_list);
228       DICT_CREATE(dict);
229 
230      ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
231         "Return code from getdns_list_set_dict()");
232 
233      ASSERT_RC(getdns_dict_set_int(dict, "address_type", 100),
234          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()");
235 
236        ASSERT_RC(getdns_dict_set_int(dict, "address_data", 200),
237          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()");
238 
239       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
240         GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Return code from getdns_context_set_upstream_recursive_servers()");
241 
242       CONTEXT_DESTROY;
243       LIST_DESTROY(upstream_list);
244       DICT_DESTROY(dict);
245     }
246     END_TEST
247 
START_TEST(getdns_context_set_upstream_recursive_servers_8)248     START_TEST (getdns_context_set_upstream_recursive_servers_8)
249     {
250      /*
251       *  create upstream_list
252       *  create context
253       *  a dict in upstream_list contains invalid address_data
254       */
255 
256       struct getdns_context *context = NULL;
257       struct getdns_list    *upstream_list = NULL;
258       struct getdns_dict *dict = NULL;
259       struct getdns_bindata address_type = { 5, (void *)"IPv5" };
260       struct getdns_bindata address_data = { 4, (void *)"\x08\x08\x08\x08" };
261       size_t index = 0;
262 
263       CONTEXT_CREATE(TRUE);
264       LIST_CREATE(upstream_list);
265       DICT_CREATE(dict);
266 
267      ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
268         "Return code from getdns_list_set_dict()");
269 
270      ASSERT_RC(getdns_dict_set_bindata(dict, "address_type", &address_type),
271          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
272        ASSERT_RC(getdns_dict_set_bindata(dict, "address_data", &address_data),
273          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
274 
275       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
276         GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Return code from getdns_context_set_upstream_recursive_servers()");
277 
278       CONTEXT_DESTROY;
279       LIST_DESTROY(upstream_list);
280       DICT_DESTROY(dict);
281     }
282     END_TEST
283 
START_TEST(getdns_context_set_upstream_recursive_servers_9)284     START_TEST (getdns_context_set_upstream_recursive_servers_9)
285     {
286      /*
287       *  create context
288       *  Call getdns_list_create() to create a list
289       *  Call getdns_dict_create() to create a list
290       *  Create bindata containing “IPv4”
291       */
292 
293       struct getdns_context *context = NULL;
294       struct getdns_list    *upstream_list = NULL;
295       struct getdns_dict *dict = NULL;
296       struct getdns_dict *response = NULL;
297       struct getdns_bindata address_type = { 4, (void *)"IPv4" };
298       struct getdns_bindata address_data = { 4, (void *)"\x08\x08\x08\x08" };
299       size_t index = 0;
300 
301       CONTEXT_CREATE(TRUE);
302       LIST_CREATE(upstream_list);
303       DICT_CREATE(dict);
304 
305       ASSERT_RC(getdns_dict_set_bindata(dict, "address_type", &address_type),
306          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
307 
308       ASSERT_RC(getdns_dict_set_bindata(dict, "address_data", &address_data),
309          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
310 
311       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
312         "Return code from getdns_list_set_dict()");
313 
314 
315       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
316         GETDNS_RETURN_GOOD, "Return code from getdns_context_set_upstream_recursive_servers()");
317 
318       ASSERT_RC(getdns_general_sync(context, "google.com", GETDNS_RRTYPE_A, NULL, &response),
319          GETDNS_RETURN_GOOD, "Return code from getdns_general_sync()");
320 
321 
322       EXTRACT_RESPONSE;
323 
324        assert_noerror(&ex_response);
325        assert_address_in_answer(&ex_response, TRUE, FALSE);
326 
327       CONTEXT_DESTROY;
328       LIST_DESTROY(upstream_list);
329       DICT_DESTROY(dict);
330       DICT_DESTROY(response);
331     }
332     END_TEST
333 
334 /* This test disabled because travis does not support IPv6 in their
335  * container based infrastructure!
336  */
337 #if 0
338     START_TEST (getdns_context_set_upstream_recursive_servers_10)
339     {
340      /*
341       *  create context
342       *  Call getdns_list_create() to create a list
343       * Call getdns_dict_create() to create a list
344       * Create bindata containing “IPv6”
345       */
346 
347       struct getdns_context *context = NULL;
348       struct getdns_list    *upstream_list = NULL;
349       struct getdns_dict *dict = NULL;
350       struct getdns_dict *response = NULL;
351       struct getdns_bindata address_type = { 5, (void *)"IPv6" };
352       struct getdns_bindata address_data = { 16, (void *)"\x26\x20\x00\x74\x00\x1b\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01" };
353       size_t index = 0;
354 
355       CONTEXT_CREATE(TRUE);
356       LIST_CREATE(upstream_list);
357       DICT_CREATE(dict);
358 
359       ASSERT_RC(getdns_dict_set_bindata(dict, "address_type", &address_type),
360          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
361 
362       ASSERT_RC(getdns_dict_set_bindata(dict, "address_data", &address_data),
363          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
364 
365       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
366         "Return code from getdns_list_set_dict()");
367 
368 
369       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
370         GETDNS_RETURN_GOOD, "Return code from getdns_context_set_upstream_recursive_servers()");
371 
372       ASSERT_RC(getdns_general_sync(context, "google.com", GETDNS_RRTYPE_A, NULL, &response),
373          GETDNS_RETURN_GOOD, "Return code from getdns_general_sync()");
374 
375       EXTRACT_RESPONSE;
376 
377        assert_noerror(&ex_response);
378        assert_address_in_answer(&ex_response, TRUE, FALSE);
379 
380       CONTEXT_DESTROY;
381       LIST_DESTROY(upstream_list);
382       DICT_DESTROY(dict);
383       DICT_DESTROY(response);
384     }
385     END_TEST
386 #endif
START_TEST(getdns_context_set_upstream_recursive_servers_11)387     START_TEST (getdns_context_set_upstream_recursive_servers_11)
388     {
389      /*
390       *  create context
391       *  Call getdns_list_create() to create a list
392       *  Call getdns_dict_create() to create a list
393       *  Create bindata containing “IPv4”
394       */
395 
396       struct getdns_context *context = NULL;
397       struct getdns_list    *upstream_list = NULL;
398       struct getdns_dict *dict = NULL;
399       struct getdns_dict *response = NULL;
400       struct getdns_bindata address_type = { 4, (void *)"IPv4" };
401       struct getdns_bindata address_data = { 4, (void *)"\x08\x08\x08\x08" };
402       struct getdns_bindata port = { 3, (void *)"53" };
403       size_t index = 0;
404 
405       CONTEXT_CREATE(TRUE);
406       LIST_CREATE(upstream_list);
407       DICT_CREATE(dict);
408 
409       ASSERT_RC(getdns_dict_set_bindata(dict, "address_type", &address_type),
410          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
411 
412       ASSERT_RC(getdns_dict_set_bindata(dict, "address_data", &address_data),
413          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
414 
415       ASSERT_RC(getdns_dict_set_bindata(dict, "53", &port),
416          GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()");
417 
418       ASSERT_RC(getdns_list_set_dict(upstream_list, index, dict), GETDNS_RETURN_GOOD,
419         "Return code from getdns_list_set_dict()");
420 
421 
422       ASSERT_RC(getdns_context_set_upstream_recursive_servers(context, upstream_list),
423         GETDNS_RETURN_GOOD, "Return code from getdns_context_set_upstream_recursive_servers()");
424 
425       ASSERT_RC(getdns_general_sync(context, "google.com", GETDNS_RRTYPE_A, NULL, &response),
426          GETDNS_RETURN_GOOD, "Return code from getdns_general_sync()");
427 
428 
429       EXTRACT_RESPONSE;
430       printf("the resp is %s\n", getdns_pretty_print_dict(response));
431 
432 
433        assert_noerror(&ex_response);
434        assert_address_in_answer(&ex_response, TRUE, FALSE);
435 
436       CONTEXT_DESTROY;
437       LIST_DESTROY(upstream_list);
438       DICT_DESTROY(dict);
439       DICT_DESTROY(response);
440     }
441     END_TEST
442 
443 
444 
445 
446     Suite *
getdns_context_set_upstream_recursive_servers_suite(void)447     getdns_context_set_upstream_recursive_servers_suite (void)
448     {
449       Suite *s = suite_create ("getdns_context_set_upstream_recursive_servers()");
450 
451       /* Negative test caseis */
452       TCase *tc_neg = tcase_create("Negative");
453       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_1);
454       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_2);
455       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_3);
456       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_4);
457       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_5);
458       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_6);
459       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_7);
460       tcase_add_test(tc_neg, getdns_context_set_upstream_recursive_servers_8);
461 
462       suite_add_tcase(s, tc_neg);
463 
464       /* Positive test cases */
465        TCase *tc_pos = tcase_create("Positive");
466        tcase_add_test(tc_pos, getdns_context_set_upstream_recursive_servers_9);
467 /***** tcase_add_test(tc_pos, getdns_context_set_upstream_recursive_servers_10); *****/
468        tcase_add_test(tc_pos, getdns_context_set_upstream_recursive_servers_11);
469 
470        suite_add_tcase(s, tc_pos);
471 
472        return s;
473 
474     }
475 
476 #endif
477