Ignore the G_SPAWN_DO_NOT_REAP_CHILD flag, can't be meaninfully
[platform/upstream/glib.git] / tests / spawn-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #ifdef G_OS_WIN32
36 #include <fcntl.h>
37 #endif
38
39
40 static void
41 run_tests (void)
42 {
43   GError *err;
44   gchar *output = NULL;
45 #ifdef G_OS_WIN32
46   gchar *erroutput = NULL;
47   int pipedown[2], pipeup[2];
48   gchar **argv = 0;
49 #endif
50   
51   printf ("The following errors are supposed to occur:\n");
52
53   err = NULL;
54   if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
55                                   NULL, NULL, NULL,
56                                   &err))
57     {
58       fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
59       g_error_free (err);
60     }
61
62   err = NULL;
63   if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
64                                    &err))
65     {
66       fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
67       g_error_free (err);
68     }
69
70   printf ("Errors after this are not supposed to happen:\n");
71   
72   err = NULL;
73 #ifdef G_OS_UNIX
74   if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
75                                   &output, NULL, NULL,
76                                   &err))
77     {
78       fprintf (stderr, "Error: %s\n", err->message);
79       g_error_free (err);
80       exit (1);
81     }
82   else
83     {
84       g_assert (output != NULL);
85       
86       if (strcmp (output, "hello\n") != 0)
87         {
88           printf ("output was '%s', should have been 'hello'\n",
89                   output);
90
91           exit (1);
92         }
93
94       g_free (output);
95     }
96 #else
97 #ifdef G_OS_WIN32
98   printf ("Running ipconfig synchronously, collecting its output\n");
99
100   if (!g_spawn_command_line_sync ("ipconfig /all",
101                                   &output, &erroutput, NULL,
102                                   &err))
103     {
104       fprintf (stderr, "Error: %s\n", err->message);
105       g_error_free (err);
106       exit (1);
107     }
108   else
109     {
110       g_assert (output != NULL);
111       g_assert (erroutput != NULL);
112       
113       if (strstr (output, "IP Configuration") == 0)
114         {
115           printf ("output was '%s', should have contained 'IP Configuration'\n",
116                   output);
117
118           exit (1);
119         }
120       if (erroutput[0] != '\0')
121         {
122           printf ("error output was '%s', should have been empty\n",
123                   erroutput);
124           exit (1);
125         }
126
127       g_free (output);
128       output = NULL;
129       g_free (erroutput);
130       erroutput = NULL;
131     }
132
133   printf ("Starting spawn-test-win32-gui asynchronously (without wait).\n"
134           "Click on the OK buttons.\n");
135
136   if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
137     {
138       fprintf (stderr, "Error: %s\n", err->message);
139       g_error_free (err);
140       exit (1);
141     }
142
143   printf ("Running spawn-test-win32-gui synchronously,\n"
144           "collecting its output. Click on the OK buttons.\n");
145   if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
146                                   &output, &erroutput, NULL,
147                                   &err))
148     {
149       fprintf (stderr, "Error: %s\n", err->message);
150       g_error_free (err);
151       exit (1);
152     }
153   else
154     {
155       g_assert (output != NULL);
156       g_assert (erroutput != NULL);
157       
158       if (strcmp (output, "This is stdout\r\n") != 0)
159         {
160           printf ("output was '%s', should have been 'This is stdout'\n",
161                   g_strescape (output, NULL));
162
163           exit (1);
164         }
165       if (strcmp (erroutput, "This is stderr\r\n") != 0)
166         {
167           printf ("error output was '%s', should have been 'This is stderr'\n",
168                   g_strescape (erroutput, NULL));
169           exit (1);
170         }
171
172       g_free (output);
173       g_free (erroutput);
174     }
175
176   printf ("Running spawn-test-win32-gui asynchronously again.\n"
177           "This time talking to it through pipes. Click on the OK buttons.\n");
178
179   if (pipe (pipedown) < 0 ||
180       pipe (pipeup) < 0)
181     {
182       fprintf (stderr, "Could not create pipes\n");
183       exit (1);
184     }
185
186   if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
187                                             pipedown[0], pipeup[1]),
188                            NULL, &argv,
189                            &err))
190     {
191       fprintf (stderr, "Error parsing command line? %s\n", err->message);
192       g_error_free (err);
193       exit (1);
194     }
195   
196   if (!g_spawn_async (NULL, argv, NULL,
197                       G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
198                       G_SPAWN_DO_NOT_REAP_CHILD,
199                       NULL, NULL, NULL,
200                       &err))
201     {
202       fprintf (stderr, "Error: %s\n", err->message);
203       g_error_free (err);
204       exit (1);
205     }
206   else
207     {
208       int k, n;
209       char buf[100];
210
211       if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
212         {
213           if (k == -1)
214             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
215           else
216             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
217                      sizeof (n), k);
218           exit (1);
219         }
220
221       if ((k = read (pipeup[0], buf, n)) != n)
222         {
223           if (k == -1)
224             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
225           else
226             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
227                      n, k);
228           exit (1);
229         }
230
231       n = strlen ("Bye then");
232       if (write (pipedown[1], &n, sizeof (n)) == -1 ||
233           write (pipedown[1], "Bye then", n) == -1)
234         {
235           fprintf (stderr, "Write error: %s\n", g_strerror (errno));
236           exit (1);
237         }
238
239       if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
240         {
241           if (k == -1)
242             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
243           else
244             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
245                      sizeof (n), k);
246           exit (1);
247         }
248
249       if ((k = read (pipeup[0], buf, n)) != n)
250         {
251           if (k == -1)
252             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
253           else
254             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
255                      n, k);
256           exit (1);
257         }
258     }
259 #endif
260 #endif
261 }
262
263 int
264 main (int   argc,
265       char *argv[])
266 {
267   run_tests ();
268   
269   return 0;
270 }