unit1653.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. UNITTEST_START
  41. {
  42. CURLUcode ret;
  43. char *ipv6port = NULL;
  44. char *portnum;
  45. /* Valid IPv6 */
  46. u = curl_url();
  47. if(!u)
  48. goto fail;
  49. ipv6port = strdup("[fe80::250:56ff:fea7:da15]");
  50. if(!ipv6port)
  51. goto fail;
  52. ret = Curl_parse_port(u, ipv6port, FALSE);
  53. fail_unless(ret == CURLUE_OK, "Curl_parse_port returned error");
  54. ret = curl_url_get(u, CURLUPART_PORT, &portnum, CURLU_NO_DEFAULT_PORT);
  55. fail_unless(ret != CURLUE_OK, "curl_url_get portnum returned something");
  56. free_and_clear(ipv6port);
  57. curl_url_cleanup(u);
  58. /* Invalid IPv6 */
  59. u = curl_url();
  60. if(!u)
  61. goto fail;
  62. ipv6port = strdup("[fe80::250:56ff:fea7:da15|");
  63. if(!ipv6port)
  64. goto fail;
  65. ret = Curl_parse_port(u, ipv6port, FALSE);
  66. fail_unless(ret != CURLUE_OK, "Curl_parse_port true on error");
  67. free_and_clear(ipv6port);
  68. curl_url_cleanup(u);
  69. u = curl_url();
  70. if(!u)
  71. goto fail;
  72. ipv6port = strdup("[fe80::250:56ff;fea7:da15]:80");
  73. if(!ipv6port)
  74. goto fail;
  75. ret = Curl_parse_port(u, ipv6port, FALSE);
  76. fail_unless(ret != CURLUE_OK, "Curl_parse_port true on error");
  77. free_and_clear(ipv6port);
  78. curl_url_cleanup(u);
  79. /* Valid IPv6 with zone index and port number */
  80. u = curl_url();
  81. if(!u)
  82. goto fail;
  83. ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]:80");
  84. if(!ipv6port)
  85. goto fail;
  86. ret = Curl_parse_port(u, ipv6port, FALSE);
  87. fail_unless(ret == CURLUE_OK, "Curl_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, "80"), "Check portnumber");
  91. curl_free(portnum);
  92. free_and_clear(ipv6port);
  93. curl_url_cleanup(u);
  94. /* Valid IPv6 with zone index without port number */
  95. u = curl_url();
  96. if(!u)
  97. goto fail;
  98. ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]");
  99. if(!ipv6port)
  100. goto fail;
  101. ret = Curl_parse_port(u, ipv6port, FALSE);
  102. fail_unless(ret == CURLUE_OK, "Curl_parse_port returned error");
  103. free_and_clear(ipv6port);
  104. curl_url_cleanup(u);
  105. /* Valid IPv6 with port number */
  106. u = curl_url();
  107. if(!u)
  108. goto fail;
  109. ipv6port = strdup("[fe80::250:56ff:fea7:da15]:81");
  110. if(!ipv6port)
  111. goto fail;
  112. ret = Curl_parse_port(u, ipv6port, FALSE);
  113. fail_unless(ret == CURLUE_OK, "Curl_parse_port returned error");
  114. ret = curl_url_get(u, CURLUPART_PORT, &portnum, 0);
  115. fail_unless(ret == CURLUE_OK, "curl_url_get portnum returned error");
  116. fail_unless(portnum && !strcmp(portnum, "81"), "Check portnumber");
  117. curl_free(portnum);
  118. free_and_clear(ipv6port);
  119. curl_url_cleanup(u);
  120. /* Valid IPv6 with syntax error in the port number */
  121. u = curl_url();
  122. if(!u)
  123. goto fail;
  124. ipv6port = strdup("[fe80::250:56ff:fea7:da15];81");
  125. if(!ipv6port)
  126. goto fail;
  127. ret = Curl_parse_port(u, ipv6port, FALSE);
  128. fail_unless(ret != CURLUE_OK, "Curl_parse_port true on error");
  129. free_and_clear(ipv6port);
  130. curl_url_cleanup(u);
  131. u = curl_url();
  132. if(!u)
  133. goto fail;
  134. ipv6port = strdup("[fe80::250:56ff:fea7:da15]80");
  135. if(!ipv6port)
  136. goto fail;
  137. ret = Curl_parse_port(u, ipv6port, FALSE);
  138. fail_unless(ret != CURLUE_OK, "Curl_parse_port true on error");
  139. free_and_clear(ipv6port);
  140. curl_url_cleanup(u);
  141. /* Valid IPv6 with no port after the colon, should use default if a scheme
  142. was used in the URL */
  143. u = curl_url();
  144. if(!u)
  145. goto fail;
  146. ipv6port = strdup("[fe80::250:56ff:fea7:da15]:");
  147. if(!ipv6port)
  148. goto fail;
  149. ret = Curl_parse_port(u, ipv6port, TRUE);
  150. fail_unless(ret == CURLUE_OK, "Curl_parse_port returned error");
  151. free_and_clear(ipv6port);
  152. curl_url_cleanup(u);
  153. /* Incorrect zone index syntax */
  154. u = curl_url();
  155. if(!u)
  156. goto fail;
  157. ipv6port = strdup("[fe80::250:56ff:fea7:da15!25eth3]:80");
  158. if(!ipv6port)
  159. goto fail;
  160. ret = Curl_parse_port(u, ipv6port, FALSE);
  161. fail_unless(ret != CURLUE_OK, "Curl_parse_port returned non-error");
  162. free_and_clear(ipv6port);
  163. curl_url_cleanup(u);
  164. /* Non percent-encoded zone index */
  165. u = curl_url();
  166. if(!u)
  167. goto fail;
  168. ipv6port = strdup("[fe80::250:56ff:fea7:da15%eth3]:80");
  169. if(!ipv6port)
  170. goto fail;
  171. ret = Curl_parse_port(u, ipv6port, FALSE);
  172. fail_unless(ret == CURLUE_OK, "Curl_parse_port returned error");
  173. free_and_clear(ipv6port);
  174. curl_url_cleanup(u);
  175. /* No scheme and no digits following the colon - not accepted. Because that
  176. makes (a*50):// that looks like a scheme be an acceptable input. */
  177. u = curl_url();
  178. if(!u)
  179. goto fail;
  180. ipv6port = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  181. "aaaaaaaaaaaaaaaaaaaaaa:");
  182. if(!ipv6port)
  183. goto fail;
  184. ret = Curl_parse_port(u, ipv6port, FALSE);
  185. fail_unless(ret == CURLUE_BAD_PORT_NUMBER, "Curl_parse_port did wrong");
  186. fail:
  187. free(ipv6port);
  188. curl_url_cleanup(u);
  189. }
  190. UNITTEST_STOP