Add lease request and release protocol
[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 "dlm-protocol.h"
32 #include "socket-path.h"
33
34 #define DEFAULT_RECV_TIMEOUT (100) // timeout in ms to receive data from server
35
36 struct client_state {
37         pthread_t tid;
38
39         int socket_fd;
40         struct test_config *config;
41 };
42
43 static void send_lease_request(int socket, enum dlm_opcode opcode)
44 {
45         struct dlm_client_request req = {
46             .opcode = opcode,
47         };
48         send_dlm_client_request(socket, &req);
49 }
50
51 static void client_gst_socket_status(int socket_fd, struct test_config *config)
52 {
53
54         config->connection_completed = true;
55
56         struct pollfd pfd = {.fd = socket_fd, .events = POLLIN};
57         if (poll(&pfd, 1, config->recv_timeout) <= 0)
58                 return;
59
60         if (pfd.revents & POLLHUP)
61                 config->connection_completed = false;
62
63         if (pfd.revents & POLLIN)
64                 config->has_data = true;
65
66         return;
67 }
68
69 static int receive_fd_from_socket(int sockfd)
70 {
71         union {
72                 struct cmsghdr align;
73                 char buf[CMSG_SPACE(sizeof(int))];
74         } u;
75
76         char data;
77         struct iovec iov = {.iov_base = &data, .iov_len = sizeof(data)};
78         struct msghdr msg = {
79             .msg_iov = &iov,
80             .msg_iovlen = 1,
81             .msg_control = u.buf,
82             .msg_controllen = sizeof(u.buf),
83         };
84
85         if (recvmsg(sockfd, &msg, 0) < 0)
86                 return -1;
87
88         int recv_fd = -1;
89         for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
90              cmsg = CMSG_NXTHDR(&msg, cmsg)) {
91                 ck_assert_int_eq(cmsg->cmsg_level, SOL_SOCKET);
92
93                 if (cmsg->cmsg_type != SCM_RIGHTS)
94                         continue;
95
96                 int nfds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
97                 ck_assert_int_eq(nfds, 1);
98                 recv_fd = *(int *)CMSG_DATA(cmsg);
99         }
100         return recv_fd;
101 }
102
103 static void *test_client_thread(void *arg)
104 {
105         struct client_state *cstate = arg;
106         struct test_config *config = cstate->config;
107
108         struct sockaddr_un address = {
109             .sun_family = AF_UNIX,
110         };
111
112         ck_assert_int_eq(
113             sockaddr_set_lease_server_path(&address, config->lease->name),
114             true);
115
116         int client = socket(PF_UNIX, SOCK_SEQPACKET, 0);
117         ck_assert_int_ge(client, 0);
118
119         int ret;
120         ret = connect(client, (struct sockaddr *)&address, sizeof(address));
121         if (ret != 0) {
122                 printf("Connect failed;: %s\n", strerror(errno));
123                 close(client);
124                 return NULL;
125         }
126
127         send_lease_request(client, DLM_GET_LEASE);
128
129         if (!config->recv_timeout)
130                 config->recv_timeout = DEFAULT_RECV_TIMEOUT;
131
132         client_gst_socket_status(client, config);
133
134         if (config->has_data) {
135                 config->received_fd = receive_fd_from_socket(client);
136         }
137
138         cstate->socket_fd = client;
139         send_lease_request(client, DLM_RELEASE_LEASE);
140
141         return NULL;
142 }
143
144 struct client_state *test_client_start(struct test_config *test_config)
145 {
146         struct client_state *cstate = malloc(sizeof(*cstate));
147
148         *cstate = (struct client_state){
149             .config = test_config,
150         };
151
152         pthread_create(&cstate->tid, NULL, test_client_thread, cstate);
153
154         return cstate;
155 }
156
157 void test_client_stop(struct client_state *cstate)
158 {
159
160         ck_assert_ptr_ne(cstate, NULL);
161
162         pthread_join(cstate->tid, NULL);
163
164         if (cstate->socket_fd >= 0)
165                 close(cstate->socket_fd);
166
167         free(cstate);
168 }
169
170 void test_config_cleanup(struct test_config *config)
171 {
172         if (config->has_data && config->received_fd >= 0)
173                 close(config->received_fd);
174 }