1 /* Copyright 2020-2021 IGEL Co., Ltd.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "test-socket-server.h"
24 #include <sys/socket.h>
28 #include "socket-path.h"
29 #include "test-helpers.h"
31 static void send_fd_list_over_socket(int socket, int nfds, int *fds)
39 int bufsize = CMSG_SPACE(nfds * sizeof(int));
40 char *buf = malloc(bufsize);
45 .msg_controllen = bufsize,
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);
55 ck_assert_int_gt(sendmsg(socket, &msg, 0), 0);
63 bool is_server_started;
65 struct test_config *config;
68 static void *test_server_thread(void *arg)
70 struct server_state *sstate = arg;
71 struct test_config *config = sstate->config;
73 struct sockaddr_un address = {
74 .sun_family = AF_UNIX,
78 sockaddr_set_lease_server_path(&address, config->lease_name), true);
80 int server = socket(PF_UNIX, SOCK_STREAM, 0);
81 ck_assert_int_ge(server, 0);
83 unlink(address.sun_path);
86 ret = bind(server, (struct sockaddr *)&address, sizeof(address));
87 ck_assert_int_eq(ret, 0);
89 ret = listen(server, 1);
90 ck_assert_int_eq(ret, 0);
92 sstate->is_server_started = true;
93 pthread_cond_signal(&sstate->cond);
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. */
105 if (config->send_no_data)
108 if (config->send_data_without_fd) {
110 write(client, &data, 1);
114 if (config->nfds == 0)
117 config->fds = calloc(config->nfds, sizeof(int));
119 for (int i = 0; i < config->nfds; i++)
120 config->fds[i] = get_dummy_fd();
122 send_fd_list_over_socket(client, config->nfds, config->fds);
129 struct server_state *test_server_start(struct test_config *test_config)
131 struct server_state *sstate = malloc(sizeof(*sstate));
133 *sstate = (struct server_state){
134 .lock = PTHREAD_MUTEX_INITIALIZER,
135 .cond = PTHREAD_COND_INITIALIZER,
136 .is_server_started = false,
137 .config = test_config,
140 pthread_create(&sstate->tid, NULL, test_server_thread, sstate);
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);
149 void test_server_stop(struct server_state *sstate)
152 ck_assert_ptr_ne(sstate, NULL);
154 pthread_cancel(sstate->tid);
155 pthread_join(sstate->tid, NULL);
160 void test_config_cleanup(struct test_config *config)
165 for (int i = 0; i < config->nfds; i++)
166 close(config->fds[i]);