xref: /curl/tests/unit/unit1663.c (revision 71cf0d1f)
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 "curlcheck.h"
25 
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
28 #endif
29 #ifdef HAVE_NETINET_IN6_H
30 #include <netinet/in6.h>
31 #endif
32 
33 #include <curl/curl.h>
34 
35 #include "cf-socket.h"
36 
37 #include "memdebug.h" /* LAST include file */
38 
unit_setup(void)39 static CURLcode unit_setup(void)
40 {
41   CURLcode res = CURLE_OK;
42   global_init(CURL_GLOBAL_ALL);
43   return res;
44 }
45 
unit_stop(void)46 static void unit_stop(void)
47 {
48   curl_global_cleanup();
49 }
50 
test_parse(const char * input_data,const char * exp_dev,const char * exp_iface,const char * exp_host,CURLcode exp_rc)51 static void test_parse(
52   const char *input_data,
53   const char *exp_dev,
54   const char *exp_iface,
55   const char *exp_host,
56   CURLcode exp_rc)
57 {
58   char *dev = NULL;
59   char *iface = NULL;
60   char *host = NULL;
61   CURLcode rc = Curl_parse_interface(input_data, &dev, &iface, &host);
62   fail_unless(rc == exp_rc, "Curl_parse_interface() failed");
63 
64   fail_unless(!!exp_dev == !!dev, "dev expectation failed.");
65   fail_unless(!!exp_iface == !!iface, "iface expectation failed");
66   fail_unless(!!exp_host == !!host, "host expectation failed");
67 
68   if(!unitfail) {
69     fail_unless(!exp_dev || strcmp(dev, exp_dev) == 0,
70                 "dev should be equal to exp_dev");
71     fail_unless(!exp_iface || strcmp(iface, exp_iface) == 0,
72                 "iface should be equal to exp_iface");
73     fail_unless(!exp_host || strcmp(host, exp_host) == 0,
74                 "host should be equal to exp_host");
75   }
76 
77   free(dev);
78   free(iface);
79   free(host);
80 }
81 
82 UNITTEST_START
83 {
84   test_parse("dev", "dev", NULL, NULL, CURLE_OK);
85   test_parse("if!eth0", NULL, "eth0", NULL, CURLE_OK);
86   test_parse("host!myname", NULL, NULL, "myname", CURLE_OK);
87   test_parse("ifhost!eth0!myname", NULL, "eth0", "myname", CURLE_OK);
88   test_parse("", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
89   test_parse("!", "!", NULL, NULL, CURLE_OK);
90   test_parse("if!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
91   test_parse("if!eth0!blubb", NULL, "eth0!blubb", NULL, CURLE_OK);
92   test_parse("host!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
93   test_parse("ifhost!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
94   test_parse("ifhost!eth0", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
95   test_parse("ifhost!eth0!", NULL, NULL, NULL, CURLE_BAD_FUNCTION_ARGUMENT);
96 }
97 UNITTEST_STOP
98