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