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