summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kdbus/test-chat.c
blob: 41e5b53fe0cc94876d459e5c110ef2355ba6f0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <poll.h>
#include <stdbool.h>

#include "kdbus-test.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"

int kdbus_test_chat(struct kdbus_test_env *env)
{
	int ret, cookie;
	struct kdbus_conn *conn_a, *conn_b;
	struct pollfd fds[2];
	uint64_t flags;
	int count;

	conn_a = kdbus_hello(env->buspath, 0, NULL, 0);
	conn_b = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn_a && conn_b);

	flags = KDBUS_NAME_ALLOW_REPLACEMENT;
	ret = kdbus_name_acquire(conn_a, "foo.bar.test", &flags);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(conn_a, "foo.bar.baz", NULL);
	ASSERT_RETURN(ret == 0);

	flags = KDBUS_NAME_QUEUE;
	ret = kdbus_name_acquire(conn_b, "foo.bar.baz", &flags);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
	ASSERT_RETURN(ret == 0);

	flags = 0;
	ret = kdbus_name_acquire(conn_a, "foo.bar.double", &flags);
	ASSERT_RETURN(ret == 0);
	ASSERT_RETURN(!(flags & KDBUS_NAME_ACQUIRED));

	ret = kdbus_name_release(conn_a, "foo.bar.double");
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_release(conn_a, "foo.bar.double");
	ASSERT_RETURN(ret == -ESRCH);

	ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
				 KDBUS_LIST_NAMES  |
				 KDBUS_LIST_QUEUED |
				 KDBUS_LIST_ACTIVATORS);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_empty(conn_a);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_empty(conn_b);
	ASSERT_RETURN(ret == 0);

	cookie = 0;
	ret = kdbus_msg_send(conn_b, NULL, 0xc0000000 | cookie, 0, 0, 0,
			     KDBUS_DST_ID_BROADCAST);
	ASSERT_RETURN(ret == 0);

	fds[0].fd = conn_a->fd;
	fds[1].fd = conn_b->fd;

	kdbus_printf("-- entering poll loop ...\n");

	for (count = 0;; count++) {
		int i, nfds = sizeof(fds) / sizeof(fds[0]);

		for (i = 0; i < nfds; i++) {
			fds[i].events = POLLIN | POLLPRI | POLLHUP;
			fds[i].revents = 0;
		}

		ret = poll(fds, nfds, 3000);
		ASSERT_RETURN(ret >= 0);

		if (fds[0].revents & POLLIN) {
			if (count > 2)
				kdbus_name_release(conn_a, "foo.bar.baz");

			ret = kdbus_msg_recv(conn_a, NULL, NULL);
			ASSERT_RETURN(ret == 0);
			ret = kdbus_msg_send(conn_a, NULL,
					     0xc0000000 | cookie++,
					     0, 0, 0, conn_b->id);
			ASSERT_RETURN(ret == 0);
		}

		if (fds[1].revents & POLLIN) {
			ret = kdbus_msg_recv(conn_b, NULL, NULL);
			ASSERT_RETURN(ret == 0);
			ret = kdbus_msg_send(conn_b, NULL,
					     0xc0000000 | cookie++,
					     0, 0, 0, conn_a->id);
			ASSERT_RETURN(ret == 0);
		}

		ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
					 KDBUS_LIST_NAMES  |
					 KDBUS_LIST_QUEUED |
					 KDBUS_LIST_ACTIVATORS);
		ASSERT_RETURN(ret == 0);

		if (count > 10)
			break;
	}

	kdbus_printf("-- closing bus connections\n");
	kdbus_conn_free(conn_a);
	kdbus_conn_free(conn_b);

	return TEST_OK;
}