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 /* 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
}
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
);
169 g_assert_true (test
->valid_parse
);
170 g_assert_true (G_IS_INET_SOCKET_ADDRESS (addr
));
171 g_object_unref (addr
);
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
);
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.
187 test_resolve_address_gresolver (gconstpointer d
)
189 const ResolveTest
*test
= d
;
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
);
201 g_assert_true (test
->valid_resolve
);
202 g_assert_cmpint (g_list_length (addrs
), ==, 1);
205 g_assert_true (G_IS_INET_ADDRESS (iaddr
));
207 g_object_unref (iaddr
);
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
);
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
);
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
;
243 #define SCOPE_ID_TEST_IFNAME "1"
244 #define SCOPE_ID_TEST_INDEX 1
248 find_ifname_and_index (void)
250 if (SCOPE_ID_TEST_INDEX
!= 0)
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
));
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
))
265 g_assert_cmpstr (SCOPE_ID_TEST_IFNAME
, !=, "");
270 test_scope_id (GSocketConnectable
*addr
)
272 GSocketAddressEnumerator
*addr_enum
;
273 GSocketAddress
*saddr
;
274 GInetSocketAddress
*isaddr
;
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
);
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
);
304 test_host_scope_id (void)
306 GSocketConnectable
*addr
;
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
);
315 test_scope_id (addr
);
316 g_object_unref (addr
);
320 test_uri_scope_id (void)
322 GSocketConnectable
*addr
;
324 GError
*error
= NULL
;
326 find_ifname_and_index ();
328 uri
= g_strdup_printf ("http://[%s%%%s]:%d/foo",
330 SCOPE_ID_TEST_IFNAME
,
332 addr
= g_network_address_parse_uri (uri
, 0, &error
);
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",
341 SCOPE_ID_TEST_IFNAME
,
343 addr
= g_network_address_parse_uri (uri
, 0, &error
);
345 g_assert_no_error (error
);
347 test_scope_id (addr
);
348 g_object_unref (addr
);
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
);
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
);
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
);
396 a
= g_socket_address_enumerator_next (enumerator
, NULL
, &error
);
397 g_assert_no_error (error
);
398 assert_socket_address_matches (a
, "::1", 616);
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);
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
);
416 GList
/*<owned GSocketAddress> */ *addrs
; /* owned */
417 GMainLoop
*loop
; /* owned */
421 got_addr (GObject
*source_object
, GAsyncResult
*result
, gpointer user_data
)
423 GSocketAddressEnumerator
*enumerator
;
425 GSocketAddress
*a
; /* owned */
426 GError
*error
= NULL
;
428 enumerator
= G_SOCKET_ADDRESS_ENUMERATOR (source_object
);
431 a
= g_socket_address_enumerator_next_finish (enumerator
, result
, &error
);
432 g_assert_no_error (error
);
436 /* End of results. */
437 data
->addrs
= g_list_reverse (data
->addrs
);
438 g_main_loop_quit (data
->loop
);
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
);
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. */
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
);
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
);
481 test_to_string (void)
483 GSocketConnectable
*addr
= NULL
;
485 GError
*error
= NULL
;
488 addr
= g_network_address_new ("some-hostname", 0);
489 str
= g_socket_connectable_to_string (addr
);
490 g_assert_cmpstr (str
, ==, "some-hostname");
492 g_object_unref (addr
);
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");
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");
507 g_object_unref (addr
);
510 addr
= g_network_address_new ("localhost", 456);
511 str
= g_socket_connectable_to_string (addr
);
512 g_assert_cmpstr (str
, ==, "localhost:456");
514 g_object_unref (addr
);
518 main (int argc
, char *argv
[])
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
);
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
);
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
);
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
);
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 ();