summaryrefslogtreecommitdiffstats
path: root/libdlmclient/test/test-socket-server.c
blob: 6aaa4e43e323d424affd7ba84eaae34d67ee7ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright 2020-2021 IGEL Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "test-socket-server.h"
#include <check.h>

#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include "dlm-protocol.h"
#include "socket-path.h"
#include "test-helpers.h"

static void send_fd_list_over_socket(int socket, int nfds, int *fds)
{
	char data;
	struct iovec iov = {
	    .iov_base = &data,
	    .iov_len = 1,
	};

	int bufsize = CMSG_SPACE(nfds * sizeof(int));
	char *buf = malloc(bufsize);

	struct msghdr msg = {
	    .msg_iov = &iov,
	    .msg_iovlen = 1,
	    .msg_controllen = bufsize,
	    .msg_control = buf,
	};

	struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;
	cmsg->cmsg_len = CMSG_LEN(nfds * sizeof(int));
	memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * nfds);

	ck_assert_int_gt(sendmsg(socket, &msg, 0), 0);
	free(buf);
}

static void expect_client_command(int socket, enum dlm_opcode opcode)
{
	struct dlm_client_request req;
	ck_assert_int_eq(receive_dlm_client_request(socket, &req), true);
	ck_assert_int_eq(req.opcode, opcode);
}

struct server_state {
	pthread_t tid;
	pthread_mutex_t lock;
	pthread_cond_t cond;
	bool is_server_started;

	struct test_config *config;
};

static void *test_server_thread(void *arg)
{
	struct server_state *sstate = arg;
	struct test_config *config = sstate->config;

	struct sockaddr_un address = {
	    .sun_family = AF_UNIX,
	};

	ck_assert_int_eq(
	    sockaddr_set_lease_server_path(&address, config->lease_name), true);

	int server = socket(PF_UNIX, SOCK_SEQPACKET, 0);
	ck_assert_int_ge(server, 0);

	unlink(address.sun_path);

	int ret;
	ret = bind(server, (struct sockaddr *)&address, sizeof(address));
	ck_assert_int_eq(ret, 0);

	ret = listen(server, 1);
	ck_assert_int_eq(ret, 0);

	sstate->is_server_started = true;
	pthread_cond_signal(&sstate->cond);

	int client = accept(server, NULL, NULL);
	/* accept is the cancellation point for this thread. If
	 * pthread_cancel() is called on this thread, accept() may return
	 * -1, so don't assert on it. */

	if (client < 0) {
		close(server);
		return NULL;
	}

	expect_client_command(client, DLM_GET_LEASE);

	if (config->send_no_data)
		goto done;

	if (config->send_data_without_fd) {
		char data;
		write(client, &data, 1);
		goto done;
	}

	if (config->nfds == 0)
		config->nfds = 1;

	config->fds = calloc(config->nfds, sizeof(int));

	for (int i = 0; i < config->nfds; i++)
		config->fds[i] = get_dummy_fd();

	send_fd_list_over_socket(client, config->nfds, config->fds);
	expect_client_command(client, DLM_RELEASE_LEASE);
done:
	close(client);
	close(server);
	return NULL;
}

struct server_state *test_server_start(struct test_config *test_config)
{
	struct server_state *sstate = malloc(sizeof(*sstate));

	*sstate = (struct server_state){
	    .lock = PTHREAD_MUTEX_INITIALIZER,
	    .cond = PTHREAD_COND_INITIALIZER,
	    .is_server_started = false,
	    .config = test_config,
	};

	pthread_create(&sstate->tid, NULL, test_server_thread, sstate);

	pthread_mutex_lock(&sstate->lock);
	while (!sstate->is_server_started)
		pthread_cond_wait(&sstate->cond, &sstate->lock);
	pthread_mutex_unlock(&sstate->lock);
	return sstate;
}

void test_server_stop(struct server_state *sstate)
{

	ck_assert_ptr_ne(sstate, NULL);

	pthread_cancel(sstate->tid);
	pthread_join(sstate->tid, NULL);

	free(sstate);
}

void test_config_cleanup(struct test_config *config)
{
	if (!config->fds)
		return;

	for (int i = 0; i < config->nfds; i++)
		close(config->fds[i]);

	free(config->fds);
}