Improve fileutils test coverage

Add some tests for g_file_read_link.
This commit is contained in:
Matthias Clasen 2013-12-24 23:26:25 -05:00
parent f16753cfe0
commit 6fe85aee8d

View File

@ -19,6 +19,7 @@
* if advised of the possibility of such damage.
*/
#include "config.h"
#include <string.h>
#include <errno.h>
@ -777,6 +778,47 @@ test_set_contents (void)
g_free (name);
}
static void
test_read_link (void)
{
#ifdef HAVE_READLINK
#ifdef G_OS_UNIX
int ret;
const gchar *oldpath;
const gchar *newpath;
const gchar *badpath;
gchar *path;
GError *error = NULL;
oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL);
newpath = g_test_get_filename (G_TEST_DIST, "page-of-junk", NULL);
badpath = g_test_get_filename (G_TEST_DIST, "4097-random-bytes", NULL);
remove (newpath);
ret = symlink (oldpath, newpath);
g_assert (ret == 0);
path = g_file_read_link (newpath, &error);
g_assert_no_error (error);
g_assert_cmpstr (path, ==, oldpath);
g_free (path);
remove (newpath);
ret = symlink (badpath, newpath);
g_assert (ret == 0);
path = g_file_read_link (newpath, &error);
g_assert_no_error (error);
g_assert_cmpstr (path, ==, badpath);
g_free (path);
path = g_file_read_link (oldpath, &error);
g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
g_assert_null (path);
#endif
#else
g_test_skip ("Symbolic links not supported");
#endif
}
int
main (int argc,
char *argv[])
@ -796,6 +838,7 @@ main (int argc,
g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
g_test_add_func ("/fileutils/set-contents", test_set_contents);
g_test_add_func ("/fileutils/read-link", test_read_link);
return g_test_run ();
}