Add initial version
[src/drm-lease-manager.git] / drm-lease-manager / test / lease-server-test.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 <check.h>
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <pthread.h>
23
24 #include "lease-server.h"
25 #include "log.h"
26 #include "test-helpers.h"
27 #include "test-socket-client.h"
28
29 #define SOCKETDIR "/tmp"
30
31 /************** Test fixutre functions *************************/
32 struct test_config default_test_config;
33
34 #define TEST_LEASE_NAME "test-lease"
35
36 static struct lease_handle test_lease = {
37     .name = TEST_LEASE_NAME,
38 };
39
40 static void test_setup(void)
41 {
42         dlm_log_enable_debug(true);
43         setenv("DLM_RUNTIME_PATH", SOCKETDIR, 1);
44
45         default_test_config = (struct test_config){
46             .lease = &test_lease,
47         };
48 }
49
50 static void test_shutdown(void)
51 {
52         test_config_cleanup(&default_test_config);
53 }
54
55 static struct ls *create_default_server(void)
56 {
57         struct lease_handle *leases[] = {
58             &test_lease,
59         };
60         struct ls *ls = ls_create(leases, 1);
61         ck_assert_ptr_ne(ls, NULL);
62         return ls;
63 }
64
65 /**************  Lease server error handling tests *************/
66
67 /* duplicate_server_failure
68  *
69  * Test details: Try to intialize the same server twice
70  * Expected results: ls_create() fails.
71  */
72 START_TEST(duplicate_server_failure)
73 {
74         struct lease_handle *leases[] = {&test_lease, &test_lease};
75         struct ls *ls = ls_create(leases, 2);
76         ck_assert_ptr_eq(ls, NULL);
77 }
78 END_TEST
79
80 START_TEST(long_lease_name_failure)
81 {
82         char long_lease_name[200];
83
84         size_t len = sizeof(long_lease_name) - 1;
85         memset(long_lease_name, 'a', len);
86         long_lease_name[len] = '\0';
87
88         struct lease_handle long_name_lease = {.name = long_lease_name};
89
90         struct lease_handle *leases[] = {&long_name_lease};
91         struct ls *ls = ls_create(leases, 1);
92         ck_assert_ptr_eq(ls, NULL);
93 }
94 END_TEST
95
96 static void add_error_tests(Suite *s)
97 {
98         TCase *tc = tcase_create("Lease server errors");
99
100         tcase_add_checked_fixture(tc, test_setup, test_shutdown);
101
102         tcase_add_test(tc, duplicate_server_failure);
103         tcase_add_test(tc, long_lease_name_failure);
104         suite_add_tcase(s, tc);
105 }
106
107 /**************  Client request handling tests ************/
108
109 /* Test the handling of client requests.  Make sure that the
110  * proper struct ls_req are generated for each client request.
111  */
112
113 static void check_request(struct ls_req *req,
114                           struct lease_handle *expected_lease,
115                           enum ls_req_type expected_type)
116 {
117         ck_assert_ptr_eq(req->lease_handle, expected_lease);
118         ck_assert_int_eq(req->type, expected_type);
119 }
120
121 static void get_and_check_request(struct ls *ls,
122                                   struct lease_handle *expected_lease,
123                                   enum ls_req_type expected_type)
124 {
125         struct ls_req req;
126         bool req_valid = ls_get_request(ls, &req);
127         ck_assert_int_eq(req_valid, true);
128         check_request(&req, expected_lease, expected_type);
129 }
130
131 /* Asynchronous version of the above.  Has the extra overhead of
132  * spawning a new thread, so should be used sparingly. */
133 struct async_req {
134         pthread_t tid;
135         struct ls *ls;
136
137         bool req_valid;
138         struct ls_req expected;
139         struct ls_req actual;
140 };
141
142 static void *get_request_thread(void *arg)
143 {
144         struct async_req *async_req = arg;
145         async_req->req_valid =
146             ls_get_request(async_req->ls, &async_req->actual);
147
148         return NULL;
149 }
150
151 static struct async_req *
152 get_and_check_request_async(struct ls *ls, struct lease_handle *expected_lease,
153                             enum ls_req_type expected_type)
154
155 {
156         struct async_req *req = malloc(sizeof(struct async_req));
157         ck_assert_ptr_ne(req, NULL);
158
159         *req = (struct async_req){
160             .ls = ls,
161             .expected =
162                 {
163                     .lease_handle = expected_lease,
164                     .type = expected_type,
165                 },
166         };
167
168         int ret = pthread_create(&req->tid, NULL, get_request_thread, req);
169         ck_assert_int_eq(ret, 0);
170
171         return req;
172 }
173
174 static void check_async_req_result(struct async_req *req)
175 {
176
177         pthread_join(req->tid, NULL);
178         ck_assert_int_eq(req->req_valid, true);
179         check_request(&req->actual, req->expected.lease_handle,
180                       req->expected.type);
181         free(req);
182 }
183
184 /* issue_lease_request_and_release
185  *
186  * Test details: Generate a lease request and lease release command from
187  *               a client.
188  * Expected results: One get lease and one release lease request are returned
189  *                   from ls_get_request().
190  */
191 START_TEST(issue_lease_request_and_release)
192 {
193         struct ls *ls = create_default_server();
194
195         struct client_state *cstate = test_client_start(&default_test_config);
196
197         get_and_check_request(ls, &test_lease, LS_REQ_GET_LEASE);
198         test_client_stop(cstate);
199         get_and_check_request(ls, &test_lease, LS_REQ_RELEASE_LEASE);
200 }
201 END_TEST
202
203 /* issue_lease_request_and_early_release
204  *
205  * Test details: Close client connection immediately after connecting (before
206  *               lease request is processed)
207  * Expected results: Should be the same result as
208  * issue_lease_request_and_release.
209  */
210 START_TEST(issue_lease_request_and_early_release)
211 {
212         struct ls *ls = create_default_server();
213
214         struct client_state *cstate = test_client_start(&default_test_config);
215
216         test_client_stop(cstate);
217         get_and_check_request(ls, &test_lease, LS_REQ_GET_LEASE);
218         get_and_check_request(ls, &test_lease, LS_REQ_RELEASE_LEASE);
219 }
220 END_TEST
221
222 /* issue_multiple_lease_requests
223  *
224  * Test details: Generate multiple lease requests to the same lease server from
225  *               multiple clients at the same time
226  * Expected results: One get lease and one release lease request are returned
227  *                   from ls_get_request().
228  *                   Requests from all but the first client are rejected
229  *                   (sockets are closed).
230  */
231 START_TEST(issue_multiple_lease_requests)
232 {
233         struct lease_handle *leases[] = {
234             &test_lease,
235         };
236         struct ls *ls = ls_create(leases, 1);
237
238         struct test_config accepted_config;
239         struct client_state *accepted_cstate;
240
241         accepted_config = default_test_config;
242         accepted_cstate = test_client_start(&accepted_config);
243         get_and_check_request(ls, &test_lease, LS_REQ_GET_LEASE);
244
245         /*Try to make additional connections while the first is still
246          *connected. */
247         const int nextra_clients = 2;
248         struct test_config extra_configs[nextra_clients];
249         struct client_state *extra_cstates[nextra_clients];
250
251         for (int i = 0; i < nextra_clients; i++) {
252                 extra_configs[i] = default_test_config;
253                 extra_cstates[i] = test_client_start(&extra_configs[i]);
254         }
255
256         // Start asyncronously checking for the accepted client to release.
257         struct async_req *async_release_req =
258             get_and_check_request_async(ls, &test_lease, LS_REQ_RELEASE_LEASE);
259
260         for (int i = 0; i < nextra_clients; i++) {
261                 test_client_stop(extra_cstates[i]);
262         }
263
264         /* Release the first connection and check results */
265         test_client_stop(accepted_cstate);
266         check_async_req_result(async_release_req);
267
268         /* Only one connection should be granted access by the lease manager */
269         ck_assert_int_eq(accepted_config.connection_completed, true);
270         for (int i = 0; i < nextra_clients; i++)
271                 ck_assert_int_eq(extra_configs[i].connection_completed, false);
272 }
273 END_TEST
274
275 static void add_client_request_tests(Suite *s)
276 {
277         TCase *tc = tcase_create("Client request testing");
278
279         tcase_add_checked_fixture(tc, test_setup, test_shutdown);
280
281         tcase_add_test(tc, issue_lease_request_and_release);
282         tcase_add_test(tc, issue_lease_request_and_early_release);
283         tcase_add_test(tc, issue_multiple_lease_requests);
284         suite_add_tcase(s, tc);
285 }
286
287 /**************  File descriptor sending tests ************/
288
289 /* Test the sending (and failure to send) of file descriptors
290  * to the client.
291  */
292
293 /* send_fd_to_client
294  *
295  * Test details: Send a valid fd to a given client.
296  * Expected results: The correct fd is successfully sent.
297  */
298 START_TEST(send_fd_to_client)
299 {
300         struct ls *ls = create_default_server();
301
302         struct client_state *cstate = test_client_start(&default_test_config);
303
304         struct ls_req req;
305         bool req_valid = ls_get_request(ls, &req);
306         ck_assert_int_eq(req_valid, true);
307         check_request(&req, &test_lease, LS_REQ_GET_LEASE);
308
309         /* send an fd to the client*/
310         int test_fd = get_dummy_fd();
311         ck_assert_int_eq(ls_send_fd(ls, req.server, test_fd), true);
312
313         test_client_stop(cstate);
314         get_and_check_request(ls, &test_lease, LS_REQ_RELEASE_LEASE);
315
316         ck_assert_int_eq(default_test_config.connection_completed, true);
317         ck_assert_int_eq(default_test_config.has_data, true);
318         check_fd_equality(test_fd, default_test_config.received_fd);
319 }
320 END_TEST
321
322 /* ls_send_fd_is_noop_when_fd_is_invalid
323  *
324  * Test details: Call ls_send_fd() with an invalid  fd.
325  * Expected results: No fd is sent to client.  The connection to the
326  *                   client is closed.
327  */
328 START_TEST(ls_send_fd_is_noop_when_fd_is_invalid)
329 {
330         struct ls *ls = create_default_server();
331
332         struct client_state *cstate = test_client_start(&default_test_config);
333
334         struct ls_req req;
335         bool req_valid = ls_get_request(ls, &req);
336         ck_assert_int_eq(req_valid, true);
337         check_request(&req, &test_lease, LS_REQ_GET_LEASE);
338
339         int invalid_fd = get_dummy_fd();
340         close(invalid_fd);
341
342         ck_assert_int_eq(ls_send_fd(ls, req.server, invalid_fd), false);
343
344         test_client_stop(cstate);
345         get_and_check_request(ls, &test_lease, LS_REQ_RELEASE_LEASE);
346         ck_assert_int_eq(default_test_config.connection_completed, true);
347         ck_assert_int_eq(default_test_config.has_data, false);
348 }
349 END_TEST
350
351 static void add_fd_send_tests(Suite *s)
352 {
353         TCase *tc = tcase_create("File descriptor sending tests");
354
355         tcase_add_checked_fixture(tc, test_setup, test_shutdown);
356
357         tcase_add_test(tc, send_fd_to_client);
358         tcase_add_test(tc, ls_send_fd_is_noop_when_fd_is_invalid);
359         suite_add_tcase(s, tc);
360 }
361
362 int main(void)
363 {
364         int number_failed;
365         Suite *s;
366         SRunner *sr;
367
368         s = suite_create("DLM lease server tests");
369
370         add_error_tests(s);
371         add_client_request_tests(s);
372         add_fd_send_tests(s);
373
374         sr = srunner_create(s);
375
376         srunner_run_all(sr, CK_NORMAL);
377         number_failed = srunner_ntests_failed(sr);
378         srunner_free(sr);
379         return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
380 }