GVariant: support serialising to GVariantVectors
[platform/upstream/glib.git] / glib / tests / mappedfile.c
index 7a56e67..ccaea34 100644 (file)
@@ -1,9 +1,18 @@
-#include <config.h>
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
+
 #include <glib.h>
 #include <string.h>
-#ifdef HAVE_UNISTD_H
+#include <glib/gstdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#ifdef G_OS_UNIX
 #include <unistd.h>
 #endif
+#ifdef G_OS_WIN32
+#include <io.h>
+#endif
 
 static void
 test_basic (void)
@@ -12,7 +21,7 @@ test_basic (void)
   GError *error;
 
   error = NULL;
-  file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
+  file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
   g_assert_no_error (error);
 
   g_mapped_file_ref (file);
@@ -28,7 +37,7 @@ test_empty (void)
   GError *error;
 
   error = NULL;
-  file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
+  file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
   g_assert_no_error (error);
 
   g_assert (g_mapped_file_get_contents (file) == NULL);
@@ -36,6 +45,7 @@ test_empty (void)
   g_mapped_file_free (file);
 }
 
+#ifdef G_OS_UNIX
 static void
 test_device (void)
 {
@@ -43,10 +53,13 @@ test_device (void)
   GMappedFile *file;
 
   file = g_mapped_file_new ("/dev/null", FALSE, &error);
-  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
+  g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
+            g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NODEV) ||
+            g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
   g_assert (file == NULL);
   g_error_free (error);
 }
+#endif
 
 static void
 test_nonexisting (void)
@@ -65,19 +78,69 @@ static void
 test_writable (void)
 {
   GMappedFile *file;
-  GError *error;
+  GError *error = NULL;
   gchar *contents;
+  gsize len;
   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
   const gchar *new = "abcdefghijklmnopqrstuvxyz";
+  gchar *tmp_copy_path;
+
+  tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
 
-  if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
-    {
-      g_test_message ("Skipping writable mapping test");
-      return;
-    }
+  g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
+  g_assert_no_error (error);
+  g_file_set_contents (tmp_copy_path, contents, len, &error);
+  g_assert_no_error (error);
+  
+  g_free (contents);
+
+  file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
+  g_assert_no_error (error);
+
+  contents = g_mapped_file_get_contents (file);
+  g_assert (strncmp (contents, old, strlen (old)) == 0);
+
+  memcpy (contents, new, strlen (new));
+  g_assert (strncmp (contents, new, strlen (new)) == 0);
+
+  g_mapped_file_free (file);
 
   error = NULL;
-  file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
+  file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
+  g_assert_no_error (error);
+
+  contents = g_mapped_file_get_contents (file);
+  g_assert (strncmp (contents, old, strlen (old)) == 0);
+
+  g_mapped_file_free (file);
+
+  g_free (tmp_copy_path);
+}
+
+static void
+test_writable_fd (void)
+{
+  GMappedFile *file;
+  GError *error = NULL;
+  gchar *contents;
+  const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
+  const gchar *new = "abcdefghijklmnopqrstuvxyz";
+  gsize len;
+  int fd;
+  gchar *tmp_copy_path;
+
+  tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
+
+  g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
+  g_assert_no_error (error);
+  g_file_set_contents (tmp_copy_path, contents, len, &error);
+  g_assert_no_error (error);
+  
+  g_free (contents);
+
+  fd = g_open (tmp_copy_path, O_RDWR, 0);
+  g_assert (fd != -1);
+  file = g_mapped_file_new_from_fd (fd, TRUE, &error);
   g_assert_no_error (error);
 
   contents = g_mapped_file_get_contents (file);
@@ -87,15 +150,38 @@ test_writable (void)
   g_assert (strncmp (contents, new, strlen (new)) == 0);
 
   g_mapped_file_free (file);
+  close (fd);
 
   error = NULL;
-  file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
+  fd = g_open (tmp_copy_path, O_RDWR, 0);
+  g_assert (fd != -1);
+  file = g_mapped_file_new_from_fd (fd, TRUE, &error);
   g_assert_no_error (error);
 
   contents = g_mapped_file_get_contents (file);
   g_assert (strncmp (contents, old, strlen (old)) == 0);
 
   g_mapped_file_free (file);
+
+  g_free (tmp_copy_path);
+}
+
+static void
+test_gbytes (void)
+{
+  GMappedFile *file;
+  GBytes *bytes;
+  GError *error;
+
+  error = NULL;
+  file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
+  g_assert_no_error (error);
+
+  bytes = g_mapped_file_get_bytes (file);
+  g_mapped_file_unref (file);
+
+  g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
+  g_bytes_unref (bytes);
 }
 
 int
@@ -105,8 +191,13 @@ main (int argc, char *argv[])
 
   g_test_add_func ("/mappedfile/basic", test_basic);
   g_test_add_func ("/mappedfile/empty", test_empty);
+#ifdef G_OS_UNIX
+  g_test_add_func ("/mappedfile/device", test_device);
+#endif
   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
   g_test_add_func ("/mappedfile/writable", test_writable);
+  g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
+  g_test_add_func ("/mappedfile/gbytes", test_gbytes);
 
   return g_test_run ();
 }