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