kdbus: the driver, original and non-working
[platform/kernel/linux-rpi.git] / tools / testing / selftests / kdbus / test-timeout.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <assert.h>
11 #include <poll.h>
12 #include <stdbool.h>
13
14 #include "kdbus-api.h"
15 #include "kdbus-test.h"
16 #include "kdbus-util.h"
17 #include "kdbus-enum.h"
18
19 int timeout_msg_recv(struct kdbus_conn *conn, uint64_t *expected)
20 {
21         struct kdbus_cmd_recv recv = { .size = sizeof(recv) };
22         struct kdbus_msg *msg;
23         int ret;
24
25         ret = kdbus_cmd_recv(conn->fd, &recv);
26         if (ret < 0) {
27                 kdbus_printf("error receiving message: %d (%m)\n", ret);
28                 return ret;
29         }
30
31         msg = (struct kdbus_msg *)(conn->buf + recv.msg.offset);
32
33         ASSERT_RETURN_VAL(msg->payload_type == KDBUS_PAYLOAD_KERNEL, -EINVAL);
34         ASSERT_RETURN_VAL(msg->src_id == KDBUS_SRC_ID_KERNEL, -EINVAL);
35         ASSERT_RETURN_VAL(msg->dst_id == conn->id, -EINVAL);
36
37         *expected &= ~(1ULL << msg->cookie_reply);
38         kdbus_printf("Got message timeout for cookie %llu\n",
39                      msg->cookie_reply);
40
41         ret = kdbus_free(conn, recv.msg.offset);
42         if (ret < 0)
43                 return ret;
44
45         return 0;
46 }
47
48 int kdbus_test_timeout(struct kdbus_test_env *env)
49 {
50         struct kdbus_conn *conn_a, *conn_b;
51         struct pollfd fd;
52         int ret, i, n_msgs = 4;
53         uint64_t expected = 0;
54         uint64_t cookie = 0xdeadbeef;
55
56         conn_a = kdbus_hello(env->buspath, 0, NULL, 0);
57         conn_b = kdbus_hello(env->buspath, 0, NULL, 0);
58         ASSERT_RETURN(conn_a && conn_b);
59
60         fd.fd = conn_b->fd;
61
62         /*
63          * send messages that expect a reply (within 100 msec),
64          * but never answer it.
65          */
66         for (i = 0; i < n_msgs; i++, cookie++) {
67                 kdbus_printf("Sending message with cookie %llu ...\n",
68                              (unsigned long long)cookie);
69                 ASSERT_RETURN(kdbus_msg_send(conn_b, NULL, cookie,
70                               KDBUS_MSG_EXPECT_REPLY,
71                               (i + 1) * 100ULL * 1000000ULL, 0,
72                               conn_a->id, 0, NULL) == 0);
73                 expected |= 1ULL << cookie;
74         }
75
76         for (;;) {
77                 fd.events = POLLIN | POLLPRI | POLLHUP;
78                 fd.revents = 0;
79
80                 ret = poll(&fd, 1, (n_msgs + 1) * 100);
81                 if (ret == 0)
82                         kdbus_printf("--- timeout\n");
83                 if (ret <= 0)
84                         break;
85
86                 if (fd.revents & POLLIN)
87                         ASSERT_RETURN(!timeout_msg_recv(conn_b, &expected));
88
89                 if (expected == 0)
90                         break;
91         }
92
93         ASSERT_RETURN(expected == 0);
94
95         kdbus_conn_free(conn_a);
96         kdbus_conn_free(conn_b);
97
98         return TEST_OK;
99 }