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

View File

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