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