Branch data Line data Source code
1 : : /* Unit tests for GInetAddress
2 : : * Copyright (C) 2012 Red Hat, Inc
3 : : * Author: Matthias Clasen
4 : : *
5 : : * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 : : *
7 : : * This work is provided "as is"; redistribution and modification
8 : : * in whole or in part, in any medium, physical or electronic is
9 : : * permitted without restriction.
10 : : *
11 : : * This work is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 : : *
15 : : * In no event shall the authors or contributors be liable for any
16 : : * direct, indirect, incidental, special, exemplary, or consequential
17 : : * damages (including, but not limited to, procurement of substitute
18 : : * goods or services; loss of use, data, or profits; or business
19 : : * interruption) however caused and on any theory of liability, whether
20 : : * in contract, strict liability, or tort (including negligence or
21 : : * otherwise) arising in any way out of the use of this software, even
22 : : * if advised of the possibility of such damage.
23 : : */
24 : :
25 : : #include "config.h"
26 : :
27 : : #include <gio/gio.h>
28 : : #include <gio/gnetworking.h>
29 : :
30 : : static void
31 : 1 : test_parse (void)
32 : : {
33 : : GInetAddress *addr;
34 : :
35 : 1 : addr = g_inet_address_new_from_string ("0:0:0:0:0:0:0:0");
36 : 1 : g_assert (addr != NULL);
37 : 1 : g_object_unref (addr);
38 : 1 : addr = g_inet_address_new_from_string ("1:0:0:0:0:0:0:8");
39 : 1 : g_assert (addr != NULL);
40 : 1 : g_object_unref (addr);
41 : 1 : addr = g_inet_address_new_from_string ("0:0:0:0:0:FFFF:204.152.189.116");
42 : 1 : g_assert (addr != NULL);
43 : 1 : g_object_unref (addr);
44 : 1 : addr = g_inet_address_new_from_string ("::1");
45 : 1 : g_assert (addr != NULL);
46 : 1 : g_object_unref (addr);
47 : 1 : addr = g_inet_address_new_from_string ("::");
48 : 1 : g_assert (addr != NULL);
49 : 1 : g_object_unref (addr);
50 : 1 : addr = g_inet_address_new_from_string ("::FFFF:204.152.189.116");
51 : 1 : g_assert (addr != NULL);
52 : 1 : g_object_unref (addr);
53 : 1 : addr = g_inet_address_new_from_string ("204.152.189.116");
54 : 1 : g_assert (addr != NULL);
55 : 1 : g_object_unref (addr);
56 : : #ifndef G_OS_WIN32 /* getaddrinfo on Windows does not support scope-id */
57 : 1 : addr = g_inet_address_new_from_string ("::1%0");
58 : 1 : g_assert (addr != NULL);
59 : 1 : g_object_unref (addr);
60 : : #endif
61 : :
62 : 1 : addr = g_inet_address_new_from_string ("::1::2");
63 : 1 : g_assert (addr == NULL);
64 : 1 : addr = g_inet_address_new_from_string ("2001:1:2:3:4:5:6:7]");
65 : 1 : g_assert (addr == NULL);
66 : 1 : addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7");
67 : 1 : g_assert (addr == NULL);
68 : : #ifndef G_OS_WIN32 /* getaddrinfo on Windows is more forgiving about format and accepts these strings */
69 : 1 : addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]");
70 : 1 : g_assert (addr == NULL);
71 : 1 : addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]:80");
72 : 1 : g_assert (addr == NULL);
73 : : #endif
74 : 1 : addr = g_inet_address_new_from_string ("0:1:2:3:4:5:6:7:8:9");
75 : 1 : g_assert (addr == NULL);
76 : 1 : addr = g_inet_address_new_from_string ("::FFFFFFF");
77 : 1 : g_assert (addr == NULL);
78 : 1 : addr = g_inet_address_new_from_string ("204.152.189.116:80");
79 : 1 : g_assert (addr == NULL);
80 : 1 : }
81 : :
82 : : static void
83 : 1 : test_any (void)
84 : : {
85 : : GInetAddress *addr;
86 : 1 : GSocketFamily family[2] = { G_SOCKET_FAMILY_IPV4, G_SOCKET_FAMILY_IPV6 };
87 : 1 : gsize size[2] = { 4, 16 };
88 : : gint i;
89 : :
90 : 3 : for (i = 0; i < 2; i++)
91 : : {
92 : 2 : addr = g_inet_address_new_any (family[i]);
93 : :
94 : 2 : g_assert (g_inet_address_get_is_any (addr));
95 : 2 : g_assert (g_inet_address_get_family (addr) == family[i]);
96 : 2 : g_assert (g_inet_address_get_native_size (addr) == size[i]);
97 : 2 : g_assert (!g_inet_address_get_is_loopback (addr));
98 : 2 : g_assert (!g_inet_address_get_is_link_local (addr));
99 : 2 : g_assert (!g_inet_address_get_is_site_local (addr));
100 : 2 : g_assert (!g_inet_address_get_is_multicast (addr));
101 : 2 : g_assert (!g_inet_address_get_is_mc_global (addr));
102 : 2 : g_assert (!g_inet_address_get_is_mc_link_local (addr));
103 : 2 : g_assert (!g_inet_address_get_is_mc_node_local (addr));
104 : 2 : g_assert (!g_inet_address_get_is_mc_org_local (addr));
105 : 2 : g_assert (!g_inet_address_get_is_mc_site_local (addr));
106 : :
107 : 2 : g_object_unref (addr);
108 : : }
109 : 1 : }
110 : :
111 : : static void
112 : 1 : test_loopback (void)
113 : : {
114 : : GInetAddress *addr;
115 : :
116 : 1 : addr = g_inet_address_new_from_string ("::1");
117 : 1 : g_assert (g_inet_address_get_family (addr) == G_SOCKET_FAMILY_IPV6);
118 : 1 : g_assert (g_inet_address_get_is_loopback (addr));
119 : 1 : g_object_unref (addr);
120 : :
121 : 1 : addr = g_inet_address_new_from_string ("127.0.0.0");
122 : 1 : g_assert (g_inet_address_get_family (addr) == G_SOCKET_FAMILY_IPV4);
123 : 1 : g_assert (g_inet_address_get_is_loopback (addr));
124 : 1 : g_object_unref (addr);
125 : 1 : }
126 : :
127 : : static void
128 : 1 : test_bytes (void)
129 : : {
130 : : GInetAddress *addr1, *addr2, *addr3;
131 : : const guint8 *bytes;
132 : :
133 : 1 : addr1 = g_inet_address_new_from_string ("192.168.0.100");
134 : 1 : addr2 = g_inet_address_new_from_string ("192.168.0.101");
135 : 1 : bytes = g_inet_address_to_bytes (addr1);
136 : 1 : addr3 = g_inet_address_new_from_bytes (bytes, G_SOCKET_FAMILY_IPV4);
137 : :
138 : 1 : g_assert (!g_inet_address_equal (addr1, addr2));
139 : 1 : g_assert (g_inet_address_equal (addr1, addr3));
140 : :
141 : 1 : g_object_unref (addr1);
142 : 1 : g_object_unref (addr2);
143 : 1 : g_object_unref (addr3);
144 : 1 : }
145 : :
146 : : static void
147 : 1 : test_property (void)
148 : : {
149 : : GInetAddress *addr;
150 : : GSocketFamily family;
151 : : const guint8 *bytes;
152 : : gboolean any;
153 : : gboolean loopback;
154 : : gboolean link_local;
155 : : gboolean site_local;
156 : : gboolean multicast;
157 : : gboolean mc_global;
158 : : gboolean mc_link_local;
159 : : gboolean mc_node_local;
160 : : gboolean mc_org_local;
161 : : gboolean mc_site_local;
162 : :
163 : 1 : addr = g_inet_address_new_from_string ("ff85::");
164 : 1 : g_object_get (addr,
165 : : "family", &family,
166 : : "bytes", &bytes,
167 : : "is-any", &any,
168 : : "is-loopback", &loopback,
169 : : "is-link-local", &link_local,
170 : : "is-site-local", &site_local,
171 : : "is-multicast", &multicast,
172 : : "is-mc-global", &mc_global,
173 : : "is-mc-link-local", &mc_link_local,
174 : : "is-mc-node-local", &mc_node_local,
175 : : "is-mc-org-local", &mc_org_local,
176 : : "is-mc-site-local", &mc_site_local,
177 : : NULL);
178 : :
179 : 1 : g_assert (family == G_SOCKET_FAMILY_IPV6);
180 : 1 : g_assert (!any);
181 : 1 : g_assert (!loopback);
182 : 1 : g_assert (!link_local);
183 : 1 : g_assert (!site_local);
184 : 1 : g_assert (multicast);
185 : 1 : g_assert (!mc_global);
186 : 1 : g_assert (!mc_link_local);
187 : 1 : g_assert (!mc_node_local);
188 : 1 : g_assert (!mc_org_local);
189 : 1 : g_assert (mc_site_local);
190 : :
191 : 1 : g_object_unref (addr);
192 : 1 : }
193 : :
194 : : static void
195 : 1 : test_socket_address (void)
196 : : {
197 : : GInetAddress *addr;
198 : : GInetSocketAddress *saddr;
199 : : guint port;
200 : : guint32 flowinfo;
201 : : guint32 scope_id;
202 : : GSocketFamily family;
203 : :
204 : 1 : addr = g_inet_address_new_from_string ("::ffff:125.1.15.5");
205 : 1 : saddr = G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, 308));
206 : :
207 : 1 : g_assert (g_inet_address_equal (addr, g_inet_socket_address_get_address (saddr)));
208 : 1 : g_object_unref (addr);
209 : :
210 : 1 : g_assert (g_inet_socket_address_get_port (saddr) == 308);
211 : 1 : g_assert (g_inet_socket_address_get_flowinfo (saddr) == 0);
212 : 1 : g_assert (g_inet_socket_address_get_scope_id (saddr) == 0);
213 : :
214 : 1 : g_object_unref (saddr);
215 : :
216 : 1 : addr = g_inet_address_new_from_string ("::1");
217 : 1 : saddr = G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
218 : : "address", addr,
219 : : "port", 308,
220 : : "flowinfo", 10,
221 : : "scope-id", 25,
222 : : NULL));
223 : 1 : g_object_unref (addr);
224 : :
225 : 1 : g_assert (g_inet_socket_address_get_port (saddr) == 308);
226 : 1 : g_assert (g_inet_socket_address_get_flowinfo (saddr) == 10);
227 : 1 : g_assert (g_inet_socket_address_get_scope_id (saddr) == 25);
228 : :
229 : 1 : g_object_get (saddr,
230 : : "family", &family,
231 : : "address", &addr,
232 : : "port", &port,
233 : : "flowinfo", &flowinfo,
234 : : "scope-id", &scope_id,
235 : : NULL);
236 : :
237 : 1 : g_assert (family == G_SOCKET_FAMILY_IPV6);
238 : 1 : g_assert (addr != NULL);
239 : 1 : g_assert (port == 308);
240 : 1 : g_assert (flowinfo == 10);
241 : 1 : g_assert (scope_id == 25);
242 : :
243 : 1 : g_object_unref (addr);
244 : 1 : g_object_unref (saddr);
245 : 1 : }
246 : :
247 : : static void
248 : 1 : test_socket_address_to_string (void)
249 : : {
250 : 1 : GSocketAddress *sa = NULL;
251 : 1 : GInetAddress *ia = NULL;
252 : 1 : gchar *str = NULL;
253 : :
254 : : /* IPv4. */
255 : 1 : ia = g_inet_address_new_from_string ("123.1.123.1");
256 : 1 : sa = g_inet_socket_address_new (ia, 80);
257 : 1 : str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
258 : 1 : g_assert_cmpstr (str, ==, "123.1.123.1:80");
259 : 1 : g_free (str);
260 : 1 : g_object_unref (sa);
261 : 1 : g_object_unref (ia);
262 : :
263 : : /* IPv6. */
264 : 1 : ia = g_inet_address_new_from_string ("fe80::80");
265 : 1 : sa = g_inet_socket_address_new (ia, 80);
266 : 1 : str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
267 : 1 : g_assert_cmpstr (str, ==, "[fe80::80]:80");
268 : 1 : g_free (str);
269 : 1 : g_object_unref (sa);
270 : 1 : g_object_unref (ia);
271 : :
272 : : /* IPv6 without port. */
273 : 1 : ia = g_inet_address_new_from_string ("fe80::80");
274 : 1 : sa = g_inet_socket_address_new (ia, 0);
275 : 1 : str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
276 : 1 : g_assert_cmpstr (str, ==, "fe80::80");
277 : 1 : g_free (str);
278 : 1 : g_object_unref (sa);
279 : 1 : g_object_unref (ia);
280 : :
281 : : /* IPv6 with scope. */
282 : 1 : ia = g_inet_address_new_from_string ("::1");
283 : 1 : sa = G_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
284 : : "address", ia,
285 : : "port", 123,
286 : : "flowinfo", 10,
287 : : "scope-id", 25,
288 : : NULL));
289 : 1 : str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
290 : 1 : g_assert_cmpstr (str, ==, "[::1%25]:123");
291 : 1 : g_free (str);
292 : 1 : g_object_unref (sa);
293 : 1 : g_object_unref (ia);
294 : 1 : }
295 : :
296 : : static void
297 : 1 : test_mask_parse (void)
298 : : {
299 : : GInetAddressMask *mask;
300 : 1 : GError *error = NULL;
301 : :
302 : 1 : mask = g_inet_address_mask_new_from_string ("10.0.0.0/8", &error);
303 : 1 : g_assert_no_error (error);
304 : 1 : g_assert (mask != NULL);
305 : 1 : g_object_unref (mask);
306 : :
307 : 1 : mask = g_inet_address_mask_new_from_string ("fe80::/10", &error);
308 : 1 : g_assert_no_error (error);
309 : 1 : g_assert (mask != NULL);
310 : 1 : g_object_unref (mask);
311 : :
312 : 1 : mask = g_inet_address_mask_new_from_string ("::", &error);
313 : 1 : g_assert_no_error (error);
314 : 1 : g_assert (mask != NULL);
315 : 1 : g_object_unref (mask);
316 : :
317 : 1 : mask = g_inet_address_mask_new_from_string ("::/abc", &error);
318 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
319 : 1 : g_assert (mask == NULL);
320 : 1 : g_clear_error (&error);
321 : :
322 : 1 : mask = g_inet_address_mask_new_from_string ("127.0.0.1/128", &error);
323 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
324 : 1 : g_assert (mask == NULL);
325 : 1 : g_clear_error (&error);
326 : 1 : }
327 : :
328 : : static void
329 : 1 : test_mask_property (void)
330 : : {
331 : : GInetAddressMask *mask;
332 : : GInetAddress *addr;
333 : : GSocketFamily family;
334 : : guint len;
335 : :
336 : 1 : addr = g_inet_address_new_from_string ("fe80::");
337 : 1 : mask = g_inet_address_mask_new_from_string ("fe80::/10", NULL);
338 : 1 : g_assert (g_inet_address_mask_get_family (mask) == G_SOCKET_FAMILY_IPV6);
339 : 1 : g_assert (g_inet_address_equal (addr, g_inet_address_mask_get_address (mask)));
340 : 1 : g_assert (g_inet_address_mask_get_length (mask) == 10);
341 : 1 : g_object_unref (addr);
342 : :
343 : 1 : g_object_get (mask,
344 : : "family", &family,
345 : : "address", &addr,
346 : : "length", &len,
347 : : NULL);
348 : 1 : g_assert (family == G_SOCKET_FAMILY_IPV6);
349 : 1 : g_assert (addr != NULL);
350 : 1 : g_assert (len == 10);
351 : 1 : g_object_unref (addr);
352 : :
353 : 1 : g_object_unref (mask);
354 : 1 : }
355 : :
356 : : static void
357 : 1 : test_mask_equal (void)
358 : : {
359 : : GInetAddressMask *mask;
360 : : GInetAddressMask *mask2;
361 : : gchar *str;
362 : :
363 : 1 : mask = g_inet_address_mask_new_from_string ("fe80:0:0::/10", NULL);
364 : 1 : str = g_inet_address_mask_to_string (mask);
365 : 1 : g_assert_cmpstr (str, ==, "fe80::/10");
366 : 1 : mask2 = g_inet_address_mask_new_from_string (str, NULL);
367 : 1 : g_assert (g_inet_address_mask_equal (mask, mask2));
368 : 1 : g_object_unref (mask2);
369 : 1 : g_free (str);
370 : :
371 : 1 : mask2 = g_inet_address_mask_new_from_string ("fe80::/12", NULL);
372 : 1 : g_assert (!g_inet_address_mask_equal (mask, mask2));
373 : 1 : g_object_unref (mask2);
374 : :
375 : 1 : mask2 = g_inet_address_mask_new_from_string ("ff80::/10", NULL);
376 : 1 : g_assert (!g_inet_address_mask_equal (mask, mask2));
377 : 1 : g_object_unref (mask2);
378 : :
379 : 1 : g_object_unref (mask);
380 : 1 : }
381 : :
382 : : static void
383 : 1 : test_mask_match (void)
384 : : {
385 : : GInetAddressMask *mask;
386 : : GInetAddress *addr;
387 : :
388 : 1 : mask = g_inet_address_mask_new_from_string ("1.2.0.0/16", NULL);
389 : :
390 : 1 : addr = g_inet_address_new_from_string ("1.2.0.0");
391 : 1 : g_assert (g_inet_address_mask_matches (mask, addr));
392 : 1 : g_object_unref (addr);
393 : 1 : addr = g_inet_address_new_from_string ("1.2.3.4");
394 : 1 : g_assert (g_inet_address_mask_matches (mask, addr));
395 : 1 : g_object_unref (addr);
396 : 1 : addr = g_inet_address_new_from_string ("1.3.1.1");
397 : 1 : g_assert (!g_inet_address_mask_matches (mask, addr));
398 : 1 : g_object_unref (addr);
399 : :
400 : 1 : g_object_unref (mask);
401 : :
402 : 1 : mask = g_inet_address_mask_new_from_string ("1.2.0.0/24", NULL);
403 : :
404 : 1 : addr = g_inet_address_new_from_string ("1.2.0.0");
405 : 1 : g_assert (g_inet_address_mask_matches (mask, addr));
406 : 1 : g_object_unref (addr);
407 : 1 : addr = g_inet_address_new_from_string ("1.2.3.4");
408 : 1 : g_assert (!g_inet_address_mask_matches (mask, addr));
409 : 1 : g_object_unref (addr);
410 : 1 : addr = g_inet_address_new_from_string ("1.2.0.24");
411 : 1 : g_assert (g_inet_address_mask_matches (mask, addr));
412 : 1 : g_object_unref (addr);
413 : :
414 : 1 : g_object_unref (mask);
415 : :
416 : 1 : }
417 : :
418 : : int
419 : 1 : main (int argc, char *argv[])
420 : : {
421 : 1 : g_test_init (&argc, &argv, NULL);
422 : :
423 : 1 : g_test_add_func ("/inet-address/parse", test_parse);
424 : 1 : g_test_add_func ("/inet-address/any", test_any);
425 : 1 : g_test_add_func ("/inet-address/loopback", test_loopback);
426 : 1 : g_test_add_func ("/inet-address/bytes", test_bytes);
427 : 1 : g_test_add_func ("/inet-address/property", test_property);
428 : 1 : g_test_add_func ("/socket-address/basic", test_socket_address);
429 : 1 : g_test_add_func ("/socket-address/to-string", test_socket_address_to_string);
430 : 1 : g_test_add_func ("/address-mask/parse", test_mask_parse);
431 : 1 : g_test_add_func ("/address-mask/property", test_mask_property);
432 : 1 : g_test_add_func ("/address-mask/equal", test_mask_equal);
433 : 1 : g_test_add_func ("/address-mask/match", test_mask_match);
434 : :
435 : 1 : return g_test_run ();
436 : : }
|