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