1 /* GStaticProxyResolver tests
2 *
3 * Copyright 2011, 2013 Red Hat, Inc.
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
22 #include <gio/gio.h>
23
24 static void
25 async_result_cb (GObject *obj,
26 GAsyncResult *result,
27 gpointer user_data)
28 {
29 GAsyncResult **result_out = user_data;
30 *result_out = g_object_ref (result);
31 }
32
33 static void
34 test_uris (void)
35 {
36 GProxyResolver *resolver;
37 const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
38 gchar **proxies;
39 GError *error = NULL;
40 const gchar *uri;
41 gchar *str = NULL;
42 GAsyncResult *result = NULL;
43
44 /* Valid URI. */
45 uri = "http://%E0%B4%A8%E0%B4%B2:80/";
46 resolver = g_simple_proxy_resolver_new (NULL, (char **) ignore_hosts);
47
48 proxies = g_proxy_resolver_lookup (resolver, uri, NULL, &error);
49 g_assert_no_error (error);
50 g_strfreev (proxies);
51
52 g_proxy_resolver_lookup_async (resolver, uri, NULL, async_result_cb, &result);
53 while (result == NULL)
54 g_main_context_iteration (NULL, TRUE);
55 proxies = g_proxy_resolver_lookup_finish (resolver, result, &error);
56 g_assert_no_error (error);
57 g_strfreev (proxies);
58 g_clear_object (&result);
59
60 g_object_unref (resolver);
61
62 /* Invalid URI. */
63 uri = "%E0%B4%A8%E0%B4%B2";
64 str = g_strdup_printf ("Invalid URI ‘%s’", uri);
65 resolver = g_simple_proxy_resolver_new (NULL, (char **) ignore_hosts);
66
67 proxies = g_proxy_resolver_lookup (resolver, uri, NULL, &error);
68 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
69 g_assert_cmpstr (error->message, ==, str);
70 g_clear_error (&error);
71 g_assert_null (proxies);
72 g_clear_object (&result);
73
74 g_proxy_resolver_lookup_async (resolver, uri, NULL, async_result_cb, &result);
75 while (result == NULL)
76 g_main_context_iteration (NULL, TRUE);
77 proxies = g_proxy_resolver_lookup_finish (resolver, result, &error);
78 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
79 g_assert_cmpstr (error->message, ==, str);
80 g_clear_error (&error);
81 g_assert_null (proxies);
82 g_object_unref (result);
83
84 g_object_unref (resolver);
85 g_free (str);
86
87 resolver = g_simple_proxy_resolver_new ("default://", (char **) ignore_hosts);
88 g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
89 "http", "http://proxy.example.com");
90 g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
91 "ftp", "ftp://proxy.example.com");
92
93 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
94 NULL, &error);
95 g_assert_no_error (error);
96 g_assert_cmpint (g_strv_length (proxies), ==, 1);
97 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com");
98 g_strfreev (proxies);
99
100 proxies = g_proxy_resolver_lookup (resolver, "HTTP://uppercase.example.com/",
101 NULL, &error);
102 g_assert_no_error (error);
103 g_assert_cmpint (g_strv_length (proxies), ==, 1);
104 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com");
105 g_strfreev (proxies);
106
107 proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
108 NULL, &error);
109 g_assert_no_error (error);
110 g_assert_cmpint (g_strv_length (proxies), ==, 1);
111 g_assert_cmpstr (proxies[0], ==, "default://");
112 g_strfreev (proxies);
113
114 proxies = g_proxy_resolver_lookup (resolver, "https://extra-letter.example.com/",
115 NULL, &error);
116 g_assert_no_error (error);
117 g_assert_cmpint (g_strv_length (proxies), ==, 1);
118 g_assert_cmpstr (proxies[0], ==, "default://");
119 g_strfreev (proxies);
120
121 proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
122 NULL, &error);
123 g_assert_no_error (error);
124 g_assert_cmpint (g_strv_length (proxies), ==, 1);
125 g_assert_cmpstr (proxies[0], ==, "ftp://proxy.example.com");
126 g_strfreev (proxies);
127
128 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
129 NULL, &error);
130 g_assert_no_error (error);
131 g_assert_cmpint (g_strv_length (proxies), ==, 1);
132 g_assert_cmpstr (proxies[0], ==, "direct://");
133 g_strfreev (proxies);
134
135 g_object_unref (resolver);
136 }
137
138 static void
139 test_socks (void)
140 {
141 GProxyResolver *resolver;
142 const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
143 gchar **proxies;
144 GError *error = NULL;
145
146 resolver = g_simple_proxy_resolver_new ("socks://proxy.example.com", (char **) ignore_hosts);
147
148 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
149 NULL, &error);
150 g_assert_no_error (error);
151 g_assert_cmpint (g_strv_length (proxies), ==, 3);
152 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com");
153 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com");
154 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com");
155 g_strfreev (proxies);
156
157 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
158 NULL, &error);
159 g_assert_no_error (error);
160 g_assert_cmpint (g_strv_length (proxies), ==, 1);
161 g_assert_cmpstr (proxies[0], ==, "direct://");
162 g_strfreev (proxies);
163
164 g_object_unref (resolver);
165
166 resolver = g_simple_proxy_resolver_new ("default-proxy://", (char **) ignore_hosts);
167 g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
168 "http", "socks://proxy.example.com");
169
170 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
171 NULL, &error);
172 g_assert_no_error (error);
173 g_assert_cmpint (g_strv_length (proxies), ==, 3);
174 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com");
175 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com");
176 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com");
177 g_strfreev (proxies);
178
179 proxies = g_proxy_resolver_lookup (resolver, "ftp://two.example.com/",
180 NULL, &error);
181 g_assert_no_error (error);
182 g_assert_cmpint (g_strv_length (proxies), ==, 1);
183 g_assert_cmpstr (proxies[0], ==, "default-proxy://");
184 g_strfreev (proxies);
185
186 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
187 NULL, &error);
188 g_assert_no_error (error);
189 g_assert_cmpint (g_strv_length (proxies), ==, 1);
190 g_assert_cmpstr (proxies[0], ==, "direct://");
191 g_strfreev (proxies);
192
193 g_object_unref (resolver);
194 }
195
196 static const char *ignore_hosts[] = {
197 ".bbb.xx",
198 "*.ccc.xx",
199 "ddd.xx",
200 "*.eee.xx:8000",
201 "127.0.0.0/24",
202 "10.0.0.1:8000",
203 "::1",
204 "fe80::/10",
205 NULL
206 };
207
208 static const struct {
209 const char *uri;
210 const char *proxy;
211 } ignore_tests[] = {
212 { "http://aaa.xx/", "http://localhost:8080" },
213 { "http://aaa.xx:8000/", "http://localhost:8080" },
214 { "http://www.aaa.xx/", "http://localhost:8080" },
215 { "http://www.aaa.xx:8000/", "http://localhost:8080" },
216 { "https://aaa.xx/", "http://localhost:8080" },
217 { "http://bbb.xx/", "direct://" },
218 { "http://www.bbb.xx/", "direct://" },
219 { "http://bbb.xx:8000/", "direct://" },
220 { "http://www.bbb.xx:8000/", "direct://" },
221 { "https://bbb.xx/", "direct://" },
222 { "http://nobbb.xx/", "http://localhost:8080" },
223 { "http://www.nobbb.xx/", "http://localhost:8080" },
224 { "http://nobbb.xx:8000/", "http://localhost:8080" },
225 { "http://www.nobbb.xx:8000/", "http://localhost:8080" },
226 { "https://nobbb.xx/", "http://localhost:8080" },
227 { "http://ccc.xx/", "direct://" },
228 { "http://www.ccc.xx/", "direct://" },
229 { "http://ccc.xx:8000/", "direct://" },
230 { "http://www.ccc.xx:8000/", "direct://" },
231 { "https://ccc.xx/", "direct://" },
232 { "http://ddd.xx/", "direct://" },
233 { "http://ddd.xx:8000/", "direct://" },
234 { "http://www.ddd.xx/", "direct://" },
235 { "http://www.ddd.xx:8000/", "direct://" },
236 { "https://ddd.xx/", "direct://" },
237 { "http://eee.xx/", "http://localhost:8080" },
238 { "http://eee.xx:8000/", "direct://" },
239 { "http://www.eee.xx/", "http://localhost:8080" },
240 { "http://www.eee.xx:8000/", "direct://" },
241 { "https://eee.xx/", "http://localhost:8080" },
242 { "http://1.2.3.4/", "http://localhost:8080" },
243 { "http://127.0.0.1/", "direct://" },
244 { "http://127.0.0.2/", "direct://" },
245 { "http://127.0.0.255/", "direct://" },
246 { "http://127.0.1.0/", "http://localhost:8080" },
247 { "http://10.0.0.1/", "http://localhost:8080" },
248 { "http://10.0.0.1:8000/", "direct://" },
249 { "http://[::1]/", "direct://" },
250 { "http://[::1]:80/", "direct://" },
251 { "http://[::1:1]/", "http://localhost:8080" },
252 { "http://[::1:1]:80/", "http://localhost:8080" },
253 { "http://[fe80::1]/", "direct://" },
254 { "http://[fe80::1]:80/", "direct://" },
255 { "http://[fec0::1]/", "http://localhost:8080" },
256 { "http://[fec0::1]:80/", "http://localhost:8080" }
257 };
258 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
259
260 static void
261 test_ignore (void)
262 {
263 GProxyResolver *resolver;
264 GError *error = NULL;
265 char **proxies;
266 int i;
267
268 resolver = g_simple_proxy_resolver_new ("http://localhost:8080",
269 (char **)ignore_hosts);
270
271 for (i = 0; i < n_ignore_tests; i++)
272 {
273 proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
274 NULL, &error);
275 g_assert_no_error (error);
276
277 g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
278 g_strfreev (proxies);
279 }
280
281 g_object_unref (resolver);
282 }
283
284 int
285 main (int argc,
286 char *argv[])
287 {
288 g_test_init (&argc, &argv, NULL);
289
290 g_test_add_func ("/static-proxy/uri", test_uris);
291 g_test_add_func ("/static-proxy/socks", test_socks);
292 g_test_add_func ("/static-proxy/ignore", test_ignore);
293
294 return g_test_run();
295 }