tests: add a test for connection_compatible() for wired devices
authorJiří Klimeš <jklimes@redhat.com>
Fri, 25 Sep 2015 07:06:20 +0000 (09:06 +0200)
committerJiří Klimeš <jklimes@redhat.com>
Tue, 29 Sep 2015 07:31:41 +0000 (09:31 +0200)
Allow setting MAC address and S390 subchannels for ethernet devices in
testing NM service.

libnm-glib/tests/test-nm-client.c
libnm/tests/common.c
libnm/tests/common.h
libnm/tests/test-nm-client.c
tools/test-networkmanager-service.py

index f3a5811..3b269e0 100644 (file)
@@ -100,6 +100,29 @@ add_device (const char *method, const char *ifname, char **out_path)
        return TRUE;
 }
 
+static gboolean
+add_wired_device (const char *method, const char *ifname, char **out_path)
+{
+       const char *empty[] = { NULL };
+       GError *error = NULL;
+       GVariant *ret;
+
+       ret = g_dbus_proxy_call_sync (sinfo->proxy,
+                                     method,
+                                     g_variant_new ("(ss^as)", ifname, "/", empty),
+                                     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 {
@@ -168,7 +191,7 @@ test_device_added (void)
        g_assert (devices == NULL);
 
        /* Tell the test service to add a new device */
-       add_device ("AddWiredDevice", "eth0", NULL);
+       add_wired_device ("AddWiredDevice", "eth0", NULL);
 
        g_signal_connect (client,
                          "device-added",
@@ -731,8 +754,8 @@ 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]);
+       add_wired_device ("AddWiredDevice", "eth0", &paths[1]);
+       add_wired_device ("AddWiredDevice", "eth1", &paths[2]);
        info.quit_count = 3;
 
        g_signal_connect (client,
index f2cdc59..5b3f74e 100644 (file)
@@ -149,22 +149,53 @@ timeout (gpointer user_data)
        return G_SOURCE_REMOVE;
 }
 
-NMDevice *
-nm_test_service_add_device (NMTestServiceInfo *sinfo, NMClient *client,
-                            const char *method, const char *ifname)
+static GVariant *
+call_add_wired_device (GDBusProxy *proxy, const char *ifname, const char *hwaddr,
+                       const char **subchannels, GError **error)
 {
-       AddDeviceInfo info;
-       GError *error = NULL;
-       GVariant *ret;
-       guint timeout_id;
+       const char *empty[] = { NULL };
+
+       if (!hwaddr)
+               hwaddr = "/";
+       if (!subchannels)
+               subchannels = empty;
+
+       return g_dbus_proxy_call_sync (proxy,
+                                      "AddWiredDevice",
+                                      g_variant_new ("(ss^as)", ifname, hwaddr, subchannels),
+                                      G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                                      3000,
+                                      NULL,
+                                      error);
+}
 
-       ret = g_dbus_proxy_call_sync (sinfo->proxy,
+static GVariant *
+call_add_device (GDBusProxy *proxy, const char *method, const char *ifname, GError **error)
+{
+       return g_dbus_proxy_call_sync (proxy,
                                      method,
                                      g_variant_new ("(s)", ifname),
                                      G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                      3000,
                                      NULL,
-                                     &error);
+                                     error);
+}
+
+static NMDevice *
+add_device_common (NMTestServiceInfo *sinfo, NMClient *client,
+                   const char *method, const char *ifname,
+                   const char *hwaddr, const char **subchannels)
+{
+       AddDeviceInfo info;
+       GError *error = NULL;
+       GVariant *ret;
+       guint timeout_id;
+
+       if (g_strcmp0 (method, "AddWiredDevice") == 0)
+               ret = call_add_wired_device (sinfo->proxy, ifname, hwaddr, subchannels, &error);
+       else
+               ret = call_add_device (sinfo->proxy, method, ifname, &error);
+
        g_assert_no_error (error);
        g_assert (ret);
        g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(o)");
@@ -186,3 +217,18 @@ nm_test_service_add_device (NMTestServiceInfo *sinfo, NMClient *client,
 
        return info.device;
 }
+
+NMDevice *
+nm_test_service_add_device (NMTestServiceInfo *sinfo, NMClient *client,
+                            const char *method, const char *ifname)
+{
+       return add_device_common (sinfo, client, method, ifname, NULL, NULL);
+}
+
+NMDevice *
+nm_test_service_add_wired_device (NMTestServiceInfo *sinfo, NMClient *client,
+                                  const char *ifname, const char *hwaddr,
+                                  const char **subchannels)
+{
+       return add_device_common (sinfo, client, "AddWiredDevice", ifname, hwaddr, subchannels);
+}
index 4919d3f..501ea16 100644 (file)
@@ -36,3 +36,10 @@ NMDevice *nm_test_service_add_device (NMTestServiceInfo *info,
                                       NMClient *client,
                                       const char *method,
                                       const char *ifname);
+
+NMDevice * nm_test_service_add_wired_device (NMTestServiceInfo *sinfo,
+                                             NMClient *client,
+                                             const char *ifname,
+                                             const char *hwaddr,
+                                             const char **subchannels);
+
index ccd952c..9da5647 100644 (file)
@@ -1160,6 +1160,77 @@ test_activate_failed (void)
        g_clear_pointer (&sinfo, nm_test_service_cleanup);
 }
 
+static void
+test_device_connection_compatibility (void)
+{
+       NMClient *client;
+       NMDevice *device1, *device2;
+       NMConnection *conn;
+       NMSettingWired *s_wired;
+       GError *error = NULL;
+       const char *subchannels[] = { "0.0.8000", "0.0.8001", "0.0.8002", NULL };
+       const char *subchannels_2[] = { "0.0.8000", "0.0.8001", NULL };
+       const char *subchannels_x[] = { "0.0.8000", "0.0.8001", "0.0.800X", NULL };
+       const char *hw_addr1 = "52:54:00:ab:db:23";
+       const char *hw_addr2 = "52:54:00:ab:db:24";
+
+       sinfo = nm_test_service_init ();
+       client = nm_client_new (NULL, &error);
+       g_assert_no_error (error);
+
+       /* Create two devices */
+       device1 = nm_test_service_add_wired_device (sinfo, client, "eth0", hw_addr1, subchannels);
+       device2 = nm_test_service_add_wired_device (sinfo, client, "eth1", hw_addr2, NULL);
+
+       g_assert_cmpstr (nm_device_get_hw_address (device1), ==, hw_addr1);
+       g_assert_cmpstr (nm_device_get_hw_address (device2), ==, hw_addr2);
+
+       conn = nmtst_create_minimal_connection ("wired-matches", NULL,
+                                               NM_SETTING_WIRED_SETTING_NAME, NULL);
+       s_wired = nm_connection_get_setting_wired (conn);
+       nm_setting_wired_add_mac_blacklist_item (s_wired, "00:11:22:33:44:55");
+
+       /* device1 and conn are compatible */
+       g_object_set (s_wired,
+                     NM_SETTING_WIRED_MAC_ADDRESS, hw_addr1,
+                     NM_SETTING_WIRED_S390_SUBCHANNELS, subchannels,
+                     NULL);
+       nm_device_connection_compatible (device1, conn, &error);
+       g_assert_no_error (error);
+
+       /* device2 and conn differ in subchannels */
+       g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, subchannels_x, NULL);
+       nm_device_connection_compatible (device2, conn, &error);
+       g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION);
+       g_clear_error (&error);
+
+       /* device1 and conn differ in subchannels - 2 in connection, 3 in device */
+       g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, subchannels_2, NULL);
+       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_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, NULL, NULL);
+
+       /* device2 and conn differ in MAC address */
+       g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, "aa:bb:cc:dd:ee:ee", NULL);
+       nm_device_connection_compatible (device2, conn, &error);
+       g_assert_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION);
+       g_clear_error (&error);
+       g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, NULL, NULL);
+
+       /* device1 is blacklisted in conn */
+       nm_setting_wired_add_mac_blacklist_item (s_wired, hw_addr1);
+       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, nm_test_service_cleanup);
+}
+
 /*******************************************************************/
 
 NMTST_DEFINE ();
@@ -1182,6 +1253,7 @@ main (int argc, char **argv)
        g_test_add_func ("/libnm/active-connections", test_active_connections);
        g_test_add_func ("/libnm/activate-virtual", test_activate_virtual);
        g_test_add_func ("/libnm/activate-failed", test_activate_failed);
+       g_test_add_func ("/libnm/device-connection-compatibility", test_device_connection_compatibility);
 
        return g_test_run ();
 }
index 6035fec..2608100 100755 (executable)
@@ -207,14 +207,19 @@ PE_HW_ADDRESS = "HwAddress"
 PE_PERM_HW_ADDRESS = "PermHwAddress"
 PE_SPEED = "Speed"
 PE_CARRIER = "Carrier"
+PE_S390_SUBCHANNELS = "S390Subchannels"
 
 class WiredDevice(Device):
-    def __init__(self, bus, iface):
+    def __init__(self, bus, iface, mac, subchannels):
         Device.__init__(self, bus, iface, NM_DEVICE_TYPE_ETHERNET)
         self.add_dbus_interface(IFACE_WIRED, self.__get_props)
 
-        self.mac = random_mac()
+        if mac is None:
+            self.mac = random_mac()
+        else:
+            self.mac = mac
         self.carrier = False
+        self.s390_subchannels = subchannels
 
     # Properties interface
     def __get_props(self):
@@ -223,6 +228,7 @@ class WiredDevice(Device):
         props[PE_PERM_HW_ADDRESS] = self.mac
         props[PE_SPEED] = dbus.UInt32(100)
         props[PE_CARRIER] = self.carrier
+        props[PE_S390_SUBCHANNELS] = self.s390_subchannels
         return props
 
     def __notify(self, propname):
@@ -838,12 +844,12 @@ class NetworkManager(ExportedObj):
     def Quit(self):
         mainloop.quit()
 
-    @dbus.service.method(IFACE_TEST, in_signature='s', out_signature='o')
-    def AddWiredDevice(self, ifname):
+    @dbus.service.method(IFACE_TEST, in_signature='ssas', out_signature='o')
+    def AddWiredDevice(self, ifname, mac, subchannels):
         for d in self.devices:
             if d.iface == ifname:
                 raise PermissionDeniedException("Device already added")
-        dev = WiredDevice(self._bus, ifname)
+        dev = WiredDevice(self._bus, ifname, mac, subchannels)
         self.add_device(dev)
         return to_path(dev)