unit1653.c 6.0 KB

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