Improve fileutils test coverage
authorMatthias Clasen <mclasen@redhat.com>
Wed, 25 Dec 2013 04:26:25 +0000 (23:26 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Wed, 1 Jan 2014 22:59:21 +0000 (17:59 -0500)
Add some tests for g_file_read_link.

glib/tests/fileutils.c

index 5ac5340..d12a34c 100644 (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 ();
 }