os/ossrv/glib/tsrc/BC/tests/file-test.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
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
/*
sl@0
    21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
sl@0
    22
 * file for a list of people on the GLib Team.  See the ChangeLog
sl@0
    23
 * files for a list of changes.  These files are distributed with
sl@0
    24
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
sl@0
    25
 */
sl@0
    26
sl@0
    27
#include "config.h"
sl@0
    28
sl@0
    29
#undef G_DISABLE_ASSERT
sl@0
    30
#undef G_LOG_DOMAIN
sl@0
    31
sl@0
    32
#ifdef GLIB_COMPILATION
sl@0
    33
#undef GLIB_COMPILATION
sl@0
    34
#endif
sl@0
    35
sl@0
    36
#include <string.h>
sl@0
    37
sl@0
    38
#include <glib.h>
sl@0
    39
sl@0
    40
#include <gstdio.h>
sl@0
    41
sl@0
    42
#ifdef HAVE_UNISTD_H
sl@0
    43
#include <unistd.h>
sl@0
    44
#endif
sl@0
    45
sl@0
    46
#ifdef G_OS_WIN32
sl@0
    47
#include <io.h>			/* For read(), write() etc */
sl@0
    48
#endif
sl@0
    49
sl@0
    50
#ifdef SYMBIAN
sl@0
    51
#include "mrt2_glib2_test.h"
sl@0
    52
#endif /*SYMBIAN*/
sl@0
    53
sl@0
    54
sl@0
    55
static void
sl@0
    56
test_mkstemp (void)
sl@0
    57
{
sl@0
    58
  char template[32];
sl@0
    59
  int fd;
sl@0
    60
  int i;
sl@0
    61
  const char hello[] = "Hello, World";
sl@0
    62
  const int hellolen = sizeof (hello) - 1;
sl@0
    63
  char chars[62];
sl@0
    64
  
sl@0
    65
  #ifndef EMULATOR
sl@0
    66
  mkdir("C:\\Private\\e0000009", 0666);
sl@0
    67
  chdir("C:\\Private\\e0000009");
sl@0
    68
  #endif//EMULATOR
sl@0
    69
  
sl@0
    70
  strcpy (template, "foobar");
sl@0
    71
  fd = g_mkstemp (template);
sl@0
    72
  if (fd != -1)
sl@0
    73
    g_warning ("g_mkstemp works even if template doesn't end in XXXXXX");
sl@0
    74
  close (fd);
sl@0
    75
sl@0
    76
  strcpy (template, "fooXXXXXX");
sl@0
    77
  fd = g_mkstemp (template);
sl@0
    78
  g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX");
sl@0
    79
  i = write (fd, hello, hellolen);
sl@0
    80
  g_assert (i != -1 && "write() failed");
sl@0
    81
  g_assert (i == hellolen && "write() has written too few bytes");
sl@0
    82
sl@0
    83
  lseek (fd, 0, 0);
sl@0
    84
  i = read (fd, chars, sizeof (chars));
sl@0
    85
  g_assert (i != -1 && "read() failed: %s");
sl@0
    86
  g_assert (i == hellolen && "read() has got wrong number of bytes");
sl@0
    87
sl@0
    88
  chars[i] = 0;
sl@0
    89
  g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back");
sl@0
    90
sl@0
    91
  close (fd);
sl@0
    92
  remove (template);
sl@0
    93
}
sl@0
    94
sl@0
    95
static void
sl@0
    96
test_readlink (void)
sl@0
    97
{
sl@0
    98
#ifdef HAVE_SYMLINK
sl@0
    99
  FILE *file;
sl@0
   100
  int result;
sl@0
   101
  char *filename = "file-test-data";
sl@0
   102
  char *link1 = "file-test-link1";
sl@0
   103
  char *link2 = "file-test-link2";
sl@0
   104
  char *link3 = "file-test-link3";
sl@0
   105
  char *data;
sl@0
   106
  GError *error;
sl@0
   107
sl@0
   108
  file = fopen (filename, "w");
sl@0
   109
  g_assert (file != NULL && "fopen() failed");
sl@0
   110
  fclose (file);
sl@0
   111
sl@0
   112
  result = symlink (filename, link1);
sl@0
   113
  g_assert (result == 0 && "symlink() failed");
sl@0
   114
  result = symlink (filename, link2);
sl@0
   115
  g_assert (result == 0 && "symlink() failed");
sl@0
   116
  
sl@0
   117
  error = NULL;
sl@0
   118
  data = g_file_read_link (link1, &error);
sl@0
   119
  g_assert (data != NULL && "couldn't read link1");
sl@0
   120
  g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data");
sl@0
   121
  g_free (data);
sl@0
   122
  error = NULL;
sl@0
   123
  data = g_file_read_link (link2, &error);
sl@0
   124
  g_assert (data != NULL && "couldn't read link2");
sl@0
   125
  g_assert (strcmp (data, filename) == 0 && "link2 contains wrong data");
sl@0
   126
  g_free (data);
sl@0
   127
  
sl@0
   128
  error = NULL;
sl@0
   129
  data = g_file_read_link (link3, &error);
sl@0
   130
  g_assert (data == NULL && "could read link3");
sl@0
   131
  g_assert (error != NULL && "error not set");
sl@0
   132
sl@0
   133
  error = NULL;
sl@0
   134
  data = g_file_read_link (filename, &error);
sl@0
   135
  g_assert (data == NULL && "could read regular file as link");
sl@0
   136
  g_assert (error != NULL && "error not set");
sl@0
   137
  
sl@0
   138
  remove (filename);
sl@0
   139
  remove (link1);
sl@0
   140
  remove (link2);
sl@0
   141
#endif
sl@0
   142
}
sl@0
   143
sl@0
   144
static void
sl@0
   145
test_get_contents (void)
sl@0
   146
{
sl@0
   147
  const gchar *text = "abcdefghijklmnopqrstuvwxyz";
sl@0
   148
  const gchar *filename = "file-test-get-contents";
sl@0
   149
  gchar *contents;
sl@0
   150
  gsize len;
sl@0
   151
  FILE *f;
sl@0
   152
  GError *error = NULL;
sl@0
   153
sl@0
   154
  f = g_fopen (filename, "w");
sl@0
   155
  if( f == NULL) 
sl@0
   156
  {
sl@0
   157
  	g_print("file-test.c : g_fopen Failed !!");
sl@0
   158
  	return;
sl@0
   159
  }
sl@0
   160
  fwrite (text, 1, strlen (text), f);
sl@0
   161
  fclose (f);
sl@0
   162
sl@0
   163
  g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
sl@0
   164
sl@0
   165
  if (! g_file_get_contents (filename, &contents, &len, &error))
sl@0
   166
    g_error ("g_file_get_contents() failed: %s", error->message);
sl@0
   167
sl@0
   168
  g_assert (strcmp (text, contents) == 0 && "content mismatch");
sl@0
   169
sl@0
   170
  g_free (contents);
sl@0
   171
}
sl@0
   172
sl@0
   173
int 
sl@0
   174
main (int argc, char *argv[])
sl@0
   175
{
sl@0
   176
  #ifdef SYMBIAN
sl@0
   177
  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);
sl@0
   178
  g_set_print_handler(mrtPrintHandler);
sl@0
   179
  #endif /*SYMBIAN*/
sl@0
   180
	  
sl@0
   181
sl@0
   182
  test_mkstemp ();
sl@0
   183
  test_readlink ();
sl@0
   184
  test_get_contents ();
sl@0
   185
  
sl@0
   186
  #if SYMBIAN
sl@0
   187
  testResultXml("file-test");
sl@0
   188
  #endif /* EMULATOR */
sl@0
   189
sl@0
   190
  return 0;
sl@0
   191
}