sl@0: /* GLIB - Library of useful routines for C programming sl@0: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald sl@0: * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: /* sl@0: * Modified by the GLib Team and others 1997-2000. See the AUTHORS sl@0: * file for a list of people on the GLib Team. See the ChangeLog sl@0: * files for a list of changes. These files are distributed with sl@0: * GLib at ftp://ftp.gtk.org/pub/gtk/. sl@0: */ sl@0: sl@0: #include "config.h" sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #ifdef GLIB_COMPILATION sl@0: #undef GLIB_COMPILATION sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #ifdef HAVE_UNISTD_H sl@0: #include sl@0: #endif sl@0: sl@0: #ifdef G_OS_WIN32 sl@0: #include /* For read(), write() etc */ sl@0: #endif sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: sl@0: static void sl@0: test_mkstemp (void) sl@0: { sl@0: char template[32]; sl@0: int fd; sl@0: int i; sl@0: const char hello[] = "Hello, World"; sl@0: const int hellolen = sizeof (hello) - 1; sl@0: char chars[62]; sl@0: sl@0: #ifndef EMULATOR sl@0: mkdir("C:\\Private\\e0000009", 0666); sl@0: chdir("C:\\Private\\e0000009"); sl@0: #endif//EMULATOR sl@0: sl@0: strcpy (template, "foobar"); sl@0: fd = g_mkstemp (template); sl@0: if (fd != -1) sl@0: g_warning ("g_mkstemp works even if template doesn't end in XXXXXX"); sl@0: close (fd); sl@0: sl@0: strcpy (template, "fooXXXXXX"); sl@0: fd = g_mkstemp (template); sl@0: g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX"); sl@0: i = write (fd, hello, hellolen); sl@0: g_assert (i != -1 && "write() failed"); sl@0: g_assert (i == hellolen && "write() has written too few bytes"); sl@0: sl@0: lseek (fd, 0, 0); sl@0: i = read (fd, chars, sizeof (chars)); sl@0: g_assert (i != -1 && "read() failed: %s"); sl@0: g_assert (i == hellolen && "read() has got wrong number of bytes"); sl@0: sl@0: chars[i] = 0; sl@0: g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back"); sl@0: sl@0: close (fd); sl@0: remove (template); sl@0: } sl@0: sl@0: static void sl@0: test_readlink (void) sl@0: { sl@0: #ifdef HAVE_SYMLINK sl@0: FILE *file; sl@0: int result; sl@0: char *filename = "file-test-data"; sl@0: char *link1 = "file-test-link1"; sl@0: char *link2 = "file-test-link2"; sl@0: char *link3 = "file-test-link3"; sl@0: char *data; sl@0: GError *error; sl@0: sl@0: file = fopen (filename, "w"); sl@0: g_assert (file != NULL && "fopen() failed"); sl@0: fclose (file); sl@0: sl@0: result = symlink (filename, link1); sl@0: g_assert (result == 0 && "symlink() failed"); sl@0: result = symlink (filename, link2); sl@0: g_assert (result == 0 && "symlink() failed"); sl@0: sl@0: error = NULL; sl@0: data = g_file_read_link (link1, &error); sl@0: g_assert (data != NULL && "couldn't read link1"); sl@0: g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data"); sl@0: g_free (data); sl@0: error = NULL; sl@0: data = g_file_read_link (link2, &error); sl@0: g_assert (data != NULL && "couldn't read link2"); sl@0: g_assert (strcmp (data, filename) == 0 && "link2 contains wrong data"); sl@0: g_free (data); sl@0: sl@0: error = NULL; sl@0: data = g_file_read_link (link3, &error); sl@0: g_assert (data == NULL && "could read link3"); sl@0: g_assert (error != NULL && "error not set"); sl@0: sl@0: error = NULL; sl@0: data = g_file_read_link (filename, &error); sl@0: g_assert (data == NULL && "could read regular file as link"); sl@0: g_assert (error != NULL && "error not set"); sl@0: sl@0: remove (filename); sl@0: remove (link1); sl@0: remove (link2); sl@0: #endif sl@0: } sl@0: sl@0: static void sl@0: test_get_contents (void) sl@0: { sl@0: const gchar *text = "abcdefghijklmnopqrstuvwxyz"; sl@0: const gchar *filename = "file-test-get-contents"; sl@0: gchar *contents; sl@0: gsize len; sl@0: FILE *f; sl@0: GError *error = NULL; sl@0: sl@0: f = g_fopen (filename, "w"); sl@0: if( f == NULL) sl@0: { sl@0: g_print("file-test.c : g_fopen Failed !!"); sl@0: return; sl@0: } sl@0: fwrite (text, 1, strlen (text), f); sl@0: fclose (f); sl@0: sl@0: g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); sl@0: sl@0: if (! g_file_get_contents (filename, &contents, &len, &error)) sl@0: g_error ("g_file_get_contents() failed: %s", error->message); sl@0: sl@0: g_assert (strcmp (text, contents) == 0 && "content mismatch"); sl@0: sl@0: g_free (contents); sl@0: } sl@0: sl@0: int sl@0: main (int argc, char *argv[]) sl@0: { sl@0: #ifdef SYMBIAN sl@0: 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: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: sl@0: test_mkstemp (); sl@0: test_readlink (); sl@0: test_get_contents (); sl@0: sl@0: #if SYMBIAN sl@0: testResultXml("file-test"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return 0; sl@0: }