xref: /curl/tests/unit/unit1614.c (revision 4e71f134)
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 #include "noproxy.h"
27 
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30   return CURLE_OK;
31 }
32 
unit_stop(void)33 static void unit_stop(void)
34 {
35 
36 }
37 
38 struct check {
39   const char *a;
40   const char *n;
41   unsigned int bits;
42   bool match;
43 };
44 
45 struct noproxy {
46   const char *a;
47   const char *n;
48   bool match;
49 };
50 
51 UNITTEST_START
52 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_PROXY)
53 {
54   int i;
55   int err = 0;
56   struct check list4[]= {
57     { "192.160.0.1", "192.160.0.1", 33, FALSE},
58     { "192.160.0.1", "192.160.0.1", 32, TRUE},
59     { "192.160.0.1", "192.160.0.1", 0, TRUE},
60     { "192.160.0.1", "192.160.0.1", 24, TRUE},
61     { "192.160.0.1", "192.160.0.1", 26, TRUE},
62     { "192.160.0.1", "192.160.0.1", 20, TRUE},
63     { "192.160.0.1", "192.160.0.1", 18, TRUE},
64     { "192.160.0.1", "192.160.0.1", 12, TRUE},
65     { "192.160.0.1", "192.160.0.1", 8, TRUE},
66     { "192.160.0.1", "10.0.0.1", 8, FALSE},
67     { "192.160.0.1", "10.0.0.1", 32, FALSE},
68     { "192.160.0.1", "10.0.0.1", 0, FALSE},
69     { NULL, NULL, 0, FALSE} /* end marker */
70   };
71   struct check list6[]= {
72     { "::1", "::1", 0, TRUE},
73     { "::1", "::1", 128, TRUE},
74     { "::1", "0:0::1", 128, TRUE},
75     { "::1", "0:0::1", 129, FALSE},
76     { "fe80::ab47:4396:55c9:8474", "fe80::ab47:4396:55c9:8474", 64, TRUE},
77     { NULL, NULL, 0, FALSE} /* end marker */
78   };
79   struct noproxy list[]= {
80     { "www.example.com", "localhost .example.com .example.de", FALSE},
81     { "www.example.com", "localhost,.example.com,.example.de", TRUE},
82     { "www.example.com.", "localhost,.example.com,.example.de", TRUE},
83     { "example.com", "localhost,.example.com,.example.de", TRUE},
84     { "example.com.", "localhost,.example.com,.example.de", TRUE},
85     { "www.example.com", "localhost,.example.com.,.example.de", TRUE},
86     { "www.example.com", "localhost,www.example.com.,.example.de", TRUE},
87     { "example.com", "localhost,example.com,.example.de", TRUE},
88     { "example.com.", "localhost,example.com,.example.de", TRUE},
89     { "nexample.com", "localhost,example.com,.example.de", FALSE},
90     { "www.example.com", "localhost,example.com,.example.de", TRUE},
91     { "127.0.0.1", "127.0.0.1,localhost", TRUE},
92     { "127.0.0.1", "127.0.0.1,localhost,", TRUE},
93     { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE},
94     { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE},
95     { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE},
96     { "127.0.0.1", "localhost,127.0.0.1", TRUE},
97     { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
98       "127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127."
99       "0.0.1.127.0.0.1.127.0.0." /* 128 bytes "address" */, FALSE},
100     { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
101       "127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1.127."
102       "0.0.1.127.0.0.1.127.0.0" /* 127 bytes "address" */, FALSE},
103     { "localhost", "localhost,127.0.0.1", TRUE},
104     { "localhost", "127.0.0.1,localhost", TRUE},
105     { "foobar", "barfoo", FALSE},
106     { "foobar", "foobar", TRUE},
107     { "192.168.0.1", "foobar", FALSE},
108     { "192.168.0.1", "192.168.0.0/16", TRUE},
109     { "192.168.0.1", "192.168.0.0/24", TRUE},
110     { "192.168.0.1", "192.168.0.0/32", FALSE},
111     { "192.168.0.1", "192.168.0.0", FALSE},
112     { "192.168.1.1", "192.168.0.0/24", FALSE},
113     { "192.168.1.1", "192.168.0.0/33", FALSE},
114     { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE},
115     { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE},
116     { "[::1]", "foo, bar, 192.168.0.0/16", FALSE},
117     { "[::1]", "foo, bar, ::1/64", TRUE},
118     { "[::1]", "::1/64", TRUE},
119     { "[::1]", "::1/96", TRUE},
120     { "[::1]", "::1/129", FALSE},
121     { "bar", "foo, bar, ::1/64", TRUE},
122     { "BAr", "foo, bar, ::1/64", TRUE},
123     { "BAr", "foo,,,,,              bar, ::1/64", TRUE},
124     { "www.example.com", "foo, .example.com", TRUE},
125     { "www.example.com", "www2.example.com, .example.net", FALSE},
126     { "example.com", ".example.com, .example.net", TRUE},
127     { "nonexample.com", ".example.com, .example.net", FALSE},
128     { NULL, NULL, FALSE}
129   };
130   for(i = 0; list4[i].a; i++) {
131     bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
132     if(match != list4[i].match) {
133       fprintf(stderr, "%s in %s/%u should %smatch\n",
134               list4[i].a, list4[i].n, list4[i].bits,
135               list4[i].match ? "": "not ");
136       err++;
137     }
138   }
139   for(i = 0; list6[i].a; i++) {
140     bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
141     if(match != list6[i].match) {
142       fprintf(stderr, "%s in %s/%u should %smatch\n",
143               list6[i].a, list6[i].n, list6[i].bits,
144               list6[i].match ? "": "not ");
145       err++;
146     }
147   }
148   for(i = 0; list[i].a; i++) {
149     bool match = Curl_check_noproxy(list[i].a, list[i].n);
150     if(match != list[i].match) {
151       fprintf(stderr, "%s in %s should %smatch\n",
152               list[i].a, list[i].n,
153               list[i].match ? "": "not ");
154       err++;
155     }
156   }
157   fail_if(err, "errors");
158 }
159 #endif
160 UNITTEST_STOP
161