Imported Upstream version 7.59.0
[platform/upstream/curl.git] / tests / unit / unit1607.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2018, 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 #include "memdebug.h" /* LAST include file */
29
30 static struct Curl_easy *easy;
31 static struct curl_hash *hostcache;
32
33 static void unit_stop(void)
34 {
35   curl_easy_cleanup(easy);
36   curl_global_cleanup();
37 }
38
39 static CURLcode unit_setup(void)
40 {
41   int res = CURLE_OK;
42
43   global_init(CURL_GLOBAL_ALL);
44
45   easy = curl_easy_init();
46   if(!easy) {
47     curl_global_cleanup();
48     return CURLE_OUT_OF_MEMORY;
49   }
50
51   hostcache = Curl_global_host_cache_init();
52   if(!hostcache) {
53     unit_stop();
54     return CURLE_OUT_OF_MEMORY;
55   }
56
57   return res;
58 }
59
60 struct testcase {
61   /* host:port:address[,address]... */
62   const char *optval;
63
64   /* lowercase host and port to retrieve the addresses from hostcache */
65   const char *host;
66   int port;
67
68   /* 0 to 9 addresses expected from hostcache */
69   const char *address[10];
70 };
71
72
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;
76 #ifdef ENABLE_IPV6
77 #define IPV6ONLY(x) x
78 #else
79 #define IPV6ONLY(x) &skip
80 #endif
81
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, }
87   },
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"), }
90   },
91   { "test.com:80:::1,127.0.0.1",
92     "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
93   },
94   { "test.com:80:[::1],127.0.0.1",
95     "test.com", 80, { IPV6ONLY("::1"), "127.0.0.1", }
96   },
97   { "test.com:80:::1",
98     "test.com", 80, { IPV6ONLY("::1"), }
99   },
100   { "test.com:80:[::1]",
101     "test.com", 80, { IPV6ONLY("::1"), }
102   },
103   { "test.com:80:127.0.0.1",
104     "test.com", 80, { "127.0.0.1", }
105   },
106   { "test.com:80:,127.0.0.1",
107     "test.com", 80, { "127.0.0.1", }
108   },
109   { "test.com:80:127.0.0.1,",
110     "test.com", 80, { "127.0.0.1", }
111   },
112   { "test.com:0:127.0.0.1",
113     "test.com", 0, { "127.0.0.1", }
114   },
115 };
116
117 UNITTEST_START
118   int i;
119   int testnum = sizeof(tests) / sizeof(struct testcase);
120
121   for(i = 0; i < testnum; ++i, curl_easy_reset(easy)) {
122     int j;
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;
127     void *entry_id;
128     bool problem = false;
129
130     Curl_hostcache_clean(easy, hostcache);
131     easy->dns.hostcache = hostcache;
132     easy->dns.hostcachetype = HCACHE_GLOBAL;
133
134     list = curl_slist_append(NULL, tests[i].optval);
135     if(!list)
136         goto unit_test_abort;
137     curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
138
139     Curl_loadhostpairs(easy);
140
141     entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
142     if(!entry_id) {
143       curl_slist_free_all(list);
144       goto unit_test_abort;
145     }
146     dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
147     free(entry_id);
148     entry_id = NULL;
149
150     addr = dns ? dns->addr : NULL;
151
152     for(j = 0; j < addressnum; ++j) {
153       long port = 0;
154       char ipaddress[MAX_IPADR_LEN] = {0};
155
156       if(!addr && !tests[i].address[j])
157         break;
158
159       if(tests[i].address[j] == &skip)
160         continue;
161
162       if(addr && !Curl_getaddressinfo(addr->ai_addr,
163                                       ipaddress, &port)) {
164         fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
165                 __FILE__, __LINE__, i);
166         problem = true;
167         break;
168       }
169
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);
174         problem = true;
175         break;
176       }
177
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]);
182         problem = true;
183         break;
184       }
185
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]);
190         problem = true;
191         break;
192       }
193
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);
198         problem = true;
199         break;
200       }
201
202       addr = addr->ai_next;
203     }
204
205     Curl_hostcache_clean(easy, easy->dns.hostcache);
206     curl_slist_free_all(list);
207
208     if(problem) {
209       unitfail++;
210       continue;
211     }
212   }
213 UNITTEST_STOP