cbdbe60ada7cfd229b54e1a78a012141749eab62
[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.
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 gboolean
58 recv_message (GIOChannel  *channel,
59               GIOCondition cond,
60               gpointer    data)
61 {
62   gint fd = g_io_channel_unix_get_fd (channel);
63
64   g_print ("testgio: ...from %d:%s%s%s%s\n", fd,
65            (cond & G_IO_ERR) ? " ERR" : "",
66            (cond & G_IO_HUP) ? " HUP" : "",
67            (cond & G_IO_IN)  ? " IN"  : "",
68            (cond & G_IO_PRI) ? " PRI" : "");
69
70   if (cond & (G_IO_ERR | G_IO_HUP))
71     {
72       g_source_remove (*(guint *) data);
73       nrunning--;
74       if (nrunning == 0)
75         g_main_quit (main_loop);
76     }
77
78   if (cond & G_IO_IN)
79     {
80       char buf[BUFSIZE];
81       guint nbytes;
82       guint nb;
83       int i, j, seq;
84       GIOError error;
85       
86       error = g_io_channel_read (channel, (gchar *) &seq, sizeof (seq), &nb);
87       if (error == G_IO_ERROR_NONE)
88         {
89           if (nb == 0)
90             {
91               g_print ("testgio: ...from %d: EOF\n", fd);
92               return FALSE;
93             }
94           
95           g_assert (nb == sizeof (nbytes));
96
97           for (i = 0; i < nkiddies; i++)
98             if (seqtab[i].fd == fd)
99               {
100                 if (seq != seqtab[i].seq)
101                   {
102                     g_print ("testgio: ...from &d: invalid sequence number %d, expected %d\n",
103                              seq, seqtab[i].seq);
104                     g_assert_not_reached ();
105                   }
106                 seqtab[i].seq++;
107                 break;
108               }
109
110           error = g_io_channel_read (channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
111         }
112
113       if (error != G_IO_ERROR_NONE)
114         {
115           g_print ("testgio: ...from %d: G_IO_ERROR_%s\n", fd,
116                    (error == G_IO_ERROR_AGAIN ? "AGAIN" :
117                     (error == G_IO_ERROR_INVAL ? "INVAL" :
118                      (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
119
120           return FALSE;
121         }
122       
123       if (nb == 0)
124         {
125           g_print ("testgio: ...from %d: EOF\n", fd);
126           return FALSE;
127         }
128       
129       g_assert (nb == sizeof (nbytes));
130
131       if (nbytes >= BUFSIZE)
132         {
133           g_print ("testgio: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
134           g_assert_not_reached ();
135         }
136       g_assert (nbytes >= 0 && nbytes < BUFSIZE);
137       
138       g_print ("testgio: ...from %d: %d bytes\n", fd, nbytes);
139       
140       if (nbytes > 0)
141         {
142           error = g_io_channel_read (channel, buf, nbytes, &nb);
143           
144           if (error != G_IO_ERROR_NONE)
145             {
146               g_print ("testgio: ...from %d: G_IO_ERROR_%s\n", fd,
147                        (error == G_IO_ERROR_AGAIN ? "AGAIN" :
148                         (error == G_IO_ERROR_INVAL ? "INVAL" :
149                          (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
150               
151               return FALSE;
152             }
153
154           if (nb != nbytes)
155             {
156               g_print ("testgio: ...from %d: nb=%d != nbytes=%d\n",
157                        fd, nb, nbytes);
158               g_assert_not_reached ();
159             }
160           
161           for (j = 0; j < nbytes; j++)
162             if (buf[j] != ' ' + ((nbytes + j) % 95))
163               {
164                 g_print ("testgio: ...from %d: buf[%d] == '%c', should be '%c'\n",
165                          fd, j, buf[j], 'a' + ((nbytes + j) % 32));
166                 g_assert_not_reached ();
167               }
168           g_print ("testgio: ...from %d: OK\n", fd);
169         }
170     }
171   return TRUE;
172 }
173
174 int
175 main (int    argc,
176       char **argv)
177 {
178   if (argc < 3)
179     {
180       /* Parent */
181       
182       GIOChannel *my_read_channel;
183       gchar *cmdline;
184       guint *id;
185       int i;
186 #ifdef G_OS_WIN32
187       GTimeVal start, end;
188       int pollresult;
189 #endif
190
191       nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
192       seqtab = g_malloc (nkiddies * 2 * sizeof (int));
193
194       for (i = 0; i < nkiddies; i++)
195         {
196           int pipe_to_sub[2], pipe_from_sub[2];
197           
198           if (pipe (pipe_to_sub) == -1 ||
199               pipe (pipe_from_sub) == -1)
200             perror ("pipe"), exit (1);
201           
202           
203           seqtab[i].fd = pipe_from_sub[0];
204           seqtab[i].seq = 0;
205
206           my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
207           
208           id = g_new (guint, 1);
209           *id =
210             g_io_add_watch (my_read_channel,
211                             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
212                             recv_message,
213                             id);
214           
215           cmdline = g_strdup_printf ("%s %d %d &", argv[0],
216                                      pipe_to_sub[0], pipe_from_sub[1]);
217           
218           nrunning++;
219           
220 #ifdef G_OS_WIN32
221           {
222             gchar *readfd = g_strdup_printf ("%d", pipe_to_sub[0]);
223             gchar *writefd = g_strdup_printf ("%d", pipe_from_sub[1]);
224             _spawnl (_P_NOWAIT, argv[0], argv[0], readfd, writefd, NULL);
225           }
226 #else
227           system (cmdline);
228 #endif
229           close (pipe_to_sub[0]);
230           close (pipe_from_sub [1]);
231
232 #ifdef G_OS_WIN32
233           g_get_current_time (&start);
234           pollresult = g_io_channel_win32_wait_for_condition (my_read_channel, G_IO_IN, 100);
235           g_get_current_time (&end);
236           if (end.tv_usec < start.tv_usec)
237             end.tv_sec--, end.tv_usec += 1000000;
238           g_print ("testgio: had to wait %ld.%03ld s, result:%d\n",
239                    end.tv_sec - start.tv_sec,
240                    (end.tv_usec - start.tv_usec) / 1000,
241                    pollresult);
242 #endif
243         }
244       
245       main_loop = g_main_new (FALSE);
246       
247       g_main_run (main_loop);
248     }
249   else if (argc == 3)
250     {
251       /* Child */
252       
253       int readfd, writefd;
254       int i, j;
255       char buf[BUFSIZE];
256       int buflen;
257       GTimeVal tv;
258   
259       g_get_current_time (&tv);
260       
261       readfd = atoi (argv[1]);
262       writefd = atoi (argv[2]);
263       
264       srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
265   
266       for (i = 0; i < 20 + rand() % 20; i++)
267         {
268           g_usleep (100 + (rand() % 10) * 5000);
269           buflen = rand() % BUFSIZE;
270           for (j = 0; j < buflen; j++)
271             buf[j] = ' ' + ((buflen + j) % 95);
272           g_print ("testgio: child writing %d bytes to %d\n", buflen, writefd);
273           write (writefd, &i, sizeof (i));
274           write (writefd, &buflen, sizeof (buflen));
275           write (writefd, buf, buflen);
276         }
277       g_print ("testgio: child exiting, closing %d\n", writefd);
278       close (writefd);
279     }
280   else
281     g_print ("Huh?\n");
282   
283   return 0;
284 }
285