diff options
author | Damian Hobson-Garcia <dhobsong@igel.co.jp> | 2020-11-24 17:16:39 +0900 |
---|---|---|
committer | Damian Hobson-Garcia <dhobsong@igel.co.jp> | 2021-02-19 10:48:23 +0000 |
commit | f991de200799118355fd75237a740321bda7aaa7 (patch) | |
tree | 589f536d835d81f1166410e67aedb578d6e70395 /libdlmclient/test | |
parent | 464ec461237b36e03c4cdb175e9d87ab502ee1d5 (diff) |
Add initial version
The initial version implements the basic functionality
of the client/server communication and lease management.
For now, one lease is created per valid connector (dependent
on CRTC availablity).
Bug-AGL: SPEC-3729
Signed-off-by: Damian Hobson-Garcia <dhobsong@igel.co.jp>
Change-Id: I2b37a892742cc22bdc53a5172c8ad3d8a7bb5e66
Diffstat (limited to 'libdlmclient/test')
-rw-r--r-- | libdlmclient/test/libdlmclient-test.c | 291 | ||||
-rw-r--r-- | libdlmclient/test/meson.build | 6 | ||||
-rw-r--r-- | libdlmclient/test/test-socket-server.c | 169 | ||||
-rw-r--r-- | libdlmclient/test/test-socket-server.h | 33 |
4 files changed, 499 insertions, 0 deletions
diff --git a/libdlmclient/test/libdlmclient-test.c b/libdlmclient/test/libdlmclient-test.c new file mode 100644 index 0000000..5b04c09 --- /dev/null +++ b/libdlmclient/test/libdlmclient-test.c @@ -0,0 +1,291 @@ +/* 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 <check.h> + +#include <dirent.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "config.h" +#include "dlmclient.h" +#include "test-helpers.h" +#include "test-socket-server.h" + +#define SOCKETDIR "/tmp" + +#define TEST_LEASE_NAME "test-lease" + +/************** Test fixutre functions *************/ +struct test_config default_test_config; + +static void test_setup(void) +{ + dlm_enable_debug_log(true); + setenv("DLM_RUNTIME_PATH", SOCKETDIR, 1); + + default_test_config = (struct test_config){ + .lease_name = TEST_LEASE_NAME, + .nfds = 1, + }; +} + +static void test_shutdown(void) +{ + test_config_cleanup(&default_test_config); +} + +/************** Lease manager error tests *************/ + +/* These tests verify that the client library gracefully handles + * failures when trying to receive data from the lease manager. + * Failures or errors in the lease manager should cause meaningful + * errors to be reported by the client. Lease manager errors should + * not cause crashes or invalid state in the client */ + +/* manager_connection_err + * + * Test details: Simulate socket connection failure. + * Expected results: dlm_get_lease() fails. + */ +START_TEST(manager_connection_err) +{ + struct server_state *sstate = test_server_start(&default_test_config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME "-bad"); + + ck_assert_ptr_eq(lease, NULL); + + test_server_stop(sstate); +} +END_TEST + +/* no_data_from_manager + * + * Test details: Close the remote (lease manager) without sending any data. + * Currently this means that the lease request has been rejected + * for some reason. + * + * TODO: Update this when the client-server protocol is updated to + * include the reason for the lease rejection. + * + * Expected results: dlm_get_lease() fails, errno set to EACCESS. + */ +START_TEST(no_data_from_manager) +{ + + struct test_config config = { + .lease_name = TEST_LEASE_NAME, + .send_no_data = true, + }; + + struct server_state *sstate = test_server_start(&config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME); + + ck_assert_ptr_eq(lease, NULL); + ck_assert_int_eq(errno, EACCES); + + test_server_stop(sstate); +} +END_TEST + +/* no_lease_fd_from_manager + * + * Test details: Simulate receiving response from lease manager with + * no fd attached. (i.e. a protocol error) + * + * Expected results: dlm_get_lease() fails, errno set to EPROTO. + */ +START_TEST(no_lease_fd_from_manager) +{ + /* Receive message from the lease manager with missing lease fd */ + struct test_config config = { + .lease_name = TEST_LEASE_NAME, + .send_data_without_fd = true, + }; + + struct server_state *sstate = test_server_start(&config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME); + + ck_assert_ptr_eq(lease, NULL); + ck_assert_int_eq(errno, EPROTO); + + test_server_stop(sstate); +} +END_TEST + +static void add_lease_manager_error_tests(Suite *s) +{ + TCase *tc = tcase_create("Lease manager error handling"); + + tcase_add_checked_fixture(tc, test_setup, test_shutdown); + + tcase_add_test(tc, manager_connection_err); + tcase_add_test(tc, no_data_from_manager); + tcase_add_test(tc, no_lease_fd_from_manager); + + suite_add_tcase(s, tc); +} + +/************** Lease handling tests *****************/ + +/* These tests verify that the client library handles the received + * lease data properly. Receiving the lease fds without leaks, + * properly passing the fds to the client application and cleaning + * them up on release. + */ + +static int count_open_fds(void) +{ + int fds = 0; + DIR *dirp = opendir("/proc/self/fd"); + while ((readdir(dirp) != NULL)) + fds++; + closedir(dirp); + return fds; +} + +/* receive_fd_from_manager + * + * Test details: Successfully receive a file descriptor. + * Expected results: dlm_get_lease() succeeds. + * dlm_lease_fd() returns the correct fd value. + */ +START_TEST(receive_fd_from_manager) +{ + struct server_state *sstate = test_server_start(&default_test_config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME); + ck_assert_ptr_ne(lease, NULL); + + int received_fd = dlm_lease_fd(lease); + + int sent_fd = default_test_config.fds[0]; + + check_fd_equality(received_fd, sent_fd); + + dlm_release_lease(lease); + + test_server_stop(sstate); + close(sent_fd); +} +END_TEST + +/* lease_fd_is_closed_on_release + * + * Test details: Verify that dlm_release_lease() closes the lease fd. + * Expected results: lease fd is closed. + */ +START_TEST(lease_fd_is_closed_on_release) +{ + struct server_state *sstate = test_server_start(&default_test_config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME); + ck_assert_ptr_ne(lease, NULL); + + int received_fd = dlm_lease_fd(lease); + + check_fd_is_open(received_fd); + dlm_release_lease(lease); + check_fd_is_closed(received_fd); + + test_server_stop(sstate); +} +END_TEST + +/* dlm_lease_fd_always_returns_same_lease + * + * Test details: Verify that dlm_lease_fd() always returns the same value + * for a given lease. + * Expected results: same value is returned when called multiple times. + */ +START_TEST(dlm_lease_fd_always_returns_same_lease) +{ + struct server_state *sstate = test_server_start(&default_test_config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME); + ck_assert_ptr_ne(lease, NULL); + + int received_fd = dlm_lease_fd(lease); + + ck_assert_int_eq(received_fd, dlm_lease_fd(lease)); + ck_assert_int_eq(received_fd, dlm_lease_fd(lease)); + + dlm_release_lease(lease); + + test_server_stop(sstate); +} +END_TEST + +START_TEST(verify_that_unused_fds_are_not_leaked) +{ + int nopen_fds = count_open_fds(); + + struct test_config config = { + .lease_name = TEST_LEASE_NAME, + .nfds = 2, + }; + + struct server_state *sstate = test_server_start(&config); + + struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME); + + ck_assert_ptr_eq(lease, NULL); + ck_assert_int_eq(errno, EPROTO); + + dlm_release_lease(lease); + + test_server_stop(sstate); + + test_config_cleanup(&config); + ck_assert_int_eq(nopen_fds, count_open_fds()); +} +END_TEST + +static void add_lease_handling_tests(Suite *s) +{ + TCase *tc = tcase_create("Lease processing tests"); + + tcase_add_checked_fixture(tc, test_setup, test_shutdown); + + tcase_add_test(tc, receive_fd_from_manager); + tcase_add_test(tc, lease_fd_is_closed_on_release); + tcase_add_test(tc, dlm_lease_fd_always_returns_same_lease); + tcase_add_test(tc, verify_that_unused_fds_are_not_leaked); + suite_add_tcase(s, tc); +} + +int main(void) +{ + int number_failed; + Suite *s; + SRunner *sr; + + s = suite_create("DLM client library tests"); + + add_lease_manager_error_tests(s); + add_lease_handling_tests(s); + + sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/libdlmclient/test/meson.build b/libdlmclient/test/meson.build new file mode 100644 index 0000000..35e8575 --- /dev/null +++ b/libdlmclient/test/meson.build @@ -0,0 +1,6 @@ +cl_test = executable('libdlmclient-test', + sources: [ 'libdlmclient-test.c', 'test-socket-server.c'], + dependencies: [check_dep, fff_dep, dlmcommon_dep, dlmclient_dep, thread_dep], + include_directories: configuration_inc) + +test('Client library test', cl_test, is_parallel: false) diff --git a/libdlmclient/test/test-socket-server.c b/libdlmclient/test/test-socket-server.c new file mode 100644 index 0000000..281aaf7 --- /dev/null +++ b/libdlmclient/test/test-socket-server.c @@ -0,0 +1,169 @@ +/* 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 "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); +} + +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_STREAM, 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; + } + + 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); +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); +} diff --git a/libdlmclient/test/test-socket-server.h b/libdlmclient/test/test-socket-server.h new file mode 100644 index 0000000..ebdbd2d --- /dev/null +++ b/libdlmclient/test/test-socket-server.h @@ -0,0 +1,33 @@ +/* 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. + */ + +#ifndef TEST_SOCKET_SERVER_H +#define TEST_SOCKET_SERVER_H +#include <stdbool.h> + +struct test_config { + char *lease_name; + int nfds; + int *fds; + + bool send_data_without_fd; + bool send_no_data; +}; + +void test_config_cleanup(struct test_config *config); + +struct server_state *test_server_start(struct test_config *test_config); +void test_server_stop(struct server_state *sstate); +#endif |