unit1305.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifdef HAVE_NETINET_IN_H
  24. # include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_NETDB_H
  27. # include <netdb.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. # include <arpa/inet.h>
  31. #endif
  32. #define ENABLE_CURLX_PRINTF
  33. #include "curlx.h"
  34. #include "hash.h"
  35. #include "hostip.h"
  36. #include "memdebug.h" /* LAST include file */
  37. static struct Curl_easy *data;
  38. static struct Curl_hash hp;
  39. static char *data_key;
  40. static struct Curl_dns_entry *data_node;
  41. static CURLcode unit_setup(void)
  42. {
  43. int rc;
  44. data = curl_easy_init();
  45. if(!data) {
  46. curl_global_cleanup();
  47. return CURLE_OUT_OF_MEMORY;
  48. }
  49. rc = Curl_mk_dnscache(&hp);
  50. if(rc) {
  51. curl_easy_cleanup(data);
  52. curl_global_cleanup();
  53. return CURLE_OUT_OF_MEMORY;
  54. }
  55. return CURLE_OK;
  56. }
  57. static void unit_stop(void)
  58. {
  59. if(data_node) {
  60. Curl_freeaddrinfo(data_node->addr);
  61. free(data_node);
  62. }
  63. free(data_key);
  64. Curl_hash_destroy(&hp);
  65. curl_easy_cleanup(data);
  66. curl_global_cleanup();
  67. }
  68. static struct Curl_addrinfo *fake_ai(void)
  69. {
  70. static struct Curl_addrinfo *ai;
  71. static const char dummy[]="dummy";
  72. size_t namelen = sizeof(dummy); /* including the zero terminator */
  73. ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_in) +
  74. namelen);
  75. if(!ai)
  76. return NULL;
  77. ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
  78. ai->ai_canonname = (void *)((char *)ai->ai_addr +
  79. sizeof(struct sockaddr_in));
  80. memcpy(ai->ai_canonname, dummy, namelen);
  81. ai->ai_family = AF_INET;
  82. ai->ai_addrlen = sizeof(struct sockaddr_in);
  83. return ai;
  84. }
  85. static CURLcode create_node(void)
  86. {
  87. data_key = aprintf("%s:%d", "dummy", 0);
  88. if(!data_key)
  89. return CURLE_OUT_OF_MEMORY;
  90. data_node = calloc(1, sizeof(struct Curl_dns_entry));
  91. if(!data_node)
  92. return CURLE_OUT_OF_MEMORY;
  93. data_node->addr = fake_ai();
  94. if(!data_node->addr)
  95. return CURLE_OUT_OF_MEMORY;
  96. return CURLE_OK;
  97. }
  98. UNITTEST_START
  99. struct Curl_dns_entry *nodep;
  100. size_t key_len;
  101. /* Test 1305 exits without adding anything to the hash */
  102. if(strcmp(arg, "1305") != 0) {
  103. CURLcode rc = create_node();
  104. abort_unless(rc == CURLE_OK, "data node creation failed");
  105. key_len = strlen(data_key);
  106. data_node->inuse = 1; /* hash will hold the reference */
  107. nodep = Curl_hash_add(&hp, data_key, key_len + 1, data_node);
  108. abort_unless(nodep, "insertion into hash failed");
  109. /* Freeing will now be done by Curl_hash_destroy */
  110. data_node = NULL;
  111. }
  112. UNITTEST_STOP