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