1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "curlcheck.h"
28 #include "memdebug.h" /* LAST include file */
30 static struct Curl_easy *easy;
31 static struct curl_hash *hostcache;
33 static void unit_stop(void)
35 curl_easy_cleanup(easy);
36 curl_global_cleanup();
39 static CURLcode unit_setup(void)
43 global_init(CURL_GLOBAL_ALL);
45 easy = curl_easy_init();
47 curl_global_cleanup();
48 return CURLE_OUT_OF_MEMORY;
51 hostcache = Curl_global_host_cache_init();
54 return CURLE_OUT_OF_MEMORY;
61 /* host:port:address[,address]... */
64 /* lowercase host and port to retrieve the addresses from hostcache */
68 /* 0 to 9 addresses expected from hostcache */
69 const char *address[10];
73 /* In builds without IPv6 support CURLOPT_RESOLVE should skip over those
74 addresses, so we have to do that as well. */
75 static const char skip = 0;
79 #define IPV6ONLY(x) &skip
82 /* CURLOPT_RESOLVE address parsing tests */
83 static const struct testcase tests[] = {
84 /* spaces aren't allowed, for now */
85 { "test.com:80:127.0.0.1, 127.0.0.2",
86 "test.com", 80, { NULL, }
88 { "TEST.com:80:,,127.0.0.1,,,127.0.0.2,,,,::1,,,",
89 "test.com", 80, { "127.0.0.1", "127.0.0.2", IPV6ONLY("::1"), }
91 { "test.com:80:::1,127.0.0.1",
92 "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
94 { "test.com:80:[::1],127.0.0.1",
95 "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
98 "test.com", 80, { IPV6ONLY("::1"), }
100 { "test.com:80:[::1]",
101 "test.com", 80, { IPV6ONLY("::1"), }
103 { "test.com:80:127.0.0.1",
104 "test.com", 80, { "127.0.0.1", }
106 { "test.com:80:,127.0.0.1",
107 "test.com", 80, { "127.0.0.1", }
109 { "test.com:80:127.0.0.1,",
110 "test.com", 80, { "127.0.0.1", }
112 { "test.com:0:127.0.0.1",
113 "test.com", 0, { "127.0.0.1", }
119 int testnum = sizeof(tests) / sizeof(struct testcase);
121 for(i = 0; i < testnum; ++i, curl_easy_reset(easy)) {
123 int addressnum = sizeof tests[i].address / sizeof *tests[i].address;
124 struct Curl_addrinfo *addr;
125 struct Curl_dns_entry *dns;
126 struct curl_slist *list;
128 bool problem = false;
130 Curl_hostcache_clean(easy, hostcache);
131 easy->dns.hostcache = hostcache;
132 easy->dns.hostcachetype = HCACHE_GLOBAL;
134 list = curl_slist_append(NULL, tests[i].optval);
136 goto unit_test_abort;
137 curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
139 Curl_loadhostpairs(easy);
141 entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
143 curl_slist_free_all(list);
144 goto unit_test_abort;
146 dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
150 addr = dns ? dns->addr : NULL;
152 for(j = 0; j < addressnum; ++j) {
154 char ipaddress[MAX_IPADR_LEN] = {0};
156 if(!addr && !tests[i].address[j])
159 if(tests[i].address[j] == &skip)
162 if(addr && !Curl_getaddressinfo(addr->ai_addr,
164 fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
165 __FILE__, __LINE__, i);
170 if(addr && !tests[i].address[j]) {
171 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
172 "is %s but tests[%d].address[%d] is NULL.\n",
173 __FILE__, __LINE__, i, ipaddress, i, j);
178 if(!addr && tests[i].address[j]) {
179 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
180 "is NULL but tests[%d].address[%d] is %s.\n",
181 __FILE__, __LINE__, i, i, j, tests[i].address[j]);
186 if(!curl_strequal(ipaddress, tests[i].address[j])) {
187 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
188 "%s is not equal to tests[%d].address[%d] %s.\n",
189 __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
194 if(port != tests[i].port) {
195 fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
196 "for tests[%d].address[%d] is %ld but tests[%d].port is %d.\n",
197 __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
202 addr = addr->ai_next;
205 Curl_hostcache_clean(easy, easy->dns.hostcache);
206 curl_slist_free_all(list);