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