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 /* 0 to 9 addresses expected from hostcache */ 5513498266Sopenharmony_ci const char *address[10]; 5613498266Sopenharmony_ci}; 5713498266Sopenharmony_ci 5813498266Sopenharmony_ci 5913498266Sopenharmony_ci/* CURLOPT_RESOLVE address parsing test - to test the following defect fix: 6013498266Sopenharmony_ci 6113498266Sopenharmony_ci 1) if there is already existing host:port pair in the DNS cache and 6213498266Sopenharmony_ci we call CURLOPT_RESOLVE, it should also replace addresses. 6313498266Sopenharmony_ci for example, if there is "test.com:80" with address "1.1.1.1" 6413498266Sopenharmony_ci and we called CURLOPT_RESOLVE with address "2.2.2.2", then DNS entry needs to 6513498266Sopenharmony_ci reflect that. 6613498266Sopenharmony_ci 6713498266Sopenharmony_ci 2) when cached address is already there and close to expire, then by the 6813498266Sopenharmony_ci time request is made, it can get expired. This happens because, when 6913498266Sopenharmony_ci we set address using CURLOPT_RESOLVE, 7013498266Sopenharmony_ci it usually marks as permanent (by setting timestamp to zero). However, 7113498266Sopenharmony_ci if address already exists 7213498266Sopenharmony_ciin the cache, then it does not mark it, but just leaves it as it is. 7313498266Sopenharmony_ci So we fixing this by timestamp to zero if address already exists too. 7413498266Sopenharmony_ci 7513498266Sopenharmony_ciTest: 7613498266Sopenharmony_ci 7713498266Sopenharmony_ci - insert new entry 7813498266Sopenharmony_ci - verify that timestamp is not zero 7913498266Sopenharmony_ci - call set options with CURLOPT_RESOLVE 8013498266Sopenharmony_ci - then, call Curl_loadhostpairs 8113498266Sopenharmony_ci 8213498266Sopenharmony_ci expected result: cached address has zero timestamp. 8313498266Sopenharmony_ci 8413498266Sopenharmony_ci - call set options with CURLOPT_RESOLVE with same host:port pair, 8513498266Sopenharmony_ci different address. 8613498266Sopenharmony_ci - then, call Curl_loadhostpairs 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci expected result: cached address has zero timestamp and new address 8913498266Sopenharmony_ci*/ 9013498266Sopenharmony_ci 9113498266Sopenharmony_cistatic const struct testcase tests[] = { 9213498266Sopenharmony_ci /* spaces aren't allowed, for now */ 9313498266Sopenharmony_ci { "test.com:80:127.0.0.1", 9413498266Sopenharmony_ci "test.com", 80, { "127.0.0.1", } 9513498266Sopenharmony_ci }, 9613498266Sopenharmony_ci { "test.com:80:127.0.0.2", 9713498266Sopenharmony_ci "test.com", 80, { "127.0.0.2", } 9813498266Sopenharmony_ci }, 9913498266Sopenharmony_ci}; 10013498266Sopenharmony_ci 10113498266Sopenharmony_ciUNITTEST_START 10213498266Sopenharmony_ci{ 10313498266Sopenharmony_ci int i; 10413498266Sopenharmony_ci int testnum = sizeof(tests) / sizeof(struct testcase); 10513498266Sopenharmony_ci struct Curl_multi *multi = NULL; 10613498266Sopenharmony_ci struct Curl_easy *easy = NULL; 10713498266Sopenharmony_ci struct curl_slist *list = NULL; 10813498266Sopenharmony_ci 10913498266Sopenharmony_ci/* important: we setup cache outside of the loop 11013498266Sopenharmony_ci and also clean cache after the loop. In contrast,for example, 11113498266Sopenharmony_ci test 1607 sets up and cleans cache on each iteration. */ 11213498266Sopenharmony_ci 11313498266Sopenharmony_ci for(i = 0; i < testnum; ++i) { 11413498266Sopenharmony_ci int j; 11513498266Sopenharmony_ci int addressnum = sizeof (tests[i].address) / sizeof (*tests[i].address); 11613498266Sopenharmony_ci struct Curl_addrinfo *addr; 11713498266Sopenharmony_ci struct Curl_dns_entry *dns; 11813498266Sopenharmony_ci void *entry_id; 11913498266Sopenharmony_ci bool problem = false; 12013498266Sopenharmony_ci easy = curl_easy_init(); 12113498266Sopenharmony_ci if(!easy) { 12213498266Sopenharmony_ci curl_global_cleanup(); 12313498266Sopenharmony_ci return CURLE_OUT_OF_MEMORY; 12413498266Sopenharmony_ci } 12513498266Sopenharmony_ci /* create a multi handle and add the easy handle to it so that the 12613498266Sopenharmony_ci hostcache is setup */ 12713498266Sopenharmony_ci multi = curl_multi_init(); 12813498266Sopenharmony_ci if(!multi) 12913498266Sopenharmony_ci goto error; 13013498266Sopenharmony_ci curl_multi_add_handle(multi, easy); 13113498266Sopenharmony_ci 13213498266Sopenharmony_ci list = curl_slist_append(NULL, tests[i].optval); 13313498266Sopenharmony_ci if(!list) 13413498266Sopenharmony_ci goto error; 13513498266Sopenharmony_ci 13613498266Sopenharmony_ci curl_easy_setopt(easy, CURLOPT_RESOLVE, list); 13713498266Sopenharmony_ci 13813498266Sopenharmony_ci if(Curl_loadhostpairs(easy)) 13913498266Sopenharmony_ci goto error; 14013498266Sopenharmony_ci 14113498266Sopenharmony_ci entry_id = (void *)aprintf("%s:%d", tests[i].host, tests[i].port); 14213498266Sopenharmony_ci if(!entry_id) 14313498266Sopenharmony_ci goto error; 14413498266Sopenharmony_ci 14513498266Sopenharmony_ci dns = Curl_hash_pick(easy->dns.hostcache, entry_id, strlen(entry_id) + 1); 14613498266Sopenharmony_ci free(entry_id); 14713498266Sopenharmony_ci entry_id = NULL; 14813498266Sopenharmony_ci 14913498266Sopenharmony_ci addr = dns ? dns->addr : NULL; 15013498266Sopenharmony_ci 15113498266Sopenharmony_ci for(j = 0; j < addressnum; ++j) { 15213498266Sopenharmony_ci int port = 0; 15313498266Sopenharmony_ci char ipaddress[MAX_IPADR_LEN] = {0}; 15413498266Sopenharmony_ci 15513498266Sopenharmony_ci if(!addr && !tests[i].address[j]) 15613498266Sopenharmony_ci break; 15713498266Sopenharmony_ci 15813498266Sopenharmony_ci if(addr && !Curl_addr2string(addr->ai_addr, addr->ai_addrlen, 15913498266Sopenharmony_ci ipaddress, &port)) { 16013498266Sopenharmony_ci fprintf(stderr, "%s:%d tests[%d] failed. Curl_addr2string failed.\n", 16113498266Sopenharmony_ci __FILE__, __LINE__, i); 16213498266Sopenharmony_ci problem = true; 16313498266Sopenharmony_ci break; 16413498266Sopenharmony_ci } 16513498266Sopenharmony_ci 16613498266Sopenharmony_ci if(addr && !tests[i].address[j]) { 16713498266Sopenharmony_ci fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr " 16813498266Sopenharmony_ci "is %s but tests[%d].address[%d] is NULL.\n", 16913498266Sopenharmony_ci __FILE__, __LINE__, i, ipaddress, i, j); 17013498266Sopenharmony_ci problem = true; 17113498266Sopenharmony_ci break; 17213498266Sopenharmony_ci } 17313498266Sopenharmony_ci 17413498266Sopenharmony_ci if(!addr && tests[i].address[j]) { 17513498266Sopenharmony_ci fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr " 17613498266Sopenharmony_ci "is NULL but tests[%d].address[%d] is %s.\n", 17713498266Sopenharmony_ci __FILE__, __LINE__, i, i, j, tests[i].address[j]); 17813498266Sopenharmony_ci problem = true; 17913498266Sopenharmony_ci break; 18013498266Sopenharmony_ci } 18113498266Sopenharmony_ci 18213498266Sopenharmony_ci if(!curl_strequal(ipaddress, tests[i].address[j])) { 18313498266Sopenharmony_ci fprintf(stderr, "%s:%d tests[%d] failed. the retrieved addr " 18413498266Sopenharmony_ci "%s is not equal to tests[%d].address[%d] %s.\n", 18513498266Sopenharmony_ci __FILE__, __LINE__, i, ipaddress, i, j, tests[i].address[j]); 18613498266Sopenharmony_ci problem = true; 18713498266Sopenharmony_ci break; 18813498266Sopenharmony_ci } 18913498266Sopenharmony_ci 19013498266Sopenharmony_ci if(port != tests[i].port) { 19113498266Sopenharmony_ci fprintf(stderr, "%s:%d tests[%d] failed. the retrieved port " 19213498266Sopenharmony_ci "for tests[%d].address[%d] is %d but tests[%d].port is %d.\n", 19313498266Sopenharmony_ci __FILE__, __LINE__, i, i, j, port, i, tests[i].port); 19413498266Sopenharmony_ci problem = true; 19513498266Sopenharmony_ci break; 19613498266Sopenharmony_ci } 19713498266Sopenharmony_ci 19813498266Sopenharmony_ci addr = addr->ai_next; 19913498266Sopenharmony_ci } 20013498266Sopenharmony_ci 20113498266Sopenharmony_ci curl_easy_cleanup(easy); 20213498266Sopenharmony_ci easy = NULL; 20313498266Sopenharmony_ci Curl_hash_destroy(&multi->hostcache); 20413498266Sopenharmony_ci curl_multi_cleanup(multi); 20513498266Sopenharmony_ci multi = NULL; 20613498266Sopenharmony_ci curl_slist_free_all(list); 20713498266Sopenharmony_ci list = NULL; 20813498266Sopenharmony_ci 20913498266Sopenharmony_ci if(problem) { 21013498266Sopenharmony_ci unitfail++; 21113498266Sopenharmony_ci continue; 21213498266Sopenharmony_ci } 21313498266Sopenharmony_ci } 21413498266Sopenharmony_ci goto unit_test_abort; 21513498266Sopenharmony_cierror: 21613498266Sopenharmony_ci curl_easy_cleanup(easy); 21713498266Sopenharmony_ci curl_multi_cleanup(multi); 21813498266Sopenharmony_ci curl_slist_free_all(list); 21913498266Sopenharmony_ci} 22013498266Sopenharmony_ciUNITTEST_STOP 221