quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

unit1305.c (3292B)


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