[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / inet-address.c
1 /* Unit tests for GInetAddress
2  * Copyright (C) 2012 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
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.
8  *
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.
12  *
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.
21  */
22
23 #include "config.h"
24
25 #include <gio/gio.h>
26 #include <gio/gnetworking.h>
27
28 static void
29 test_parse (void)
30 {
31   GInetAddress *addr;
32
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);
54
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);
71 }
72
73 static void
74 test_any (void)
75 {
76   GInetAddress *addr;
77   GSocketFamily family[2] = { G_SOCKET_FAMILY_IPV4, G_SOCKET_FAMILY_IPV6 };
78   gsize size[2] = { 4, 16 };
79   gint i;
80
81   for (i = 0; i < 2; i++)
82     {
83       addr = g_inet_address_new_any (family[i]);
84
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));
97
98       g_object_unref (addr);
99    }
100 }
101
102 static void
103 test_loopback (void)
104 {
105   GInetAddress *addr;
106
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);
111
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);
116 }
117
118 static void
119 test_bytes (void)
120 {
121   GInetAddress *addr1, *addr2, *addr3;
122   const guint8 *bytes;
123
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);
128
129   g_assert (!g_inet_address_equal (addr1, addr2));
130   g_assert (g_inet_address_equal (addr1, addr3));
131
132   g_object_unref (addr1);
133   g_object_unref (addr2);
134   g_object_unref (addr3);
135 }
136
137 static void
138 test_property (void)
139 {
140   GInetAddress *addr;
141   GSocketFamily family;
142   const guint8 *bytes;
143   gboolean any;
144   gboolean loopback;
145   gboolean link_local;
146   gboolean site_local;
147   gboolean multicast;
148   gboolean mc_global;
149   gboolean mc_link_local;
150   gboolean mc_node_local;
151   gboolean mc_org_local;
152   gboolean mc_site_local;
153
154   addr = g_inet_address_new_from_string ("ff85::");
155   g_object_get (addr,
156                 "family", &family,
157                 "bytes", &bytes,
158                 "is-any", &any,
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,
168                 NULL);
169
170   g_assert (family == G_SOCKET_FAMILY_IPV6);
171   g_assert (!any);
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);
181
182   g_object_unref (addr);
183 }
184
185 static void
186 test_socket_address (void)
187 {
188   GInetAddress *addr;
189   GInetSocketAddress *saddr;
190   guint port;
191   guint32 flowinfo;
192   guint32 scope_id;
193   GSocketFamily family;
194
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));
197
198   g_assert (g_inet_address_equal (addr, g_inet_socket_address_get_address (saddr)));
199   g_object_unref (addr);
200
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);
204
205   g_object_unref (saddr);
206
207   addr = g_inet_address_new_from_string ("::1");
208   saddr = G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
209                                                "address", addr,
210                                                "port", 308,
211                                                "flowinfo", 10,
212                                                "scope-id", 25,
213                                                NULL));
214   g_object_unref (addr);
215
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);
219
220   g_object_get (saddr,
221                 "family", &family,
222                 "address", &addr,
223                 "port", &port,
224                 "flowinfo", &flowinfo,
225                 "scope-id", &scope_id,
226                 NULL);
227
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);
233
234   g_object_unref (addr);
235   g_object_unref (saddr);
236 }
237
238 static void
239 test_mask_parse (void)
240 {
241   GInetAddressMask *mask;
242   GError *error = NULL;
243
244   mask = g_inet_address_mask_new_from_string ("10.0.0.0/8", &error);
245   g_assert_no_error (error);
246   g_assert (mask != NULL);
247   g_object_unref (mask);
248
249   mask = g_inet_address_mask_new_from_string ("fe80::/10", &error);
250   g_assert_no_error (error);
251   g_assert (mask != NULL);
252   g_object_unref (mask);
253
254   mask = g_inet_address_mask_new_from_string ("::", &error);
255   g_assert_no_error (error);
256   g_assert (mask != NULL);
257   g_object_unref (mask);
258
259   mask = g_inet_address_mask_new_from_string ("::/abc", &error);
260   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
261   g_assert (mask == NULL);
262   g_clear_error (&error);
263
264   mask = g_inet_address_mask_new_from_string ("127.0.0.1/128", &error);
265   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
266   g_assert (mask == NULL);
267   g_clear_error (&error);
268 }
269
270 static void
271 test_mask_property (void)
272 {
273   GInetAddressMask *mask;
274   GInetAddress *addr;
275   GSocketFamily family;
276   guint len;
277
278   addr = g_inet_address_new_from_string ("fe80::");
279   mask = g_inet_address_mask_new_from_string ("fe80::/10", NULL);
280   g_assert (g_inet_address_mask_get_family (mask) == G_SOCKET_FAMILY_IPV6);
281   g_assert (g_inet_address_equal (addr, g_inet_address_mask_get_address (mask)));
282   g_assert (g_inet_address_mask_get_length (mask) == 10);
283   g_object_unref (addr);
284
285   g_object_get (mask,
286                 "family", &family,
287                 "address", &addr,
288                 "length", &len,
289                 NULL);
290   g_assert (family == G_SOCKET_FAMILY_IPV6);
291   g_assert (addr != NULL);
292   g_assert (len == 10);
293   g_object_unref (addr);
294
295   g_object_unref (mask);
296 }
297
298 static void
299 test_mask_equal (void)
300 {
301   GInetAddressMask *mask;
302   GInetAddressMask *mask2;
303   gchar *str;
304
305   mask = g_inet_address_mask_new_from_string ("fe80:0:0::/10", NULL);
306   str = g_inet_address_mask_to_string (mask);
307   g_assert_cmpstr (str, ==, "fe80::/10");
308   mask2 = g_inet_address_mask_new_from_string (str, NULL);
309   g_assert (g_inet_address_mask_equal (mask, mask2));
310   g_object_unref (mask2);
311   g_free (str);
312
313   mask2 = g_inet_address_mask_new_from_string ("fe80::/12", NULL);
314   g_assert (!g_inet_address_mask_equal (mask, mask2));
315   g_object_unref (mask2);
316
317   mask2 = g_inet_address_mask_new_from_string ("ff80::/10", NULL);
318   g_assert (!g_inet_address_mask_equal (mask, mask2));
319   g_object_unref (mask2);
320
321   g_object_unref (mask);
322 }
323
324 static void
325 test_mask_match (void)
326 {
327   GInetAddressMask *mask;
328   GInetAddress *addr;
329
330   mask = g_inet_address_mask_new_from_string ("1.2.0.0/16", NULL);
331
332   addr = g_inet_address_new_from_string ("1.2.0.0");
333   g_assert (g_inet_address_mask_matches (mask, addr));
334   g_object_unref (addr);
335   addr = g_inet_address_new_from_string ("1.2.3.4");
336   g_assert (g_inet_address_mask_matches (mask, addr));
337   g_object_unref (addr);
338   addr = g_inet_address_new_from_string ("1.3.1.1");
339   g_assert (!g_inet_address_mask_matches (mask, addr));
340   g_object_unref (addr);
341
342   g_object_unref (mask);
343
344   mask = g_inet_address_mask_new_from_string ("1.2.0.0/24", NULL);
345
346   addr = g_inet_address_new_from_string ("1.2.0.0");
347   g_assert (g_inet_address_mask_matches (mask, addr));
348   g_object_unref (addr);
349   addr = g_inet_address_new_from_string ("1.2.3.4");
350   g_assert (!g_inet_address_mask_matches (mask, addr));
351   g_object_unref (addr);
352   addr = g_inet_address_new_from_string ("1.2.0.24");
353   g_assert (g_inet_address_mask_matches (mask, addr));
354   g_object_unref (addr);
355
356   g_object_unref (mask);
357
358 }
359
360 int
361 main (int argc, char *argv[])
362 {
363   g_test_init (&argc, &argv, NULL);
364
365   g_test_add_func ("/inet-address/parse", test_parse);
366   g_test_add_func ("/inet-address/any", test_any);
367   g_test_add_func ("/inet-address/loopback", test_loopback);
368   g_test_add_func ("/inet-address/bytes", test_bytes);
369   g_test_add_func ("/inet-address/property", test_property);
370   g_test_add_func ("/socket-address/basic", test_socket_address);
371   g_test_add_func ("/address-mask/parse", test_mask_parse);
372   g_test_add_func ("/address-mask/property", test_mask_property);
373   g_test_add_func ("/address-mask/equal", test_mask_equal);
374   g_test_add_func ("/address-mask/match", test_mask_match);
375
376   return g_test_run ();
377 }