f95bf5ae0ffd142110dfbef37c62987a79296044
[platform/upstream/glib.git] / tests / gio-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2000  Tor Lillqvist
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 /* A test program for the main loop and IO channel code.
21  * Just run it. Optional parameter is number of sub-processes.
22  */
23
24 #include "config.h"
25
26 #include <glib.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
31 #include <time.h>
32
33 #ifdef G_OS_WIN32
34   #include <io.h>
35   #include <fcntl.h>
36   #include <process.h>
37 #else
38   #ifdef HAVE_UNISTD_H
39     #include <unistd.h>
40   #endif
41 #endif
42
43 static int nrunning;
44 static GMainLoop *main_loop;
45
46 #define BUFSIZE 5000            /* Larger than the circular buffer in
47                                  * giowin32.c on purpose.
48                                  */
49
50 static int nkiddies;
51
52 static struct {
53   int fd;
54   int seq;
55 } *seqtab;
56
57 static GIOError
58 read_all (int         fd,
59           GIOChannel *channel,
60           char       *buffer,
61           guint       nbytes,
62           guint      *bytes_read)
63 {
64   guint left = nbytes;
65   guint nb;
66   GIOError error = G_IO_ERROR_NONE;
67   char *bufp = buffer;
68
69   /* g_io_channel_read() doesn't necessarily return all the
70    * data we want at once.
71    */
72   *bytes_read = 0;
73   while (left)
74     {
75       error = g_io_channel_read (channel, bufp, left, &nb);
76       
77       if (error != G_IO_ERROR_NONE)
78         {
79           g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
80                    (error == G_IO_ERROR_AGAIN ? "AGAIN" :
81                     (error == G_IO_ERROR_INVAL ? "INVAL" :
82                      (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
83           if (error == G_IO_ERROR_AGAIN)
84             continue;
85           break;
86         }
87       if (nb == 0)
88         return error;
89       left -= nb;
90       bufp += nb;
91       *bytes_read += nb;
92     }
93   return error;
94 }
95
96 static void
97 shutdown_source (gpointer data)
98 {
99   if (g_source_remove (*(guint *) data))
100     {
101       nrunning--;
102       if (nrunning == 0)
103         g_main_quit (main_loop);
104     }
105 }
106
107 static gboolean
108 recv_message (GIOChannel  *channel,
109               GIOCondition cond,
110               gpointer    data)
111 {
112   gint fd = g_io_channel_unix_get_fd (channel);
113   gboolean retval = TRUE;
114
115   g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
116            (cond & G_IO_ERR) ? " ERR" : "",
117            (cond & G_IO_HUP) ? " HUP" : "",
118            (cond & G_IO_IN)  ? " IN"  : "",
119            (cond & G_IO_PRI) ? " PRI" : "");
120
121   if (cond & (G_IO_ERR | G_IO_HUP))
122     {
123       shutdown_source (data);
124       retval = FALSE;
125     }
126
127   if (cond & G_IO_IN)
128     {
129       char buf[BUFSIZE];
130       guint nbytes;
131       guint nb;
132       int i, j, seq;
133       GIOError error;
134       
135       error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
136       if (error == G_IO_ERROR_NONE)
137         {
138           if (nb == 0)
139             {
140               g_print ("gio-test: ...from %d: EOF\n", fd);
141               shutdown_source (data);
142               return FALSE;
143             }
144           
145           g_assert (nb == sizeof (nbytes));
146
147           for (i = 0; i < nkiddies; i++)
148             if (seqtab[i].fd == fd)
149               {
150                 if (seq != seqtab[i].seq)
151                   {
152                     g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
153                              fd, seq, seqtab[i].seq);
154                     g_assert_not_reached ();
155                   }
156                 seqtab[i].seq++;
157                 break;
158               }
159
160           error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
161         }
162
163       if (error != G_IO_ERROR_NONE)
164         return FALSE;
165       
166       if (nb == 0)
167         {
168           g_print ("gio-test: ...from %d: EOF\n", fd);
169           shutdown_source (data);
170           return FALSE;
171         }
172       
173       g_assert (nb == sizeof (nbytes));
174
175       if (nbytes >= BUFSIZE)
176         {
177           g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
178           g_assert_not_reached ();
179         }
180       g_assert (nbytes >= 0 && nbytes < BUFSIZE);
181       
182       g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
183       
184       if (nbytes > 0)
185         {
186           error = read_all (fd, channel, buf, nbytes, &nb);
187
188           if (error != G_IO_ERROR_NONE)
189             return FALSE;
190
191           if (nb == 0)
192             {
193               g_print ("gio-test: ...from %d: EOF\n", fd);
194               shutdown_source (data);
195               return FALSE;
196             }
197       
198           for (j = 0; j < nbytes; j++)
199             if (buf[j] != ' ' + ((nbytes + j) % 95))
200               {
201                 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
202                          fd, j, buf[j], 'a' + ((nbytes + j) % 32));
203                 g_assert_not_reached ();
204               }
205           g_print ("gio-test: ...from %d: OK\n", fd);
206         }
207     }
208   return retval;
209 }
210
211 int
212 main (int    argc,
213       char **argv)
214 {
215   if (argc < 3)
216     {
217       /* Parent */
218       
219       GIOChannel *my_read_channel;
220       gchar *cmdline;
221       guint *id;
222       int i;
223 #ifdef G_OS_WIN32
224       GTimeVal start, end;
225       GPollFD pollfd;
226       int pollresult;
227 #endif
228
229       nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
230       seqtab = g_malloc (nkiddies * 2 * sizeof (int));
231
232       for (i = 0; i < nkiddies; i++)
233         {
234           int pipe_to_sub[2], pipe_from_sub[2];
235           
236           if (pipe (pipe_to_sub) == -1 ||
237               pipe (pipe_from_sub) == -1)
238             perror ("pipe"), exit (1);
239           
240           
241           seqtab[i].fd = pipe_from_sub[0];
242           seqtab[i].seq = 0;
243
244           my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
245           
246           id = g_new (guint, 1);
247           *id =
248             g_io_add_watch (my_read_channel,
249                             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
250                             recv_message,
251                             id);
252           
253           cmdline = g_strdup_printf ("%s %d %d &", argv[0],
254                                      pipe_to_sub[0], pipe_from_sub[1]);
255           
256           nrunning++;
257           
258 #ifdef G_OS_WIN32
259           {
260             gchar *readfd = g_strdup_printf ("%d", pipe_to_sub[0]);
261             gchar *writefd = g_strdup_printf ("%d", pipe_from_sub[1]);
262             _spawnl (_P_NOWAIT, argv[0], argv[0], readfd, writefd, NULL);
263           }
264 #else
265           system (cmdline);
266 #endif
267           close (pipe_to_sub[0]);
268           close (pipe_from_sub [1]);
269
270 #ifdef G_OS_WIN32
271           g_get_current_time (&start);
272           g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
273           pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
274           g_get_current_time (&end);
275           if (end.tv_usec < start.tv_usec)
276             end.tv_sec--, end.tv_usec += 1000000;
277           g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
278                    end.tv_sec - start.tv_sec,
279                    (end.tv_usec - start.tv_usec) / 1000,
280                    pollresult);
281 #endif
282         }
283       
284       main_loop = g_main_new (FALSE);
285       
286       g_main_run (main_loop);
287     }
288   else if (argc == 3)
289     {
290       /* Child */
291       
292       int readfd, writefd;
293       int i, j;
294       char buf[BUFSIZE];
295       int buflen;
296       GTimeVal tv;
297   
298       g_get_current_time (&tv);
299       
300       readfd = atoi (argv[1]);
301       writefd = atoi (argv[2]);
302       
303       srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
304   
305       for (i = 0; i < 20 + rand() % 20; i++)
306         {
307           g_usleep (100 + (rand() % 10) * 5000);
308           buflen = rand() % BUFSIZE;
309           for (j = 0; j < buflen; j++)
310             buf[j] = ' ' + ((buflen + j) % 95);
311           g_print ("gio-test: child writing %d bytes to %d\n", buflen, writefd);
312           write (writefd, &i, sizeof (i));
313           write (writefd, &buflen, sizeof (buflen));
314           write (writefd, buf, buflen);
315         }
316       g_print ("gio-test: child exiting, closing %d\n", writefd);
317       close (writefd);
318     }
319   else
320     g_print ("Huh?\n");
321   
322   return 0;
323 }
324