mapping-test: Add debug spew
[platform/upstream/glib.git] / tests / mapping-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2005 Matthias Clasen
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 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <signal.h>
21
22 #include "glib.h"
23 #include "gstdio.h"
24
25 #ifdef G_OS_UNIX
26 #include <unistd.h>
27 #endif
28
29 static gchar *dir, *filename, *displayname, *childname;
30
31 static gboolean stop = FALSE;
32
33 static gint parent_pid;
34
35 #ifndef G_OS_WIN32
36
37 static void
38 handle_usr1 (int signum)
39 {
40   stop = TRUE;
41 }
42
43 #endif
44
45 static gboolean
46 check_stop (gpointer data)
47 {
48   GMainLoop *loop = data;
49
50 #ifdef G_OS_WIN32
51   stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
52 #endif
53
54   if (stop)
55     g_main_loop_quit (loop);
56
57   return TRUE;
58 }
59
60 static void
61 write_or_die (const gchar *filename,
62               const gchar *contents,
63               gssize       length)
64 {
65   GError *error = NULL;
66   gchar *displayname;    
67
68   if (!g_file_set_contents (filename, contents, length, &error)) 
69     {
70       displayname = g_filename_display_name (childname);
71       g_print ("failed to write '%s': %s\n", 
72                displayname, error->message);
73       exit (1);
74     }
75 }
76
77 static GMappedFile *
78 map_or_die (const gchar *filename,
79             gboolean     writable)
80 {
81   GError *error = NULL;
82   GMappedFile *map;
83   gchar *displayname;
84
85   map = g_mapped_file_new (filename, writable, &error);
86   if (!map)
87     {
88       displayname = g_filename_display_name (childname);
89       g_print ("failed to map '%s' non-writable, shared: %s\n", 
90                displayname, error->message);
91       exit (1);
92     }
93
94   return map;
95 }
96     
97 static gboolean
98 signal_parent (gpointer data)
99 {
100 #ifndef G_OS_WIN32
101   kill (parent_pid, SIGUSR1);
102 #endif
103   return G_SOURCE_REMOVE;
104 }
105
106 static int
107 child_main (int argc, char *argv[])
108 {
109   GMappedFile *map;
110   GMainLoop *loop;
111
112   parent_pid = atoi (argv[2]);
113   map = map_or_die (filename, FALSE);
114
115 #ifndef G_OS_WIN32
116   signal (SIGUSR1, handle_usr1);
117 #endif
118   loop = g_main_loop_new (NULL, FALSE);
119   g_idle_add (check_stop, loop);
120   g_idle_add (signal_parent, NULL);
121   g_main_loop_run (loop);
122
123   write_or_die (childname, 
124                 g_mapped_file_get_contents (map),
125                 g_mapped_file_get_length (map));
126
127   signal_parent (NULL);
128
129   return 0;
130 }
131
132 static void
133 test_mapping (void)
134 {
135   GMappedFile *map;
136
137   write_or_die (filename, "ABC", -1);
138
139   map = map_or_die (filename, FALSE);
140   g_assert (g_mapped_file_get_length (map) == 3);
141   g_mapped_file_free (map);
142
143   map = map_or_die (filename, TRUE);
144   g_assert (g_mapped_file_get_length (map) == 3);
145   g_mapped_file_free (map);
146   g_message ("test_mapping: ok");
147 }
148
149 static void 
150 test_private (void)
151 {
152   GError *error = NULL;
153   GMappedFile *map;
154   gchar *buffer;
155   gsize len;
156
157   write_or_die (filename, "ABC", -1);
158   map = map_or_die (filename, TRUE);
159
160   buffer = (gchar *)g_mapped_file_get_contents (map);
161   buffer[0] = '1';
162   buffer[1] = '2';
163   buffer[2] = '3';
164   g_mapped_file_free (map);
165
166   if (!g_file_get_contents (filename, &buffer, &len, &error))
167     {
168       g_print ("failed to read '%s': %s\n", 
169                displayname, error->message);
170       exit (1);
171       
172     }
173   g_assert (len == 3);
174   g_assert (strcmp (buffer, "ABC") == 0);
175   g_free (buffer);
176
177   g_message ("test_private: ok");
178 }
179
180 static void
181 test_child_private (gchar *argv0)
182 {
183   GError *error = NULL;
184   GMappedFile *map;
185   gchar *buffer;
186   gsize len;
187   gchar *child_argv[4];
188   GPid  child_pid;
189   GMainLoop *loop;
190   gchar pid[100];
191   
192 #ifdef G_OS_WIN32
193   g_remove ("STOP");
194   g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
195 #endif
196
197   write_or_die (filename, "ABC", -1);
198   map = map_or_die (filename, TRUE);
199
200 #ifndef G_OS_WIN32
201   signal (SIGUSR1, handle_usr1);
202 #endif
203
204   g_snprintf (pid, sizeof(pid), "%d", getpid ());
205   child_argv[0] = argv0;
206   child_argv[1] = "mapchild";
207   child_argv[2] = pid;
208   child_argv[3] = NULL;
209   if (!g_spawn_async (dir, child_argv, NULL,
210                       0, NULL, NULL, &child_pid, &error))
211     {
212       g_print ("failed to spawn child: %s\n", 
213                error->message);
214       exit (1);            
215     }
216
217 #ifndef G_OS_WIN32
218   loop = g_main_loop_new (NULL, FALSE);
219   g_idle_add (check_stop, loop);
220   g_main_loop_run (loop);
221   stop = FALSE;
222 #else
223   g_usleep (2000000);
224 #endif
225
226   buffer = (gchar *)g_mapped_file_get_contents (map);
227   buffer[0] = '1';
228   buffer[1] = '2';
229   buffer[2] = '3';
230   g_mapped_file_free (map);
231
232 #ifndef G_OS_WIN32
233   kill (child_pid, SIGUSR1);
234 #else
235   g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
236 #endif
237
238 #ifndef G_OS_WIN32
239   g_idle_add (check_stop, loop);
240   g_main_loop_run (loop);
241 #else
242   g_usleep (2000000);
243 #endif
244
245   if (!g_file_get_contents (childname, &buffer, &len, &error))
246     {
247       gchar *name;
248
249       name = g_filename_display_name (childname);
250       g_print ("failed to read '%s': %s\n", name, error->message);
251       exit (1);      
252     }
253   g_assert (len == 3);
254   g_assert (strcmp (buffer, "ABC") == 0);
255   g_free (buffer);
256
257   g_message ("test_child_private: ok");
258 }
259
260 static int 
261 parent_main (int   argc,
262              char *argv[])
263 {
264   /* test mapping with various flag combinations */
265   test_mapping ();
266
267   /* test private modification */
268   test_private ();
269
270   /* test multiple clients, non-shared */
271   test_child_private (argv[0]);
272
273   return 0;
274 }
275
276 int
277 main (int argc, 
278       char *argv[])
279 {
280   int ret;
281
282   dir = g_get_current_dir ();
283   filename = g_build_filename (dir, "maptest", NULL);
284   displayname = g_filename_display_name (filename);
285   childname = g_build_filename (dir, "mapchild", NULL);
286
287   if (argc > 1)
288     ret = child_main (argc, argv);
289   else 
290     ret = parent_main (argc, argv);
291
292   g_free (childname);
293   g_free (filename);
294   g_free (displayname);
295   g_free (dir);
296
297   return ret;
298 }