2
0

unit1653.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "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]:808");
  85. if(!ipv6port)
  86. goto fail;
  87. ret = parse_port(u, ipv6port, FALSE);
  88. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  89. ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
  90. fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
  91. fail_unless(portnum && !strcmp(portnum, "808"), "Check portnumber");
  92. curl_free(portnum);
  93. free_and_clear(ipv6port);
  94. curl_url_cleanup(u);
  95. /* Valid IPv6 with zone index and port number */
  96. u = curl_url();
  97. if(!u)
  98. goto fail;
  99. ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]:80");
  100. if(!ipv6port)
  101. goto fail;
  102. ret = parse_port(u, ipv6port, FALSE);
  103. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  104. ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
  105. fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
  106. fail_unless(portnum && !strcmp(portnum, "80"), "Check portnumber");
  107. curl_free(portnum);
  108. free_and_clear(ipv6port);
  109. curl_url_cleanup(u);
  110. /* Valid IPv6 with zone index without port number */
  111. u = curl_url();
  112. if(!u)
  113. goto fail;
  114. ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]");
  115. if(!ipv6port)
  116. goto fail;
  117. ret = parse_port(u, ipv6port, FALSE);
  118. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  119. free_and_clear(ipv6port);
  120. curl_url_cleanup(u);
  121. /* Valid IPv6 with port number */
  122. u = curl_url();
  123. if(!u)
  124. goto fail;
  125. ipv6port = strdup("[fe80::250:56ff:fea7:da15]:81");
  126. if(!ipv6port)
  127. goto fail;
  128. ret = parse_port(u, ipv6port, FALSE);
  129. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  130. ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
  131. fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
  132. fail_unless(portnum && !strcmp(portnum, "81"), "Check portnumber");
  133. curl_free(portnum);
  134. free_and_clear(ipv6port);
  135. curl_url_cleanup(u);
  136. /* Valid IPv6 with syntax error in the port number */
  137. u = curl_url();
  138. if(!u)
  139. goto fail;
  140. ipv6port = strdup("[fe80::250:56ff:fea7:da15];81");
  141. if(!ipv6port)
  142. goto fail;
  143. ret = parse_port(u, ipv6port, FALSE);
  144. fail_unless(ret != CURLUE_OK, "parse_port true on error");
  145. free_and_clear(ipv6port);
  146. curl_url_cleanup(u);
  147. u = curl_url();
  148. if(!u)
  149. goto fail;
  150. ipv6port = strdup("[fe80::250:56ff:fea7:da15]80");
  151. if(!ipv6port)
  152. goto fail;
  153. ret = parse_port(u, ipv6port, FALSE);
  154. fail_unless(ret != CURLUE_OK, "parse_port true on error");
  155. free_and_clear(ipv6port);
  156. curl_url_cleanup(u);
  157. /* Valid IPv6 with no port after the colon, should use default if a scheme
  158. was used in the URL */
  159. u = curl_url();
  160. if(!u)
  161. goto fail;
  162. ipv6port = strdup("[fe80::250:56ff:fea7:da15]:");
  163. if(!ipv6port)
  164. goto fail;
  165. ret = parse_port(u, ipv6port, TRUE);
  166. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  167. free_and_clear(ipv6port);
  168. curl_url_cleanup(u);
  169. /* Incorrect zone index syntax, but the port extractor doesn't care */
  170. u = curl_url();
  171. if(!u)
  172. goto fail;
  173. ipv6port = strdup("[fe80::250:56ff:fea7:da15!25eth3]:180");
  174. if(!ipv6port)
  175. goto fail;
  176. ret = parse_port(u, ipv6port, FALSE);
  177. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  178. ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
  179. fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
  180. fail_unless(portnum && !strcmp(portnum, "180"), "Check portnumber");
  181. curl_free(portnum);
  182. free_and_clear(ipv6port);
  183. curl_url_cleanup(u);
  184. /* Non percent-encoded zone index */
  185. u = curl_url();
  186. if(!u)
  187. goto fail;
  188. ipv6port = strdup("[fe80::250:56ff:fea7:da15%eth3]:80");
  189. if(!ipv6port)
  190. goto fail;
  191. ret = parse_port(u, ipv6port, FALSE);
  192. fail_unless(ret == CURLUE_OK, "parse_port returned error");
  193. free_and_clear(ipv6port);
  194. curl_url_cleanup(u);
  195. /* No scheme and no digits following the colon - not accepted. Because that
  196. makes (a*50):// that looks like a scheme be an acceptable input. */
  197. u = curl_url();
  198. if(!u)
  199. goto fail;
  200. ipv6port = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  201. "aaaaaaaaaaaaaaaaaaaaaa:");
  202. if(!ipv6port)
  203. goto fail;
  204. ret = parse_port(u, ipv6port, FALSE);
  205. fail_unless(ret == CURLUE_BAD_PORT_NUMBER, "parse_port did wrong");
  206. fail:
  207. free(ipv6port);
  208. curl_url_cleanup(u);
  209. }
  210. UNITTEST_STOP