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