|
| 1 | +/** |
| 2 | + * @file test_client_monitoring.c |
| 3 | + * @author Roman Janota <janota@cesnet.cz> |
| 4 | + * @brief libnetconf2 client monitoring thread test |
| 5 | + * |
| 6 | + * @copyright |
| 7 | + * Copyright (c) 2024 CESNET, z.s.p.o. |
| 8 | + * |
| 9 | + * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | + * You may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * https://opensource.org/licenses/BSD-3-Clause |
| 14 | + */ |
| 15 | + |
| 16 | +#define _GNU_SOURCE |
| 17 | + |
| 18 | +#include <errno.h> |
| 19 | +#include <pthread.h> |
| 20 | +#include <setjmp.h> |
| 21 | +#include <stdarg.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#include <cmocka.h> |
| 27 | + |
| 28 | +#include "ln2_test.h" |
| 29 | +#include "session_p.h" |
| 30 | + |
| 31 | +#include <libssh/libssh.h> |
| 32 | + |
| 33 | +int TEST_PORT = 10050; |
| 34 | +const char *TEST_PORT_STR = "10050"; |
| 35 | + |
| 36 | +void |
| 37 | +monitoring_clb(struct nc_session *sess, void *user_data) |
| 38 | +{ |
| 39 | + pthread_barrier_t *barrier = user_data; |
| 40 | + |
| 41 | + /* signal the main thread that the monitoring callback was called */ |
| 42 | + pthread_barrier_wait(barrier); |
| 43 | + printf("Session with ID %d disconnected by the server.\n", nc_session_get_id(sess)); |
| 44 | +} |
| 45 | + |
| 46 | +static void * |
| 47 | +client_thread(void *arg) |
| 48 | +{ |
| 49 | + int ret; |
| 50 | + struct nc_session *session = NULL; |
| 51 | + struct ln2_test_ctx *test_ctx = arg; |
| 52 | + pthread_barrier_t monitoring_barrier; |
| 53 | + |
| 54 | + /* initialize the barrier */ |
| 55 | + ret = pthread_barrier_init(&monitoring_barrier, NULL, 2); |
| 56 | + assert_int_equal(ret, 0); |
| 57 | + |
| 58 | + /* start the monitoring thread */ |
| 59 | + ret = nc_client_monitoring_thread_start(monitoring_clb, &monitoring_barrier, NULL); |
| 60 | + assert_int_equal(ret, 0); |
| 61 | + |
| 62 | + /* skip all hostkey and known_hosts checks */ |
| 63 | + nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 64 | + |
| 65 | + /* set the search path for the schemas */ |
| 66 | + ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 67 | + assert_int_equal(ret, 0); |
| 68 | + |
| 69 | + /* set the client's username */ |
| 70 | + ret = nc_client_ssh_set_username("test_client_monitoring"); |
| 71 | + assert_int_equal(ret, 0); |
| 72 | + |
| 73 | + /* add the client's key pair */ |
| 74 | + ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa"); |
| 75 | + assert_int_equal(ret, 0); |
| 76 | + |
| 77 | + /* wait for the server to be ready and connect */ |
| 78 | + pthread_barrier_wait(&test_ctx->barrier); |
| 79 | + session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
| 80 | + assert_non_null(session); |
| 81 | + |
| 82 | + /* wait for the monitoring thread callback to be called */ |
| 83 | + pthread_barrier_wait(&monitoring_barrier); |
| 84 | + |
| 85 | + /* stop the monitoring thread */ |
| 86 | + nc_client_monitoring_thread_stop(); |
| 87 | + |
| 88 | + pthread_barrier_destroy(&monitoring_barrier); |
| 89 | + return NULL; |
| 90 | +} |
| 91 | + |
| 92 | +void * |
| 93 | +server_thread(void *arg) |
| 94 | +{ |
| 95 | + int ret; |
| 96 | + NC_MSG_TYPE msgtype; |
| 97 | + struct nc_session *session = NULL; |
| 98 | + struct nc_pollsession *ps = NULL; |
| 99 | + struct ln2_test_ctx *test_ctx = arg; |
| 100 | + int fd; |
| 101 | + struct linger ling = {1, 0}; |
| 102 | + |
| 103 | + ps = nc_ps_new(); |
| 104 | + assert_non_null(ps); |
| 105 | + |
| 106 | + /* wait for the client to be ready to connect */ |
| 107 | + pthread_barrier_wait(&test_ctx->barrier); |
| 108 | + |
| 109 | + /* accept a session and add it to the poll session structure */ |
| 110 | + msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session); |
| 111 | + assert_int_equal(msgtype, NC_MSG_HELLO); |
| 112 | + |
| 113 | + /* get the session's fd */ |
| 114 | + fd = ssh_get_fd(session->ti.libssh.session); |
| 115 | + assert_int_not_equal(fd, -1); |
| 116 | + |
| 117 | + /* set the socket to close immediately */ |
| 118 | + ret = setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); |
| 119 | + assert_int_equal(ret, 0); |
| 120 | + |
| 121 | + /* add the session to the poll session */ |
| 122 | + ret = nc_ps_add_session(ps, session); |
| 123 | + assert_int_equal(ret, 0); |
| 124 | + |
| 125 | + /* poll until the client stops sending messages */ |
| 126 | + do { |
| 127 | + ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 128 | + } while ((ret & NC_PSPOLL_RPC)); |
| 129 | + |
| 130 | + /* free the session (it will close the socket -> client needs to detect this) */ |
| 131 | + nc_ps_clear(ps, 1, NULL); |
| 132 | + nc_ps_free(ps); |
| 133 | + return NULL; |
| 134 | +} |
| 135 | + |
| 136 | +static void |
| 137 | +test_nc_client_monitoring(void **state) |
| 138 | +{ |
| 139 | + int ret, i; |
| 140 | + pthread_t tids[2]; |
| 141 | + |
| 142 | + ret = pthread_create(&tids[0], NULL, client_thread, *state); |
| 143 | + assert_int_equal(ret, 0); |
| 144 | + ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 145 | + assert_int_equal(ret, 0); |
| 146 | + |
| 147 | + for (i = 0; i < 2; i++) { |
| 148 | + pthread_join(tids[i], NULL); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +static int |
| 153 | +setup(void **state) |
| 154 | +{ |
| 155 | + int ret; |
| 156 | + struct lyd_node *tree = NULL; |
| 157 | + struct ln2_test_ctx *test_ctx; |
| 158 | + |
| 159 | + /* global setup */ |
| 160 | + ret = ln2_glob_test_setup(&test_ctx); |
| 161 | + assert_int_equal(ret, 0); |
| 162 | + |
| 163 | + *state = test_ctx; |
| 164 | + |
| 165 | + /* add endpoint */ |
| 166 | + ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree); |
| 167 | + assert_int_equal(ret, 0); |
| 168 | + |
| 169 | + /* add hostkey */ |
| 170 | + ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree); |
| 171 | + assert_int_equal(ret, 0); |
| 172 | + |
| 173 | + /* add the test client */ |
| 174 | + ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_client_monitoring", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree); |
| 175 | + assert_int_equal(ret, 0); |
| 176 | + |
| 177 | + /* configure the server based on the data */ |
| 178 | + ret = nc_server_config_setup_data(tree); |
| 179 | + assert_int_equal(ret, 0); |
| 180 | + |
| 181 | + lyd_free_all(tree); |
| 182 | + |
| 183 | + return 0; |
| 184 | +} |
| 185 | + |
| 186 | +int |
| 187 | +main(void) |
| 188 | +{ |
| 189 | + const struct CMUnitTest tests[] = { |
| 190 | + cmocka_unit_test_setup_teardown(test_nc_client_monitoring, setup, ln2_glob_test_teardown) |
| 191 | + }; |
| 192 | + |
| 193 | + /* try to get ports from the environment, otherwise use the default */ |
| 194 | + if (ln2_glob_test_get_ports(1, &TEST_PORT, &TEST_PORT_STR)) { |
| 195 | + return 1; |
| 196 | + } |
| 197 | + |
| 198 | + setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 199 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 200 | +} |
0 commit comments