4 #include <gio/gnetworking.h>
9 GNetworkAddress
*address
;
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
);
25 g_object_unref (address
);
31 const gchar
*hostname
;
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 }
50 test_parse_uri (gconstpointer d
)
52 const ParseTest
*test
= d
;
53 GNetworkAddress
*address
;
57 address
= (GNetworkAddress
*)g_network_address_parse_uri (test
->input
, 8080, &error
);
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
);
67 g_assert_error (error
, G_IO_ERROR
, test
->error_code
);
70 g_object_unref (address
);
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
}
90 test_parse_host (gconstpointer d
)
92 const ParseTest
*test
= d
;
93 GNetworkAddress
*address
;
97 address
= (GNetworkAddress
*)g_network_address_parse (test
->input
, 1234, &error
);
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
);
108 g_assert_error (error
, G_IO_ERROR
, test
->error_code
);
112 g_object_unref (address
);
114 g_error_free (error
);
119 gboolean valid_parse
, valid_resolve
, valid_ip
;
122 static ResolveTest address_tests
[] = {
123 { "192.168.1.2", TRUE
, TRUE
, TRUE
},
124 { "fe80::42", TRUE
, TRUE
, TRUE
},
126 /* g_network_address_parse() accepts these, but they are not
127 * (just) IP addresses.
129 { "192.168.1.2:80", TRUE
, FALSE
, FALSE
},
130 { "[fe80::42]", TRUE
, FALSE
, FALSE
},
131 { "[fe80::42]:80", TRUE
, FALSE
, FALSE
},
133 /* These should not be considered IP addresses by anyone. */
134 { "192.168.258", FALSE
, FALSE
, FALSE
},
135 { "192.11010306", FALSE
, FALSE
, FALSE
},
136 { "3232235778", FALSE
, FALSE
, FALSE
},
137 { "0300.0250.0001.0001", FALSE
, FALSE
, FALSE
},
138 { "0xC0.0xA8.0x01.0x02", FALSE
, FALSE
, FALSE
},
139 { "0xc0.0xa8.0x01.0x02", FALSE
, FALSE
, FALSE
},
140 { "0xc0a80102", FALSE
, FALSE
, FALSE
}
144 test_resolve_address (gconstpointer d
)
146 const ResolveTest
*test
= d
;
147 GSocketConnectable
*connectable
;
148 GSocketAddressEnumerator
*addr_enum
;
149 GSocketAddress
*addr
;
150 GError
*error
= NULL
;
152 g_test_message ("Input: %s", test
->input
);
154 g_assert_cmpint (test
->valid_ip
, ==, g_hostname_is_ip_address (test
->input
));
156 connectable
= g_network_address_parse (test
->input
, 1234, &error
);
157 g_assert_no_error (error
);
159 addr_enum
= g_socket_connectable_enumerate (connectable
);
160 addr
= g_socket_address_enumerator_next (addr_enum
, NULL
, &error
);
161 g_object_unref (addr_enum
);
162 g_object_unref (connectable
);
166 g_assert_true (test
->valid_parse
);
167 g_assert_true (G_IS_INET_SOCKET_ADDRESS (addr
));
168 g_object_unref (addr
);
172 g_assert_false (test
->valid_parse
);
173 g_assert_error (error
, G_RESOLVER_ERROR
, G_RESOLVER_ERROR_NOT_FOUND
);
174 g_error_free (error
);
179 /* Technically this should be in a GResolver test program, but we don't
180 * have one of those since it's mostly impossible to test programmatically.
181 * So it goes here so it can share the tests.
184 test_resolve_address_gresolver (gconstpointer d
)
186 const ResolveTest
*test
= d
;
190 GError
*error
= NULL
;
192 g_test_message ("Input: %s", test
->input
);
194 resolver
= g_resolver_get_default ();
195 addrs
= g_resolver_lookup_by_name (resolver
, test
->input
, NULL
, &error
);
196 g_object_unref (resolver
);
200 g_assert_true (test
->valid_resolve
);
201 g_assert_cmpint (g_list_length (addrs
), ==, 1);
204 g_assert_true (G_IS_INET_ADDRESS (iaddr
));
206 g_object_unref (iaddr
);
211 g_assert_nonnull (error
);
212 g_test_message ("Error: %s", error
->message
);
213 g_assert_false (test
->valid_resolve
);
215 if (!test
->valid_parse
)
217 /* GResolver should have rejected the address internally, in
218 * which case we're guaranteed to get G_RESOLVER_ERROR_NOT_FOUND.
220 g_assert_error (error
, G_RESOLVER_ERROR
, G_RESOLVER_ERROR_NOT_FOUND
);
224 /* If GResolver didn't reject the string itself, then we
225 * might have attempted to send it over the network. If that
226 * attempt succeeded, we'd get back NOT_FOUND, but if
227 * there's no network available we might have gotten some
228 * other error instead.
232 g_error_free (error
);
237 #define SCOPE_ID_TEST_ADDR "fe80::42"
238 #define SCOPE_ID_TEST_PORT 99
240 #if defined (HAVE_IF_INDEXTONAME) && defined (HAVE_IF_NAMETOINDEX)
241 static char SCOPE_ID_TEST_IFNAME
[IF_NAMESIZE
];
242 static int SCOPE_ID_TEST_INDEX
;
244 #define SCOPE_ID_TEST_IFNAME "1"
245 #define SCOPE_ID_TEST_INDEX 1
249 find_ifname_and_index (void)
251 if (SCOPE_ID_TEST_INDEX
!= 0)
254 #if defined (HAVE_IF_INDEXTONAME) && defined (HAVE_IF_NAMETOINDEX)
255 SCOPE_ID_TEST_INDEX
= if_nametoindex ("lo");
256 if (SCOPE_ID_TEST_INDEX
!= 0)
258 g_strlcpy (SCOPE_ID_TEST_IFNAME
, "lo", sizeof (SCOPE_ID_TEST_IFNAME
));
262 for (SCOPE_ID_TEST_INDEX
= 1; SCOPE_ID_TEST_INDEX
< 1024; SCOPE_ID_TEST_INDEX
++) {
263 if (if_indextoname (SCOPE_ID_TEST_INDEX
, SCOPE_ID_TEST_IFNAME
))
266 g_assert_cmpstr (SCOPE_ID_TEST_IFNAME
, !=, "");
271 test_scope_id (GSocketConnectable
*addr
)
273 GSocketAddressEnumerator
*addr_enum
;
274 GSocketAddress
*saddr
;
275 GInetSocketAddress
*isaddr
;
278 GError
*error
= NULL
;
280 addr_enum
= g_socket_connectable_enumerate (addr
);
281 saddr
= g_socket_address_enumerator_next (addr_enum
, NULL
, &error
);
282 g_assert_no_error (error
);
284 g_assert (saddr
!= NULL
);
285 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr
));
287 isaddr
= G_INET_SOCKET_ADDRESS (saddr
);
288 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr
), ==, SCOPE_ID_TEST_INDEX
);
289 g_assert_cmpint (g_inet_socket_address_get_port (isaddr
), ==, SCOPE_ID_TEST_PORT
);
291 iaddr
= g_inet_socket_address_get_address (isaddr
);
292 tostring
= g_inet_address_to_string (iaddr
);
293 g_assert_cmpstr (tostring
, ==, SCOPE_ID_TEST_ADDR
);
296 g_object_unref (saddr
);
297 saddr
= g_socket_address_enumerator_next (addr_enum
, NULL
, &error
);
298 g_assert_no_error (error
);
299 g_assert (saddr
== NULL
);
301 g_object_unref (addr_enum
);
305 test_host_scope_id (void)
307 GSocketConnectable
*addr
;
310 find_ifname_and_index ();
312 str
= g_strdup_printf ("%s%%%s", SCOPE_ID_TEST_ADDR
, SCOPE_ID_TEST_IFNAME
);
313 addr
= g_network_address_new (str
, SCOPE_ID_TEST_PORT
);
316 test_scope_id (addr
);
317 g_object_unref (addr
);
321 test_uri_scope_id (void)
323 GSocketConnectable
*addr
;
325 GError
*error
= NULL
;
327 find_ifname_and_index ();
329 uri
= g_strdup_printf ("http://[%s%%%s]:%d/foo",
331 SCOPE_ID_TEST_IFNAME
,
333 addr
= g_network_address_parse_uri (uri
, 0, &error
);
335 g_assert_no_error (error
);
337 test_scope_id (addr
);
338 g_object_unref (addr
);
340 uri
= g_strdup_printf ("http://[%s%%25%s]:%d/foo",
342 SCOPE_ID_TEST_IFNAME
,
344 addr
= g_network_address_parse_uri (uri
, 0, &error
);
346 g_assert_no_error (error
);
348 test_scope_id (addr
);
349 g_object_unref (addr
);
353 test_loopback_basic (void)
355 GNetworkAddress
*addr
; /* owned */
357 addr
= G_NETWORK_ADDRESS (g_network_address_new_loopback (666));
359 /* Test basic properties. */
360 g_assert_cmpstr (g_network_address_get_hostname (addr
), ==, "localhost");
361 g_assert_cmpuint (g_network_address_get_port (addr
), ==, 666);
362 g_assert_null (g_network_address_get_scheme (addr
));
364 g_object_unref (addr
);
368 assert_socket_address_matches (GSocketAddress
*a
,
369 const gchar
*expected_address
,
370 guint16 expected_port
)
372 GInetSocketAddress
*sa
;
373 gchar
*str
; /* owned */
375 g_assert (G_IS_INET_SOCKET_ADDRESS (a
));
377 sa
= G_INET_SOCKET_ADDRESS (a
);
378 g_assert_cmpint (g_inet_socket_address_get_port (sa
), ==, expected_port
);
380 str
= g_inet_address_to_string (g_inet_socket_address_get_address (sa
));
381 g_assert_cmpstr (str
, ==, expected_address
);
386 test_loopback_sync (void)
388 GSocketConnectable
*addr
; /* owned */
389 GSocketAddressEnumerator
*enumerator
; /* owned */
390 GSocketAddress
*a
; /* owned */
391 GError
*error
= NULL
;
393 addr
= g_network_address_new_loopback (616);
394 enumerator
= g_socket_connectable_enumerate (addr
);
397 a
= g_socket_address_enumerator_next (enumerator
, NULL
, &error
);
398 g_assert_no_error (error
);
399 assert_socket_address_matches (a
, "::1", 616);
403 a
= g_socket_address_enumerator_next (enumerator
, NULL
, &error
);
404 g_assert_no_error (error
);
405 assert_socket_address_matches (a
, "127.0.0.1", 616);
408 /* End of results. */
409 g_assert_null (g_socket_address_enumerator_next (enumerator
, NULL
, &error
));
410 g_assert_no_error (error
);
412 g_object_unref (enumerator
);
413 g_object_unref (addr
);
417 GList
/*<owned GSocketAddress> */ *addrs
; /* owned */
418 GMainLoop
*loop
; /* owned */
422 got_addr (GObject
*source_object
, GAsyncResult
*result
, gpointer user_data
)
424 GSocketAddressEnumerator
*enumerator
;
426 GSocketAddress
*a
; /* owned */
427 GError
*error
= NULL
;
429 enumerator
= G_SOCKET_ADDRESS_ENUMERATOR (source_object
);
432 a
= g_socket_address_enumerator_next_finish (enumerator
, result
, &error
);
433 g_assert_no_error (error
);
437 /* End of results. */
438 data
->addrs
= g_list_reverse (data
->addrs
);
439 g_main_loop_quit (data
->loop
);
443 g_assert (G_IS_INET_SOCKET_ADDRESS (a
));
444 data
->addrs
= g_list_prepend (data
->addrs
, a
);
446 g_socket_address_enumerator_next_async (enumerator
, NULL
,
447 got_addr
, user_data
);
452 test_loopback_async (void)
454 GSocketConnectable
*addr
; /* owned */
455 GSocketAddressEnumerator
*enumerator
; /* owned */
456 AsyncData data
= { 0, };
458 addr
= g_network_address_new_loopback (610);
459 enumerator
= g_socket_connectable_enumerate (addr
);
461 /* Get all the addresses. */
463 data
.loop
= g_main_loop_new (NULL
, FALSE
);
465 g_socket_address_enumerator_next_async (enumerator
, NULL
, got_addr
, &data
);
467 g_main_loop_run (data
.loop
);
468 g_main_loop_unref (data
.loop
);
471 g_assert_cmpuint (g_list_length (data
.addrs
), ==, 2);
472 assert_socket_address_matches (data
.addrs
->data
, "::1", 610);
473 assert_socket_address_matches (data
.addrs
->next
->data
, "127.0.0.1", 610);
475 g_list_free_full (data
.addrs
, (GDestroyNotify
) g_object_unref
);
477 g_object_unref (enumerator
);
478 g_object_unref (addr
);
482 test_to_string (void)
484 GSocketConnectable
*addr
= NULL
;
486 GError
*error
= NULL
;
489 addr
= g_network_address_new ("some-hostname", 0);
490 str
= g_socket_connectable_to_string (addr
);
491 g_assert_cmpstr (str
, ==, "some-hostname");
493 g_object_unref (addr
);
496 addr
= g_network_address_new ("some-hostname", 123);
497 str
= g_socket_connectable_to_string (addr
);
498 g_assert_cmpstr (str
, ==, "some-hostname:123");
500 g_object_unref (addr
);
502 /* With scheme and port. */
503 addr
= g_network_address_parse_uri ("http://some-hostname:123", 80, &error
);
504 g_assert_no_error (error
);
505 str
= g_socket_connectable_to_string (addr
);
506 g_assert_cmpstr (str
, ==, "http:some-hostname:123");
508 g_object_unref (addr
);
511 addr
= g_network_address_new ("localhost", 456);
512 str
= g_socket_connectable_to_string (addr
);
513 g_assert_cmpstr (str
, ==, "localhost:456");
515 g_object_unref (addr
);
519 main (int argc
, char *argv
[])
524 g_test_init (&argc
, &argv
, NULL
);
526 g_test_add_func ("/network-address/basic", test_basic
);
528 for (i
= 0; i
< G_N_ELEMENTS (host_tests
); i
++)
530 path
= g_strdup_printf ("/network-address/parse-host/%d", i
);
531 g_test_add_data_func (path
, &host_tests
[i
], test_parse_host
);
535 for (i
= 0; i
< G_N_ELEMENTS (uri_tests
); i
++)
537 path
= g_strdup_printf ("/network-address/parse-uri/%d", i
);
538 g_test_add_data_func (path
, &uri_tests
[i
], test_parse_uri
);
542 for (i
= 0; i
< G_N_ELEMENTS (address_tests
); i
++)
544 path
= g_strdup_printf ("/network-address/resolve-address/%d", i
);
545 g_test_add_data_func (path
, &address_tests
[i
], test_resolve_address
);
549 for (i
= 0; i
< G_N_ELEMENTS (address_tests
); i
++)
551 path
= g_strdup_printf ("/gresolver/resolve-address/%d", i
);
552 g_test_add_data_func (path
, &address_tests
[i
], test_resolve_address_gresolver
);
556 g_test_add_func ("/network-address/scope-id", test_host_scope_id
);
557 g_test_add_func ("/network-address/uri-scope-id", test_uri_scope_id
);
558 g_test_add_func ("/network-address/loopback/basic", test_loopback_basic
);
559 g_test_add_func ("/network-address/loopback/sync", test_loopback_sync
);
560 g_test_add_func ("/network-address/loopback/async", test_loopback_async
);
561 g_test_add_func ("/network-address/to-string", test_to_string
);
563 return g_test_run ();