Merge branch 'backport-1192-1193-gdbus-peer-brokenness' into 'glib-2-62'

Backport !1192, !1193, !1197 Fixes for gdbus-peer tests to glib-2-62

See merge request GNOME/glib!1203
This commit is contained in:
Simon McVittie 2019-10-31 14:01:26 +00:00
commit b123ee4db6
3 changed files with 87 additions and 54 deletions

View File

@ -48,6 +48,7 @@ struct _GDBusAuthMechanismSha1Private
gboolean is_client;
gboolean is_server;
GDBusAuthMechanismState state;
gchar *reject_reason; /* non-NULL iff (state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED) */
/* used on the client side */
gchar *to_send;
@ -101,6 +102,7 @@ _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
{
GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
g_free (mechanism->priv->reject_reason);
g_free (mechanism->priv->to_send);
g_free (mechanism->priv->cookie);
@ -290,7 +292,7 @@ ensure_keyring_directory (GError **error)
goto out;
}
if (g_mkdir (path, 0700) != 0)
if (g_mkdir_with_parents (path, 0700) != 0)
{
int errsv = errno;
g_set_error (error,
@ -963,7 +965,8 @@ mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
tokens = g_strsplit (data, " ", 0);
if (g_strv_length (tokens) != 2)
{
g_warning ("Malformed data '%s'", data);
g_free (m->priv->reject_reason);
m->priv->reject_reason = g_strdup_printf ("Malformed data '%s'", data);
m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
goto out;
}
@ -979,6 +982,8 @@ mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
}
else
{
g_free (m->priv->reject_reason);
m->priv->reject_reason = g_strdup_printf ("SHA-1 mismatch");
m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
}
@ -1014,7 +1019,8 @@ mechanism_server_data_send (GDBusAuthMechanism *mechanism,
&m->priv->cookie,
&error))
{
g_warning ("Error adding entry to keyring: %s", error->message);
g_free (m->priv->reject_reason);
m->priv->reject_reason = g_strdup_printf ("Error adding entry to keyring: %s", error->message);
g_error_free (error);
m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
goto out;
@ -1042,10 +1048,7 @@ mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
/* can never end up here because we are never in the REJECTED state */
g_assert_not_reached ();
return NULL;
return g_strdup (m->priv->reject_reason);
}
static void
@ -1128,7 +1131,8 @@ mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
tokens = g_strsplit (data, " ", 0);
if (g_strv_length (tokens) != 3)
{
g_warning ("Malformed data '%s'", data);
g_free (m->priv->reject_reason);
m->priv->reject_reason = g_strdup_printf ("Malformed data '%s'", data);
m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
goto out;
}
@ -1137,7 +1141,8 @@ mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
if (*endp != '\0')
{
g_warning ("Malformed cookie_id '%s'", tokens[1]);
g_free (m->priv->reject_reason);
m->priv->reject_reason = g_strdup_printf ("Malformed cookie_id '%s'", tokens[1]);
m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
goto out;
}
@ -1147,7 +1152,8 @@ mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
if (cookie == NULL)
{
g_warning ("Problems looking up entry in keyring: %s", error->message);
g_free (m->priv->reject_reason);
m->priv->reject_reason = g_strdup_printf ("Problems looking up entry in keyring: %s", error->message);
g_error_free (error);
m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
goto out;

View File

@ -179,6 +179,8 @@ g_dbus_server_finalize (GObject *object)
{
GDBusServer *server = G_DBUS_SERVER (object);
g_assert (!server->active);
if (server->authentication_observer != NULL)
g_object_unref (server->authentication_observer);
@ -197,19 +199,8 @@ g_dbus_server_finalize (GObject *object)
g_free (server->nonce);
}
if (server->unix_socket_path)
{
if (g_unlink (server->unix_socket_path) != 0)
g_warning ("Failed to delete %s: %s", server->unix_socket_path, g_strerror (errno));
g_free (server->unix_socket_path);
}
if (server->nonce_file)
{
if (g_unlink (server->nonce_file) != 0)
g_warning ("Failed to delete %s: %s", server->nonce_file, g_strerror (errno));
g_free (server->nonce_file);
}
g_free (server->unix_socket_path);
g_free (server->nonce_file);
g_main_context_unref (server->main_context_at_construction);
@ -622,6 +613,12 @@ g_dbus_server_start (GDBusServer *server)
return;
/* Right now we don't have any transport not using the listener... */
g_assert (server->is_using_listener);
server->run_signal_handler_id = g_signal_connect_data (G_SOCKET_SERVICE (server->listener),
"run",
G_CALLBACK (on_run),
g_object_ref (server),
(GClosureNotify) g_object_unref,
0 /* flags */);
g_socket_service_start (G_SOCKET_SERVICE (server->listener));
server->active = TRUE;
g_object_notify (G_OBJECT (server), "active");
@ -648,6 +645,18 @@ g_dbus_server_stop (GDBusServer *server)
g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
server->active = FALSE;
g_object_notify (G_OBJECT (server), "active");
if (server->unix_socket_path)
{
if (g_unlink (server->unix_socket_path) != 0)
g_warning ("Failed to delete %s: %s", server->unix_socket_path, g_strerror (errno));
}
if (server->nonce_file)
{
if (g_unlink (server->nonce_file) != 0)
g_warning ("Failed to delete %s: %s", server->nonce_file, g_strerror (errno));
}
}
/* ---------------------------------------------------------------------------------------------------- */
@ -1159,15 +1168,7 @@ initable_init (GInitable *initable,
if (ret)
{
if (last_error != NULL)
g_error_free (last_error);
/* Right now we don't have any transport not using the listener... */
g_assert (server->is_using_listener);
server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener),
"run",
G_CALLBACK (on_run),
server);
g_clear_error (&last_error);
}
else
{

View File

@ -273,14 +273,9 @@ setup_test_address (void)
{
if (is_unix)
{
g_test_message ("Testing with unix:tmpdir address");
if (g_unix_socket_address_abstract_names_supported ())
tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
else
{
tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
}
g_test_message ("Testing with unix:dir address");
tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
tmp_address = g_strdup_printf ("unix:dir=%s", tmpdir);
}
else
tmp_address = g_strdup ("nonce-tcp:");
@ -288,11 +283,11 @@ setup_test_address (void)
#ifdef G_OS_UNIX
static void
setup_dir_test_address (void)
setup_tmpdir_test_address (void)
{
g_test_message ("Testing with unix:dir address");
g_test_message ("Testing with unix:tmpdir address");
tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
tmp_address = g_strdup_printf ("unix:dir=%s", tmpdir);
tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
}
static void
@ -313,7 +308,8 @@ teardown_test_address (void)
/* Ensuring the rmdir succeeds also ensures any sockets created on the
* filesystem are also deleted.
*/
g_assert_cmpint (g_rmdir (tmpdir), ==, 0);
g_assert_cmpstr (g_rmdir (tmpdir) == 0 ? "OK" : g_strerror (errno),
==, "OK");
g_clear_pointer (&tmpdir, g_free);
}
}
@ -1033,6 +1029,9 @@ do_test_peer (void)
static void
test_peer (void)
{
test_guid = g_dbus_generate_guid ();
loop = g_main_loop_new (NULL, FALSE);
/* Run this test multiple times using different address formats to ensure
* they all work.
*/
@ -1041,7 +1040,7 @@ test_peer (void)
teardown_test_address ();
#ifdef G_OS_UNIX
setup_dir_test_address ();
setup_tmpdir_test_address ();
do_test_peer ();
teardown_test_address ();
@ -1049,6 +1048,9 @@ test_peer (void)
do_test_peer ();
teardown_test_address ();
#endif
g_main_loop_unref (loop);
g_free (test_guid);
}
/* ---------------------------------------------------------------------------------------------------- */
@ -1064,6 +1066,9 @@ test_peer_signals (void)
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1620");
test_guid = g_dbus_generate_guid ();
loop = g_main_loop_new (NULL, FALSE);
setup_test_address ();
memset (&data, '\0', sizeof (PeerData));
data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
@ -1119,6 +1124,9 @@ test_peer_signals (void)
g_thread_join (service_thread);
teardown_test_address ();
g_main_loop_unref (loop);
g_free (test_guid);
}
/* ---------------------------------------------------------------------------------------------------- */
@ -1246,6 +1254,7 @@ dmp_thread_func (gpointer user_data)
data->loop = g_main_loop_new (data->context, FALSE);
g_main_loop_run (data->loop);
g_dbus_server_stop (data->server);
g_main_context_pop_thread_default (data->context);
g_free (guid);
@ -1260,6 +1269,9 @@ delayed_message_processing (void)
GThread *service_thread;
guint n;
test_guid = g_dbus_generate_guid ();
loop = g_main_loop_new (NULL, FALSE);
setup_test_address ();
data = g_new0 (DmpData, 1);
@ -1307,6 +1319,9 @@ delayed_message_processing (void)
g_thread_join (service_thread);
dmp_data_free (data);
teardown_test_address ();
g_main_loop_unref (loop);
g_free (test_guid);
}
/* ---------------------------------------------------------------------------------------------------- */
@ -1405,6 +1420,9 @@ test_nonce_tcp (void)
gboolean res;
const gchar *address;
test_guid = g_dbus_generate_guid ();
loop = g_main_loop_new (NULL, FALSE);
memset (&data, '\0', sizeof (PeerData));
data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
@ -1512,6 +1530,9 @@ test_nonce_tcp (void)
g_thread_join (service_thread);
g_ptr_array_unref (data.current_connections);
g_main_loop_unref (loop);
g_free (test_guid);
}
static void
@ -1596,6 +1617,9 @@ test_tcp_anonymous (void)
GDBusConnection *connection;
GError *error;
test_guid = g_dbus_generate_guid ();
loop = g_main_loop_new (NULL, FALSE);
seen_connection = FALSE;
service_thread = g_thread_new ("tcp-anon-service",
tcp_anonymous_service_thread_func,
@ -1623,6 +1647,9 @@ test_tcp_anonymous (void)
server = NULL;
g_thread_join (service_thread);
g_main_loop_unref (loop);
g_free (test_guid);
}
/* ---------------------------------------------------------------------------------------------------- */
@ -1767,6 +1794,9 @@ codegen_test_peer (void)
GVariant *value;
const gchar *s;
test_guid = g_dbus_generate_guid ();
loop = g_main_loop_new (NULL, FALSE);
setup_test_address ();
/* bring up a server - we run the server in a different thread to avoid deadlocks */
@ -1874,6 +1904,9 @@ codegen_test_peer (void)
g_thread_join (service_thread);
teardown_test_address ();
g_main_loop_unref (loop);
g_free (test_guid);
}
/* ---------------------------------------------------------------------------------------------------- */
@ -1886,17 +1919,12 @@ main (int argc,
gint ret;
GDBusNodeInfo *introspection_data = NULL;
g_test_init (&argc, &argv, NULL);
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
g_assert (introspection_data != NULL);
test_interface_introspection_data = introspection_data->interfaces[0];
test_guid = g_dbus_generate_guid ();
/* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE);
g_test_add_func ("/gdbus/peer-to-peer", test_peer);
g_test_add_func ("/gdbus/peer-to-peer/signals", test_peer_signals);
g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
@ -1906,10 +1934,8 @@ main (int argc,
g_test_add_func ("/gdbus/credentials", test_credentials);
g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
ret = g_test_run();
ret = g_test_run ();
g_main_loop_unref (loop);
g_free (test_guid);
g_dbus_node_info_unref (introspection_data);
return ret;