chiark / gitweb /
test-rtnl: fix typo
[elogind.git] / src / libsystemd-rtnl / test-rtnl.c
index bcfd816688f8d6d98b70b5fea2418a430ae2a93f..912cc66ba7d7e30e5e985ea040e2578f4e35a6f1 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <linux/rtnetlink.h>
 #include <netinet/ether.h>
 
 #include "util.h"
 #include "macro.h"
 #include "sd-rtnl.h"
 #include "socket-util.h"
+#include "rtnl-util.h"
 
 static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
-        __u16 type;
+        uint16_t type;
         const char *mac = "98:fe:94:3f:c6:18", *name = "test";
         unsigned int mtu = 1450;
         void *data;
@@ -51,8 +53,54 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
         assert(type == IFLA_MTU);
         assert(mtu == *(unsigned int *) data);
 
-        /* let's assume that this test is always ran when the loopback device is up, so that it will fail */
-        assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 2 * USEC_PER_SEC, NULL) == 0);
+        assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 0, NULL) == 0);
+}
+
+static void test_route(void) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req;
+        uint32_t addr = htonl(INADDR_LOOPBACK);
+        uint32_t index = 2;
+        uint16_t type;
+        void *data;
+        int r;
+
+        r = sd_rtnl_message_route_new(RTM_NEWROUTE, AF_INET, 0, 0, 0,
+                                      RT_TABLE_MAIN, RT_SCOPE_UNIVERSE, RTPROT_BOOT,
+                                      RTN_UNICAST, 0, &req);
+        if (r < 0) {
+                log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
+                return;
+        }
+
+        r = sd_rtnl_message_append(req, RTA_GATEWAY, &addr);
+        if (r < 0) {
+                log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
+                return;
+        }
+
+        r = sd_rtnl_message_append(req, RTA_OIF, &index);
+        if (r < 0) {
+                log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
+                return;
+        }
+
+        assert(sd_rtnl_message_read(req, &type, &data) > 0);
+        assert(type == RTA_GATEWAY);
+        assert(*(uint32_t *) data == addr);
+
+        assert(sd_rtnl_message_read(req, &type, &data) > 0);
+        assert(type == RTA_OIF);
+        assert(*(uint32_t *) data == index);
+}
+
+static void test_multiple(void) {
+        sd_rtnl *rtnl1, *rtnl2;
+
+        assert(sd_rtnl_open(0, &rtnl1) >= 0);
+        assert(sd_rtnl_open(0, &rtnl2) >= 0);
+
+        rtnl1 = sd_rtnl_unref(rtnl1);
+        rtnl2 = sd_rtnl_unref(rtnl2);
 }
 
 int main(void) {
@@ -61,10 +109,14 @@ int main(void) {
         sd_rtnl_message *r;
         void *data;
         int if_loopback;
-        __u16 type;
+        uint16_t type;
         unsigned int mtu = 0;
         unsigned int *mtu_reply;
 
+        test_multiple();
+
+        test_route();
+
         assert(sd_rtnl_open(0, &rtnl) >= 0);
         assert(rtnl);
 
@@ -81,11 +133,11 @@ int main(void) {
 
         assert(sd_rtnl_message_read(m, &type, &data) == 0);
 
-        assert(sd_rtnl_send_with_reply_and_block(rtnl, m, 100000000, &r) >= 0);
+        assert(sd_rtnl_send_with_reply_and_block(rtnl, m, 0, &r) >= 0);
         assert(sd_rtnl_message_get_type(r, &type) >= 0);
         assert(type == RTM_NEWLINK);
 
-        assert(sd_rtnl_message_read(m, &type, data) == 0);
+        assert(sd_rtnl_message_read(m, &type, &data) == 0);
         assert((r = sd_rtnl_message_unref(r)) == NULL);
 
         assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) == -EPERM);
@@ -101,20 +153,20 @@ int main(void) {
         assert(type == IFLA_MTU);
         assert(*mtu_reply == 0);
 
-        assert(sd_rtnl_message_read(m, &type, data) == 0);
+        assert(sd_rtnl_message_read(m, &type, &data) == 0);
 
         assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) >= 0);
-        while (sd_rtnl_message_read(r, &type, &data)) {
+        while (sd_rtnl_message_read(r, &type, &data) > 0) {
                 switch (type) {
-                        case IFLA_MTU:
-                                assert(*(unsigned int *) data == 65536);
-                                break;;
+//                        case IFLA_MTU:
+//                                assert(*(unsigned int *) data == 65536);
+//                                break;
+//                        case IFLA_QDISC:
+//                                assert(streq((char *) data, "noqueue"));
+//                                break;
                         case IFLA_IFNAME:
                                 assert(streq((char *) data, "lo"));
-                                break;;
-                        case IFLA_QDISC:
-                                assert(streq((char *) data, "noqueue"));
-                                break;;
+                                break;
                 }
         }