1 /* GLib testing framework examples and tests
3 * Copyright 2012 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_34
28 * We have an echo server, two proxy servers, two GProxy
29 * implementations, and a GProxyResolver implementation.
31 * The echo server runs at @server.server_addr (on
32 * @server.server_port).
34 * The two proxy servers, A and B, run on @proxy_a.port and
35 * @proxy_b.port, with @proxy_a.uri and @proxy_b.uri pointing to them.
36 * The "negotiation" with the two proxies is just sending the single
37 * letter "a" or "b" and receiving it back in uppercase; the proxy
38 * then connects to @server_addr.
40 * Proxy A supports "alpha://" URIs, and does not support hostname
41 * resolution, and Proxy B supports "beta://" URIs, and does support
42 * hostname resolution (but it just ignores the hostname and always
43 * connects to @server_addr anyway).
45 * The GProxyResolver (GTestProxyResolver) looks at its URI and
46 * returns [ "direct://" ] for "simple://" URIs, and [ proxy_a.uri,
47 * proxy_b.uri ] for other URIs.
52 gchar *supported_protocol;
56 GCancellable *cancellable;
60 GSocket *client_sock, *server_sock;
66 static ProxyData proxy_a, proxy_b;
70 GThread *server_thread;
71 GCancellable *cancellable;
72 GSocketAddress *server_addr;
76 static ServerData server;
78 static gchar **last_proxies;
80 static GSocketClient *client;
83 /**************************************/
84 /* Test GProxyResolver implementation */
85 /**************************************/
88 GObject parent_instance;
92 GObjectClass parent_class;
93 } GTestProxyResolverClass;
95 static void g_test_proxy_resolver_iface_init (GProxyResolverInterface *iface);
97 #define g_test_proxy_resolver_get_type _g_test_proxy_resolver_get_type
98 G_DEFINE_TYPE_WITH_CODE (GTestProxyResolver, g_test_proxy_resolver, G_TYPE_OBJECT,
99 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
100 g_test_proxy_resolver_iface_init)
101 g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
107 g_test_proxy_resolver_init (GTestProxyResolver *resolver)
112 g_test_proxy_resolver_is_supported (GProxyResolver *resolver)
118 g_test_proxy_resolver_lookup (GProxyResolver *resolver,
120 GCancellable *cancellable,
125 g_assert (last_proxies == NULL);
127 if (g_cancellable_set_error_if_cancelled (cancellable, error))
130 proxies = g_new (gchar *, 3);
132 if (!strncmp (uri, "simple://", 4))
134 proxies[0] = g_strdup ("direct://");
139 /* Proxy A can only deal with "alpha://" URIs, not
140 * "beta://", but we always return both URIs
141 * anyway so we can test error handling when the first
144 proxies[0] = g_strdup (proxy_a.uri);
145 proxies[1] = g_strdup (proxy_b.uri);
149 last_proxies = g_strdupv (proxies);
155 g_test_proxy_resolver_lookup_async (GProxyResolver *resolver,
157 GCancellable *cancellable,
158 GAsyncReadyCallback callback,
161 GError *error = NULL;
162 GSimpleAsyncResult *simple;
165 proxies = g_test_proxy_resolver_lookup (resolver, uri, cancellable, &error);
167 simple = g_simple_async_result_new (G_OBJECT (resolver),
169 g_test_proxy_resolver_lookup_async);
172 g_simple_async_result_take_error (simple, error);
174 g_simple_async_result_set_op_res_gpointer (simple, proxies, (GDestroyNotify) g_strfreev);
176 g_simple_async_result_complete_in_idle (simple);
177 g_object_unref (simple);
181 g_test_proxy_resolver_lookup_finish (GProxyResolver *resolver,
182 GAsyncResult *result,
185 if (G_IS_SIMPLE_ASYNC_RESULT (result))
187 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
190 if (g_simple_async_result_propagate_error (simple, error))
193 proxies = g_simple_async_result_get_op_res_gpointer (simple);
194 return g_strdupv (proxies);
201 g_test_proxy_resolver_class_init (GTestProxyResolverClass *resolver_class)
206 g_test_proxy_resolver_iface_init (GProxyResolverInterface *iface)
208 iface->is_supported = g_test_proxy_resolver_is_supported;
209 iface->lookup = g_test_proxy_resolver_lookup;
210 iface->lookup_async = g_test_proxy_resolver_lookup_async;
211 iface->lookup_finish = g_test_proxy_resolver_lookup_finish;
215 /****************************************/
216 /* Test proxy implementation base class */
217 /****************************************/
222 ProxyData *proxy_data;
226 GObjectClass parent_class;
229 #define g_proxy_base_get_type _g_proxy_base_get_type
230 G_DEFINE_ABSTRACT_TYPE (GProxyBase, g_proxy_base, G_TYPE_OBJECT)
233 g_proxy_base_init (GProxyBase *proxy)
238 g_proxy_base_connect (GProxy *proxy,
239 GIOStream *io_stream,
240 GProxyAddress *proxy_address,
241 GCancellable *cancellable,
244 ProxyData *data = ((GProxyBase *) proxy)->proxy_data;
245 const gchar *protocol;
246 GOutputStream *ostream;
247 GInputStream *istream;
250 g_assert_no_error (data->last_error);
252 protocol = g_proxy_address_get_destination_protocol (proxy_address);
253 if (strcmp (protocol, data->supported_protocol) != 0)
255 g_set_error_literal (&data->last_error,
256 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
257 "Unsupported protocol");
261 ostream = g_io_stream_get_output_stream (io_stream);
262 if (g_output_stream_write (ostream, data->proxy_command, 1, cancellable,
263 &data->last_error) != 1)
266 istream = g_io_stream_get_input_stream (io_stream);
267 if (g_input_stream_read (istream, &response, 1, cancellable,
268 &data->last_error) != 1)
271 if (response != g_ascii_toupper (*data->proxy_command))
273 g_set_error_literal (&data->last_error,
274 G_IO_ERROR, G_IO_ERROR_FAILED,
279 return g_object_ref (io_stream);
282 g_propagate_error (error, g_error_copy (data->last_error));
287 g_proxy_base_connect_async (GProxy *proxy,
288 GIOStream *io_stream,
289 GProxyAddress *proxy_address,
290 GCancellable *cancellable,
291 GAsyncReadyCallback callback,
294 GError *error = NULL;
295 GSimpleAsyncResult *simple;
296 GIOStream *proxy_io_stream;
298 simple = g_simple_async_result_new (G_OBJECT (proxy),
300 g_proxy_base_connect_async);
302 proxy_io_stream = g_proxy_connect (proxy, io_stream, proxy_address,
303 cancellable, &error);
306 g_simple_async_result_set_op_res_gpointer (simple, proxy_io_stream,
310 g_simple_async_result_take_error (simple, error);
311 g_simple_async_result_complete_in_idle (simple);
312 g_object_unref (simple);
316 g_proxy_base_connect_finish (GProxy *proxy,
317 GAsyncResult *result,
320 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
322 if (g_simple_async_result_propagate_error (simple, error))
325 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
329 g_proxy_base_class_init (GProxyBaseClass *class)
334 /********************************************/
335 /* Test proxy implementation #1 ("Proxy A") */
336 /********************************************/
338 typedef GProxyBase GProxyA;
339 typedef GProxyBaseClass GProxyAClass;
341 static void g_proxy_a_iface_init (GProxyInterface *proxy_iface);
343 #define g_proxy_a_get_type _g_proxy_a_get_type
344 G_DEFINE_TYPE_WITH_CODE (GProxyA, g_proxy_a, g_proxy_base_get_type (),
345 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
346 g_proxy_a_iface_init)
347 g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
353 g_proxy_a_init (GProxyA *proxy)
355 ((GProxyBase *) proxy)->proxy_data = &proxy_a;
359 g_proxy_a_supports_hostname (GProxy *proxy)
365 g_proxy_a_class_init (GProxyAClass *class)
370 g_proxy_a_iface_init (GProxyInterface *proxy_iface)
372 proxy_iface->connect = g_proxy_base_connect;
373 proxy_iface->connect_async = g_proxy_base_connect_async;
374 proxy_iface->connect_finish = g_proxy_base_connect_finish;
375 proxy_iface->supports_hostname = g_proxy_a_supports_hostname;
378 /********************************************/
379 /* Test proxy implementation #2 ("Proxy B") */
380 /********************************************/
382 typedef GProxyBase GProxyB;
383 typedef GProxyBaseClass GProxyBClass;
385 static void g_proxy_b_iface_init (GProxyInterface *proxy_iface);
387 #define g_proxy_b_get_type _g_proxy_b_get_type
388 G_DEFINE_TYPE_WITH_CODE (GProxyB, g_proxy_b, g_proxy_base_get_type (),
389 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
390 g_proxy_b_iface_init)
391 g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
397 g_proxy_b_init (GProxyB *proxy)
399 ((GProxyBase *) proxy)->proxy_data = &proxy_b;
403 g_proxy_b_supports_hostname (GProxy *proxy)
409 g_proxy_b_class_init (GProxyBClass *class)
414 g_proxy_b_iface_init (GProxyInterface *proxy_iface)
416 proxy_iface->connect = g_proxy_base_connect;
417 proxy_iface->connect_async = g_proxy_base_connect_async;
418 proxy_iface->connect_finish = g_proxy_base_connect_finish;
419 proxy_iface->supports_hostname = g_proxy_b_supports_hostname;
423 /***********************************/
424 /* The proxy server implementation */
425 /***********************************/
428 proxy_bytes (GSocket *socket,
429 GIOCondition condition,
432 ProxyData *proxy = user_data;
433 gssize nread, nwrote, total;
436 GError *error = NULL;
438 nread = g_socket_receive_with_blocking (socket, buffer, sizeof (buffer),
442 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
446 g_assert_no_error (error);
450 g_main_loop_quit (proxy->loop);
454 if (socket == proxy->client_sock)
455 out_socket = proxy->server_sock;
457 out_socket = proxy->client_sock;
459 for (total = 0; total < nread; total += nwrote)
461 nwrote = g_socket_send_with_blocking (out_socket,
462 buffer + total, nread - total,
464 g_assert_no_error (error);
471 proxy_thread (gpointer user_data)
473 ProxyData *proxy = user_data;
474 GError *error = NULL;
475 gssize nread, nwrote;
476 gchar command[2] = { 0, 0 };
477 GMainContext *context;
480 context = g_main_context_new ();
481 proxy->loop = g_main_loop_new (context, FALSE);
485 proxy->client_sock = g_socket_accept (proxy->server, proxy->cancellable, &error);
486 if (!proxy->client_sock)
488 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
492 g_assert_no_error (error);
494 nread = g_socket_receive (proxy->client_sock, command, 1, NULL, &error);
495 g_assert_no_error (error);
499 g_clear_object (&proxy->client_sock);
503 g_assert_cmpint (nread, ==, 1);
504 g_assert_cmpstr (command, ==, proxy->proxy_command);
506 *command = g_ascii_toupper (*command);
507 nwrote = g_socket_send (proxy->client_sock, command, 1, NULL, &error);
508 g_assert_no_error (error);
509 g_assert_cmpint (nwrote, ==, 1);
511 proxy->server_sock = g_socket_new (G_SOCKET_FAMILY_IPV4,
512 G_SOCKET_TYPE_STREAM,
513 G_SOCKET_PROTOCOL_DEFAULT,
515 g_assert_no_error (error);
516 g_socket_connect (proxy->server_sock, server.server_addr, NULL, &error);
517 g_assert_no_error (error);
519 source = g_socket_create_source (proxy->client_sock, G_IO_IN, NULL);
520 g_source_set_callback (source, (GSourceFunc)proxy_bytes, proxy, NULL);
521 g_source_attach (source, context);
522 g_source_unref (source);
524 source = g_socket_create_source (proxy->server_sock, G_IO_IN, NULL);
525 g_source_set_callback (source, (GSourceFunc)proxy_bytes, proxy, NULL);
526 g_source_attach (source, context);
527 g_source_unref (source);
529 g_main_loop_run (proxy->loop);
531 g_socket_close (proxy->client_sock, &error);
532 g_assert_no_error (error);
533 g_clear_object (&proxy->client_sock);
535 g_socket_close (proxy->server_sock, &error);
536 g_assert_no_error (error);
537 g_clear_object (&proxy->server_sock);
540 g_main_loop_unref (proxy->loop);
541 g_main_context_unref (context);
543 g_object_unref (proxy->server);
544 g_object_unref (proxy->cancellable);
550 create_proxy (ProxyData *proxy,
551 gchar proxy_protocol,
552 const gchar *destination_protocol,
553 GCancellable *cancellable)
555 GError *error = NULL;
556 GSocketAddress *addr;
559 proxy->proxy_command = g_strdup_printf ("%c", proxy_protocol);
560 proxy->supported_protocol = g_strdup (destination_protocol);
561 proxy->cancellable = g_object_ref (cancellable);
563 proxy->server = g_socket_new (G_SOCKET_FAMILY_IPV4,
564 G_SOCKET_TYPE_STREAM,
565 G_SOCKET_PROTOCOL_DEFAULT,
567 g_assert_no_error (error);
569 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
570 addr = g_inet_socket_address_new (iaddr, 0);
571 g_object_unref (iaddr);
573 g_socket_bind (proxy->server, addr, TRUE, &error);
574 g_assert_no_error (error);
575 g_object_unref (addr);
577 addr = g_socket_get_local_address (proxy->server, &error);
578 proxy->port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
579 proxy->uri = g_strdup_printf ("proxy-%c://127.0.0.1:%u",
580 g_ascii_tolower (proxy_protocol),
582 g_object_unref (addr);
584 g_socket_listen (proxy->server, &error);
585 g_assert_no_error (error);
587 proxy->thread = g_thread_new ("proxy", proxy_thread, proxy);
592 /**************************/
593 /* The actual echo server */
594 /**************************/
597 echo_server_thread (gpointer user_data)
599 ServerData *data = user_data;
601 GError *error = NULL;
602 gssize nread, nwrote;
607 sock = g_socket_accept (data->server, data->cancellable, &error);
610 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
614 g_assert_no_error (error);
618 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
619 g_assert_no_error (error);
620 g_assert_cmpint (nread, >=, 0);
625 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
626 g_assert_no_error (error);
627 g_assert_cmpint (nwrote, ==, nread);
630 g_socket_close (sock, &error);
631 g_assert_no_error (error);
632 g_object_unref (sock);
635 g_object_unref (data->server);
636 g_object_unref (data->cancellable);
642 create_server (ServerData *data, GCancellable *cancellable)
644 GError *error = NULL;
645 GSocketAddress *addr;
648 data->cancellable = g_object_ref (cancellable);
650 data->server = g_socket_new (G_SOCKET_FAMILY_IPV4,
651 G_SOCKET_TYPE_STREAM,
652 G_SOCKET_PROTOCOL_DEFAULT,
654 g_assert_no_error (error);
656 g_socket_set_blocking (data->server, TRUE);
657 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
658 addr = g_inet_socket_address_new (iaddr, 0);
659 g_object_unref (iaddr);
661 g_socket_bind (data->server, addr, TRUE, &error);
662 g_assert_no_error (error);
663 g_object_unref (addr);
665 data->server_addr = g_socket_get_local_address (data->server, &error);
666 g_assert_no_error (error);
668 data->server_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (data->server_addr));
670 g_socket_listen (data->server, &error);
671 g_assert_no_error (error);
673 data->server_thread = g_thread_new ("server", echo_server_thread, data);
677 /******************************************************************/
678 /* Now a GResolver implementation, so the can't-resolve test will */
679 /* pass even if you have an evil DNS-faking ISP. */
680 /******************************************************************/
682 typedef GResolver GFakeResolver;
683 typedef GResolverClass GFakeResolverClass;
685 G_DEFINE_TYPE (GFakeResolver, g_fake_resolver, G_TYPE_RESOLVER)
688 g_fake_resolver_init (GFakeResolver *gtr)
693 g_fake_resolver_lookup_by_name (GResolver *resolver,
694 const gchar *hostname,
695 GCancellable *cancellable,
698 /* This is only ever called with lookups that are expected to
703 G_RESOLVER_ERROR_NOT_FOUND,
709 g_fake_resolver_lookup_by_name_async (GResolver *resolver,
710 const gchar *hostname,
711 GCancellable *cancellable,
712 GAsyncReadyCallback callback,
715 g_simple_async_report_error_in_idle (G_OBJECT (resolver),
718 G_RESOLVER_ERROR_NOT_FOUND,
723 g_fake_resolver_class_init (GFakeResolverClass *fake_class)
725 GResolverClass *resolver_class = G_RESOLVER_CLASS (fake_class);
727 resolver_class->lookup_by_name = g_fake_resolver_lookup_by_name;
728 resolver_class->lookup_by_name_async = g_fake_resolver_lookup_by_name_async;
733 /****************************************/
734 /* We made it! Now for the actual test! */
735 /****************************************/
738 setup_test (gpointer fixture,
739 gconstpointer user_data)
744 teardown_test (gpointer fixture,
745 gconstpointer user_data)
749 g_strfreev (last_proxies);
752 g_clear_error (&proxy_a.last_error);
753 g_clear_error (&proxy_b.last_error);
757 static const gchar *testbuf = "0123456789abcdef";
760 do_echo_test (GSocketConnection *conn)
762 GIOStream *iostream = G_IO_STREAM (conn);
763 GInputStream *istream = g_io_stream_get_input_stream (iostream);
764 GOutputStream *ostream = g_io_stream_get_output_stream (iostream);
768 GError *error = NULL;
770 g_output_stream_write_all (ostream, testbuf, strlen (testbuf),
771 &nwrote, NULL, &error);
772 g_assert_no_error (error);
773 g_assert_cmpint (nwrote, ==, strlen (testbuf));
775 for (total = 0; total < nwrote; total += nread)
777 nread = g_input_stream_read (istream,
778 buf + total, sizeof (buf) - total,
780 g_assert_no_error (error);
781 g_assert_cmpint (nread, >, 0);
785 g_assert_cmpstr (buf, ==, testbuf);
789 async_got_conn (GObject *source,
790 GAsyncResult *result,
793 GSocketConnection **conn = user_data;
794 GError *error = NULL;
796 *conn = g_socket_client_connect_finish (G_SOCKET_CLIENT (source),
798 g_assert_no_error (error);
802 async_got_error (GObject *source,
803 GAsyncResult *result,
806 GError **error = user_data;
808 g_assert (error != NULL && *error == NULL);
809 g_socket_client_connect_finish (G_SOCKET_CLIENT (source),
811 g_assert (*error != NULL);
816 assert_direct (GSocketConnection *conn)
818 GSocketAddress *addr;
819 GError *error = NULL;
821 g_assert_cmpint (g_strv_length (last_proxies), ==, 1);
822 g_assert_cmpstr (last_proxies[0], ==, "direct://");
823 g_assert_no_error (proxy_a.last_error);
824 g_assert_no_error (proxy_b.last_error);
826 addr = g_socket_connection_get_remote_address (conn, &error);
827 g_assert_no_error (error);
828 g_assert (!G_IS_PROXY_ADDRESS (addr));
832 test_direct_sync (gpointer fixture,
833 gconstpointer user_data)
835 GSocketConnection *conn;
837 GError *error = NULL;
839 /* The simple:// URI should not require any proxy. */
841 uri = g_strdup_printf ("simple://127.0.0.1:%u", server.server_port);
842 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
844 g_assert_no_error (error);
846 assert_direct (conn);
848 g_object_unref (conn);
852 test_direct_async (gpointer fixture,
853 gconstpointer user_data)
855 GSocketConnection *conn;
858 /* The simple:// URI should not require any proxy. */
859 uri = g_strdup_printf ("simple://127.0.0.1:%u", server.server_port);
861 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
862 async_got_conn, &conn);
865 g_main_context_iteration (NULL, TRUE);
867 assert_direct (conn);
869 g_object_unref (conn);
873 assert_single (GSocketConnection *conn)
875 GSocketAddress *addr;
876 const gchar *proxy_uri;
878 GError *error = NULL;
880 g_assert_cmpint (g_strv_length (last_proxies), ==, 2);
881 g_assert_cmpstr (last_proxies[0], ==, proxy_a.uri);
882 g_assert_cmpstr (last_proxies[1], ==, proxy_b.uri);
883 g_assert_no_error (proxy_a.last_error);
884 g_assert_no_error (proxy_b.last_error);
886 addr = g_socket_connection_get_remote_address (conn, &error);
887 g_assert_no_error (error);
888 g_assert (G_IS_PROXY_ADDRESS (addr));
889 proxy_uri = g_proxy_address_get_uri (G_PROXY_ADDRESS (addr));
890 g_assert_cmpstr (proxy_uri, ==, proxy_a.uri);
891 proxy_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
892 g_assert_cmpint (proxy_port, ==, proxy_a.port);
896 test_single_sync (gpointer fixture,
897 gconstpointer user_data)
899 GSocketConnection *conn;
900 GError *error = NULL;
903 /* The alpha:// URI should be proxied via Proxy A */
904 uri = g_strdup_printf ("alpha://127.0.0.1:%u", server.server_port);
905 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
907 g_assert_no_error (error);
909 assert_single (conn);
912 g_object_unref (conn);
916 test_single_async (gpointer fixture,
917 gconstpointer user_data)
919 GSocketConnection *conn;
922 /* The alpha:// URI should be proxied via Proxy A */
923 uri = g_strdup_printf ("alpha://127.0.0.1:%u", server.server_port);
925 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
926 async_got_conn, &conn);
929 g_main_context_iteration (NULL, TRUE);
931 assert_single (conn);
933 g_object_unref (conn);
937 assert_multiple (GSocketConnection *conn)
939 GSocketAddress *addr;
940 const gchar *proxy_uri;
942 GError *error = NULL;
944 g_assert_cmpint (g_strv_length (last_proxies), ==, 2);
945 g_assert_cmpstr (last_proxies[0], ==, proxy_a.uri);
946 g_assert_cmpstr (last_proxies[1], ==, proxy_b.uri);
947 g_assert_error (proxy_a.last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
948 g_assert_no_error (proxy_b.last_error);
950 addr = g_socket_connection_get_remote_address (conn, &error);
951 g_assert_no_error (error);
952 g_assert (G_IS_PROXY_ADDRESS (addr));
953 proxy_uri = g_proxy_address_get_uri (G_PROXY_ADDRESS (addr));
954 g_assert_cmpstr (proxy_uri, ==, proxy_b.uri);
955 proxy_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
956 g_assert_cmpint (proxy_port, ==, proxy_b.port);
960 test_multiple_sync (gpointer fixture,
961 gconstpointer user_data)
963 GSocketConnection *conn;
964 GError *error = NULL;
967 /* The beta:// URI should be proxied via Proxy B, after failing
970 uri = g_strdup_printf ("beta://127.0.0.1:%u", server.server_port);
971 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
973 g_assert_no_error (error);
975 assert_multiple (conn);
977 g_object_unref (conn);
981 test_multiple_async (gpointer fixture,
982 gconstpointer user_data)
984 GSocketConnection *conn;
987 /* The beta:// URI should be proxied via Proxy B, after failing
990 uri = g_strdup_printf ("beta://127.0.0.1:%u", server.server_port);
992 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
993 async_got_conn, &conn);
996 g_main_context_iteration (NULL, TRUE);
998 assert_multiple (conn);
1000 g_object_unref (conn);
1004 test_dns (gpointer fixture,
1005 gconstpointer user_data)
1007 GSocketConnection *conn;
1008 GError *error = NULL;
1011 /* The simple:// and alpha:// URIs should fail with a DNS error,
1012 * but the beta:// URI should succeed, because we pass it to
1013 * Proxy B without trying to resolve it first
1017 uri = g_strdup_printf ("simple://no-such-host.xx:%u", server.server_port);
1018 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
1019 g_assert_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND);
1020 g_clear_error (&error);
1022 g_assert_no_error (proxy_a.last_error);
1023 g_assert_no_error (proxy_b.last_error);
1024 teardown_test (NULL, NULL);
1026 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
1027 async_got_error, &error);
1028 while (error == NULL)
1029 g_main_context_iteration (NULL, TRUE);
1030 g_assert_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND);
1031 g_clear_error (&error);
1034 g_assert_no_error (proxy_a.last_error);
1035 g_assert_no_error (proxy_b.last_error);
1036 teardown_test (NULL, NULL);
1039 uri = g_strdup_printf ("alpha://no-such-host.xx:%u", server.server_port);
1040 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
1041 /* Since Proxy A fails, @client will try Proxy B too, which won't
1042 * load an alpha:// URI.
1044 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1045 g_clear_error (&error);
1047 g_assert_no_error (proxy_a.last_error);
1048 g_assert_error (proxy_b.last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1049 teardown_test (NULL, NULL);
1051 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
1052 async_got_error, &error);
1053 while (error == NULL)
1054 g_main_context_iteration (NULL, TRUE);
1055 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1056 g_clear_error (&error);
1059 g_assert_no_error (proxy_a.last_error);
1060 g_assert_error (proxy_b.last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1061 teardown_test (NULL, NULL);
1064 uri = g_strdup_printf ("beta://no-such-host.xx:%u", server.server_port);
1065 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
1066 g_assert_no_error (error);
1067 g_clear_object (&conn);
1069 g_assert_no_error (proxy_a.last_error);
1070 g_assert_no_error (proxy_b.last_error);
1071 teardown_test (NULL, NULL);
1073 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
1074 async_got_conn, &conn);
1075 while (conn == NULL)
1076 g_main_context_iteration (NULL, TRUE);
1077 g_clear_object (&conn);
1080 g_assert_no_error (proxy_a.last_error);
1081 g_assert_no_error (proxy_b.last_error);
1082 teardown_test (NULL, NULL);
1089 GResolver *fake_resolver;
1090 GCancellable *cancellable;
1094 g_test_init (&argc, &argv, NULL);
1096 /* Register stuff. The dummy g_proxy_get_default_for_protocol() call
1097 * is to force _g_io_modules_ensure_extension_points_registered() to
1098 * get called, so we can then register a proxy resolver extension
1101 g_proxy_get_default_for_protocol ("foo");
1102 g_test_proxy_resolver_get_type ();
1103 g_proxy_a_get_type ();
1104 g_proxy_b_get_type ();
1105 g_setenv ("GIO_USE_PROXY_RESOLVER", "test", TRUE);
1107 fake_resolver = g_object_new (g_fake_resolver_get_type (), NULL);
1108 g_resolver_set_default (fake_resolver);
1110 cancellable = g_cancellable_new ();
1111 create_server (&server, cancellable);
1112 create_proxy (&proxy_a, 'a', "alpha", cancellable);
1113 create_proxy (&proxy_b, 'b', "beta", cancellable);
1115 client = g_socket_client_new ();
1116 g_assert_cmpint (g_socket_client_get_enable_proxy (client), ==, TRUE);
1118 g_test_add_vtable ("/proxy/direct_sync", 0, NULL, setup_test, test_direct_sync, teardown_test);
1119 g_test_add_vtable ("/proxy/direct_async", 0, NULL, setup_test, test_direct_async, teardown_test);
1120 g_test_add_vtable ("/proxy/single_sync", 0, NULL, setup_test, test_single_sync, teardown_test);
1121 g_test_add_vtable ("/proxy/single_async", 0, NULL, setup_test, test_single_async, teardown_test);
1122 g_test_add_vtable ("/proxy/multiple_sync", 0, NULL, setup_test, test_multiple_sync, teardown_test);
1123 g_test_add_vtable ("/proxy/multiple_async", 0, NULL, setup_test, test_multiple_async, teardown_test);
1124 g_test_add_vtable ("/proxy/dns", 0, NULL, setup_test, test_dns, teardown_test);
1126 result = g_test_run();
1128 g_object_unref (client);
1130 g_cancellable_cancel (cancellable);
1131 g_thread_join (proxy_a.thread);
1132 g_thread_join (proxy_b.thread);
1133 g_thread_join (server.server_thread);