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;
165 proxies = g_test_proxy_resolver_lookup (resolver, uri, cancellable, &error);
167 task = g_task_new (resolver, NULL, callback, user_data);
169 g_task_return_error (task, error);
171 g_task_return_pointer (task, proxies, (GDestroyNotify) g_strfreev);
173 g_object_unref (task);
177 g_test_proxy_resolver_lookup_finish (GProxyResolver *resolver,
178 GAsyncResult *result,
181 return g_task_propagate_pointer (G_TASK (result), error);
185 g_test_proxy_resolver_class_init (GTestProxyResolverClass *resolver_class)
190 g_test_proxy_resolver_iface_init (GProxyResolverInterface *iface)
192 iface->is_supported = g_test_proxy_resolver_is_supported;
193 iface->lookup = g_test_proxy_resolver_lookup;
194 iface->lookup_async = g_test_proxy_resolver_lookup_async;
195 iface->lookup_finish = g_test_proxy_resolver_lookup_finish;
199 /****************************************/
200 /* Test proxy implementation base class */
201 /****************************************/
206 ProxyData *proxy_data;
210 GObjectClass parent_class;
213 #define g_proxy_base_get_type _g_proxy_base_get_type
214 G_DEFINE_ABSTRACT_TYPE (GProxyBase, g_proxy_base, G_TYPE_OBJECT)
217 g_proxy_base_init (GProxyBase *proxy)
222 g_proxy_base_connect (GProxy *proxy,
223 GIOStream *io_stream,
224 GProxyAddress *proxy_address,
225 GCancellable *cancellable,
228 ProxyData *data = ((GProxyBase *) proxy)->proxy_data;
229 const gchar *protocol;
230 GOutputStream *ostream;
231 GInputStream *istream;
234 g_assert_no_error (data->last_error);
236 protocol = g_proxy_address_get_destination_protocol (proxy_address);
237 if (strcmp (protocol, data->supported_protocol) != 0)
239 g_set_error_literal (&data->last_error,
240 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
241 "Unsupported protocol");
245 ostream = g_io_stream_get_output_stream (io_stream);
246 if (g_output_stream_write (ostream, data->proxy_command, 1, cancellable,
247 &data->last_error) != 1)
250 istream = g_io_stream_get_input_stream (io_stream);
251 if (g_input_stream_read (istream, &response, 1, cancellable,
252 &data->last_error) != 1)
255 if (response != g_ascii_toupper (*data->proxy_command))
257 g_set_error_literal (&data->last_error,
258 G_IO_ERROR, G_IO_ERROR_FAILED,
263 return g_object_ref (io_stream);
266 g_propagate_error (error, g_error_copy (data->last_error));
271 g_proxy_base_connect_async (GProxy *proxy,
272 GIOStream *io_stream,
273 GProxyAddress *proxy_address,
274 GCancellable *cancellable,
275 GAsyncReadyCallback callback,
278 GError *error = NULL;
280 GIOStream *proxy_io_stream;
282 task = g_task_new (proxy, NULL, callback, user_data);
284 proxy_io_stream = g_proxy_connect (proxy, io_stream, proxy_address,
285 cancellable, &error);
287 g_task_return_pointer (task, proxy_io_stream, g_object_unref);
289 g_task_return_error (task, error);
290 g_object_unref (task);
294 g_proxy_base_connect_finish (GProxy *proxy,
295 GAsyncResult *result,
298 return g_task_propagate_pointer (G_TASK (result), error);
302 g_proxy_base_class_init (GProxyBaseClass *class)
307 /********************************************/
308 /* Test proxy implementation #1 ("Proxy A") */
309 /********************************************/
311 typedef GProxyBase GProxyA;
312 typedef GProxyBaseClass GProxyAClass;
314 static void g_proxy_a_iface_init (GProxyInterface *proxy_iface);
316 #define g_proxy_a_get_type _g_proxy_a_get_type
317 G_DEFINE_TYPE_WITH_CODE (GProxyA, g_proxy_a, g_proxy_base_get_type (),
318 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
319 g_proxy_a_iface_init)
320 g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
326 g_proxy_a_init (GProxyA *proxy)
328 ((GProxyBase *) proxy)->proxy_data = &proxy_a;
332 g_proxy_a_supports_hostname (GProxy *proxy)
338 g_proxy_a_class_init (GProxyAClass *class)
343 g_proxy_a_iface_init (GProxyInterface *proxy_iface)
345 proxy_iface->connect = g_proxy_base_connect;
346 proxy_iface->connect_async = g_proxy_base_connect_async;
347 proxy_iface->connect_finish = g_proxy_base_connect_finish;
348 proxy_iface->supports_hostname = g_proxy_a_supports_hostname;
351 /********************************************/
352 /* Test proxy implementation #2 ("Proxy B") */
353 /********************************************/
355 typedef GProxyBase GProxyB;
356 typedef GProxyBaseClass GProxyBClass;
358 static void g_proxy_b_iface_init (GProxyInterface *proxy_iface);
360 #define g_proxy_b_get_type _g_proxy_b_get_type
361 G_DEFINE_TYPE_WITH_CODE (GProxyB, g_proxy_b, g_proxy_base_get_type (),
362 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
363 g_proxy_b_iface_init)
364 g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
370 g_proxy_b_init (GProxyB *proxy)
372 ((GProxyBase *) proxy)->proxy_data = &proxy_b;
376 g_proxy_b_supports_hostname (GProxy *proxy)
382 g_proxy_b_class_init (GProxyBClass *class)
387 g_proxy_b_iface_init (GProxyInterface *proxy_iface)
389 proxy_iface->connect = g_proxy_base_connect;
390 proxy_iface->connect_async = g_proxy_base_connect_async;
391 proxy_iface->connect_finish = g_proxy_base_connect_finish;
392 proxy_iface->supports_hostname = g_proxy_b_supports_hostname;
396 /***********************************/
397 /* The proxy server implementation */
398 /***********************************/
401 proxy_bytes (GSocket *socket,
402 GIOCondition condition,
405 ProxyData *proxy = user_data;
406 gssize nread, nwrote, total;
409 GError *error = NULL;
411 nread = g_socket_receive_with_blocking (socket, buffer, sizeof (buffer),
415 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
419 g_assert_no_error (error);
423 g_main_loop_quit (proxy->loop);
427 if (socket == proxy->client_sock)
428 out_socket = proxy->server_sock;
430 out_socket = proxy->client_sock;
432 for (total = 0; total < nread; total += nwrote)
434 nwrote = g_socket_send_with_blocking (out_socket,
435 buffer + total, nread - total,
437 g_assert_no_error (error);
444 proxy_thread (gpointer user_data)
446 ProxyData *proxy = user_data;
447 GError *error = NULL;
448 gssize nread, nwrote;
449 gchar command[2] = { 0, 0 };
450 GMainContext *context;
451 GSource *read_source, *write_source;
453 context = g_main_context_new ();
454 proxy->loop = g_main_loop_new (context, FALSE);
458 proxy->client_sock = g_socket_accept (proxy->server, proxy->cancellable, &error);
459 if (!proxy->client_sock)
461 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
462 g_error_free (error);
466 g_assert_no_error (error);
468 nread = g_socket_receive (proxy->client_sock, command, 1, NULL, &error);
469 g_assert_no_error (error);
473 g_clear_object (&proxy->client_sock);
477 g_assert_cmpint (nread, ==, 1);
478 g_assert_cmpstr (command, ==, proxy->proxy_command);
480 *command = g_ascii_toupper (*command);
481 nwrote = g_socket_send (proxy->client_sock, command, 1, NULL, &error);
482 g_assert_no_error (error);
483 g_assert_cmpint (nwrote, ==, 1);
485 proxy->server_sock = g_socket_new (G_SOCKET_FAMILY_IPV4,
486 G_SOCKET_TYPE_STREAM,
487 G_SOCKET_PROTOCOL_DEFAULT,
489 g_assert_no_error (error);
490 g_socket_connect (proxy->server_sock, server.server_addr, NULL, &error);
491 g_assert_no_error (error);
493 read_source = g_socket_create_source (proxy->client_sock, G_IO_IN, NULL);
494 g_source_set_callback (read_source, (GSourceFunc)proxy_bytes, proxy, NULL);
495 g_source_attach (read_source, context);
497 write_source = g_socket_create_source (proxy->server_sock, G_IO_IN, NULL);
498 g_source_set_callback (write_source, (GSourceFunc)proxy_bytes, proxy, NULL);
499 g_source_attach (write_source, context);
501 g_main_loop_run (proxy->loop);
503 g_socket_close (proxy->client_sock, &error);
504 g_assert_no_error (error);
505 g_clear_object (&proxy->client_sock);
507 g_socket_close (proxy->server_sock, &error);
508 g_assert_no_error (error);
509 g_clear_object (&proxy->server_sock);
511 g_source_destroy (read_source);
512 g_source_unref (read_source);
513 g_source_destroy (write_source);
514 g_source_unref (write_source);
517 g_main_loop_unref (proxy->loop);
518 g_main_context_unref (context);
520 g_object_unref (proxy->server);
521 g_object_unref (proxy->cancellable);
523 g_free (proxy->proxy_command);
524 g_free (proxy->supported_protocol);
531 create_proxy (ProxyData *proxy,
532 gchar proxy_protocol,
533 const gchar *destination_protocol,
534 GCancellable *cancellable)
536 GError *error = NULL;
537 GSocketAddress *addr;
540 proxy->proxy_command = g_strdup_printf ("%c", proxy_protocol);
541 proxy->supported_protocol = g_strdup (destination_protocol);
542 proxy->cancellable = g_object_ref (cancellable);
544 proxy->server = g_socket_new (G_SOCKET_FAMILY_IPV4,
545 G_SOCKET_TYPE_STREAM,
546 G_SOCKET_PROTOCOL_DEFAULT,
548 g_assert_no_error (error);
550 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
551 addr = g_inet_socket_address_new (iaddr, 0);
552 g_object_unref (iaddr);
554 g_socket_bind (proxy->server, addr, TRUE, &error);
555 g_assert_no_error (error);
556 g_object_unref (addr);
558 addr = g_socket_get_local_address (proxy->server, &error);
559 proxy->port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
560 proxy->uri = g_strdup_printf ("proxy-%c://127.0.0.1:%u",
561 g_ascii_tolower (proxy_protocol),
563 g_object_unref (addr);
565 g_socket_listen (proxy->server, &error);
566 g_assert_no_error (error);
568 proxy->thread = g_thread_new ("proxy", proxy_thread, proxy);
573 /**************************/
574 /* The actual echo server */
575 /**************************/
578 echo_server_thread (gpointer user_data)
580 ServerData *data = user_data;
582 GError *error = NULL;
583 gssize nread, nwrote;
588 sock = g_socket_accept (data->server, data->cancellable, &error);
591 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
592 g_error_free (error);
596 g_assert_no_error (error);
600 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
601 g_assert_no_error (error);
602 g_assert_cmpint (nread, >=, 0);
607 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
608 g_assert_no_error (error);
609 g_assert_cmpint (nwrote, ==, nread);
612 g_socket_close (sock, &error);
613 g_assert_no_error (error);
614 g_object_unref (sock);
617 g_object_unref (data->server);
618 g_object_unref (data->server_addr);
619 g_object_unref (data->cancellable);
625 create_server (ServerData *data, GCancellable *cancellable)
627 GError *error = NULL;
628 GSocketAddress *addr;
631 data->cancellable = g_object_ref (cancellable);
633 data->server = g_socket_new (G_SOCKET_FAMILY_IPV4,
634 G_SOCKET_TYPE_STREAM,
635 G_SOCKET_PROTOCOL_DEFAULT,
637 g_assert_no_error (error);
639 g_socket_set_blocking (data->server, TRUE);
640 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
641 addr = g_inet_socket_address_new (iaddr, 0);
642 g_object_unref (iaddr);
644 g_socket_bind (data->server, addr, TRUE, &error);
645 g_assert_no_error (error);
646 g_object_unref (addr);
648 data->server_addr = g_socket_get_local_address (data->server, &error);
649 g_assert_no_error (error);
651 data->server_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (data->server_addr));
653 g_socket_listen (data->server, &error);
654 g_assert_no_error (error);
656 data->server_thread = g_thread_new ("server", echo_server_thread, data);
660 /******************************************************************/
661 /* Now a GResolver implementation, so the can't-resolve test will */
662 /* pass even if you have an evil DNS-faking ISP. */
663 /******************************************************************/
665 typedef GResolver GFakeResolver;
666 typedef GResolverClass GFakeResolverClass;
668 G_DEFINE_TYPE (GFakeResolver, g_fake_resolver, G_TYPE_RESOLVER)
671 g_fake_resolver_init (GFakeResolver *gtr)
676 g_fake_resolver_lookup_by_name (GResolver *resolver,
677 const gchar *hostname,
678 GCancellable *cancellable,
681 /* This is only ever called with lookups that are expected to
686 G_RESOLVER_ERROR_NOT_FOUND,
692 g_fake_resolver_lookup_by_name_async (GResolver *resolver,
693 const gchar *hostname,
694 GCancellable *cancellable,
695 GAsyncReadyCallback callback,
698 g_task_report_new_error (resolver, callback, user_data,
699 g_fake_resolver_lookup_by_name_async,
700 G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
705 g_fake_resolver_lookup_by_name_finish (GResolver *resolver,
706 GAsyncResult *result,
709 return g_task_propagate_pointer (G_TASK (result), error);
713 g_fake_resolver_class_init (GFakeResolverClass *fake_class)
715 GResolverClass *resolver_class = G_RESOLVER_CLASS (fake_class);
717 resolver_class->lookup_by_name = g_fake_resolver_lookup_by_name;
718 resolver_class->lookup_by_name_async = g_fake_resolver_lookup_by_name_async;
719 resolver_class->lookup_by_name_finish = g_fake_resolver_lookup_by_name_finish;
724 /****************************************/
725 /* We made it! Now for the actual test! */
726 /****************************************/
729 setup_test (gpointer fixture,
730 gconstpointer user_data)
735 teardown_test (gpointer fixture,
736 gconstpointer user_data)
740 g_strfreev (last_proxies);
743 g_clear_error (&proxy_a.last_error);
744 g_clear_error (&proxy_b.last_error);
748 static const gchar *testbuf = "0123456789abcdef";
751 do_echo_test (GSocketConnection *conn)
753 GIOStream *iostream = G_IO_STREAM (conn);
754 GInputStream *istream = g_io_stream_get_input_stream (iostream);
755 GOutputStream *ostream = g_io_stream_get_output_stream (iostream);
759 GError *error = NULL;
761 g_output_stream_write_all (ostream, testbuf, strlen (testbuf),
762 &nwrote, NULL, &error);
763 g_assert_no_error (error);
764 g_assert_cmpint (nwrote, ==, strlen (testbuf));
766 for (total = 0; total < nwrote; total += nread)
768 nread = g_input_stream_read (istream,
769 buf + total, sizeof (buf) - total,
771 g_assert_no_error (error);
772 g_assert_cmpint (nread, >, 0);
776 g_assert_cmpstr (buf, ==, testbuf);
780 async_got_conn (GObject *source,
781 GAsyncResult *result,
784 GSocketConnection **conn = user_data;
785 GError *error = NULL;
787 *conn = g_socket_client_connect_finish (G_SOCKET_CLIENT (source),
789 g_assert_no_error (error);
793 async_got_error (GObject *source,
794 GAsyncResult *result,
797 GError **error = user_data;
799 g_assert (error != NULL && *error == NULL);
800 g_socket_client_connect_finish (G_SOCKET_CLIENT (source),
802 g_assert (*error != NULL);
807 assert_direct (GSocketConnection *conn)
809 GSocketAddress *addr;
810 GError *error = NULL;
812 g_assert_cmpint (g_strv_length (last_proxies), ==, 1);
813 g_assert_cmpstr (last_proxies[0], ==, "direct://");
814 g_assert_no_error (proxy_a.last_error);
815 g_assert_no_error (proxy_b.last_error);
817 addr = g_socket_connection_get_remote_address (conn, &error);
818 g_assert_no_error (error);
819 g_assert (!G_IS_PROXY_ADDRESS (addr));
820 g_object_unref (addr);
822 addr = g_socket_connection_get_local_address (conn, &error);
823 g_assert_no_error (error);
824 g_object_unref (addr);
826 g_assert (g_socket_connection_is_connected (conn));
830 test_direct_sync (gpointer fixture,
831 gconstpointer user_data)
833 GSocketConnection *conn;
835 GError *error = NULL;
837 /* The simple:// URI should not require any proxy. */
839 uri = g_strdup_printf ("simple://127.0.0.1:%u", server.server_port);
840 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
842 g_assert_no_error (error);
844 assert_direct (conn);
846 g_object_unref (conn);
850 test_direct_async (gpointer fixture,
851 gconstpointer user_data)
853 GSocketConnection *conn;
856 /* The simple:// URI should not require any proxy. */
857 uri = g_strdup_printf ("simple://127.0.0.1:%u", server.server_port);
859 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
860 async_got_conn, &conn);
863 g_main_context_iteration (NULL, TRUE);
865 assert_direct (conn);
867 g_object_unref (conn);
871 assert_single (GSocketConnection *conn)
873 GSocketAddress *addr;
874 const gchar *proxy_uri;
876 GError *error = NULL;
878 g_assert_cmpint (g_strv_length (last_proxies), ==, 2);
879 g_assert_cmpstr (last_proxies[0], ==, proxy_a.uri);
880 g_assert_cmpstr (last_proxies[1], ==, proxy_b.uri);
881 g_assert_no_error (proxy_a.last_error);
882 g_assert_no_error (proxy_b.last_error);
884 addr = g_socket_connection_get_remote_address (conn, &error);
885 g_assert_no_error (error);
886 g_assert (G_IS_PROXY_ADDRESS (addr));
887 proxy_uri = g_proxy_address_get_uri (G_PROXY_ADDRESS (addr));
888 g_assert_cmpstr (proxy_uri, ==, proxy_a.uri);
889 proxy_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
890 g_assert_cmpint (proxy_port, ==, proxy_a.port);
892 g_object_unref (addr);
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);
958 g_object_unref (addr);
962 test_multiple_sync (gpointer fixture,
963 gconstpointer user_data)
965 GSocketConnection *conn;
966 GError *error = NULL;
969 /* The beta:// URI should be proxied via Proxy B, after failing
972 uri = g_strdup_printf ("beta://127.0.0.1:%u", server.server_port);
973 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
975 g_assert_no_error (error);
977 assert_multiple (conn);
979 g_object_unref (conn);
983 test_multiple_async (gpointer fixture,
984 gconstpointer user_data)
986 GSocketConnection *conn;
989 /* The beta:// URI should be proxied via Proxy B, after failing
992 uri = g_strdup_printf ("beta://127.0.0.1:%u", server.server_port);
994 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
995 async_got_conn, &conn);
998 g_main_context_iteration (NULL, TRUE);
1000 assert_multiple (conn);
1001 do_echo_test (conn);
1002 g_object_unref (conn);
1006 test_dns (gpointer fixture,
1007 gconstpointer user_data)
1009 GSocketConnection *conn;
1010 GError *error = NULL;
1013 /* The simple:// and alpha:// URIs should fail with a DNS error,
1014 * but the beta:// URI should succeed, because we pass it to
1015 * Proxy B without trying to resolve it first
1019 uri = g_strdup_printf ("simple://no-such-host.xx:%u", server.server_port);
1020 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
1021 g_assert_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND);
1022 g_clear_error (&error);
1024 g_assert_no_error (proxy_a.last_error);
1025 g_assert_no_error (proxy_b.last_error);
1026 teardown_test (NULL, NULL);
1028 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
1029 async_got_error, &error);
1030 while (error == NULL)
1031 g_main_context_iteration (NULL, TRUE);
1032 g_assert_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND);
1033 g_clear_error (&error);
1036 g_assert_no_error (proxy_a.last_error);
1037 g_assert_no_error (proxy_b.last_error);
1038 teardown_test (NULL, NULL);
1041 uri = g_strdup_printf ("alpha://no-such-host.xx:%u", server.server_port);
1042 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
1043 /* Since Proxy A fails, @client will try Proxy B too, which won't
1044 * load an alpha:// URI.
1046 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1047 g_clear_error (&error);
1049 g_assert_no_error (proxy_a.last_error);
1050 g_assert_error (proxy_b.last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1051 teardown_test (NULL, NULL);
1053 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
1054 async_got_error, &error);
1055 while (error == NULL)
1056 g_main_context_iteration (NULL, TRUE);
1057 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1058 g_clear_error (&error);
1061 g_assert_no_error (proxy_a.last_error);
1062 g_assert_error (proxy_b.last_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1063 teardown_test (NULL, NULL);
1066 uri = g_strdup_printf ("beta://no-such-host.xx:%u", server.server_port);
1067 conn = g_socket_client_connect_to_uri (client, uri, 0, NULL, &error);
1068 g_assert_no_error (error);
1070 g_assert_no_error (proxy_a.last_error);
1071 g_assert_no_error (proxy_b.last_error);
1073 do_echo_test (conn);
1074 g_clear_object (&conn);
1075 teardown_test (NULL, NULL);
1077 g_socket_client_connect_to_uri_async (client, uri, 0, NULL,
1078 async_got_conn, &conn);
1079 while (conn == NULL)
1080 g_main_context_iteration (NULL, TRUE);
1083 g_assert_no_error (proxy_a.last_error);
1084 g_assert_no_error (proxy_b.last_error);
1086 do_echo_test (conn);
1087 g_clear_object (&conn);
1088 teardown_test (NULL, NULL);
1095 GResolver *fake_resolver;
1096 GCancellable *cancellable;
1100 g_test_init (&argc, &argv, NULL);
1102 /* Register stuff. The dummy g_proxy_get_default_for_protocol() call
1103 * is to force _g_io_modules_ensure_extension_points_registered() to
1104 * get called, so we can then register a proxy resolver extension
1107 g_proxy_get_default_for_protocol ("foo");
1108 g_test_proxy_resolver_get_type ();
1109 g_proxy_a_get_type ();
1110 g_proxy_b_get_type ();
1111 g_setenv ("GIO_USE_PROXY_RESOLVER", "test", TRUE);
1113 fake_resolver = g_object_new (g_fake_resolver_get_type (), NULL);
1114 g_resolver_set_default (fake_resolver);
1116 cancellable = g_cancellable_new ();
1117 create_server (&server, cancellable);
1118 create_proxy (&proxy_a, 'a', "alpha", cancellable);
1119 create_proxy (&proxy_b, 'b', "beta", cancellable);
1121 client = g_socket_client_new ();
1122 g_assert_cmpint (g_socket_client_get_enable_proxy (client), ==, TRUE);
1124 g_test_add_vtable ("/proxy/direct_sync", 0, NULL, setup_test, test_direct_sync, teardown_test);
1125 g_test_add_vtable ("/proxy/direct_async", 0, NULL, setup_test, test_direct_async, teardown_test);
1126 g_test_add_vtable ("/proxy/single_sync", 0, NULL, setup_test, test_single_sync, teardown_test);
1127 g_test_add_vtable ("/proxy/single_async", 0, NULL, setup_test, test_single_async, teardown_test);
1128 g_test_add_vtable ("/proxy/multiple_sync", 0, NULL, setup_test, test_multiple_sync, teardown_test);
1129 g_test_add_vtable ("/proxy/multiple_async", 0, NULL, setup_test, test_multiple_async, teardown_test);
1130 g_test_add_vtable ("/proxy/dns", 0, NULL, setup_test, test_dns, teardown_test);
1132 result = g_test_run();
1134 g_object_unref (client);
1136 g_cancellable_cancel (cancellable);
1137 g_thread_join (proxy_a.thread);
1138 g_thread_join (proxy_b.thread);
1139 g_thread_join (server.server_thread);
1141 g_object_unref (cancellable);