kdbus: test suite changed to common format
[platform/kernel/linux-rpi.git] / tools / testing / selftests / kdbus / test-monitor.c
index ae87d87..a5918e5 100644 (file)
@@ -22,7 +22,7 @@
 #include "kdbus-enum.h"
 #include "kdbus-test.h"
 
-int kdbus_test_monitor(struct kdbus_test_env *env)
+wur int kdbus_test_monitor(struct kdbus_test_env *env)
 {
        struct kdbus_conn *monitor, *conn;
        unsigned int cookie = 0xdeadbeef;
@@ -30,148 +30,125 @@ int kdbus_test_monitor(struct kdbus_test_env *env)
        uint64_t offset = 0;
        int ret;
 
-       conn = kdbus_hello(env->buspath, 0, NULL, 0);
-       ASSERT_RETURN(conn);
+       ASSERT_NONZERO(conn = kdbus_hello(env->buspath, 0, NULL, 0));
 
        /* add matches to make sure the monitor do not trigger an item add or
         * remove on connect and disconnect, respectively.
         */
-       ret = kdbus_add_match_id(conn, 0x1, KDBUS_ITEM_ID_ADD,
-                                KDBUS_MATCH_ID_ANY);
-       ASSERT_RETURN(ret == 0);
-
-       ret = kdbus_add_match_id(conn, 0x2, KDBUS_ITEM_ID_REMOVE,
-                                KDBUS_MATCH_ID_ANY);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_add_match_id(conn, 0x1, KDBUS_ITEM_ID_ADD, KDBUS_MATCH_ID_ANY));
+       ASSERT_ZERO(kdbus_add_match_id(conn, 0x2, KDBUS_ITEM_ID_REMOVE, KDBUS_MATCH_ID_ANY));
 
        /* register a monitor */
-       monitor = kdbus_hello(env->buspath, KDBUS_HELLO_MONITOR, NULL, 0);
-       ASSERT_RETURN(monitor);
+       ASSERT_NONZERO(monitor = kdbus_hello(env->buspath, KDBUS_HELLO_MONITOR, NULL, 0));
 
        /* make sure we did not receive a monitor connect notification */
-       ret = kdbus_msg_recv(conn, &msg, &offset);
-       ASSERT_RETURN(ret == -EAGAIN);
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
        /* check that a monitor cannot acquire a name */
-       ret = kdbus_name_acquire(monitor, "foo.bar.baz", NULL);
-       ASSERT_RETURN(ret == -EOPNOTSUPP);
+       ASSERT_RETURN(-EOPNOTSUPP,==,kdbus_name_acquire(monitor, "foo.bar.baz", NULL));
 
-       ret = kdbus_msg_send(env->conn, NULL, cookie, 0, 0,  0, conn->id,
-                            0, NULL);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_msg_send(env->conn, NULL, cookie, 0, 0,  0, conn->id));
 
        /* the recipient should have gotten the message */
-       ret = kdbus_msg_recv(conn, &msg, &offset);
-       ASSERT_RETURN(ret == 0);
-       ASSERT_RETURN(msg->cookie == cookie);
+       ASSERT_ZERO(kdbus_msg_recv(conn, &msg, &offset));
+       ASSERT_RETURN(msg->cookie,==,cookie);
        kdbus_msg_free(msg);
-       kdbus_free(conn, offset);
+       ASSERT_ZERO(kdbus_free(conn, offset));
 
        /* and so should the monitor */
-       ret = kdbus_msg_recv(monitor, &msg, &offset);
-       ASSERT_RETURN(ret == 0);
-       ASSERT_RETURN(msg->cookie == cookie);
+       ASSERT_ZERO(kdbus_msg_recv(monitor, &msg, &offset));
+       ASSERT_RETURN(msg->cookie,==,cookie);
 
        kdbus_msg_free(msg);
-       kdbus_free(monitor, offset);
+       ASSERT_ZERO(kdbus_free(monitor, offset));
+
+       /* make sure there are no pending messages */
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
-       /* Installing matches for monitors must fais must fail */
-       ret = kdbus_add_match_empty(monitor);
-       ASSERT_RETURN(ret == -EOPNOTSUPP);
+       /* Installing matches for monitors must fail */
+       ASSERT_RETURN(-EOPNOTSUPP,==,kdbus_add_match_empty(monitor));
 
        cookie++;
-       ret = kdbus_msg_send(env->conn, NULL, cookie, 0, 0, 0,
-                            KDBUS_DST_ID_BROADCAST, 0, NULL);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_msg_send(env->conn, NULL, cookie, 0, 0, 0, KDBUS_DST_ID_BROADCAST));
+
+       /* make sure there are no pending messages */
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
        /* The monitor should get the message. */
-       ret = kdbus_msg_recv_poll(monitor, 100, &msg, &offset);
-       ASSERT_RETURN(ret == 0);
-       ASSERT_RETURN(msg->cookie == cookie);
+       ASSERT_ZERO(kdbus_msg_recv_poll(monitor, 100, &msg, &offset));
+       ASSERT_RETURN(msg->cookie,==,cookie);
 
        kdbus_msg_free(msg);
-       kdbus_free(monitor, offset);
+       ASSERT_ZERO(kdbus_free(monitor, offset));
 
        /*
         * Since we are the only monitor, update the attach flags
-        * and tell we are not interessted in attach flags recv
+        * and tell we are not interested in attach flags recv
         */
 
-       ret = kdbus_conn_update_attach_flags(monitor,
-                                            _KDBUS_ATTACH_ALL,
-                                            0);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_conn_update_attach_flags(monitor, _KDBUS_ATTACH_ALL, 0));
 
        cookie++;
-       ret = kdbus_msg_send(env->conn, NULL, cookie, 0, 0, 0,
-                            KDBUS_DST_ID_BROADCAST, 0, NULL);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_msg_send(env->conn, NULL, cookie, 0, 0, 0, KDBUS_DST_ID_BROADCAST));
 
-       ret = kdbus_msg_recv_poll(monitor, 100, &msg, &offset);
-       ASSERT_RETURN(ret == 0);
-       ASSERT_RETURN(msg->cookie == cookie);
+       /* make sure there are no pending messages */
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
-       ret = kdbus_item_in_message(msg, KDBUS_ITEM_TIMESTAMP);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_msg_recv_poll(monitor, 100, &msg, &offset));
+       ASSERT_RETURN(msg->cookie,==,cookie);
+
+       ASSERT_ZERO(kdbus_item_in_message(msg, KDBUS_ITEM_TIMESTAMP));
 
        kdbus_msg_free(msg);
-       kdbus_free(monitor, offset);
+       ASSERT_ZERO(kdbus_free(monitor, offset));
 
        /*
         * Now we are interested in KDBUS_ITEM_TIMESTAMP and
         * KDBUS_ITEM_CREDS
         */
-       ret = kdbus_conn_update_attach_flags(monitor,
-                                            _KDBUS_ATTACH_ALL,
-                                            KDBUS_ATTACH_TIMESTAMP |
-                                            KDBUS_ATTACH_CREDS);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_conn_update_attach_flags(monitor, _KDBUS_ATTACH_ALL, KDBUS_ATTACH_TIMESTAMP | KDBUS_ATTACH_CREDS));
 
        cookie++;
-       ret = kdbus_msg_send(env->conn, NULL, cookie, 0, 0, 0,
-                            KDBUS_DST_ID_BROADCAST, 0, NULL);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_msg_send(env->conn, NULL, cookie, 0, 0, 0, KDBUS_DST_ID_BROADCAST));
+
+       /* make sure there are no pending messages */
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
-       ret = kdbus_msg_recv_poll(monitor, 100, &msg, &offset);
-       ASSERT_RETURN(ret == 0);
-       ASSERT_RETURN(msg->cookie == cookie);
+       ASSERT_ZERO(kdbus_msg_recv_poll(monitor, 100, &msg, &offset));
+       ASSERT_RETURN(msg->cookie,==,cookie);
 
-       ret = kdbus_item_in_message(msg, KDBUS_ITEM_TIMESTAMP);
-       ASSERT_RETURN(ret == 1);
+       ASSERT_RETURN(1,==,kdbus_item_in_message(msg, KDBUS_ITEM_TIMESTAMP));
 
-       ret = kdbus_item_in_message(msg, KDBUS_ITEM_CREDS);
-       ASSERT_RETURN(ret == 1);
+       ASSERT_RETURN(1,==,kdbus_item_in_message(msg, KDBUS_ITEM_CREDS));
 
        /* the KDBUS_ITEM_PID_COMM was not requested */
-       ret = kdbus_item_in_message(msg, KDBUS_ITEM_PID_COMM);
-       ASSERT_RETURN(ret == 0);
+       ASSERT_ZERO(kdbus_item_in_message(msg, KDBUS_ITEM_PID_COMM));
 
        kdbus_msg_free(msg);
-       kdbus_free(monitor, offset);
+       ASSERT_ZERO(kdbus_free(monitor, offset));
+
+       /* make sure there are no pending messages */
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
        kdbus_conn_free(monitor);
        /* make sure we did not receive a monitor disconnect notification */
-       ret = kdbus_msg_recv(conn, &msg, &offset);
-       ASSERT_RETURN(ret == -EAGAIN);
+       ASSERT_RETURN(-EAGAIN,==,kdbus_msg_recv(conn, &msg, &offset));
 
        kdbus_conn_free(conn);
 
        /* Make sure that monitor as unprivileged is not allowed */
        ret = test_is_capable(CAP_SETUID, CAP_SETGID, -1);
-       ASSERT_RETURN(ret >= 0);
+       ASSERT_RETURN(ret,>=,0);
 
-       if (ret && all_uids_gids_are_mapped()) {
-               ret = RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_UID, ({
-                       monitor = kdbus_hello(env->buspath,
-                                             KDBUS_HELLO_MONITOR,
-                                             NULL, 0);
-                       ASSERT_EXIT(!monitor && errno == EPERM);
+       if (ret && all_uids_gids_are_mapped())
+               RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_UID, ({
+                       monitor = kdbus_hello(env->buspath, KDBUS_HELLO_MONITOR, NULL, 0);
+                       ASSERT_EXIT(errno,==,EPERM);
+                       ASSERT_EXIT_ZERO(monitor);
 
-                       _exit(EXIT_SUCCESS);
+                       exit(EXIT_SUCCESS);
                }),
-               ({ 0; }));
-               ASSERT_RETURN(ret == 0);
-       }
+               ({}));
 
        return TEST_OK;
 }