Files
debian
docs
gio
glib
gmodule
gobject
gthread
m4macros
po
tests
bookmarks
collate
gobject
markups
refcount
Makefile.am
array-test.c
asyncqueue-test.c
atomic-test.c
base64-test.c
bit-test.c
bookmarkfile-test.c
casefold.txt
casemap.txt
checksum-test.c
child-test.c
completion-test.c
convert-test.c
cxx-test.C
date-test.c
dirname-test.c
env-test.c
errorcheck-mutex-test.c
file-test.c
gen-casefold-txt.pl
gen-casemap-txt.pl
gio-ls.c
gio-test.c
hash-test.c
iochannel-test-infile
iochannel-test.c
keyfile-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
list-test.c
mainloop-test.c
makefile.msc.in
mapping-test.c
markup-collect.c
markup-escape-test.c
markup-test.c
memchunks.c
module-test.c
node-test.c
onceinit.c
option-test.c
patterntest.c
printf-test.c
qsort-test.c
queue-test.c
rand-test.c
regex-test.c
relation-test.c
run-bookmark-test.sh
run-collate-tests.sh
run-markup-tests.sh
scannerapi.c
sequence-test.c
shell-test.c
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
slist-test.c
spawn-test-win32-gui.c
spawn-test.c
strfunc-test.c
string-test.c
strtod-test.c
strtoll-test.c
testgdate.c
testgdateparser.c
testglib.c
testingbase64.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop-closure.c
timeloop.c
tree-test.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
uri-test.c
utf8-pointer.c
utf8-validate.c
utf8.txt
.gitignore
AUTHORS
COPYING
ChangeLog
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL
INSTALL.in
MAINTAINERS
Makefile.am
Makefile.decl
NEWS
NEWS.pre-1-3
README
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.in
gio-2.0-uninstalled.pc.in
gio-2.0.pc.in
gio-unix-2.0-uninstalled.pc.in
gio-unix-2.0.pc.in
glib-2.0-uninstalled.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
glibconfig.h.win32.in
gmodule-2.0-uninstalled.pc.in
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0-uninstalled.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0-uninstalled.pc.in
gobject-2.0.pc.in
gthread-2.0-uninstalled.pc.in
gthread-2.0.pc.in
makefile.msc
mkinstalldirs
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
glib/tests/file-test.c
Matthias Clasen 6cc8a27894 Add tests.
2006-06-16  Matthias Clasen  <mclasen@redhat.com>

	* tests/file-test.c (test_mkstemp): Add tests.

	* glib/gfileutils.c (g_mkstemp): Allow the XXXXXX to occur
	inside the template, not just at the end.
2006-06-16 15:12:32 +00:00

181 lines
4.6 KiB
C

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