2020-07-27 16:40:13 +02:00
|
|
|
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
|
2011-10-12 06:37:02 +02:00
|
|
|
#define GLIB_DISABLE_DEPRECATION_WARNINGS
|
2020-07-27 16:40:13 +02:00
|
|
|
#endif
|
2011-10-12 06:37:02 +02:00
|
|
|
|
2011-02-14 05:47:42 +01:00
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
2012-08-28 00:30:06 +02:00
|
|
|
#include <glib/gstdio.h>
|
2011-09-21 19:59:03 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
2011-02-14 05:47:42 +01:00
|
|
|
|
Replace #ifdef HAVE_UNISTD_H checks with #ifdef G_OS_UNIX
In Windows development environments that have it, <unistd.h> is mostly
just a wrapper around several other native headers (in particular,
<io.h>, which contains read(), close(), etc, and <process.h>, which
contains getpid()). But given that some Windows dev environments don't
have <unistd.h>, everything that uses those functions on Windows
already needed to include the correct Windows header as well, and so
there is never any point to including <unistd.h> on Windows.
Also, remove some <unistd.h> includes (and a few others) that were
unnecessary even on unix.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 19:04:00 +02:00
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2011-06-06 07:14:23 +02:00
|
|
|
static void
|
|
|
|
test_basic (void)
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
2013-05-29 14:49:16 +02:00
|
|
|
file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
|
2011-06-06 07:14:23 +02:00
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
g_mapped_file_ref (file);
|
|
|
|
g_mapped_file_unref (file);
|
|
|
|
|
|
|
|
g_mapped_file_unref (file);
|
|
|
|
}
|
|
|
|
|
2011-02-14 05:47:42 +01:00
|
|
|
static void
|
|
|
|
test_empty (void)
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
2013-05-29 14:49:16 +02:00
|
|
|
file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
|
2011-02-14 05:47:42 +01:00
|
|
|
g_assert_no_error (error);
|
|
|
|
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_null (g_mapped_file_get_contents (file));
|
2011-02-14 05:47:42 +01:00
|
|
|
|
|
|
|
g_mapped_file_free (file);
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:57 +01:00
|
|
|
#ifdef G_OS_UNIX
|
2011-09-18 01:58:28 +02:00
|
|
|
static void
|
|
|
|
test_device (void)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GMappedFile *file;
|
|
|
|
|
|
|
|
file = g_mapped_file_new ("/dev/null", FALSE, &error);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_true (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_null (file);
|
2011-09-18 01:58:28 +02:00
|
|
|
g_error_free (error);
|
|
|
|
}
|
2012-11-10 17:06:57 +01:00
|
|
|
#endif
|
2011-09-18 01:58:28 +02:00
|
|
|
|
2011-02-14 05:47:42 +01:00
|
|
|
static void
|
|
|
|
test_nonexisting (void)
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
file = g_mapped_file_new ("no-such-file", FALSE, &error);
|
|
|
|
g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
|
|
|
|
g_clear_error (&error);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_null (file);
|
2011-02-14 05:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_writable (void)
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
2013-05-04 00:13:51 +02:00
|
|
|
GError *error = NULL;
|
2011-02-14 05:47:42 +01:00
|
|
|
gchar *contents;
|
2013-05-04 00:13:51 +02:00
|
|
|
gsize len;
|
2011-02-14 05:47:42 +01:00
|
|
|
const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
|
|
|
|
const gchar *new = "abcdefghijklmnopqrstuvxyz";
|
2013-05-04 00:13:51 +02:00
|
|
|
gchar *tmp_copy_path;
|
2011-02-14 05:47:42 +01:00
|
|
|
|
2014-02-16 19:44:35 +01:00
|
|
|
tmp_copy_path = g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL);
|
2011-04-13 14:19:35 +02:00
|
|
|
|
2013-05-29 14:49:16 +02:00
|
|
|
g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
|
2013-05-04 00:13:51 +02:00
|
|
|
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);
|
2011-02-14 05:47:42 +01:00
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
contents = g_mapped_file_get_contents (file);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);
|
2011-02-14 05:47:42 +01:00
|
|
|
|
|
|
|
memcpy (contents, new, strlen (new));
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpuint (strncmp (contents, new, strlen (new)), ==, 0);
|
2011-02-14 05:47:42 +01:00
|
|
|
|
|
|
|
g_mapped_file_free (file);
|
|
|
|
|
|
|
|
error = NULL;
|
2013-05-04 00:13:51 +02:00
|
|
|
file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
|
2011-02-14 05:47:42 +01:00
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
contents = g_mapped_file_get_contents (file);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);
|
2011-02-14 05:47:42 +01:00
|
|
|
|
|
|
|
g_mapped_file_free (file);
|
2013-05-04 00:13:51 +02:00
|
|
|
|
|
|
|
g_free (tmp_copy_path);
|
2011-02-14 05:47:42 +01:00
|
|
|
}
|
|
|
|
|
2011-09-21 19:59:03 +02:00
|
|
|
static void
|
|
|
|
test_writable_fd (void)
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
2013-05-04 00:13:51 +02:00
|
|
|
GError *error = NULL;
|
2011-09-21 19:59:03 +02:00
|
|
|
gchar *contents;
|
|
|
|
const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
|
|
|
|
const gchar *new = "abcdefghijklmnopqrstuvxyz";
|
2013-05-04 00:13:51 +02:00
|
|
|
gsize len;
|
2011-09-21 19:59:03 +02:00
|
|
|
int fd;
|
2013-05-04 00:13:51 +02:00
|
|
|
gchar *tmp_copy_path;
|
2011-09-21 19:59:03 +02:00
|
|
|
|
2014-02-16 19:44:35 +01:00
|
|
|
tmp_copy_path = g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL);
|
2011-09-21 19:59:03 +02:00
|
|
|
|
2013-05-29 14:49:16 +02:00
|
|
|
g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
|
2013-05-04 00:13:51 +02:00
|
|
|
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);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpint (fd, !=, -1);
|
2011-09-21 19:59:03 +02:00
|
|
|
file = g_mapped_file_new_from_fd (fd, TRUE, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
contents = g_mapped_file_get_contents (file);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);
|
2011-09-21 19:59:03 +02:00
|
|
|
|
|
|
|
memcpy (contents, new, strlen (new));
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpuint (strncmp (contents, new, strlen (new)), ==, 0);
|
2011-09-21 19:59:03 +02:00
|
|
|
|
|
|
|
g_mapped_file_free (file);
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
error = NULL;
|
2013-05-04 00:13:51 +02:00
|
|
|
fd = g_open (tmp_copy_path, O_RDWR, 0);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpint (fd, !=, -1);
|
2011-09-21 19:59:03 +02:00
|
|
|
file = g_mapped_file_new_from_fd (fd, TRUE, &error);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
contents = g_mapped_file_get_contents (file);
|
2019-03-05 11:58:09 +01:00
|
|
|
g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);
|
2011-09-21 19:59:03 +02:00
|
|
|
|
|
|
|
g_mapped_file_free (file);
|
|
|
|
|
2013-05-04 00:13:51 +02:00
|
|
|
g_free (tmp_copy_path);
|
2011-09-21 19:59:03 +02:00
|
|
|
}
|
|
|
|
|
2012-05-30 00:54:58 +02:00
|
|
|
static void
|
|
|
|
test_gbytes (void)
|
|
|
|
{
|
|
|
|
GMappedFile *file;
|
|
|
|
GBytes *bytes;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
2013-05-29 14:49:16 +02:00
|
|
|
file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
|
2012-05-30 00:54:58 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-02-14 05:47:42 +01:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
2011-06-06 07:14:23 +02:00
|
|
|
g_test_add_func ("/mappedfile/basic", test_basic);
|
2011-02-14 05:47:42 +01:00
|
|
|
g_test_add_func ("/mappedfile/empty", test_empty);
|
2012-11-10 17:06:57 +01:00
|
|
|
#ifdef G_OS_UNIX
|
2011-09-19 04:14:19 +02:00
|
|
|
g_test_add_func ("/mappedfile/device", test_device);
|
2012-11-10 17:06:57 +01:00
|
|
|
#endif
|
2011-02-14 05:47:42 +01:00
|
|
|
g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
|
|
|
|
g_test_add_func ("/mappedfile/writable", test_writable);
|
2011-09-21 19:59:03 +02:00
|
|
|
g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
|
2012-05-30 00:54:58 +02:00
|
|
|
g_test_add_func ("/mappedfile/gbytes", test_gbytes);
|
2011-02-14 05:47:42 +01:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|