os/ossrv/glib/tsrc/BC/tests/mapping-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 2005 Matthias Clasen
sl@0
     3
 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General Public
sl@0
    15
 * License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
#include <stdlib.h>
sl@0
    21
#include <string.h>
sl@0
    22
#include <unistd.h>
sl@0
    23
#include <sys/types.h>
sl@0
    24
#include <pthread.h>
sl@0
    25
sl@0
    26
#include "glib.h"
sl@0
    27
#include "gstdio.h"
sl@0
    28
sl@0
    29
#ifdef SYMBIAN
sl@0
    30
#include "mrt2_glib2_test.h"
sl@0
    31
#endif /*SYMBIAN*/
sl@0
    32
sl@0
    33
sl@0
    34
static gchar *dir, *filename, *displayname, *childname;
sl@0
    35
sl@0
    36
static gboolean stop = FALSE;
sl@0
    37
sl@0
    38
#ifndef G_OS_WIN32
sl@0
    39
sl@0
    40
static void
sl@0
    41
handle_usr1 (int signum)
sl@0
    42
{
sl@0
    43
  stop = TRUE;
sl@0
    44
}
sl@0
    45
sl@0
    46
#endif
sl@0
    47
sl@0
    48
static gboolean
sl@0
    49
check_stop (gpointer data)
sl@0
    50
{
sl@0
    51
  GMainLoop *loop = data;
sl@0
    52
sl@0
    53
#if defined(G_OS_WIN32) || defined(SYMBIAN)
sl@0
    54
  stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
sl@0
    55
#endif
sl@0
    56
sl@0
    57
  if (stop)
sl@0
    58
    g_main_loop_quit (loop);
sl@0
    59
sl@0
    60
  return TRUE;
sl@0
    61
}
sl@0
    62
sl@0
    63
static void
sl@0
    64
write_or_die (const gchar *filename,
sl@0
    65
	      const gchar *contents,
sl@0
    66
	      gssize       length)
sl@0
    67
{
sl@0
    68
  GError *error = NULL;
sl@0
    69
  gchar *displayname;    
sl@0
    70
sl@0
    71
  if (!g_file_set_contents (filename, contents, length, &error)) 
sl@0
    72
    {
sl@0
    73
      displayname = g_filename_display_name (childname);
sl@0
    74
      g_print ("failed to write '%s': %s\n", 
sl@0
    75
	       displayname, error->message);
sl@0
    76
	    
sl@0
    77
	  g_assert(FALSE && "mapping-test failed");
sl@0
    78
	  
sl@0
    79
	  #ifdef SYMBIAN
sl@0
    80
  	  testResultXml("mapping-test");
sl@0
    81
  	  #endif /* EMULATOR */
sl@0
    82
      
sl@0
    83
      exit (1);
sl@0
    84
    }
sl@0
    85
}
sl@0
    86
sl@0
    87
static GMappedFile *
sl@0
    88
map_or_die (const gchar *filename,
sl@0
    89
	    gboolean     writable)
sl@0
    90
{
sl@0
    91
  GError *error = NULL;
sl@0
    92
  GMappedFile *map;
sl@0
    93
  gchar *displayname;
sl@0
    94
sl@0
    95
  map = g_mapped_file_new (filename, writable, &error);
sl@0
    96
  if (!map)
sl@0
    97
    {
sl@0
    98
      displayname = g_filename_display_name (childname);
sl@0
    99
      g_print ("failed to map '%s' non-writable, shared: %s\n", 
sl@0
   100
	       displayname, error->message);
sl@0
   101
	       
sl@0
   102
	  g_assert(FALSE && "mapping-test failed");
sl@0
   103
	  
sl@0
   104
	  #ifdef SYMBIAN
sl@0
   105
  	  testResultXml("mapping-test");
sl@0
   106
  	  #endif /* EMULATOR */
sl@0
   107
  	  
sl@0
   108
      exit (1);
sl@0
   109
    }
sl@0
   110
sl@0
   111
  return map;
sl@0
   112
}
sl@0
   113
	    
sl@0
   114
void *child_main (void *dummy)
sl@0
   115
{
sl@0
   116
  GMappedFile *map;
sl@0
   117
  GMainLoop *loop; 
sl@0
   118
sl@0
   119
  map = map_or_die (filename, FALSE);
sl@0
   120
  
sl@0
   121
  loop = g_main_loop_new (NULL, FALSE);
sl@0
   122
sl@0
   123
#if !defined(G_OS_WIN32) && !defined(SYMBIAN)
sl@0
   124
  signal (SIGUSR1, handle_usr1);
sl@0
   125
#endif
sl@0
   126
  g_idle_add (check_stop, loop);
sl@0
   127
  g_main_loop_run (loop);
sl@0
   128
sl@0
   129
  write_or_die (childname, 
sl@0
   130
		g_mapped_file_get_contents (map),
sl@0
   131
		g_mapped_file_get_length (map));
sl@0
   132
sl@0
   133
  return NULL;
sl@0
   134
}
sl@0
   135
sl@0
   136
static void
sl@0
   137
test_mapping (void)
sl@0
   138
{
sl@0
   139
  GMappedFile *map;
sl@0
   140
sl@0
   141
  write_or_die (filename, "ABC", -1);
sl@0
   142
sl@0
   143
  map = map_or_die (filename, FALSE);
sl@0
   144
  g_assert (g_mapped_file_get_length (map) == 3);
sl@0
   145
  g_mapped_file_free (map);
sl@0
   146
sl@0
   147
  map = map_or_die (filename, TRUE);
sl@0
   148
  g_assert (g_mapped_file_get_length (map) == 3);
sl@0
   149
  g_mapped_file_free (map);
sl@0
   150
}
sl@0
   151
sl@0
   152
static void 
sl@0
   153
test_private (void)
sl@0
   154
{
sl@0
   155
  GError *error = NULL;
sl@0
   156
  GMappedFile *map;
sl@0
   157
  gchar *buffer;
sl@0
   158
  gsize len;
sl@0
   159
sl@0
   160
  write_or_die (filename, "ABC", -1);
sl@0
   161
  map = map_or_die (filename, TRUE);
sl@0
   162
sl@0
   163
  buffer = (gchar *)g_mapped_file_get_contents (map);
sl@0
   164
  buffer[0] = '1';
sl@0
   165
  buffer[1] = '2';
sl@0
   166
  buffer[2] = '3';
sl@0
   167
  g_mapped_file_free (map);
sl@0
   168
sl@0
   169
  if (!g_file_get_contents (filename, &buffer, &len, &error))
sl@0
   170
    {
sl@0
   171
      g_print ("failed to read '%s': %s\n", 
sl@0
   172
	       displayname, error->message);
sl@0
   173
	       
sl@0
   174
	  g_assert(FALSE && "mapping-test failed");
sl@0
   175
	  
sl@0
   176
	  #ifdef SYMBIAN
sl@0
   177
  	  testResultXml("mapping-test");
sl@0
   178
  	  #endif /* EMULATOR */
sl@0
   179
  	  
sl@0
   180
      exit (1);
sl@0
   181
      
sl@0
   182
    }
sl@0
   183
  g_assert (len == 3);
sl@0
   184
  g_assert (strcmp (buffer, "ABC") == 0);
sl@0
   185
  g_free (buffer);
sl@0
   186
sl@0
   187
}
sl@0
   188
sl@0
   189
static void
sl@0
   190
test_child_private (gchar *argv0)
sl@0
   191
{
sl@0
   192
  GError *error = NULL;
sl@0
   193
  GMappedFile *map;
sl@0
   194
  gchar *buffer;
sl@0
   195
  gsize len;
sl@0
   196
  gchar *child_argv[3];
sl@0
   197
  GPid  child_pid;
sl@0
   198
  pthread_t child_thread;
sl@0
   199
  
sl@0
   200
  
sl@0
   201
#if defined(G_OS_WIN32) && defined(SYMBIAN)
sl@0
   202
  g_remove ("STOP");
sl@0
   203
  g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
sl@0
   204
#endif
sl@0
   205
sl@0
   206
  write_or_die (filename, "ABC", -1);
sl@0
   207
  map = map_or_die (filename, TRUE);
sl@0
   208
sl@0
   209
  child_argv[0] = argv0;
sl@0
   210
  child_argv[1] = "mapchild";
sl@0
   211
  child_argv[2] = NULL;
sl@0
   212
  pthread_create(&child_thread, NULL, child_main, NULL);
sl@0
   213
  //pthread_join(thread1, NULL);
sl@0
   214
  
sl@0
   215
  /*if (!g_spawn_async (dir, child_argv, NULL,
sl@0
   216
		      0, NULL, NULL, &child_pid, &error))
sl@0
   217
    {
sl@0
   218
      g_print ("failed to spawn child: %s\n", 
sl@0
   219
	       error->message);
sl@0
   220
      exit (1);            
sl@0
   221
    }*/
sl@0
   222
sl@0
   223
  /* give the child some time to set up its mapping */
sl@0
   224
  g_usleep (2000000);
sl@0
   225
sl@0
   226
  buffer = (gchar *)g_mapped_file_get_contents (map);
sl@0
   227
  buffer[0] = '1';
sl@0
   228
  buffer[1] = '2';
sl@0
   229
  buffer[2] = '3';
sl@0
   230
  g_mapped_file_free (map);
sl@0
   231
sl@0
   232
#if !defined(G_OS_WIN32) && !defined(SYMBIAN)
sl@0
   233
  kill (child_pid, SIGUSR1);
sl@0
   234
#else
sl@0
   235
  g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
sl@0
   236
#endif
sl@0
   237
sl@0
   238
  /* give the child some time to write the file */
sl@0
   239
  g_usleep (2000000);
sl@0
   240
  pthread_join(child_thread, NULL);
sl@0
   241
sl@0
   242
  if (!g_file_get_contents (childname, &buffer, &len, &error))
sl@0
   243
    {
sl@0
   244
      gchar *name;
sl@0
   245
sl@0
   246
      name = g_filename_display_name (childname);
sl@0
   247
      g_print ("failed to read '%s': %s\n", name, error->message);
sl@0
   248
      
sl@0
   249
      g_assert(FALSE && "mapping-test failed");
sl@0
   250
	  
sl@0
   251
	  #ifdef SYMBIAN
sl@0
   252
  	  testResultXml("mapping-test");
sl@0
   253
  	  #endif /* EMULATOR */
sl@0
   254
  	  
sl@0
   255
      exit (1);      
sl@0
   256
    }
sl@0
   257
  g_assert (len == 3);
sl@0
   258
  g_assert (strcmp (buffer, "ABC") == 0);
sl@0
   259
  g_free (buffer);
sl@0
   260
}
sl@0
   261
sl@0
   262
static int 
sl@0
   263
parent_main (int   argc,
sl@0
   264
	     char *argv[])
sl@0
   265
{
sl@0
   266
  /* test mapping with various flag combinations */
sl@0
   267
  test_mapping ();
sl@0
   268
sl@0
   269
  /* test private modification */
sl@0
   270
  test_private ();
sl@0
   271
sl@0
   272
  /* test multiple clients, non-shared */
sl@0
   273
  test_child_private (argv[0]);
sl@0
   274
sl@0
   275
  return 0;
sl@0
   276
}
sl@0
   277
sl@0
   278
int
sl@0
   279
main (int argc, 
sl@0
   280
      char *argv[])
sl@0
   281
{
sl@0
   282
  int retval;
sl@0
   283
  
sl@0
   284
  #ifndef EMULATOR
sl@0
   285
  mkdir("C:\\Private\\e0000009", 0666);
sl@0
   286
  chdir("C:\\Private\\e0000009");
sl@0
   287
  #endif//EMULATOR
sl@0
   288
sl@0
   289
  dir = g_get_current_dir ();
sl@0
   290
  filename = g_build_filename (dir, "maptest", NULL);
sl@0
   291
  displayname = g_filename_display_name (filename);
sl@0
   292
  childname = g_build_filename (dir, "mapchild", NULL);
sl@0
   293
sl@0
   294
  #ifdef SYMBIAN
sl@0
   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);
sl@0
   296
  g_set_print_handler(mrtPrintHandler);
sl@0
   297
  #endif
sl@0
   298
  /*if (argc > 1)
sl@0
   299
    return child_main (argc, argv);
sl@0
   300
  else */
sl@0
   301
  
sl@0
   302
  retval = parent_main (argc, argv);
sl@0
   303
  
sl@0
   304
  #ifdef SYMBIAN
sl@0
   305
  testResultXml("mapping-test");
sl@0
   306
  #endif /* EMULATOR */
sl@0
   307
  
sl@0
   308
  return retval;
sl@0
   309
}