Fix tests to compile again - add missing headers to W32, call correct process-id...
[platform/upstream/glib.git] / glib / tests / fileutils.c
index 5ac5340..1f66392 100644 (file)
@@ -19,6 +19,7 @@
  * if advised of the possibility of such damage.
  */
 
+#include "config.h"
 #include <string.h>
 #include <errno.h>
 
 #define GLIB_DISABLE_DEPRECATION_WARNINGS
 
 #include <glib.h>
+
+/* Test our stdio wrappers here */
+#define G_STDIO_NO_WRAP_ON_UNIX
 #include <glib/gstdio.h>
 
 #ifdef G_OS_UNIX
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #endif
+#include <fcntl.h>
+#include <utime.h>
 #ifdef G_OS_WIN32
 #include <windows.h>
 #endif
@@ -642,17 +650,20 @@ test_dir_make_tmp (void)
 {
   gchar *name;
   GError *error = NULL;
+  gint ret;
 
   name = g_dir_make_tmp ("testXXXXXXtest", &error);
   g_assert_no_error (error);
   g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
-  g_assert (g_rmdir (name) == 0);
+  ret = g_rmdir (name);
+  g_assert (ret == 0);
   g_free (name);
 
   name = g_dir_make_tmp (NULL, &error);
   g_assert_no_error (error);
   g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
-  g_assert (g_rmdir (name) == 0);
+  ret = g_rmdir (name);
+  g_assert (ret == 0);
   g_free (name);
 
   name = g_dir_make_tmp ("test/XXXXXX", &error);
@@ -777,6 +788,117 @@ 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;
+  gchar *cwd;
+  gchar *newpath;
+  gchar *badpath;
+  gchar *path;
+  GError *error = NULL;
+
+  cwd = g_get_current_dir ();
+
+  oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL);
+  newpath = g_build_filename (cwd, "page-of-junk", NULL);
+  badpath = g_build_filename (cwd, "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);
+
+  g_free (cwd);
+  g_free (newpath);
+  g_free (badpath);
+
+#endif
+#else
+  g_test_skip ("Symbolic links not supported");
+#endif
+}
+
+static void
+test_stdio_wrappers (void)
+{
+  GStatBuf buf;
+  gchar *cwd, *path;
+  gint ret;
+  struct utimbuf ut;
+  GError *error = NULL;
+
+  g_remove ("mkdir-test/test-create");
+  g_rmdir ("mkdir-test");
+
+  ret = g_stat ("mkdir-test", &buf);
+  g_assert (ret == -1);
+  ret = g_mkdir ("mkdir-test", 0666);
+  g_assert (ret == 0);
+  ret = g_stat ("mkdir-test", &buf);
+  g_assert (ret == 0);
+  g_assert (S_ISDIR (buf.st_mode));
+
+  cwd = g_get_current_dir ();
+  path = g_build_filename (cwd, "mkdir-test", NULL);
+  g_free (cwd);
+  ret = g_chdir (path);
+  g_assert (errno == EACCES);
+  g_assert (ret == -1);
+  ret = g_chmod (path, 0777);
+  g_assert (ret == 0);
+  ret = g_chdir (path);
+  g_assert (ret == 0);
+  cwd = g_get_current_dir ();
+  g_assert (g_str_equal (cwd, path));
+  g_free (cwd);
+  g_free (path);
+
+  ret = g_creat ("test-creat", 0555);
+  g_close (ret, &error);
+  g_assert_no_error (error);
+
+  ret = g_access ("test-creat", F_OK);
+  g_assert (ret == 0);
+
+  ret = g_rename ("test-creat", "test-create");
+  g_assert (ret == 0);
+
+  ret = g_open ("test-create", O_RDONLY, 0666);
+  g_close (ret, &error);
+  g_assert_no_error (error);
+
+  ut.actime = ut.modtime = (time_t)0;
+  ret = g_utime ("test-create", &ut);
+  g_assert (ret == 0);
+
+  ret = g_lstat ("test-create", &buf);
+  g_assert (ret == 0);
+  g_assert (buf.st_atime == (time_t)0);
+  g_assert (buf.st_mtime == (time_t)0);
+
+  g_chdir ("..");
+  g_remove ("mkdir-test/test-create");
+  g_rmdir ("mkdir-test");
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -796,6 +918,8 @@ 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);
+  g_test_add_func ("/fileutils/stdio-wrappers", test_stdio_wrappers);
 
   return g_test_run ();
 }