diff options
author | Dan Winship <danw@gnome.org> | 2014-10-08 13:44:45 -0400 |
---|---|---|
committer | Dan Winship <danw@gnome.org> | 2014-10-08 13:44:45 -0400 |
commit | c08764337a1805394e73ed9c00a0c0afef09ee8f (patch) | |
tree | 981b4d738dc33fba663f8b9486d05df2940aa0f3 | |
parent | 476eb9d1af9eaca891ed21ba2c6eef82e5ed6410 (diff) | |
download | NetworkManager-danw/bgo737993-objectcache-orig.tar.gz |
libnm/tests: add a virtual device creation-and-activation testdanw/bgo737993-objectcache-orig
Add a test of creating a (virtual) device and activating a connection
on it at the same time.
-rw-r--r-- | libnm/tests/test-nm-client.c | 123 | ||||
-rwxr-xr-x | tools/test-networkmanager-service.py | 46 |
2 files changed, 161 insertions, 8 deletions
diff --git a/libnm/tests/test-nm-client.c b/libnm/tests/test-nm-client.c index e889d87194..fb55a77c9c 100644 --- a/libnm/tests/test-nm-client.c +++ b/libnm/tests/test-nm-client.c @@ -719,6 +719,7 @@ typedef struct { NMActiveConnection *ac; int remaining; + gboolean add_and_activate; } TestACInfo; static void @@ -778,7 +779,10 @@ add_and_activate_cb (GObject *object, GError *error = NULL; const GPtrArray *devices; - info->ac = nm_client_add_and_activate_connection_finish (NM_CLIENT (object), result, &error); + if (info->add_and_activate) + info->ac = nm_client_add_and_activate_connection_finish (NM_CLIENT (object), result, &error); + else + info->ac = nm_client_activate_connection_finish (NM_CLIENT (object), result, &error); g_assert_no_error (error); g_assert (info->ac != NULL); @@ -848,7 +852,7 @@ test_active_connections (void) NMClient *client; NMDevice *device; NMConnection *conn; - TestACInfo info = { loop, NULL, 0 }; + TestACInfo info = { loop, NULL, 0, TRUE }; GError *error = NULL; sinfo = nm_test_service_init (); @@ -905,6 +909,120 @@ test_active_connections (void) g_clear_pointer (&sinfo, nm_test_service_cleanup); } +static void +client_devices_changed_cb (GObject *client, + GParamSpec *pspec, + gpointer user_data) +{ + TestACInfo *info = user_data; + const GPtrArray *devices; + NMDevice *device; + + devices = nm_client_get_devices (NM_CLIENT (client)); + g_assert (devices != NULL); + g_assert_cmpint (devices->len, ==, 2); + + if (NM_IS_DEVICE_VLAN (devices->pdata[0])) + device = devices->pdata[0]; + else if (NM_IS_DEVICE_VLAN (devices->pdata[1])) + device = devices->pdata[1]; + else + g_assert_not_reached (); + + g_assert_cmpstr (nm_device_get_iface (device), ==, "eth0.1"); + + if (nm_device_get_active_connection (device)) + info->remaining--; + else { + g_signal_connect (device, "notify::" NM_DEVICE_ACTIVE_CONNECTION, + G_CALLBACK (device_ac_changed_cb), &info); + } + + info->remaining--; + if (!info->remaining) + g_main_loop_quit (info->loop); +} + +typedef struct { + GMainLoop *loop; + NMRemoteConnection *remote; +} TestConnectionInfo; + +static void +add_connection_cb (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + TestConnectionInfo *info = user_data; + GError *error = NULL; + + info->remote = nm_remote_settings_add_connection_finish (NM_REMOTE_SETTINGS (object), result, &error); + g_assert_no_error (error); + g_main_loop_quit (info->loop); +} + +static void +test_activate_virtual (void) +{ + NMClient *client; + NMRemoteSettings *settings; + NMConnection *conn; + NMSettingConnection *s_con; + NMSettingVlan *s_vlan; + TestACInfo info = { loop, NULL, 0, FALSE }; + TestConnectionInfo conn_info = { loop, NULL }; + GError *error = NULL; + + sinfo = nm_test_service_init (); + client = nm_client_new (NULL, &error); + g_assert_no_error (error); + settings = nm_remote_settings_new (NULL, &error); + g_assert_no_error (error); + + nm_test_service_add_device (sinfo, client, "AddWiredDevice", "eth0"); + + conn = nmtst_create_minimal_connection ("test-ac", NULL, NM_SETTING_VLAN_SETTING_NAME, &s_con); + g_object_set (s_con, + NM_SETTING_CONNECTION_INTERFACE_NAME, "eth0.1", + NULL); + s_vlan = nm_connection_get_setting_vlan (conn); + g_object_set (s_vlan, + NM_SETTING_VLAN_ID, 1, + NM_SETTING_VLAN_PARENT, "eth0", + NULL); + + nm_remote_settings_add_connection_async (settings, conn, TRUE, + NULL, add_connection_cb, &conn_info); + g_main_loop_run (loop); + g_object_unref (conn); + conn = NM_CONNECTION (conn_info.remote); + + nm_client_activate_connection_async (client, conn, NULL, NULL, + NULL, add_and_activate_cb, &info); + g_object_unref (conn); + + /* As with test_active_connections() above, except that now the NMDeviceVlan + * is created at the same time, so there's one more thing to wait for. + */ + info.remaining = 5; + + g_signal_connect (client, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS, + G_CALLBACK (client_acs_changed_cb), &info); + g_signal_connect (client, "notify::" NM_CLIENT_DEVICES, + G_CALLBACK (client_devices_changed_cb), &info); + + g_main_loop_run (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, nm_test_service_cleanup); +} + /*******************************************************************/ int @@ -926,6 +1044,7 @@ main (int argc, char **argv) g_test_add_func ("/libnm/devices-array", test_devices_array); g_test_add_func ("/libnm/client-nm-running", test_client_nm_running); g_test_add_func ("/libnm/active-connections", test_active_connections); + g_test_add_func ("/libnm/activate-virtual", test_activate_virtual); return g_test_run (); } diff --git a/tools/test-networkmanager-service.py b/tools/test-networkmanager-service.py index 8a747a26e8..bff80d13ca 100755 --- a/tools/test-networkmanager-service.py +++ b/tools/test-networkmanager-service.py @@ -226,6 +226,34 @@ class WiredDevice(Device): pass ################################################################### +IFACE_VLAN = 'org.freedesktop.NetworkManager.Device.Vlan' + +PV_HW_ADDRESS = "HwAddress" +PV_CARRIER = "Carrier" +PV_VLAN_ID = "VlanId" + +class VlanDevice(Device): + def __init__(self, bus, iface): + Device.__init__(self, bus, iface, NM_DEVICE_TYPE_VLAN) + self.add_dbus_interface(IFACE_VLAN, self.__get_props) + + self.mac = random_mac() + self.carrier = False + self.vlan_id = 1 + + # Properties interface + def __get_props(self): + props = {} + props[PV_HW_ADDRESS] = self.mac + props[PV_CARRIER] = self.carrier + props[PV_VLAN_ID] = self.vlan_id + return props + + @dbus.service.signal(IFACE_VLAN, signature='a{sv}') + def PropertiesChanged(self, changed): + pass + +################################################################### IFACE_WIFI_AP = 'org.freedesktop.NetworkManager.AccessPoint' PP_FLAGS = "Flags" @@ -650,21 +678,27 @@ class NetworkManager(ExportedObj): @dbus.service.method(dbus_interface=IFACE_NM, in_signature='ooo', out_signature='o') def ActivateConnection(self, conpath, devpath, specific_object): + try: + connection = settings.get_connection(conpath) + except Exception as e: + raise UnknownConnectionException("Connection not found") + + hash = connection.GetSettings() + s_con = hash['connection'] + device = None for d in self.devices: if d.path == devpath: device = d break + if not device and s_con['type'] == 'vlan': + ifname = s_con['interface-name'] + device = VlanDevice(self._bus, ifname) + self.add_device(device) if not device: raise UnknownDeviceException("No device found for the requested iface.") - try: - connection = settings.get_connection(conpath) - except Exception as e: - raise UnknownConnectionException("Connection not found") - # See if we need secrets. For the moment, we only support WPA - hash = connection.GetSettings() if hash.has_key('802-11-wireless-security'): s_wsec = hash['802-11-wireless-security'] if (s_wsec['key-mgmt'] == 'wpa-psk' and not s_wsec.has_key('psk')): |