113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#ifdef HAVE_NETINET_IN_H
2713498266Sopenharmony_ci#  include <netinet/in.h>
2813498266Sopenharmony_ci#endif
2913498266Sopenharmony_ci#ifdef HAVE_NETDB_H
3013498266Sopenharmony_ci#  include <netdb.h>
3113498266Sopenharmony_ci#endif
3213498266Sopenharmony_ci#ifdef HAVE_ARPA_INET_H
3313498266Sopenharmony_ci#  include <arpa/inet.h>
3413498266Sopenharmony_ci#endif
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci#define ENABLE_CURLX_PRINTF
3713498266Sopenharmony_ci#include "curlx.h"
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci#include "hash.h"
4013498266Sopenharmony_ci#include "hostip.h"
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#include "memdebug.h" /* LAST include file */
4313498266Sopenharmony_ci
4413498266Sopenharmony_cistatic struct Curl_easy *data;
4513498266Sopenharmony_cistatic struct Curl_hash hp;
4613498266Sopenharmony_cistatic char *data_key;
4713498266Sopenharmony_cistatic struct Curl_dns_entry *data_node;
4813498266Sopenharmony_ci
4913498266Sopenharmony_cistatic CURLcode unit_setup(void)
5013498266Sopenharmony_ci{
5113498266Sopenharmony_ci  data = curl_easy_init();
5213498266Sopenharmony_ci  if(!data) {
5313498266Sopenharmony_ci    curl_global_cleanup();
5413498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
5513498266Sopenharmony_ci  }
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci  Curl_init_dnscache(&hp, 7);
5813498266Sopenharmony_ci  return CURLE_OK;
5913498266Sopenharmony_ci}
6013498266Sopenharmony_ci
6113498266Sopenharmony_cistatic void unit_stop(void)
6213498266Sopenharmony_ci{
6313498266Sopenharmony_ci  if(data_node) {
6413498266Sopenharmony_ci    Curl_freeaddrinfo(data_node->addr);
6513498266Sopenharmony_ci    free(data_node);
6613498266Sopenharmony_ci  }
6713498266Sopenharmony_ci  free(data_key);
6813498266Sopenharmony_ci  Curl_hash_destroy(&hp);
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  curl_easy_cleanup(data);
7113498266Sopenharmony_ci  curl_global_cleanup();
7213498266Sopenharmony_ci}
7313498266Sopenharmony_ci
7413498266Sopenharmony_cistatic struct Curl_addrinfo *fake_ai(void)
7513498266Sopenharmony_ci{
7613498266Sopenharmony_ci  static struct Curl_addrinfo *ai;
7713498266Sopenharmony_ci  static const char dummy[]="dummy";
7813498266Sopenharmony_ci  size_t namelen = sizeof(dummy); /* including the null-terminator */
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_in) +
8113498266Sopenharmony_ci              namelen);
8213498266Sopenharmony_ci  if(!ai)
8313498266Sopenharmony_ci    return NULL;
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci  ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
8613498266Sopenharmony_ci  ai->ai_canonname = (void *)((char *)ai->ai_addr +
8713498266Sopenharmony_ci                              sizeof(struct sockaddr_in));
8813498266Sopenharmony_ci  memcpy(ai->ai_canonname, dummy, namelen);
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci  ai->ai_family = AF_INET;
9113498266Sopenharmony_ci  ai->ai_addrlen = sizeof(struct sockaddr_in);
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  return ai;
9413498266Sopenharmony_ci}
9513498266Sopenharmony_ci
9613498266Sopenharmony_cistatic CURLcode create_node(void)
9713498266Sopenharmony_ci{
9813498266Sopenharmony_ci  data_key = aprintf("%s:%d", "dummy", 0);
9913498266Sopenharmony_ci  if(!data_key)
10013498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
10113498266Sopenharmony_ci
10213498266Sopenharmony_ci  data_node = calloc(1, sizeof(struct Curl_dns_entry));
10313498266Sopenharmony_ci  if(!data_node)
10413498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci  data_node->addr = fake_ai();
10713498266Sopenharmony_ci  if(!data_node->addr)
10813498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
10913498266Sopenharmony_ci
11013498266Sopenharmony_ci  return CURLE_OK;
11113498266Sopenharmony_ci}
11213498266Sopenharmony_ci
11313498266Sopenharmony_ci
11413498266Sopenharmony_ciUNITTEST_START
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci  struct Curl_dns_entry *nodep;
11713498266Sopenharmony_ci  size_t key_len;
11813498266Sopenharmony_ci
11913498266Sopenharmony_ci  /* Test 1305 exits without adding anything to the hash */
12013498266Sopenharmony_ci  if(strcmp(arg, "1305") != 0) {
12113498266Sopenharmony_ci    CURLcode rc = create_node();
12213498266Sopenharmony_ci    abort_unless(rc == CURLE_OK, "data node creation failed");
12313498266Sopenharmony_ci    key_len = strlen(data_key);
12413498266Sopenharmony_ci
12513498266Sopenharmony_ci    data_node->inuse = 1; /* hash will hold the reference */
12613498266Sopenharmony_ci    nodep = Curl_hash_add(&hp, data_key, key_len + 1, data_node);
12713498266Sopenharmony_ci    abort_unless(nodep, "insertion into hash failed");
12813498266Sopenharmony_ci    /* Freeing will now be done by Curl_hash_destroy */
12913498266Sopenharmony_ci    data_node = NULL;
13013498266Sopenharmony_ci  }
13113498266Sopenharmony_ci
13213498266Sopenharmony_ciUNITTEST_STOP
133