roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1 | /** |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 2 | * @file test_two_channels.c |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 4 | * @brief libnetconf2 Openning a new session on an established SSH channel test. |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 5 | * |
| 6 | * @copyright |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 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 | */ |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 16 | |
| 17 | #include <errno.h> |
| 18 | #include <pthread.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 19 | #include <setjmp.h> |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 20 | #include <stdarg.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 25 | |
| 26 | #include <cmocka.h> |
| 27 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 28 | #include "tests/config.h" |
| 29 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 30 | #define NC_ACCEPT_TIMEOUT 2000 |
| 31 | #define NC_PS_POLL_TIMEOUT 2000 |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 32 | #define BACKOFF_TIMEOUT_USECS 100 |
| 33 | |
| 34 | struct ly_ctx *ctx; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 35 | |
| 36 | static void * |
| 37 | server_thread(void *arg) |
| 38 | { |
| 39 | int ret, del_session_count = 0, sleep_count = 0; |
| 40 | NC_MSG_TYPE msgtype; |
| 41 | struct nc_session *session, *new_session; |
| 42 | struct nc_pollsession *ps; |
| 43 | |
| 44 | (void) arg; |
| 45 | |
| 46 | ps = nc_ps_new(); |
| 47 | assert_non_null(ps); |
| 48 | |
| 49 | while (del_session_count < 2) { |
roman | 4bbd86b | 2024-03-05 14:30:30 +0100 | [diff] [blame] | 50 | msgtype = nc_accept(0, ctx, &new_session); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 51 | |
| 52 | if (msgtype == NC_MSG_HELLO) { |
roman | 4bbd86b | 2024-03-05 14:30:30 +0100 | [diff] [blame] | 53 | ret = nc_ps_add_session(ps, new_session); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 54 | assert_int_equal(ret, 0); |
| 55 | } |
| 56 | |
roman | 4bbd86b | 2024-03-05 14:30:30 +0100 | [diff] [blame] | 57 | ret = nc_ps_poll(ps, 0, &session); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 58 | |
| 59 | if (ret & NC_PSPOLL_SESSION_TERM) { |
roman | 4bbd86b | 2024-03-05 14:30:30 +0100 | [diff] [blame] | 60 | nc_ps_del_session(ps, session); |
| 61 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 62 | del_session_count++; |
| 63 | } else if (ret & NC_PSPOLL_SSH_CHANNEL) { |
| 64 | msgtype = nc_session_accept_ssh_channel(session, &new_session); |
| 65 | if (msgtype == NC_MSG_HELLO) { |
| 66 | ret = nc_ps_add_session(ps, new_session); |
| 67 | assert_int_equal(ret, 0); |
| 68 | } |
| 69 | } else if (ret & NC_PS_POLL_TIMEOUT) { |
| 70 | usleep(BACKOFF_TIMEOUT_USECS); |
| 71 | sleep_count++; |
| 72 | assert_int_not_equal(sleep_count, 50000); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | nc_ps_free(ps); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 77 | return NULL; |
| 78 | } |
| 79 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 80 | static void * |
| 81 | client_thread(void *arg) |
| 82 | { |
| 83 | (void) arg; |
| 84 | int ret; |
| 85 | struct nc_session *session_cl1, *session_cl2; |
| 86 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 87 | /* skip all hostkey and known_hosts checks */ |
| 88 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 89 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 90 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 91 | assert_int_equal(ret, 0); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 92 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 93 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ed25519.pub", TESTS_DIR "/data/id_ed25519"); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 94 | assert_int_equal(ret, 0); |
| 95 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 96 | ret = nc_client_ssh_set_username("client_1"); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 97 | assert_int_equal(ret, 0); |
| 98 | |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 99 | session_cl1 = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 100 | assert_non_null(session_cl1); |
| 101 | |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 102 | ret = nc_client_ssh_set_username("client_2"); |
| 103 | assert_int_equal(ret, 0); |
| 104 | |
| 105 | ret = nc_client_ssh_del_keypair(0); |
| 106 | assert_int_equal(ret, 0); |
| 107 | |
| 108 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ecdsa521.pub", TESTS_DIR "/data/id_ecdsa521"); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 109 | assert_int_equal(ret, 0); |
| 110 | |
| 111 | session_cl2 = nc_connect_ssh_channel(session_cl1, NULL); |
| 112 | assert_non_null(session_cl2); |
| 113 | |
| 114 | nc_client_destroy(); |
| 115 | nc_session_free(session_cl1, NULL); |
| 116 | nc_session_free(session_cl2, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | test_nc_two_channels(void **state) |
| 122 | { |
| 123 | int ret, i; |
| 124 | pthread_t tids[2]; |
| 125 | |
| 126 | (void) state; |
| 127 | |
| 128 | ret = pthread_create(&tids[0], NULL, client_thread, NULL); |
| 129 | assert_int_equal(ret, 0); |
| 130 | ret = pthread_create(&tids[1], NULL, server_thread, NULL); |
| 131 | assert_int_equal(ret, 0); |
| 132 | |
| 133 | for (i = 0; i < 2; i++) { |
| 134 | pthread_join(tids[i], NULL); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static int |
| 139 | setup_f(void **state) |
| 140 | { |
| 141 | int ret; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 142 | struct lyd_node *tree = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 143 | |
| 144 | (void) state; |
| 145 | |
| 146 | nc_verbosity(NC_VERB_VERBOSE); |
| 147 | |
| 148 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 149 | assert_int_equal(ret, 0); |
| 150 | |
| 151 | ret = nc_server_init_ctx(&ctx); |
| 152 | assert_int_equal(ret, 0); |
| 153 | |
| 154 | ret = nc_server_config_load_modules(&ctx); |
| 155 | assert_int_equal(ret, 0); |
| 156 | |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 157 | ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_LIBSSH, "127.0.0.1", TEST_PORT, &tree); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 158 | assert_int_equal(ret, 0); |
| 159 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 160 | ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 161 | assert_int_equal(ret, 0); |
| 162 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 163 | ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "client_1", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 164 | assert_int_equal(ret, 0); |
| 165 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 166 | ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "client_2", "pubkey", TESTS_DIR "/data/id_ecdsa521.pub", &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 167 | assert_int_equal(ret, 0); |
| 168 | |
| 169 | ret = nc_server_config_setup_data(tree); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 170 | assert_int_equal(ret, 0); |
| 171 | |
| 172 | ret = nc_server_init(); |
| 173 | assert_int_equal(ret, 0); |
| 174 | |
roman | eadc478 | 2023-09-14 10:10:08 +0200 | [diff] [blame] | 175 | /* initialize client */ |
| 176 | ret = nc_client_init(); |
| 177 | assert_int_equal(ret, 0); |
| 178 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 179 | lyd_free_all(tree); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int |
| 185 | teardown_f(void **state) |
| 186 | { |
| 187 | (void) state; |
| 188 | |
| 189 | nc_server_destroy(); |
| 190 | ly_ctx_destroy(ctx); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | main(void) |
| 197 | { |
| 198 | const struct CMUnitTest tests[] = { |
| 199 | cmocka_unit_test_setup_teardown(test_nc_two_channels, setup_f, teardown_f), |
| 200 | }; |
| 201 | |
| 202 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 203 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 204 | } |