Update contrib.
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.
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.
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.
23 #include <sys/types.h>
30 #include "mrt2_glib2_test.h"
34 static gchar *dir, *filename, *displayname, *childname;
36 static gboolean stop = FALSE;
41 handle_usr1 (int signum)
49 check_stop (gpointer data)
51 GMainLoop *loop = data;
53 #if defined(G_OS_WIN32) || defined(SYMBIAN)
54 stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
58 g_main_loop_quit (loop);
64 write_or_die (const gchar *filename,
65 const gchar *contents,
71 if (!g_file_set_contents (filename, contents, length, &error))
73 displayname = g_filename_display_name (childname);
74 g_print ("failed to write '%s': %s\n",
75 displayname, error->message);
77 g_assert(FALSE && "mapping-test failed");
80 testResultXml("mapping-test");
88 map_or_die (const gchar *filename,
95 map = g_mapped_file_new (filename, writable, &error);
98 displayname = g_filename_display_name (childname);
99 g_print ("failed to map '%s' non-writable, shared: %s\n",
100 displayname, error->message);
102 g_assert(FALSE && "mapping-test failed");
105 testResultXml("mapping-test");
106 #endif /* EMULATOR */
114 void *child_main (void *dummy)
119 map = map_or_die (filename, FALSE);
121 loop = g_main_loop_new (NULL, FALSE);
123 #if !defined(G_OS_WIN32) && !defined(SYMBIAN)
124 signal (SIGUSR1, handle_usr1);
126 g_idle_add (check_stop, loop);
127 g_main_loop_run (loop);
129 write_or_die (childname,
130 g_mapped_file_get_contents (map),
131 g_mapped_file_get_length (map));
141 write_or_die (filename, "ABC", -1);
143 map = map_or_die (filename, FALSE);
144 g_assert (g_mapped_file_get_length (map) == 3);
145 g_mapped_file_free (map);
147 map = map_or_die (filename, TRUE);
148 g_assert (g_mapped_file_get_length (map) == 3);
149 g_mapped_file_free (map);
155 GError *error = NULL;
160 write_or_die (filename, "ABC", -1);
161 map = map_or_die (filename, TRUE);
163 buffer = (gchar *)g_mapped_file_get_contents (map);
167 g_mapped_file_free (map);
169 if (!g_file_get_contents (filename, &buffer, &len, &error))
171 g_print ("failed to read '%s': %s\n",
172 displayname, error->message);
174 g_assert(FALSE && "mapping-test failed");
177 testResultXml("mapping-test");
178 #endif /* EMULATOR */
184 g_assert (strcmp (buffer, "ABC") == 0);
190 test_child_private (gchar *argv0)
192 GError *error = NULL;
196 gchar *child_argv[3];
198 pthread_t child_thread;
201 #if defined(G_OS_WIN32) && defined(SYMBIAN)
203 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
206 write_or_die (filename, "ABC", -1);
207 map = map_or_die (filename, TRUE);
209 child_argv[0] = argv0;
210 child_argv[1] = "mapchild";
211 child_argv[2] = NULL;
212 pthread_create(&child_thread, NULL, child_main, NULL);
213 //pthread_join(thread1, NULL);
215 /*if (!g_spawn_async (dir, child_argv, NULL,
216 0, NULL, NULL, &child_pid, &error))
218 g_print ("failed to spawn child: %s\n",
223 /* give the child some time to set up its mapping */
226 buffer = (gchar *)g_mapped_file_get_contents (map);
230 g_mapped_file_free (map);
232 #if !defined(G_OS_WIN32) && !defined(SYMBIAN)
233 kill (child_pid, SIGUSR1);
235 g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
238 /* give the child some time to write the file */
240 pthread_join(child_thread, NULL);
242 if (!g_file_get_contents (childname, &buffer, &len, &error))
246 name = g_filename_display_name (childname);
247 g_print ("failed to read '%s': %s\n", name, error->message);
249 g_assert(FALSE && "mapping-test failed");
252 testResultXml("mapping-test");
253 #endif /* EMULATOR */
258 g_assert (strcmp (buffer, "ABC") == 0);
263 parent_main (int argc,
266 /* test mapping with various flag combinations */
269 /* test private modification */
272 /* test multiple clients, non-shared */
273 test_child_private (argv[0]);
285 mkdir("C:\\Private\\e0000009", 0666);
286 chdir("C:\\Private\\e0000009");
289 dir = g_get_current_dir ();
290 filename = g_build_filename (dir, "maptest", NULL);
291 displayname = g_filename_display_name (filename);
292 childname = g_build_filename (dir, "mapchild", NULL);
295 g_log_set_handler (NULL, (GLogLevelFlags)(G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG), &mrtLogHandler, NULL);
296 g_set_print_handler(mrtPrintHandler);
299 return child_main (argc, argv);
302 retval = parent_main (argc, argv);
305 testResultXml("mapping-test");
306 #endif /* EMULATOR */