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-client.h"
27 #include <sys/socket.h>
31 #include "socket-path.h"
33 #define DEFAULT_RECV_TIMEOUT (100) // timeout in ms to receive data from server
39 struct test_config *config;
42 static void client_gst_socket_status(int socket_fd, struct test_config *config)
45 config->connection_completed = true;
47 struct pollfd pfd = {.fd = socket_fd, .events = POLLIN};
48 if (poll(&pfd, 1, config->recv_timeout) <= 0)
51 if (pfd.revents & POLLHUP)
52 config->connection_completed = false;
54 if (pfd.revents & POLLIN)
55 config->has_data = true;
60 static int receive_fd_from_socket(int sockfd)
64 char buf[CMSG_SPACE(sizeof(int))];
68 struct iovec iov = {.iov_base = &data, .iov_len = sizeof(data)};
73 .msg_controllen = sizeof(u.buf),
76 if (recvmsg(sockfd, &msg, 0) < 0)
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);
84 if (cmsg->cmsg_type != SCM_RIGHTS)
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);
94 static void *test_client_thread(void *arg)
96 struct client_state *cstate = arg;
97 struct test_config *config = cstate->config;
99 struct sockaddr_un address = {
100 .sun_family = AF_UNIX,
104 sockaddr_set_lease_server_path(&address, config->lease->name),
107 int client = socket(PF_UNIX, SOCK_STREAM, 0);
108 ck_assert_int_ge(client, 0);
111 ret = connect(client, (struct sockaddr *)&address, sizeof(address));
113 printf("Connect failed;: %s\n", strerror(errno));
118 if (!config->recv_timeout)
119 config->recv_timeout = DEFAULT_RECV_TIMEOUT;
121 client_gst_socket_status(client, config);
123 if (config->has_data) {
124 config->received_fd = receive_fd_from_socket(client);
127 cstate->socket_fd = client;
132 struct client_state *test_client_start(struct test_config *test_config)
134 struct client_state *cstate = malloc(sizeof(*cstate));
136 *cstate = (struct client_state){
137 .config = test_config,
140 pthread_create(&cstate->tid, NULL, test_client_thread, cstate);
145 void test_client_stop(struct client_state *cstate)
148 ck_assert_ptr_ne(cstate, NULL);
150 pthread_join(cstate->tid, NULL);
152 if (cstate->socket_fd >= 0)
153 close(cstate->socket_fd);
158 void test_config_cleanup(struct test_config *config)
160 if (config->has_data && config->received_fd >= 0)
161 close(config->received_fd);