Add lease request and release protocol
[src/drm-lease-manager.git] / libdlmclient / test / test-socket-server.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-server.h"
17 #include <check.h>
18
19 #include <pthread.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include "dlm-protocol.h"
29 #include "socket-path.h"
30 #include "test-helpers.h"
31
32 static void send_fd_list_over_socket(int socket, int nfds, int *fds)
33 {
34         char data;
35         struct iovec iov = {
36             .iov_base = &data,
37             .iov_len = 1,
38         };
39
40         int bufsize = CMSG_SPACE(nfds * sizeof(int));
41         char *buf = malloc(bufsize);
42
43         struct msghdr msg = {
44             .msg_iov = &iov,
45             .msg_iovlen = 1,
46             .msg_controllen = bufsize,
47             .msg_control = buf,
48         };
49
50         struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
51         cmsg->cmsg_level = SOL_SOCKET;
52         cmsg->cmsg_type = SCM_RIGHTS;
53         cmsg->cmsg_len = CMSG_LEN(nfds * sizeof(int));
54         memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * nfds);
55
56         ck_assert_int_gt(sendmsg(socket, &msg, 0), 0);
57         free(buf);
58 }
59
60 static void expect_client_command(int socket, enum dlm_opcode opcode)
61 {
62         struct dlm_client_request req;
63         ck_assert_int_eq(receive_dlm_client_request(socket, &req), true);
64         ck_assert_int_eq(req.opcode, opcode);
65 }
66
67 struct server_state {
68         pthread_t tid;
69         pthread_mutex_t lock;
70         pthread_cond_t cond;
71         bool is_server_started;
72
73         struct test_config *config;
74 };
75
76 static void *test_server_thread(void *arg)
77 {
78         struct server_state *sstate = arg;
79         struct test_config *config = sstate->config;
80
81         struct sockaddr_un address = {
82             .sun_family = AF_UNIX,
83         };
84
85         ck_assert_int_eq(
86             sockaddr_set_lease_server_path(&address, config->lease_name), true);
87
88         int server = socket(PF_UNIX, SOCK_SEQPACKET, 0);
89         ck_assert_int_ge(server, 0);
90
91         unlink(address.sun_path);
92
93         int ret;
94         ret = bind(server, (struct sockaddr *)&address, sizeof(address));
95         ck_assert_int_eq(ret, 0);
96
97         ret = listen(server, 1);
98         ck_assert_int_eq(ret, 0);
99
100         sstate->is_server_started = true;
101         pthread_cond_signal(&sstate->cond);
102
103         int client = accept(server, NULL, NULL);
104         /* accept is the cancellation point for this thread. If
105          * pthread_cancel() is called on this thread, accept() may return
106          * -1, so don't assert on it. */
107
108         if (client < 0) {
109                 close(server);
110                 return NULL;
111         }
112
113         expect_client_command(client, DLM_GET_LEASE);
114
115         if (config->send_no_data)
116                 goto done;
117
118         if (config->send_data_without_fd) {
119                 char data;
120                 write(client, &data, 1);
121                 goto done;
122         }
123
124         if (config->nfds == 0)
125                 config->nfds = 1;
126
127         config->fds = calloc(config->nfds, sizeof(int));
128
129         for (int i = 0; i < config->nfds; i++)
130                 config->fds[i] = get_dummy_fd();
131
132         send_fd_list_over_socket(client, config->nfds, config->fds);
133         expect_client_command(client, DLM_RELEASE_LEASE);
134 done:
135         close(client);
136         close(server);
137         return NULL;
138 }
139
140 struct server_state *test_server_start(struct test_config *test_config)
141 {
142         struct server_state *sstate = malloc(sizeof(*sstate));
143
144         *sstate = (struct server_state){
145             .lock = PTHREAD_MUTEX_INITIALIZER,
146             .cond = PTHREAD_COND_INITIALIZER,
147             .is_server_started = false,
148             .config = test_config,
149         };
150
151         pthread_create(&sstate->tid, NULL, test_server_thread, sstate);
152
153         pthread_mutex_lock(&sstate->lock);
154         while (!sstate->is_server_started)
155                 pthread_cond_wait(&sstate->cond, &sstate->lock);
156         pthread_mutex_unlock(&sstate->lock);
157         return sstate;
158 }
159
160 void test_server_stop(struct server_state *sstate)
161 {
162
163         ck_assert_ptr_ne(sstate, NULL);
164
165         pthread_cancel(sstate->tid);
166         pthread_join(sstate->tid, NULL);
167
168         free(sstate);
169 }
170
171 void test_config_cleanup(struct test_config *config)
172 {
173         if (!config->fds)
174                 return;
175
176         for (int i = 0; i < config->nfds; i++)
177                 close(config->fds[i]);
178
179         free(config->fds);
180 }