Debian Codegraph @

proxy/tests/common.c

raw
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * GProxyResolver tests
4 *
5 * Copyright 2011-2013 Red Hat, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
22 #include <gio/gio.h>
23
24 static void
25 test_proxy_uri_common (void)
26 {
27 GProxyResolver *resolver;
28 gchar **proxies;
29 GError *error = NULL;
30
31 resolver = g_proxy_resolver_get_default ();
32
33 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
34 NULL, &error);
35 g_assert_no_error (error);
36 g_assert_cmpint (g_strv_length (proxies), ==, 1);
37 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080");
38 g_strfreev (proxies);
39
40 proxies = g_proxy_resolver_lookup (resolver, "HTTPS://uppercase.example.com/",
41 NULL, &error);
42 g_assert_no_error (error);
43 g_assert_cmpint (g_strv_length (proxies), ==, 1);
44 g_assert_cmpstr (proxies[0], ==, "http://proxy-s.example.com:7070");
45 g_strfreev (proxies);
46
47 /* Unknown protocols will use the http proxy by default in this configuration. */
48 proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
49 NULL, &error);
50 g_assert_no_error (error);
51 g_assert_cmpint (g_strv_length (proxies), ==, 1);
52 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080");
53 g_strfreev (proxies);
54
55 proxies = g_proxy_resolver_lookup (resolver, "ftps://extra-letter.example.com/",
56 NULL, &error);
57 g_assert_no_error (error);
58 g_assert_cmpint (g_strv_length (proxies), ==, 1);
59 g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080");
60 g_strfreev (proxies);
61
62 proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
63 NULL, &error);
64 g_assert_no_error (error);
65 g_assert_cmpint (g_strv_length (proxies), ==, 1);
66 g_assert_cmpstr (proxies[0], ==, "ftp://proxy-f.example.com:6060");
67 g_strfreev (proxies);
68 }
69
70 static void
71 test_proxy_socks_common (void)
72 {
73 GProxyResolver *resolver;
74 gchar **proxies;
75 GError *error = NULL;
76
77 resolver = g_proxy_resolver_get_default ();
78
79 proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
80 NULL, &error);
81 g_assert_no_error (error);
82 g_assert_cmpint (g_strv_length (proxies), ==, 3);
83 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com:1234");
84 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com:1234");
85 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com:1234");
86 g_strfreev (proxies);
87
88 proxies = g_proxy_resolver_lookup (resolver, "wednesday://two.example.com/",
89 NULL, &error);
90 g_assert_no_error (error);
91 g_assert_cmpint (g_strv_length (proxies), ==, 3);
92 g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com:1234");
93 g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com:1234");
94 g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com:1234");
95 g_strfreev (proxies);
96
97 proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
98 NULL, &error);
99 g_assert_no_error (error);
100 g_assert_cmpint (g_strv_length (proxies), ==, 1);
101 g_assert_cmpstr (proxies[0], ==, "direct://");
102 g_strfreev (proxies);
103 }
104
105 static const char *ignore_hosts[] = {
106 ".bbb.xx",
107 "*.ccc.xx",
108 "ddd.xx",
109 "*.eee.xx:8000",
110 "127.0.0.0/24",
111 "10.0.0.1:8000",
112 "::1",
113 "fe80::/10",
114 NULL
115 };
116 static const int n_ignore_hosts = G_N_ELEMENTS (ignore_hosts) - 1;
117
118 static const struct {
119 const char *uri;
120 const char *proxy;
121 gboolean libproxy_fails;
122 } ignore_tests[] = {
123 { "http://aaa.xx/", "http://localhost:8080" },
124 { "http://aaa.xx:8000/", "http://localhost:8080" },
125 { "http://www.aaa.xx/", "http://localhost:8080" },
126 { "http://www.aaa.xx:8000/", "http://localhost:8080" },
127 { "https://aaa.xx/", "http://localhost:8080" },
128 { "http://bbb.xx/", "direct://", TRUE },
129 { "http://www.bbb.xx/", "direct://" },
130 { "http://bbb.xx:8000/", "direct://", TRUE },
131 { "http://www.bbb.xx:8000/", "direct://" },
132 { "https://bbb.xx/", "direct://", TRUE },
133 { "http://nobbb.xx/", "http://localhost:8080" },
134 { "http://www.nobbb.xx/", "http://localhost:8080" },
135 { "http://nobbb.xx:8000/", "http://localhost:8080" },
136 { "http://www.nobbb.xx:8000/", "http://localhost:8080" },
137 { "https://nobbb.xx/", "http://localhost:8080" },
138 { "http://ccc.xx/", "direct://", TRUE },
139 { "http://www.ccc.xx/", "direct://" },
140 { "http://ccc.xx:8000/", "direct://", TRUE },
141 { "http://www.ccc.xx:8000/", "direct://" },
142 { "https://ccc.xx/", "direct://", TRUE },
143 { "http://ddd.xx/", "direct://" },
144 { "http://ddd.xx:8000/", "direct://" },
145 { "http://www.ddd.xx/", "direct://", TRUE },
146 { "http://www.ddd.xx:8000/", "direct://", TRUE },
147 { "https://ddd.xx/", "direct://" },
148 { "http://eee.xx/", "http://localhost:8080", TRUE },
149 { "http://eee.xx:8000/", "direct://", TRUE },
150 { "http://www.eee.xx/", "http://localhost:8080" },
151 { "http://www.eee.xx:8000/", "direct://" },
152 { "https://eee.xx/", "http://localhost:8080", TRUE },
153 { "http://1.2.3.4/", "http://localhost:8080" },
154 { "http://127.0.0.1/", "direct://" },
155 { "http://127.0.0.2/", "direct://" },
156 { "http://127.0.0.255/", "direct://" },
157 { "http://127.0.1.0/", "http://localhost:8080" },
158 { "http://10.0.0.1/", "http://localhost:8080" },
159 { "http://10.0.0.1:8000/", "direct://" },
160 { "http://[::1]/", "direct://", TRUE },
161 { "http://[::1]:80/", "direct://", TRUE },
162 { "http://[::1:1]/", "http://localhost:8080" },
163 { "http://[::1:1]:80/", "http://localhost:8080" },
164 { "http://[fe80::1]/", "direct://", TRUE },
165 { "http://[fe80::1]:80/", "direct://", TRUE },
166 { "http://[fec0::1]/", "http://localhost:8080" },
167 { "http://[fec0::1]:80/", "http://localhost:8080" }
168 };
169 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
170
171 static void
172 test_proxy_ignore_common (void)
173 {
174 GProxyResolver *resolver;
175 GError *error = NULL;
176 char **proxies;
177 int i;
178
179 #ifndef LIBPROXY_0_5
180 gboolean is_libproxy = g_strcmp0 (g_getenv ("GIO_PROXY_TEST_NAME"), "libproxy") == 0;
181 #endif
182
183 resolver = g_proxy_resolver_get_default ();
184
185 for (i = 0; i < n_ignore_tests; i++)
186 {
187 proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
188 NULL, &error);
189 g_assert_no_error (error);
190
191 #ifndef LIBPROXY_0_5
192 if (is_libproxy && ignore_tests[i].libproxy_fails)
193 g_assert_cmpstr (proxies[0], ==, "http://localhost:8080");
194 else
195 #endif
196 g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
197
198 g_strfreev (proxies);
199 }
200 }