kdbus: the driver, original and non-working
[platform/kernel/linux-exynos.git] / tools / testing / selftests / kdbus / test-chat.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-test.h"
15 #include "kdbus-util.h"
16 #include "kdbus-enum.h"
17
18 int kdbus_test_chat(struct kdbus_test_env *env)
19 {
20         int ret, cookie;
21         struct kdbus_conn *conn_a, *conn_b;
22         struct pollfd fds[2];
23         uint64_t flags;
24         int count;
25
26         conn_a = kdbus_hello(env->buspath, 0, NULL, 0);
27         conn_b = kdbus_hello(env->buspath, 0, NULL, 0);
28         ASSERT_RETURN(conn_a && conn_b);
29
30         flags = KDBUS_NAME_ALLOW_REPLACEMENT;
31         ret = kdbus_name_acquire(conn_a, "foo.bar.test", &flags);
32         ASSERT_RETURN(ret == 0);
33
34         ret = kdbus_name_acquire(conn_a, "foo.bar.baz", NULL);
35         ASSERT_RETURN(ret == 0);
36
37         flags = KDBUS_NAME_QUEUE;
38         ret = kdbus_name_acquire(conn_b, "foo.bar.baz", &flags);
39         ASSERT_RETURN(ret == 0);
40
41         ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
42         ASSERT_RETURN(ret == 0);
43
44         ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
45         ASSERT_RETURN(ret == -EALREADY);
46
47         ret = kdbus_name_release(conn_a, "foo.bar.double");
48         ASSERT_RETURN(ret == 0);
49
50         ret = kdbus_name_release(conn_a, "foo.bar.double");
51         ASSERT_RETURN(ret == -ESRCH);
52
53         ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
54                                  KDBUS_LIST_NAMES  |
55                                  KDBUS_LIST_QUEUED |
56                                  KDBUS_LIST_ACTIVATORS);
57         ASSERT_RETURN(ret == 0);
58
59         ret = kdbus_add_match_empty(conn_a);
60         ASSERT_RETURN(ret == 0);
61
62         ret = kdbus_add_match_empty(conn_b);
63         ASSERT_RETURN(ret == 0);
64
65         cookie = 0;
66         ret = kdbus_msg_send(conn_b, NULL, 0xc0000000 | cookie, 0, 0, 0,
67                              KDBUS_DST_ID_BROADCAST, 0, NULL);
68         ASSERT_RETURN(ret == 0);
69
70         fds[0].fd = conn_a->fd;
71         fds[1].fd = conn_b->fd;
72
73         kdbus_printf("-- entering poll loop ...\n");
74
75         for (count = 0;; count++) {
76                 int i, nfds = sizeof(fds) / sizeof(fds[0]);
77
78                 for (i = 0; i < nfds; i++) {
79                         fds[i].events = POLLIN | POLLPRI | POLLHUP;
80                         fds[i].revents = 0;
81                 }
82
83                 ret = poll(fds, nfds, 3000);
84                 ASSERT_RETURN(ret >= 0);
85
86                 if (fds[0].revents & POLLIN) {
87                         if (count > 2)
88                                 kdbus_name_release(conn_a, "foo.bar.baz");
89
90                         ret = kdbus_msg_recv(conn_a, NULL, NULL);
91                         ASSERT_RETURN(ret == 0);
92                         ret = kdbus_msg_send(conn_a, NULL,
93                                              0xc0000000 | cookie++,
94                                              0, 0, 0, conn_b->id, 0, NULL);
95                         ASSERT_RETURN(ret == 0);
96                 }
97
98                 if (fds[1].revents & POLLIN) {
99                         ret = kdbus_msg_recv(conn_b, NULL, NULL);
100                         ASSERT_RETURN(ret == 0);
101                         ret = kdbus_msg_send(conn_b, NULL,
102                                              0xc0000000 | cookie++,
103                                              0, 0, 0, conn_a->id, 0, NULL);
104                         ASSERT_RETURN(ret == 0);
105                 }
106
107                 ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
108                                          KDBUS_LIST_NAMES  |
109                                          KDBUS_LIST_QUEUED |
110                                          KDBUS_LIST_ACTIVATORS);
111                 ASSERT_RETURN(ret == 0);
112
113                 if (count > 10)
114                         break;
115         }
116
117         kdbus_printf("-- closing bus connections\n");
118         kdbus_conn_free(conn_a);
119         kdbus_conn_free(conn_b);
120
121         return TEST_OK;
122 }