260437ae08697a9d4628c9822cce27da6e75fb50
[src/drm-lease-manager.git] / drm-lease-manager / test / test-socket-client.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "test-socket-client.h"
17
18 #include <check.h>
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30
31 #include "socket-path.h"
32
33 #define DEFAULT_RECV_TIMEOUT (100) // timeout in ms to receive data from server
34
35 struct client_state {
36         pthread_t tid;
37
38         int socket_fd;
39         struct test_config *config;
40 };
41
42 static void client_gst_socket_status(int socket_fd, struct test_config *config)
43 {
44
45         config->connection_completed = true;
46
47         struct pollfd pfd = {.fd = socket_fd, .events = POLLIN};
48         if (poll(&pfd, 1, config->recv_timeout) <= 0)
49                 return;
50
51         if (pfd.revents & POLLHUP)
52                 config->connection_completed = false;
53
54         if (pfd.revents & POLLIN)
55                 config->has_data = true;
56
57         return;
58 }
59
60 static int receive_fd_from_socket(int sockfd)
61 {
62         union {
63                 struct cmsghdr align;
64                 char buf[CMSG_SPACE(sizeof(int))];
65         } u;
66
67         char data;
68         struct iovec iov = {.iov_base = &data, .iov_len = sizeof(data)};
69         struct msghdr msg = {
70             .msg_iov = &iov,
71             .msg_iovlen = 1,
72             .msg_control = u.buf,
73             .msg_controllen = sizeof(u.buf),
74         };
75
76         if (recvmsg(sockfd, &msg, 0) < 0)
77                 return -1;
78
79         int recv_fd = -1;
80         for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
81              cmsg = CMSG_NXTHDR(&msg, cmsg)) {
82                 ck_assert_int_eq(cmsg->cmsg_level, SOL_SOCKET);
83
84                 if (cmsg->cmsg_type != SCM_RIGHTS)
85                         continue;
86
87                 int nfds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
88                 ck_assert_int_eq(nfds, 1);
89                 recv_fd = *(int *)CMSG_DATA(cmsg);
90         }
91         return recv_fd;
92 }
93
94 static void *test_client_thread(void *arg)
95 {
96         struct client_state *cstate = arg;
97         struct test_config *config = cstate->config;
98
99         struct sockaddr_un address = {
100             .sun_family = AF_UNIX,
101         };
102
103         ck_assert_int_eq(
104             sockaddr_set_lease_server_path(&address, config->lease->name),
105             true);
106
107         int client = socket(PF_UNIX, SOCK_STREAM, 0);
108         ck_assert_int_ge(client, 0);
109
110         int ret;
111         ret = connect(client, (struct sockaddr *)&address, sizeof(address));
112         if (ret != 0) {
113                 printf("Connect failed;: %s\n", strerror(errno));
114                 close(client);
115                 return NULL;
116         }
117
118         if (!config->recv_timeout)
119                 config->recv_timeout = DEFAULT_RECV_TIMEOUT;
120
121         client_gst_socket_status(client, config);
122
123         if (config->has_data) {
124                 config->received_fd = receive_fd_from_socket(client);
125         }
126
127         cstate->socket_fd = client;
128
129         return NULL;
130 }
131
132 struct client_state *test_client_start(struct test_config *test_config)
133 {
134         struct client_state *cstate = malloc(sizeof(*cstate));
135
136         *cstate = (struct client_state){
137             .config = test_config,
138         };
139
140         pthread_create(&cstate->tid, NULL, test_client_thread, cstate);
141
142         return cstate;
143 }
144
145 void test_client_stop(struct client_state *cstate)
146 {
147
148         ck_assert_ptr_ne(cstate, NULL);
149
150         pthread_join(cstate->tid, NULL);
151
152         if (cstate->socket_fd >= 0)
153                 close(cstate->socket_fd);
154
155         free(cstate);
156 }
157
158 void test_config_cleanup(struct test_config *config)
159 {
160         if (config->has_data && config->received_fd >= 0)
161                 close(config->received_fd);
162 }