Merge branch 'mapping-test' into 'main'

Moving tests/mapping-test.c to glib/test/mapping.c

See merge request GNOME/glib!2607
This commit is contained in:
Philip Withnall 2022-05-10 18:05:07 +00:00
commit 2f73cc670c
3 changed files with 108 additions and 109 deletions

View File

@ -14,13 +14,9 @@
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include "glib.h" #include <glib.h>
#include "gstdio.h" #include <glib/gstdio.h>
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
#include <unistd.h> #include <unistd.h>
@ -29,12 +25,12 @@
#include <process.h> #include <process.h>
#endif #endif
static gchar *dir, *global_filename, *global_displayname, *childname;
static gboolean stop = FALSE; static gboolean stop = FALSE;
static gint parent_pid; static gint parent_pid;
/* Passing argc and argv through global variables */
static char **local_argv;
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
static void static void
@ -66,15 +62,11 @@ write_or_die (const gchar *filename,
gssize length) gssize length)
{ {
GError *error = NULL; GError *error = NULL;
gchar *displayname; gboolean result;
if (!g_file_set_contents (filename, contents, length, &error)) result = g_file_set_contents (filename, contents, length, &error);
{ g_assert_no_error (error);
displayname = g_filename_display_name (childname); g_assert_true (result);
g_print ("failed to write '%s': %s\n",
displayname, error->message);
exit (1);
}
} }
static GMappedFile * static GMappedFile *
@ -83,20 +75,14 @@ map_or_die (const gchar *filename,
{ {
GError *error = NULL; GError *error = NULL;
GMappedFile *map; GMappedFile *map;
gchar *displayname;
map = g_mapped_file_new (filename, writable, &error); map = g_mapped_file_new (filename, writable, &error);
if (!map) g_assert_no_error (error);
{ g_assert_nonnull (map);
displayname = g_filename_display_name (childname);
g_print ("failed to map '%s' non-writable, shared: %s\n",
displayname, error->message);
exit (1);
}
return map; return map;
} }
static gboolean static gboolean
signal_parent (gpointer data) signal_parent (gpointer data)
{ {
@ -106,13 +92,18 @@ signal_parent (gpointer data)
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
static int static void
child_main (int argc, char *argv[]) child_main (void)
{ {
GMappedFile *map; GMappedFile *map;
GMainLoop *loop; GMainLoop *loop;
gchar *dir, *global_filename, *childname;
parent_pid = atoi (argv[2]); dir = g_get_current_dir ();
global_filename = g_build_filename (dir, "maptest", NULL);
childname = g_build_filename (dir, "mapchild", NULL);
parent_pid = atoi (local_argv[2]);
map = map_or_die (global_filename, FALSE); map = map_or_die (global_filename, FALSE);
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
@ -123,41 +114,58 @@ child_main (int argc, char *argv[])
g_idle_add (signal_parent, NULL); g_idle_add (signal_parent, NULL);
g_main_loop_run (loop); g_main_loop_run (loop);
g_message ("test_child_private: received parent signal"); g_test_message ("test_child_private: received parent signal");
write_or_die (childname, write_or_die (childname,
g_mapped_file_get_contents (map), g_mapped_file_get_contents (map),
g_mapped_file_get_length (map)); g_mapped_file_get_length (map));
g_free (childname);
g_free (global_filename);
g_free (dir);
signal_parent (NULL); signal_parent (NULL);
return 0;
} }
static void static void
test_mapping (void) test_mapping_flags (void)
{ {
GMappedFile *map; GMappedFile *map;
gchar *dir, *global_filename;
dir = g_get_current_dir ();
global_filename = g_build_filename (dir, "maptest", NULL);
write_or_die (global_filename, "ABC", -1); write_or_die (global_filename, "ABC", -1);
map = map_or_die (global_filename, FALSE); map = map_or_die (global_filename, FALSE);
g_assert (g_mapped_file_get_length (map) == 3); g_assert_cmpint (g_mapped_file_get_length (map), ==, 3);
g_mapped_file_free (map); g_mapped_file_unref (map);
map = map_or_die (global_filename, TRUE); map = map_or_die (global_filename, TRUE);
g_assert (g_mapped_file_get_length (map) == 3); g_assert_cmpint (g_mapped_file_get_length (map), ==, 3);
g_mapped_file_free (map); g_mapped_file_unref (map);
g_message ("test_mapping: ok"); g_test_message ("test_mapping: ok");
/* Cleaning left over files */
g_remove ("maptest");
g_free (global_filename);
g_free (dir);
} }
static void static void
test_private (void) test_private (void)
{ {
GError *error = NULL; GError *error = NULL;
GMappedFile *map; GMappedFile *map;
gboolean result;
gchar *buffer; gchar *buffer;
gsize len; gsize len;
gchar *dir, *global_filename;
dir = g_get_current_dir ();
global_filename = g_build_filename (dir, "maptest", NULL);
write_or_die (global_filename, "ABC", -1); write_or_die (global_filename, "ABC", -1);
map = map_or_die (global_filename, TRUE); map = map_or_die (global_filename, TRUE);
@ -166,27 +174,30 @@ test_private (void)
buffer[0] = '1'; buffer[0] = '1';
buffer[1] = '2'; buffer[1] = '2';
buffer[2] = '3'; buffer[2] = '3';
g_mapped_file_free (map); g_mapped_file_unref (map);
if (!g_file_get_contents (global_filename, &buffer, &len, &error)) result = g_file_get_contents (global_filename, &buffer, &len, &error);
{ g_assert_no_error (error);
g_print ("failed to read '%s': %s\n", g_assert_true (result);
global_displayname, error->message); g_assert_cmpint (len, ==, 3);
exit (1); g_assert_cmpstr (buffer, ==, "ABC");
}
g_assert (len == 3);
g_assert (strcmp (buffer, "ABC") == 0);
g_free (buffer); g_free (buffer);
g_message ("test_private: ok"); g_free (global_filename);
g_free (dir);
/* Cleaning left over files */
g_remove ("maptest");
g_test_message ("test_private: ok");
} }
static void static void
test_child_private (gchar *argv0) test_child_private (void)
{ {
GError *error = NULL; GError *error = NULL;
GMappedFile *map; GMappedFile *map;
gboolean result;
gchar *buffer; gchar *buffer;
gsize len; gsize len;
gchar *child_argv[4]; gchar *child_argv[4];
@ -195,12 +206,17 @@ test_child_private (gchar *argv0)
GMainLoop *loop; GMainLoop *loop;
#endif #endif
gchar pid[100]; gchar pid[100];
gchar *dir, *global_filename, *childname;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
g_remove ("STOP"); g_remove ("STOP");
g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS)); g_assert_false (g_file_test ("STOP", G_FILE_TEST_EXISTS));
#endif #endif
dir = g_get_current_dir ();
global_filename = g_build_filename (dir, "maptest", NULL);
childname = g_build_filename (dir, "mapchild", NULL);
write_or_die (global_filename, "ABC", -1); write_or_die (global_filename, "ABC", -1);
map = map_or_die (global_filename, TRUE); map = map_or_die (global_filename, TRUE);
@ -209,18 +225,16 @@ test_child_private (gchar *argv0)
#endif #endif
g_snprintf (pid, sizeof(pid), "%d", getpid ()); g_snprintf (pid, sizeof(pid), "%d", getpid ());
child_argv[0] = argv0; child_argv[0] = local_argv[0];
child_argv[1] = "mapchild"; child_argv[1] = "mapchild";
child_argv[2] = pid; child_argv[2] = pid;
child_argv[3] = NULL; child_argv[3] = NULL;
if (!g_spawn_async (dir, child_argv, NULL,
0, NULL, NULL, &child_pid, &error)) result = g_spawn_async (dir, child_argv, NULL,
{ 0, NULL, NULL, &child_pid, &error);
g_print ("failed to spawn child: %s\n", g_assert_no_error (error);
error->message); g_assert_true (result);
exit (1); g_test_message ("test_child_private: child spawned");
}
g_message ("test_child_private: child spawned");
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
@ -231,13 +245,13 @@ test_child_private (gchar *argv0)
g_usleep (2000000); g_usleep (2000000);
#endif #endif
g_message ("test_child_private: received first child signal"); g_test_message ("test_child_private: received first child signal");
buffer = (gchar *)g_mapped_file_get_contents (map); buffer = (gchar *)g_mapped_file_get_contents (map);
buffer[0] = '1'; buffer[0] = '1';
buffer[1] = '2'; buffer[1] = '2';
buffer[2] = '3'; buffer[2] = '3';
g_mapped_file_free (map); g_mapped_file_unref (map);
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
kill (child_pid, SIGUSR1); kill (child_pid, SIGUSR1);
@ -252,44 +266,30 @@ test_child_private (gchar *argv0)
g_usleep (2000000); g_usleep (2000000);
#endif #endif
g_message ("test_child_private: received second child signal"); g_test_message ("test_child_private: received second child signal");
if (!g_file_get_contents (childname, &buffer, &len, &error)) result = g_file_get_contents (childname, &buffer, &len, &error);
{ g_assert_no_error (error);
gchar *name; g_assert_true (result);
g_assert_cmpint (len, ==, 3);
name = g_filename_display_name (childname); g_assert_cmpstr (buffer, ==, "ABC");
g_print ("failed to read '%s': %s\n", name, error->message);
exit (1);
}
g_assert (len == 3);
g_assert (strcmp (buffer, "ABC") == 0);
g_free (buffer); g_free (buffer);
g_message ("test_child_private: ok"); g_free (childname);
} g_free (global_filename);
g_free (dir);
static int /* Cleaning left over files */
parent_main (int argc, g_remove ("mapchild");
char *argv[]) g_remove ("maptest");
{
/* test mapping with various flag combinations */
test_mapping ();
/* test private modification */ g_test_message ("test_child_private: ok");
test_private ();
/* test multiple clients, non-shared */
test_child_private (argv[0]);
return 0;
} }
int int
main (int argc, main (int argc,
char *argv[]) char *argv[])
{ {
int ret;
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
sigset_t sig_mask, old_mask; sigset_t sig_mask, old_mask;
@ -298,24 +298,23 @@ main (int argc,
if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0) if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0)
{ {
if (sigismember (&old_mask, SIGUSR1)) if (sigismember (&old_mask, SIGUSR1))
g_message ("SIGUSR1 was blocked, unblocking it"); g_test_message ("SIGUSR1 was blocked, unblocking it");
} }
#endif #endif
dir = g_get_current_dir (); local_argv = argv;
global_filename = g_build_filename (dir, "maptest", NULL);
global_displayname = g_filename_display_name (global_filename);
childname = g_build_filename (dir, "mapchild", NULL);
if (argc > 1) if (argc > 1)
ret = child_main (argc, argv); {
else child_main ();
ret = parent_main (argc, argv); return EXIT_SUCCESS;
}
g_free (childname); g_test_init (&argc, &argv, NULL);
g_free (global_filename);
g_free (global_displayname);
g_free (dir);
return ret; g_test_add_func ("/mapping/flags", test_mapping_flags);
g_test_add_func ("/mapping/private", test_private);
g_test_add_func ("/mapping/private-child", test_child_private);
return g_test_run ();
} }

View File

@ -52,6 +52,7 @@ glib_tests = {
'macros' : {}, 'macros' : {},
'mainloop' : {}, 'mainloop' : {},
'mappedfile' : {}, 'mappedfile' : {},
'mapping' : {},
'markup' : {}, 'markup' : {},
'markup-parse' : {}, 'markup-parse' : {},
'markup-collect' : {}, 'markup-collect' : {},

View File

@ -17,7 +17,6 @@ subdir('refcount')
tests = { tests = {
'gio-test' : {}, 'gio-test' : {},
'mapping-test' : {},
'slice-threadinit' : { 'slice-threadinit' : {
'dependencies' : [libgthread_dep], 'dependencies' : [libgthread_dep],
}, },