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 #ifdef USE_IPV6
72 struct check list6[]= {
73 { "::1", "::1", 0, TRUE},
74 { "::1", "::1", 128, TRUE},
75 { "::1", "0:0::1", 128, TRUE},
76 { "::1", "0:0::1", 129, FALSE},
77 { "fe80::ab47:4396:55c9:8474", "fe80::ab47:4396:55c9:8474", 64, TRUE},
78 { NULL, NULL, 0, FALSE} /* end marker */
79 };
80 #endif
81 struct noproxy list[]= {
82 { "www.example.com", "localhost .example.com .example.de", FALSE},
83 { "www.example.com", "localhost,.example.com,.example.de", TRUE},
84 { "www.example.com.", "localhost,.example.com,.example.de", TRUE},
85 { "example.com", "localhost,.example.com,.example.de", TRUE},
86 { "example.com.", "localhost,.example.com,.example.de", TRUE},
87 { "www.example.com", "localhost,.example.com.,.example.de", TRUE},
88 { "www.example.com", "localhost,www.example.com.,.example.de", TRUE},
89 { "example.com", "localhost,example.com,.example.de", TRUE},
90 { "example.com.", "localhost,example.com,.example.de", TRUE},
91 { "nexample.com", "localhost,example.com,.example.de", FALSE},
92 { "www.example.com", "localhost,example.com,.example.de", TRUE},
93 { "127.0.0.1", "127.0.0.1,localhost", TRUE},
94 { "127.0.0.1", "127.0.0.1,localhost,", TRUE},
95 { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE},
96 { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE},
97 { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE},
98 { "127.0.0.1", "localhost,127.0.0.1", TRUE},
99 { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
100 "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."
101 "0.0.1.127.0.0.1.127.0.0." /* 128 bytes "address" */, FALSE},
102 { "127.0.0.1", "localhost,127.0.0.1.127.0.0.1.127.0.0.1.127.0.0.1."
103 "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."
104 "0.0.1.127.0.0.1.127.0.0" /* 127 bytes "address" */, FALSE},
105 { "localhost", "localhost,127.0.0.1", TRUE},
106 { "localhost", "127.0.0.1,localhost", TRUE},
107 { "foobar", "barfoo", FALSE},
108 { "foobar", "foobar", TRUE},
109 { "192.168.0.1", "foobar", FALSE},
110 { "192.168.0.1", "192.168.0.0/16", TRUE},
111 { "192.168.0.1", "192.168.0.0/24", TRUE},
112 { "192.168.0.1", "192.168.0.0/32", FALSE},
113 { "192.168.0.1", "192.168.0.0", FALSE},
114 { "192.168.1.1", "192.168.0.0/24", FALSE},
115 { "192.168.1.1", "192.168.0.0/33", FALSE},
116 { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE},
117 { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE},
118 #ifdef USE_IPV6
119 { "[::1]", "foo, bar, 192.168.0.0/16", FALSE},
120 { "[::1]", "foo, bar, ::1/64", TRUE},
121 { "[::1]", "::1/64", TRUE},
122 { "[::1]", "::1/96", TRUE},
123 { "[::1]", "::1/129", FALSE},
124 { "bar", "foo, bar, ::1/64", TRUE},
125 { "BAr", "foo, bar, ::1/64", TRUE},
126 { "BAr", "foo,,,,, bar, ::1/64", TRUE},
127 #endif
128 { "www.example.com", "foo, .example.com", TRUE},
129 { "www.example.com", "www2.example.com, .example.net", FALSE},
130 { "example.com", ".example.com, .example.net", TRUE},
131 { "nonexample.com", ".example.com, .example.net", FALSE},
132 { NULL, NULL, FALSE}
133 };
134 for(i = 0; list4[i].a; i++) {
135 bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
136 if(match != list4[i].match) {
137 fprintf(stderr, "%s in %s/%u should %smatch\n",
138 list4[i].a, list4[i].n, list4[i].bits,
139 list4[i].match ? "": "not ");
140 err++;
141 }
142 }
143 #ifdef USE_IPV6
144 for(i = 0; list6[i].a; i++) {
145 bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
146 if(match != list6[i].match) {
147 fprintf(stderr, "%s in %s/%u should %smatch\n",
148 list6[i].a, list6[i].n, list6[i].bits,
149 list6[i].match ? "": "not ");
150 err++;
151 }
152 }
153 #endif
154 for(i = 0; list[i].a; i++) {
155 bool match = Curl_check_noproxy(list[i].a, list[i].n);
156 if(match != list[i].match) {
157 fprintf(stderr, "%s in %s should %smatch\n",
158 list[i].a, list[i].n,
159 list[i].match ? "": "not ");
160 err++;
161 }
162 }
163 fail_if(err, "errors");
164 }
165 #endif
166 UNITTEST_STOP
167