diff options
author | Matthias Clasen <mclasen@redhat.com> | 2011-08-14 14:09:58 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2011-08-14 14:09:58 -0400 |
commit | b76bb6713ba12a88fbccdaaf063d916ecd3af0b2 (patch) | |
tree | e20c6789aae418c7c9eab2649aca74c70e493293 /tests | |
parent | 8377a886857396854069fb7a8309baeb77f144c2 (diff) |
Add g_mkdtemp in the spirit of g_mkstemp
At the same time, also add g_mkdtemp_full and g_dir_make_tmp
variants. The patch also unifies the unique-name-generating
code for all variants of mkstemp and mkdtemp and adds tests
for the new functions.
Based on patches by Paolo Bonzini,
http://bugzilla.gnome.org/show_bug.cgi?id=118563
Diffstat (limited to 'tests')
-rw-r--r-- | tests/file-test.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/file-test.c b/tests/file-test.c index adab1f20f..62f712c11 100644 --- a/tests/file-test.c +++ b/tests/file-test.c @@ -43,6 +43,8 @@ #include <unistd.h> #endif +#include <fcntl.h> /* For open() */ + #ifdef G_OS_WIN32 #include <io.h> /* For read(), write() etc */ #endif @@ -96,6 +98,55 @@ test_mkstemp (void) } static void +test_mkdtemp (void) +{ + char template[32], *retval; + int fd; + int i; + + strcpy (template, "foodir"); + retval = g_mkdtemp (template); + if (retval != NULL) + { + g_warning ("g_mkdtemp works even if template doesn't contain XXXXXX"); + g_rmdir (retval); + } + + strcpy (template, "foodir"); + retval = g_mkdtemp (template); + if (retval != NULL) + { + g_warning ("g_mkdtemp works even if template contains less than six X"); + g_rmdir (retval); + } + + strcpy (template, "fooXXXXXX"); + retval = g_mkdtemp (template); + g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX"); + g_assert (retval == template && "g_mkdtemp allocated the resulting string?"); + g_assert (!g_file_test (template, G_FILE_TEST_IS_REGULAR)); + g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); + + strcat (template, "/abc"); + fd = g_open (template, O_WRONLY | O_CREAT, 0600); + g_assert (fd != -1 && "couldn't open file in temporary directory"); + close (fd); + g_assert (g_file_test (template, G_FILE_TEST_IS_REGULAR)); + i = g_unlink (template); + g_assert (i != -1 && "couldn't unlink file in temporary directory"); + + template[9] = '\0'; + i = g_rmdir (template); + g_assert (i != -1 && "couldn't remove temporary directory"); + + strcpy (template, "fooXXXXXX.dir"); + retval = g_mkdtemp (template); + g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX.dir"); + g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); + g_rmdir (template); +} + +static void test_readlink (void) { #ifdef HAVE_SYMLINK @@ -173,6 +224,7 @@ int main (int argc, char *argv[]) { test_mkstemp (); + test_mkdtemp (); test_readlink (); test_get_contents (); |