test-fd: check if we did receive the right number of passed fds
authorDjalal Harouni <tixxdz@opendz.org>
Tue, 28 Oct 2014 10:44:17 +0000 (11:44 +0100)
committerDjalal Harouni <tixxdz@opendz.org>
Tue, 28 Oct 2014 10:44:17 +0000 (11:44 +0100)
Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test/test-fd.c

index 48336eacae2740fecd0395a64d8bfde7eb483bfd..98d0f2c637378d8864b59b85f779b499cce71a36 100644 (file)
@@ -184,10 +184,37 @@ static int send_fds_memfds(struct kdbus_conn *conn, uint64_t dst_id,
        return ret;
 }
 
+/* Return the number of received fds */
+static unsigned int kdbus_item_get_nfds(struct kdbus_msg *msg)
+{
+       unsigned int fds = 0;
+       const struct kdbus_item *item;
+
+       KDBUS_ITEM_FOREACH(item, msg, items) {
+               switch (item->type) {
+               case KDBUS_ITEM_FDS: {
+                       fds += (item->size - KDBUS_ITEM_HEADER_SIZE) /
+                               sizeof(int);
+                       break;
+               }
+
+               case KDBUS_ITEM_PAYLOAD_MEMFD:
+                       fds++;
+                       break;
+
+               default:
+                       break;
+               }
+       }
+
+       return fds;
+}
+
 static int kdbus_send_multiple_fds(struct kdbus_conn *conn_src,
                                   struct kdbus_conn *conn_dst)
 {
        int ret, i;
+       unsigned int nfds;
        int fds[KDBUS_MSG_MAX_FDS + 1];
        int memfds[KDBUS_MSG_MAX_ITEMS + 1];
        struct kdbus_msg *msg;
@@ -211,6 +238,10 @@ static int kdbus_send_multiple_fds(struct kdbus_conn *conn_src,
        ret = kdbus_msg_recv(conn_dst, &msg, NULL);
        ASSERT_RETURN(ret == 0);
 
+       /* Check we got the right number of fds */
+       nfds = kdbus_item_get_nfds(msg);
+       ASSERT_RETURN(nfds == KDBUS_MSG_MAX_FDS);
+
        kdbus_msg_free(msg);
 
        for (i = 0; i < KDBUS_MSG_MAX_ITEMS + 1; i++, dummy_value++) {
@@ -233,6 +264,10 @@ static int kdbus_send_multiple_fds(struct kdbus_conn *conn_src,
        ret = kdbus_msg_recv(conn_dst, &msg, NULL);
        ASSERT_RETURN(ret == 0);
 
+       /* Check we got the right number of fds */
+       nfds = kdbus_item_get_nfds(msg);
+       ASSERT_RETURN(nfds == KDBUS_MSG_MAX_ITEMS);
+
        kdbus_msg_free(msg);
 
 
@@ -255,8 +290,11 @@ static int kdbus_send_multiple_fds(struct kdbus_conn *conn_src,
        ret = kdbus_msg_recv(conn_dst, &msg, NULL);
        ASSERT_RETURN(ret == 0);
 
-       kdbus_msg_free(msg);
+       /* Check we got the right number of fds */
+       nfds = kdbus_item_get_nfds(msg);
+       ASSERT_RETURN(nfds == 253);
 
+       kdbus_msg_free(msg);
 
        for (i = 0; i < KDBUS_MSG_MAX_FDS + 1; i++)
                close(fds[i]);