1 /* GProxyResolverGnome tests
3 * Copyright 2011 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 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/>.
21 #include <gdesktop-enums.h>
23 #define GNOME_PROXY_SETTINGS_SCHEMA "org.gnome.system.proxy"
24 #define GNOME_PROXY_MODE_KEY "mode"
25 #define GNOME_PROXY_AUTOCONFIG_URL_KEY "autoconfig-url"
26 #define GNOME_PROXY_IGNORE_HOSTS_KEY "ignore-hosts"
27 #define GNOME_PROXY_USE_SAME_PROXY_KEY "use-same-proxy"
29 #define GNOME_PROXY_HTTP_CHILD_SCHEMA "http"
30 #define GNOME_PROXY_HTTP_HOST_KEY "host"
31 #define GNOME_PROXY_HTTP_PORT_KEY "port"
32 #define GNOME_PROXY_HTTP_USE_AUTH_KEY "use-authentication"
33 #define GNOME_PROXY_HTTP_USER_KEY "authentication-user"
34 #define GNOME_PROXY_HTTP_PASSWORD_KEY "authentication-password"
36 #define GNOME_PROXY_HTTPS_CHILD_SCHEMA "https"
37 #define GNOME_PROXY_HTTPS_HOST_KEY "host"
38 #define GNOME_PROXY_HTTPS_PORT_KEY "port"
40 #define GNOME_PROXY_FTP_CHILD_SCHEMA "ftp"
41 #define GNOME_PROXY_FTP_HOST_KEY "host"
42 #define GNOME_PROXY_FTP_PORT_KEY "port"
44 #define GNOME_PROXY_SOCKS_CHILD_SCHEMA "socks"
45 #define GNOME_PROXY_SOCKS_HOST_KEY "host"
46 #define GNOME_PROXY_SOCKS_PORT_KEY "port"
49 reset_proxy_settings (gpointer fixture,
50 gconstpointer user_data)
52 GSettings *settings, *child;
54 settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
55 g_settings_reset (settings, GNOME_PROXY_MODE_KEY);
56 g_settings_reset (settings, GNOME_PROXY_USE_SAME_PROXY_KEY);
58 child = g_settings_get_child (settings, GNOME_PROXY_HTTP_CHILD_SCHEMA);
59 g_settings_reset (child, GNOME_PROXY_HTTP_HOST_KEY);
60 g_settings_reset (child, GNOME_PROXY_HTTP_PORT_KEY);
61 g_object_unref (child);
63 child = g_settings_get_child (settings, GNOME_PROXY_HTTPS_CHILD_SCHEMA);
64 g_settings_reset (child, GNOME_PROXY_HTTPS_HOST_KEY);
65 g_settings_reset (child, GNOME_PROXY_HTTPS_PORT_KEY);
66 g_object_unref (child);
68 child = g_settings_get_child (settings, GNOME_PROXY_FTP_CHILD_SCHEMA);
69 g_settings_reset (child, GNOME_PROXY_FTP_HOST_KEY);
70 g_settings_reset (child, GNOME_PROXY_FTP_PORT_KEY);
71 g_object_unref (child);
73 child = g_settings_get_child (settings, GNOME_PROXY_SOCKS_CHILD_SCHEMA);
74 g_settings_reset (child, GNOME_PROXY_SOCKS_HOST_KEY);
75 g_settings_reset (child, GNOME_PROXY_SOCKS_PORT_KEY);
76 g_object_unref (child);
78 g_object_unref (settings);
82 test_proxy_uri (gpointer fixture,
83 gconstpointer user_data)
85 GSettings *settings, *child;
86 GProxyResolver *resolver;
90 settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
91 g_settings_set_enum (settings, GNOME_PROXY_MODE_KEY, G_DESKTOP_PROXY_MODE_MANUAL);
92 g_settings_set_boolean (settings, GNOME_PROXY_USE_SAME_PROXY_KEY, TRUE);
94 child = g_settings_get_child (settings, GNOME_PROXY_HTTP_CHILD_SCHEMA);
95 g_settings_set_string (child, GNOME_PROXY_HTTP_HOST_KEY, "proxy.example.com");
96 g_settings_set_int (child, GNOME_PROXY_HTTP_PORT_KEY, 8080);
97 g_object_unref (child);
99 child = g_settings_get_child (settings, GNOME_PROXY_HTTPS_CHILD_SCHEMA);
100 g_settings_set_string (child, GNOME_PROXY_HTTPS_HOST_KEY, "proxy-s.example.com");
101 g_settings_set_int (child, GNOME_PROXY_HTTPS_PORT_KEY, 7070);
102 g_object_unref (child);
104 child = g_settings_get_child (settings, GNOME_PROXY_FTP_CHILD_SCHEMA);
105 g_settings_set_string (child, GNOME_PROXY_FTP_HOST_KEY, "proxy-f.example.com");
106 g_settings_set_int (child, GNOME_PROXY_FTP_PORT_KEY, 6060);
107 g_object_unref (child);
109 g_object_unref (settings);
111 resolver = g_proxy_resolver_get_default ();
113 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
115 g_assert_no_error (error);
116 g_assert_cmpint (g_strv_length (proxies), ==, 1);
117 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080");
118 g_strfreev (proxies);
120 proxies = g_proxy_resolver_lookup (resolver, "HTTPS://uppercase.example.com/",
122 g_assert_no_error (error);
123 g_assert_cmpint (g_strv_length (proxies), ==, 1);
124 g_assert_cmpstr (proxies[0], ==, "http://proxy-s.example.com:7070");
125 g_strfreev (proxies);
127 /* Because we set use_same_proxy = TRUE, unknown protocols will use
128 * the http proxy by default.
130 proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
132 g_assert_no_error (error);
133 g_assert_cmpint (g_strv_length (proxies), ==, 1);
134 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080");
135 g_strfreev (proxies);
137 proxies = g_proxy_resolver_lookup (resolver, "ftps://extra-letter.example.com/",
139 g_assert_no_error (error);
140 g_assert_cmpint (g_strv_length (proxies), ==, 1);
141 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080");
142 g_strfreev (proxies);
144 proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
146 g_assert_no_error (error);
147 g_assert_cmpint (g_strv_length (proxies), ==, 1);
148 g_assert_cmpstr (proxies[0], ==, "ftp://proxy-f.example.com:6060");
149 g_strfreev (proxies);
153 test_proxy_socks (gpointer fixture,
154 gconstpointer user_data)
156 GSettings *settings, *child;
157 GProxyResolver *resolver;
158 const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
160 GError *error = NULL;
162 settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
163 g_settings_set_enum (settings, GNOME_PROXY_MODE_KEY, G_DESKTOP_PROXY_MODE_MANUAL);
164 g_settings_set (settings, GNOME_PROXY_IGNORE_HOSTS_KEY,
165 "@as", g_variant_new_strv (ignore_hosts, -1));
167 child = g_settings_get_child (settings, GNOME_PROXY_SOCKS_CHILD_SCHEMA);
168 g_settings_set_string (child, GNOME_PROXY_SOCKS_HOST_KEY, "proxy.example.com");
169 g_settings_set_int (child, GNOME_PROXY_SOCKS_PORT_KEY, 1234);
170 g_object_unref (child);
171 g_object_unref (settings);
173 resolver = g_proxy_resolver_get_default ();
175 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
177 g_assert_no_error (error);
178 g_assert_cmpint (g_strv_length (proxies), ==, 3);
179 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com:1234");
180 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com:1234");
181 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com:1234");
182 g_strfreev (proxies);
184 proxies = g_proxy_resolver_lookup (resolver, "wednesday://two.example.com/",
186 g_assert_no_error (error);
187 g_assert_cmpint (g_strv_length (proxies), ==, 3);
188 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com:1234");
189 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com:1234");
190 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com:1234");
191 g_strfreev (proxies);
193 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
195 g_assert_no_error (error);
196 g_assert_cmpint (g_strv_length (proxies), ==, 1);
197 g_assert_cmpstr (proxies[0], ==, "direct://");
198 g_strfreev (proxies);
201 static const char *ignore_hosts[] = {
211 static const int n_ignore_hosts = G_N_ELEMENTS (ignore_hosts);
213 static const struct {
217 { "http://aaa.xx/", "http://localhost:8080" },
218 { "http://aaa.xx:8000/", "http://localhost:8080" },
219 { "http://www.aaa.xx/", "http://localhost:8080" },
220 { "http://www.aaa.xx:8000/", "http://localhost:8080" },
221 { "https://aaa.xx/", "http://localhost:8080" },
222 { "http://bbb.xx/", "direct://" },
223 { "http://www.bbb.xx/", "direct://" },
224 { "http://bbb.xx:8000/", "direct://" },
225 { "http://www.bbb.xx:8000/", "direct://" },
226 { "https://bbb.xx/", "direct://" },
227 { "http://nobbb.xx/", "http://localhost:8080" },
228 { "http://www.nobbb.xx/", "http://localhost:8080" },
229 { "http://nobbb.xx:8000/", "http://localhost:8080" },
230 { "http://www.nobbb.xx:8000/", "http://localhost:8080" },
231 { "https://nobbb.xx/", "http://localhost:8080" },
232 { "http://ccc.xx/", "direct://" },
233 { "http://www.ccc.xx/", "direct://" },
234 { "http://ccc.xx:8000/", "direct://" },
235 { "http://www.ccc.xx:8000/", "direct://" },
236 { "https://ccc.xx/", "direct://" },
237 { "http://ddd.xx/", "direct://" },
238 { "http://ddd.xx:8000/", "direct://" },
239 { "http://www.ddd.xx/", "direct://" },
240 { "http://www.ddd.xx:8000/", "direct://" },
241 { "https://ddd.xx/", "direct://" },
242 { "http://eee.xx/", "http://localhost:8080" },
243 { "http://eee.xx:8000/", "direct://" },
244 { "http://www.eee.xx/", "http://localhost:8080" },
245 { "http://www.eee.xx:8000/", "direct://" },
246 { "https://eee.xx/", "http://localhost:8080" },
247 { "http://1.2.3.4/", "http://localhost:8080" },
248 { "http://127.0.0.1/", "direct://" },
249 { "http://127.0.0.2/", "direct://" },
250 { "http://127.0.0.255/", "direct://" },
251 { "http://127.0.1.0/", "http://localhost:8080" },
252 { "http://10.0.0.1/", "http://localhost:8080" },
253 { "http://10.0.0.1:8000/", "direct://" },
254 { "http://[::1]/", "direct://" },
255 { "http://[::1]:80/", "direct://" },
256 { "http://[::1:1]/", "http://localhost:8080" },
257 { "http://[::1:1]:80/", "http://localhost:8080" },
258 { "http://[fe80::1]/", "direct://" },
259 { "http://[fe80::1]:80/", "direct://" },
260 { "http://[fec0::1]/", "http://localhost:8080" },
261 { "http://[fec0::1]:80/", "http://localhost:8080" }
263 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
266 test_proxy_ignore (gpointer fixture,
267 gconstpointer user_data)
269 GSettings *settings, *http;
270 GProxyResolver *resolver;
271 GError *error = NULL;
275 settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
276 g_settings_set_enum (settings, GNOME_PROXY_MODE_KEY, G_DESKTOP_PROXY_MODE_MANUAL);
277 g_settings_set (settings, GNOME_PROXY_IGNORE_HOSTS_KEY,
278 "@as", g_variant_new_strv (ignore_hosts, n_ignore_hosts));
280 http = g_settings_get_child (settings, GNOME_PROXY_HTTP_CHILD_SCHEMA);
281 g_settings_set_string (http, GNOME_PROXY_HTTP_HOST_KEY, "localhost");
282 g_settings_set_int (http, GNOME_PROXY_HTTP_PORT_KEY, 8080);
284 g_object_unref (http);
285 g_object_unref (settings);
287 resolver = g_proxy_resolver_get_default ();
289 for (i = 0; i < n_ignore_tests; i++)
291 proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
293 g_assert_no_error (error);
295 g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
296 g_strfreev (proxies);
304 g_test_init (&argc, &argv, NULL);
306 g_setenv ("GIO_EXTRA_MODULES", TOP_BUILDDIR "/proxy/gnome/.libs", TRUE);
307 g_setenv ("GIO_USE_PROXY_RESOLVER", "gnome", TRUE);
308 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
309 g_setenv ("DESKTOP_SESSION", "gnome", TRUE);
311 g_test_add_vtable ("/proxy/gnome/uri", 0, NULL,
312 reset_proxy_settings, test_proxy_uri, NULL);
313 g_test_add_vtable ("/proxy/gnome/socks", 0, NULL,
314 reset_proxy_settings, test_proxy_socks, NULL);
315 g_test_add_vtable ("/proxy/gnome/ignore", 0, NULL,
316 reset_proxy_settings, test_proxy_ignore, NULL);