glib/gspawn-win32.c Implement G_SPAWN_FILE_AND_ARGV_ZERO. (#136792, Bruce
[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 #include <io.h>
38 #endif
39
40
41 static void
42 run_tests (void)
43 {
44   GError *err;
45   gchar *output = NULL;
46 #ifdef G_OS_WIN32
47   gchar *erroutput = NULL;
48   int pipedown[2], pipeup[2];
49   gchar **argv = 0;
50 #endif
51   
52   printf ("The following errors are supposed to occur:\n");
53
54   err = NULL;
55   if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
56                                   NULL, NULL, NULL,
57                                   &err))
58     {
59       fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
60       g_error_free (err);
61     }
62
63   err = NULL;
64   if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
65                                    &err))
66     {
67       fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
68       g_error_free (err);
69     }
70
71   printf ("Errors after this are not supposed to happen:\n");
72   
73   err = NULL;
74 #ifdef G_OS_UNIX
75   if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
76                                   &output, NULL, NULL,
77                                   &err))
78     {
79       fprintf (stderr, "Error: %s\n", err->message);
80       g_error_free (err);
81       exit (1);
82     }
83   else
84     {
85       g_assert (output != NULL);
86       
87       if (strcmp (output, "hello\n") != 0)
88         {
89           printf ("output was '%s', should have been 'hello'\n",
90                   output);
91
92           exit (1);
93         }
94
95       g_free (output);
96     }
97 #else
98 #ifdef G_OS_WIN32
99   printf ("Running ipconfig synchronously, collecting its output\n");
100
101   if (!g_spawn_command_line_sync ("ipconfig /all",
102                                   &output, &erroutput, NULL,
103                                   &err))
104     {
105       fprintf (stderr, "Error: %s\n", err->message);
106       g_error_free (err);
107       exit (1);
108     }
109   else
110     {
111       g_assert (output != NULL);
112       g_assert (erroutput != NULL);
113       
114       if (strstr (output, "IP Configuration") == 0)
115         {
116           printf ("output was '%s', should have contained 'IP Configuration'\n",
117                   output);
118
119           exit (1);
120         }
121       if (erroutput[0] != '\0')
122         {
123           printf ("error output was '%s', should have been empty\n",
124                   erroutput);
125           exit (1);
126         }
127
128       g_free (output);
129       output = NULL;
130       g_free (erroutput);
131       erroutput = NULL;
132     }
133
134   printf ("Running spawn-test-win32-gui in various ways. Click on the OK buttons.\n");
135
136   printf ("First asynchronously (without wait).\n");
137
138   if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
139     {
140       fprintf (stderr, "Error: %s\n", err->message);
141       g_error_free (err);
142       exit (1);
143     }
144
145   printf ("Now synchronously, collecting its output.\n");
146   if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
147                                   &output, &erroutput, NULL,
148                                   &err))
149     {
150       fprintf (stderr, "Error: %s\n", err->message);
151       g_error_free (err);
152       exit (1);
153     }
154   else
155     {
156       g_assert (output != NULL);
157       g_assert (erroutput != NULL);
158       
159       if (strcmp (output, "This is stdout\r\n") != 0)
160         {
161           printf ("output was '%s', should have been 'This is stdout'\n",
162                   g_strescape (output, NULL));
163
164           exit (1);
165         }
166       if (strcmp (erroutput, "This is stderr\r\n") != 0)
167         {
168           printf ("error output was '%s', should have been 'This is stderr'\n",
169                   g_strescape (erroutput, NULL));
170           exit (1);
171         }
172
173       g_free (output);
174       g_free (erroutput);
175     }
176
177   printf ("Now with G_SPAWN_FILE_AND_ARGV_ZERO.\n");
178
179   if (!g_shell_parse_argv ("'.\\spawn-test-win32-gui.exe' this-should-be-argv-zero nop", NULL, &argv, &err))
180     {
181       fprintf (stderr, "Error parsing command line? %s\n", err->message);
182       g_error_free (err);
183       exit (1);
184     }
185
186   if (!g_spawn_async (NULL, argv, NULL,
187                       G_SPAWN_FILE_AND_ARGV_ZERO,
188                       NULL, NULL, NULL,
189                       &err))
190     {
191       fprintf (stderr, "Error: %s\n", err->message);
192       g_error_free (err);
193       exit (1);
194     }
195
196   printf ("Now talking to it through pipes.\n");
197
198   if (pipe (pipedown) < 0 ||
199       pipe (pipeup) < 0)
200     {
201       fprintf (stderr, "Could not create pipes\n");
202       exit (1);
203     }
204
205   if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
206                                             pipedown[0], pipeup[1]),
207                            NULL, &argv,
208                            &err))
209     {
210       fprintf (stderr, "Error parsing command line? %s\n", err->message);
211       g_error_free (err);
212       exit (1);
213     }
214   
215   if (!g_spawn_async (NULL, argv, NULL,
216                       G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
217                       G_SPAWN_DO_NOT_REAP_CHILD,
218                       NULL, NULL, NULL,
219                       &err))
220     {
221       fprintf (stderr, "Error: %s\n", err->message);
222       g_error_free (err);
223       exit (1);
224     }
225   else
226     {
227       int k, n;
228       char buf[100];
229
230       if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
231         {
232           if (k == -1)
233             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
234           else
235             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
236                      sizeof (n), k);
237           exit (1);
238         }
239
240       if ((k = read (pipeup[0], buf, n)) != n)
241         {
242           if (k == -1)
243             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
244           else
245             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
246                      n, k);
247           exit (1);
248         }
249
250       n = strlen ("Bye then");
251       if (write (pipedown[1], &n, sizeof (n)) == -1 ||
252           write (pipedown[1], "Bye then", n) == -1)
253         {
254           fprintf (stderr, "Write error: %s\n", g_strerror (errno));
255           exit (1);
256         }
257
258       if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
259         {
260           if (k == -1)
261             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
262           else
263             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
264                      sizeof (n), k);
265           exit (1);
266         }
267
268       if ((k = read (pipeup[0], buf, n)) != n)
269         {
270           if (k == -1)
271             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
272           else
273             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
274                      n, k);
275           exit (1);
276         }
277     }
278 #endif
279 #endif
280 }
281
282 int
283 main (int   argc,
284       char *argv[])
285 {
286   run_tests ();
287   
288   return 0;
289 }