1 | /* GStaticProxyResolver tests |
2 | * |
3 | * Copyright 2011, 2013 Red Hat, Inc. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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/>. |
18 | */ |
19 | |
20 | #include <gio/gio.h> |
21 | |
22 | static void |
23 | async_result_cb (GObject *obj, |
24 | GAsyncResult *result, |
25 | gpointer user_data) |
26 | { |
27 | GAsyncResult **result_out = user_data; |
28 | *result_out = g_object_ref (result); |
29 | } |
30 | |
31 | static void |
32 | test_uris (void) |
33 | { |
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; |
41 | |
42 | /* Valid URI. */ |
43 | uri = "http://%E0%B4%A8%E0%B4%B2:80/" ; |
44 | resolver = g_simple_proxy_resolver_new (NULL, ignore_hosts: (char **) ignore_hosts); |
45 | |
46 | proxies = g_proxy_resolver_lookup (resolver, uri, NULL, error: &error); |
47 | g_assert_no_error (error); |
48 | g_strfreev (str_array: proxies); |
49 | |
50 | g_proxy_resolver_lookup_async (resolver, uri, NULL, callback: async_result_cb, user_data: &result); |
51 | while (result == NULL) |
52 | g_main_context_iteration (NULL, TRUE); |
53 | proxies = g_proxy_resolver_lookup_finish (resolver, result, error: &error); |
54 | g_assert_no_error (error); |
55 | g_strfreev (str_array: proxies); |
56 | g_clear_object (&result); |
57 | |
58 | g_object_unref (object: resolver); |
59 | |
60 | /* Invalid URI. */ |
61 | uri = "%E0%B4%A8%E0%B4%B2" ; |
62 | str = g_strdup_printf (format: "Invalid URI ā%sā" , uri); |
63 | resolver = g_simple_proxy_resolver_new (NULL, ignore_hosts: (char **) ignore_hosts); |
64 | |
65 | proxies = g_proxy_resolver_lookup (resolver, uri, NULL, error: &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 (err: &error); |
69 | g_assert_null (proxies); |
70 | g_clear_object (&result); |
71 | |
72 | g_proxy_resolver_lookup_async (resolver, uri, NULL, callback: async_result_cb, user_data: &result); |
73 | while (result == NULL) |
74 | g_main_context_iteration (NULL, TRUE); |
75 | proxies = g_proxy_resolver_lookup_finish (resolver, result, error: &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 (err: &error); |
79 | g_assert_null (proxies); |
80 | g_object_unref (object: result); |
81 | |
82 | g_object_unref (object: resolver); |
83 | g_free (mem: str); |
84 | |
85 | resolver = g_simple_proxy_resolver_new (default_proxy: "default://" , ignore_hosts: (char **) ignore_hosts); |
86 | g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver), |
87 | uri_scheme: "http" , proxy: "http://proxy.example.com" ); |
88 | g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver), |
89 | uri_scheme: "ftp" , proxy: "ftp://proxy.example.com" ); |
90 | |
91 | proxies = g_proxy_resolver_lookup (resolver, uri: "http://one.example.com/" , |
92 | NULL, error: &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 (str_array: proxies); |
97 | |
98 | proxies = g_proxy_resolver_lookup (resolver, uri: "HTTP://uppercase.example.com/" , |
99 | NULL, error: &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 (str_array: proxies); |
104 | |
105 | proxies = g_proxy_resolver_lookup (resolver, uri: "htt://missing-letter.example.com/" , |
106 | NULL, error: &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 (str_array: proxies); |
111 | |
112 | proxies = g_proxy_resolver_lookup (resolver, uri: "https://extra-letter.example.com/" , |
113 | NULL, error: &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 (str_array: proxies); |
118 | |
119 | proxies = g_proxy_resolver_lookup (resolver, uri: "ftp://five.example.com/" , |
120 | NULL, error: &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 (str_array: proxies); |
125 | |
126 | proxies = g_proxy_resolver_lookup (resolver, uri: "http://127.0.0.1/" , |
127 | NULL, error: &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 (str_array: proxies); |
132 | |
133 | g_object_unref (object: resolver); |
134 | } |
135 | |
136 | static void |
137 | test_socks (void) |
138 | { |
139 | GProxyResolver *resolver; |
140 | const gchar *ignore_hosts[2] = { "127.0.0.1" , NULL }; |
141 | gchar **proxies; |
142 | GError *error = NULL; |
143 | |
144 | resolver = g_simple_proxy_resolver_new (default_proxy: "socks://proxy.example.com" , ignore_hosts: (char **) ignore_hosts); |
145 | |
146 | proxies = g_proxy_resolver_lookup (resolver, uri: "http://one.example.com/" , |
147 | NULL, error: &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 (str_array: proxies); |
154 | |
155 | proxies = g_proxy_resolver_lookup (resolver, uri: "http://127.0.0.1/" , |
156 | NULL, error: &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 (str_array: proxies); |
161 | |
162 | g_object_unref (object: resolver); |
163 | |
164 | resolver = g_simple_proxy_resolver_new (default_proxy: "default-proxy://" , ignore_hosts: (char **) ignore_hosts); |
165 | g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver), |
166 | uri_scheme: "http" , proxy: "socks://proxy.example.com" ); |
167 | |
168 | proxies = g_proxy_resolver_lookup (resolver, uri: "http://one.example.com/" , |
169 | NULL, error: &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 (str_array: proxies); |
176 | |
177 | proxies = g_proxy_resolver_lookup (resolver, uri: "ftp://two.example.com/" , |
178 | NULL, error: &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 (str_array: proxies); |
183 | |
184 | proxies = g_proxy_resolver_lookup (resolver, uri: "http://127.0.0.1/" , |
185 | NULL, error: &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 (str_array: proxies); |
190 | |
191 | g_object_unref (object: resolver); |
192 | } |
193 | |
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 |
204 | }; |
205 | |
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" } |
255 | }; |
256 | static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests); |
257 | |
258 | static void |
259 | test_ignore (void) |
260 | { |
261 | GProxyResolver *resolver; |
262 | GError *error = NULL; |
263 | char **proxies; |
264 | int i; |
265 | |
266 | resolver = g_simple_proxy_resolver_new (default_proxy: "http://localhost:8080" , |
267 | ignore_hosts: (char **)ignore_hosts); |
268 | |
269 | for (i = 0; i < n_ignore_tests; i++) |
270 | { |
271 | proxies = g_proxy_resolver_lookup (resolver, uri: ignore_tests[i].uri, |
272 | NULL, error: &error); |
273 | g_assert_no_error (error); |
274 | |
275 | g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy); |
276 | g_strfreev (str_array: proxies); |
277 | } |
278 | |
279 | g_object_unref (object: resolver); |
280 | } |
281 | |
282 | int |
283 | main (int argc, |
284 | char *argv[]) |
285 | { |
286 | g_test_init (argc: &argc, argv: &argv, NULL); |
287 | |
288 | g_test_add_func (testpath: "/static-proxy/uri" , test_func: test_uris); |
289 | g_test_add_func (testpath: "/static-proxy/socks" , test_func: test_socks); |
290 | g_test_add_func (testpath: "/static-proxy/ignore" , test_func: test_ignore); |
291 | |
292 | return g_test_run(); |
293 | } |
294 | |