xref: /aosp_15_r20/external/curl/tests/unit/unit1663.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker #include "curlcheck.h"
25*6236dae4SAndroid Build Coastguard Worker 
26*6236dae4SAndroid Build Coastguard Worker #ifdef HAVE_NETINET_IN_H
27*6236dae4SAndroid Build Coastguard Worker #include <netinet/in.h>
28*6236dae4SAndroid Build Coastguard Worker #endif
29*6236dae4SAndroid Build Coastguard Worker #ifdef HAVE_NETINET_IN6_H
30*6236dae4SAndroid Build Coastguard Worker #include <netinet/in6.h>
31*6236dae4SAndroid Build Coastguard Worker #endif
32*6236dae4SAndroid Build Coastguard Worker 
33*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
34*6236dae4SAndroid Build Coastguard Worker 
35*6236dae4SAndroid Build Coastguard Worker #include "cf-socket.h"
36*6236dae4SAndroid Build Coastguard Worker 
37*6236dae4SAndroid Build Coastguard Worker #include "memdebug.h" /* LAST include file */
38*6236dae4SAndroid Build Coastguard Worker 
unit_setup(void)39*6236dae4SAndroid Build Coastguard Worker static CURLcode unit_setup(void)
40*6236dae4SAndroid Build Coastguard Worker {
41*6236dae4SAndroid Build Coastguard Worker   CURLcode res = CURLE_OK;
42*6236dae4SAndroid Build Coastguard Worker   global_init(CURL_GLOBAL_ALL);
43*6236dae4SAndroid Build Coastguard Worker   return res;
44*6236dae4SAndroid Build Coastguard Worker }
45*6236dae4SAndroid Build Coastguard Worker 
unit_stop(void)46*6236dae4SAndroid Build Coastguard Worker static void unit_stop(void)
47*6236dae4SAndroid Build Coastguard Worker {
48*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
49*6236dae4SAndroid Build Coastguard Worker }
50*6236dae4SAndroid Build Coastguard Worker 
test_parse(const char * input_data,const char * exp_dev,const char * exp_iface,const char * exp_host,CURLcode exp_rc)51*6236dae4SAndroid Build Coastguard Worker static void test_parse(
52*6236dae4SAndroid Build Coastguard Worker   const char *input_data,
53*6236dae4SAndroid Build Coastguard Worker   const char *exp_dev,
54*6236dae4SAndroid Build Coastguard Worker   const char *exp_iface,
55*6236dae4SAndroid Build Coastguard Worker   const char *exp_host,
56*6236dae4SAndroid Build Coastguard Worker   CURLcode exp_rc)
57*6236dae4SAndroid Build Coastguard Worker {
58*6236dae4SAndroid Build Coastguard Worker   char *dev = NULL;
59*6236dae4SAndroid Build Coastguard Worker   char *iface = NULL;
60*6236dae4SAndroid Build Coastguard Worker   char *host = NULL;
61*6236dae4SAndroid Build Coastguard Worker   CURLcode rc = Curl_parse_interface(input_data, &dev, &iface, &host);
62*6236dae4SAndroid Build Coastguard Worker   fail_unless(rc == exp_rc, "Curl_parse_interface() failed");
63*6236dae4SAndroid Build Coastguard Worker 
64*6236dae4SAndroid Build Coastguard Worker   fail_unless(!!exp_dev == !!dev, "dev expectation failed.");
65*6236dae4SAndroid Build Coastguard Worker   fail_unless(!!exp_iface == !!iface, "iface expectation failed");
66*6236dae4SAndroid Build Coastguard Worker   fail_unless(!!exp_host == !!host, "host expectation failed");
67*6236dae4SAndroid Build Coastguard Worker 
68*6236dae4SAndroid Build Coastguard Worker   if(!unitfail) {
69*6236dae4SAndroid Build Coastguard Worker     fail_unless(!exp_dev || strcmp(dev, exp_dev) == 0,
70*6236dae4SAndroid Build Coastguard Worker                 "dev should be equal to exp_dev");
71*6236dae4SAndroid Build Coastguard Worker     fail_unless(!exp_iface || strcmp(iface, exp_iface) == 0,
72*6236dae4SAndroid Build Coastguard Worker                 "iface should be equal to exp_iface");
73*6236dae4SAndroid Build Coastguard Worker     fail_unless(!exp_host || strcmp(host, exp_host) == 0,
74*6236dae4SAndroid Build Coastguard Worker                 "host should be equal to exp_host");
75*6236dae4SAndroid Build Coastguard Worker   }
76*6236dae4SAndroid Build Coastguard Worker 
77*6236dae4SAndroid Build Coastguard Worker   free(dev);
78*6236dae4SAndroid Build Coastguard Worker   free(iface);
79*6236dae4SAndroid Build Coastguard Worker   free(host);
80*6236dae4SAndroid Build Coastguard Worker }
81*6236dae4SAndroid Build Coastguard Worker 
82*6236dae4SAndroid Build Coastguard Worker UNITTEST_START
83*6236dae4SAndroid Build Coastguard Worker {
84*6236dae4SAndroid Build Coastguard Worker   test_parse("dev", "dev", NULL, NULL, CURLE_OK);
85*6236dae4SAndroid Build Coastguard Worker   test_parse("if!eth0", NULL, "eth0", NULL, CURLE_OK);
86*6236dae4SAndroid Build Coastguard Worker   test_parse("host!myname", NULL, NULL, "myname", CURLE_OK);
87*6236dae4SAndroid Build Coastguard Worker   test_parse("ifhost!eth0!myname", NULL, "eth0", "myname", CURLE_OK);
88*6236dae4SAndroid Build Coastguard Worker   test_parse("", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
89*6236dae4SAndroid Build Coastguard Worker   test_parse("!", "!", NULL, NULL, CURLE_OK);
90*6236dae4SAndroid Build Coastguard Worker   test_parse("if!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
91*6236dae4SAndroid Build Coastguard Worker   test_parse("if!eth0!blubb", NULL, "eth0!blubb", NULL, CURLE_OK);
92*6236dae4SAndroid Build Coastguard Worker   test_parse("host!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
93*6236dae4SAndroid Build Coastguard Worker   test_parse("ifhost!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
94*6236dae4SAndroid Build Coastguard Worker   test_parse("ifhost!eth0", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
95*6236dae4SAndroid Build Coastguard Worker   test_parse("ifhost!eth0!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
96*6236dae4SAndroid Build Coastguard Worker }
97*6236dae4SAndroid Build Coastguard Worker UNITTEST_STOP
98