unit1609.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /* CURLOPT_RESOLVE address parsing test - to test the following defect fix:
  60. 1) if there is already existing host:port pair in the DNS cache and
  61. we call CURLOPT_RESOLVE, it should also replace addresses.
  62. for example, if there is "test.com:80" with address "1.1.1.1"
  63. and we called CURLOPT_RESOLVE with address "2.2.2.2", then DNS entry needs to
  64. reflect that.
  65. 2) when cached address is already there and close to expire, then by the
  66. time request is made, it can get expired. This happens because, when
  67. we set address using CURLOPT_RESOLVE,
  68. it usually marks as permanent (by setting timestamp to zero). However,
  69. if address already exists
  70. in the cache, then it does not mark it, but just leaves it as it is.
  71. So we fixing this by timestamp to zero if address already exists too.
  72. Test:
  73. - insert new entry
  74. - verify that timestamp is not zero
  75. - call set options with CURLOPT_RESOLVE
  76. - then, call Curl_loadhostpairs
  77. expected result: cached address has zero timestamp.
  78. - call set options with CURLOPT_RESOLVE with same host:port pair,
  79. different address.
  80. - then, call Curl_loadhostpairs
  81. expected result: cached address has zero timestamp and new address
  82. */
  83. static const struct testcase tests[] = {
  84. /* spaces aren't allowed, for now */
  85. { "test.com:80:127.0.0.1",
  86. "test.com", 80, { "127.0.0.1", }
  87. },
  88. { "test.com:80:127.0.0.2",
  89. "test.com", 80, { "127.0.0.2", }
  90. },
  91. };
  92. UNITTEST_START
  93. int i;
  94. int testnum = sizeof(tests) / sizeof(struct testcase);
  95. /* important: we setup cache outside of the loop
  96. and also clean cache after the loop. In contrast,for example,
  97. test 1607 sets up and cleans cache on each iteration. */
  98. Curl_hostcache_clean(easy, hostcache);
  99. easy->dns.hostcache = hostcache;
  100. easy->dns.hostcachetype = HCACHE_GLOBAL;
  101. for(i = 0; i < testnum; ++i, curl_easy_reset(easy)) {
  102. int j;
  103. int addressnum = sizeof (tests[i].address) / sizeof (*tests[i].address);
  104. struct Curl_addrinfo *addr;
  105. struct Curl_dns_entry *dns;
  106. struct curl_slist *list;
  107. void *entry_id;
  108. bool problem = false;
  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(addr && !Curl_getaddressinfo(addr->ai_addr,
  129. ipaddress, &port)) {
  130. fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
  131. __FILE__, __LINE__, i);
  132. problem = true;
  133. break;
  134. }
  135. if(addr && !tests[i].address[j]) {
  136. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
  137. "is %s but tests[%d].address[%d] is NULL.\n",
  138. __FILE__, __LINE__, i, ipaddress, i, j);
  139. problem = true;
  140. break;
  141. }
  142. if(!addr && tests[i].address[j]) {
  143. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
  144. "is NULL but tests[%d].address[%d] is %s.\n",
  145. __FILE__, __LINE__, i, i, j, tests[i].address[j]);
  146. problem = true;
  147. break;
  148. }
  149. if(!curl_strequal(ipaddress, tests[i].address[j])) {
  150. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
  151. "%s is not equal to tests[%d].address[%d] %s.\n",
  152. __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
  153. problem = true;
  154. break;
  155. }
  156. if(port != tests[i].port) {
  157. fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
  158. "for tests[%d].address[%d] is %ld but tests[%d].port is %d.\n",
  159. __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
  160. problem = true;
  161. break;
  162. }
  163. addr = addr->ai_next;
  164. }
  165. curl_slist_free_all(list);
  166. if(problem) {
  167. unitfail++;
  168. continue;
  169. }
  170. }
  171. Curl_hostcache_clean(easy, easy->dns.hostcache);
  172. UNITTEST_STOP