Debian Codegraph @

deps/curl/tests/unit/unit1607.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "curlcheck.h"
23
24 #include "urldata.h"
25 #include "connect.h"
26 #include "share.h"
27
28 #include "memdebug.h" /* LAST include file */
29
30 static void unit_stop(void)
31 {
32 curl_global_cleanup();
33 }
34
35 static CURLcode unit_setup(void)
36 {
37 int res = CURLE_OK;
38
39 global_init(CURL_GLOBAL_ALL);
40
41 return res;
42 }
43
44 struct testcase {
45 /* host:port:address[,address]... */
46 const char *optval;
47
48 /* lowercase host and port to retrieve the addresses from hostcache */
49 const char *host;
50 int port;
51
52 /* 0 to 9 addresses expected from hostcache */
53 const char *address[10];
54 };
55
56
57 /* In builds without IPv6 support CURLOPT_RESOLVE should skip over those
58 addresses, so we have to do that as well. */
59 static const char skip = 0;
60 #ifdef ENABLE_IPV6
61 #define IPV6ONLY(x) x
62 #else
63 #define IPV6ONLY(x) &skip
64 #endif
65
66 /* CURLOPT_RESOLVE address parsing tests */
67 static const struct testcase tests[] = {
68 /* spaces aren't allowed, for now */
69 { "test.com:80:127.0.0.1, 127.0.0.2",
70 "test.com", 80, { NULL, }
71 },
72 { "TEST.com:80:,,127.0.0.1,,,127.0.0.2,,,,::1,,,",
73 "test.com", 80, { "127.0.0.1", "127.0.0.2", IPV6ONLY("::1"), }
74 },
75 { "test.com:80:::1,127.0.0.1",
76 "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
77 },
78 { "test.com:80:[::1],127.0.0.1",
79 "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
80 },
81 { "test.com:80:::1",
82 "test.com", 80, { IPV6ONLY("::1"), }
83 },
84 { "test.com:80:[::1]",
85 "test.com", 80, { IPV6ONLY("::1"), }
86 },
87 { "test.com:80:127.0.0.1",
88 "test.com", 80, { "127.0.0.1", }
89 },
90 { "test.com:80:,127.0.0.1",
91 "test.com", 80, { "127.0.0.1", }
92 },
93 { "test.com:80:127.0.0.1,",
94 "test.com", 80, { "127.0.0.1", }
95 },
96 { "test.com:0:127.0.0.1",
97 "test.com", 0, { "127.0.0.1", }
98 },
99 };
100
101 UNITTEST_START
102 {
103 int i;
104 int testnum = sizeof(tests) / sizeof(struct testcase);
105 struct Curl_multi *multi = NULL;
106 struct Curl_easy *easy = NULL;
107 struct curl_slist *list = NULL;
108
109 for(i = 0; i < testnum; ++i) {
110 int j;
111 int addressnum = sizeof(tests[i].address) / sizeof(*tests[i].address);
112 struct Curl_addrinfo *addr;
113 struct Curl_dns_entry *dns;
114 void *entry_id;
115 bool problem = false;
116 easy = curl_easy_init();
117 if(!easy)
118 goto error;
119
120 /* create a multi handle and add the easy handle to it so that the
121 hostcache is setup */
122 multi = curl_multi_init();
123 curl_multi_add_handle(multi, easy);
124
125 list = curl_slist_append(NULL, tests[i].optval);
126 if(!list)
127 goto error;
128 curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
129
130 Curl_loadhostpairs(easy);
131
132 entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
133 if(!entry_id)
134 goto error;
135 dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
136 free(entry_id);
137 entry_id = NULL;
138
139 addr = dns ? dns->addr : NULL;
140
141 for(j = 0; j < addressnum; ++j) {
142 long port = 0;
143 char ipaddress[MAX_IPADR_LEN] = {0};
144
145 if(!addr && !tests[i].address[j])
146 break;
147
148 if(tests[i].address[j] == &skip)
149 continue;
150
151 if(addr && !Curl_addr2string(addr->ai_addr, addr->ai_addrlen,
152 ipaddress, &port)) {
153 fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
154 __FILE__, __LINE__, i);
155 problem = true;
156 break;
157 }
158
159 if(addr && !tests[i].address[j]) {
160 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
161 "is %s but tests[%d].address[%d] is NULL.\n",
162 __FILE__, __LINE__, i, ipaddress, i, j);
163 problem = true;
164 break;
165 }
166
167 if(!addr && tests[i].address[j]) {
168 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
169 "is NULL but tests[%d].address[%d] is %s.\n",
170 __FILE__, __LINE__, i, i, j, tests[i].address[j]);
171 problem = true;
172 break;
173 }
174
175 if(!curl_strequal(ipaddress, tests[i].address[j])) {
176 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
177 "%s is not equal to tests[%d].address[%d] %s.\n",
178 __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
179 problem = true;
180 break;
181 }
182
183 if(port != tests[i].port) {
184 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
185 "for tests[%d].address[%d] is %ld but tests[%d].port is %d.\n",
186 __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
187 problem = true;
188 break;
189 }
190
191 if(dns->timestamp != 0) {
192 fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is not zero. "
193 "for tests[%d].address[%d\n",
194 __FILE__, __LINE__, i, i, j);
195 problem = true;
196 break;
197 }
198
199 addr = addr->ai_next;
200 }
201
202 curl_easy_cleanup(easy);
203 easy = NULL;
204 curl_multi_cleanup(multi);
205 multi = NULL;
206 curl_slist_free_all(list);
207 list = NULL;
208
209 if(problem) {
210 unitfail++;
211 continue;
212 }
213 }
214 error:
215 curl_easy_cleanup(easy);
216 curl_multi_cleanup(multi);
217 curl_slist_free_all(list);
218 }
219 UNITTEST_STOP