Add some more cases to the app-id unit tests
[glib.git] / gio / tests / network-address.c
blob622af17b15a4f5bda0b686d897ec0a99cb9586b0
1 #include "config.h"
3 #include <gio/gio.h>
4 #include <gio/gnetworking.h>
6 static void
7 test_basic (void)
9 GNetworkAddress *address;
10 guint port;
11 gchar *hostname;
12 gchar *scheme;
14 address = (GNetworkAddress*)g_network_address_new ("www.gnome.org", 8080);
16 g_assert_cmpstr (g_network_address_get_hostname (address), ==, "www.gnome.org");
17 g_assert_cmpint (g_network_address_get_port (address), ==, 8080);
19 g_object_get (address, "hostname", &hostname, "port", &port, "scheme", &scheme, NULL);
20 g_assert_cmpstr (hostname, ==, "www.gnome.org");
21 g_assert_cmpint (port, ==, 8080);
22 g_assert (scheme == NULL);
23 g_free (hostname);
25 g_object_unref (address);
28 typedef struct {
29 const gchar *input;
30 const gchar *scheme;
31 const gchar *hostname;
32 guint16 port;
33 gint error_code;
34 } ParseTest;
36 static ParseTest uri_tests[] = {
37 { "http://www.gnome.org:2020/start", "http", "www.gnome.org", 2020, -1 },
38 { "ftp://joe~:(*)%46@ftp.gnome.org:2020/start", "ftp", "ftp.gnome.org", 2020, -1 },
39 { "ftp://[fec0::abcd]/start", "ftp", "fec0::abcd", 8080, -1 },
40 { "ftp://[fec0::abcd]:999/start", "ftp", "fec0::abcd", 999, -1 },
41 { "ftp://joe%x-@ftp.gnome.org:2020/start", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
42 { "http://[fec0::abcd%em1]/start", "http", "fec0::abcd%em1", 8080, -1 },
43 { "http://[fec0::abcd%25em1]/start", "http", "fec0::abcd%em1", 8080, -1 },
44 { "http://[fec0::abcd%10]/start", "http", "fec0::abcd%10", 8080, -1 },
45 { "http://[fec0::abcd%25em%31]/start", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
46 { "ftp://ftp.gnome.org/start?foo=bar@baz", "ftp", "ftp.gnome.org", 8080, -1 }
49 static void
50 test_parse_uri (gconstpointer d)
52 const ParseTest *test = d;
53 GNetworkAddress *address;
54 GError *error;
56 error = NULL;
57 address = (GNetworkAddress*)g_network_address_parse_uri (test->input, 8080, &error);
59 if (address)
61 g_assert_cmpstr (g_network_address_get_scheme (address), ==, test->scheme);
62 g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
63 g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
64 g_assert_no_error (error);
66 else
67 g_assert_error (error, G_IO_ERROR, test->error_code);
69 if (address)
70 g_object_unref (address);
71 if (error)
72 g_error_free (error);
75 static ParseTest host_tests[] =
77 { "www.gnome.org", NULL, "www.gnome.org", 1234, -1 },
78 { "www.gnome.org:8080", NULL, "www.gnome.org", 8080, -1 },
79 { "[2001:db8::1]", NULL, "2001:db8::1", 1234, -1 },
80 { "[2001:db8::1]:888", NULL, "2001:db8::1", 888, -1 },
81 { "[2001:db8::1%em1]", NULL, "2001:db8::1%em1", 1234, -1 },
82 { "[hostname", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
83 { "[hostnam]e", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
84 { "hostname:", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
85 { "hostname:-1", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
86 { "hostname:9999999", NULL, NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
89 static void
90 test_parse_host (gconstpointer d)
92 const ParseTest *test = d;
93 GNetworkAddress *address;
94 GError *error;
96 error = NULL;
97 address = (GNetworkAddress*)g_network_address_parse (test->input, 1234, &error);
99 if (address)
101 g_assert_null (g_network_address_get_scheme (address));
102 g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
103 g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
104 g_assert_no_error (error);
106 else
108 g_assert_error (error, G_IO_ERROR, test->error_code);
111 if (address)
112 g_object_unref (address);
113 if (error)
114 g_error_free (error);
117 typedef struct {
118 const gchar *input;
119 gboolean valid_parse, valid_resolve, valid_ip;
120 } ResolveTest;
122 static ResolveTest address_tests[] = {
123 { "192.168.1.2", TRUE, TRUE, TRUE },
124 { "fe80::42", TRUE, TRUE, TRUE },
126 /* GResolver accepts this by ignoring the scope ID. This was not
127 * intentional, but it's best to not "fix" it at this point.
129 { "fe80::42%1", TRUE, TRUE, FALSE },
131 /* g_network_address_parse() accepts these, but they are not
132 * (just) IP addresses.
134 { "192.168.1.2:80", TRUE, FALSE, FALSE },
135 { "[fe80::42]", TRUE, FALSE, FALSE },
136 { "[fe80::42]:80", TRUE, FALSE, FALSE },
138 /* These should not be considered IP addresses by anyone. */
139 { "192.168.258", FALSE, FALSE, FALSE },
140 { "192.11010306", FALSE, FALSE, FALSE },
141 { "3232235778", FALSE, FALSE, FALSE },
142 { "0300.0250.0001.0001", FALSE, FALSE, FALSE },
143 { "0xC0.0xA8.0x01.0x02", FALSE, FALSE, FALSE },
144 { "0xc0.0xa8.0x01.0x02", FALSE, FALSE, FALSE },
145 { "0xc0a80102", FALSE, FALSE, FALSE }
148 static void
149 test_resolve_address (gconstpointer d)
151 const ResolveTest *test = d;
152 GSocketConnectable *connectable;
153 GSocketAddressEnumerator *addr_enum;
154 GSocketAddress *addr;
155 GError *error = NULL;
157 g_assert_cmpint (test->valid_ip, ==, g_hostname_is_ip_address (test->input));
159 connectable = g_network_address_parse (test->input, 1234, &error);
160 g_assert_no_error (error);
162 addr_enum = g_socket_connectable_enumerate (connectable);
163 addr = g_socket_address_enumerator_next (addr_enum, NULL, &error);
164 g_object_unref (addr_enum);
165 g_object_unref (connectable);
167 if (addr)
169 g_assert_true (test->valid_parse);
170 g_assert_true (G_IS_INET_SOCKET_ADDRESS (addr));
171 g_object_unref (addr);
173 else
175 g_assert_false (test->valid_parse);
176 g_assert_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND);
177 g_error_free (error);
178 return;
182 /* Technically this should be in a GResolver test program, but we don't
183 * have one of those since it's mostly impossible to test programmatically.
184 * So it goes here so it can share the tests.
186 static void
187 test_resolve_address_gresolver (gconstpointer d)
189 const ResolveTest *test = d;
190 GResolver *resolver;
191 GList *addrs;
192 GInetAddress *iaddr;
193 GError *error = NULL;
195 resolver = g_resolver_get_default ();
196 addrs = g_resolver_lookup_by_name (resolver, test->input, NULL, &error);
197 g_object_unref (resolver);
199 if (addrs)
201 g_assert_true (test->valid_resolve);
202 g_assert_cmpint (g_list_length (addrs), ==, 1);
204 iaddr = addrs->data;
205 g_assert_true (G_IS_INET_ADDRESS (iaddr));
207 g_object_unref (iaddr);
208 g_list_free (addrs);
210 else
212 g_assert_false (test->valid_resolve);
214 if (!test->valid_parse)
216 /* GResolver should have rejected the address internally, in
217 * which case we're guaranteed to get G_RESOLVER_ERROR_NOT_FOUND.
219 g_assert_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND);
221 else
223 /* If GResolver didn't reject the string itself, then we
224 * might have attempted to send it over the network. If that
225 * attempt succeeded, we'd get back NOT_FOUND, but if
226 * there's no network available we might have gotten some
227 * other error instead.
231 g_error_free (error);
232 return;
236 #define SCOPE_ID_TEST_ADDR "fe80::42"
237 #define SCOPE_ID_TEST_PORT 99
239 #if defined (HAVE_IF_INDEXTONAME) && defined (HAVE_IF_NAMETOINDEX)
240 static char SCOPE_ID_TEST_IFNAME[IF_NAMESIZE];
241 static int SCOPE_ID_TEST_INDEX;
242 #else
243 #define SCOPE_ID_TEST_IFNAME "1"
244 #define SCOPE_ID_TEST_INDEX 1
245 #endif
247 static void
248 find_ifname_and_index (void)
250 if (SCOPE_ID_TEST_INDEX != 0)
251 return;
253 #if defined (HAVE_IF_INDEXTONAME) && defined (HAVE_IF_NAMETOINDEX)
254 SCOPE_ID_TEST_INDEX = if_nametoindex ("lo");
255 if (SCOPE_ID_TEST_INDEX != 0)
257 g_strlcpy (SCOPE_ID_TEST_IFNAME, "lo", sizeof (SCOPE_ID_TEST_IFNAME));
258 return;
261 for (SCOPE_ID_TEST_INDEX = 1; SCOPE_ID_TEST_INDEX < 1024; SCOPE_ID_TEST_INDEX++) {
262 if (if_indextoname (SCOPE_ID_TEST_INDEX, SCOPE_ID_TEST_IFNAME))
263 break;
265 g_assert_cmpstr (SCOPE_ID_TEST_IFNAME, !=, "");
266 #endif
269 static void
270 test_scope_id (GSocketConnectable *addr)
272 GSocketAddressEnumerator *addr_enum;
273 GSocketAddress *saddr;
274 GInetSocketAddress *isaddr;
275 GInetAddress *iaddr;
276 char *tostring;
277 GError *error = NULL;
279 addr_enum = g_socket_connectable_enumerate (addr);
280 saddr = g_socket_address_enumerator_next (addr_enum, NULL, &error);
281 g_assert_no_error (error);
283 g_assert (saddr != NULL);
284 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
286 isaddr = G_INET_SOCKET_ADDRESS (saddr);
287 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, SCOPE_ID_TEST_INDEX);
288 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, SCOPE_ID_TEST_PORT);
290 iaddr = g_inet_socket_address_get_address (isaddr);
291 tostring = g_inet_address_to_string (iaddr);
292 g_assert_cmpstr (tostring, ==, SCOPE_ID_TEST_ADDR);
293 g_free (tostring);
295 g_object_unref (saddr);
296 saddr = g_socket_address_enumerator_next (addr_enum, NULL, &error);
297 g_assert_no_error (error);
298 g_assert (saddr == NULL);
300 g_object_unref (addr_enum);
303 static void
304 test_host_scope_id (void)
306 GSocketConnectable *addr;
307 char *str;
309 find_ifname_and_index ();
311 str = g_strdup_printf ("%s%%%s", SCOPE_ID_TEST_ADDR, SCOPE_ID_TEST_IFNAME);
312 addr = g_network_address_new (str, SCOPE_ID_TEST_PORT);
313 g_free (str);
315 test_scope_id (addr);
316 g_object_unref (addr);
319 static void
320 test_uri_scope_id (void)
322 GSocketConnectable *addr;
323 char *uri;
324 GError *error = NULL;
326 find_ifname_and_index ();
328 uri = g_strdup_printf ("http://[%s%%%s]:%d/foo",
329 SCOPE_ID_TEST_ADDR,
330 SCOPE_ID_TEST_IFNAME,
331 SCOPE_ID_TEST_PORT);
332 addr = g_network_address_parse_uri (uri, 0, &error);
333 g_free (uri);
334 g_assert_no_error (error);
336 test_scope_id (addr);
337 g_object_unref (addr);
339 uri = g_strdup_printf ("http://[%s%%25%s]:%d/foo",
340 SCOPE_ID_TEST_ADDR,
341 SCOPE_ID_TEST_IFNAME,
342 SCOPE_ID_TEST_PORT);
343 addr = g_network_address_parse_uri (uri, 0, &error);
344 g_free (uri);
345 g_assert_no_error (error);
347 test_scope_id (addr);
348 g_object_unref (addr);
351 static void
352 test_loopback_basic (void)
354 GNetworkAddress *addr; /* owned */
356 addr = G_NETWORK_ADDRESS (g_network_address_new_loopback (666));
358 /* Test basic properties. */
359 g_assert_cmpstr (g_network_address_get_hostname (addr), ==, "localhost");
360 g_assert_cmpuint (g_network_address_get_port (addr), ==, 666);
361 g_assert_null (g_network_address_get_scheme (addr));
363 g_object_unref (addr);
366 static void
367 assert_socket_address_matches (GSocketAddress *a,
368 const gchar *expected_address,
369 guint16 expected_port)
371 GInetSocketAddress *sa;
372 gchar *str; /* owned */
374 g_assert (G_IS_INET_SOCKET_ADDRESS (a));
376 sa = G_INET_SOCKET_ADDRESS (a);
377 g_assert_cmpint (g_inet_socket_address_get_port (sa), ==, expected_port);
379 str = g_inet_address_to_string (g_inet_socket_address_get_address (sa));
380 g_assert_cmpstr (str, ==, expected_address);
381 g_free (str);
384 static void
385 test_loopback_sync (void)
387 GSocketConnectable *addr; /* owned */
388 GSocketAddressEnumerator *enumerator; /* owned */
389 GSocketAddress *a; /* owned */
390 GError *error = NULL;
392 addr = g_network_address_new_loopback (616);
393 enumerator = g_socket_connectable_enumerate (addr);
395 /* IPv6 address. */
396 a = g_socket_address_enumerator_next (enumerator, NULL, &error);
397 g_assert_no_error (error);
398 assert_socket_address_matches (a, "::1", 616);
399 g_object_unref (a);
401 /* IPv4 address. */
402 a = g_socket_address_enumerator_next (enumerator, NULL, &error);
403 g_assert_no_error (error);
404 assert_socket_address_matches (a, "127.0.0.1", 616);
405 g_object_unref (a);
407 /* End of results. */
408 g_assert_null (g_socket_address_enumerator_next (enumerator, NULL, &error));
409 g_assert_no_error (error);
411 g_object_unref (enumerator);
412 g_object_unref (addr);
415 typedef struct {
416 GList/*<owned GSocketAddress> */ *addrs; /* owned */
417 GMainLoop *loop; /* owned */
418 } AsyncData;
420 static void
421 got_addr (GObject *source_object, GAsyncResult *result, gpointer user_data)
423 GSocketAddressEnumerator *enumerator;
424 AsyncData *data;
425 GSocketAddress *a; /* owned */
426 GError *error = NULL;
428 enumerator = G_SOCKET_ADDRESS_ENUMERATOR (source_object);
429 data = user_data;
431 a = g_socket_address_enumerator_next_finish (enumerator, result, &error);
432 g_assert_no_error (error);
434 if (a == NULL)
436 /* End of results. */
437 data->addrs = g_list_reverse (data->addrs);
438 g_main_loop_quit (data->loop);
440 else
442 g_assert (G_IS_INET_SOCKET_ADDRESS (a));
443 data->addrs = g_list_prepend (data->addrs, a);
445 g_socket_address_enumerator_next_async (enumerator, NULL,
446 got_addr, user_data);
450 static void
451 test_loopback_async (void)
453 GSocketConnectable *addr; /* owned */
454 GSocketAddressEnumerator *enumerator; /* owned */
455 AsyncData data = { 0, };
457 addr = g_network_address_new_loopback (610);
458 enumerator = g_socket_connectable_enumerate (addr);
460 /* Get all the addresses. */
461 data.addrs = NULL;
462 data.loop = g_main_loop_new (NULL, FALSE);
464 g_socket_address_enumerator_next_async (enumerator, NULL, got_addr, &data);
466 g_main_loop_run (data.loop);
467 g_main_loop_unref (data.loop);
469 /* Check results. */
470 g_assert_cmpuint (g_list_length (data.addrs), ==, 2);
471 assert_socket_address_matches (data.addrs->data, "::1", 610);
472 assert_socket_address_matches (data.addrs->next->data, "127.0.0.1", 610);
474 g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
476 g_object_unref (enumerator);
477 g_object_unref (addr);
480 static void
481 test_to_string (void)
483 GSocketConnectable *addr = NULL;
484 gchar *str = NULL;
485 GError *error = NULL;
487 /* Without port. */
488 addr = g_network_address_new ("some-hostname", 0);
489 str = g_socket_connectable_to_string (addr);
490 g_assert_cmpstr (str, ==, "some-hostname");
491 g_free (str);
492 g_object_unref (addr);
494 /* With port. */
495 addr = g_network_address_new ("some-hostname", 123);
496 str = g_socket_connectable_to_string (addr);
497 g_assert_cmpstr (str, ==, "some-hostname:123");
498 g_free (str);
499 g_object_unref (addr);
501 /* With scheme and port. */
502 addr = g_network_address_parse_uri ("http://some-hostname:123", 80, &error);
503 g_assert_no_error (error);
504 str = g_socket_connectable_to_string (addr);
505 g_assert_cmpstr (str, ==, "http:some-hostname:123");
506 g_free (str);
507 g_object_unref (addr);
509 /* Loopback. */
510 addr = g_network_address_new ("localhost", 456);
511 str = g_socket_connectable_to_string (addr);
512 g_assert_cmpstr (str, ==, "localhost:456");
513 g_free (str);
514 g_object_unref (addr);
518 main (int argc, char *argv[])
520 gint i;
521 gchar *path;
523 g_test_init (&argc, &argv, NULL);
525 g_test_add_func ("/network-address/basic", test_basic);
527 for (i = 0; i < G_N_ELEMENTS (host_tests); i++)
529 path = g_strdup_printf ("/network-address/parse-host/%d", i);
530 g_test_add_data_func (path, &host_tests[i], test_parse_host);
531 g_free (path);
534 for (i = 0; i < G_N_ELEMENTS (uri_tests); i++)
536 path = g_strdup_printf ("/network-address/parse-uri/%d", i);
537 g_test_add_data_func (path, &uri_tests[i], test_parse_uri);
538 g_free (path);
541 for (i = 0; i < G_N_ELEMENTS (address_tests); i++)
543 path = g_strdup_printf ("/network-address/resolve-address/%d", i);
544 g_test_add_data_func (path, &address_tests[i], test_resolve_address);
545 g_free (path);
548 for (i = 0; i < G_N_ELEMENTS (address_tests); i++)
550 path = g_strdup_printf ("/gresolver/resolve-address/%d", i);
551 g_test_add_data_func (path, &address_tests[i], test_resolve_address_gresolver);
552 g_free (path);
555 g_test_add_func ("/network-address/scope-id", test_host_scope_id);
556 g_test_add_func ("/network-address/uri-scope-id", test_uri_scope_id);
557 g_test_add_func ("/network-address/loopback/basic", test_loopback_basic);
558 g_test_add_func ("/network-address/loopback/sync", test_loopback_sync);
559 g_test_add_func ("/network-address/loopback/async", test_loopback_async);
560 g_test_add_func ("/network-address/to-string", test_to_string);
562 return g_test_run ();