afc520d21f3e6ca46a7a3f0a44e262bff1621fd1
[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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <signal.h>
25
26 #include "glib.h"
27 #include "gstdio.h"
28
29 static gchar *dir, *filename, *displayname, *childname;
30
31 static gboolean stop = FALSE;
32
33 #ifndef G_OS_WIN32
34
35 static void
36 handle_usr1 (int signum)
37 {
38   stop = TRUE;
39 }
40
41 #endif
42
43 static gboolean
44 check_stop (gpointer data)
45 {
46   GMainLoop *loop = data;
47
48 #ifdef G_OS_WIN32
49   stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
50 #endif
51
52   if (stop)
53     g_main_loop_quit (loop);
54
55   return TRUE;
56 }
57
58 static void
59 write_or_die (const gchar *filename,
60               const gchar *contents,
61               gssize       length)
62 {
63   GError *error = NULL;
64   gchar *displayname;    
65
66   if (!g_file_set_contents (filename, contents, length, &error)) 
67     {
68       displayname = g_filename_display_name (childname);
69       g_print ("failed to write '%s': %s\n", 
70                displayname, error->message);
71       exit (1);
72     }
73 }
74
75 static GMappedFile *
76 map_or_die (const gchar *filename,
77             gboolean     writable)
78 {
79   GError *error = NULL;
80   GMappedFile *map;
81   gchar *displayname;
82
83   map = g_mapped_file_new (filename, writable, &error);
84   if (!map)
85     {
86       displayname = g_filename_display_name (childname);
87       g_print ("failed to map '%s' non-writable, shared: %s\n", 
88                displayname, error->message);
89       exit (1);
90     }
91
92   return map;
93 }
94             
95 static int
96 child_main (int argc, char *argv[])
97 {
98   GMappedFile *map;
99   GMainLoop *loop;
100
101   map = map_or_die (filename, FALSE);
102   
103   loop = g_main_loop_new (NULL, FALSE);
104
105 #ifndef G_OS_WIN32
106   signal (SIGUSR1, handle_usr1);
107 #endif
108   g_idle_add (check_stop, loop);
109   g_main_loop_run (loop);
110
111   write_or_die (childname, 
112                 g_mapped_file_get_contents (map),
113                 g_mapped_file_get_length (map));
114
115   return 0;
116 }
117
118 static void
119 test_mapping (void)
120 {
121   GMappedFile *map;
122
123   write_or_die (filename, "ABC", -1);
124
125   map = map_or_die (filename, FALSE);
126   g_assert (g_mapped_file_get_length (map) == 3);
127   g_mapped_file_free (map);
128
129   map = map_or_die (filename, TRUE);
130   g_assert (g_mapped_file_get_length (map) == 3);
131   g_mapped_file_free (map);
132 }
133
134 static void 
135 test_private (void)
136 {
137   GError *error = NULL;
138   GMappedFile *map;
139   gchar *buffer;
140   gsize len;
141
142   write_or_die (filename, "ABC", -1);
143   map = map_or_die (filename, TRUE);
144
145   buffer = (gchar *)g_mapped_file_get_contents (map);
146   buffer[0] = '1';
147   buffer[1] = '2';
148   buffer[2] = '3';
149   g_mapped_file_free (map);
150
151   if (!g_file_get_contents (filename, &buffer, &len, &error))
152     {
153       g_print ("failed to read '%s': %s\n", 
154                displayname, error->message);
155       exit (1);
156       
157     }
158   g_assert (len == 3);
159   g_assert (strcmp (buffer, "ABC") == 0);
160   g_free (buffer);
161
162 }
163
164 static void
165 test_child_private (gchar *argv0)
166 {
167   GError *error = NULL;
168   GMappedFile *map;
169   gchar *buffer;
170   gsize len;
171   gchar *child_argv[3];
172   GPid  child_pid;
173   
174 #ifdef G_OS_WIN32
175   g_remove ("STOP");
176   g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
177 #endif
178
179   write_or_die (filename, "ABC", -1);
180   map = map_or_die (filename, TRUE);
181
182   child_argv[0] = argv0;
183   child_argv[1] = "mapchild";
184   child_argv[2] = NULL;
185   if (!g_spawn_async (dir, child_argv, NULL,
186                       0, NULL, NULL, &child_pid, &error))
187     {
188       g_print ("failed to spawn child: %s\n", 
189                error->message);
190       exit (1);            
191     }
192
193   /* give the child some time to set up its mapping */
194   g_usleep (2000000);
195
196   buffer = (gchar *)g_mapped_file_get_contents (map);
197   buffer[0] = '1';
198   buffer[1] = '2';
199   buffer[2] = '3';
200   g_mapped_file_free (map);
201
202 #ifndef G_OS_WIN32
203   kill (child_pid, SIGUSR1);
204 #else
205   g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
206 #endif
207
208   /* give the child some time to write the file */
209   g_usleep (2000000);
210
211   if (!g_file_get_contents (childname, &buffer, &len, &error))
212     {
213       gchar *name;
214
215       name = g_filename_display_name (childname);
216       g_print ("failed to read '%s': %s\n", name, error->message);
217       exit (1);      
218     }
219   g_assert (len == 3);
220   g_assert (strcmp (buffer, "ABC") == 0);
221   g_free (buffer);
222 }
223
224 static int 
225 parent_main (int   argc,
226              char *argv[])
227 {
228   /* test mapping with various flag combinations */
229   test_mapping ();
230
231   /* test private modification */
232   test_private ();
233
234   /* test multiple clients, non-shared */
235   test_child_private (argv[0]);
236
237   return 0;
238 }
239
240 int
241 main (int argc, 
242       char *argv[])
243 {
244   dir = g_get_current_dir ();
245   filename = g_build_filename (dir, "maptest", NULL);
246   displayname = g_filename_display_name (filename);
247   childname = g_build_filename (dir, "mapchild", NULL);
248
249   if (argc > 1)
250     return child_main (argc, argv);
251   else 
252     return parent_main (argc, argv);
253 }