unit1607.c 5.9 KB

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