unit1607.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 "connect.h"
  25. #include "share.h"
  26. #include "memdebug.h" /* LAST include file */
  27. static void unit_stop(void)
  28. {
  29. curl_global_cleanup();
  30. }
  31. static CURLcode unit_setup(void)
  32. {
  33. int res = CURLE_OK;
  34. global_init(CURL_GLOBAL_ALL);
  35. return res;
  36. }
  37. struct testcase {
  38. /* host:port:address[,address]... */
  39. const char *optval;
  40. /* lowercase host and port to retrieve the addresses from hostcache */
  41. const char *host;
  42. int port;
  43. /* whether we expect a permanent or non-permanent cache entry */
  44. bool permanent;
  45. /* 0 to 9 addresses expected from hostcache */
  46. const char *address[10];
  47. };
  48. /* In builds without IPv6 support CURLOPT_RESOLVE should skip over those
  49. addresses, so we have to do that as well. */
  50. static const char skip = 0;
  51. #ifdef ENABLE_IPV6
  52. #define IPV6ONLY(x) x
  53. #else
  54. #define IPV6ONLY(x) &skip
  55. #endif
  56. /* CURLOPT_RESOLVE address parsing tests */
  57. static const struct testcase tests[] = {
  58. /* spaces aren't allowed, for now */
  59. { "test.com:80:127.0.0.1, 127.0.0.2",
  60. "test.com", 80, TRUE, { NULL, }
  61. },
  62. { "TEST.com:80:,,127.0.0.1,,,127.0.0.2,,,,::1,,,",
  63. "test.com", 80, TRUE, { "127.0.0.1", "127.0.0.2", IPV6ONLY("::1"), }
  64. },
  65. { "test.com:80:::1,127.0.0.1",
  66. "test.com", 80, TRUE, { IPV6ONLY("::1"), "127.0.0.1", }
  67. },
  68. { "test.com:80:[::1],127.0.0.1",
  69. "test.com", 80, TRUE, { IPV6ONLY("::1"), "127.0.0.1", }
  70. },
  71. { "test.com:80:::1",
  72. "test.com", 80, TRUE, { IPV6ONLY("::1"), }
  73. },
  74. { "test.com:80:[::1]",
  75. "test.com", 80, TRUE, { IPV6ONLY("::1"), }
  76. },
  77. { "test.com:80:127.0.0.1",
  78. "test.com", 80, TRUE, { "127.0.0.1", }
  79. },
  80. { "test.com:80:,127.0.0.1",
  81. "test.com", 80, TRUE, { "127.0.0.1", }
  82. },
  83. { "test.com:80:127.0.0.1,",
  84. "test.com", 80, TRUE, { "127.0.0.1", }
  85. },
  86. { "test.com:0:127.0.0.1",
  87. "test.com", 0, TRUE, { "127.0.0.1", }
  88. },
  89. { "+test.com:80:127.0.0.1,",
  90. "test.com", 80, FALSE, { "127.0.0.1", }
  91. },
  92. };
  93. UNITTEST_START
  94. {
  95. int i;
  96. int testnum = sizeof(tests) / sizeof(struct testcase);
  97. struct Curl_multi *multi = NULL;
  98. struct Curl_easy *easy = NULL;
  99. struct curl_slist *list = NULL;
  100. for(i = 0; i < testnum; ++i) {
  101. int j;
  102. int addressnum = sizeof(tests[i].address) / sizeof(*tests[i].address);
  103. struct Curl_addrinfo *addr;
  104. struct Curl_dns_entry *dns;
  105. void *entry_id;
  106. bool problem = false;
  107. easy = curl_easy_init();
  108. if(!easy)
  109. goto error;
  110. /* create a multi handle and add the easy handle to it so that the
  111. hostcache is setup */
  112. multi = curl_multi_init();
  113. curl_multi_add_handle(multi, easy);
  114. list = curl_slist_append(NULL, tests[i].optval);
  115. if(!list)
  116. goto error;
  117. curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
  118. Curl_loadhostpairs(easy);
  119. entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
  120. if(!entry_id)
  121. goto error;
  122. dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
  123. free(entry_id);
  124. entry_id = NULL;
  125. addr = dns ? dns->addr : NULL;
  126. for(j = 0; j < addressnum; ++j) {
  127. int port = 0;
  128. char ipaddress[MAX_IPADR_LEN] = {0};
  129. if(!addr && !tests[i].address[j])
  130. break;
  131. if(tests[i].address[j] == &skip)
  132. continue;
  133. if(addr && !Curl_addr2string(addr->ai_addr, addr->ai_addrlen,
  134. ipaddress, &port)) {
  135. fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
  136. __FILE__, __LINE__, i);
  137. problem = true;
  138. break;
  139. }
  140. if(addr && !tests[i].address[j]) {
  141. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
  142. "is %s but tests[%d].address[%d] is NULL.\n",
  143. __FILE__, __LINE__, i, ipaddress, i, j);
  144. problem = true;
  145. break;
  146. }
  147. if(!addr && tests[i].address[j]) {
  148. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
  149. "is NULL but tests[%d].address[%d] is %s.\n",
  150. __FILE__, __LINE__, i, i, j, tests[i].address[j]);
  151. problem = true;
  152. break;
  153. }
  154. if(!curl_strequal(ipaddress, tests[i].address[j])) {
  155. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
  156. "%s is not equal to tests[%d].address[%d] %s.\n",
  157. __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
  158. problem = true;
  159. break;
  160. }
  161. if(port != tests[i].port) {
  162. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
  163. "for tests[%d].address[%d] is %ld but tests[%d].port is %d.\n",
  164. __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
  165. problem = true;
  166. break;
  167. }
  168. if(dns->timestamp && tests[i].permanent) {
  169. fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is not zero "
  170. "but tests[%d].permanent is TRUE\n",
  171. __FILE__, __LINE__, i, i);
  172. problem = true;
  173. break;
  174. }
  175. if(dns->timestamp == 0 && !tests[i].permanent) {
  176. fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is zero "
  177. "but tests[%d].permanent is FALSE\n",
  178. __FILE__, __LINE__, i, i);
  179. problem = true;
  180. break;
  181. }
  182. addr = addr->ai_next;
  183. }
  184. curl_easy_cleanup(easy);
  185. easy = NULL;
  186. curl_multi_cleanup(multi);
  187. multi = NULL;
  188. curl_slist_free_all(list);
  189. list = NULL;
  190. if(problem) {
  191. unitfail++;
  192. continue;
  193. }
  194. }
  195. error:
  196. curl_easy_cleanup(easy);
  197. curl_multi_cleanup(multi);
  198. curl_slist_free_all(list);
  199. }
  200. UNITTEST_STOP