unit1305.c 3.3 KB

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