unit1305.c 3.4 KB

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