kdbus: the driver, original and non-working
[platform/kernel/linux-exynos.git] / tools / testing / selftests / kdbus / test-send.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 /* Use in conjunction with test-kdbus-daemon */
7
8 #include <unistd.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <errno.h>
12 #include <assert.h>
13 #include <poll.h>
14 #include <sys/ioctl.h>
15
16 #include "kdbus-test.h"
17 #include "kdbus-util.h"
18 #include "kdbus-enum.h"
19
20 int get_file(const char *fname, int flags, const char *content)
21 {
22         FILE *f;
23
24         if (access(fname, F_OK) < 0) {
25                 f = fopen(fname, "w");
26                 if (!f)
27                         return -1;
28                 fprintf(f, "%s\n", content);
29                 fclose(f);
30         }
31
32         return open(fname, flags);
33 }
34
35 int kdbus_test_send(struct kdbus_test_env *env)
36 {
37         int ret;
38         int serial = 1;
39         int fds[3];
40         size_t i;
41
42         if (!env->conn)
43                 return EXIT_FAILURE;
44
45         fds[0] = get_file("/tmp/kdbus-test-send.rd", O_RDONLY, "foo");
46         fds[1] = get_file("/tmp/kdbus-test-send.wr", O_WRONLY, "bar");
47         fds[2] = get_file("/tmp/kdbus-test-send.rdwr", O_RDWR, "baz");
48
49         for (i = 0; i < ELEMENTSOF(fds); i++) {
50                 if (fds[i] < 0) {
51                         fprintf(stderr, "Unable to open data/fileN file(s)\n");
52                         return EXIT_FAILURE;
53                 }
54         }
55
56         ret = kdbus_msg_send(env->conn, "com.example.kdbus-test", serial++,
57                              0, 0, 0, 0, 0, NULL);
58         if (ret < 0)
59                 fprintf(stderr, "error sending simple message: %d (%m)\n",
60                         ret);
61
62         ret = kdbus_msg_send(env->conn, "com.example.kdbus-test", serial++,
63                              0, 0, 0, 0, 1, fds);
64         if (ret < 0)
65                 fprintf(stderr, "error sending message with 1 fd: %d (%m)\n",
66                         ret);
67
68         ret = kdbus_msg_send(env->conn, "com.example.kdbus-test", serial++,
69                              0, 0, 0, 0, 2, fds);
70         if (ret < 0)
71                 fprintf(stderr, "error sending message with 2 fds: %d (%m)\n",
72                         ret);
73
74         ret = kdbus_msg_send(env->conn, "com.example.kdbus-test", serial++,
75                              0, 0, 0, 0, 3, fds);
76         if (ret < 0)
77                 fprintf(stderr, "error sending message with 3 fds: %d (%m)\n",
78                         ret);
79
80         for (i = 0; i < ELEMENTSOF(fds); i++)
81                 close(fds[i]);
82
83         return EXIT_SUCCESS;
84 }