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