1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, 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.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
unit_stop(void)30 static void unit_stop(void)
31 {
32 curl_global_cleanup();
33 }
34
unit_setup(void)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 /* whether we expect a permanent or non-permanent cache entry */
53 bool permanent;
54
55 /* 0 to 9 addresses expected from hostcache */
56 const char *address[10];
57 };
58
59
60 /* In builds without IPv6 support CURLOPT_RESOLVE should skip over those
61 addresses, so we have to do that as well. */
62 static const char skip = 0;
63 #ifdef ENABLE_IPV6
64 #define IPV6ONLY(x) x
65 #else
66 #define IPV6ONLY(x) &skip
67 #endif
68
69 /* CURLOPT_RESOLVE address parsing tests */
70 static const struct testcase tests[] = {
71 /* spaces aren't allowed, for now */
72 { "test.com:80:127.0.0.1, 127.0.0.2",
73 "test.com", 80, TRUE, { NULL, }
74 },
75 { "TEST.com:80:,,127.0.0.1,,,127.0.0.2,,,,::1,,,",
76 "test.com", 80, TRUE, { "127.0.0.1", "127.0.0.2", IPV6ONLY("::1"), }
77 },
78 { "test.com:80:::1,127.0.0.1",
79 "test.com", 80, TRUE, { IPV6ONLY("::1"), "127.0.0.1", }
80 },
81 { "test.com:80:[::1],127.0.0.1",
82 "test.com", 80, TRUE, { IPV6ONLY("::1"), "127.0.0.1", }
83 },
84 { "test.com:80:::1",
85 "test.com", 80, TRUE, { IPV6ONLY("::1"), }
86 },
87 { "test.com:80:[::1]",
88 "test.com", 80, TRUE, { IPV6ONLY("::1"), }
89 },
90 { "test.com:80:127.0.0.1",
91 "test.com", 80, TRUE, { "127.0.0.1", }
92 },
93 { "test.com:80:,127.0.0.1",
94 "test.com", 80, TRUE, { "127.0.0.1", }
95 },
96 { "test.com:80:127.0.0.1,",
97 "test.com", 80, TRUE, { "127.0.0.1", }
98 },
99 { "test.com:0:127.0.0.1",
100 "test.com", 0, TRUE, { "127.0.0.1", }
101 },
102 { "+test.com:80:127.0.0.1,",
103 "test.com", 80, FALSE, { "127.0.0.1", }
104 },
105 };
106
107 UNITTEST_START
108 {
109 int i;
110 int testnum = sizeof(tests) / sizeof(struct testcase);
111 struct Curl_multi *multi = NULL;
112 struct Curl_easy *easy = NULL;
113 struct curl_slist *list = NULL;
114
115 for(i = 0; i < testnum; ++i) {
116 int j;
117 int addressnum = sizeof(tests[i].address) / sizeof(*tests[i].address);
118 struct Curl_addrinfo *addr;
119 struct Curl_dns_entry *dns;
120 void *entry_id;
121 bool problem = false;
122 easy = curl_easy_init();
123 if(!easy)
124 goto error;
125
126 /* create a multi handle and add the easy handle to it so that the
127 hostcache is setup */
128 multi = curl_multi_init();
129 curl_multi_add_handle(multi, easy);
130
131 list = curl_slist_append(NULL, tests[i].optval);
132 if(!list)
133 goto error;
134 curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
135
136 Curl_loadhostpairs(easy);
137
138 entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
139 if(!entry_id)
140 goto error;
141 dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
142 free(entry_id);
143 entry_id = NULL;
144
145 addr = dns ? dns->addr : NULL;
146
147 for(j = 0; j < addressnum; ++j) {
148 int port = 0;
149 char ipaddress[MAX_IPADR_LEN] = {0};
150
151 if(!addr && !tests[i].address[j])
152 break;
153
154 if(tests[i].address[j] == &skip)
155 continue;
156
157 if(addr && !Curl_addr2string(addr->ai_addr, addr->ai_addrlen,
158 ipaddress, &port)) {
159 fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
160 __FILE__, __LINE__, i);
161 problem = true;
162 break;
163 }
164
165 if(addr && !tests[i].address[j]) {
166 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
167 "is %s but tests[%d].address[%d] is NULL.\n",
168 __FILE__, __LINE__, i, ipaddress, i, j);
169 problem = true;
170 break;
171 }
172
173 if(!addr && tests[i].address[j]) {
174 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
175 "is NULL but tests[%d].address[%d] is %s.\n",
176 __FILE__, __LINE__, i, i, j, tests[i].address[j]);
177 problem = true;
178 break;
179 }
180
181 if(!curl_strequal(ipaddress, tests[i].address[j])) {
182 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
183 "%s is not equal to tests[%d].address[%d] %s.\n",
184 __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
185 problem = true;
186 break;
187 }
188
189 if(port != tests[i].port) {
190 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
191 "for tests[%d].address[%d] is %ld but tests[%d].port is %d.\n",
192 __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
193 problem = true;
194 break;
195 }
196
197 if(dns->timestamp && tests[i].permanent) {
198 fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is not zero "
199 "but tests[%d].permanent is TRUE\n",
200 __FILE__, __LINE__, i, i);
201 problem = true;
202 break;
203 }
204
205 if(dns->timestamp == 0 && !tests[i].permanent) {
206 fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is zero "
207 "but tests[%d].permanent is FALSE\n",
208 __FILE__, __LINE__, i, i);
209 problem = true;
210 break;
211 }
212
213 addr = addr->ai_next;
214 }
215
216 curl_easy_cleanup(easy);
217 easy = NULL;
218 curl_multi_cleanup(multi);
219 multi = NULL;
220 curl_slist_free_all(list);
221 list = NULL;
222
223 if(problem) {
224 unitfail++;
225 continue;
226 }
227 }
228 error:
229 curl_easy_cleanup(easy);
230 curl_multi_cleanup(multi);
231 curl_slist_free_all(list);
232 }
233 UNITTEST_STOP
234