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#include "urldata.h"
2713498266Sopenharmony_ci#include "connect.h"
2813498266Sopenharmony_ci#include "share.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include "memdebug.h" /* LAST include file */
3113498266Sopenharmony_ci
3213498266Sopenharmony_cistatic void unit_stop(void)
3313498266Sopenharmony_ci{
3413498266Sopenharmony_ci  curl_global_cleanup();
3513498266Sopenharmony_ci}
3613498266Sopenharmony_ci
3713498266Sopenharmony_cistatic CURLcode unit_setup(void)
3813498266Sopenharmony_ci{
3913498266Sopenharmony_ci  CURLcode res = CURLE_OK;
4013498266Sopenharmony_ci
4113498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci  return res;
4413498266Sopenharmony_ci}
4513498266Sopenharmony_ci
4613498266Sopenharmony_cistruct testcase {
4713498266Sopenharmony_ci  /* host:port:address[,address]... */
4813498266Sopenharmony_ci  const char *optval;
4913498266Sopenharmony_ci
5013498266Sopenharmony_ci  /* lowercase host and port to retrieve the addresses from hostcache */
5113498266Sopenharmony_ci  const char *host;
5213498266Sopenharmony_ci  int port;
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  /* whether we expect a permanent or non-permanent cache entry */
5513498266Sopenharmony_ci  bool permanent;
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci  /* 0 to 9 addresses expected from hostcache */
5813498266Sopenharmony_ci  const char *address[10];
5913498266Sopenharmony_ci};
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci/* In builds without IPv6 support CURLOPT_RESOLVE should skip over those
6313498266Sopenharmony_ci   addresses, so we have to do that as well. */
6413498266Sopenharmony_cistatic const char skip = 0;
6513498266Sopenharmony_ci#ifdef ENABLE_IPV6
6613498266Sopenharmony_ci#define IPV6ONLY(x) x
6713498266Sopenharmony_ci#else
6813498266Sopenharmony_ci#define IPV6ONLY(x) &skip
6913498266Sopenharmony_ci#endif
7013498266Sopenharmony_ci
7113498266Sopenharmony_ci/* CURLOPT_RESOLVE address parsing tests */
7213498266Sopenharmony_cistatic const struct testcase tests[] = {
7313498266Sopenharmony_ci  /* spaces aren't allowed, for now */
7413498266Sopenharmony_ci  { "test.com:80:127.0.0.1, 127.0.0.2",
7513498266Sopenharmony_ci    "test.com", 80, TRUE, { NULL, }
7613498266Sopenharmony_ci  },
7713498266Sopenharmony_ci  { "TEST.com:80:,,127.0.0.1,,,127.0.0.2,,,,::1,,,",
7813498266Sopenharmony_ci    "test.com", 80, TRUE, { "127.0.0.1", "127.0.0.2", IPV6ONLY("::1"), }
7913498266Sopenharmony_ci  },
8013498266Sopenharmony_ci  { "test.com:80:::1,127.0.0.1",
8113498266Sopenharmony_ci    "test.com", 80, TRUE, { IPV6ONLY("::1"), "127.0.0.1", }
8213498266Sopenharmony_ci  },
8313498266Sopenharmony_ci  { "test.com:80:[::1],127.0.0.1",
8413498266Sopenharmony_ci    "test.com", 80, TRUE, { IPV6ONLY("::1"), "127.0.0.1", }
8513498266Sopenharmony_ci  },
8613498266Sopenharmony_ci  { "test.com:80:::1",
8713498266Sopenharmony_ci    "test.com", 80, TRUE, { IPV6ONLY("::1"), }
8813498266Sopenharmony_ci  },
8913498266Sopenharmony_ci  { "test.com:80:[::1]",
9013498266Sopenharmony_ci    "test.com", 80, TRUE, { IPV6ONLY("::1"), }
9113498266Sopenharmony_ci  },
9213498266Sopenharmony_ci  { "test.com:80:127.0.0.1",
9313498266Sopenharmony_ci    "test.com", 80, TRUE, { "127.0.0.1", }
9413498266Sopenharmony_ci  },
9513498266Sopenharmony_ci  { "test.com:80:,127.0.0.1",
9613498266Sopenharmony_ci    "test.com", 80, TRUE, { "127.0.0.1", }
9713498266Sopenharmony_ci  },
9813498266Sopenharmony_ci  { "test.com:80:127.0.0.1,",
9913498266Sopenharmony_ci    "test.com", 80, TRUE, { "127.0.0.1", }
10013498266Sopenharmony_ci  },
10113498266Sopenharmony_ci  { "test.com:0:127.0.0.1",
10213498266Sopenharmony_ci    "test.com", 0, TRUE, { "127.0.0.1", }
10313498266Sopenharmony_ci  },
10413498266Sopenharmony_ci  { "+test.com:80:127.0.0.1,",
10513498266Sopenharmony_ci    "test.com", 80, FALSE, { "127.0.0.1", }
10613498266Sopenharmony_ci  },
10713498266Sopenharmony_ci};
10813498266Sopenharmony_ci
10913498266Sopenharmony_ciUNITTEST_START
11013498266Sopenharmony_ci{
11113498266Sopenharmony_ci  int i;
11213498266Sopenharmony_ci  int testnum = sizeof(tests) / sizeof(struct testcase);
11313498266Sopenharmony_ci  struct Curl_multi *multi = NULL;
11413498266Sopenharmony_ci  struct Curl_easy *easy = NULL;
11513498266Sopenharmony_ci  struct curl_slist *list = NULL;
11613498266Sopenharmony_ci
11713498266Sopenharmony_ci  for(i = 0; i < testnum; ++i) {
11813498266Sopenharmony_ci    int j;
11913498266Sopenharmony_ci    int addressnum = sizeof(tests[i].address) / sizeof(*tests[i].address);
12013498266Sopenharmony_ci    struct Curl_addrinfo *addr;
12113498266Sopenharmony_ci    struct Curl_dns_entry *dns;
12213498266Sopenharmony_ci    void *entry_id;
12313498266Sopenharmony_ci    bool problem = false;
12413498266Sopenharmony_ci    easy = curl_easy_init();
12513498266Sopenharmony_ci    if(!easy)
12613498266Sopenharmony_ci      goto error;
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci    /* create a multi handle and add the easy handle to it so that the
12913498266Sopenharmony_ci       hostcache is setup */
13013498266Sopenharmony_ci    multi = curl_multi_init();
13113498266Sopenharmony_ci    curl_multi_add_handle(multi, easy);
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci    list = curl_slist_append(NULL, tests[i].optval);
13413498266Sopenharmony_ci    if(!list)
13513498266Sopenharmony_ci      goto error;
13613498266Sopenharmony_ci    curl_easy_setopt(easy, CURLOPT_RESOLVE, list);
13713498266Sopenharmony_ci
13813498266Sopenharmony_ci    Curl_loadhostpairs(easy);
13913498266Sopenharmony_ci
14013498266Sopenharmony_ci    entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port);
14113498266Sopenharmony_ci    if(!entry_id)
14213498266Sopenharmony_ci      goto error;
14313498266Sopenharmony_ci    dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1);
14413498266Sopenharmony_ci    free(entry_id);
14513498266Sopenharmony_ci    entry_id = NULL;
14613498266Sopenharmony_ci
14713498266Sopenharmony_ci    addr = dns ? dns->addr : NULL;
14813498266Sopenharmony_ci
14913498266Sopenharmony_ci    for(j = 0; j < addressnum; ++j) {
15013498266Sopenharmony_ci      int port = 0;
15113498266Sopenharmony_ci      char ipaddress[MAX_IPADR_LEN] = {0};
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci      if(!addr && !tests[i].address[j])
15413498266Sopenharmony_ci        break;
15513498266Sopenharmony_ci
15613498266Sopenharmony_ci      if(tests[i].address[j] == &skip)
15713498266Sopenharmony_ci        continue;
15813498266Sopenharmony_ci
15913498266Sopenharmony_ci      if(addr && !Curl_addr2string(addr->ai_addr, addr->ai_addrlen,
16013498266Sopenharmony_ci                                   ipaddress, &port)) {
16113498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. getaddressinfo failed.\n",
16213498266Sopenharmony_ci                __FILE__, __LINE__, i);
16313498266Sopenharmony_ci        problem = true;
16413498266Sopenharmony_ci        break;
16513498266Sopenharmony_ci      }
16613498266Sopenharmony_ci
16713498266Sopenharmony_ci      if(addr && !tests[i].address[j]) {
16813498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
16913498266Sopenharmony_ci                "is %s but tests[%d].address[%d] is NULL.\n",
17013498266Sopenharmony_ci                __FILE__, __LINE__, i, ipaddress, i, j);
17113498266Sopenharmony_ci        problem = true;
17213498266Sopenharmony_ci        break;
17313498266Sopenharmony_ci      }
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci      if(!addr && tests[i].address[j]) {
17613498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
17713498266Sopenharmony_ci                "is NULL but tests[%d].address[%d] is %s.\n",
17813498266Sopenharmony_ci                __FILE__, __LINE__, i, i, j, tests[i].address[j]);
17913498266Sopenharmony_ci        problem = true;
18013498266Sopenharmony_ci        break;
18113498266Sopenharmony_ci      }
18213498266Sopenharmony_ci
18313498266Sopenharmony_ci      if(!curl_strequal(ipaddress, tests[i].address[j])) {
18413498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr "
18513498266Sopenharmony_ci                "%s is not equal to tests[%d].address[%d] %s.\n",
18613498266Sopenharmony_ci                __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]);
18713498266Sopenharmony_ci        problem = true;
18813498266Sopenharmony_ci        break;
18913498266Sopenharmony_ci      }
19013498266Sopenharmony_ci
19113498266Sopenharmony_ci      if(port != tests[i].port) {
19213498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port "
19313498266Sopenharmony_ci                "for tests[%d].address[%d] is %d but tests[%d].port is %d.\n",
19413498266Sopenharmony_ci                __FILE__, __LINE__, i, i, j, port, i, tests[i].port);
19513498266Sopenharmony_ci        problem = true;
19613498266Sopenharmony_ci        break;
19713498266Sopenharmony_ci      }
19813498266Sopenharmony_ci
19913498266Sopenharmony_ci      if(dns->timestamp && tests[i].permanent) {
20013498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is not zero "
20113498266Sopenharmony_ci                "but tests[%d].permanent is TRUE\n",
20213498266Sopenharmony_ci                __FILE__, __LINE__, i, i);
20313498266Sopenharmony_ci        problem = true;
20413498266Sopenharmony_ci        break;
20513498266Sopenharmony_ci      }
20613498266Sopenharmony_ci
20713498266Sopenharmony_ci      if(dns->timestamp == 0 && !tests[i].permanent) {
20813498266Sopenharmony_ci        fprintf(stderr, "%s:%d tests[%d] failed. the timestamp is zero "
20913498266Sopenharmony_ci                "but tests[%d].permanent is FALSE\n",
21013498266Sopenharmony_ci                __FILE__, __LINE__, i, i);
21113498266Sopenharmony_ci        problem = true;
21213498266Sopenharmony_ci        break;
21313498266Sopenharmony_ci      }
21413498266Sopenharmony_ci
21513498266Sopenharmony_ci      addr = addr->ai_next;
21613498266Sopenharmony_ci    }
21713498266Sopenharmony_ci
21813498266Sopenharmony_ci    curl_easy_cleanup(easy);
21913498266Sopenharmony_ci    easy = NULL;
22013498266Sopenharmony_ci    curl_multi_cleanup(multi);
22113498266Sopenharmony_ci    multi = NULL;
22213498266Sopenharmony_ci    curl_slist_free_all(list);
22313498266Sopenharmony_ci    list = NULL;
22413498266Sopenharmony_ci
22513498266Sopenharmony_ci    if(problem) {
22613498266Sopenharmony_ci      unitfail++;
22713498266Sopenharmony_ci      continue;
22813498266Sopenharmony_ci    }
22913498266Sopenharmony_ci  }
23013498266Sopenharmony_cierror:
23113498266Sopenharmony_ci  curl_easy_cleanup(easy);
23213498266Sopenharmony_ci  curl_multi_cleanup(multi);
23313498266Sopenharmony_ci  curl_slist_free_all(list);
23413498266Sopenharmony_ci}
23513498266Sopenharmony_ciUNITTEST_STOP
236