1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2019, 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.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 /* retrieves ip address and port from a sockaddr structure.
29    note it calls Curl_inet_ntop which sets errno on fail, not SOCKERRNO. */
30 bool getaddressinfo(struct sockaddr *sa, char *addr, long *port);
31 
32 #include "memdebug.h" /* LAST include file */
33 
34 static struct Curl_easy *easy;
35 static struct curl_hash *hostcache;
36 
unit_stop(void)37 static void unit_stop(void)
38 {
39   curl_easy_cleanup(easy);
40   curl_global_cleanup();
41 }
42 
unit_setup(void)43 static CURLcode unit_setup(void)
44 {
45   int res = CURLE_OK;
46 
47   global_init(CURL_GLOBAL_ALL);
48 
49   easy = curl_easy_init();
50   if(!easy) {
51     curl_global_cleanup();
52     return CURLE_OUT_OF_MEMORY;
53   }
54 
55   hostcache = Curl_global_host_cache_init();
56   if(!hostcache) {
57     unit_stop();
58     return CURLE_OUT_OF_MEMORY;
59   }
60 
61   return res;
62 }
63 
64 struct testcase {
65   /* host:port:address[,address]... */
66   const char *optval;
67 
68   /* lowercase host and port to retrieve the addresses from hostcache */
69   const char *host;
70   int port;
71 
72   /* 0 to 9 addresses expected from hostcache */
73   const char *address[10];
74 };
75 
76 
77 /* In builds without IPv6 support CURLOPT_RESOLVE should skip over those
78    addresses, so we have to do that as well. */
79 static const char skip = 0;
80 #ifdef ENABLE_IPV6
81 #define IPV6ONLY(x) x
82 #else
83 #define IPV6ONLY(x) &skip
84 #endif
85 
86 /* CURLOPT_RESOLVE address parsing tests */
87 static const struct testcase tests[] = {
88   /* spaces aren't allowed, for now */
89   { "test.com:80:127.0.0.1, 127.0.0.2",
90     "test.com", 80, { NULL, }
91   },
92   { "TEST.com:80:,,127.0.0.1,,,127.0.0.2,,,,::1,,,",
93     "test.com", 80, { "127.0.0.1", "127.0.0.2", IPV6ONLY("::1"), }
94   },
95   { "test.com:80:::1,127.0.0.1",
96     "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
97   },
98   { "test.com:80:[::1],127.0.0.1",
99     "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
100   },
101   { "test.com:80:::1",
102     "test.com", 80, { IPV6ONLY("::1"), }
103   },
104   { "test.com:80:[::1]",
105     "test.com", 80, { IPV6ONLY("::1"), }
106   },
107   { "test.com:80:127.0.0.1",
108     "test.com", 80, { "127.0.0.1", }
109   },
110   { "test.com:80:,127.0.0.1",
111     "test.com", 80, { "127.0.0.1", }
112   },
113   { "test.com:80:127.0.0.1,",
114     "test.com", 80, { "127.0.0.1", }
115   },
116   { "test.com:0:127.0.0.1",
117     "test.com", 0, { "127.0.0.1", }
118   },
119 };
120 
121 UNITTEST_START
122   int i;
123   int testnum = sizeof(tests) / sizeof(struct testcase);
124 
125   for(i = 0; i < testnum; ++i, curl_easy_reset(easy)) {
126     int j;
127     int addressnum = sizeof(tests[i].address) / sizeof(*tests[i].address);
128     struct Curl_addrinfo *addr;
129     struct Curl_dns_entry *dns;
130     struct curl_slist *list;
131     void *entry_id;
132     bool problem = false;
133 
134     Curl_hostcache_clean(easy, hostcache);
135     easy->dns.hostcache = hostcache;
136     easy->dns.hostcachetype = HCACHE_GLOBAL;
137 
138     list = curl_slist_append(NULL, tests[i].optval);
139     if(!list)
140         goto unit_test_abort;
141     curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
142 
143     Curl_loadhostpairs(easy);
144 
145     entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
146     if(!entry_id) {
147       curl_slist_free_all(list);
148       goto unit_test_abort;
149     }
150     dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
151     free(entry_id);
152     entry_id = NULL;
153 
154     addr = dns ? dns->addr : NULL;
155 
156     for(j = 0; j < addressnum; ++j) {
157       long port = 0;
158       char ipaddress[MAX_IPADR_LEN] = {0};
159 
160       if(!addr && !tests[i].address[j])
161         break;
162 
163       if(tests[i].address[j] == &skip)
164         continue;
165 
166       if(addr && !getaddressinfo(addr->ai_addr,
167                                  ipaddress, &port)) {
168         fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
169                 __FILE__, __LINE__, i);
170         problem = true;
171         break;
172       }
173 
174       if(addr && !tests[i].address[j]) {
175         fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
176                 "is %s but tests[%d].address[%d] is NULL.\n",
177                 __FILE__, __LINE__, i, ipaddress, i, j);
178         problem = true;
179         break;
180       }
181 
182       if(!addr && tests[i].address[j]) {
183         fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
184                 "is NULL but tests[%d].address[%d] is %s.\n",
185                 __FILE__, __LINE__, i, i, j, tests[i].address[j]);
186         problem = true;
187         break;
188       }
189 
190       if(!curl_strequal(ipaddress, tests[i].address[j])) {
191         fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
192                 "%s is not equal to tests[%d].address[%d] %s.\n",
193                 __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
194         problem = true;
195         break;
196       }
197 
198       if(port != tests[i].port) {
199         fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
200                 "for tests[%d].address[%d] is %ld but tests[%d].port is %d.\n",
201                 __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
202         problem = true;
203         break;
204       }
205 
206       if(dns->timestamp != 0) {
207         fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is not zero. "
208                 "for tests[%d].address[%d\n",
209                 __FILE__, __LINE__, i, i, j);
210         problem = true;
211         break;
212       }
213 
214       addr = addr->ai_next;
215     }
216 
217     Curl_hostcache_clean(easy, easy->dns.hostcache);
218     curl_slist_free_all(list);
219 
220     if(problem) {
221       unitfail++;
222       continue;
223     }
224   }
225 UNITTEST_STOP
226