Blame tests/file-test.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#ifdef GLIB_COMPILATION
Packit ae235b
#undef GLIB_COMPILATION
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
#include <gstdio.h>
Packit ae235b
Packit ae235b
#include <fcntl.h>		/* For open() */
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>			/* For read(), write() etc */
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_mkstemp (void)
Packit ae235b
{
Packit ae235b
  char template[32];
Packit ae235b
  int fd;
Packit ae235b
  int i;
Packit ae235b
  const char hello[] = "Hello, World";
Packit ae235b
  const int hellolen = sizeof (hello) - 1;
Packit ae235b
  char chars[62];
Packit ae235b
Packit ae235b
  strcpy (template, "foobar");
Packit ae235b
  fd = g_mkstemp (template);
Packit ae235b
  if (fd != -1)
Packit ae235b
    {
Packit ae235b
      g_warning ("g_mkstemp works even if template doesn't contain XXXXXX");
Packit ae235b
      close (fd);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strcpy (template, "foobarXXX");
Packit ae235b
  fd = g_mkstemp (template);
Packit ae235b
  if (fd != -1)
Packit ae235b
    {
Packit ae235b
      g_warning ("g_mkstemp works even if template contains less than six X");
Packit ae235b
      close (fd);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strcpy (template, "fooXXXXXX");
Packit ae235b
  fd = g_mkstemp (template);
Packit ae235b
  g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX");
Packit ae235b
  i = write (fd, hello, hellolen);
Packit ae235b
  g_assert (i != -1 && "write() failed");
Packit ae235b
  g_assert (i == hellolen && "write() has written too few bytes");
Packit ae235b
Packit ae235b
  lseek (fd, 0, 0);
Packit ae235b
  i = read (fd, chars, sizeof (chars));
Packit ae235b
  g_assert (i != -1 && "read() failed: %s");
Packit ae235b
  g_assert (i == hellolen && "read() has got wrong number of bytes");
Packit ae235b
Packit ae235b
  chars[i] = 0;
Packit ae235b
  g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back");
Packit ae235b
Packit ae235b
  close (fd);
Packit ae235b
  remove (template);
Packit ae235b
Packit ae235b
  strcpy (template, "fooXXXXXX.pdf");
Packit ae235b
  fd = g_mkstemp (template);
Packit ae235b
  g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX.pdf");
Packit ae235b
Packit ae235b
  close (fd);
Packit ae235b
  remove (template);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_mkdtemp (void)
Packit ae235b
{
Packit ae235b
  char template[32], *retval;
Packit ae235b
  int fd;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  strcpy (template, "foodir");
Packit ae235b
  retval = g_mkdtemp (template);
Packit ae235b
  if (retval != NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("g_mkdtemp works even if template doesn't contain XXXXXX");
Packit ae235b
      g_rmdir (retval);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strcpy (template, "foodir");
Packit ae235b
  retval = g_mkdtemp (template);
Packit ae235b
  if (retval != NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("g_mkdtemp works even if template contains less than six X");
Packit ae235b
      g_rmdir (retval);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strcpy (template, "fooXXXXXX");
Packit ae235b
  retval = g_mkdtemp (template);
Packit ae235b
  g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX");
Packit ae235b
  g_assert (retval == template && "g_mkdtemp allocated the resulting string?");
Packit ae235b
  g_assert (!g_file_test (template, G_FILE_TEST_IS_REGULAR));
Packit ae235b
  g_assert (g_file_test (template, G_FILE_TEST_IS_DIR));
Packit ae235b
Packit ae235b
  strcat (template, "/abc");
Packit ae235b
  fd = g_open (template, O_WRONLY | O_CREAT, 0600);
Packit ae235b
  g_assert (fd != -1 && "couldn't open file in temporary directory");
Packit ae235b
  close (fd);
Packit ae235b
  g_assert (g_file_test (template, G_FILE_TEST_IS_REGULAR));
Packit ae235b
  i = g_unlink (template);
Packit ae235b
  g_assert (i != -1 && "couldn't unlink file in temporary directory");
Packit ae235b
Packit ae235b
  template[9] = '\0';
Packit ae235b
  i = g_rmdir (template);
Packit ae235b
  g_assert (i != -1 && "couldn't remove temporary directory");
Packit ae235b
Packit ae235b
  strcpy (template, "fooXXXXXX.dir");
Packit ae235b
  retval = g_mkdtemp (template);
Packit ae235b
  g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX.dir");
Packit ae235b
  g_assert (g_file_test (template, G_FILE_TEST_IS_DIR));
Packit ae235b
  g_rmdir (template);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_readlink (void)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_SYMLINK
Packit ae235b
  FILE *file;
Packit ae235b
  int result;
Packit ae235b
  char *filename = "file-test-data";
Packit ae235b
  char *link1 = "file-test-link1";
Packit ae235b
  char *link2 = "file-test-link2";
Packit ae235b
  char *link3 = "file-test-link3";
Packit ae235b
  char *data;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  file = fopen (filename, "w");
Packit ae235b
  g_assert (file != NULL && "fopen() failed");
Packit ae235b
  fclose (file);
Packit ae235b
Packit ae235b
  result = symlink (filename, link1);
Packit ae235b
  g_assert (result == 0 && "symlink() failed");
Packit ae235b
  result = symlink (link1, link2);
Packit ae235b
  g_assert (result == 0 && "symlink() failed");
Packit ae235b
  
Packit ae235b
  error = NULL;
Packit ae235b
  data = g_file_read_link (link1, &error);
Packit ae235b
  g_assert (data != NULL && "couldn't read link1");
Packit ae235b
  g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data");
Packit ae235b
  g_free (data);
Packit ae235b
  
Packit ae235b
  error = NULL;
Packit ae235b
  data = g_file_read_link (link2, &error);
Packit ae235b
  g_assert (data != NULL && "couldn't read link2");
Packit ae235b
  g_assert (strcmp (data, link1) == 0 && "link2 contains wrong data");
Packit ae235b
  g_free (data);
Packit ae235b
  
Packit ae235b
  error = NULL;
Packit ae235b
  data = g_file_read_link (link3, &error);
Packit ae235b
  g_assert (data == NULL && "could read link3");
Packit ae235b
  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  data = g_file_read_link (filename, &error);
Packit ae235b
  g_assert (data == NULL && "could read regular file as link");
Packit ae235b
  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  remove (filename);
Packit ae235b
  remove (link1);
Packit ae235b
  remove (link2);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_get_contents (void)
Packit ae235b
{
Packit ae235b
  const gchar *text = "abcdefghijklmnopqrstuvwxyz";
Packit ae235b
  const gchar *filename = "file-test-get-contents";
Packit ae235b
  gchar *contents;
Packit ae235b
  gsize len;
Packit ae235b
  FILE *f;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  f = g_fopen (filename, "w");
Packit ae235b
  fwrite (text, 1, strlen (text), f);
Packit ae235b
  fclose (f);
Packit ae235b
Packit ae235b
  g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
Packit ae235b
Packit ae235b
  if (! g_file_get_contents (filename, &contents, &len, &error))
Packit ae235b
    g_error ("g_file_get_contents() failed: %s", error->message);
Packit ae235b
Packit ae235b
  g_assert (strcmp (text, contents) == 0 && "content mismatch");
Packit ae235b
Packit ae235b
  g_free (contents);
Packit ae235b
}
Packit ae235b
Packit ae235b
int 
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  test_mkstemp ();
Packit ae235b
  test_mkdtemp ();
Packit ae235b
  test_readlink ();
Packit ae235b
  test_get_contents ();
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}