blob: 40a21dd066baaab648a552acb670029064353bff [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curlcheck.h"
25
26#include "urldata.h"
27#include "curl/urlapi.h"
28#include "urlapi-int.h"
29
30
31static CURLU *u;
32
33static CURLcode
34unit_setup(void)
35{
36 return CURLE_OK;
37}
38
39static void
40unit_stop(void)
41{
42 curl_global_cleanup();
43}
44
45#define free_and_clear(x) free(x); x = NULL
46
47static CURLUcode parse_port(CURLU *url,
48 char *h, bool has_scheme)
49{
50 struct dynbuf host;
51 CURLUcode ret;
52 Curl_dyn_init(&host, 10000);
53 if(Curl_dyn_add(&host, h))
54 return CURLUE_OUT_OF_MEMORY;
55 ret = Curl_parse_port(url, &host, has_scheme);
56 Curl_dyn_free(&host);
57 return ret;
58}
59
60UNITTEST_START
61{
62 CURLUcode ret;
63 char *ipv6port = NULL;
64 char *portnum;
65
66 /* Valid IPv6 */
67 u = curl_url();
68 if(!u)
69 goto fail;
70 ipv6port = strdup("[fe80::250:56ff:fea7:da15]");
71 if(!ipv6port)
72 goto fail;
73 ret = parse_port(u, ipv6port, FALSE);
74 fail_unless(ret == CURLUE_OK, "parse_port returned error");
75 ret = curl_url_get(u, CURLUPART_PORT, &portnum, CURLU_NO_DEFAULT_PORT);
76 fail_unless(ret != CURLUE_OK, "curl_url_get portnum returned something");
77 free_and_clear(ipv6port);
78 curl_url_cleanup(u);
79
80 /* Invalid IPv6 */
81 u = curl_url();
82 if(!u)
83 goto fail;
84 ipv6port = strdup("[fe80::250:56ff:fea7:da15|");
85 if(!ipv6port)
86 goto fail;
87 ret = parse_port(u, ipv6port, FALSE);
88 fail_unless(ret != CURLUE_OK, "parse_port true on error");
89 free_and_clear(ipv6port);
90 curl_url_cleanup(u);
91
92 u = curl_url();
93 if(!u)
94 goto fail;
95 ipv6port = strdup("[fe80::250:56ff;fea7:da15]:80");
96 if(!ipv6port)
97 goto fail;
98 ret = parse_port(u, ipv6port, FALSE);
99 fail_unless(ret != CURLUE_OK, "parse_port true on error");
100 free_and_clear(ipv6port);
101 curl_url_cleanup(u);
102
103 /* Valid IPv6 with zone index and port number */
104 u = curl_url();
105 if(!u)
106 goto fail;
107 ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]:80");
108 if(!ipv6port)
109 goto fail;
110 ret = parse_port(u, ipv6port, FALSE);
111 fail_unless(ret == CURLUE_OK, "parse_port returned error");
112 ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
113 fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
114 fail_unless(portnum && !strcmp(portnum, "80"), "Check portnumber");
115 curl_free(portnum);
116 free_and_clear(ipv6port);
117 curl_url_cleanup(u);
118
119 /* Valid IPv6 with zone index without port number */
120 u = curl_url();
121 if(!u)
122 goto fail;
123 ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]");
124 if(!ipv6port)
125 goto fail;
126 ret = parse_port(u, ipv6port, FALSE);
127 fail_unless(ret == CURLUE_OK, "parse_port returned error");
128 free_and_clear(ipv6port);
129 curl_url_cleanup(u);
130
131 /* Valid IPv6 with port number */
132 u = curl_url();
133 if(!u)
134 goto fail;
135 ipv6port = strdup("[fe80::250:56ff:fea7:da15]:81");
136 if(!ipv6port)
137 goto fail;
138 ret = parse_port(u, ipv6port, FALSE);
139 fail_unless(ret == CURLUE_OK, "parse_port returned error");
140 ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
141 fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
142 fail_unless(portnum && !strcmp(portnum, "81"), "Check portnumber");
143 curl_free(portnum);
144 free_and_clear(ipv6port);
145 curl_url_cleanup(u);
146
147 /* Valid IPv6 with syntax error in the port number */
148 u = curl_url();
149 if(!u)
150 goto fail;
151 ipv6port = strdup("[fe80::250:56ff:fea7:da15];81");
152 if(!ipv6port)
153 goto fail;
154 ret = parse_port(u, ipv6port, FALSE);
155 fail_unless(ret != CURLUE_OK, "parse_port true on error");
156 free_and_clear(ipv6port);
157 curl_url_cleanup(u);
158
159 u = curl_url();
160 if(!u)
161 goto fail;
162 ipv6port = strdup("[fe80::250:56ff:fea7:da15]80");
163 if(!ipv6port)
164 goto fail;
165 ret = parse_port(u, ipv6port, FALSE);
166 fail_unless(ret != CURLUE_OK, "parse_port true on error");
167 free_and_clear(ipv6port);
168 curl_url_cleanup(u);
169
170 /* Valid IPv6 with no port after the colon, should use default if a scheme
171 was used in the URL */
172 u = curl_url();
173 if(!u)
174 goto fail;
175 ipv6port = strdup("[fe80::250:56ff:fea7:da15]:");
176 if(!ipv6port)
177 goto fail;
178 ret = parse_port(u, ipv6port, TRUE);
179 fail_unless(ret == CURLUE_OK, "parse_port returned error");
180 free_and_clear(ipv6port);
181 curl_url_cleanup(u);
182
183 /* Incorrect zone index syntax */
184 u = curl_url();
185 if(!u)
186 goto fail;
187 ipv6port = strdup("[fe80::250:56ff:fea7:da15!25eth3]:80");
188 if(!ipv6port)
189 goto fail;
190 ret = parse_port(u, ipv6port, FALSE);
191 fail_unless(ret != CURLUE_OK, "parse_port returned non-error");
192 free_and_clear(ipv6port);
193 curl_url_cleanup(u);
194
195 /* Non percent-encoded zone index */
196 u = curl_url();
197 if(!u)
198 goto fail;
199 ipv6port = strdup("[fe80::250:56ff:fea7:da15%eth3]:80");
200 if(!ipv6port)
201 goto fail;
202 ret = parse_port(u, ipv6port, FALSE);
203 fail_unless(ret == CURLUE_OK, "parse_port returned error");
204 free_and_clear(ipv6port);
205 curl_url_cleanup(u);
206
207 /* No scheme and no digits following the colon - not accepted. Because that
208 makes (a*50):// that looks like a scheme be an acceptable input. */
209 u = curl_url();
210 if(!u)
211 goto fail;
212 ipv6port = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
213 "aaaaaaaaaaaaaaaaaaaaaa:");
214 if(!ipv6port)
215 goto fail;
216 ret = parse_port(u, ipv6port, FALSE);
217 fail_unless(ret == CURLUE_BAD_PORT_NUMBER, "parse_port did wrong");
218 fail:
219 free(ipv6port);
220 curl_url_cleanup(u);
221
222}
223UNITTEST_STOP