unit1607.c 6.0 KB

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