Blame tests/spawn-test.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
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 Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <io.h>
Packit ae235b
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
run_tests (void)
Packit ae235b
{
Packit ae235b
  GError *err;
Packit ae235b
  gchar *output = NULL;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gchar *erroutput = NULL;
Packit ae235b
  int pipedown[2], pipeup[2];
Packit ae235b
  gchar **argv = 0;
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  err = NULL;
Packit ae235b
  if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
Packit ae235b
                                  NULL, NULL, NULL,
Packit ae235b
                                  &err))
Packit ae235b
    {
Packit ae235b
      g_error_free (err);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_warning ("no error for sync spawn of nonexistent application");
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  err = NULL;
Packit ae235b
  if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
Packit ae235b
                                   &err))
Packit ae235b
    {
Packit ae235b
      g_error_free (err);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_warning ("no error for async spawn of nonexistent application");
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  err = NULL;
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
Packit ae235b
                                  &output, NULL, NULL,
Packit ae235b
                                  &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error: %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_assert (output != NULL);
Packit ae235b
      
Packit ae235b
      if (strcmp (output, "hello\n") != 0)
Packit ae235b
        {
Packit ae235b
          printf ("output was '%s', should have been 'hello'\n",
Packit ae235b
                  output);
Packit ae235b
Packit ae235b
          exit (1);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_free (output);
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  printf ("Running netstat synchronously, collecting its output\n");
Packit ae235b
Packit ae235b
  if (!g_spawn_command_line_sync ("netstat -n",
Packit ae235b
                                  &output, &erroutput, NULL,
Packit ae235b
                                  &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error: %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_assert (output != NULL);
Packit ae235b
      g_assert (erroutput != NULL);
Packit ae235b
      
Packit ae235b
      if (strstr (output, "Active Connections") == 0)
Packit ae235b
        {
Packit ae235b
          printf ("output was '%s', should have contained 'Active Connections'\n",
Packit ae235b
                  output);
Packit ae235b
Packit ae235b
          exit (1);
Packit ae235b
        }
Packit ae235b
      if (erroutput[0] != '\0')
Packit ae235b
	{
Packit ae235b
	  printf ("error output was '%s', should have been empty\n",
Packit ae235b
		  erroutput);
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_free (output);
Packit ae235b
      output = NULL;
Packit ae235b
      g_free (erroutput);
Packit ae235b
      erroutput = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  printf ("Running spawn-test-win32-gui in various ways. Click on the OK buttons.\n");
Packit ae235b
Packit ae235b
  printf ("First asynchronously (without wait).\n");
Packit ae235b
Packit ae235b
  if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error: %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  printf ("Now synchronously, collecting its output.\n");
Packit ae235b
  if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
Packit ae235b
				  &output, &erroutput, NULL,
Packit ae235b
				  &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error: %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_assert (output != NULL);
Packit ae235b
      g_assert (erroutput != NULL);
Packit ae235b
      
Packit ae235b
      if (strcmp (output, "This is stdout\r\n") != 0)
Packit ae235b
        {
Packit ae235b
          printf ("output was '%s', should have been 'This is stdout'\n",
Packit ae235b
                  g_strescape (output, NULL));
Packit ae235b
Packit ae235b
          exit (1);
Packit ae235b
        }
Packit ae235b
      if (strcmp (erroutput, "This is stderr\r\n") != 0)
Packit ae235b
	{
Packit ae235b
	  printf ("error output was '%s', should have been 'This is stderr'\n",
Packit ae235b
		  g_strescape (erroutput, NULL));
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_free (output);
Packit ae235b
      g_free (erroutput);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  printf ("Now with G_SPAWN_FILE_AND_ARGV_ZERO.\n");
Packit ae235b
Packit ae235b
  if (!g_shell_parse_argv ("'.\\spawn-test-win32-gui.exe' this-should-be-argv-zero nop", NULL, &argv, &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error parsing command line? %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_spawn_async (NULL, argv, NULL,
Packit ae235b
		      G_SPAWN_FILE_AND_ARGV_ZERO,
Packit ae235b
		      NULL, NULL, NULL,
Packit ae235b
		      &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error: %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  printf ("Now talking to it through pipes.\n");
Packit ae235b
Packit ae235b
  if (pipe (pipedown) < 0 ||
Packit ae235b
      pipe (pipeup) < 0)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Could not create pipes\n");
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
Packit ae235b
					    pipedown[0], pipeup[1]),
Packit ae235b
                           NULL, &argv,
Packit ae235b
                           &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error parsing command line? %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (!g_spawn_async (NULL, argv, NULL,
Packit ae235b
		      G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
Packit ae235b
		      G_SPAWN_DO_NOT_REAP_CHILD,
Packit ae235b
		      NULL, NULL, NULL,
Packit ae235b
		      &err))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Error: %s\n", err->message);
Packit ae235b
      g_error_free (err);
Packit ae235b
      exit (1);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      int k, n;
Packit ae235b
      char buf[100];
Packit ae235b
Packit ae235b
      if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
Packit ae235b
	{
Packit ae235b
	  int errsv = errno;
Packit ae235b
	  if (k == -1)
Packit ae235b
	    fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
Packit ae235b
	  else
Packit ae235b
	    fprintf (stderr, "Wanted to read %d bytes, got %d\n",
Packit ae235b
		     sizeof (n), k);
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if ((k = read (pipeup[0], buf, n)) != n)
Packit ae235b
	{
Packit ae235b
	  int errsv = errno;
Packit ae235b
	  if (k == -1)
Packit ae235b
	    fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
Packit ae235b
	  else
Packit ae235b
	    fprintf (stderr, "Wanted to read %d bytes, got %d\n",
Packit ae235b
		     n, k);
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      n = strlen ("Bye then");
Packit ae235b
      if (write (pipedown[1], &n, sizeof (n)) == -1 ||
Packit ae235b
	  write (pipedown[1], "Bye then", n) == -1)
Packit ae235b
	{
Packit ae235b
	  int errsv = errno;
Packit ae235b
	  fprintf (stderr, "Write error: %s\n", g_strerror (errsv));
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
Packit ae235b
	{
Packit ae235b
	  int errsv = errno;
Packit ae235b
	  if (k == -1)
Packit ae235b
	    fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
Packit ae235b
	  else
Packit ae235b
	    fprintf (stderr, "Wanted to read %d bytes, got %d\n",
Packit ae235b
		     sizeof (n), k);
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if ((k = read (pipeup[0], buf, n)) != n)
Packit ae235b
	{
Packit ae235b
	  int errsv = errno;
Packit ae235b
	  if (k == -1)
Packit ae235b
	    fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
Packit ae235b
	  else
Packit ae235b
	    fprintf (stderr, "Wanted to read %d bytes, got %d\n",
Packit ae235b
		     n, k);
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  run_tests ();
Packit ae235b
  
Packit ae235b
  return 0;
Packit ae235b
}