Blame gio/tests/pollable.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <gio/gunixinputstream.h>
Packit ae235b
#include <gio/gunixoutputstream.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
GMainLoop *loop;
Packit ae235b
GPollableInputStream *in;
Packit ae235b
GOutputStream *out;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
poll_source_callback (GPollableInputStream *in,
Packit ae235b
		      gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  char buf[2];
Packit ae235b
  gssize nread;
Packit ae235b
  gboolean *success = user_data;
Packit ae235b
Packit ae235b
  nread = g_pollable_input_stream_read_nonblocking (in, buf, 2, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpint (nread, ==, 2);
Packit ae235b
  g_assert_cmpstr (buf, ==, "x");
Packit ae235b
Packit ae235b
  *success = TRUE;
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_source_readability_callback (gpointer user_data)
Packit ae235b
{
Packit ae235b
  gboolean expected = GPOINTER_TO_INT (user_data);
Packit ae235b
  gboolean readable;
Packit ae235b
Packit ae235b
  readable = g_pollable_input_stream_is_readable (in);
Packit ae235b
  g_assert_cmpint (readable, ==, expected);
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
write_callback (gpointer user_data)
Packit ae235b
{
Packit ae235b
  char *buf = "x";
Packit ae235b
  gssize nwrote;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  nwrote = g_output_stream_write (out, buf, 2, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpint (nwrote, ==, 2);
Packit ae235b
/* Give the pipe a few ticks to propagate the write for sockets. On my
Packit ae235b
 * iMac i7, 40 works, 30 doesn't. */
Packit ae235b
  g_usleep (80L);
Packit ae235b
Packit ae235b
  check_source_readability_callback (GINT_TO_POINTER (TRUE));
Packit ae235b
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_source_and_quit_callback (gpointer user_data)
Packit ae235b
{
Packit ae235b
  check_source_readability_callback (user_data);
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_streams (void)
Packit ae235b
{
Packit ae235b
  gboolean readable;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  char buf[1];
Packit ae235b
  gssize nread;
Packit ae235b
  GSource *poll_source;
Packit ae235b
  gboolean success = FALSE;
Packit ae235b
Packit ae235b
  g_assert (g_pollable_input_stream_can_poll (in));
Packit ae235b
  g_assert (g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
Packit ae235b
Packit ae235b
  readable = g_pollable_input_stream_is_readable (in);
Packit ae235b
  g_assert (!readable);
Packit ae235b
Packit ae235b
  nread = g_pollable_input_stream_read_nonblocking (in, buf, 1, NULL, &error);
Packit ae235b
  g_assert_cmpint (nread, ==, -1);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
Packit ae235b
  g_clear_error (&error);
Packit ae235b
Packit ae235b
  /* Create 4 sources, in decreasing order of priority:
Packit ae235b
   *   1. poll source on @in
Packit ae235b
   *   2. idle source that checks if @in is readable once
Packit ae235b
   *      (it won't be) and then removes itself
Packit ae235b
   *   3. idle source that writes a byte to @out, checks that
Packit ae235b
   *      @in is now readable, and removes itself
Packit ae235b
   *   4. idle source that checks if @in is readable once
Packit ae235b
   *      (it won't be, since the poll source will fire before
Packit ae235b
   *      this one does) and then quits the loop.
Packit ae235b
   *
Packit ae235b
   * If the poll source triggers before it should, then it will get a
Packit ae235b
   * %G_IO_ERROR_WOULD_BLOCK, and if check() fails in either
Packit ae235b
   * direction, we will catch it at some point.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  poll_source = g_pollable_input_stream_create_source (in, NULL);
Packit ae235b
  g_source_set_priority (poll_source, 1);
Packit ae235b
  g_source_set_callback (poll_source, (GSourceFunc) poll_source_callback, &success, NULL);
Packit ae235b
  g_source_attach (poll_source, NULL);
Packit ae235b
  g_source_unref (poll_source);
Packit ae235b
Packit ae235b
  g_idle_add_full (2, check_source_readability_callback, GINT_TO_POINTER (FALSE), NULL);
Packit ae235b
  g_idle_add_full (3, write_callback, NULL, NULL);
Packit ae235b
  g_idle_add_full (4, check_source_and_quit_callback, GINT_TO_POINTER (FALSE), NULL);
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  g_assert_cmpint (success, ==, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static void
Packit ae235b
test_pollable_unix (void)
Packit ae235b
{
Packit ae235b
  int pipefds[2], status, fd;
Packit ae235b
Packit ae235b
  status = pipe (pipefds);
Packit ae235b
  g_assert_cmpint (status, ==, 0);
Packit ae235b
Packit ae235b
  in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
Packit ae235b
  out = g_unix_output_stream_new (pipefds[1], TRUE);
Packit ae235b
Packit ae235b
  test_streams ();
Packit ae235b
Packit ae235b
  g_object_unref (in);
Packit ae235b
  g_object_unref (out);
Packit ae235b
Packit ae235b
  /* Non-pipe/socket unix streams are not pollable */
Packit ae235b
  fd = g_open ("/dev/null", O_RDWR, 0);
Packit ae235b
  g_assert_cmpint (fd, !=, -1);
Packit ae235b
  in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (fd, FALSE));
Packit ae235b
  out = g_unix_output_stream_new (fd, FALSE);
Packit ae235b
Packit ae235b
  g_assert (!g_pollable_input_stream_can_poll (in));
Packit ae235b
  g_assert (!g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
Packit ae235b
Packit ae235b
  g_object_unref (in);
Packit ae235b
  g_object_unref (out);
Packit ae235b
  close (fd);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_pollable_converter (void)
Packit ae235b
{
Packit ae235b
  GConverter *converter;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GInputStream *ibase;
Packit ae235b
  int pipefds[2], status;
Packit ae235b
Packit ae235b
  status = pipe (pipefds);
Packit ae235b
  g_assert_cmpint (status, ==, 0);
Packit ae235b
Packit ae235b
  ibase = G_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
Packit ae235b
  converter = G_CONVERTER (g_charset_converter_new ("UTF-8", "UTF-8", &error));
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  in = G_POLLABLE_INPUT_STREAM (g_converter_input_stream_new (ibase, converter));
Packit ae235b
  g_object_unref (converter);
Packit ae235b
  g_object_unref (ibase);
Packit ae235b
Packit ae235b
  out = g_unix_output_stream_new (pipefds[1], TRUE);
Packit ae235b
Packit ae235b
  test_streams ();
Packit ae235b
Packit ae235b
  g_object_unref (in);
Packit ae235b
  g_object_unref (out);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
client_connected (GObject      *source,
Packit ae235b
		  GAsyncResult *result,
Packit ae235b
		  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSocketClient *client = G_SOCKET_CLIENT (source);
Packit ae235b
  GSocketConnection **conn = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  *conn = g_socket_client_connect_finish (client, result, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
server_connected (GObject      *source,
Packit ae235b
		  GAsyncResult *result,
Packit ae235b
		  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSocketListener *listener = G_SOCKET_LISTENER (source);
Packit ae235b
  GSocketConnection **conn = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  *conn = g_socket_listener_accept_finish (listener, result, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_pollable_socket (void)
Packit ae235b
{
Packit ae235b
  GInetAddress *iaddr;
Packit ae235b
  GSocketAddress *saddr, *effective_address;
Packit ae235b
  GSocketListener *listener;
Packit ae235b
  GSocketClient *client;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GSocketConnection *client_conn = NULL, *server_conn = NULL;
Packit ae235b
Packit ae235b
  iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
Packit ae235b
  saddr = g_inet_socket_address_new (iaddr, 0);
Packit ae235b
  g_object_unref (iaddr);
Packit ae235b
Packit ae235b
  listener = g_socket_listener_new ();
Packit ae235b
  g_socket_listener_add_address (listener, saddr,
Packit ae235b
				 G_SOCKET_TYPE_STREAM,
Packit ae235b
				 G_SOCKET_PROTOCOL_TCP,
Packit ae235b
				 NULL,
Packit ae235b
				 &effective_address,
Packit ae235b
				 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_object_unref (saddr);
Packit ae235b
Packit ae235b
  client = g_socket_client_new ();
Packit ae235b
Packit ae235b
  g_socket_client_connect_async (client,
Packit ae235b
				 G_SOCKET_CONNECTABLE (effective_address),
Packit ae235b
				 NULL, client_connected, &client_conn);
Packit ae235b
  g_socket_listener_accept_async (listener, NULL,
Packit ae235b
				  server_connected, &server_conn);
Packit ae235b
Packit ae235b
  while (!client_conn || !server_conn)
Packit ae235b
    g_main_context_iteration (NULL, TRUE);
Packit ae235b
Packit ae235b
  in = G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (client_conn)));
Packit ae235b
  out = g_io_stream_get_output_stream (G_IO_STREAM (server_conn));
Packit ae235b
Packit ae235b
  test_streams ();
Packit ae235b
Packit ae235b
  g_object_unref (client_conn);
Packit ae235b
  g_object_unref (server_conn);
Packit ae235b
  g_object_unref (client);
Packit ae235b
  g_object_unref (listener);
Packit ae235b
  g_object_unref (effective_address);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  g_test_add_func ("/pollable/unix", test_pollable_unix);
Packit ae235b
  g_test_add_func ("/pollable/converter", test_pollable_converter);
Packit ae235b
#endif
Packit ae235b
  g_test_add_func ("/pollable/socket", test_pollable_socket);
Packit ae235b
Packit ae235b
  return g_test_run();
Packit ae235b
}
Packit ae235b