Add GRegex for regular expression matching. (#50075)
[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 #undef G_DISABLE_ASSERT
25 #undef G_LOG_DOMAIN
26
27 #include "config.h"
28
29 #include <glib.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <time.h>
35
36 #ifdef G_OS_WIN32
37   #include <io.h>
38   #include <fcntl.h>
39   #include <process.h>
40   #define STRICT
41   #include <windows.h>
42 #else
43   #ifdef HAVE_UNISTD_H
44     #include <unistd.h>
45   #endif
46 #endif
47
48 static int nrunning;
49 static GMainLoop *main_loop;
50
51 #define BUFSIZE 5000            /* Larger than the circular buffer in
52                                  * giowin32.c on purpose.
53                                  */
54
55 static int nkiddies;
56
57 static struct {
58   int fd;
59   int seq;
60 } *seqtab;
61
62 static GIOError
63 read_all (int         fd,
64           GIOChannel *channel,
65           char       *buffer,
66           guint       nbytes,
67           guint      *bytes_read)
68 {
69   guint left = nbytes;
70   gsize nb;
71   GIOError error = G_IO_ERROR_NONE;
72   char *bufp = buffer;
73
74   /* g_io_channel_read() doesn't necessarily return all the
75    * data we want at once.
76    */
77   *bytes_read = 0;
78   while (left)
79     {
80       error = g_io_channel_read (channel, bufp, left, &nb);
81       
82       if (error != G_IO_ERROR_NONE)
83         {
84           g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
85                    (error == G_IO_ERROR_AGAIN ? "AGAIN" :
86                     (error == G_IO_ERROR_INVAL ? "INVAL" :
87                      (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
88           if (error == G_IO_ERROR_AGAIN)
89             continue;
90           break;
91         }
92       if (nb == 0)
93         return error;
94       left -= nb;
95       bufp += nb;
96       *bytes_read += nb;
97     }
98   return error;
99 }
100
101 static void
102 shutdown_source (gpointer data)
103 {
104   if (g_source_remove (*(guint *) data))
105     {
106       nrunning--;
107       if (nrunning == 0)
108         g_main_loop_quit (main_loop);
109     }
110 }
111
112 static gboolean
113 recv_message (GIOChannel  *channel,
114               GIOCondition cond,
115               gpointer    data)
116 {
117   gint fd = g_io_channel_unix_get_fd (channel);
118   gboolean retval = TRUE;
119
120 #ifdef VERBOSE
121   g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
122            (cond & G_IO_ERR) ? " ERR" : "",
123            (cond & G_IO_HUP) ? " HUP" : "",
124            (cond & G_IO_IN)  ? " IN"  : "",
125            (cond & G_IO_PRI) ? " PRI" : "");
126 #endif
127
128   if (cond & (G_IO_ERR | G_IO_HUP))
129     {
130       shutdown_source (data);
131       retval = FALSE;
132     }
133
134   if (cond & G_IO_IN)
135     {
136       char buf[BUFSIZE];
137       guint nbytes;
138       guint nb;
139       int i, j, seq;
140       GIOError error;
141       
142       error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
143       if (error == G_IO_ERROR_NONE)
144         {
145           if (nb == 0)
146             {
147 #ifdef VERBOSE
148               g_print ("gio-test: ...from %d: EOF\n", fd);
149 #endif
150               shutdown_source (data);
151               return FALSE;
152             }
153           
154           g_assert (nb == sizeof (nbytes));
155
156           for (i = 0; i < nkiddies; i++)
157             if (seqtab[i].fd == fd)
158               {
159                 if (seq != seqtab[i].seq)
160                   {
161                     g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
162                              fd, seq, seqtab[i].seq);
163                     g_assert_not_reached ();
164                   }
165                 seqtab[i].seq++;
166                 break;
167               }
168
169           error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
170         }
171
172       if (error != G_IO_ERROR_NONE)
173         return FALSE;
174       
175       if (nb == 0)
176         {
177 #ifdef VERBOSE
178           g_print ("gio-test: ...from %d: EOF\n", fd);
179 #endif
180           shutdown_source (data);
181           return FALSE;
182         }
183       
184       g_assert (nb == sizeof (nbytes));
185
186       if (nbytes >= BUFSIZE)
187         {
188           g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
189           g_assert_not_reached ();
190         }
191       g_assert (nbytes >= 0 && nbytes < BUFSIZE);
192 #ifdef VERBOSE      
193       g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
194 #endif      
195       if (nbytes > 0)
196         {
197           error = read_all (fd, channel, buf, nbytes, &nb);
198
199           if (error != G_IO_ERROR_NONE)
200             return FALSE;
201
202           if (nb == 0)
203             {
204 #ifdef VERBOSE
205               g_print ("gio-test: ...from %d: EOF\n", fd);
206 #endif
207               shutdown_source (data);
208               return FALSE;
209             }
210       
211           for (j = 0; j < nbytes; j++)
212             if (buf[j] != ' ' + ((nbytes + j) % 95))
213               {
214                 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
215                          fd, j, buf[j], 'a' + ((nbytes + j) % 32));
216                 g_assert_not_reached ();
217               }
218 #ifdef VERBOSE
219           g_print ("gio-test: ...from %d: OK\n", fd);
220 #endif
221         }
222     }
223   return retval;
224 }
225
226 #ifdef G_OS_WIN32
227
228 static gboolean
229 recv_windows_message (GIOChannel  *channel,
230                       GIOCondition cond,
231                       gpointer    data)
232 {
233   GIOError error;
234   MSG msg;
235   guint nb;
236   
237   while (1)
238     {
239       error = g_io_channel_read (channel, &msg, sizeof (MSG), &nb);
240       
241       if (error != G_IO_ERROR_NONE)
242         {
243           g_print ("gio-test: ...reading Windows message: G_IO_ERROR_%s\n",
244                    (error == G_IO_ERROR_AGAIN ? "AGAIN" :
245                     (error == G_IO_ERROR_INVAL ? "INVAL" :
246                      (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
247           if (error == G_IO_ERROR_AGAIN)
248             continue;
249         }
250       break;
251     }
252
253   g_print ("gio-test: ...Windows message for %#x: %d,%d,%d\n",
254            msg.hwnd, msg.message, msg.wParam, msg.lParam);
255
256   return TRUE;
257 }
258
259 LRESULT CALLBACK 
260 window_procedure (HWND hwnd,
261                   UINT message,
262                   WPARAM wparam,
263                   LPARAM lparam)
264 {
265   g_print ("gio-test: window_procedure for %#x: %d,%d,%d\n",
266            hwnd, message, wparam, lparam);
267   return DefWindowProc (hwnd, message, wparam, lparam);
268 }
269
270 #endif
271
272 int
273 main (int    argc,
274       char **argv)
275 {
276   if (argc < 3)
277     {
278       /* Parent */
279       
280       GIOChannel *my_read_channel;
281       gchar *cmdline;
282       guint *id;
283       int i;
284 #ifdef G_OS_WIN32
285       GTimeVal start, end;
286       GPollFD pollfd;
287       int pollresult;
288       ATOM klass;
289       static WNDCLASS wcl;
290       HWND hwnd;
291       GIOChannel *windows_messages_channel;
292 #endif
293
294       nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
295       seqtab = g_malloc (nkiddies * 2 * sizeof (int));
296
297 #ifdef G_OS_WIN32
298       wcl.style = 0;
299       wcl.lpfnWndProc = window_procedure;
300       wcl.cbClsExtra = 0;
301       wcl.cbWndExtra = 0;
302       wcl.hInstance = GetModuleHandle (NULL);
303       wcl.hIcon = NULL;
304       wcl.hCursor = NULL;
305       wcl.hbrBackground = NULL;
306       wcl.lpszMenuName = NULL;
307       wcl.lpszClassName = "gio-test";
308
309       klass = RegisterClass (&wcl);
310
311       if (!klass)
312         {
313           g_print ("gio-test: RegisterClass failed\n");
314           exit (1);
315         }
316
317       hwnd = CreateWindow (MAKEINTATOM(klass), "gio-test", 0, 0, 0, 10, 10,
318                            NULL, NULL, wcl.hInstance, NULL);
319       if (!hwnd)
320         {
321           g_print ("gio-test: CreateWindow failed\n");
322           exit (1);
323         }
324
325       windows_messages_channel = g_io_channel_win32_new_messages ((guint)hwnd);
326       g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
327 #endif
328
329       for (i = 0; i < nkiddies; i++)
330         {
331           int pipe_to_sub[2], pipe_from_sub[2];
332           
333           if (pipe (pipe_to_sub) == -1 ||
334               pipe (pipe_from_sub) == -1)
335             perror ("pipe"), exit (1);
336           
337           seqtab[i].fd = pipe_from_sub[0];
338           seqtab[i].seq = 0;
339
340           my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
341           
342           id = g_new (guint, 1);
343           *id =
344             g_io_add_watch (my_read_channel,
345                             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
346                             recv_message,
347                             id);
348           
349           nrunning++;
350           
351 #ifdef G_OS_WIN32
352           cmdline = g_strdup_printf ("%d:%d:%d",
353                                      pipe_to_sub[0],
354                                      pipe_from_sub[1],
355                                      hwnd);
356           _spawnl (_P_NOWAIT, argv[0], argv[0], "--child", cmdline, NULL);
357 #else
358           cmdline = g_strdup_printf ("%s --child %d:%d &", argv[0],
359                                      pipe_to_sub[0], pipe_from_sub[1]);
360           
361           system (cmdline);
362 #endif
363           close (pipe_to_sub[0]);
364           close (pipe_from_sub [1]);
365
366 #ifdef G_OS_WIN32
367           g_get_current_time (&start);
368           g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
369           pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
370           g_get_current_time (&end);
371           if (end.tv_usec < start.tv_usec)
372             end.tv_sec--, end.tv_usec += 1000000;
373           g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
374                    end.tv_sec - start.tv_sec,
375                    (end.tv_usec - start.tv_usec) / 1000,
376                    pollresult);
377 #endif
378         }
379       
380       main_loop = g_main_loop_new (NULL, FALSE);
381       
382       g_main_loop_run (main_loop);
383     }
384   else if (argc == 3)
385     {
386       /* Child */
387       
388       int readfd, writefd;
389 #ifdef G_OS_WIN32
390       HWND hwnd;
391 #endif
392       int i, j;
393       char buf[BUFSIZE];
394       int buflen;
395       GTimeVal tv;
396       int n;
397   
398       g_get_current_time (&tv);
399       
400       sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
401
402 #ifdef G_OS_WIN32
403       sscanf (argv[2] + n, ":%d", &hwnd);
404 #endif
405       
406       srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
407   
408       for (i = 0; i < 20 + rand() % 20; i++)
409         {
410           g_usleep (100 + (rand() % 10) * 5000);
411           buflen = rand() % BUFSIZE;
412           for (j = 0; j < buflen; j++)
413             buf[j] = ' ' + ((buflen + j) % 95);
414 #ifdef VERBOSE
415           g_print ("gio-test: child writing %d+%d bytes to %d\n",
416                    (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
417 #endif
418           write (writefd, &i, sizeof (i));
419           write (writefd, &buflen, sizeof (buflen));
420           write (writefd, buf, buflen);
421
422 #ifdef G_OS_WIN32
423           if (rand() % 100 < 5)
424             {
425               int msg = WM_USER + (rand() % 100);
426               WPARAM wparam = rand ();
427               LPARAM lparam = rand ();
428               g_print ("gio-test: child posting message %d,%d,%d to %#x\n",
429                        msg, wparam, lparam, hwnd);
430               PostMessage (hwnd, msg, wparam, lparam);
431             }
432 #endif
433         }
434 #ifdef VERBOSE
435       g_print ("gio-test: child exiting, closing %d\n", writefd);
436 #endif
437       close (writefd);
438     }
439   else
440     g_print ("Huh?\n");
441   
442   return 0;
443 }
444