* 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
+
#define S G_DIR_SEPARATOR_S
static void
{
gchar *args[10];
+ g_assert (g_build_pathv ("", NULL) == NULL);
args[0] = NULL;
check_string (g_build_pathv ("", args), "");
args[0] = ""; args[1] = NULL;
/* Test also using the slash as file name separator */
#define U "/"
- check_string (g_build_filename (NULL), "");
+ /* check_string (g_build_filename (NULL), ""); */
check_string (g_build_filename (U, NULL), U);
check_string (g_build_filename (U"x", NULL), U"x");
check_string (g_build_filename ("x"U, NULL), "x"U);
static void
test_format_size_for_display (void)
{
+#ifdef G_OS_WIN32
+ SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
+#endif
/* nobody called setlocale(), so we should get "C" behaviour... */
check_string (g_format_size_for_display (0), "0 bytes");
check_string (g_format_size_for_display (1), "1 byte");
check_string (g_format_size_for_display (1024), "1.0 KB");
check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
+ check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
+ check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
+ check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
check_string (g_format_size (0), "0 bytes");
check_string (g_format_size (1), "1 byte");
check_string (g_format_size (2), "2 bytes");
- check_string (g_format_size (1000), "1.0 kB");
- check_string (g_format_size (1000 * 1000), "1.0 MB");
- check_string (g_format_size (1000 * 1000 * 1000), "1.0 GB");
+ check_string (g_format_size (1000ULL), "1.0 kB");
+ check_string (g_format_size (1000ULL * 1000), "1.0 MB");
+ check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
+ check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
+ check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
+ check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
+
+ check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
+ check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
+ check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
+
+ check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
+ check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
+ check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
+ check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
+ check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
+ check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
}
+static void
+test_file_errors (void)
+{
+#ifdef EEXIST
+ g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
+#endif
+#ifdef EISDIR
+ g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
+#endif
+#ifdef EACCES
+ g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
+#endif
+#ifdef ENAMETOOLONG
+ g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
+#endif
+#ifdef ENOENT
+ g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
+#endif
+#ifdef ENOTDIR
+ g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
+#endif
+#ifdef ENXIO
+ g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
+#endif
+#ifdef ENODEV
+ g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
+#endif
+#ifdef EROFS
+ g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
+#endif
+#ifdef ETXTBSY
+ g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
+#endif
+#ifdef EFAULT
+ g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
+#endif
+#ifdef ELOOP
+ g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
+#endif
+#ifdef ENOSPC
+ g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
+#endif
+#ifdef ENOMEM
+ g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
+#endif
+#ifdef EMFILE
+ g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
+#endif
+#ifdef ENFILE
+ g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
+#endif
+#ifdef EBADF
+ g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
+#endif
+#ifdef EINVAL
+ g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
+#endif
+#ifdef EPIPE
+ g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
+#endif
+#ifdef EAGAIN
+ g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
+#endif
+#ifdef EINTR
+ g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
+#endif
+#ifdef EIO
+ g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
+#endif
+#ifdef EPERM
+ g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
+#endif
+#ifdef ENOSYS
+ g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
+#endif
+}
+
+static void
+test_basename (void)
+{
+ gchar *b;
+
+ b = g_path_get_basename ("");
+ g_assert_cmpstr (b, ==, ".");
+ g_free (b);
+
+ b = g_path_get_basename ("///");
+ g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
+ g_free (b);
+
+ b = g_path_get_basename ("/a/b/c/d");
+ g_assert_cmpstr (b, ==, "d");
+ g_free (b);
+}
+
+static void
+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));
+ 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));
+ ret = g_rmdir (name);
+ g_assert (ret == 0);
+ g_free (name);
+
+ name = g_dir_make_tmp ("test/XXXXXX", &error);
+ g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
+ g_clear_error (&error);
+ g_assert (name == NULL);
+
+ name = g_dir_make_tmp ("XXXXxX", &error);
+ g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
+ g_clear_error (&error);
+ g_assert (name == NULL);
+}
+
+static void
+test_file_open_tmp (void)
+{
+ gchar *name = NULL;
+ GError *error = NULL;
+ gint fd;
+
+ fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
+ g_assert (fd != -1);
+ g_assert_no_error (error);
+ g_assert (name != NULL);
+ unlink (name);
+ g_free (name);
+ close (fd);
+
+ fd = g_file_open_tmp (NULL, &name, &error);
+ g_assert (fd != -1);
+ g_assert_no_error (error);
+ g_assert (name != NULL);
+ g_unlink (name);
+ g_free (name);
+ close (fd);
+
+ name = NULL;
+ fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
+ g_assert (fd == -1);
+ g_assert (name == NULL);
+ g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
+ g_clear_error (&error);
+
+ fd = g_file_open_tmp ("XXXXxX", &name, &error);
+ g_assert (fd == -1);
+ g_assert (name == NULL);
+ g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
+ g_clear_error (&error);
+}
+
+static void
+test_mkstemp (void)
+{
+ gchar *name;
+ gint fd;
+
+ name = g_strdup ("testXXXXXXtest"),
+ fd = g_mkstemp (name);
+ g_assert (fd != -1);
+ g_assert (strstr (name, "XXXXXX") == NULL);
+ unlink (name);
+ close (fd);
+ g_free (name);
+
+ name = g_strdup ("testYYYYYYtest"),
+ fd = g_mkstemp (name);
+ g_assert (fd == -1);
+ g_free (name);
+}
+
+static void
+test_mkdtemp (void)
+{
+ gchar *name;
+ gchar *ret;
+
+ name = g_strdup ("testXXXXXXtest"),
+ ret = g_mkdtemp (name);
+ g_assert (ret == name);
+ g_assert (strstr (name, "XXXXXX") == NULL);
+ g_rmdir (name);
+ g_free (name);
+
+ name = g_strdup ("testYYYYYYtest"),
+ ret = g_mkdtemp (name);
+ g_assert (ret == NULL);
+ g_free (name);
+}
+
+static void
+test_set_contents (void)
+{
+ GError *error = NULL;
+ gint fd;
+ gchar *name;
+ gchar *buf;
+ gsize len;
+ gboolean ret;
+
+ fd = g_file_open_tmp (NULL, &name, &error);
+ g_assert_no_error (error);
+ write (fd, "a", 1);
+ close (fd);
+
+ ret = g_file_get_contents (name, &buf, &len, &error);
+ g_assert (ret);
+ g_assert_no_error (error);
+ g_assert_cmpstr (buf, ==, "a");
+ g_free (buf);
+
+ ret = g_file_set_contents (name, "b", 1, &error);
+ g_assert (ret);
+ g_assert_no_error (error);
+
+ ret = g_file_get_contents (name, &buf, &len, &error);
+ g_assert (ret);
+ g_assert_no_error (error);
+ g_assert_cmpstr (buf, ==, "b");
+ g_free (buf);
+
+ g_remove (name);
+ 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[])
g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
-
- return g_test_run();
+ g_test_add_func ("/fileutils/errors", test_file_errors);
+ g_test_add_func ("/fileutils/basename", test_basename);
+ g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
+ g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
+ 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 ();
}