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 bool space; /* space separated */
50 };
51
52 UNITTEST_START
53 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_PROXY)
54 {
55 int i;
56 int err = 0;
57 struct check list4[]= {
58 { "192.160.0.1", "192.160.0.1", 33, FALSE},
59 { "192.160.0.1", "192.160.0.1", 32, TRUE},
60 { "192.160.0.1", "192.160.0.1", 0, TRUE},
61 { "192.160.0.1", "192.160.0.1", 24, TRUE},
62 { "192.160.0.1", "192.160.0.1", 26, TRUE},
63 { "192.160.0.1", "192.160.0.1", 20, TRUE},
64 { "192.160.0.1", "192.160.0.1", 18, TRUE},
65 { "192.160.0.1", "192.160.0.1", 12, TRUE},
66 { "192.160.0.1", "192.160.0.1", 8, TRUE},
67 { "192.160.0.1", "10.0.0.1", 8, FALSE},
68 { "192.160.0.1", "10.0.0.1", 32, FALSE},
69 { "192.160.0.1", "10.0.0.1", 0, FALSE},
70 { NULL, NULL, 0, FALSE} /* end marker */
71 };
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 struct noproxy list[]= {
81 { "www.example.com", "localhost .example.com .example.de", TRUE, TRUE},
82 { "www.example.com", "localhost,.example.com,.example.de", TRUE, FALSE},
83 { "www.example.com.", "localhost,.example.com,.example.de", TRUE, FALSE},
84 { "example.com", "localhost,.example.com,.example.de", TRUE, FALSE},
85 { "example.com.", "localhost,.example.com,.example.de", TRUE, FALSE},
86 { "www.example.com", "localhost,.example.com.,.example.de", TRUE, FALSE},
87 { "www.example.com", "localhost,www.example.com.,.example.de",
88 TRUE, FALSE},
89 { "example.com", "localhost,example.com,.example.de", TRUE, FALSE},
90 { "example.com.", "localhost,example.com,.example.de", TRUE, FALSE},
91 { "nexample.com", "localhost,example.com,.example.de", FALSE, FALSE},
92 { "www.example.com", "localhost,example.com,.example.de", TRUE, FALSE},
93 { "127.0.0.1", "127.0.0.1,localhost", TRUE, FALSE},
94 { "127.0.0.1", "127.0.0.1,localhost,", TRUE, FALSE},
95 { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE, FALSE},
96 { "127.0.0.1", "127.0.0.1/28,localhost,", TRUE, FALSE},
97 { "127.0.0.1", "127.0.0.1/31,localhost,", TRUE, FALSE},
98 { "127.0.0.1", "localhost,127.0.0.1", TRUE, FALSE},
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, 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, FALSE},
105 { "localhost", "localhost,127.0.0.1", TRUE, FALSE},
106 { "localhost", "127.0.0.1,localhost", TRUE, FALSE},
107 { "foobar", "barfoo", FALSE, FALSE},
108 { "foobar", "foobar", TRUE, FALSE},
109 { "192.168.0.1", "foobar", FALSE, FALSE},
110 { "192.168.0.1", "192.168.0.0/16", TRUE, FALSE},
111 { "192.168.0.1", "192.168.0.0/24", TRUE, FALSE},
112 { "192.168.0.1", "192.168.0.0/32", FALSE, FALSE},
113 { "192.168.0.1", "192.168.0.0", FALSE, FALSE},
114 { "192.168.1.1", "192.168.0.0/24", FALSE, FALSE},
115 { "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE, FALSE},
116 { "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE, FALSE},
117 { "[::1]", "foo, bar, 192.168.0.0/16", FALSE, FALSE},
118 { "[::1]", "foo, bar, ::1/64", TRUE, FALSE},
119 { "bar", "foo, bar, ::1/64", TRUE, FALSE},
120 { "BAr", "foo, bar, ::1/64", TRUE, FALSE},
121 { "BAr", "foo,,,,, bar, ::1/64", TRUE, FALSE},
122 { "www.example.com", "foo, .example.com", TRUE, FALSE},
123 { "www.example.com", "www2.example.com, .example.net", FALSE, FALSE},
124 { "example.com", ".example.com, .example.net", TRUE, FALSE},
125 { "nonexample.com", ".example.com, .example.net", FALSE, FALSE},
126 { NULL, NULL, FALSE, FALSE}
127 };
128 for(i = 0; list4[i].a; i++) {
129 bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
130 if(match != list4[i].match) {
131 fprintf(stderr, "%s in %s/%u should %smatch\n",
132 list4[i].a, list4[i].n, list4[i].bits,
133 list4[i].match ? "": "not ");
134 err++;
135 }
136 }
137 for(i = 0; list6[i].a; i++) {
138 bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
139 if(match != list6[i].match) {
140 fprintf(stderr, "%s in %s/%u should %smatch\n",
141 list6[i].a, list6[i].n, list6[i].bits,
142 list6[i].match ? "": "not ");
143 err++;
144 }
145 }
146 for(i = 0; list[i].a; i++) {
147 bool spacesep = FALSE;
148 bool match = Curl_check_noproxy(list[i].a, list[i].n, &spacesep);
149 if(match != list[i].match) {
150 fprintf(stderr, "%s in %s should %smatch\n",
151 list[i].a, list[i].n,
152 list[i].match ? "": "not ");
153 err++;
154 }
155 if(spacesep != list[i].space) {
156 fprintf(stderr, "%s is claimed to be %sspace separated\n",
157 list[i].n, list[i].space?"":"NOT ");
158 err++;
159 }
160 }
161 fail_if(err, "errors");
162 }
163 #endif
164 UNITTEST_STOP
165