1 /* GStaticProxyResolver tests
3 * Copyright 2011, 2013 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
25 async_result_cb (GObject *obj,
29 GAsyncResult **result_out = user_data;
30 *result_out = g_object_ref (result);
36 GProxyResolver *resolver;
37 const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
42 GAsyncResult *result = NULL;
45 uri = "http://%E0%B4%A8%E0%B4%B2:80/";
46 resolver = g_simple_proxy_resolver_new (NULL, (char **) ignore_hosts);
48 proxies = g_proxy_resolver_lookup (resolver, uri, NULL, &error);
49 g_assert_no_error (error);
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);
58 g_clear_object (&result);
60 g_object_unref (resolver);
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);
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);
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);
84 g_object_unref (resolver);
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");
93 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
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");
100 proxies = g_proxy_resolver_lookup (resolver, "HTTP://uppercase.example.com/",
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);
107 proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
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);
114 proxies = g_proxy_resolver_lookup (resolver, "https://extra-letter.example.com/",
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);
121 proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
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);
128 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
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);
135 g_object_unref (resolver);
141 GProxyResolver *resolver;
142 const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
144 GError *error = NULL;
146 resolver = g_simple_proxy_resolver_new ("socks://proxy.example.com", (char **) ignore_hosts);
148 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
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);
157 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
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);
164 g_object_unref (resolver);
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");
170 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
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);
179 proxies = g_proxy_resolver_lookup (resolver, "ftp://two.example.com/",
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);
186 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
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);
193 g_object_unref (resolver);
196 static const char *ignore_hosts[] = {
208 static const struct {
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" }
258 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
263 GProxyResolver *resolver;
264 GError *error = NULL;
268 resolver = g_simple_proxy_resolver_new ("http://localhost:8080",
269 (char **)ignore_hosts);
271 for (i = 0; i < n_ignore_tests; i++)
273 proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
275 g_assert_no_error (error);
277 g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
278 g_strfreev (proxies);
281 g_object_unref (resolver);
288 g_test_init (&argc, &argv, NULL);
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);