1 /* Unit tests for GInetAddress
2 * Copyright (C) 2012 Red Hat, Inc
3 * Author: Matthias Clasen
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
26 #include <gio/gnetworking.h>
33 addr
= g_inet_address_new_from_string ("0:0:0:0:0:0:0:0");
34 g_assert (addr
!= NULL
);
35 g_object_unref (addr
);
36 addr
= g_inet_address_new_from_string ("1:0:0:0:0:0:0:8");
37 g_assert (addr
!= NULL
);
38 g_object_unref (addr
);
39 addr
= g_inet_address_new_from_string ("0:0:0:0:0:FFFF:204.152.189.116");
40 g_assert (addr
!= NULL
);
41 g_object_unref (addr
);
42 addr
= g_inet_address_new_from_string ("::1");
43 g_assert (addr
!= NULL
);
44 g_object_unref (addr
);
45 addr
= g_inet_address_new_from_string ("::");
46 g_assert (addr
!= NULL
);
47 g_object_unref (addr
);
48 addr
= g_inet_address_new_from_string ("::FFFF:204.152.189.116");
49 g_assert (addr
!= NULL
);
50 g_object_unref (addr
);
51 addr
= g_inet_address_new_from_string ("204.152.189.116");
52 g_assert (addr
!= NULL
);
53 g_object_unref (addr
);
55 addr
= g_inet_address_new_from_string ("::1::2");
56 g_assert (addr
== NULL
);
57 addr
= g_inet_address_new_from_string ("2001:1:2:3:4:5:6:7]");
58 g_assert (addr
== NULL
);
59 addr
= g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7");
60 g_assert (addr
== NULL
);
61 addr
= g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]");
62 g_assert (addr
== NULL
);
63 addr
= g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]:80");
64 g_assert (addr
== NULL
);
65 addr
= g_inet_address_new_from_string ("0:1:2:3:4:5:6:7:8:9");
66 g_assert (addr
== NULL
);
67 addr
= g_inet_address_new_from_string ("::FFFFFFF");
68 g_assert (addr
== NULL
);
69 addr
= g_inet_address_new_from_string ("204.152.189.116:80");
70 g_assert (addr
== NULL
);
77 GSocketFamily family
[2] = { G_SOCKET_FAMILY_IPV4
, G_SOCKET_FAMILY_IPV6
};
78 gsize size
[2] = { 4, 16 };
81 for (i
= 0; i
< 2; i
++)
83 addr
= g_inet_address_new_any (family
[i
]);
85 g_assert (g_inet_address_get_is_any (addr
));
86 g_assert (g_inet_address_get_family (addr
) == family
[i
]);
87 g_assert (g_inet_address_get_native_size (addr
) == size
[i
]);
88 g_assert (!g_inet_address_get_is_loopback (addr
));
89 g_assert (!g_inet_address_get_is_link_local (addr
));
90 g_assert (!g_inet_address_get_is_site_local (addr
));
91 g_assert (!g_inet_address_get_is_multicast (addr
));
92 g_assert (!g_inet_address_get_is_mc_global (addr
));
93 g_assert (!g_inet_address_get_is_mc_link_local (addr
));
94 g_assert (!g_inet_address_get_is_mc_node_local (addr
));
95 g_assert (!g_inet_address_get_is_mc_org_local (addr
));
96 g_assert (!g_inet_address_get_is_mc_site_local (addr
));
98 g_object_unref (addr
);
107 addr
= g_inet_address_new_from_string ("::1");
108 g_assert (g_inet_address_get_family (addr
) == G_SOCKET_FAMILY_IPV6
);
109 g_assert (g_inet_address_get_is_loopback (addr
));
110 g_object_unref (addr
);
112 addr
= g_inet_address_new_from_string ("127.0.0.0");
113 g_assert (g_inet_address_get_family (addr
) == G_SOCKET_FAMILY_IPV4
);
114 g_assert (g_inet_address_get_is_loopback (addr
));
115 g_object_unref (addr
);
121 GInetAddress
*addr1
, *addr2
, *addr3
;
124 addr1
= g_inet_address_new_from_string ("192.168.0.100");
125 addr2
= g_inet_address_new_from_string ("192.168.0.101");
126 bytes
= g_inet_address_to_bytes (addr1
);
127 addr3
= g_inet_address_new_from_bytes (bytes
, G_SOCKET_FAMILY_IPV4
);
129 g_assert (!g_inet_address_equal (addr1
, addr2
));
130 g_assert (g_inet_address_equal (addr1
, addr3
));
132 g_object_unref (addr1
);
133 g_object_unref (addr2
);
134 g_object_unref (addr3
);
141 GSocketFamily family
;
149 gboolean mc_link_local
;
150 gboolean mc_node_local
;
151 gboolean mc_org_local
;
152 gboolean mc_site_local
;
154 addr
= g_inet_address_new_from_string ("ff85::");
159 "is-loopback", &loopback
,
160 "is-link-local", &link_local
,
161 "is-site-local", &site_local
,
162 "is-multicast", &multicast
,
163 "is-mc-global", &mc_global
,
164 "is-mc-link-local", &mc_link_local
,
165 "is-mc-node-local", &mc_node_local
,
166 "is-mc-org-local", &mc_org_local
,
167 "is-mc-site-local", &mc_site_local
,
170 g_assert (family
== G_SOCKET_FAMILY_IPV6
);
172 g_assert (!loopback
);
173 g_assert (!link_local
);
174 g_assert (!site_local
);
175 g_assert (multicast
);
176 g_assert (!mc_global
);
177 g_assert (!mc_link_local
);
178 g_assert (!mc_node_local
);
179 g_assert (!mc_org_local
);
180 g_assert (mc_site_local
);
182 g_object_unref (addr
);
186 test_socket_address (void)
189 GInetSocketAddress
*saddr
;
193 GSocketFamily family
;
195 addr
= g_inet_address_new_from_string ("::ffff:125.1.15.5");
196 saddr
= G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr
, 308));
198 g_assert (g_inet_address_equal (addr
, g_inet_socket_address_get_address (saddr
)));
199 g_object_unref (addr
);
201 g_assert (g_inet_socket_address_get_port (saddr
) == 308);
202 g_assert (g_inet_socket_address_get_flowinfo (saddr
) == 0);
203 g_assert (g_inet_socket_address_get_scope_id (saddr
) == 0);
205 g_object_unref (saddr
);
207 addr
= g_inet_address_new_from_string ("::1");
208 saddr
= G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS
,
214 g_object_unref (addr
);
216 g_assert (g_inet_socket_address_get_port (saddr
) == 308);
217 g_assert (g_inet_socket_address_get_flowinfo (saddr
) == 10);
218 g_assert (g_inet_socket_address_get_scope_id (saddr
) == 25);
224 "flowinfo", &flowinfo
,
225 "scope-id", &scope_id
,
228 g_assert (family
== G_SOCKET_FAMILY_IPV6
);
229 g_assert (addr
!= NULL
);
230 g_assert (port
== 308);
231 g_assert (flowinfo
== 10);
232 g_assert (scope_id
== 25);
234 g_object_unref (addr
);
235 g_object_unref (saddr
);
239 test_socket_address_to_string (void)
241 GSocketAddress
*sa
= NULL
;
242 GInetAddress
*ia
= NULL
;
246 ia
= g_inet_address_new_from_string ("123.1.123.1");
247 sa
= g_inet_socket_address_new (ia
, 80);
248 str
= g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa
));
249 g_assert_cmpstr (str
, ==, "123.1.123.1:80");
255 ia
= g_inet_address_new_from_string ("fe80::80");
256 sa
= g_inet_socket_address_new (ia
, 80);
257 str
= g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa
));
258 g_assert_cmpstr (str
, ==, "[fe80::80]:80");
263 /* IPv6 without port. */
264 ia
= g_inet_address_new_from_string ("fe80::80");
265 sa
= g_inet_socket_address_new (ia
, 0);
266 str
= g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa
));
267 g_assert_cmpstr (str
, ==, "fe80::80");
272 /* IPv6 with scope. */
273 ia
= g_inet_address_new_from_string ("::1");
274 sa
= G_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS
,
280 str
= g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa
));
281 g_assert_cmpstr (str
, ==, "[::1%25]:123");
288 test_mask_parse (void)
290 GInetAddressMask
*mask
;
291 GError
*error
= NULL
;
293 mask
= g_inet_address_mask_new_from_string ("10.0.0.0/8", &error
);
294 g_assert_no_error (error
);
295 g_assert (mask
!= NULL
);
296 g_object_unref (mask
);
298 mask
= g_inet_address_mask_new_from_string ("fe80::/10", &error
);
299 g_assert_no_error (error
);
300 g_assert (mask
!= NULL
);
301 g_object_unref (mask
);
303 mask
= g_inet_address_mask_new_from_string ("::", &error
);
304 g_assert_no_error (error
);
305 g_assert (mask
!= NULL
);
306 g_object_unref (mask
);
308 mask
= g_inet_address_mask_new_from_string ("::/abc", &error
);
309 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
);
310 g_assert (mask
== NULL
);
311 g_clear_error (&error
);
313 mask
= g_inet_address_mask_new_from_string ("127.0.0.1/128", &error
);
314 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
);
315 g_assert (mask
== NULL
);
316 g_clear_error (&error
);
320 test_mask_property (void)
322 GInetAddressMask
*mask
;
324 GSocketFamily family
;
327 addr
= g_inet_address_new_from_string ("fe80::");
328 mask
= g_inet_address_mask_new_from_string ("fe80::/10", NULL
);
329 g_assert (g_inet_address_mask_get_family (mask
) == G_SOCKET_FAMILY_IPV6
);
330 g_assert (g_inet_address_equal (addr
, g_inet_address_mask_get_address (mask
)));
331 g_assert (g_inet_address_mask_get_length (mask
) == 10);
332 g_object_unref (addr
);
339 g_assert (family
== G_SOCKET_FAMILY_IPV6
);
340 g_assert (addr
!= NULL
);
341 g_assert (len
== 10);
342 g_object_unref (addr
);
344 g_object_unref (mask
);
348 test_mask_equal (void)
350 GInetAddressMask
*mask
;
351 GInetAddressMask
*mask2
;
354 mask
= g_inet_address_mask_new_from_string ("fe80:0:0::/10", NULL
);
355 str
= g_inet_address_mask_to_string (mask
);
356 g_assert_cmpstr (str
, ==, "fe80::/10");
357 mask2
= g_inet_address_mask_new_from_string (str
, NULL
);
358 g_assert (g_inet_address_mask_equal (mask
, mask2
));
359 g_object_unref (mask2
);
362 mask2
= g_inet_address_mask_new_from_string ("fe80::/12", NULL
);
363 g_assert (!g_inet_address_mask_equal (mask
, mask2
));
364 g_object_unref (mask2
);
366 mask2
= g_inet_address_mask_new_from_string ("ff80::/10", NULL
);
367 g_assert (!g_inet_address_mask_equal (mask
, mask2
));
368 g_object_unref (mask2
);
370 g_object_unref (mask
);
374 test_mask_match (void)
376 GInetAddressMask
*mask
;
379 mask
= g_inet_address_mask_new_from_string ("1.2.0.0/16", NULL
);
381 addr
= g_inet_address_new_from_string ("1.2.0.0");
382 g_assert (g_inet_address_mask_matches (mask
, addr
));
383 g_object_unref (addr
);
384 addr
= g_inet_address_new_from_string ("1.2.3.4");
385 g_assert (g_inet_address_mask_matches (mask
, addr
));
386 g_object_unref (addr
);
387 addr
= g_inet_address_new_from_string ("1.3.1.1");
388 g_assert (!g_inet_address_mask_matches (mask
, addr
));
389 g_object_unref (addr
);
391 g_object_unref (mask
);
393 mask
= g_inet_address_mask_new_from_string ("1.2.0.0/24", NULL
);
395 addr
= g_inet_address_new_from_string ("1.2.0.0");
396 g_assert (g_inet_address_mask_matches (mask
, addr
));
397 g_object_unref (addr
);
398 addr
= g_inet_address_new_from_string ("1.2.3.4");
399 g_assert (!g_inet_address_mask_matches (mask
, addr
));
400 g_object_unref (addr
);
401 addr
= g_inet_address_new_from_string ("1.2.0.24");
402 g_assert (g_inet_address_mask_matches (mask
, addr
));
403 g_object_unref (addr
);
405 g_object_unref (mask
);
410 main (int argc
, char *argv
[])
412 g_test_init (&argc
, &argv
, NULL
);
414 g_test_add_func ("/inet-address/parse", test_parse
);
415 g_test_add_func ("/inet-address/any", test_any
);
416 g_test_add_func ("/inet-address/loopback", test_loopback
);
417 g_test_add_func ("/inet-address/bytes", test_bytes
);
418 g_test_add_func ("/inet-address/property", test_property
);
419 g_test_add_func ("/socket-address/basic", test_socket_address
);
420 g_test_add_func ("/socket-address/to-string", test_socket_address_to_string
);
421 g_test_add_func ("/address-mask/parse", test_mask_parse
);
422 g_test_add_func ("/address-mask/property", test_mask_property
);
423 g_test_add_func ("/address-mask/equal", test_mask_equal
);
424 g_test_add_func ("/address-mask/match", test_mask_match
);
426 return g_test_run ();