Add some more cases to the app-id unit tests
[glib.git] / gio / tests / simple-proxy.c
blobfaf1cd638989883f6a096d13ef453c20ebc9a87d
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 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 test_uris (void)
25 GProxyResolver *resolver;
26 gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
27 gchar **proxies;
28 GError *error = NULL;
30 resolver = g_simple_proxy_resolver_new ("default://", ignore_hosts);
31 g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
32 "http", "http://proxy.example.com");
33 g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
34 "ftp", "ftp://proxy.example.com");
36 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
37 NULL, &error);
38 g_assert_no_error (error);
39 g_assert_cmpint (g_strv_length (proxies), ==, 1);
40 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com");
41 g_strfreev (proxies);
43 proxies = g_proxy_resolver_lookup (resolver, "HTTP://uppercase.example.com/",
44 NULL, &error);
45 g_assert_no_error (error);
46 g_assert_cmpint (g_strv_length (proxies), ==, 1);
47 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com");
48 g_strfreev (proxies);
50 proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
51 NULL, &error);
52 g_assert_no_error (error);
53 g_assert_cmpint (g_strv_length (proxies), ==, 1);
54 g_assert_cmpstr (proxies[0], ==, "default://");
55 g_strfreev (proxies);
57 proxies = g_proxy_resolver_lookup (resolver, "https://extra-letter.example.com/",
58 NULL, &error);
59 g_assert_no_error (error);
60 g_assert_cmpint (g_strv_length (proxies), ==, 1);
61 g_assert_cmpstr (proxies[0], ==, "default://");
62 g_strfreev (proxies);
64 proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
65 NULL, &error);
66 g_assert_no_error (error);
67 g_assert_cmpint (g_strv_length (proxies), ==, 1);
68 g_assert_cmpstr (proxies[0], ==, "ftp://proxy.example.com");
69 g_strfreev (proxies);
71 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
72 NULL, &error);
73 g_assert_no_error (error);
74 g_assert_cmpint (g_strv_length (proxies), ==, 1);
75 g_assert_cmpstr (proxies[0], ==, "direct://");
76 g_strfreev (proxies);
78 g_object_unref (resolver);
81 static void
82 test_socks (void)
84 GProxyResolver *resolver;
85 gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
86 gchar **proxies;
87 GError *error = NULL;
89 resolver = g_simple_proxy_resolver_new ("socks://proxy.example.com", ignore_hosts);
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), ==, 3);
95 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com");
96 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com");
97 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com");
98 g_strfreev (proxies);
100 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
101 NULL, &error);
102 g_assert_no_error (error);
103 g_assert_cmpint (g_strv_length (proxies), ==, 1);
104 g_assert_cmpstr (proxies[0], ==, "direct://");
105 g_strfreev (proxies);
107 g_object_unref (resolver);
109 resolver = g_simple_proxy_resolver_new ("default-proxy://", ignore_hosts);
110 g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
111 "http", "socks://proxy.example.com");
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), ==, 3);
117 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com");
118 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com");
119 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com");
120 g_strfreev (proxies);
122 proxies = g_proxy_resolver_lookup (resolver, "ftp://two.example.com/",
123 NULL, &error);
124 g_assert_no_error (error);
125 g_assert_cmpint (g_strv_length (proxies), ==, 1);
126 g_assert_cmpstr (proxies[0], ==, "default-proxy://");
127 g_strfreev (proxies);
129 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
130 NULL, &error);
131 g_assert_no_error (error);
132 g_assert_cmpint (g_strv_length (proxies), ==, 1);
133 g_assert_cmpstr (proxies[0], ==, "direct://");
134 g_strfreev (proxies);
136 g_object_unref (resolver);
139 static const char *ignore_hosts[] = {
140 ".bbb.xx",
141 "*.ccc.xx",
142 "ddd.xx",
143 "*.eee.xx:8000",
144 "127.0.0.0/24",
145 "10.0.0.1:8000",
146 "::1",
147 "fe80::/10",
148 NULL
151 static const struct {
152 const char *uri;
153 const char *proxy;
154 } ignore_tests[] = {
155 { "http://aaa.xx/", "http://localhost:8080" },
156 { "http://aaa.xx:8000/", "http://localhost:8080" },
157 { "http://www.aaa.xx/", "http://localhost:8080" },
158 { "http://www.aaa.xx:8000/", "http://localhost:8080" },
159 { "https://aaa.xx/", "http://localhost:8080" },
160 { "http://bbb.xx/", "direct://" },
161 { "http://www.bbb.xx/", "direct://" },
162 { "http://bbb.xx:8000/", "direct://" },
163 { "http://www.bbb.xx:8000/", "direct://" },
164 { "https://bbb.xx/", "direct://" },
165 { "http://nobbb.xx/", "http://localhost:8080" },
166 { "http://www.nobbb.xx/", "http://localhost:8080" },
167 { "http://nobbb.xx:8000/", "http://localhost:8080" },
168 { "http://www.nobbb.xx:8000/", "http://localhost:8080" },
169 { "https://nobbb.xx/", "http://localhost:8080" },
170 { "http://ccc.xx/", "direct://" },
171 { "http://www.ccc.xx/", "direct://" },
172 { "http://ccc.xx:8000/", "direct://" },
173 { "http://www.ccc.xx:8000/", "direct://" },
174 { "https://ccc.xx/", "direct://" },
175 { "http://ddd.xx/", "direct://" },
176 { "http://ddd.xx:8000/", "direct://" },
177 { "http://www.ddd.xx/", "direct://" },
178 { "http://www.ddd.xx:8000/", "direct://" },
179 { "https://ddd.xx/", "direct://" },
180 { "http://eee.xx/", "http://localhost:8080" },
181 { "http://eee.xx:8000/", "direct://" },
182 { "http://www.eee.xx/", "http://localhost:8080" },
183 { "http://www.eee.xx:8000/", "direct://" },
184 { "https://eee.xx/", "http://localhost:8080" },
185 { "http://1.2.3.4/", "http://localhost:8080" },
186 { "http://127.0.0.1/", "direct://" },
187 { "http://127.0.0.2/", "direct://" },
188 { "http://127.0.0.255/", "direct://" },
189 { "http://127.0.1.0/", "http://localhost:8080" },
190 { "http://10.0.0.1/", "http://localhost:8080" },
191 { "http://10.0.0.1:8000/", "direct://" },
192 { "http://[::1]/", "direct://" },
193 { "http://[::1]:80/", "direct://" },
194 { "http://[::1:1]/", "http://localhost:8080" },
195 { "http://[::1:1]:80/", "http://localhost:8080" },
196 { "http://[fe80::1]/", "direct://" },
197 { "http://[fe80::1]:80/", "direct://" },
198 { "http://[fec0::1]/", "http://localhost:8080" },
199 { "http://[fec0::1]:80/", "http://localhost:8080" }
201 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
203 static void
204 test_ignore (void)
206 GProxyResolver *resolver;
207 GError *error = NULL;
208 char **proxies;
209 int i;
211 resolver = g_simple_proxy_resolver_new ("http://localhost:8080",
212 (char **)ignore_hosts);
214 for (i = 0; i < n_ignore_tests; i++)
216 proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
217 NULL, &error);
218 g_assert_no_error (error);
220 g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
221 g_strfreev (proxies);
224 g_object_unref (resolver);
228 main (int argc,
229 char *argv[])
231 g_test_init (&argc, &argv, NULL);
233 g_test_add_func ("/static-proxy/uri", test_uris);
234 g_test_add_func ("/static-proxy/socks", test_socks);
235 g_test_add_func ("/static-proxy/ignore", test_ignore);
237 return g_test_run();