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
24#ifdef HAVE_NETINET_IN_H
25#  include <netinet/in.h>
26#endif
27#ifdef HAVE_NETDB_H
28#  include <netdb.h>
29#endif
30#ifdef HAVE_ARPA_INET_H
31#  include <arpa/inet.h>
32#endif
33
34#define ENABLE_CURLX_PRINTF
35#include "curlx.h"
36
37#include "hash.h"
38#include "hostip.h"
39
40#include "curl_memory.h"
41#include "memdebug.h" /* LAST include file */
42
43static struct SessionHandle *data;
44static struct curl_hash *hp;
45static char *data_key;
46static struct Curl_dns_entry *data_node;
47
48static CURLcode unit_setup( void )
49{
50  data = curl_easy_init();
51  if (!data)
52    return CURLE_OUT_OF_MEMORY;
53
54  hp = Curl_mk_dnscache();
55  if(!hp) {
56    curl_easy_cleanup(data);
57    curl_global_cleanup();
58    return CURLE_OUT_OF_MEMORY;
59  }
60  return CURLE_OK;
61}
62
63static void unit_stop( void )
64{
65  if (data_node) {
66    Curl_freeaddrinfo(data_node->addr);
67    free(data_node);
68  }
69  if (data_key)
70    free(data_key);
71
72  Curl_hash_destroy(hp);
73
74  curl_easy_cleanup(data);
75  curl_global_cleanup();
76}
77
78static Curl_addrinfo *fake_ai(void)
79{
80  static Curl_addrinfo *ai;
81  int ss_size;
82
83  ss_size = sizeof (struct sockaddr_in);
84
85  if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL)
86    return NULL;
87
88  if((ai->ai_canonname = strdup("dummy")) == NULL) {
89    free(ai);
90    return NULL;
91  }
92
93  if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
94    free(ai->ai_canonname);
95    free(ai);
96    return NULL;
97  }
98
99  ai->ai_family = AF_INET;
100  ai->ai_addrlen = ss_size;
101
102  return ai;
103}
104
105static CURLcode create_node(void)
106{
107  data_key = aprintf("%s:%d", "dummy", 0);
108  if (!data_key)
109    return CURLE_OUT_OF_MEMORY;
110
111  data_node = calloc(1, sizeof(struct Curl_dns_entry));
112  if (!data_node)
113    return CURLE_OUT_OF_MEMORY;
114
115  data_node->addr = fake_ai();
116  if (!data_node->addr)
117    return CURLE_OUT_OF_MEMORY;
118
119  return CURLE_OK;
120}
121
122
123UNITTEST_START
124
125  struct Curl_dns_entry *nodep;
126  size_t key_len;
127
128  /* Test 1305 exits without adding anything to the hash */
129  if (strcmp(arg, "1305") != 0) {
130    CURLcode rc = create_node();
131    abort_unless(rc == CURLE_OK, "data node creation failed");
132    key_len = strlen(data_key);
133
134    nodep = Curl_hash_add(hp, data_key, key_len+1, data_node);
135    abort_unless(nodep, "insertion into hash failed");
136    /* Freeing will now be done by Curl_hash_destroy */
137    data_node = NULL;
138
139    /* To do: test retrieval, deletion, edge conditions */
140  }
141
142UNITTEST_STOP
143