1 /**
2  * \file
3  * unit tests for getdns_dict helper routines, these should be used to
4  * perform regression tests, output must be unchanged from canonical output
5  * stored with the sources
6  */
7 
8 /*
9  * Copyright (c) 2013, NLNet Labs, Verisign, Inc.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * * Redistributions of source code must retain the above copyright
15  *   notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above copyright
17  *   notice, this list of conditions and the following disclaimer in the
18  *   documentation and/or other materials provided with the distribution.
19  * * Neither the names of the copyright holders nor the
20  *   names of its contributors may be used to endorse or promote products
21  *   derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "testmessages.h"
39 #include "getdns/getdns.h"
40 
41 static void
print_response(struct getdns_dict * response)42 print_response(struct getdns_dict * response)
43 {
44     char *dict_str = getdns_pretty_print_dict(response);
45     if (dict_str) {
46         fprintf(stdout, "The packet %s\n", dict_str);
47         free(dict_str);
48     }
49 }
50 
51 int
main()52 main()
53 {
54 
55     /* First up, use the default settings on a general and non-general call */
56 
57     /* The namespaces used are per query depending on the type of method called.
58        But note that namespaces can only be changed before the first query.*/
59 
60     /* Create the DNS context for this call */
61     struct getdns_context *this_context = NULL;
62     getdns_return_t context_create_return =
63         getdns_context_create(&this_context, 1);
64     if (context_create_return != GETDNS_RETURN_GOOD) {
65         fprintf(stderr, "Trying to create the context failed: %d",
66             context_create_return);
67         return (GETDNS_RETURN_GENERIC_ERROR);
68     }
69     getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB);
70 
71     /* This will return a response with only the just_address_answers part
72        as the current implementaiton uses [LOCALNAMES, DNS]*/
73     struct getdns_dict *response = NULL;
74     getdns_return_t ret =
75         getdns_address_sync(this_context, "localhost", NULL, &response);
76 
77     if (ret != GETDNS_RETURN_GOOD || response == NULL) {
78         fprintf(stderr, "Address sync returned error.\n");
79         exit(EXIT_FAILURE);
80     }
81     print_response(response);
82     getdns_dict_destroy(response);
83 
84     /* This should fall back to a full DNS lookup*/
85     ret = getdns_address_sync(this_context, "www.google.com", NULL, &response);
86 
87     if (ret != GETDNS_RETURN_GOOD || response == NULL) {
88         fprintf(stderr, "Address sync returned error.\n");
89         exit(EXIT_FAILURE);
90     }
91     print_response(response);
92     getdns_dict_destroy(response);
93 
94     /* This should return a full DNS reply as the general lookups don't use
95        the namespaces, they just do pure DNS*/
96     ret = getdns_general_sync(this_context, "localhost", GETDNS_RRTYPE_A,
97                               NULL, &response);
98     if (ret != GETDNS_RETURN_GOOD || response == NULL) {
99         fprintf(stderr, "General sync over TCP returned error.\n");
100         exit(EXIT_FAILURE);
101     }
102     print_response(response);
103     getdns_dict_destroy(response);
104 
105     /* Clean up */
106     getdns_context_destroy(this_context);
107 
108 
109     /* Secondly, specify the namespace and see what happens*/
110 
111     /* Create the DNS context for this call */
112     struct getdns_context *next_context = NULL;
113     context_create_return = getdns_context_create(&next_context, 1);
114     if (context_create_return != GETDNS_RETURN_GOOD) {
115         fprintf(stderr, "Trying to create the context failed: %d",
116             context_create_return);
117         return (GETDNS_RETURN_GENERIC_ERROR);
118     }
119     getdns_context_set_resolution_type(next_context, GETDNS_RESOLUTION_STUB);
120 
121     getdns_namespace_t namespace_arr[2] = {GETDNS_NAMESPACE_DNS, GETDNS_NAMESPACE_LOCALNAMES};
122     getdns_context_set_namespaces(next_context, 2,namespace_arr);
123 
124     /* This will return a full DNS reply*/
125     ret = getdns_address_sync(next_context, "localhost", NULL, &response);
126 
127     if (ret != GETDNS_RETURN_GOOD || response == NULL) {
128         fprintf(stderr, "Address sync returned error.\n");
129         exit(EXIT_FAILURE);
130     }
131     print_response(response);
132     getdns_dict_destroy(response);
133 
134     /* Clean up */
135     getdns_context_destroy(next_context);
136 
137     /* Assuming we get here, leave gracefully */
138     exit(EXIT_SUCCESS);
139 }                /* main */
140 
141 /* tests_stub_sync.c */
142 
143