/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * Copyright (C) Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at https://curl.se/docs/copyright.html. * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is * furnished to do so, under the terms of the COPYING file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * * SPDX-License-Identifier: curl * ***************************************************************************/ #include "unitcheck.h" #include "urldata.h" #include "vtls/vtls.h" #ifdef HAVE_NET_IF_H #include #endif #if defined(HAVE_IF_NAMETOINDEX) && defined(USE_WINSOCK) #if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 5) #include /* workaround for old mingw-w64 missing to include it */ #endif #include #endif static CURLcode test_create2413(const char *name, CURL *curl, const struct Curl_scheme *scheme, const char *hostname, uint16_t port, const char *exp_hostname, bool exp_ipv6, const char *exp_zoneid, int exp_scopeid) { struct Curl_peer *peer = NULL; CURLcode result; result = Curl_peer_create((struct Curl_easy *)curl, scheme, hostname, port, &peer); if(result) { curl_mfprintf(stderr, "%s: create failed %d", name, (int)result); goto out; } result = CURLE_FAILED_INIT; #ifndef USE_IPV6 (void)exp_scopeid; #endif if(peer->scheme != scheme) { curl_mfprintf(stderr, "%s: has wrong scheme", name); } else if(!curl_strequal(peer->user_hostname, hostname)) { curl_mfprintf(stderr, "%s: user_hostname=%s, expected %s", name, peer->user_hostname, exp_hostname); } else if(exp_hostname && !curl_strequal(peer->hostname, exp_hostname)) curl_mfprintf(stderr, "%s: hostname=%s, expected %s", name, peer->hostname, exp_hostname); else if(peer->port != port) curl_mfprintf(stderr, "%s: port=%u, expected %u", name, peer->port, port); else if((bool)peer->ipv6 != exp_ipv6) curl_mfprintf(stderr, "%s: ipv6=%d, expected %d", name, peer->ipv6, exp_ipv6); else if(exp_zoneid && (!peer->zoneid || !curl_strequal(exp_zoneid, peer->zoneid))) curl_mfprintf(stderr, "%s: zoneid=%s, expected %s", name, peer->zoneid, exp_zoneid); else if(!exp_zoneid && peer->zoneid) curl_mfprintf(stderr, "%s: zoneid=%s, expected nothing", name, peer->zoneid); #ifdef USE_IPV6 else if((exp_scopeid >= 0) && (peer->scopeid != (uint32_t)exp_scopeid)) curl_mfprintf(stderr, "%s: scopeid=%u, expected %d", name, peer->scopeid, exp_scopeid); #endif else result = CURLE_OK; out: Curl_peer_unlink(&peer); fail_unless(!result, "check failed"); return result; } static CURLcode t2413_cmp(const char *name, CURL *curl, const struct Curl_scheme *scheme1, const char *hostname1, uint16_t port1, const struct Curl_scheme *scheme2, const char *hostname2, uint16_t port2, bool exp_equal, bool exp_same_dest) { struct Curl_peer *peer1 = NULL; struct Curl_peer *peer2 = NULL; CURLcode result; result = Curl_peer_create((struct Curl_easy *)curl, scheme1, hostname1, port1, &peer1); if(result) { curl_mfprintf(stderr, "%s: create peer1 failed %d", name, (int)result); goto out; } result = Curl_peer_create((struct Curl_easy *)curl, scheme2, hostname2, port2, &peer2); if(result) { curl_mfprintf(stderr, "%s: create peer2 failed %d", name, (int)result); goto out; } result = CURLE_FAILED_INIT; if(exp_equal != Curl_peer_equal(peer1, peer2)) curl_mfprintf(stderr, "%s: equal check failed, not %s", name, exp_equal ? "equal" : "different"); else if(exp_same_dest != Curl_peer_same_destination(peer1, peer2)) curl_mfprintf(stderr, "%s: same destination, not %s", name, exp_equal ? "the same" : "different"); else result = CURLE_OK; out: Curl_peer_unlink(&peer1); Curl_peer_unlink(&peer2); fail_unless(!result, "check failed"); return result; } #ifndef CURL_DISABLE_PROXY static CURLcode t2413_proxy(const char *name, CURL *curl, const char *proxy, uint8_t proxytype, long proxyport, uint16_t exp_port) { struct Curl_peer *peer = NULL; uint8_t ptype = proxytype; CURLU *uh = curl_url(); CURLcode result = CURLE_OUT_OF_MEMORY; if(!uh) goto out; /* parse it the same way the proxy setup does */ if(curl_url_set(uh, CURLUPART_URL, proxy, CURLU_NON_SUPPORT_SCHEME | CURLU_GUESS_SCHEME)) { curl_mfprintf(stderr, "%s: failed to parse %s", name, proxy); result = CURLE_URL_MALFORMAT; goto out; } curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxyport); result = Curl_peer_from_proxy_url(uh, (struct Curl_easy *)curl, proxy, proxytype, &peer, &ptype); if(result) { curl_mfprintf(stderr, "%s: create failed %d", name, (int)result); goto out; } if(peer->port != exp_port) { curl_mfprintf(stderr, "%s: port=%u, expected %u", name, peer->port, exp_port); result = CURLE_FAILED_INIT; } out: Curl_peer_unlink(&peer); curl_url_cleanup(uh); fail_unless(!result, "check failed"); return result; } #endif static uint32_t t2413_scopeid(const char *zone) { #ifdef HAVE_IF_NAMETOINDEX return (uint32_t)if_nametoindex(zone); #else (void)zone; return 0; #endif } static CURLcode test_unit2413(const char *arg) { UNITTEST_BEGIN_SIMPLE CURL *curl; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(!curl) { curl_global_cleanup(); goto unit_test_abort; } test_create2413("peer1", curl, &Curl_scheme_https, "test.curl.se", 1234, "test.curl.se", FALSE, NULL, 0); test_create2413("peer2", curl, &Curl_scheme_https, "127.0.0.1", 1234, "127.0.0.1", FALSE, NULL, 0); test_create2413("peer3", curl, &Curl_scheme_https, "::1", 1234, "::1", TRUE, NULL, 0); test_create2413("peer4", curl, &Curl_scheme_https, "[::1]", 1234, "::1", TRUE, NULL, 0); test_create2413("peer5", curl, &Curl_scheme_https, "test.curl.se.", 1234, "test.curl.se.", FALSE, NULL, 0); test_create2413("peer6", curl, &Curl_scheme_https, "[::1%tada]", 1234, "::1", TRUE, "tada", t2413_scopeid("tada")); test_create2413("peer7", curl, &Curl_scheme_https, "::1%tada", 1234, "::1", TRUE, "tada", t2413_scopeid("tada")); test_create2413("peer8", curl, &Curl_scheme_https, "::1%123", 1234, "::1", TRUE, "123", 123); test_create2413("peer9", curl, &Curl_scheme_https, "::1%123x", 1234, "::1", TRUE, "123x", t2413_scopeid("123x")); t2413_cmp("cmp1", curl, &Curl_scheme_https, "test.curl.se", 1234, &Curl_scheme_https, "test.curl.se", 1234, TRUE, TRUE); t2413_cmp("cmp2", curl, &Curl_scheme_https, "Test.curl.se", 1234, &Curl_scheme_https, "test.curl.se", 1234, TRUE, TRUE); t2413_cmp("cmp3", curl, &Curl_scheme_http, "test.curl.se", 1234, &Curl_scheme_https, "test.curl.se", 1234, FALSE, TRUE); t2413_cmp("cmp4", curl, &Curl_scheme_https, "test.curl.se", 1234, &Curl_scheme_https, "test.curl.se", 443, FALSE, FALSE); t2413_cmp("cmp5", curl, &Curl_scheme_https, "[::1%tada]", 443, &Curl_scheme_https, "[::1%tada]", 443, TRUE, TRUE); t2413_cmp("cmp6", curl, &Curl_scheme_https, "[::1%tada]", 443, &Curl_scheme_https, "[::1%Tada]", 443, FALSE, FALSE); #ifndef CURL_DISABLE_PROXY /* an explicit port is used, even when it is the default for the (possibly guessed) scheme */ t2413_proxy("proxy1", curl, "127.0.0.1:80", CURLPROXY_SOCKS5, 0, 80); t2413_proxy("proxy2", curl, "127.0.0.1:443", CURLPROXY_SOCKS4, 0, 443); t2413_proxy("proxy3", curl, "socks5h://127.0.0.1:80", CURLPROXY_HTTP, 0, 80); /* an explicit port overrides CURLOPT_PROXYPORT */ t2413_proxy("proxy4", curl, "http://127.0.0.1:80", CURLPROXY_HTTP, 3128, 80); t2413_proxy("proxy5", curl, "socks5://127.0.0.1:1080", CURLPROXY_HTTP, 9999, 1080); /* without a port, use CURLOPT_PROXYPORT or the default */ t2413_proxy("proxy6", curl, "127.0.0.1", CURLPROXY_HTTP, 3128, 3128); t2413_proxy("proxy7", curl, "127.0.0.1", CURLPROXY_HTTP, 0, 1080); t2413_proxy("proxy8", curl, "http://127.0.0.1", CURLPROXY_HTTP, 0, 1080); t2413_proxy("proxy9", curl, "socks4://127.0.0.1", CURLPROXY_HTTP, 0, 1080); if(Curl_ssl_supports((struct Curl_easy *)curl, SSLSUPP_HTTPS_PROXY)) { t2413_proxy("proxy10", curl, "https://127.0.0.1", CURLPROXY_HTTP, 0, 443); t2413_proxy("proxy11", curl, "https://127.0.0.1:443", CURLPROXY_HTTP, 3129, 443); } curl_easy_setopt(curl, CURLOPT_PROXYPORT, 0L); #endif curl_easy_cleanup(curl); curl_global_cleanup(); UNITTEST_END_SIMPLE }