2
0

unit1653.c 6.2 KB

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