proxy/gnome: port to GSimpleProxyResolver
[platform/upstream/glib-networking.git] / proxy / tests / gnome.c
1 /* GProxyResolverGnome tests
2  *
3  * Copyright 2011 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 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 #include <gdesktop-enums.h>
22
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"
28
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"
35
36 #define GNOME_PROXY_HTTPS_CHILD_SCHEMA    "https"
37 #define GNOME_PROXY_HTTPS_HOST_KEY        "host"
38 #define GNOME_PROXY_HTTPS_PORT_KEY        "port"
39
40 #define GNOME_PROXY_FTP_CHILD_SCHEMA      "ftp"
41 #define GNOME_PROXY_FTP_HOST_KEY          "host"
42 #define GNOME_PROXY_FTP_PORT_KEY          "port"
43
44 #define GNOME_PROXY_SOCKS_CHILD_SCHEMA    "socks"
45 #define GNOME_PROXY_SOCKS_HOST_KEY        "host"
46 #define GNOME_PROXY_SOCKS_PORT_KEY        "port"
47
48 static void
49 reset_proxy_settings (gpointer      fixture,
50                       gconstpointer user_data)
51 {
52   GSettings *settings, *child;
53
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);
57
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);
62
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);
67
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);
72
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);
77
78   g_object_unref (settings);
79 }
80
81 static void
82 test_proxy_uri (gpointer      fixture,
83                 gconstpointer user_data)
84 {
85   GSettings *settings, *child;
86   GProxyResolver *resolver;
87   gchar **proxies;
88   GError *error = NULL;
89
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);
93
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);
98
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);
103
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);
108
109   g_object_unref (settings);
110
111   resolver = g_proxy_resolver_get_default ();
112
113   proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
114                                      NULL, &error);
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);
119
120   proxies = g_proxy_resolver_lookup (resolver, "HTTPS://uppercase.example.com/",
121                                      NULL, &error);
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);
126
127   /* Because we set use_same_proxy = TRUE, unknown protocols will use
128    * the http proxy by default.
129    */
130   proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
131                                      NULL, &error);
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);
136
137   proxies = g_proxy_resolver_lookup (resolver, "ftps://extra-letter.example.com/",
138                                      NULL, &error);
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);
143
144   proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
145                                      NULL, &error);
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);
150 }
151
152 static void
153 test_proxy_socks (gpointer      fixture,
154                   gconstpointer user_data)
155 {
156   GSettings *settings, *child;
157   GProxyResolver *resolver;
158   const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
159   gchar **proxies;
160   GError *error = NULL;
161
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));
166
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);
172
173   resolver = g_proxy_resolver_get_default ();
174
175   proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
176                                      NULL, &error);
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);
183
184   proxies = g_proxy_resolver_lookup (resolver, "wednesday://two.example.com/",
185                                      NULL, &error);
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);
192
193   proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
194                                      NULL, &error);
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);
199 }
200
201 static const char *ignore_hosts[] = {
202   ".bbb.xx",
203   "*.ccc.xx",
204   "ddd.xx",
205   "*.eee.xx:8000",
206   "127.0.0.0/24",
207   "10.0.0.1:8000",
208   "::1",
209   "fe80::/10"
210 };
211 static const int n_ignore_hosts = G_N_ELEMENTS (ignore_hosts);
212
213 static const struct {
214   const char *uri;
215   const char *proxy;
216 } ignore_tests[] = {
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" }
262 };
263 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
264
265 static void
266 test_proxy_ignore (gpointer      fixture,
267                    gconstpointer user_data)
268 {
269   GSettings *settings, *http;
270   GProxyResolver *resolver;
271   GError *error = NULL;
272   char **proxies;
273   int i;
274
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));
279
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);
283
284   g_object_unref (http);
285   g_object_unref (settings);
286
287   resolver = g_proxy_resolver_get_default ();
288
289   for (i = 0; i < n_ignore_tests; i++)
290     {
291       proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
292                                          NULL, &error);
293       g_assert_no_error (error);
294
295       g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
296       g_strfreev (proxies);
297     }
298 }
299
300 int
301 main (int   argc,
302       char *argv[])
303 {
304   g_test_init (&argc, &argv, NULL);
305
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);
310
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);
317
318   return g_test_run();
319 }