diff options
author | DongHun Kwak <dh0128.kwak@samsung.com> | 2021-10-29 10:36:08 +0900 |
---|---|---|
committer | DongHun Kwak <dh0128.kwak@samsung.com> | 2021-10-29 10:36:08 +0900 |
commit | 96171dcc7c00290fd968decfcf5ca5fc61fc69eb (patch) | |
tree | 5a389066c35ba59b6b2aa593f26177cbb3a2969b /gio | |
parent | ceeb6230c225466e29ca00144216e73ef1edd96b (diff) | |
download | glib-96171dcc7c00290fd968decfcf5ca5fc61fc69eb.tar.gz glib-96171dcc7c00290fd968decfcf5ca5fc61fc69eb.tar.bz2 glib-96171dcc7c00290fd968decfcf5ca5fc61fc69eb.zip |
Imported Upstream version 2.69.2upstream/2.69.2
Diffstat (limited to 'gio')
-rw-r--r-- | gio/gdbusaddress.c | 26 | ||||
-rw-r--r-- | gio/gdbusnamewatching.c | 28 | ||||
-rw-r--r-- | gio/giomodule.c | 2 | ||||
-rw-r--r-- | gio/giotypes.h | 4 | ||||
-rw-r--r-- | gio/gpowerprofilemonitorportal.c | 182 | ||||
-rw-r--r-- | gio/gpowerprofilemonitorportal.h | 31 | ||||
-rw-r--r-- | gio/gtlscertificate.c | 19 | ||||
-rw-r--r-- | gio/gtlsdatabase.c | 61 | ||||
-rwxr-xr-x | gio/gwin32packageparser.c | 2 | ||||
-rw-r--r-- | gio/meson.build | 1 | ||||
-rw-r--r-- | gio/tests/desktop-app-info.c | 3 | ||||
-rw-r--r-- | gio/tests/gdbus-names.c | 213 | ||||
-rwxr-xr-x | gio/tests/memory-monitor-dbus.py.in | 31 | ||||
-rwxr-xr-x | gio/tests/memory-monitor-portal.py.in | 32 | ||||
-rw-r--r-- | gio/tests/meson.build | 14 | ||||
-rwxr-xr-x | gio/tests/power-profile-monitor-dbus.py.in | 107 | ||||
-rwxr-xr-x | gio/tests/power-profile-monitor-portal.py.in | 126 | ||||
-rw-r--r-- | gio/tests/socket.c | 40 | ||||
-rw-r--r-- | gio/tests/trash.c | 19 |
19 files changed, 766 insertions, 175 deletions
diff --git a/gio/gdbusaddress.c b/gio/gdbusaddress.c index f873be282..48c766682 100644 --- a/gio/gdbusaddress.c +++ b/gio/gdbusaddress.c @@ -1343,31 +1343,9 @@ g_dbus_address_get_for_bus_sync (GBusType bus_type, case G_BUS_TYPE_SESSION: if (has_elevated_privileges) - { -#ifdef G_OS_UNIX - if (geteuid () == getuid ()) - { - /* Ideally we shouldn't do this, because setgid and - * filesystem capabilities are also elevated privileges - * with which we should not be trusting environment variables - * from the caller. Unfortunately, there are programs with - * elevated privileges that rely on the session bus being - * available. We already prevent the really dangerous - * transports like autolaunch: and unixexec: when our - * privileges are elevated, so this can only make us connect - * to the wrong AF_UNIX or TCP socket. */ - ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS")); - } - else -#endif - { - ret = NULL; - } - } + ret = NULL; else - { - ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS")); - } + ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS")); if (ret == NULL) { diff --git a/gio/gdbusnamewatching.c b/gio/gdbusnamewatching.c index 8daa69cd9..d4272e4ca 100644 --- a/gio/gdbusnamewatching.c +++ b/gio/gdbusnamewatching.c @@ -90,6 +90,13 @@ client_ref (Client *client) return client; } +static gboolean +free_user_data_cb (gpointer user_data) +{ + /* The user data is actually freed by the GDestroyNotify for the idle source */ + return G_SOURCE_REMOVE; +} + static void client_unref (Client *client) { @@ -105,9 +112,26 @@ client_unref (Client *client) } g_free (client->name); g_free (client->name_owner); - g_main_context_unref (client->main_context); + if (client->user_data_free_func != NULL) - client->user_data_free_func (client->user_data); + { + /* Ensure client->user_data_free_func() is called from the right thread */ + if (client->main_context != g_main_context_get_thread_default ()) + { + GSource *idle_source = g_idle_source_new (); + g_source_set_callback (idle_source, free_user_data_cb, + client->user_data, + client->user_data_free_func); + g_source_set_name (idle_source, "[gio, gdbusnamewatching.c] free_user_data_cb"); + g_source_attach (idle_source, client->main_context); + g_source_unref (idle_source); + } + else + client->user_data_free_func (client->user_data); + } + + g_main_context_unref (client->main_context); + g_free (client); } } diff --git a/gio/giomodule.c b/gio/giomodule.c index dfd895717..d34037a45 100644 --- a/gio/giomodule.c +++ b/gio/giomodule.c @@ -50,6 +50,7 @@ #include "gmemorymonitordbus.h" #include "gpowerprofilemonitor.h" #include "gpowerprofilemonitordbus.h" +#include "gpowerprofilemonitorportal.h" #ifdef G_OS_WIN32 #include "gregistrysettingsbackend.h" #include "giowin32-priv.h" @@ -1305,6 +1306,7 @@ _g_io_modules_ensure_loaded (void) g_type_ensure (g_memory_monitor_dbus_get_type ()); g_type_ensure (g_memory_monitor_portal_get_type ()); g_type_ensure (g_network_monitor_portal_get_type ()); + g_type_ensure (g_power_profile_monitor_portal_get_type ()); g_type_ensure (g_proxy_resolver_portal_get_type ()); #endif #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 diff --git a/gio/giotypes.h b/gio/giotypes.h index 995c9cb15..da6a10a4b 100644 --- a/gio/giotypes.h +++ b/gio/giotypes.h @@ -271,6 +271,10 @@ typedef struct _GVolumeMonitor GVolumeMonitor; * where the #GTask was created. All other users of * #GAsyncReadyCallback must likewise call it asynchronously in a * later iteration of the main context. + * + * The asynchronous operation is guaranteed to have held a reference to + * @source_object from the time when the `*_async()` function was called, until + * after this callback returns. **/ typedef void (*GAsyncReadyCallback) (GObject *source_object, GAsyncResult *res, diff --git a/gio/gpowerprofilemonitorportal.c b/gio/gpowerprofilemonitorportal.c new file mode 100644 index 000000000..bb1b4fd15 --- /dev/null +++ b/gio/gpowerprofilemonitorportal.c @@ -0,0 +1,182 @@ +/* GIO - GLib Input, Output and Streaming Library + * + * Copyright 2021 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * 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 "config.h" + +#include "gpowerprofilemonitor.h" +#include "gpowerprofilemonitorportal.h" +#include "gdbuserror.h" +#include "gdbusproxy.h" +#include "ginitable.h" +#include "gioerror.h" +#include "giomodule-priv.h" +#include "gportalsupport.h" + +#define G_POWER_PROFILE_MONITOR_PORTAL_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable)) + +static void g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *iface); +static void g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface); + +typedef enum +{ + PROP_POWER_SAVER_ENABLED = 1, +} GPowerProfileMonitorPortalProperty; + +struct _GPowerProfileMonitorPortal +{ + GObject parent_instance; + + GDBusProxy *proxy; + gulong signal_id; + gboolean power_saver_enabled; +}; + +G_DEFINE_TYPE_WITH_CODE (GPowerProfileMonitorPortal, g_power_profile_monitor_portal, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, + g_power_profile_monitor_portal_initable_iface_init) + G_IMPLEMENT_INTERFACE (G_TYPE_POWER_PROFILE_MONITOR, + g_power_profile_monitor_portal_iface_init) + _g_io_modules_ensure_extension_points_registered (); + g_io_extension_point_implement (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME, + g_define_type_id, + "portal", + 40)) + +static void +g_power_profile_monitor_portal_init (GPowerProfileMonitorPortal *portal) +{ +} + +static void +proxy_properties_changed (GDBusProxy *proxy, + GVariant *changed_properties, + GStrv invalidated_properties, + gpointer user_data) +{ + GPowerProfileMonitorPortal *ppm = user_data; + gboolean power_saver_enabled; + + if (!g_variant_lookup (changed_properties, "power-saver-enabled", "b", &power_saver_enabled)) + return; + + if (power_saver_enabled == ppm->power_saver_enabled) + return; + + ppm->power_saver_enabled = power_saver_enabled; + g_object_notify (G_OBJECT (ppm), "power-saver-enabled"); +} + +static void +g_power_profile_monitor_portal_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object); + + switch ((GPowerProfileMonitorPortalProperty) prop_id) + { + case PROP_POWER_SAVER_ENABLED: + g_value_set_boolean (value, ppm->power_saver_enabled); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static gboolean +g_power_profile_monitor_portal_initable_init (GInitable *initable, + GCancellable *cancellable, + GError **error) +{ + GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (initable); + GDBusProxy *proxy; + gchar *name_owner; + + if (!glib_should_use_portal ()) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals"); + return FALSE; + } + + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.portal.Desktop", + "/org/freedesktop/portal/desktop", + "org.freedesktop.portal.PowerProfileMonitor", + cancellable, + error); + if (!proxy) + return FALSE; + + name_owner = g_dbus_proxy_get_name_owner (proxy); + + if (name_owner == NULL) + { + g_object_unref (proxy); + g_set_error (error, + G_DBUS_ERROR, + G_DBUS_ERROR_NAME_HAS_NO_OWNER, + "Desktop portal not found"); + return FALSE; + } + + g_free (name_owner); + + ppm->signal_id = g_signal_connect (proxy, "g-properties-changed", + G_CALLBACK (proxy_properties_changed), ppm); + + ppm->proxy = g_steal_pointer (&proxy); + + return TRUE; +} + +static void +g_power_profile_monitor_portal_finalize (GObject *object) +{ + GPowerProfileMonitorPortal *ppm = G_POWER_PROFILE_MONITOR_PORTAL (object); + + g_clear_signal_handler (&ppm->signal_id, ppm->proxy); + g_clear_object (&ppm->proxy); + + G_OBJECT_CLASS (g_power_profile_monitor_portal_parent_class)->finalize (object); +} + +static void +g_power_profile_monitor_portal_class_init (GPowerProfileMonitorPortalClass *nl_class) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class); + + gobject_class->get_property = g_power_profile_monitor_portal_get_property; + gobject_class->finalize = g_power_profile_monitor_portal_finalize; + + g_object_class_override_property (gobject_class, PROP_POWER_SAVER_ENABLED, "power-saver-enabled"); +} + +static void +g_power_profile_monitor_portal_iface_init (GPowerProfileMonitorInterface *monitor_iface) +{ +} + +static void +g_power_profile_monitor_portal_initable_iface_init (GInitableIface *iface) +{ + iface->init = g_power_profile_monitor_portal_initable_init; +} diff --git a/gio/gpowerprofilemonitorportal.h b/gio/gpowerprofilemonitorportal.h new file mode 100644 index 000000000..b91a14610 --- /dev/null +++ b/gio/gpowerprofilemonitorportal.h @@ -0,0 +1,31 @@ +/* GIO - GLib Input, Output and Streaming Library + * + * Copyright 2021 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * 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/>. + */ + +#ifndef __G_POWER_PROFILE_MONITOR_PORTAL_H__ +#define __G_POWER_PROFILE_MONITOR_PORTAL_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define G_TYPE_POWER_PROFILE_MONITOR_PORTAL (g_power_profile_monitor_portal_get_type ()) +G_DECLARE_FINAL_TYPE (GPowerProfileMonitorPortal, g_power_profile_monitor_portal, G, POWER_PROFILE_MONITOR_PORTAL, GObject) + +G_END_DECLS + +#endif /* __G_POWER_PROFILE_MONITOR_PORTAL_H__ */ diff --git a/gio/gtlscertificate.c b/gio/gtlscertificate.c index 308a0a7ed..2c238120c 100644 --- a/gio/gtlscertificate.c +++ b/gio/gtlscertificate.c @@ -226,6 +226,18 @@ g_tls_certificate_class_init (GTlsCertificateClass *class) * self-signed, or else the certificate of the issuer is not * available. * + * Beware the issuer certificate may not be the same as the + * certificate that would actually be used to construct a valid + * certification path during certificate verification. + * [RFC 4158](https://datatracker.ietf.org/doc/html/rfc4158) explains + * why an issuer certificate cannot be naively assumed to be part of the + * the certification path (though GLib's TLS backends may not follow the + * path building strategies outlined in this RFC). Due to the complexity + * of certification path building, GLib does not provide any way to know + * which certification path will actually be used. Accordingly, this + * property cannot be used to make security-related decisions. Only + * GLib itself should make security decisions about TLS certificates. + * * Since: 2.28 */ g_object_class_install_property (gobject_class, PROP_ISSUER, @@ -950,6 +962,13 @@ g_tls_certificate_get_issuer (GTlsCertificate *cert) * (All other #GTlsCertificateFlags values will always be set or unset * as appropriate.) * + * Because TLS session context is not used, #GTlsCertificate may not + * perform as many checks on the certificates as #GTlsConnection would. + * For example, certificate constraints cannot be honored, and some + * revocation checks cannot be performed. The best way to verify TLS + * certificates used by a TLS connection is to let #GTlsConnection + * handle the verification. + * * Returns: the appropriate #GTlsCertificateFlags * * Since: 2.28 diff --git a/gio/gtlsdatabase.c b/gio/gtlsdatabase.c index d7dcf4bbe..2e5a264e9 100644 --- a/gio/gtlsdatabase.c +++ b/gio/gtlsdatabase.c @@ -462,15 +462,11 @@ g_tls_database_class_init (GTlsDatabaseClass *klass) * @cancellable: (nullable): a #GCancellable, or %NULL * @error: (nullable): a #GError, or %NULL * - * Determines the validity of a certificate chain after looking up and - * adding any missing certificates to the chain. + * Determines the validity of a certificate chain, outside the context + * of a TLS session. * * @chain is a chain of #GTlsCertificate objects each pointing to the next - * certificate in the chain by its #GTlsCertificate:issuer property. The chain may initially - * consist of one or more certificates. After the verification process is - * complete, @chain may be modified by adding missing certificates, or removing - * extra certificates. If a certificate anchor was found, then it is added to - * the @chain. + * certificate in the chain by its #GTlsCertificate:issuer property. * * @purpose describes the purpose (or usage) for which the certificate * is being used. Typically @purpose will be set to #G_TLS_DATABASE_PURPOSE_AUTHENTICATE_SERVER @@ -497,8 +493,27 @@ g_tls_database_class_init (GTlsDatabaseClass *klass) * accordingly. @error is not set when @chain is successfully analyzed * but found to be invalid. * - * This function can block, use g_tls_database_verify_chain_async() to perform - * the verification operation asynchronously. + * Prior to GLib 2.48, GLib's default TLS backend modified @chain to + * represent the certification path built by #GTlsDatabase during + * certificate verification by adjusting the #GTlsCertificate:issuer + * property of each certificate in @chain. Since GLib 2.48, this no + * longer occurs, so you cannot rely on #GTlsCertificate:issuer to + * represent the actual certification path used during certificate + * verification. + * + * Because TLS session context is not used, #GTlsDatabase may not + * perform as many checks on the certificates as #GTlsConnection would. + * For example, certificate constraints cannot be honored, and some + * revocation checks cannot be performed. The best way to verify TLS + * certificates used by a TLS connection is to let #GTlsConnection + * handle the verification. + * + * The TLS backend may attempt to look up and add missing certificates + * to the chain. Since GLib 2.70, this may involve HTTP requests to + * download missing certificates. + * + * This function can block. Use g_tls_database_verify_chain_async() to + * perform the verification operation asynchronously. * * Returns: the appropriate #GTlsCertificateFlags which represents the * result of verification. @@ -783,14 +798,26 @@ g_tls_database_lookup_certificate_for_handle_finish (GTlsDatabase *se * @cancellable: (nullable): a #GCancellable, or %NULL * @error: (nullable): a #GError, or %NULL * - * Look up the issuer of @certificate in the database. - * - * The #GTlsCertificate:issuer property - * of @certificate is not modified, and the two certificates are not hooked - * into a chain. - * - * This function can block, use g_tls_database_lookup_certificate_issuer_async() to perform - * the lookup operation asynchronously. + * Look up the issuer of @certificate in the database. The + * #GTlsCertificate:issuer property of @certificate is not modified, and + * the two certificates are not hooked into a chain. + * + * This function can block. Use g_tls_database_lookup_certificate_issuer_async() + * to perform the lookup operation asynchronously. + * + * Beware this function cannot be used to build certification paths. The + * issuer certificate returned by this function may not be the same as + * the certificate that would actually be used to construct a valid + * certification path during certificate verification. + * [RFC 4158](https://datatracker.ietf.org/doc/html/rfc4158) explains + * why an issuer certificate cannot be naively assumed to be part of the + * the certification path (though GLib's TLS backends may not follow the + * path building strategies outlined in this RFC). Due to the complexity + * of certification path building, GLib does not provide any way to know + * which certification path will actually be used when verifying a TLS + * certificate. Accordingly, this function cannot be used to make + * security-related decisions. Only GLib itself should make security + * decisions about TLS certificates. * * Returns: (transfer full): a newly allocated issuer #GTlsCertificate, * or %NULL. Use g_object_unref() to release the certificate. diff --git a/gio/gwin32packageparser.c b/gio/gwin32packageparser.c index ad5302270..ee05bb1dd 100755 --- a/gio/gwin32packageparser.c +++ b/gio/gwin32packageparser.c @@ -390,7 +390,7 @@ g_win32_package_parser_enum_packages (GWin32PackageParserCallback callback, wcs_path = LoadedWindowsGetStringRawBuffer (path, NULL); manifest_filename_size = wcslen (wcs_path) + wcslen (bslash_appmanifest); manifest_filename = g_new (wchar_t, manifest_filename_size + 1); - memcpy (manifest_filename, wcs_path, manifest_filename_size * sizeof (wchar_t)); + memcpy (manifest_filename, wcs_path, wcslen (wcs_path) * sizeof (wchar_t)); memcpy (&manifest_filename[wcslen (wcs_path)], bslash_appmanifest, (wcslen (bslash_appmanifest) + 1) * sizeof (wchar_t)); memset (sax, 0, sizeof (*sax)); diff --git a/gio/meson.build b/gio/meson.build index d5838ed8a..ac3373f2b 100644 --- a/gio/meson.build +++ b/gio/meson.build @@ -383,6 +383,7 @@ if host_system != 'windows' 'gopenuriportal.c', 'gmemorymonitorportal.c', 'gnetworkmonitorportal.c', + 'gpowerprofilemonitorportal.c', 'gproxyresolverportal.c', 'gtrashportal.c', 'gportalsupport.c', diff --git a/gio/tests/desktop-app-info.c b/gio/tests/desktop-app-info.c index fcc29c579..15dcd8f1c 100644 --- a/gio/tests/desktop-app-info.c +++ b/gio/tests/desktop-app-info.c @@ -482,8 +482,7 @@ assert_strings_equivalent (const gchar *expected, if (g_str_equal (expected_words[i], result_words[j])) goto got_it; - g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result); - g_test_fail (); + g_test_fail_printf ("Unable to find expected string '%s' in result '%s'", expected_words[i], result); got_it: continue; diff --git a/gio/tests/gdbus-names.c b/gio/tests/gdbus-names.c index 8504220a9..838a4ee09 100644 --- a/gio/tests/gdbus-names.c +++ b/gio/tests/gdbus-names.c @@ -25,9 +25,6 @@ #include "gdbus-tests.h" -/* all tests rely on a shared mainloop */ -static GMainLoop *loop; - /* ---------------------------------------------------------------------------------------------------- */ /* Test that g_bus_own_name() works correctly */ /* ---------------------------------------------------------------------------------------------------- */ @@ -39,13 +36,14 @@ typedef struct guint num_acquired; guint num_lost; guint num_free_func; + GMainContext *main_context; /* (unowned) */ } OwnNameData; static void own_name_data_free_func (OwnNameData *data) { data->num_free_func++; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -56,7 +54,7 @@ bus_acquired_handler (GDBusConnection *connection, OwnNameData *data = user_data; g_dbus_connection_set_exit_on_close (connection, FALSE); data->num_bus_acquired += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -66,7 +64,7 @@ name_acquired_handler (GDBusConnection *connection, { OwnNameData *data = user_data; data->num_acquired += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -85,7 +83,7 @@ name_lost_handler (GDBusConnection *connection, g_dbus_connection_set_exit_on_close (connection, FALSE); } data->num_lost += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -101,6 +99,7 @@ test_bus_own_name (void) gboolean name_has_owner_reply; GDBusConnection *c2; GVariant *result; + GMainContext *main_context = NULL; /* use the global default for now */ error = NULL; name = "org.gtk.GDBus.Name1"; @@ -115,6 +114,7 @@ test_bus_own_name (void) data.num_acquired = 0; data.num_lost = 0; data.expect_null_connection = TRUE; + data.main_context = main_context; id = g_bus_own_name (G_BUS_TYPE_SESSION, name, G_BUS_NAME_OWNER_FLAGS_NONE, @@ -126,7 +126,10 @@ test_bus_own_name (void) g_assert_cmpint (data.num_bus_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_lost < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 1); @@ -154,11 +157,17 @@ test_bus_own_name (void) g_assert_cmpint (data.num_bus_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_bus_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_lost, ==, 0); @@ -190,7 +199,8 @@ test_bus_own_name (void) * Stop owning the name - this should invoke our free func */ g_bus_unown_name (id); - g_main_loop_run (loop); + while (data.num_free_func < 2) + g_main_context_iteration (main_context, TRUE); g_assert_cmpint (data.num_free_func, ==, 2); /* @@ -235,7 +245,11 @@ test_bus_own_name (void) g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_lost, ==, 0); g_assert_cmpint (data.num_free_func, ==, 2); - g_main_loop_run (loop); /* the GDestroyNotify is called in idle because the bus is acquired in idle */ + + /* the GDestroyNotify is called in idle because the bus is acquired in idle */ + while (data.num_free_func < 3) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_free_func, ==, 3); /* @@ -260,11 +274,17 @@ test_bus_own_name (void) g_assert_cmpint (data.num_bus_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_bus_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_lost, ==, 0); @@ -278,6 +298,7 @@ test_bus_own_name (void) data2.num_acquired = 0; data2.num_lost = 0; data2.expect_null_connection = FALSE; + data2.main_context = main_context; id2 = g_bus_own_name (G_BUS_TYPE_SESSION, name, G_BUS_NAME_OWNER_FLAGS_NONE, @@ -289,16 +310,25 @@ test_bus_own_name (void) g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data2.num_bus_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 1); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data2.num_lost < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 1); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); + g_bus_unown_name (id2); - g_main_loop_run (loop); + while (data2.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 1); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); @@ -328,12 +358,18 @@ test_bus_own_name (void) g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data2.num_lost < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); + g_bus_unown_name (id2); - g_main_loop_run (loop); + while (data2.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); @@ -354,12 +390,18 @@ test_bus_own_name (void) g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data2.num_lost < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); + g_bus_unown_name (id2); - g_main_loop_run (loop); + while (data2.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); @@ -370,7 +412,9 @@ test_bus_own_name (void) */ data.expect_null_connection = FALSE; g_bus_unown_name (id); - g_main_loop_run (loop); + while (data.num_bus_acquired < 1 || data.num_free_func < 4) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_free_func, ==, 4); @@ -390,11 +434,17 @@ test_bus_own_name (void) g_assert_cmpint (data.num_bus_acquired, ==, 0); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_bus_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 0); g_assert_cmpint (data.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data.num_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_bus_acquired, ==, 1); g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_lost, ==, 0); @@ -419,12 +469,18 @@ test_bus_own_name (void) g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0); - g_main_loop_run (loop); + + while (data2.num_lost < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); + g_bus_unown_name (id2); - g_main_loop_run (loop); + while (data2.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_bus_acquired, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 1); @@ -447,18 +503,22 @@ test_bus_own_name (void) g_assert_cmpint (data.num_lost, ==, 0); g_assert_cmpint (data2.num_acquired, ==, 0); g_assert_cmpint (data2.num_lost, ==, 0); + /* wait for handlers for both owner and owner2 to fire */ while (data.num_lost == 0 || data2.num_acquired == 0) - g_main_loop_run (loop); + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_acquired, ==, 1); g_assert_cmpint (data.num_lost, ==, 1); g_assert_cmpint (data2.num_acquired, ==, 1); g_assert_cmpint (data2.num_lost, ==, 0); g_assert_cmpint (data2.num_bus_acquired, ==, 0); + /* ok, make owner2 release the name - then wait for owner to automagically reacquire it */ g_bus_unown_name (id2); - g_main_loop_run (loop); - g_main_loop_run (loop); + while (data.num_acquired < 2 || data2.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data2.num_free_func, ==, 1); g_assert_cmpint (data.num_acquired, ==, 2); g_assert_cmpint (data.num_lost, ==, 1); @@ -470,11 +530,15 @@ test_bus_own_name (void) data.expect_null_connection = TRUE; session_bus_stop (); while (data.num_lost != 2) - g_main_loop_run (loop); + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_acquired, ==, 2); g_assert_cmpint (data.num_lost, ==, 2); + g_bus_unown_name (id); - g_main_loop_run (loop); + while (data.num_free_func < 5) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_free_func, ==, 5); g_object_unref (c); @@ -495,6 +559,7 @@ typedef struct guint num_appeared; guint num_vanished; guint num_free_func; + GMainContext *main_context; /* (unowned), for the main test thread */ } WatchNameData; typedef struct @@ -509,13 +574,14 @@ typedef struct gboolean unwatch_early; GMutex mutex; guint watch_id; + GMainContext *thread_context; /* (unowned), only accessed from watcher_thread() */ } WatchNameThreadData; static void watch_name_data_free_func (WatchNameData *data) { data->num_free_func++; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -532,7 +598,7 @@ w_name_acquired_handler (GDBusConnection *connection, { OwnNameData *data = user_data; data->num_acquired += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -542,7 +608,7 @@ w_name_lost_handler (GDBusConnection *connection, { OwnNameData *data = user_data; data->num_lost += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -563,7 +629,7 @@ name_appeared_handler (GDBusConnection *connection, g_dbus_connection_set_exit_on_close (connection, FALSE); } data->num_appeared += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } static void @@ -583,7 +649,7 @@ name_vanished_handler (GDBusConnection *connection, g_dbus_connection_set_exit_on_close (connection, FALSE); } data->num_vanished += 1; - g_main_loop_quit (loop); + g_main_context_wakeup (data->main_context); } typedef struct @@ -630,6 +696,7 @@ stop_service (GDBusConnection *connection, GError *error = NULL; GDBusProxy *proxy = NULL; GVariant *result = NULL; + GMainContext *main_context = NULL; /* use the global default for now */ data->num_vanished = 0; @@ -655,7 +722,7 @@ stop_service (GDBusConnection *connection, if (result) g_variant_unref (result); while (data->num_vanished == 0) - g_main_loop_run (loop); + g_main_context_iteration (main_context, TRUE); } static void @@ -668,6 +735,7 @@ test_bus_watch_name (gconstpointer d) GDBusConnection *connection; const WatchNameTest *watch_name_test; const gchar *name; + GMainContext *main_context = NULL; /* use the global default for now */ watch_name_test = (WatchNameTest *) d; @@ -689,6 +757,7 @@ test_bus_watch_name (gconstpointer d) data.num_appeared = 0; data.num_vanished = 0; data.expect_null_connection = TRUE; + data.main_context = main_context; id = g_bus_watch_name (G_BUS_TYPE_SESSION, name, watch_name_test->watcher_flags, @@ -698,10 +767,17 @@ test_bus_watch_name (gconstpointer d) (GDestroyNotify) watch_name_data_free_func); g_assert_cmpint (data.num_appeared, ==, 0); g_assert_cmpint (data.num_vanished, ==, 0); - g_main_loop_run (loop); + + while (data.num_vanished < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_appeared, ==, 0); g_assert_cmpint (data.num_vanished, ==, 1); + g_bus_unwatch_name (id); + while (data.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_appeared, ==, 0); g_assert_cmpint (data.num_vanished, ==, 1); g_assert_cmpint (data.num_free_func, ==, 1); @@ -716,6 +792,7 @@ test_bus_watch_name (gconstpointer d) own_data.num_acquired = 0; own_data.num_lost = 0; data.expect_null_connection = FALSE; + own_data.main_context = main_context; owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, name, G_BUS_NAME_OWNER_FLAGS_NONE, @@ -724,7 +801,10 @@ test_bus_watch_name (gconstpointer d) w_name_lost_handler, &own_data, (GDestroyNotify) own_name_data_free_func); - g_main_loop_run (loop); + + while (own_data.num_acquired < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (own_data.num_acquired, ==, 1); g_assert_cmpint (own_data.num_lost, ==, 0); @@ -758,7 +838,10 @@ test_bus_watch_name (gconstpointer d) } g_assert_cmpint (data.num_appeared, ==, 0); g_assert_cmpint (data.num_vanished, ==, 0); - g_main_loop_run (loop); + + while (data.num_appeared < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_appeared, ==, 1); g_assert_cmpint (data.num_vanished, ==, 0); @@ -766,11 +849,16 @@ test_bus_watch_name (gconstpointer d) * Unwatch the name. */ g_bus_unwatch_name (id); + while (data.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_free_func, ==, 1); /* unown the name */ g_bus_unown_name (owner_id); - g_main_loop_run (loop); + while (own_data.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (own_data.num_acquired, ==, 1); g_assert_cmpint (own_data.num_free_func, ==, 1); own_data.num_free_func = 0; @@ -808,7 +896,10 @@ test_bus_watch_name (gconstpointer d) g_assert_cmpint (data.num_appeared, ==, 0); g_assert_cmpint (data.num_vanished, ==, 0); - g_main_loop_run (loop); + + while (data.num_appeared == 0 && data.num_vanished == 0) + g_main_context_iteration (main_context, TRUE); + if (watch_name_test->existing_service) { g_assert_cmpint (data.num_appeared, ==, 1); @@ -826,6 +917,7 @@ test_bus_watch_name (gconstpointer d) own_data.num_acquired = 0; own_data.num_lost = 0; own_data.expect_null_connection = FALSE; + own_data.main_context = main_context; owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, name, G_BUS_NAME_OWNER_FLAGS_NONE, @@ -834,8 +926,10 @@ test_bus_watch_name (gconstpointer d) w_name_lost_handler, &own_data, (GDestroyNotify) own_name_data_free_func); + while (own_data.num_acquired == 0 || data.num_appeared == 0) - g_main_loop_run (loop); + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (own_data.num_acquired, ==, 1); g_assert_cmpint (own_data.num_lost, ==, 0); g_assert_cmpint (data.num_appeared, ==, 1); @@ -855,7 +949,8 @@ test_bus_watch_name (gconstpointer d) session_bus_stop (); if (!watch_name_test->existing_service) { - g_main_loop_run (loop); + while (own_data.num_lost < 1 || data.num_vanished < 2) + g_main_context_iteration (main_context, TRUE); g_assert_cmpint (own_data.num_lost, ==, 1); g_assert_cmpint (data.num_vanished, ==, 2); } @@ -864,13 +959,19 @@ test_bus_watch_name (gconstpointer d) g_assert_cmpint (own_data.num_lost, ==, 0); g_assert_cmpint (data.num_vanished, ==, 1); } + g_bus_unwatch_name (id); + while (data.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (data.num_free_func, ==, 1); if (!watch_name_test->existing_service) { g_bus_unown_name (owner_id); - g_main_loop_run (loop); + while (own_data.num_free_func < 1) + g_main_context_iteration (main_context, TRUE); + g_assert_cmpint (own_data.num_free_func, ==, 1); } session_bus_down (); @@ -883,6 +984,9 @@ static void t_watch_name_data_free_func (WatchNameThreadData *thread_data) { thread_data->data.num_free_func++; + + g_assert_true (g_main_context_is_owner (thread_data->thread_context)); + g_main_context_wakeup (thread_data->thread_context); } /* Called in the same thread as watcher_thread() */ @@ -894,6 +998,9 @@ t_name_appeared_handler (GDBusConnection *connection, { WatchNameThreadData *thread_data = user_data; thread_data->data.num_appeared += 1; + + g_assert_true (g_main_context_is_owner (thread_data->thread_context)); + g_main_context_wakeup (thread_data->thread_context); } /* Called in the same thread as watcher_thread() */ @@ -904,6 +1011,9 @@ t_name_vanished_handler (GDBusConnection *connection, { WatchNameThreadData *thread_data = user_data; thread_data->data.num_vanished += 1; + + g_assert_true (g_main_context_is_owner (thread_data->thread_context)); + g_main_context_wakeup (thread_data->thread_context); } /* Called in the thread which constructed the GDBusConnection */ @@ -931,6 +1041,7 @@ watcher_thread (gpointer user_data) GMainContext *thread_context; thread_context = g_main_context_new (); + thread_data->thread_context = thread_context; g_main_context_push_thread_default (thread_context); // Notify that the thread has started @@ -1037,6 +1148,7 @@ watch_with_different_context (gboolean unwatch_early) GDBusConnection *connection; GThread *watcher; guint id; + GMainContext *main_context = NULL; /* use the global default for now */ session_bus_up (); @@ -1063,6 +1175,7 @@ watch_with_different_context (gboolean unwatch_early) own_data.num_lost = 0; own_data.num_free_func = 0; own_data.expect_null_connection = FALSE; + own_data.main_context = main_context; // Own the name to avoid direct name vanished in watcher thread id = g_bus_own_name_on_connection (connection, "org.gtk.GDBus.Name1", @@ -1072,7 +1185,7 @@ watch_with_different_context (gboolean unwatch_early) &own_data, (GDestroyNotify) own_name_data_free_func); while (own_data.num_acquired == 0) - g_main_context_iteration (NULL, TRUE); + g_main_context_iteration (main_context, TRUE); g_assert_cmpint (own_data.num_acquired, ==, 1); g_assert_cmpint (own_data.num_lost, ==, 0); @@ -1084,13 +1197,13 @@ watch_with_different_context (gboolean unwatch_early) // Iterate the loop until thread is waking us up while (!thread_data.ended) - g_main_context_iteration (NULL, TRUE); + g_main_context_iteration (main_context, TRUE); g_thread_join (watcher); g_bus_unown_name (id); while (own_data.num_free_func == 0) - g_main_context_iteration (NULL, TRUE); + g_main_context_iteration (main_context, TRUE); g_assert_cmpint (own_data.num_free_func, ==, 1); g_mutex_clear (&thread_data.mutex); @@ -1227,8 +1340,6 @@ main (int argc, g_test_init (&argc, &argv, NULL); - loop = g_main_loop_new (NULL, FALSE); - g_test_dbus_unset (); g_test_add_func ("/gdbus/validate-names", test_validate_names); @@ -1253,7 +1364,5 @@ main (int argc, g_test_add_func ("/gdbus/escape-object-path", test_escape_object_path); ret = g_test_run(); - g_main_loop_unref (loop); - return ret; } diff --git a/gio/tests/memory-monitor-dbus.py.in b/gio/tests/memory-monitor-dbus.py.in index 7823e7309..e8ac28faf 100755 --- a/gio/tests/memory-monitor-dbus.py.in +++ b/gio/tests/memory-monitor-dbus.py.in @@ -66,6 +66,23 @@ try: self.p_mock.terminate() self.p_mock.wait() + def assertEventually(self, condition, message=None, timeout=50): + '''Assert that condition function eventually returns True. + + Timeout is in deciseconds, defaulting to 50 (5 seconds). message is + printed on failure. + ''' + while timeout >= 0: + context = GLib.MainContext.default() + while context.iteration(False): + pass + if condition(): + break + timeout -= 1 + time.sleep(0.1) + else: + self.fail(message or 'timed out waiting for ' + str(condition)) + def memory_warning_cb(self, monitor, level): self.last_warning = level self.main_context.wakeup() @@ -82,21 +99,11 @@ try: self.dbusmock.EmitWarning(100) # Wait 2 seconds or until warning - timeout = 2 - while timeout > 0 and self.last_warning != 100: - time.sleep(0.5) - timeout -= 0.5 - self.main_context.iteration(False) - self.assertEqual(self.last_warning, 100) + self.assertEventually(self.last_warning == 100, "'100' low-memory warning not received", 20) self.dbusmock.EmitWarning(255) # Wait 2 seconds or until warning - timeout = 2 - while timeout > 0 and self.last_warning != 255: - time.sleep(0.5) - timeout -= 0.5 - self.main_context.iteration(False) - self.assertEqual(self.last_warning, 255) + self.assertEventually(self.last_warning == 255, "'255' low-memory warning not received", 20) except ImportError as e: @unittest.skip("Cannot import %s" % e.name) diff --git a/gio/tests/memory-monitor-portal.py.in b/gio/tests/memory-monitor-portal.py.in index cb4a960eb..36d5094d3 100755 --- a/gio/tests/memory-monitor-portal.py.in +++ b/gio/tests/memory-monitor-portal.py.in @@ -31,7 +31,6 @@ try: dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) - # XDG_DESKTOP_PORTAL_PATH = os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal") XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal" class TestLowMemoryMonitorPortal(dbusmock.DBusTestCase): @@ -85,6 +84,23 @@ try: self.p_mock.terminate() self.p_mock.wait() + def assertEventually(self, condition, message=None, timeout=50): + '''Assert that condition function eventually returns True. + + Timeout is in deciseconds, defaulting to 50 (5 seconds). message is + printed on failure. + ''' + while timeout >= 0: + context = GLib.MainContext.default() + while context.iteration(False): + pass + if condition(): + break + timeout -= 1 + time.sleep(0.1) + else: + self.fail(message or 'timed out waiting for ' + str(condition)) + def portal_memory_warning_cb(self, monitor, level): self.last_warning = level self.main_context.wakeup() @@ -101,21 +117,11 @@ try: self.dbusmock.EmitWarning(100) # Wait 2 seconds or until warning - timeout = 2 - while timeout > 0 and self.last_warning != 100: - time.sleep(0.5) - timeout -= 0.5 - self.main_context.iteration(False) - self.assertEqual(self.last_warning, 100) + self.assertEventually(self.last_warning == 100, "'100' low-memory warning not received", 20) self.dbusmock.EmitWarning(255) # Wait 2 seconds or until warning - timeout = 2 - while timeout > 0 and self.last_warning != 255: - time.sleep(0.5) - timeout -= 0.5 - self.main_context.iteration(False) - self.assertEqual(self.last_warning, 255) + self.assertEventually(self.last_warning == 255, "'255' low-memory warning not received", 20) except ImportError as e: @unittest.skip("Cannot import %s" % e.name) diff --git a/gio/tests/meson.build b/gio/tests/meson.build index fc2055101..5dbfb8e60 100644 --- a/gio/tests/meson.build +++ b/gio/tests/meson.build @@ -541,27 +541,29 @@ if installed_tests_enabled install_subdir('static-link', install_dir : installed_tests_execdir) install_data('static-link.py', install_dir : installed_tests_execdir) - memory_monitor_tests = [ + monitor_tests = [ 'memory-monitor-dbus', 'memory-monitor-portal', + 'power-profile-monitor-dbus', + 'power-profile-monitor-portal' ] - foreach memory_monitor_test : memory_monitor_tests + foreach monitor_test : monitor_tests cdata = configuration_data() cdata.set('installed_tests_dir', installed_tests_execdir) - cdata.set('program', memory_monitor_test + '.py') + cdata.set('program', monitor_test + '.py') cdata.set('env', '') configure_file( input: installed_tests_template_tap, - output: memory_monitor_test + '.test', + output: monitor_test + '.test', install_dir: installed_tests_metadir, configuration: cdata ) cdata = configuration_data() cdata.set('libexecdir', join_paths(glib_prefix, get_option('libexecdir'))) configure_file( - input: memory_monitor_test + '.py.in', - output: memory_monitor_test + '.py', + input: monitor_test + '.py.in', + output: monitor_test + '.py', install_dir : installed_tests_execdir, configuration: cdata, ) diff --git a/gio/tests/power-profile-monitor-dbus.py.in b/gio/tests/power-profile-monitor-dbus.py.in new file mode 100755 index 000000000..06e594f4a --- /dev/null +++ b/gio/tests/power-profile-monitor-dbus.py.in @@ -0,0 +1,107 @@ +#!/usr/bin/python3 + +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 3 of the License, or (at your option) any +# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text +# of the license. + +__author__ = 'Bastien Nocera' +__email__ = 'hadess@hadess.net' +__copyright__ = '(c) 2019, 2021 Red Hat Inc.' +__license__ = 'LGPL 3+' + +import unittest +import sys +import subprocess +import fcntl +import os +import time + +import taptestrunner + +try: + # Do all non-standard imports here so we can skip the tests if any + # needed packages are not available. + import dbus + import dbus.mainloop.glib + import dbusmock + from gi.repository import GLib + from gi.repository import Gio + + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + class TestPowerProfileMonitor(dbusmock.DBusTestCase): + '''Test GPowerProfileMonitorDBus''' + + @classmethod + def setUpClass(klass): + klass.start_system_bus() + klass.dbus_con = klass.get_dbus(True) + + def setUp(self): + try: + Gio.PowerProfileMonitor + except AttributeError: + raise unittest.SkipTest('Power Profile Monitor not in ' + 'introspection data. Requires ' + 'GObject-Introspection ≥ 1.63.2') # FIXME version + try: + (self.p_mock, self.obj_ppd) = self.spawn_server_template( + 'power_profiles_daemon', {}, stdout=subprocess.PIPE) + except ModuleNotFoundError: + raise unittest.SkipTest("power-profiles-daemon dbusmock template not " + "found. Requires dbusmock > 0.23.1.") # FIXME version + # set log to nonblocking + flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL) + fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) + self.power_saver_enabled = False + self.dbus_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE) + self.power_profile_monitor = Gio.PowerProfileMonitor.dup_default() + self.power_profile_monitor.connect("notify::power-saver-enabled", self.power_saver_enabled_cb) + self.mainloop = GLib.MainLoop() + self.main_context = self.mainloop.get_context() + + def tearDown(self): + self.p_mock.terminate() + self.p_mock.wait() + + def assertEventually(self, condition, message=None, timeout=50): + '''Assert that condition function eventually returns True. + + Timeout is in deciseconds, defaulting to 50 (5 seconds). message is + printed on failure. + ''' + while timeout >= 0: + context = GLib.MainContext.default() + while context.iteration(False): + pass + if condition(): + break + timeout -= 1 + time.sleep(0.1) + else: + self.fail(message or 'timed out waiting for ' + str(condition)) + + def power_saver_enabled_cb(self, spec, data): + self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled() + self.main_context.wakeup() + + def test_power_profile_power_saver_enabled(self): + '''power-saver-enabled property''' + + self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False) + self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1)) + self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10) + + self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1)) + self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10) + +except ImportError as e: + @unittest.skip("Cannot import %s" % e.name) + class TestPowerProfileMonitor(unittest.TestCase): + def test_power_profile_power_saver_enabled(self): + pass + +if __name__ == '__main__': + unittest.main(testRunner=taptestrunner.TAPTestRunner()) diff --git a/gio/tests/power-profile-monitor-portal.py.in b/gio/tests/power-profile-monitor-portal.py.in new file mode 100755 index 000000000..960a62232 --- /dev/null +++ b/gio/tests/power-profile-monitor-portal.py.in @@ -0,0 +1,126 @@ +#!/usr/bin/python3 + +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 3 of the License, or (at your option) any +# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text +# of the license. + +__author__ = 'Bastien Nocera' +__email__ = 'hadess@hadess.net' +__copyright__ = '(c) 2021 Red Hat Inc.' +__license__ = 'LGPL 3+' + +import unittest +import sys +import subprocess +import fcntl +import os +import time + +import taptestrunner + +try: + # Do all non-standard imports here so we can skip the tests if any + # needed packages are not available. + import dbus + import dbus.mainloop.glib + import dbusmock + from gi.repository import GLib + from gi.repository import Gio + + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + # XDG_DESKTOP_PORTAL_PATH = os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal") + XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal" + + class TestPowerProfileMonitorPortal(dbusmock.DBusTestCase): + '''Test GPowerProfileMonitorPortal''' + + @classmethod + def setUpClass(klass): + klass.start_system_bus() + klass.dbus_con = klass.get_dbus(True) + # Start session bus so that xdg-desktop-portal can run on it + klass.start_session_bus() + + def setUp(self): + try: + Gio.PowerProfileMonitor + except AttributeError: + raise unittest.SkipTest('Power Profile Monitor not in ' + 'introspection data. Requires ' + 'GObject-Introspection > 1.69.0') + try: + (self.p_mock, self.obj_ppd) = self.spawn_server_template( + 'power_profiles_daemon', {}, stdout=subprocess.PIPE) + except ModuleNotFoundError: + raise unittest.SkipTest("power-profiles-daemon dbusmock template not " + "found. Requires dbusmock > 0.23.1.") + # set log to nonblocking + flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL) + fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) + self.power_saver_enabled = False + self.dbus_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE) + try: + self.xdp = subprocess.Popen([XDG_DESKTOP_PORTAL_PATH]) + except FileNotFoundError: + raise unittest.SkipTest("xdg-desktop-portal not available") + + try: + self.wait_for_bus_object('org.freedesktop.portal.Desktop', + '/org/freedesktop/portal/desktop') + except: + raise + # subprocess.Popen(['gdbus', 'monitor', '--session', '--dest', 'org.freedesktop.portal.Desktop']) + + os.environ['GTK_USE_PORTAL'] = "1" + self.power_profile_monitor = Gio.PowerProfileMonitor.dup_default() + assert("GPowerProfileMonitorPortal" in str(self.power_profile_monitor)) + self.power_profile_monitor.connect("notify::power-saver-enabled", self.power_saver_enabled_cb) + self.mainloop = GLib.MainLoop() + self.main_context = self.mainloop.get_context() + + def tearDown(self): + self.p_mock.terminate() + self.p_mock.wait() + + def assertEventually(self, condition, message=None, timeout=50): + '''Assert that condition function eventually returns True. + + Timeout is in deciseconds, defaulting to 50 (5 seconds). message is + printed on failure. + ''' + while timeout >= 0: + context = GLib.MainContext.default() + while context.iteration(False): + pass + if condition(): + break + timeout -= 1 + time.sleep(0.1) + else: + self.fail(message or 'timed out waiting for ' + str(condition)) + + def power_saver_enabled_cb(self, spec, data): + self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled() + self.main_context.wakeup() + + def test_power_profile_power_saver_enabled_portal(self): + '''power-saver-enabled property''' + + self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False) + self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1)) + self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10) + + self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1)) + self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10) + +except ImportError as e: + @unittest.skip("Cannot import %s" % e.name) + class TestPowerProfileMonitorPortal(unittest.TestCase): + def test_power_profile_power_saver_enabled_portal(self): + pass + +if __name__ == '__main__': + unittest.main(testRunner=taptestrunner.TAPTestRunner()) diff --git a/gio/tests/socket.c b/gio/tests/socket.c index fa96a4087..ee38fabcd 100644 --- a/gio/tests/socket.c +++ b/gio/tests/socket.c @@ -341,9 +341,7 @@ test_ip_async (GSocketFamily family) data = create_server (family, echo_server_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -454,9 +452,7 @@ test_ip_sync (GSocketFamily family) data = create_server (family, echo_server_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -594,9 +590,7 @@ test_ip_sync_dgram (GSocketFamily family) echo_server_dgram_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -974,9 +968,7 @@ test_close_graceful (void) data = create_server (family, graceful_server_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -1084,9 +1076,7 @@ test_ipv6_v4mapped (void) data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -1145,9 +1135,7 @@ test_timed_wait (void) data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -1232,9 +1220,7 @@ test_fd_reuse (void) data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -1938,9 +1924,7 @@ test_credentials_tcp_client (void) data = create_server (family, echo_server_thread, FALSE, &error); if (error != NULL) { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); g_clear_error (&error); return; } @@ -2045,13 +2029,9 @@ test_credentials_tcp_server (void) goto beach; skip: - { - gchar *message = g_strdup_printf ("Failed to create server: %s", error->message); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Failed to create server: %s", error->message); + goto beach; - goto beach; - } beach: { g_clear_error (&error); diff --git a/gio/tests/trash.c b/gio/tests/trash.c index 2d3ec6774..b5fa4be92 100644 --- a/gio/tests/trash.c +++ b/gio/tests/trash.c @@ -111,11 +111,7 @@ test_trash_symlinks (void) if (!g_file_test (target, G_FILE_TEST_IS_DIR)) { - gchar *message; - - message = g_strdup_printf ("Directory '%s' does not exist", target); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Directory '%s' does not exist", target); g_free (target); return; } @@ -124,12 +120,7 @@ test_trash_symlinks (void) if (target_mount == NULL) { - gchar *message; - - message = g_strdup_printf ("Unable to determine mount point for %s", - target); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Unable to determine mount point for %s", target); g_free (target); return; } @@ -144,11 +135,7 @@ test_trash_symlinks (void) if (tmp_mount == NULL) { - gchar *message; - - message = g_strdup_printf ("Unable to determine mount point for %s", tmp); - g_test_skip (message); - g_free (message); + g_test_skip_printf ("Unable to determine mount point for %s", tmp); g_unix_mount_free (target_mount); g_free (target); g_free (tmp); |