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