diff options
author | Dan Winship <danw@gnome.org> | 2014-10-08 18:14:30 -0400 |
---|---|---|
committer | Dan Winship <danw@gnome.org> | 2014-10-19 09:27:47 -0400 |
commit | 08a344d723d1c22a9f0f3b2be85904dab9c3fd61 (patch) | |
tree | faa1cac6828c89306ed6aba142c660afe0162e4f /libnm/tests | |
parent | 4779d96685b3e2c295218088db8a3999450da39b (diff) | |
download | NetworkManager-08a344d723d1c22a9f0f3b2be85904dab9c3fd61.tar.gz |
libnm: abstract out duplicated device-creating code in tests
Diffstat (limited to 'libnm/tests')
-rw-r--r-- | libnm/tests/common.c | 67 | ||||
-rw-r--r-- | libnm/tests/common.h | 6 | ||||
-rw-r--r-- | libnm/tests/test-nm-client.c | 184 | ||||
-rw-r--r-- | libnm/tests/test-secret-agent.c | 46 |
4 files changed, 103 insertions, 200 deletions
diff --git a/libnm/tests/common.c b/libnm/tests/common.c index 109ab04322..79308329a0 100644 --- a/libnm/tests/common.c +++ b/libnm/tests/common.c @@ -116,3 +116,70 @@ nm_test_service_cleanup (NMTestServiceInfo *info) memset (info, 0, sizeof (*info)); g_free (info); } + +typedef struct { + GMainLoop *loop; + const char *ifname; + char *path; + NMDevice *device; +} AddDeviceInfo; + +static void +device_added_cb (NMClient *client, + NMDevice *device, + gpointer user_data) +{ + AddDeviceInfo *info = user_data; + + g_assert (device); + g_assert_cmpstr (nm_object_get_path (NM_OBJECT (device)), ==, info->path); + g_assert_cmpstr (nm_device_get_iface (device), ==, info->ifname); + + info->device = device; + g_main_loop_quit (info->loop); +} + +static gboolean +timeout (gpointer user_data) +{ + g_assert_not_reached (); + return G_SOURCE_REMOVE; +} + +NMDevice * +nm_test_service_add_device (NMTestServiceInfo *sinfo, NMClient *client, + const char *method, const char *ifname) +{ + AddDeviceInfo info; + GError *error = NULL; + GVariant *ret; + guint timeout_id; + + ret = g_dbus_proxy_call_sync (sinfo->proxy, + method, + g_variant_new ("(s)", ifname), + G_DBUS_CALL_FLAGS_NO_AUTO_START, + 3000, + NULL, + &error); + g_assert_no_error (error); + g_assert (ret); + g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)"); + g_variant_get (ret, "(o)", &info.path); + g_variant_unref (ret); + + /* Wait for libnm to find the device */ + info.ifname = ifname; + info.loop = g_main_loop_new (NULL, FALSE); + g_signal_connect (client, "device-added", + G_CALLBACK (device_added_cb), &info); + timeout_id = g_timeout_add_seconds (5, timeout, NULL); + g_main_loop_run (info.loop); + + g_source_remove (timeout_id); + g_signal_handlers_disconnect_by_func (client, device_added_cb, &info); + g_free (info.path); + g_main_loop_unref (info.loop); + + return info.device; +} diff --git a/libnm/tests/common.h b/libnm/tests/common.h index 7c49d2532b..aa15167b85 100644 --- a/libnm/tests/common.h +++ b/libnm/tests/common.h @@ -19,6 +19,7 @@ */ #include <gio/gio.h> +#include <NetworkManager.h> typedef struct { GDBusConnection *bus; @@ -29,3 +30,8 @@ typedef struct { NMTestServiceInfo *nm_test_service_init (void); void nm_test_service_cleanup (NMTestServiceInfo *info); + +NMDevice *nm_test_service_add_device (NMTestServiceInfo *info, + NMClient *client, + const char *method, + const char *ifname); diff --git a/libnm/tests/test-nm-client.c b/libnm/tests/test-nm-client.c index e666551f49..82943e588f 100644 --- a/libnm/tests/test-nm-client.c +++ b/libnm/tests/test-nm-client.c @@ -40,65 +40,14 @@ loop_quit (gpointer user_data) return G_SOURCE_REMOVE; } -static gboolean -add_device (const char *method, const char *ifname, char **out_path) -{ - GError *error = NULL; - GVariant *ret; - - ret = g_dbus_proxy_call_sync (sinfo->proxy, - method, - g_variant_new ("(s)", ifname), - G_DBUS_CALL_FLAGS_NO_AUTO_START, - 3000, - NULL, - &error); - g_assert_no_error (error); - g_assert (ret); - g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)"); - if (out_path) - g_variant_get (ret, "(o)", out_path); - g_variant_unref (ret); - return TRUE; -} - /*******************************************************************/ -typedef struct { - GMainLoop *loop; - gboolean signaled; - gboolean notified; - guint quit_count; - guint quit_id; -} DeviceAddedInfo; - -static void -device_add_check_quit (DeviceAddedInfo *info) -{ - info->quit_count--; - if (info->quit_count == 0) { - g_source_remove (info->quit_id); - info->quit_id = 0; - g_main_loop_quit (info->loop); - } -} - -static void -device_added_cb (NMClient *c, - NMDevice *device, - DeviceAddedInfo *info) -{ - g_assert (device); - g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0"); - info->signaled = TRUE; - device_add_check_quit (info); -} - static void devices_notify_cb (NMClient *c, GParamSpec *pspec, - DeviceAddedInfo *info) + gpointer user_data) { + gboolean *notified = user_data; const GPtrArray *devices; NMDevice *device; @@ -110,9 +59,7 @@ devices_notify_cb (NMClient *c, g_assert (device); g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0"); - info->notified = TRUE; - - device_add_check_quit (info); + *notified = TRUE; } static void @@ -121,7 +68,7 @@ test_device_added (void) NMClient *client; const GPtrArray *devices; NMDevice *device; - DeviceAddedInfo info = { loop, FALSE, FALSE, 0, 0 }; + gboolean notified = FALSE; GError *error = NULL; sinfo = nm_test_service_init (); @@ -131,30 +78,18 @@ test_device_added (void) devices = nm_client_get_devices (client); g_assert (devices->len == 0); - /* Tell the test service to add a new device */ - add_device ("AddWiredDevice", "eth0", NULL); - - g_signal_connect (client, - "device-added", - (GCallback) device_added_cb, - &info); - info.quit_count++; - g_signal_connect (client, "notify::devices", (GCallback) devices_notify_cb, - &info); - info.quit_count++; + ¬ified); - /* Wait for libnm to find the device */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); + /* Tell the test service to add a new device */ + nm_test_service_add_device (sinfo, client, "AddWiredDevice", "eth0"); - g_assert (info.signaled); - g_assert (info.notified); + while (!notified) + g_main_context_iteration (NULL, TRUE); - g_signal_handlers_disconnect_by_func (client, device_added_cb, &info); - g_signal_handlers_disconnect_by_func (client, devices_notify_cb, &info); + g_signal_handlers_disconnect_by_func (client, devices_notify_cb, ¬ified); devices = nm_client_get_devices (client); g_assert (devices); @@ -194,16 +129,6 @@ wifi_check_quit (WifiApInfo *info) } static void -wifi_device_added_cb (NMClient *c, - NMDevice *device, - WifiApInfo *info) -{ - g_assert_cmpstr (nm_device_get_iface (device), ==, "wlan0"); - info->found = TRUE; - wifi_check_quit (info); -} - -static void got_ap_path (WifiApInfo *info, const char *path) { if (info->ap_path) @@ -288,26 +213,11 @@ test_wifi_ap_added_removed (void) /*************************************/ /* Add the wifi device */ - add_device ("AddWifiDevice", "wlan0", NULL); - - g_signal_connect (client, - "device-added", - (GCallback) wifi_device_added_cb, - &info); - info.quit_count = 1; - - /* Wait for libnm to find the device */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); - - g_assert (info.found); - g_signal_handlers_disconnect_by_func (client, wifi_device_added_cb, &info); - - wifi = (NMDeviceWifi *) nm_client_get_device_by_iface (client, "wlan0"); + wifi = (NMDeviceWifi *) nm_test_service_add_device (sinfo, client, "AddWifiDevice", "wlan0"); g_assert (NM_IS_DEVICE_WIFI (wifi)); /*************************************/ - /* Add the wifi device */ + /* Add the wifi AP */ info.signaled = FALSE; info.notified = FALSE; info.quit_id = 0; @@ -418,16 +328,6 @@ wimax_check_quit (WimaxNspInfo *info) } static void -wimax_device_added_cb (NMClient *c, - NMDevice *device, - WimaxNspInfo *info) -{ - g_assert_cmpstr (nm_device_get_iface (device), ==, "wmx0"); - info->found = TRUE; - wimax_check_quit (info); -} - -static void got_nsp_path (WimaxNspInfo *info, const char *path) { if (info->nsp_path) @@ -512,22 +412,7 @@ test_wimax_nsp_added_removed (void) /*************************************/ /* Add the wimax device */ - add_device ("AddWimaxDevice", "wmx0", NULL); - - g_signal_connect (client, - "device-added", - (GCallback) wimax_device_added_cb, - &info); - info.quit_count = 1; - - /* Wait for libnm to find the device */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); - - g_assert (info.found); - g_signal_handlers_disconnect_by_func (client, wimax_device_added_cb, &info); - - wimax = (NMDeviceWimax *) nm_client_get_device_by_iface (client, "wmx0"); + wimax = (NMDeviceWimax *) nm_test_service_add_device (sinfo, client, "AddWimaxDevice", "wmx0"); g_assert (NM_IS_DEVICE_WIMAX (wimax)); /*************************************/ @@ -638,14 +523,6 @@ da_check_quit (DaInfo *info) } static void -da_device_added_cb (NMClient *c, - NMDevice *device, - DaInfo *info) -{ - da_check_quit (info); -} - -static void da_device_removed_cb (NMClient *c, NMDevice *device, DaInfo *info) @@ -700,8 +577,7 @@ test_devices_array (void) { NMClient *client = NULL; DaInfo info = { loop }; - char *paths[3] = { NULL, NULL, NULL }; - NMDevice *device; + NMDevice *wlan0, *eth0, *eth1, *device; const GPtrArray *devices; GError *error = NULL; GVariant *ret; @@ -715,22 +591,9 @@ test_devices_array (void) /*************************************/ /* Add some devices */ - add_device ("AddWifiDevice", "wlan0", &paths[0]); - add_device ("AddWiredDevice", "eth0", &paths[1]); - add_device ("AddWiredDevice", "eth1", &paths[2]); - info.quit_count = 3; - - g_signal_connect (client, - "device-added", - (GCallback) da_device_added_cb, - &info); - - /* Wait for libnm to find the device */ - info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); - g_main_loop_run (loop); - - g_assert_cmpint (info.quit_count, ==, 0); - g_signal_handlers_disconnect_by_func (client, da_device_added_cb, &info); + wlan0 = nm_test_service_add_device (sinfo, client,"AddWifiDevice", "wlan0"); + eth0 = nm_test_service_add_device (sinfo, client, "AddWiredDevice", "eth0"); + eth1 = nm_test_service_add_device (sinfo, client, "AddWiredDevice", "eth1"); /* Ensure the devices now exist */ devices = nm_client_get_devices (client); @@ -739,18 +602,21 @@ test_devices_array (void) device = nm_client_get_device_by_iface (client, "wlan0"); g_assert (NM_IS_DEVICE_WIFI (device)); + g_assert (device == wlan0); device = nm_client_get_device_by_iface (client, "eth0"); g_assert (NM_IS_DEVICE_ETHERNET (device)); + g_assert (device == eth0); device = nm_client_get_device_by_iface (client, "eth1"); g_assert (NM_IS_DEVICE_ETHERNET (device)); + g_assert (device == eth1); /********************************/ /* Now remove the device in the middle */ ret = g_dbus_proxy_call_sync (sinfo->proxy, "RemoveDevice", - g_variant_new ("(o)", paths[1]), + g_variant_new ("(o)", nm_object_get_path (NM_OBJECT (eth0))), G_DBUS_CALL_FLAGS_NO_AUTO_START, 3000, NULL, @@ -770,7 +636,7 @@ test_devices_array (void) &info); info.quit_count = 2; - /* Wait for libnm to find the device */ + /* Wait for libnm to notice the changes */ info.quit_id = g_timeout_add_seconds (5, loop_quit, loop); g_main_loop_run (loop); @@ -785,13 +651,11 @@ test_devices_array (void) device = nm_client_get_device_by_iface (client, "wlan0"); g_assert (NM_IS_DEVICE_WIFI (device)); + g_assert (device == wlan0); device = nm_client_get_device_by_iface (client, "eth1"); g_assert (NM_IS_DEVICE_ETHERNET (device)); - - g_free (paths[0]); - g_free (paths[1]); - g_free (paths[2]); + g_assert (device == eth1); g_object_unref (client); g_clear_pointer (&sinfo, nm_test_service_cleanup); diff --git a/libnm/tests/test-secret-agent.c b/libnm/tests/test-secret-agent.c index de49893c75..83755450fe 100644 --- a/libnm/tests/test-secret-agent.c +++ b/libnm/tests/test-secret-agent.c @@ -192,7 +192,6 @@ typedef struct { char *ifname; char *con_id; - char *devpath; int secrets_requested; } TestSecretAgentData; @@ -204,20 +203,6 @@ timeout_assert (gpointer user_data) } static void -device_added_cb (NMClient *c, - NMDevice *device, - gpointer user_data) -{ - TestSecretAgentData *sadata = user_data; - - g_assert (device); - g_assert_cmpstr (nm_device_get_iface (device), ==, sadata->ifname); - - sadata->device = device; - g_main_loop_quit (sadata->loop); -} - -static void connection_added_cb (GObject *s, GAsyncResult *result, gpointer user_data) @@ -261,8 +246,6 @@ test_setup (TestSecretAgentData *sadata, gconstpointer test_data) GBytes *ssid; NMSetting *s_wsec; GError *error = NULL; - GVariant *ret; - gulong handler; sadata->sinfo = nm_test_service_init (); sadata->client = nm_client_new (NULL, &error); @@ -276,24 +259,8 @@ test_setup (TestSecretAgentData *sadata, gconstpointer test_data) counter++; /* Create the device */ - ret = g_dbus_proxy_call_sync (sadata->sinfo->proxy, - "AddWifiDevice", - g_variant_new ("(s)", sadata->ifname), - G_DBUS_CALL_FLAGS_NO_AUTO_START, - 3000, - NULL, - &error); - g_assert_no_error (error); - g_assert (ret); - g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)"); - g_variant_get (ret, "(o)", &sadata->devpath); - g_variant_unref (ret); - - handler = g_signal_connect (sadata->client, "device-added", - G_CALLBACK (device_added_cb), sadata); - g_main_loop_run (sadata->loop); - g_signal_handler_disconnect (sadata->client, handler); - g_assert (sadata->device); + sadata->device = nm_test_service_add_device (sadata->sinfo, sadata->client, + "AddWifiDevice", sadata->ifname); /* Create the connection */ connection = nmtst_create_minimal_connection (sadata->con_id, NULL, NM_SETTING_WIRELESS_SETTING_NAME, &s_con); @@ -353,12 +320,9 @@ test_cleanup (TestSecretAgentData *sadata, gconstpointer test_data) g_object_unref (sadata->agent); } - g_object_unref (sadata->connection); - g_object_unref (sadata->client); - ret = g_dbus_proxy_call_sync (sadata->sinfo->proxy, "RemoveDevice", - g_variant_new ("(s)", sadata->devpath), + g_variant_new ("(s)", nm_object_get_path (NM_OBJECT (sadata->device))), G_DBUS_CALL_FLAGS_NO_AUTO_START, 3000, NULL, @@ -366,6 +330,9 @@ test_cleanup (TestSecretAgentData *sadata, gconstpointer test_data) g_assert_no_error (error); g_variant_unref (ret); + g_object_unref (sadata->connection); + g_object_unref (sadata->client); + nm_test_service_cleanup (sadata->sinfo); g_source_remove (sadata->timeout_id); @@ -373,7 +340,6 @@ test_cleanup (TestSecretAgentData *sadata, gconstpointer test_data) g_free (sadata->ifname); g_free (sadata->con_id); - g_free (sadata->devpath); } /*******************************************************************/ |