113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "urldata.h"
2713498266Sopenharmony_ci#include "curl/urlapi.h"
2813498266Sopenharmony_ci#include "urlapi-int.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci
3113498266Sopenharmony_cistatic CURLU *u;
3213498266Sopenharmony_ci
3313498266Sopenharmony_cistatic CURLcode
3413498266Sopenharmony_ciunit_setup(void)
3513498266Sopenharmony_ci{
3613498266Sopenharmony_ci  return CURLE_OK;
3713498266Sopenharmony_ci}
3813498266Sopenharmony_ci
3913498266Sopenharmony_cistatic void
4013498266Sopenharmony_ciunit_stop(void)
4113498266Sopenharmony_ci{
4213498266Sopenharmony_ci  curl_global_cleanup();
4313498266Sopenharmony_ci}
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci#define free_and_clear(x) free(x); x = NULL
4613498266Sopenharmony_ci
4713498266Sopenharmony_cistatic CURLUcode parse_port(CURLU *url,
4813498266Sopenharmony_ci                           char *h, bool has_scheme)
4913498266Sopenharmony_ci{
5013498266Sopenharmony_ci  struct dynbuf host;
5113498266Sopenharmony_ci  CURLUcode ret;
5213498266Sopenharmony_ci  Curl_dyn_init(&host, 10000);
5313498266Sopenharmony_ci  if(Curl_dyn_add(&host, h))
5413498266Sopenharmony_ci    return CURLUE_OUT_OF_MEMORY;
5513498266Sopenharmony_ci  ret = Curl_parse_port(url, &host, has_scheme);
5613498266Sopenharmony_ci  Curl_dyn_free(&host);
5713498266Sopenharmony_ci  return ret;
5813498266Sopenharmony_ci}
5913498266Sopenharmony_ci
6013498266Sopenharmony_ciUNITTEST_START
6113498266Sopenharmony_ci{
6213498266Sopenharmony_ci  CURLUcode ret;
6313498266Sopenharmony_ci  char *ipv6port = NULL;
6413498266Sopenharmony_ci  char *portnum;
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  /* Valid IPv6 */
6713498266Sopenharmony_ci  u = curl_url();
6813498266Sopenharmony_ci  if(!u)
6913498266Sopenharmony_ci    goto fail;
7013498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15]");
7113498266Sopenharmony_ci  if(!ipv6port)
7213498266Sopenharmony_ci    goto fail;
7313498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
7413498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
7513498266Sopenharmony_ci  ret = curl_url_get(u, CURLUPART_PORT, &portnum, CURLU_NO_DEFAULT_PORT);
7613498266Sopenharmony_ci  fail_unless(ret != CURLUE_OK, "curl_url_get portnum returned something");
7713498266Sopenharmony_ci  free_and_clear(ipv6port);
7813498266Sopenharmony_ci  curl_url_cleanup(u);
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  /* Invalid IPv6 */
8113498266Sopenharmony_ci  u = curl_url();
8213498266Sopenharmony_ci  if(!u)
8313498266Sopenharmony_ci    goto fail;
8413498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15|");
8513498266Sopenharmony_ci  if(!ipv6port)
8613498266Sopenharmony_ci    goto fail;
8713498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
8813498266Sopenharmony_ci  fail_unless(ret != CURLUE_OK, "parse_port true on error");
8913498266Sopenharmony_ci  free_and_clear(ipv6port);
9013498266Sopenharmony_ci  curl_url_cleanup(u);
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci  u = curl_url();
9313498266Sopenharmony_ci  if(!u)
9413498266Sopenharmony_ci    goto fail;
9513498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff;fea7:da15]:808");
9613498266Sopenharmony_ci  if(!ipv6port)
9713498266Sopenharmony_ci    goto fail;
9813498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
9913498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
10013498266Sopenharmony_ci  ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
10113498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
10213498266Sopenharmony_ci  fail_unless(portnum && !strcmp(portnum, "808"), "Check portnumber");
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci  curl_free(portnum);
10513498266Sopenharmony_ci  free_and_clear(ipv6port);
10613498266Sopenharmony_ci  curl_url_cleanup(u);
10713498266Sopenharmony_ci
10813498266Sopenharmony_ci  /* Valid IPv6 with zone index and port number */
10913498266Sopenharmony_ci  u = curl_url();
11013498266Sopenharmony_ci  if(!u)
11113498266Sopenharmony_ci    goto fail;
11213498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]:80");
11313498266Sopenharmony_ci  if(!ipv6port)
11413498266Sopenharmony_ci    goto fail;
11513498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
11613498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
11713498266Sopenharmony_ci  ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
11813498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
11913498266Sopenharmony_ci  fail_unless(portnum && !strcmp(portnum, "80"), "Check portnumber");
12013498266Sopenharmony_ci  curl_free(portnum);
12113498266Sopenharmony_ci  free_and_clear(ipv6port);
12213498266Sopenharmony_ci  curl_url_cleanup(u);
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci  /* Valid IPv6 with zone index without port number */
12513498266Sopenharmony_ci  u = curl_url();
12613498266Sopenharmony_ci  if(!u)
12713498266Sopenharmony_ci    goto fail;
12813498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]");
12913498266Sopenharmony_ci  if(!ipv6port)
13013498266Sopenharmony_ci    goto fail;
13113498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
13213498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
13313498266Sopenharmony_ci  free_and_clear(ipv6port);
13413498266Sopenharmony_ci  curl_url_cleanup(u);
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  /* Valid IPv6 with port number */
13713498266Sopenharmony_ci  u = curl_url();
13813498266Sopenharmony_ci  if(!u)
13913498266Sopenharmony_ci    goto fail;
14013498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15]:81");
14113498266Sopenharmony_ci  if(!ipv6port)
14213498266Sopenharmony_ci    goto fail;
14313498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
14413498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
14513498266Sopenharmony_ci  ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
14613498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
14713498266Sopenharmony_ci  fail_unless(portnum && !strcmp(portnum, "81"), "Check portnumber");
14813498266Sopenharmony_ci  curl_free(portnum);
14913498266Sopenharmony_ci  free_and_clear(ipv6port);
15013498266Sopenharmony_ci  curl_url_cleanup(u);
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci  /* Valid IPv6 with syntax error in the port number */
15313498266Sopenharmony_ci  u = curl_url();
15413498266Sopenharmony_ci  if(!u)
15513498266Sopenharmony_ci    goto fail;
15613498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15];81");
15713498266Sopenharmony_ci  if(!ipv6port)
15813498266Sopenharmony_ci    goto fail;
15913498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
16013498266Sopenharmony_ci  fail_unless(ret != CURLUE_OK, "parse_port true on error");
16113498266Sopenharmony_ci  free_and_clear(ipv6port);
16213498266Sopenharmony_ci  curl_url_cleanup(u);
16313498266Sopenharmony_ci
16413498266Sopenharmony_ci  u = curl_url();
16513498266Sopenharmony_ci  if(!u)
16613498266Sopenharmony_ci    goto fail;
16713498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15]80");
16813498266Sopenharmony_ci  if(!ipv6port)
16913498266Sopenharmony_ci    goto fail;
17013498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
17113498266Sopenharmony_ci  fail_unless(ret != CURLUE_OK, "parse_port true on error");
17213498266Sopenharmony_ci  free_and_clear(ipv6port);
17313498266Sopenharmony_ci  curl_url_cleanup(u);
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci  /* Valid IPv6 with no port after the colon, should use default if a scheme
17613498266Sopenharmony_ci     was used in the URL */
17713498266Sopenharmony_ci  u = curl_url();
17813498266Sopenharmony_ci  if(!u)
17913498266Sopenharmony_ci    goto fail;
18013498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15]:");
18113498266Sopenharmony_ci  if(!ipv6port)
18213498266Sopenharmony_ci    goto fail;
18313498266Sopenharmony_ci  ret = parse_port(u, ipv6port, TRUE);
18413498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
18513498266Sopenharmony_ci  free_and_clear(ipv6port);
18613498266Sopenharmony_ci  curl_url_cleanup(u);
18713498266Sopenharmony_ci
18813498266Sopenharmony_ci  /* Incorrect zone index syntax, but the port extractor doesn't care */
18913498266Sopenharmony_ci  u = curl_url();
19013498266Sopenharmony_ci  if(!u)
19113498266Sopenharmony_ci    goto fail;
19213498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15!25eth3]:180");
19313498266Sopenharmony_ci  if(!ipv6port)
19413498266Sopenharmony_ci    goto fail;
19513498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
19613498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
19713498266Sopenharmony_ci  ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
19813498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
19913498266Sopenharmony_ci  fail_unless(portnum && !strcmp(portnum, "180"), "Check portnumber");
20013498266Sopenharmony_ci  curl_free(portnum);
20113498266Sopenharmony_ci  free_and_clear(ipv6port);
20213498266Sopenharmony_ci  curl_url_cleanup(u);
20313498266Sopenharmony_ci
20413498266Sopenharmony_ci  /* Non percent-encoded zone index */
20513498266Sopenharmony_ci  u = curl_url();
20613498266Sopenharmony_ci  if(!u)
20713498266Sopenharmony_ci    goto fail;
20813498266Sopenharmony_ci  ipv6port = strdup("[fe80::250:56ff:fea7:da15%eth3]:80");
20913498266Sopenharmony_ci  if(!ipv6port)
21013498266Sopenharmony_ci    goto fail;
21113498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
21213498266Sopenharmony_ci  fail_unless(ret == CURLUE_OK, "parse_port returned error");
21313498266Sopenharmony_ci  free_and_clear(ipv6port);
21413498266Sopenharmony_ci  curl_url_cleanup(u);
21513498266Sopenharmony_ci
21613498266Sopenharmony_ci  /* No scheme and no digits following the colon - not accepted. Because that
21713498266Sopenharmony_ci     makes (a*50):// that looks like a scheme be an acceptable input. */
21813498266Sopenharmony_ci  u = curl_url();
21913498266Sopenharmony_ci  if(!u)
22013498266Sopenharmony_ci    goto fail;
22113498266Sopenharmony_ci  ipv6port = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
22213498266Sopenharmony_ci                    "aaaaaaaaaaaaaaaaaaaaaa:");
22313498266Sopenharmony_ci  if(!ipv6port)
22413498266Sopenharmony_ci    goto fail;
22513498266Sopenharmony_ci  ret = parse_port(u, ipv6port, FALSE);
22613498266Sopenharmony_ci  fail_unless(ret == CURLUE_BAD_PORT_NUMBER, "parse_port did wrong");
22713498266Sopenharmony_cifail:
22813498266Sopenharmony_ci  free(ipv6port);
22913498266Sopenharmony_ci  curl_url_cleanup(u);
23013498266Sopenharmony_ci
23113498266Sopenharmony_ci}
23213498266Sopenharmony_ciUNITTEST_STOP
233