unit1305.c 3.3 KB

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