diff options
author | Thomas Haller <thaller@redhat.com> | 2019-11-02 14:20:43 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-11-07 11:34:36 +0100 |
commit | f21b8781ed7ab3da080ab5736c77cfff6a9c7328 (patch) | |
tree | 02c571ae6fa2a4401949a73a981e925abca20c99 | |
parent | 8def37dd65ae9dfec8d3faca36665c92ea91828f (diff) | |
download | NetworkManager-f21b8781ed7ab3da080ab5736c77cfff6a9c7328.tar.gz |
tests: use nmtstc_client_new() to create NMClient instance and cleanup tests
The advantage of nmtstc_client_new() is that it randomly either uses the
synchronous or asynchronous constructor. Of course, both should behave
pretty much the same. Hence, this increases our test coverage.
-rw-r--r-- | libnm/tests/test-nm-client.c | 255 | ||||
-rw-r--r-- | libnm/tests/test-remote-settings-client.c | 207 | ||||
-rw-r--r-- | libnm/tests/test-secret-agent.c | 5 |
3 files changed, 159 insertions, 308 deletions
diff --git a/libnm/tests/test-nm-client.c b/libnm/tests/test-nm-client.c index f97aa56077..987b63ed90 100644 --- a/libnm/tests/test-nm-client.c +++ b/libnm/tests/test-nm-client.c @@ -10,8 +10,9 @@ #include "nm-test-libnm-utils.h" -static GMainLoop *loop = NULL; -static NMTstcServiceInfo *sinfo; +static struct { + GMainLoop *loop; +} gl = { }; /*****************************************************************************/ @@ -24,68 +25,6 @@ loop_quit (gpointer user_data) /*****************************************************************************/ -static NMClient * -_nm_client_new_sync (void) -{ - NMClient *client; - guint source_1; - GError *error = NULL; - - source_1 = g_idle_add (nmtst_g_source_assert_not_called, NULL); - - client = g_object_new (NM_TYPE_CLIENT, NULL); - g_assert (NM_IS_CLIENT (client)); - - if (!g_initable_init (G_INITABLE (client), NULL, &error)) - g_assert_not_reached (); - - g_assert_no_error (error); - - nm_clear_g_source (&source_1); - return client; -} - -typedef struct { - GMainLoop *loop; - NMClient *client; -} NewSyncInsideDispatchedData; - -static gboolean -_nm_client_new_sync_inside_dispatched_do (gpointer user_data) -{ - NewSyncInsideDispatchedData *d = user_data; - - g_assert (d->loop); - g_assert (!d->client); - - d->client = _nm_client_new_sync (); - - g_main_loop_quit (d->loop); - return G_SOURCE_CONTINUE; -} - -static NMClient * -_nm_client_new_sync_inside_dispatched (void) -{ - NewSyncInsideDispatchedData d = { }; - guint source_1; - - d.loop = g_main_loop_new (NULL, FALSE); - source_1 = g_idle_add (_nm_client_new_sync_inside_dispatched_do, &d); - - g_main_loop_run (d.loop); - - g_assert (NM_IS_CLIENT (d.client)); - - nm_clear_g_source (&source_1); - - g_main_loop_unref (d.loop); - - return d.client; -} - -/*****************************************************************************/ - static void devices_notify_cb (NMClient *c, GParamSpec *pspec, @@ -109,7 +48,8 @@ devices_notify_cb (NMClient *c, static void test_device_added (void) { - NMClient *client; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; const GPtrArray *devices; NMDevice *device; gboolean notified = FALSE; @@ -119,8 +59,7 @@ test_device_added (void) if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); devices = nm_client_get_devices (client); g_assert (devices->len == 0); @@ -151,9 +90,6 @@ test_device_added (void) nm_device_delete (device, NULL, &error); g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_NOT_SOFTWARE); g_clear_error (&error); - - g_object_unref (client); - g_clear_pointer (&sinfo, nmtstc_service_cleanup); } /*****************************************************************************/ @@ -190,6 +126,8 @@ devices_sai_notify_cb (NMClient *c, const GPtrArray *devices; NMDevice *device; + g_assert_cmpstr (pspec->name, ==, "devices"); + devices = nm_client_get_devices (c); g_assert (devices); g_assert_cmpint (devices->len, ==, 1); @@ -205,18 +143,17 @@ devices_sai_notify_cb (NMClient *c, static void test_device_added_signal_after_init (void) { - NMClient *client; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; const GPtrArray *devices; NMDevice *device; guint result = 0; - GError *error = NULL; sinfo = nmtstc_service_init (); if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); devices = nm_client_get_devices (client); g_assert (devices->len == 0); @@ -253,9 +190,6 @@ test_device_added_signal_after_init (void) device = g_ptr_array_index (devices, 0); g_assert (device); g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0"); - - g_object_unref (client); - g_clear_pointer (&sinfo, nmtstc_service_cleanup); } /*****************************************************************************/ @@ -355,19 +289,19 @@ wifi_ap_remove_notify_cb (NMDeviceWifi *w, static void test_wifi_ap_added_removed (void) { - NMClient *client; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; NMDeviceWifi *wifi; - WifiApInfo info = { loop, FALSE, FALSE, 0, 0 }; + WifiApInfo info = { gl.loop, FALSE, FALSE, 0, 0 }; GVariant *ret; GError *error = NULL; - char *expected_path = NULL; + gs_free char *expected_path = NULL; sinfo = nmtstc_service_init (); if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); /*************************************/ /* Add the wifi device */ @@ -406,8 +340,8 @@ test_wifi_ap_added_removed (void) info.quit_count++; /* Wait for libnm to find the AP */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); + info.quit_id = g_timeout_add_seconds (5, loop_quit, gl.loop); + g_main_loop_run (gl.loop); g_assert (info.signaled); g_assert (info.notified); @@ -445,8 +379,8 @@ test_wifi_ap_added_removed (void) info.quit_count++; /* Wait for libnm to find the AP */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); + info.quit_id = g_timeout_add_seconds (5, loop_quit, gl.loop); + g_main_loop_run (gl.loop); g_assert (info.signaled); g_assert (info.notified); @@ -454,10 +388,6 @@ test_wifi_ap_added_removed (void) g_signal_handlers_disconnect_by_func (wifi, wifi_ap_remove_notify_cb, &info); g_free (info.ap_path); - g_free (expected_path); - - g_object_unref (client); - g_clear_pointer (&sinfo, nmtstc_service_cleanup); } /*****************************************************************************/ @@ -517,25 +447,11 @@ da_devices_notify_cb (NMClient *c, } static void -new_client_cb (GObject *object, - GAsyncResult *result, - gpointer user_data) -{ - NMClient **out_client = user_data; - GError *error = NULL; - - *out_client = nm_client_new_finish (result, &error); - g_assert_no_error (error); - g_assert (*out_client != NULL); - - g_main_loop_quit (loop); -} - -static void test_devices_array (void) { - NMClient *client = NULL; - DaInfo info = { loop }; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; + DaInfo info = { gl.loop }; NMDevice *wlan0, *eth0, *eth1, *device; const GPtrArray *devices; GError *error = NULL; @@ -545,10 +461,7 @@ test_devices_array (void) if (!nmtstc_service_available (sinfo)) return; - /* Make sure that we test the async codepath in at least one test... */ - nm_client_new_async (NULL, new_client_cb, &client); - g_main_loop_run (loop); - g_assert (client != NULL); + client = nmtstc_client_new (TRUE); /*************************************/ /* Add some devices */ @@ -598,8 +511,8 @@ test_devices_array (void) info.quit_count = 2; /* Wait for libnm to notice the changes */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); + info.quit_id = g_timeout_add_seconds (5, loop_quit, gl.loop); + g_main_loop_run (gl.loop); g_assert_cmpint (info.quit_count, ==, 0); g_signal_handlers_disconnect_by_func (client, da_device_removed_cb, &info); @@ -617,9 +530,6 @@ test_devices_array (void) device = nm_client_get_device_by_iface (client, "eth1"); g_assert (NM_IS_DEVICE_ETHERNET (device)); g_assert (device == eth1); - - g_object_unref (client); - g_clear_pointer (&sinfo, nmtstc_service_cleanup); } static void @@ -630,20 +540,20 @@ nm_running_changed (GObject *client, int *running_changed = user_data; (*running_changed)++; - g_main_loop_quit (loop); + g_main_loop_quit (gl.loop); } static void test_client_nm_running (void) { + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; gs_unref_object NMClient *client1 = NULL; gs_unref_object NMClient *client2 = NULL; guint quit_id; int running_changed = 0; GError *error = NULL; - client1 = nm_client_new (NULL, &error); - g_assert_no_error (error); + client1 = nmtstc_client_new (TRUE); g_assert (!nm_client_get_nm_running (client1)); g_assert_cmpstr (nm_client_get_version (client1), ==, NULL); @@ -663,8 +573,7 @@ test_client_nm_running (void) if (!nmtstc_service_available (sinfo)) return; - client2 = nm_client_new (NULL, &error); - g_assert_no_error (error); + client2 = nmtstc_client_new (FALSE); /* client2 should know that NM is running, but the previously-created * client1 hasn't gotten the news yet. @@ -674,8 +583,8 @@ test_client_nm_running (void) g_signal_connect (client1, "notify::" NM_CLIENT_NM_RUNNING, G_CALLBACK (nm_running_changed), &running_changed); - quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); + quit_id = g_timeout_add_seconds (5, loop_quit, gl.loop); + g_main_loop_run (gl.loop); g_assert_cmpint (running_changed, ==, 1); g_assert (nm_client_get_nm_running (client1)); g_source_remove (quit_id); @@ -685,8 +594,8 @@ test_client_nm_running (void) g_assert (nm_client_get_nm_running (client1)); - quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); + quit_id = g_timeout_add_seconds (5, loop_quit, gl.loop); + g_main_loop_run (gl.loop); g_assert_cmpint (running_changed, ==, 2); g_assert (!nm_client_get_nm_running (client1)); g_source_remove (quit_id); @@ -786,18 +695,17 @@ device_ac_changed_cb (GObject *device, static void test_active_connections (void) { - NMClient *client; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; NMDevice *device; NMConnection *conn; - TestACInfo info = { loop, NULL, 0 }; - GError *error = NULL; + TestACInfo info = { gl.loop, NULL, 0 }; sinfo = nmtstc_service_init (); if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); /* Tell the test service to add a new device */ device = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0"); @@ -814,30 +722,25 @@ test_active_connections (void) /* Two signals plus activate_cb */ info.remaining = 3; - g_main_loop_run (loop); + g_main_loop_run (gl.loop); g_signal_handlers_disconnect_by_func (client, client_acs_changed_cb, &info); g_signal_handlers_disconnect_by_func (device, device_ac_changed_cb, &info); g_assert (info.ac != NULL); g_object_unref (info.ac); - g_object_unref (client); + g_clear_object (&client); /* Ensure that we can correctly resolve the recursive property link between the * AC and the Device in a newly-created client. */ - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); assert_ac_and_device (client); - g_object_unref (client); + g_clear_object (&client); - client = NULL; - nm_client_new_async (NULL, new_client_cb, &client); - g_main_loop_run (loop); + client = nmtstc_client_new (TRUE); assert_ac_and_device (client); - g_object_unref (client); - - g_clear_pointer (&sinfo, nmtstc_service_cleanup); + g_clear_object (&client); } static void @@ -916,20 +819,19 @@ activate_cb (GObject *object, static void test_activate_virtual (void) { - NMClient *client; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; NMConnection *conn; NMSettingConnection *s_con; NMSettingVlan *s_vlan; - TestACInfo info = { loop, NULL, 0 }; - TestConnectionInfo conn_info = { loop, NULL }; - GError *error = NULL; + TestACInfo info = { gl.loop, NULL, 0 }; + TestConnectionInfo conn_info = { gl.loop, NULL }; sinfo = nmtstc_service_init (); if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0"); @@ -945,7 +847,7 @@ test_activate_virtual (void) nm_client_add_connection_async (client, conn, TRUE, NULL, add_connection_cb, &conn_info); - g_main_loop_run (loop); + g_main_loop_run (gl.loop); g_object_unref (conn); conn = NM_CONNECTION (conn_info.remote); @@ -965,16 +867,13 @@ test_activate_virtual (void) */ info.remaining = 3; - g_main_loop_run (loop); + g_main_loop_run (gl.loop); g_signal_handlers_disconnect_by_func (client, client_acs_changed_cb, &info); g_signal_handlers_disconnect_by_func (client, client_devices_changed_cb, &info); g_assert (info.ac != NULL); g_object_unref (info.ac); - g_object_unref (client); - - g_clear_pointer (&sinfo, nmtstc_service_cleanup); } static void @@ -991,23 +890,22 @@ activate_failed_cb (GObject *object, g_assert_error (error, NM_CLIENT_ERROR, NM_CLIENT_ERROR_OBJECT_CREATION_FAILED); g_clear_error (&error); - g_main_loop_quit (loop); + g_main_loop_quit (gl.loop); } static void test_activate_failed (void) { - NMClient *client; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; NMDevice *device; - NMConnection *conn; - GError *error = NULL; + gs_unref_object NMConnection *conn = NULL; sinfo = nmtstc_service_init (); if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); device = nmtstc_service_add_device (sinfo, client, "AddWiredDevice", "eth0"); @@ -1017,20 +915,17 @@ test_activate_failed (void) nm_client_add_and_activate_connection_async (client, conn, device, NULL, NULL, activate_failed_cb, NULL); - g_main_loop_run (loop); - - g_object_unref (conn); - g_object_unref (client); - - g_clear_pointer (&sinfo, nmtstc_service_cleanup); + g_main_loop_run (gl.loop); } static void test_device_connection_compatibility (void) { - NMClient *client; - NMDevice *device1, *device2; - NMConnection *conn; + nmtstc_auto_service_cleanup NMTstcServiceInfo *sinfo = NULL; + gs_unref_object NMClient *client = NULL; + gs_unref_object NMConnection *conn = NULL; + NMDevice *device1; + NMDevice *device2; NMSettingWired *s_wired; GError *error = NULL; const char *subchannels[] = { "0.0.8000", "0.0.8001", "0.0.8002", NULL }; @@ -1043,8 +938,7 @@ test_device_connection_compatibility (void) if (!nmtstc_service_available (sinfo)) return; - client = nm_client_new (NULL, &error); - g_assert_no_error (error); + client = nmtstc_client_new (TRUE); /* Create two devices */ device1 = nmtstc_service_add_wired_device (sinfo, client, "eth0", hw_addr1, subchannels); @@ -1092,11 +986,6 @@ test_device_connection_compatibility (void) nm_device_connection_compatible (device1, conn, &error); g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION); g_clear_error (&error); - - g_object_unref (conn); - g_object_unref (client); - - g_clear_pointer (&sinfo, nmtstc_service_cleanup); } /*****************************************************************************/ @@ -1179,9 +1068,7 @@ test_connection_invalid (void) FALSE, &path2); - client = nmtst_get_rand_bool () - ? _nm_client_new_sync () - : _nm_client_new_sync_inside_dispatched (); + client = nmtstc_client_new (TRUE); connections = nm_client_get_connections (client); g_assert (connections); @@ -1216,7 +1103,7 @@ test_connection_invalid (void) FALSE, &path3); - nmtst_main_loop_run (loop, 1000); + nmtst_main_loop_run (gl.loop, 1000); connections = nm_client_get_connections (client); g_assert (connections); @@ -1252,7 +1139,7 @@ test_connection_invalid (void) variant, FALSE); - nmtst_main_loop_run (loop, 100); + nmtst_main_loop_run (gl.loop, 100); connections = nm_client_get_connections (client); g_assert (connections); @@ -1290,7 +1177,7 @@ test_connection_invalid (void) variant, FALSE); - nmtst_main_loop_run (loop, 100); + nmtst_main_loop_run (gl.loop, 100); connections = nm_client_get_connections (client); g_assert (connections); @@ -1325,7 +1212,7 @@ test_connection_invalid (void) connection, FALSE); - nmtst_main_loop_run (loop, 100); + nmtst_main_loop_run (gl.loop, 100); connections = nm_client_get_connections (client); g_assert (connections); @@ -1367,7 +1254,7 @@ test_connection_invalid (void) connection, FALSE); - nmtst_main_loop_run (loop, 100); + nmtst_main_loop_run (gl.loop, 100); connections = nm_client_get_connections (client); g_assert (connections); @@ -1409,7 +1296,7 @@ test_connection_invalid (void) connection, FALSE); - nmtst_main_loop_run (loop, 100); + nmtst_main_loop_run (gl.loop, 100); connections = nm_client_get_connections (client); g_assert (connections); @@ -1448,7 +1335,7 @@ main (int argc, char **argv) nmtst_init (&argc, &argv, TRUE); - loop = g_main_loop_new (NULL, FALSE); + gl.loop = g_main_loop_new (NULL, FALSE); g_test_add_func ("/libnm/device-added", test_device_added); g_test_add_func ("/libnm/device-added-signal-after-init", test_device_added_signal_after_init); diff --git a/libnm/tests/test-remote-settings-client.c b/libnm/tests/test-remote-settings-client.c index 7017808972..8483eca838 100644 --- a/libnm/tests/test-remote-settings-client.c +++ b/libnm/tests/test-remote-settings-client.c @@ -8,12 +8,16 @@ #include <sys/types.h> #include <signal.h> +#include "nm-glib-aux/nm-time-utils.h" + #include "nm-test-libnm-utils.h" -static NMTstcServiceInfo *sinfo; -static NMClient *client = NULL; -GDBusConnection *bus = NULL; -NMRemoteConnection *remote = NULL; +static struct { + NMTstcServiceInfo *sinfo; + NMClient *client; + GDBusConnection *bus; + NMRemoteConnection *remote; +} gl = { }; /*****************************************************************************/ @@ -25,17 +29,17 @@ add_cb (GObject *s, gboolean *done = user_data; GError *error = NULL; - remote = nm_client_add_connection_finish (client, result, &error); + gl.remote = nm_client_add_connection_finish (gl.client, result, &error); g_assert_no_error (error); *done = TRUE; - g_object_add_weak_pointer (G_OBJECT (remote), (void **) &remote); + g_object_add_weak_pointer (G_OBJECT (gl.remote), (void **) &gl.remote); /* nm_client_add_connection_finish() adds a ref to @remote, but we * want the weak pointer to be cleared as soon as @client drops its own ref. * So drop ours. */ - g_object_unref (remote); + g_object_unref (gl.remote); } #define TEST_CON_ID "blahblahblah" @@ -44,32 +48,27 @@ static void test_add_connection (void) { NMConnection *connection; - time_t start, now; gboolean done = FALSE; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; connection = nmtst_create_minimal_connection (TEST_CON_ID, NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); - nm_client_add_connection_async (client, + nm_client_add_connection_async (gl.client, connection, TRUE, NULL, add_cb, &done); - start = time (NULL); - do { - now = time (NULL); - g_main_context_iteration (NULL, FALSE); - } while ((done == FALSE) && (now - start < 5)); - g_assert (done == TRUE); - g_assert (remote != NULL); + nmtst_main_context_iterate_until (NULL, 5000, done); + + g_assert (gl.remote != NULL); /* Make sure the connection is the same as what we added */ g_assert (nm_connection_compare (connection, - NM_CONNECTION (remote), + NM_CONNECTION (gl.remote), NM_SETTING_COMPARE_FLAG_EXACT) == TRUE); g_object_unref (connection); } @@ -99,7 +98,7 @@ visible_changed_cb (GObject *object, GParamSpec *pspec, gboolean *done) static void connection_removed_cb (NMClient *s, NMRemoteConnection *connection, gboolean *done) { - if (connection == remote) + if (connection == gl.remote) *done = TRUE; } @@ -116,7 +115,6 @@ invis_has_settings_cb (NMSetting *setting, static void test_make_invisible (void) { - time_t start, now; const GPtrArray *conns; int i; GDBusProxy *proxy; @@ -124,17 +122,17 @@ test_make_invisible (void) gboolean has_settings = FALSE; char *path; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; - g_assert (remote != NULL); + g_assert (gl.remote != NULL); /* Listen for the remove event when the connection becomes invisible */ - g_signal_connect (remote, "notify::" NM_REMOTE_CONNECTION_VISIBLE, G_CALLBACK (visible_changed_cb), &visible_changed); - g_signal_connect (client, "connection-removed", G_CALLBACK (connection_removed_cb), &connection_removed); + g_signal_connect (gl.remote, "notify::" NM_REMOTE_CONNECTION_VISIBLE, G_CALLBACK (visible_changed_cb), &visible_changed); + g_signal_connect (gl.client, "connection-removed", G_CALLBACK (connection_removed_cb), &connection_removed); - path = g_strdup (nm_connection_get_path (NM_CONNECTION (remote))); - proxy = g_dbus_proxy_new_sync (bus, + path = g_strdup (nm_connection_get_path (NM_CONNECTION (gl.remote))); + proxy = g_dbus_proxy_new_sync (gl.bus, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL, NM_DBUS_SERVICE, @@ -153,29 +151,23 @@ test_make_invisible (void) set_visible_cb, NULL); /* Wait for the connection to be removed */ - start = time (NULL); - do { - now = time (NULL); - g_main_context_iteration (NULL, FALSE); - } while ((!visible_changed || !connection_removed) && (now - start < 5)); - g_assert (visible_changed == TRUE); - g_assert (connection_removed == TRUE); + nmtst_main_context_iterate_until (NULL, 5000, visible_changed && connection_removed); - g_signal_handlers_disconnect_by_func (remote, G_CALLBACK (visible_changed_cb), &visible_changed); - g_signal_handlers_disconnect_by_func (client, G_CALLBACK (connection_removed_cb), &connection_removed); + g_signal_handlers_disconnect_by_func (gl.remote, G_CALLBACK (visible_changed_cb), &visible_changed); + g_signal_handlers_disconnect_by_func (gl.client, G_CALLBACK (connection_removed_cb), &connection_removed); /* Ensure NMClient no longer has the connection */ - conns = nm_client_get_connections (client); + conns = nm_client_get_connections (gl.client); for (i = 0; i < conns->len; i++) { NMConnection *candidate = NM_CONNECTION (conns->pdata[i]); - g_assert ((gpointer) remote != (gpointer) candidate); + g_assert ((gpointer) gl.remote != (gpointer) candidate); g_assert (strcmp (path, nm_connection_get_path (candidate)) != 0); } /* And ensure the invisible connection no longer has any settings */ - g_assert (remote); - nm_connection_for_each_setting_value (NM_CONNECTION (remote), + g_assert (gl.remote); + nm_connection_for_each_setting_value (NM_CONNECTION (gl.remote), invis_has_settings_cb, &has_settings); g_assert (has_settings == FALSE); @@ -197,7 +189,6 @@ vis_new_connection_cb (NMClient *foo, static void test_make_visible (void) { - time_t start, now; const GPtrArray *conns; int i; GDBusProxy *proxy; @@ -205,17 +196,17 @@ test_make_visible (void) char *path; NMRemoteConnection *new = NULL; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; - g_assert (remote != NULL); + g_assert (gl.remote != NULL); /* Wait for the new-connection signal when the connection is visible again */ - g_signal_connect (client, NM_CLIENT_CONNECTION_ADDED, + g_signal_connect (gl.client, NM_CLIENT_CONNECTION_ADDED, G_CALLBACK (vis_new_connection_cb), &new); - path = g_strdup (nm_connection_get_path (NM_CONNECTION (remote))); - proxy = g_dbus_proxy_new_sync (bus, + path = g_strdup (nm_connection_get_path (NM_CONNECTION (gl.remote))); + proxy = g_dbus_proxy_new_sync (gl.bus, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL, NM_DBUS_SERVICE, @@ -234,24 +225,19 @@ test_make_visible (void) set_visible_cb, NULL); /* Wait for the settings service to announce the connection again */ - start = time (NULL); - do { - now = time (NULL); - g_main_context_iteration (NULL, FALSE); - } while ((new == NULL) && (now - start < 5)); + nmtst_main_context_iterate_until (NULL, 5000, new); /* Ensure the new connection is the same as the one we made visible again */ - g_assert (new); - g_assert (new == remote); + g_assert (new == gl.remote); - g_signal_handlers_disconnect_by_func (client, G_CALLBACK (vis_new_connection_cb), &new); + g_signal_handlers_disconnect_by_func (gl.client, G_CALLBACK (vis_new_connection_cb), &new); /* Ensure NMClient has the connection */ - conns = nm_client_get_connections (client); + conns = nm_client_get_connections (gl.client); for (i = 0; i < conns->len; i++) { NMConnection *candidate = NM_CONNECTION (conns->pdata[i]); - if ((gpointer) remote == (gpointer) candidate) { + if ((gpointer) gl.remote == (gpointer) candidate) { g_assert_cmpstr (path, ==, nm_connection_get_path (candidate)); g_assert_cmpstr (TEST_CON_ID, ==, nm_connection_get_id (candidate)); found = TRUE; @@ -282,7 +268,7 @@ deleted_cb (GObject *proxy, static void removed_cb (NMClient *s, NMRemoteConnection *connection, gboolean *done) { - if (connection == remote) + if (connection == gl.remote) *done = TRUE; } @@ -290,27 +276,26 @@ static void test_remove_connection (void) { NMRemoteConnection *connection; - time_t start, now; const GPtrArray *conns; int i; GDBusProxy *proxy; gboolean done = FALSE; char *path; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; /* Find a connection to delete */ - conns = nm_client_get_connections (client); + conns = nm_client_get_connections (gl.client); g_assert_cmpint (conns->len, >, 0); connection = NM_REMOTE_CONNECTION (conns->pdata[0]); g_assert (connection); - g_assert (remote == connection); + g_assert (gl.remote == connection); path = g_strdup (nm_connection_get_path (NM_CONNECTION (connection))); - g_signal_connect (client, "connection-removed", G_CALLBACK (removed_cb), &done); + g_signal_connect (gl.client, "connection-removed", G_CALLBACK (removed_cb), &done); - proxy = g_dbus_proxy_new_sync (bus, + proxy = g_dbus_proxy_new_sync (gl.bus, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL, NM_DBUS_SERVICE, @@ -328,18 +313,10 @@ test_remove_connection (void) NULL, deleted_cb, NULL); - start = time (NULL); - do { - now = time (NULL); - g_main_context_iteration (NULL, FALSE); - if (done && !remote) - break; - } while (now - start < 5); - g_assert (done == TRUE); - g_assert (!remote); + nmtst_main_context_iterate_until (NULL, 5000, done && !gl.remote); /* Ensure NMClient no longer has the connection */ - conns = nm_client_get_connections (client); + conns = nm_client_get_connections (gl.client); for (i = 0; i < conns->len; i++) { NMConnection *candidate = NM_CONNECTION (conns->pdata[i]); @@ -364,7 +341,7 @@ add_remove_cb (GObject *s, gboolean *done = user_data; gs_free_error GError *error = NULL; - connection = nm_client_add_connection_finish (client, result, &error); + connection = nm_client_add_connection_finish (gl.client, result, &error); g_assert_error (error, NM_CLIENT_ERROR, NM_CLIENT_ERROR_OBJECT_CREATION_FAILED); g_assert (connection == NULL); @@ -374,43 +351,34 @@ add_remove_cb (GObject *s, static void test_add_remove_connection (void) { - GVariant *ret; + gs_unref_variant GVariant *ret = NULL; GError *error = NULL; - NMConnection *connection; - time_t start, now; + gs_unref_object NMConnection *connection = NULL; gboolean done = FALSE; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; /* This will cause the test server to immediately delete the connection * after creating it. */ - ret = g_dbus_proxy_call_sync (sinfo->proxy, + ret = g_dbus_proxy_call_sync (gl.sinfo->proxy, "AutoRemoveNextConnection", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - g_assert_no_error (error); - g_variant_unref (ret); + nmtst_assert_success (ret, error); connection = nmtst_create_minimal_connection (TEST_ADD_REMOVE_ID, NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); - nm_client_add_connection_async (client, + nm_client_add_connection_async (gl.client, connection, TRUE, NULL, add_remove_cb, &done); - start = time (NULL); - do { - now = time (NULL); - g_main_context_iteration (NULL, FALSE); - } while ((done == FALSE) && (now - start < 5)); - g_assert (done == TRUE); - - g_object_unref (connection); + nmtst_main_context_iterate_until (NULL, 5000, done); } /*****************************************************************************/ @@ -423,7 +391,7 @@ add_bad_cb (GObject *s, gboolean *done = user_data; gs_free_error GError *error = NULL; - remote = nm_client_add_connection_finish (client, result, &error); + gl.remote = nm_client_add_connection_finish (gl.client, result, &error); g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); *done = TRUE; @@ -432,31 +400,25 @@ add_bad_cb (GObject *s, static void test_add_bad_connection (void) { - NMConnection *connection; - time_t start, now; + gs_unref_object NMConnection *connection = NULL; gboolean done = FALSE; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; /* The test daemon doesn't support bond connections */ connection = nmtst_create_minimal_connection ("bad connection test", NULL, NM_SETTING_BOND_SETTING_NAME, NULL); - nm_client_add_connection_async (client, + nm_client_add_connection_async (gl.client, connection, TRUE, NULL, add_bad_cb, &done); - g_object_unref (connection); + g_clear_object (&connection); - start = time (NULL); - do { - now = time (NULL); - g_main_context_iteration (NULL, FALSE); - } while ((done == FALSE) && (now - start < 5)); - g_assert (done == TRUE); - g_assert (remote == NULL); + nmtst_main_context_iterate_until (NULL, 5000, done); + g_assert (gl.remote == NULL); } /*****************************************************************************/ @@ -469,7 +431,7 @@ save_hostname_cb (GObject *s, gboolean *done = user_data; gs_free_error GError *error = NULL; - nm_client_save_hostname_finish (client, result, &error); + nm_client_save_hostname_finish (gl.client, result, &error); g_assert_no_error (error); *done = TRUE; @@ -478,27 +440,30 @@ save_hostname_cb (GObject *s, static void test_save_hostname (void) { - time_t start, now; + gint64 until_ts; gboolean done = FALSE; GError *error = NULL; - if (!nmtstc_service_available (sinfo)) + if (!nmtstc_service_available (gl.sinfo)) return; /* test-networkmanager-service.py requires the hostname to contain a '.' */ - nm_client_save_hostname (client, "foo", NULL, &error); + nm_client_save_hostname (gl.client, "foo", NULL, &error); g_assert_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_HOSTNAME); g_clear_error (&error); - nm_client_save_hostname_async (client, "example.com", NULL, save_hostname_cb, &done); + nm_client_save_hostname_async (gl.client, "example.com", NULL, save_hostname_cb, &done); - start = time (NULL); - do { - now = time (NULL); + until_ts = nm_utils_get_monotonic_timestamp_ms () + 5000; + while (TRUE) { g_main_context_iteration (NULL, FALSE); - } while ((done == FALSE) && (now - start < 5)); - g_assert (done == TRUE); - g_assert (remote == NULL); + if (done) + break; + if (nm_utils_get_monotonic_timestamp_ms () >= until_ts) + g_assert_not_reached (); + } + + g_assert (gl.remote == NULL); } /*****************************************************************************/ @@ -515,14 +480,12 @@ main (int argc, char **argv) nmtst_init (&argc, &argv, TRUE); - bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); - g_assert_no_error (error); + gl.bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); + nmtst_assert_success (gl.bus, error); - sinfo = nmtstc_service_init (); + gl.sinfo = nmtstc_service_init (); - client = nm_client_new (NULL, &error); - g_assert_no_error (error); - g_assert (client != NULL); + gl.client = nmtstc_client_new (TRUE); /* FIXME: these tests assume that they get run in order, but g_test_run() * does not actually guarantee that! @@ -537,9 +500,9 @@ main (int argc, char **argv) ret = g_test_run (); - nmtstc_service_cleanup (sinfo); - g_object_unref (client); - g_object_unref (bus); + nm_clear_pointer (&gl.sinfo, nmtstc_service_cleanup); + g_clear_object (&gl.client); + g_clear_object (&gl.bus); return ret; } diff --git a/libnm/tests/test-secret-agent.c b/libnm/tests/test-secret-agent.c index bb822a0a0e..fef6071df9 100644 --- a/libnm/tests/test-secret-agent.c +++ b/libnm/tests/test-secret-agent.c @@ -234,8 +234,9 @@ test_setup (TestSecretAgentData *sadata, gconstpointer test_data) if (!sadata->sinfo) return; - sadata->client = nm_client_new (NULL, &error); - g_assert_no_error (error); + g_assert (g_main_context_get_thread_default () == NULL); + + sadata->client = nmtstc_client_new (TRUE); sadata->loop = g_main_loop_new (NULL, FALSE); sadata->timeout_id = g_timeout_add_seconds (5, timeout_assert, NULL); |