os/ossrv/glib/tsrc/BC/tests/file-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/file-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,191 @@
     1.4 +/* GLIB - Library of useful routines for C programming
     1.5 + * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
     1.6 + * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.7 + * This library is free software; you can redistribute it and/or
     1.8 + * modify it under the terms of the GNU Lesser General Public
     1.9 + * License as published by the Free Software Foundation; either
    1.10 + * version 2 of the License, or (at your option) any later version.
    1.11 + *
    1.12 + * This library is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 + * Lesser General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU Lesser General Public
    1.18 + * License along with this library; if not, write to the
    1.19 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 + * Boston, MA 02111-1307, USA.
    1.21 + */
    1.22 +
    1.23 +/*
    1.24 + * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
    1.25 + * file for a list of people on the GLib Team.  See the ChangeLog
    1.26 + * files for a list of changes.  These files are distributed with
    1.27 + * GLib at ftp://ftp.gtk.org/pub/gtk/. 
    1.28 + */
    1.29 +
    1.30 +#include "config.h"
    1.31 +
    1.32 +#undef G_DISABLE_ASSERT
    1.33 +#undef G_LOG_DOMAIN
    1.34 +
    1.35 +#ifdef GLIB_COMPILATION
    1.36 +#undef GLIB_COMPILATION
    1.37 +#endif
    1.38 +
    1.39 +#include <string.h>
    1.40 +
    1.41 +#include <glib.h>
    1.42 +
    1.43 +#include <gstdio.h>
    1.44 +
    1.45 +#ifdef HAVE_UNISTD_H
    1.46 +#include <unistd.h>
    1.47 +#endif
    1.48 +
    1.49 +#ifdef G_OS_WIN32
    1.50 +#include <io.h>			/* For read(), write() etc */
    1.51 +#endif
    1.52 +
    1.53 +#ifdef SYMBIAN
    1.54 +#include "mrt2_glib2_test.h"
    1.55 +#endif /*SYMBIAN*/
    1.56 +
    1.57 +
    1.58 +static void
    1.59 +test_mkstemp (void)
    1.60 +{
    1.61 +  char template[32];
    1.62 +  int fd;
    1.63 +  int i;
    1.64 +  const char hello[] = "Hello, World";
    1.65 +  const int hellolen = sizeof (hello) - 1;
    1.66 +  char chars[62];
    1.67 +  
    1.68 +  #ifndef EMULATOR
    1.69 +  mkdir("C:\\Private\\e0000009", 0666);
    1.70 +  chdir("C:\\Private\\e0000009");
    1.71 +  #endif//EMULATOR
    1.72 +  
    1.73 +  strcpy (template, "foobar");
    1.74 +  fd = g_mkstemp (template);
    1.75 +  if (fd != -1)
    1.76 +    g_warning ("g_mkstemp works even if template doesn't end in XXXXXX");
    1.77 +  close (fd);
    1.78 +
    1.79 +  strcpy (template, "fooXXXXXX");
    1.80 +  fd = g_mkstemp (template);
    1.81 +  g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX");
    1.82 +  i = write (fd, hello, hellolen);
    1.83 +  g_assert (i != -1 && "write() failed");
    1.84 +  g_assert (i == hellolen && "write() has written too few bytes");
    1.85 +
    1.86 +  lseek (fd, 0, 0);
    1.87 +  i = read (fd, chars, sizeof (chars));
    1.88 +  g_assert (i != -1 && "read() failed: %s");
    1.89 +  g_assert (i == hellolen && "read() has got wrong number of bytes");
    1.90 +
    1.91 +  chars[i] = 0;
    1.92 +  g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back");
    1.93 +
    1.94 +  close (fd);
    1.95 +  remove (template);
    1.96 +}
    1.97 +
    1.98 +static void
    1.99 +test_readlink (void)
   1.100 +{
   1.101 +#ifdef HAVE_SYMLINK
   1.102 +  FILE *file;
   1.103 +  int result;
   1.104 +  char *filename = "file-test-data";
   1.105 +  char *link1 = "file-test-link1";
   1.106 +  char *link2 = "file-test-link2";
   1.107 +  char *link3 = "file-test-link3";
   1.108 +  char *data;
   1.109 +  GError *error;
   1.110 +
   1.111 +  file = fopen (filename, "w");
   1.112 +  g_assert (file != NULL && "fopen() failed");
   1.113 +  fclose (file);
   1.114 +
   1.115 +  result = symlink (filename, link1);
   1.116 +  g_assert (result == 0 && "symlink() failed");
   1.117 +  result = symlink (filename, link2);
   1.118 +  g_assert (result == 0 && "symlink() failed");
   1.119 +  
   1.120 +  error = NULL;
   1.121 +  data = g_file_read_link (link1, &error);
   1.122 +  g_assert (data != NULL && "couldn't read link1");
   1.123 +  g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data");
   1.124 +  g_free (data);
   1.125 +  error = NULL;
   1.126 +  data = g_file_read_link (link2, &error);
   1.127 +  g_assert (data != NULL && "couldn't read link2");
   1.128 +  g_assert (strcmp (data, filename) == 0 && "link2 contains wrong data");
   1.129 +  g_free (data);
   1.130 +  
   1.131 +  error = NULL;
   1.132 +  data = g_file_read_link (link3, &error);
   1.133 +  g_assert (data == NULL && "could read link3");
   1.134 +  g_assert (error != NULL && "error not set");
   1.135 +
   1.136 +  error = NULL;
   1.137 +  data = g_file_read_link (filename, &error);
   1.138 +  g_assert (data == NULL && "could read regular file as link");
   1.139 +  g_assert (error != NULL && "error not set");
   1.140 +  
   1.141 +  remove (filename);
   1.142 +  remove (link1);
   1.143 +  remove (link2);
   1.144 +#endif
   1.145 +}
   1.146 +
   1.147 +static void
   1.148 +test_get_contents (void)
   1.149 +{
   1.150 +  const gchar *text = "abcdefghijklmnopqrstuvwxyz";
   1.151 +  const gchar *filename = "file-test-get-contents";
   1.152 +  gchar *contents;
   1.153 +  gsize len;
   1.154 +  FILE *f;
   1.155 +  GError *error = NULL;
   1.156 +
   1.157 +  f = g_fopen (filename, "w");
   1.158 +  if( f == NULL) 
   1.159 +  {
   1.160 +  	g_print("file-test.c : g_fopen Failed !!");
   1.161 +  	return;
   1.162 +  }
   1.163 +  fwrite (text, 1, strlen (text), f);
   1.164 +  fclose (f);
   1.165 +
   1.166 +  g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
   1.167 +
   1.168 +  if (! g_file_get_contents (filename, &contents, &len, &error))
   1.169 +    g_error ("g_file_get_contents() failed: %s", error->message);
   1.170 +
   1.171 +  g_assert (strcmp (text, contents) == 0 && "content mismatch");
   1.172 +
   1.173 +  g_free (contents);
   1.174 +}
   1.175 +
   1.176 +int 
   1.177 +main (int argc, char *argv[])
   1.178 +{
   1.179 +  #ifdef SYMBIAN
   1.180 +  g_log_set_handler (NULL,  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);
   1.181 +  g_set_print_handler(mrtPrintHandler);
   1.182 +  #endif /*SYMBIAN*/
   1.183 +	  
   1.184 +
   1.185 +  test_mkstemp ();
   1.186 +  test_readlink ();
   1.187 +  test_get_contents ();
   1.188 +  
   1.189 +  #if SYMBIAN
   1.190 +  testResultXml("file-test");
   1.191 +  #endif /* EMULATOR */
   1.192 +
   1.193 +  return 0;
   1.194 +}