1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
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
42 static struct Curl_easy *data;
43 static struct Curl_hash hp;
44 static char *data_key;
45 static struct Curl_dns_entry *data_node;
46
unit_setup(void)47 static CURLcode unit_setup(void)
48 {
49 int rc;
50 data = curl_easy_init();
51 if(!data) {
52 curl_global_cleanup();
53 return CURLE_OUT_OF_MEMORY;
54 }
55
56 rc = Curl_mk_dnscache(&hp);
57 if(rc) {
58 curl_easy_cleanup(data);
59 curl_global_cleanup();
60 return CURLE_OUT_OF_MEMORY;
61 }
62 return CURLE_OK;
63 }
64
unit_stop(void)65 static void unit_stop(void)
66 {
67 if(data_node) {
68 Curl_freeaddrinfo(data_node->addr);
69 free(data_node);
70 }
71 free(data_key);
72 Curl_hash_destroy(&hp);
73
74 curl_easy_cleanup(data);
75 curl_global_cleanup();
76 }
77
fake_ai(void)78 static struct Curl_addrinfo *fake_ai(void)
79 {
80 static struct Curl_addrinfo *ai;
81 static const char dummy[]="dummy";
82 size_t namelen = sizeof(dummy); /* including the zero terminator */
83
84 ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_in) +
85 namelen);
86 if(!ai)
87 return NULL;
88
89 ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
90 ai->ai_canonname = (void *)((char *)ai->ai_addr +
91 sizeof(struct sockaddr_in));
92 memcpy(ai->ai_canonname, dummy, namelen);
93
94 ai->ai_family = AF_INET;
95 ai->ai_addrlen = sizeof(struct sockaddr_in);
96
97 return ai;
98 }
99
create_node(void)100 static CURLcode create_node(void)
101 {
102 data_key = aprintf("%s:%d", "dummy", 0);
103 if(!data_key)
104 return CURLE_OUT_OF_MEMORY;
105
106 data_node = calloc(1, sizeof(struct Curl_dns_entry));
107 if(!data_node)
108 return CURLE_OUT_OF_MEMORY;
109
110 data_node->addr = fake_ai();
111 if(!data_node->addr)
112 return CURLE_OUT_OF_MEMORY;
113
114 return CURLE_OK;
115 }
116
117
118 UNITTEST_START
119
120 struct Curl_dns_entry *nodep;
121 size_t key_len;
122
123 /* Test 1305 exits without adding anything to the hash */
124 if(strcmp(arg, "1305") != 0) {
125 CURLcode rc = create_node();
126 abort_unless(rc == CURLE_OK, "data node creation failed");
127 key_len = strlen(data_key);
128
129 data_node->inuse = 1; /* hash will hold the reference */
130 nodep = Curl_hash_add(&hp, data_key, key_len + 1, data_node);
131 abort_unless(nodep, "insertion into hash failed");
132 /* Freeing will now be done by Curl_hash_destroy */
133 data_node = NULL;
134 }
135
136 UNITTEST_STOP
137