proxy/gnome: Fix up multiple ignore_hosts bugs, and add a test
[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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gio/gio.h>
22 #include <gdesktop-enums.h>
23
24 #define GNOME_PROXY_SETTINGS_SCHEMA       "org.gnome.system.proxy"
25 #define GNOME_PROXY_MODE_KEY              "mode"
26 #define GNOME_PROXY_AUTOCONFIG_URL_KEY    "autoconfig-url"
27 #define GNOME_PROXY_IGNORE_HOSTS_KEY      "ignore-hosts"
28 #define GNOME_PROXY_USE_SAME_PROXY_KEY    "use-same-proxy"
29
30 #define GNOME_PROXY_HTTP_CHILD_SCHEMA     "http"
31 #define GNOME_PROXY_HTTP_HOST_KEY         "host"
32 #define GNOME_PROXY_HTTP_PORT_KEY         "port"
33 #define GNOME_PROXY_HTTP_USE_AUTH_KEY     "use-authentication"
34 #define GNOME_PROXY_HTTP_USER_KEY         "authentication-user"
35 #define GNOME_PROXY_HTTP_PASSWORD_KEY     "authentication-password"
36
37 #define GNOME_PROXY_HTTPS_CHILD_SCHEMA    "https"
38 #define GNOME_PROXY_HTTPS_HOST_KEY        "host"
39 #define GNOME_PROXY_HTTPS_PORT_KEY        "port"
40
41 #define GNOME_PROXY_FTP_CHILD_SCHEMA      "ftp"
42 #define GNOME_PROXY_FTP_HOST_KEY          "host"
43 #define GNOME_PROXY_FTP_PORT_KEY          "port"
44
45 #define GNOME_PROXY_SOCKS_CHILD_SCHEMA    "socks"
46 #define GNOME_PROXY_SOCKS_HOST_KEY        "host"
47 #define GNOME_PROXY_SOCKS_PORT_KEY        "port"
48
49 static const char *ignore_hosts[] = {
50   ".bbb.xx",
51   "*.ccc.xx",
52   "ddd.xx",
53   "*.eee.xx:8000",
54   "127.0.0.0/24",
55   "::1",
56   "fe80::/10"
57 };
58 static const int n_ignore_hosts = G_N_ELEMENTS (ignore_hosts);
59
60 static const struct {
61   const char *uri;
62   const char *proxy;
63 } ignore_tests[] = {
64   { "http://aaa.xx/",            "http://localhost:8080" },
65   { "http://aaa.xx:8000/",       "http://localhost:8080" },
66   { "http://www.aaa.xx/",        "http://localhost:8080" },
67   { "http://www.aaa.xx:8000/",   "http://localhost:8080" },
68   { "https://aaa.xx/",           "http://localhost:8080" },
69   { "http://bbb.xx/",            "direct://" },
70   { "http://www.bbb.xx/",        "direct://" },
71   { "http://bbb.xx:8000/",       "direct://" },
72   { "http://www.bbb.xx:8000/",   "direct://" },
73   { "https://bbb.xx/",           "direct://" },
74   { "http://nobbb.xx/",          "http://localhost:8080" },
75   { "http://www.nobbb.xx/",      "http://localhost:8080" },
76   { "http://nobbb.xx:8000/",     "http://localhost:8080" },
77   { "http://www.nobbb.xx:8000/", "http://localhost:8080" },
78   { "https://nobbb.xx/",         "http://localhost:8080" },
79   { "http://ccc.xx/",            "direct://" },
80   { "http://www.ccc.xx/",        "direct://" },
81   { "http://ccc.xx:8000/",       "direct://" },
82   { "http://www.ccc.xx:8000/",   "direct://" },
83   { "https://ccc.xx/",           "direct://" },
84   { "http://ddd.xx/",            "direct://" },
85   { "http://ddd.xx:8000/",       "direct://" },
86   { "http://www.ddd.xx/",        "direct://" },
87   { "http://www.ddd.xx:8000/",   "direct://" },
88   { "https://ddd.xx/",           "direct://" },
89   { "http://eee.xx/",            "http://localhost:8080" },
90   { "http://eee.xx:8000/",       "direct://" },
91   { "http://www.eee.xx/",        "http://localhost:8080" },
92   { "http://www.eee.xx:8000/",   "direct://" },
93   { "https://eee.xx/",           "http://localhost:8080" },
94   { "http://1.2.3.4/",           "http://localhost:8080" },
95   { "http://127.0.0.1/",         "direct://" },
96   { "http://127.0.0.2/",         "direct://" },
97   { "http://127.0.0.255/",       "direct://" },
98   { "http://127.0.1.0/",         "http://localhost:8080" },
99   { "http://[::1]/",             "direct://" },
100   { "http://[::1]:80/",          "direct://" },
101   { "http://[::1:1]/",           "http://localhost:8080" },
102   { "http://[::1:1]:80/",        "http://localhost:8080" },
103   { "http://[fe80::1]/",         "direct://" },
104   { "http://[fe80::1]:80/",      "direct://" },
105   { "http://[fec0::1]/",         "http://localhost:8080" },
106   { "http://[fec0::1]:80/",      "http://localhost:8080" }
107 };
108 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
109
110 static void
111 test_proxy_ignore (void)
112 {
113   GSettings *settings, *http;
114   GProxyResolver *resolver;
115   GError *error = NULL;
116   char **proxies;
117   int i;
118
119   settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
120   g_settings_set_enum (settings, GNOME_PROXY_MODE_KEY, G_DESKTOP_PROXY_MODE_MANUAL);
121   g_settings_set (settings, GNOME_PROXY_IGNORE_HOSTS_KEY,
122                   "@as", g_variant_new_strv (ignore_hosts, n_ignore_hosts));
123
124   http = g_settings_get_child (settings, GNOME_PROXY_HTTP_CHILD_SCHEMA);
125   g_settings_set_string (http, GNOME_PROXY_HTTP_HOST_KEY, "localhost");
126   g_settings_set_int (http, GNOME_PROXY_HTTP_PORT_KEY, 8080);
127
128   resolver = g_proxy_resolver_get_default ();
129
130   for (i = 0; i < n_ignore_tests; i++)
131     {
132       proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
133                                          NULL, &error);
134       g_assert_no_error (error);
135
136       g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
137       g_strfreev (proxies);
138     }
139 }
140
141 int
142 main (int   argc,
143       char *argv[])
144 {
145   g_type_init ();
146   g_test_init (&argc, &argv, NULL);
147
148   g_setenv ("GIO_EXTRA_MODULES", TOP_BUILDDIR "/proxy/gnome/.libs", TRUE);
149   g_setenv ("GIO_USE_PROXY_RESOLVER", "gnome", TRUE);
150   g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
151
152   g_test_add_func ("/proxy/gnome/ignore", test_proxy_ignore);
153
154   return g_test_run();
155 }