Blame gio/tests/win32-streams.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 * Copyright (C) 2008 Red Hat, Inc
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work 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.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <glib/glib.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gio/gwin32inputstream.h>
Packit ae235b
#include <gio/gwin32outputstream.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <io.h>
Packit ae235b
Packit ae235b
#include <windows.h>
Packit ae235b
Packit ae235b
#define DATA "abcdefghijklmnopqrstuvwxyz"
Packit ae235b
Packit ae235b
int writer_pipe[2], reader_pipe[2];
Packit ae235b
GCancellable *writer_cancel, *reader_cancel, *main_cancel;
Packit ae235b
GMainLoop *loop;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
writer_thread (gpointer user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *out;
Packit ae235b
  gssize nwrote, offset;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  HANDLE out_handle;
Packit ae235b
Packit ae235b
  g_assert (DuplicateHandle (GetCurrentProcess (),
Packit ae235b
			     (HANDLE) (gintptr) _get_osfhandle (writer_pipe[1]),
Packit ae235b
			     GetCurrentProcess (),
Packit ae235b
			     &out_handle,
Packit ae235b
			     0, FALSE,
Packit ae235b
			     DUPLICATE_SAME_ACCESS));
Packit ae235b
  close (writer_pipe[1]);
Packit ae235b
Packit ae235b
  out = g_win32_output_stream_new (out_handle, TRUE);
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      g_usleep (10);
Packit ae235b
Packit ae235b
      offset = 0;
Packit ae235b
      while (offset < (gssize) sizeof (DATA))
Packit ae235b
	{
Packit ae235b
	  nwrote = g_output_stream_write (out, DATA + offset,
Packit ae235b
					  sizeof (DATA) - offset,
Packit ae235b
					  writer_cancel, &err;;
Packit ae235b
	  if (nwrote <= 0 || err != NULL)
Packit ae235b
	    break;
Packit ae235b
	  offset += nwrote;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_assert (nwrote > 0 || err != NULL);
Packit ae235b
    }
Packit ae235b
  while (err == NULL);
Packit ae235b
Packit ae235b
  if (g_cancellable_is_cancelled (writer_cancel))
Packit ae235b
    {
Packit ae235b
      g_cancellable_cancel (main_cancel);
Packit ae235b
      g_object_unref (out);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_warning ("writer: %s", err->message);
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
reader_thread (gpointer user_data)
Packit ae235b
{
Packit ae235b
  GInputStream *in;
Packit ae235b
  gssize nread = 0, total;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  char buf[sizeof (DATA)];
Packit ae235b
  HANDLE in_handle;
Packit ae235b
Packit ae235b
  g_assert (DuplicateHandle (GetCurrentProcess (),
Packit ae235b
			     (HANDLE) (gintptr) _get_osfhandle (reader_pipe[0]),
Packit ae235b
			     GetCurrentProcess (),
Packit ae235b
			     &in_handle,
Packit ae235b
			     0, FALSE,
Packit ae235b
			     DUPLICATE_SAME_ACCESS));
Packit ae235b
  close (reader_pipe[0]);
Packit ae235b
Packit ae235b
  in = g_win32_input_stream_new (in_handle, TRUE);
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      total = 0;
Packit ae235b
      while (total < (gssize) sizeof (DATA))
Packit ae235b
	{
Packit ae235b
	  nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
Packit ae235b
				       reader_cancel, &err;;
Packit ae235b
	  if (nread <= 0 || err != NULL)
Packit ae235b
	    break;
Packit ae235b
	  total += nread;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (err)
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      if (nread == 0)
Packit ae235b
	{
Packit ae235b
	  g_assert (err == NULL);
Packit ae235b
	  /* pipe closed */
Packit ae235b
	  g_object_unref (in);
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_assert_cmpstr (buf, ==, DATA);
Packit ae235b
      g_assert (!g_cancellable_is_cancelled (reader_cancel));
Packit ae235b
    }
Packit ae235b
  while (err == NULL);
Packit ae235b
Packit ae235b
  g_warning ("reader: %s", err->message);
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
}
Packit ae235b
Packit ae235b
char main_buf[sizeof (DATA)];
Packit ae235b
gssize main_len, main_offset;
Packit ae235b
Packit ae235b
static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
Packit ae235b
static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
Packit ae235b
Packit ae235b
static void
Packit ae235b
do_main_cancel (GOutputStream *out)
Packit ae235b
{
Packit ae235b
  g_output_stream_close (out, NULL, NULL);
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
readable (GObject *source, GAsyncResult *res, gpointer user_data)
Packit ae235b
{
Packit ae235b
  GInputStream *in = G_INPUT_STREAM (source);
Packit ae235b
  GOutputStream *out = user_data;
Packit ae235b
  GError *err = NULL;
Packit ae235b
Packit ae235b
  main_len = g_input_stream_read_finish (in, res, &err;;
Packit ae235b
Packit ae235b
  if (g_cancellable_is_cancelled (main_cancel))
Packit ae235b
    {
Packit ae235b
      do_main_cancel (out);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert (err == NULL);
Packit ae235b
Packit ae235b
  main_offset = 0;
Packit ae235b
  g_output_stream_write_async (out, main_buf, main_len,
Packit ae235b
			       G_PRIORITY_DEFAULT, main_cancel,
Packit ae235b
			       writable, in);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
writable (GObject *source, GAsyncResult *res, gpointer user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *out = G_OUTPUT_STREAM (source);
Packit ae235b
  GInputStream *in = user_data;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  gssize nwrote;
Packit ae235b
Packit ae235b
  nwrote = g_output_stream_write_finish (out, res, &err;;
Packit ae235b
Packit ae235b
  if (g_cancellable_is_cancelled (main_cancel))
Packit ae235b
    {
Packit ae235b
      do_main_cancel (out);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert (err == NULL);
Packit ae235b
  g_assert_cmpint (nwrote, <=, main_len - main_offset);
Packit ae235b
Packit ae235b
  main_offset += nwrote;
Packit ae235b
  if (main_offset == main_len)
Packit ae235b
    {
Packit ae235b
      g_input_stream_read_async (in, main_buf, sizeof (main_buf),
Packit ae235b
				 G_PRIORITY_DEFAULT, main_cancel,
Packit ae235b
				 readable, out);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_output_stream_write_async (out, main_buf + main_offset,
Packit ae235b
				   main_len - main_offset,
Packit ae235b
				   G_PRIORITY_DEFAULT, main_cancel,
Packit ae235b
				   writable, in);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
timeout (gpointer cancellable)
Packit ae235b
{
Packit ae235b
  g_cancellable_cancel (cancellable);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_pipe_io (void)
Packit ae235b
{
Packit ae235b
  GThread *writer, *reader;
Packit ae235b
  GInputStream *in;
Packit ae235b
  GOutputStream *out;
Packit ae235b
  HANDLE in_handle, out_handle;
Packit ae235b
Packit ae235b
  /* Split off two (additional) threads, a reader and a writer. From
Packit ae235b
   * the writer thread, write data synchronously in small chunks,
Packit ae235b
   * which gets read asynchronously by the main thread and then
Packit ae235b
   * written asynchronously to the reader thread, which reads it
Packit ae235b
   * synchronously. Eventually a timeout in the main thread will cause
Packit ae235b
   * it to cancel the writer thread, which will in turn cancel the
Packit ae235b
   * read op in the main thread, which will then close the pipe to
Packit ae235b
   * the reader thread, causing the read op to fail.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  g_assert (_pipe (writer_pipe, 10, _O_BINARY) == 0 && _pipe (reader_pipe, 10, _O_BINARY) == 0);
Packit ae235b
Packit ae235b
  writer_cancel = g_cancellable_new ();
Packit ae235b
  reader_cancel = g_cancellable_new ();
Packit ae235b
  main_cancel = g_cancellable_new ();
Packit ae235b
Packit ae235b
  writer = g_thread_new ("writer", writer_thread, NULL);
Packit ae235b
  reader = g_thread_new ("reader", reader_thread, NULL);
Packit ae235b
Packit ae235b
  g_assert (DuplicateHandle (GetCurrentProcess (),
Packit ae235b
			     (HANDLE) (gintptr) _get_osfhandle (writer_pipe[0]),
Packit ae235b
			     GetCurrentProcess (),
Packit ae235b
			     &in_handle,
Packit ae235b
			     0, FALSE,
Packit ae235b
			     DUPLICATE_SAME_ACCESS));
Packit ae235b
  close (writer_pipe[0]);
Packit ae235b
Packit ae235b
  g_assert (DuplicateHandle (GetCurrentProcess (),
Packit ae235b
			     (HANDLE) (gintptr) _get_osfhandle (reader_pipe[1]),
Packit ae235b
			     GetCurrentProcess (),
Packit ae235b
			     &out_handle,
Packit ae235b
			     0, FALSE,
Packit ae235b
			     DUPLICATE_SAME_ACCESS));
Packit ae235b
  close (reader_pipe[1]);
Packit ae235b
Packit ae235b
  in = g_win32_input_stream_new (in_handle, TRUE);
Packit ae235b
  out = g_win32_output_stream_new (out_handle, TRUE);
Packit ae235b
Packit ae235b
  g_input_stream_read_async (in, main_buf, sizeof (main_buf),
Packit ae235b
			     G_PRIORITY_DEFAULT, main_cancel,
Packit ae235b
			     readable, out);
Packit ae235b
Packit ae235b
  g_timeout_add (500, timeout, writer_cancel);
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, TRUE);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  g_thread_join (reader);
Packit ae235b
  g_thread_join (writer);
Packit ae235b
Packit ae235b
  g_object_unref (main_cancel);
Packit ae235b
  g_object_unref (reader_cancel);
Packit ae235b
  g_object_unref (writer_cancel);
Packit ae235b
  g_object_unref (in);
Packit ae235b
  g_object_unref (out);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct _PipeIOOverlapReader
Packit ae235b
{
Packit ae235b
  char buf[sizeof (DATA)];
Packit ae235b
  GInputStream *in;
Packit ae235b
  GThread *thread;
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
  gboolean success;
Packit ae235b
} PipeIOOverlapReader;
Packit ae235b
Packit ae235b
#define TEST_PIPE_IO_OVERLAP (1024 * 4)
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
pipe_io_overlap_reader_thread (gpointer user_data)
Packit ae235b
{
Packit ae235b
  PipeIOOverlapReader *p = user_data;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  gsize read;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < TEST_PIPE_IO_OVERLAP; ++i) {
Packit ae235b
    memset (p->buf, 0, sizeof (p->buf));
Packit ae235b
    g_input_stream_read_all (p->in, p->buf, sizeof (p->buf),
Packit ae235b
                             &read, NULL, &err;;
Packit ae235b
Packit ae235b
    g_assert_cmpuint (read, ==, sizeof (p->buf));
Packit ae235b
    g_assert_no_error (err);
Packit ae235b
    g_assert_cmpstr (p->buf, ==, DATA);
Packit ae235b
  }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
pipe_io_overlap_writer_thread (gpointer user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *out = user_data;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  gsize bytes_written;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < TEST_PIPE_IO_OVERLAP; ++i) {
Packit ae235b
    g_output_stream_write_all (out, DATA, sizeof (DATA),
Packit ae235b
                               &bytes_written, NULL, &err;;
Packit ae235b
Packit ae235b
    g_assert_cmpuint (bytes_written, ==, sizeof (DATA));
Packit ae235b
    g_assert_no_error (err);
Packit ae235b
  }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_pipe_io_overlap (void)
Packit ae235b
{
Packit ae235b
  GOutputStream *out_server, *out_client;
Packit ae235b
  GThread *writer_server, *writer_client;
Packit ae235b
  PipeIOOverlapReader rs, rc;
Packit ae235b
  HANDLE server, client;
Packit ae235b
  gchar name[256];
Packit ae235b
Packit ae235b
  g_snprintf (name, sizeof (name),
Packit ae235b
              "\\\\.\\pipe\\gtest-io-overlap-%u", (guint) GetCurrentProcessId ());
Packit ae235b
Packit ae235b
  server = CreateNamedPipe (name,
Packit ae235b
                            PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
Packit ae235b
                            PIPE_READMODE_BYTE | PIPE_WAIT,
Packit ae235b
                            1, 0, 0, 0, NULL);
Packit ae235b
  g_assert (server != INVALID_HANDLE_VALUE);
Packit ae235b
Packit ae235b
  client = CreateFile (name, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
Packit ae235b
  g_assert (client != INVALID_HANDLE_VALUE);
Packit ae235b
Packit ae235b
  out_server = g_win32_output_stream_new (server, TRUE);
Packit ae235b
  writer_server = g_thread_new ("writer_server", pipe_io_overlap_writer_thread, out_server);
Packit ae235b
  rs.in = g_win32_input_stream_new (server, TRUE);
Packit ae235b
  rs.thread = g_thread_new ("reader_server", pipe_io_overlap_reader_thread, &rs);
Packit ae235b
Packit ae235b
  out_client = g_win32_output_stream_new (client, TRUE);
Packit ae235b
  writer_client = g_thread_new ("writer_client", pipe_io_overlap_writer_thread, out_client);
Packit ae235b
  rc.in = g_win32_input_stream_new (client, TRUE);
Packit ae235b
  rc.thread = g_thread_new ("reader_client", pipe_io_overlap_reader_thread, &rc);
Packit ae235b
Packit ae235b
  g_thread_join (writer_client);
Packit ae235b
  g_thread_join (writer_server);
Packit ae235b
  g_thread_join (rc.thread);
Packit ae235b
  g_thread_join (rs.thread);
Packit ae235b
Packit ae235b
  g_object_unref (rs.in);
Packit ae235b
  g_object_unref (rc.in);
Packit ae235b
  g_object_unref (out_server);
Packit ae235b
  g_object_unref (out_client);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
pipe_io_concurrent_writer_thread (gpointer user_data)
Packit ae235b
{
Packit ae235b
  GOutputStream *out = user_data;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  gsize bytes_written;
Packit ae235b
Packit ae235b
  g_output_stream_write_all (out, DATA, 1, &bytes_written, NULL, &err;;
Packit ae235b
Packit ae235b
  g_assert_cmpuint (bytes_written, ==, 1);
Packit ae235b
  g_assert_no_error (err);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
pipe_io_concurrent_reader_thread (gpointer user_data)
Packit ae235b
{
Packit ae235b
  PipeIOOverlapReader *p = user_data;
Packit ae235b
  GError *err = NULL;
Packit ae235b
  gsize read;
Packit ae235b
Packit ae235b
  memset (p->buf, 0, sizeof (p->buf));
Packit ae235b
  p->success = g_input_stream_read_all (p->in, p->buf, 1, &read, p->cancellable, &err;;
Packit ae235b
Packit ae235b
  /* only one thread will succeed, the other will be cancelled */
Packit ae235b
  if (p->success)
Packit ae235b
    {
Packit ae235b
      /* continue the main thread */
Packit ae235b
      write (writer_pipe[1], "", 1);
Packit ae235b
      g_assert_cmpuint (read, ==, 1);
Packit ae235b
      g_assert_no_error (err);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_pipe_io_concurrent (void)
Packit ae235b
{
Packit ae235b
  GOutputStream *out_server;
Packit ae235b
  GThread *writer_server;
Packit ae235b
  PipeIOOverlapReader rc1, rc2;
Packit ae235b
  HANDLE server, client;
Packit ae235b
  gchar name[256], c;
Packit ae235b
Packit ae235b
  g_snprintf (name, sizeof (name),
Packit ae235b
              "\\\\.\\pipe\\gtest-io-concurrent-%u", (guint) GetCurrentProcessId ());
Packit ae235b
Packit ae235b
  server = CreateNamedPipe (name,
Packit ae235b
                            PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
Packit ae235b
                            PIPE_READMODE_BYTE | PIPE_WAIT,
Packit ae235b
                            1, 0, 0, 0, NULL);
Packit ae235b
  g_assert (server != INVALID_HANDLE_VALUE);
Packit ae235b
  g_assert (_pipe (writer_pipe, 10, _O_BINARY) == 0);
Packit ae235b
Packit ae235b
  client = CreateFile (name, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
Packit ae235b
  g_assert (client != INVALID_HANDLE_VALUE);
Packit ae235b
Packit ae235b
  rc1.in = g_win32_input_stream_new (client, TRUE);
Packit ae235b
  rc1.success = FALSE;
Packit ae235b
  rc1.cancellable = g_cancellable_new ();
Packit ae235b
  rc1.thread = g_thread_new ("reader_client", pipe_io_concurrent_reader_thread, &rc1;;
Packit ae235b
Packit ae235b
  rc2.in = g_win32_input_stream_new (client, TRUE);
Packit ae235b
  rc2.success = FALSE;
Packit ae235b
  rc2.cancellable = g_cancellable_new ();
Packit ae235b
  rc2.thread = g_thread_new ("reader_client", pipe_io_concurrent_reader_thread, &rc2;;
Packit ae235b
Packit ae235b
  /* FIXME: how to synchronize on both reader thread waiting in read,
Packit ae235b
     before starting the writer thread? */
Packit ae235b
  g_usleep (G_USEC_PER_SEC / 10);
Packit ae235b
Packit ae235b
  out_server = g_win32_output_stream_new (server, TRUE);
Packit ae235b
  writer_server = g_thread_new ("writer_server", pipe_io_concurrent_writer_thread, out_server);
Packit ae235b
Packit ae235b
  read (writer_pipe[0], &c, 1);
Packit ae235b
Packit ae235b
  g_assert (rc1.success ^ rc2.success);
Packit ae235b
Packit ae235b
  g_cancellable_cancel (rc1.cancellable);
Packit ae235b
  g_cancellable_cancel (rc2.cancellable);
Packit ae235b
Packit ae235b
  g_thread_join (writer_server);
Packit ae235b
  g_thread_join (rc1.thread);
Packit ae235b
  g_thread_join (rc2.thread);
Packit ae235b
Packit ae235b
  g_object_unref (rc1.in);
Packit ae235b
  g_object_unref (rc2.in);
Packit ae235b
  g_object_unref (out_server);
Packit ae235b
Packit ae235b
  close (writer_pipe[0]);
Packit ae235b
  close (writer_pipe[1]);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
readable_cancel (GObject *source, GAsyncResult *res, gpointer user_data)
Packit ae235b
{
Packit ae235b
  GInputStream *in = G_INPUT_STREAM (source);
Packit ae235b
  GError *err = NULL;
Packit ae235b
  gssize len;
Packit ae235b
Packit ae235b
  len = g_input_stream_read_finish (in, res, &err;;
Packit ae235b
  g_assert_cmpint (len, ==, -1);
Packit ae235b
  g_assert_error (err, G_IO_ERROR, G_IO_ERROR_CANCELLED);
Packit ae235b
  g_error_free (err);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_pipe_io_cancel (void)
Packit ae235b
{
Packit ae235b
  GInputStream *in;
Packit ae235b
  GOutputStream *out;
Packit ae235b
  HANDLE in_handle, out_handle;
Packit ae235b
  gchar name[256];
Packit ae235b
Packit ae235b
  g_snprintf (name, sizeof (name),
Packit ae235b
              "\\\\.\\pipe\\gtest-io-cancel-%u", (guint) GetCurrentProcessId ());
Packit ae235b
Packit ae235b
  in_handle = CreateNamedPipe (name,
Packit ae235b
                               PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
Packit ae235b
                               PIPE_READMODE_BYTE | PIPE_WAIT,
Packit ae235b
                               1, 0, 0, 0, NULL);
Packit ae235b
  g_assert (in_handle != INVALID_HANDLE_VALUE);
Packit ae235b
Packit ae235b
  out_handle = CreateFile (name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Packit ae235b
  g_assert (out_handle != INVALID_HANDLE_VALUE);
Packit ae235b
Packit ae235b
  in = g_win32_input_stream_new (in_handle, TRUE);
Packit ae235b
  out = g_win32_output_stream_new (out_handle, TRUE);
Packit ae235b
Packit ae235b
  reader_cancel = g_cancellable_new ();
Packit ae235b
  g_input_stream_read_async (in, main_buf, sizeof (main_buf),
Packit ae235b
                             G_PRIORITY_DEFAULT, reader_cancel,
Packit ae235b
                             readable_cancel, out);
Packit ae235b
Packit ae235b
  g_timeout_add (500, timeout, reader_cancel);
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, TRUE);
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  g_object_unref (reader_cancel);
Packit ae235b
  g_object_unref (in);
Packit ae235b
  g_object_unref (out);
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
  g_test_add_func ("/win32-streams/pipe-io-test", test_pipe_io);
Packit ae235b
  g_test_add_func ("/win32-streams/pipe-io-cancel-test", test_pipe_io_cancel);
Packit ae235b
  g_test_add_func ("/win32-streams/pipe-io-overlap-test", test_pipe_io_overlap);
Packit ae235b
  g_test_add_func ("/win32-streams/pipe-io-concurrent-test", test_pipe_io_concurrent);
Packit ae235b
Packit ae235b
  return g_test_run();
Packit ae235b
}