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