for msvc build
[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 ("Starting spawn-test-win32-gui asynchronously (without wait).\n"
135           "Click on the OK buttons.\n");
136
137   if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
138     {
139       fprintf (stderr, "Error: %s\n", err->message);
140       g_error_free (err);
141       exit (1);
142     }
143
144   printf ("Running spawn-test-win32-gui synchronously,\n"
145           "collecting its output. Click on the OK buttons.\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 ("Running spawn-test-win32-gui asynchronously again.\n"
178           "This time talking to it through pipes. Click on the OK buttons.\n");
179
180   if (pipe (pipedown) < 0 ||
181       pipe (pipeup) < 0)
182     {
183       fprintf (stderr, "Could not create pipes\n");
184       exit (1);
185     }
186
187   if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
188                                             pipedown[0], pipeup[1]),
189                            NULL, &argv,
190                            &err))
191     {
192       fprintf (stderr, "Error parsing command line? %s\n", err->message);
193       g_error_free (err);
194       exit (1);
195     }
196   
197   if (!g_spawn_async (NULL, argv, NULL,
198                       G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
199                       G_SPAWN_DO_NOT_REAP_CHILD,
200                       NULL, NULL, NULL,
201                       &err))
202     {
203       fprintf (stderr, "Error: %s\n", err->message);
204       g_error_free (err);
205       exit (1);
206     }
207   else
208     {
209       int k, n;
210       char buf[100];
211
212       if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
213         {
214           if (k == -1)
215             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
216           else
217             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
218                      sizeof (n), k);
219           exit (1);
220         }
221
222       if ((k = read (pipeup[0], buf, n)) != n)
223         {
224           if (k == -1)
225             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
226           else
227             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
228                      n, k);
229           exit (1);
230         }
231
232       n = strlen ("Bye then");
233       if (write (pipedown[1], &n, sizeof (n)) == -1 ||
234           write (pipedown[1], "Bye then", n) == -1)
235         {
236           fprintf (stderr, "Write error: %s\n", g_strerror (errno));
237           exit (1);
238         }
239
240       if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (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                      sizeof (n), k);
247           exit (1);
248         }
249
250       if ((k = read (pipeup[0], buf, n)) != n)
251         {
252           if (k == -1)
253             fprintf (stderr, "Read error: %s\n", g_strerror (errno));
254           else
255             fprintf (stderr, "Wanted to read %d bytes, got %d\n",
256                      n, k);
257           exit (1);
258         }
259     }
260 #endif
261 #endif
262 }
263
264 int
265 main (int   argc,
266       char *argv[])
267 {
268   run_tests ();
269   
270   return 0;
271 }