unit1305.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. #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 "curl_memory.h"
  37. #include "memdebug.h" /* LAST include file */
  38. static struct SessionHandle *data;
  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. data = curl_easy_init();
  45. if (!data)
  46. return CURLE_OUT_OF_MEMORY;
  47. hp = Curl_mk_dnscache();
  48. if(!hp) {
  49. curl_easy_cleanup(data);
  50. curl_global_cleanup();
  51. return CURLE_OUT_OF_MEMORY;
  52. }
  53. return CURLE_OK;
  54. }
  55. static void unit_stop( void )
  56. {
  57. if (data_node) {
  58. Curl_freeaddrinfo(data_node->addr);
  59. free(data_node);
  60. }
  61. if (data_key)
  62. free(data_key);
  63. Curl_hash_destroy(hp);
  64. curl_easy_cleanup(data);
  65. curl_global_cleanup();
  66. }
  67. static Curl_addrinfo *fake_ai(void)
  68. {
  69. static Curl_addrinfo *ai;
  70. int ss_size;
  71. ss_size = sizeof (struct sockaddr_in);
  72. if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL)
  73. return NULL;
  74. if((ai->ai_canonname = strdup("dummy")) == NULL) {
  75. free(ai);
  76. return NULL;
  77. }
  78. if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
  79. free(ai->ai_canonname);
  80. free(ai);
  81. return NULL;
  82. }
  83. ai->ai_family = AF_INET;
  84. ai->ai_addrlen = ss_size;
  85. return ai;
  86. }
  87. static CURLcode create_node(void)
  88. {
  89. data_key = aprintf("%s:%d", "dummy", 0);
  90. if (!data_key)
  91. return CURLE_OUT_OF_MEMORY;
  92. data_node = calloc(1, sizeof(struct Curl_dns_entry));
  93. if (!data_node)
  94. return CURLE_OUT_OF_MEMORY;
  95. data_node->addr = fake_ai();
  96. if (!data_node->addr)
  97. return CURLE_OUT_OF_MEMORY;
  98. return CURLE_OK;
  99. }
  100. UNITTEST_START
  101. struct Curl_dns_entry *nodep;
  102. size_t key_len;
  103. /* Test 1305 exits without adding anything to the hash */
  104. if (strcmp(arg, "1305") != 0) {
  105. CURLcode rc = create_node();
  106. abort_unless(rc == CURLE_OK, "data node creation failed");
  107. key_len = strlen(data_key);
  108. nodep = Curl_hash_add(hp, data_key, key_len+1, data_node);
  109. abort_unless(nodep, "insertion into hash failed");
  110. /* Freeing will now be done by Curl_hash_destroy */
  111. data_node = NULL;
  112. /* To do: test retrieval, deletion, edge conditions */
  113. }
  114. UNITTEST_STOP