os/ossrv/glib/tsrc/BC/tests/mapping-test.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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.
     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 <pthread.h>
    25 
    26 #include "glib.h"
    27 #include "gstdio.h"
    28 
    29 #ifdef SYMBIAN
    30 #include "mrt2_glib2_test.h"
    31 #endif /*SYMBIAN*/
    32 
    33 
    34 static gchar *dir, *filename, *displayname, *childname;
    35 
    36 static gboolean stop = FALSE;
    37 
    38 #ifndef G_OS_WIN32
    39 
    40 static void
    41 handle_usr1 (int signum)
    42 {
    43   stop = TRUE;
    44 }
    45 
    46 #endif
    47 
    48 static gboolean
    49 check_stop (gpointer data)
    50 {
    51   GMainLoop *loop = data;
    52 
    53 #if defined(G_OS_WIN32) || defined(SYMBIAN)
    54   stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
    55 #endif
    56 
    57   if (stop)
    58     g_main_loop_quit (loop);
    59 
    60   return TRUE;
    61 }
    62 
    63 static void
    64 write_or_die (const gchar *filename,
    65 	      const gchar *contents,
    66 	      gssize       length)
    67 {
    68   GError *error = NULL;
    69   gchar *displayname;    
    70 
    71   if (!g_file_set_contents (filename, contents, length, &error)) 
    72     {
    73       displayname = g_filename_display_name (childname);
    74       g_print ("failed to write '%s': %s\n", 
    75 	       displayname, error->message);
    76 	    
    77 	  g_assert(FALSE && "mapping-test failed");
    78 	  
    79 	  #ifdef SYMBIAN
    80   	  testResultXml("mapping-test");
    81   	  #endif /* EMULATOR */
    82       
    83       exit (1);
    84     }
    85 }
    86 
    87 static GMappedFile *
    88 map_or_die (const gchar *filename,
    89 	    gboolean     writable)
    90 {
    91   GError *error = NULL;
    92   GMappedFile *map;
    93   gchar *displayname;
    94 
    95   map = g_mapped_file_new (filename, writable, &error);
    96   if (!map)
    97     {
    98       displayname = g_filename_display_name (childname);
    99       g_print ("failed to map '%s' non-writable, shared: %s\n", 
   100 	       displayname, error->message);
   101 	       
   102 	  g_assert(FALSE && "mapping-test failed");
   103 	  
   104 	  #ifdef SYMBIAN
   105   	  testResultXml("mapping-test");
   106   	  #endif /* EMULATOR */
   107   	  
   108       exit (1);
   109     }
   110 
   111   return map;
   112 }
   113 	    
   114 void *child_main (void *dummy)
   115 {
   116   GMappedFile *map;
   117   GMainLoop *loop; 
   118 
   119   map = map_or_die (filename, FALSE);
   120   
   121   loop = g_main_loop_new (NULL, FALSE);
   122 
   123 #if !defined(G_OS_WIN32) && !defined(SYMBIAN)
   124   signal (SIGUSR1, handle_usr1);
   125 #endif
   126   g_idle_add (check_stop, loop);
   127   g_main_loop_run (loop);
   128 
   129   write_or_die (childname, 
   130 		g_mapped_file_get_contents (map),
   131 		g_mapped_file_get_length (map));
   132 
   133   return NULL;
   134 }
   135 
   136 static void
   137 test_mapping (void)
   138 {
   139   GMappedFile *map;
   140 
   141   write_or_die (filename, "ABC", -1);
   142 
   143   map = map_or_die (filename, FALSE);
   144   g_assert (g_mapped_file_get_length (map) == 3);
   145   g_mapped_file_free (map);
   146 
   147   map = map_or_die (filename, TRUE);
   148   g_assert (g_mapped_file_get_length (map) == 3);
   149   g_mapped_file_free (map);
   150 }
   151 
   152 static void 
   153 test_private (void)
   154 {
   155   GError *error = NULL;
   156   GMappedFile *map;
   157   gchar *buffer;
   158   gsize len;
   159 
   160   write_or_die (filename, "ABC", -1);
   161   map = map_or_die (filename, TRUE);
   162 
   163   buffer = (gchar *)g_mapped_file_get_contents (map);
   164   buffer[0] = '1';
   165   buffer[1] = '2';
   166   buffer[2] = '3';
   167   g_mapped_file_free (map);
   168 
   169   if (!g_file_get_contents (filename, &buffer, &len, &error))
   170     {
   171       g_print ("failed to read '%s': %s\n", 
   172 	       displayname, error->message);
   173 	       
   174 	  g_assert(FALSE && "mapping-test failed");
   175 	  
   176 	  #ifdef SYMBIAN
   177   	  testResultXml("mapping-test");
   178   	  #endif /* EMULATOR */
   179   	  
   180       exit (1);
   181       
   182     }
   183   g_assert (len == 3);
   184   g_assert (strcmp (buffer, "ABC") == 0);
   185   g_free (buffer);
   186 
   187 }
   188 
   189 static void
   190 test_child_private (gchar *argv0)
   191 {
   192   GError *error = NULL;
   193   GMappedFile *map;
   194   gchar *buffer;
   195   gsize len;
   196   gchar *child_argv[3];
   197   GPid  child_pid;
   198   pthread_t child_thread;
   199   
   200   
   201 #if defined(G_OS_WIN32) && defined(SYMBIAN)
   202   g_remove ("STOP");
   203   g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
   204 #endif
   205 
   206   write_or_die (filename, "ABC", -1);
   207   map = map_or_die (filename, TRUE);
   208 
   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);
   214   
   215   /*if (!g_spawn_async (dir, child_argv, NULL,
   216 		      0, NULL, NULL, &child_pid, &error))
   217     {
   218       g_print ("failed to spawn child: %s\n", 
   219 	       error->message);
   220       exit (1);            
   221     }*/
   222 
   223   /* give the child some time to set up its mapping */
   224   g_usleep (2000000);
   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 #if !defined(G_OS_WIN32) && !defined(SYMBIAN)
   233   kill (child_pid, SIGUSR1);
   234 #else
   235   g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
   236 #endif
   237 
   238   /* give the child some time to write the file */
   239   g_usleep (2000000);
   240   pthread_join(child_thread, NULL);
   241 
   242   if (!g_file_get_contents (childname, &buffer, &len, &error))
   243     {
   244       gchar *name;
   245 
   246       name = g_filename_display_name (childname);
   247       g_print ("failed to read '%s': %s\n", name, error->message);
   248       
   249       g_assert(FALSE && "mapping-test failed");
   250 	  
   251 	  #ifdef SYMBIAN
   252   	  testResultXml("mapping-test");
   253   	  #endif /* EMULATOR */
   254   	  
   255       exit (1);      
   256     }
   257   g_assert (len == 3);
   258   g_assert (strcmp (buffer, "ABC") == 0);
   259   g_free (buffer);
   260 }
   261 
   262 static int 
   263 parent_main (int   argc,
   264 	     char *argv[])
   265 {
   266   /* test mapping with various flag combinations */
   267   test_mapping ();
   268 
   269   /* test private modification */
   270   test_private ();
   271 
   272   /* test multiple clients, non-shared */
   273   test_child_private (argv[0]);
   274 
   275   return 0;
   276 }
   277 
   278 int
   279 main (int argc, 
   280       char *argv[])
   281 {
   282   int retval;
   283   
   284   #ifndef EMULATOR
   285   mkdir("C:\\Private\\e0000009", 0666);
   286   chdir("C:\\Private\\e0000009");
   287   #endif//EMULATOR
   288 
   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);
   293 
   294   #ifdef SYMBIAN
   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);
   297   #endif
   298   /*if (argc > 1)
   299     return child_main (argc, argv);
   300   else */
   301   
   302   retval = parent_main (argc, argv);
   303   
   304   #ifdef SYMBIAN
   305   testResultXml("mapping-test");
   306   #endif /* EMULATOR */
   307   
   308   return retval;
   309 }