Move lease fd send/receive to dlm-protocol
[src/drm-lease-manager.git] / drm-lease-manager / test / test-socket-client.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-client.h"
17
18 #include <check.h>
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30
31 #include "dlm-protocol.h"
32 #include "socket-path.h"
33
34 #define DEFAULT_RECV_TIMEOUT (100) // timeout in ms to receive data from server
35
36 struct client_state {
37         pthread_t tid;
38
39         int socket_fd;
40         struct test_config *config;
41 };
42
43 static void send_lease_request(int socket, enum dlm_opcode opcode)
44 {
45         struct dlm_client_request req = {
46             .opcode = opcode,
47         };
48         send_dlm_client_request(socket, &req);
49 }
50
51 static void client_gst_socket_status(int socket_fd, struct test_config *config)
52 {
53
54         config->connection_completed = true;
55
56         struct pollfd pfd = {.fd = socket_fd, .events = POLLIN};
57         if (poll(&pfd, 1, config->recv_timeout) <= 0)
58                 return;
59
60         if (pfd.revents & POLLHUP)
61                 config->connection_completed = false;
62
63         if (pfd.revents & POLLIN)
64                 config->has_data = true;
65
66         return;
67 }
68
69 static void *test_client_thread(void *arg)
70 {
71         struct client_state *cstate = arg;
72         struct test_config *config = cstate->config;
73
74         struct sockaddr_un address = {
75             .sun_family = AF_UNIX,
76         };
77
78         ck_assert_int_eq(
79             sockaddr_set_lease_server_path(&address, config->lease->name),
80             true);
81
82         int client = socket(PF_UNIX, SOCK_SEQPACKET, 0);
83         ck_assert_int_ge(client, 0);
84
85         int ret;
86         ret = connect(client, (struct sockaddr *)&address, sizeof(address));
87         if (ret != 0) {
88                 printf("Connect failed;: %s\n", strerror(errno));
89                 close(client);
90                 return NULL;
91         }
92
93         send_lease_request(client, DLM_GET_LEASE);
94
95         if (!config->recv_timeout)
96                 config->recv_timeout = DEFAULT_RECV_TIMEOUT;
97
98         client_gst_socket_status(client, config);
99
100         if (config->has_data) {
101                 config->received_fd = receive_lease_fd(client);
102         }
103
104         cstate->socket_fd = client;
105         send_lease_request(client, DLM_RELEASE_LEASE);
106
107         return NULL;
108 }
109
110 struct client_state *test_client_start(struct test_config *test_config)
111 {
112         struct client_state *cstate = malloc(sizeof(*cstate));
113
114         *cstate = (struct client_state){
115             .config = test_config,
116         };
117
118         pthread_create(&cstate->tid, NULL, test_client_thread, cstate);
119
120         return cstate;
121 }
122
123 void test_client_stop(struct client_state *cstate)
124 {
125
126         ck_assert_ptr_ne(cstate, NULL);
127
128         pthread_join(cstate->tid, NULL);
129
130         if (cstate->socket_fd >= 0)
131                 close(cstate->socket_fd);
132
133         free(cstate);
134 }
135
136 void test_config_cleanup(struct test_config *config)
137 {
138         if (config->has_data && config->received_fd >= 0)
139                 close(config->received_fd);
140 }