roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_endpt_share_clients.c |
| 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
| 4 | * @brief libnetconf2 Sharing clients between endpoints test |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2023 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 "tests/config.h" |
| 29 | |
| 30 | #define NC_ACCEPT_TIMEOUT 2000 |
| 31 | #define NC_PS_POLL_TIMEOUT 2000 |
| 32 | |
| 33 | struct ly_ctx *ctx; |
| 34 | |
| 35 | struct test_state { |
| 36 | pthread_barrier_t barrier; |
| 37 | }; |
| 38 | |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 39 | static void * |
| 40 | server_thread(void *arg) |
| 41 | { |
| 42 | int ret; |
| 43 | NC_MSG_TYPE msgtype; |
| 44 | struct nc_session *session; |
| 45 | struct nc_pollsession *ps; |
| 46 | struct test_state *state = arg; |
| 47 | |
| 48 | ps = nc_ps_new(); |
| 49 | assert_non_null(ps); |
| 50 | |
| 51 | /* accept a session and add it to the poll session structure */ |
| 52 | pthread_barrier_wait(&state->barrier); |
| 53 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 54 | assert_int_equal(msgtype, NC_MSG_HELLO); |
| 55 | |
| 56 | ret = nc_ps_add_session(ps, session); |
| 57 | assert_int_equal(ret, 0); |
| 58 | |
| 59 | do { |
| 60 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 61 | assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC); |
| 62 | } while (!(ret & NC_PSPOLL_SESSION_TERM)); |
| 63 | |
| 64 | nc_ps_clear(ps, 1, NULL); |
| 65 | nc_ps_free(ps); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | static void * |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 70 | client_thread_ssh(void *arg) |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 71 | { |
| 72 | int ret; |
| 73 | struct nc_session *session = NULL; |
| 74 | struct test_state *state = arg; |
| 75 | |
| 76 | /* skip all hostkey and known_hosts checks */ |
| 77 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 78 | |
| 79 | /* set directory where to search for modules */ |
| 80 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 81 | assert_int_equal(ret, 0); |
| 82 | |
| 83 | /* set ssh username */ |
| 84 | ret = nc_client_ssh_set_username("client"); |
| 85 | assert_int_equal(ret, 0); |
| 86 | |
| 87 | /* add client's key pair */ |
| 88 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa"); |
| 89 | assert_int_equal(ret, 0); |
| 90 | |
| 91 | /* wait for the server to reach polling */ |
| 92 | pthread_barrier_wait(&state->barrier); |
| 93 | |
| 94 | /* connect */ |
| 95 | session = nc_connect_ssh("127.0.0.1", 10005, NULL); |
| 96 | assert_non_null(session); |
| 97 | |
| 98 | nc_session_free(session, NULL); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | static void |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 103 | nc_test_endpt_share_clients_ssh(void **state) |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 104 | { |
| 105 | int ret, i; |
| 106 | pthread_t tids[2]; |
| 107 | |
| 108 | assert_non_null(state); |
| 109 | |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 110 | ret = pthread_create(&tids[0], NULL, client_thread_ssh, *state); |
| 111 | assert_int_equal(ret, 0); |
| 112 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 113 | assert_int_equal(ret, 0); |
| 114 | |
| 115 | for (i = 0; i < 2; i++) { |
| 116 | pthread_join(tids[i], NULL); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void * |
| 121 | client_thread_tls(void *arg) |
| 122 | { |
| 123 | int ret; |
| 124 | struct nc_session *session = NULL; |
| 125 | struct test_state *state = arg; |
| 126 | |
| 127 | /* set directory where to search for modules */ |
| 128 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 129 | assert_int_equal(ret, 0); |
| 130 | |
| 131 | /* set client cert */ |
| 132 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key"); |
| 133 | assert_int_equal(ret, 0); |
| 134 | |
| 135 | /* set client ca */ |
| 136 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data"); |
| 137 | assert_int_equal(ret, 0); |
| 138 | |
| 139 | pthread_barrier_wait(&state->barrier); |
| 140 | session = nc_connect_tls("127.0.0.1", 10008, NULL); |
| 141 | assert_non_null(session); |
| 142 | |
| 143 | nc_session_free(session, NULL); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | nc_test_endpt_share_clients_tls(void **state) |
| 149 | { |
| 150 | int ret, i; |
| 151 | pthread_t tids[2]; |
| 152 | |
| 153 | assert_non_null(state); |
| 154 | |
| 155 | ret = pthread_create(&tids[0], NULL, client_thread_tls, *state); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 156 | assert_int_equal(ret, 0); |
| 157 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 158 | assert_int_equal(ret, 0); |
| 159 | |
| 160 | for (i = 0; i < 2; i++) { |
| 161 | pthread_join(tids[i], NULL); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static int |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 166 | setup_ssh(void **state) |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 167 | { |
| 168 | int ret; |
| 169 | struct lyd_node *tree = NULL; |
| 170 | struct test_state *test_state; |
| 171 | |
| 172 | nc_verbosity(NC_VERB_VERBOSE); |
| 173 | |
| 174 | /* init barrier */ |
| 175 | test_state = malloc(sizeof *test_state); |
| 176 | assert_non_null(test_state); |
| 177 | |
| 178 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 179 | assert_int_equal(ret, 0); |
| 180 | |
| 181 | *state = test_state; |
| 182 | |
| 183 | /* create new context */ |
| 184 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 185 | assert_int_equal(ret, 0); |
| 186 | |
| 187 | /* load default modules into context */ |
| 188 | ret = nc_server_init_ctx(&ctx); |
| 189 | assert_int_equal(ret, 0); |
| 190 | |
| 191 | /* load ietf-netconf-server module and it's imports into context */ |
| 192 | ret = nc_server_config_load_modules(&ctx); |
| 193 | assert_int_equal(ret, 0); |
| 194 | |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 195 | /* create the first SSH endpoint with a client reference to the second endpoint */ |
| 196 | ret = nc_server_config_new_ssh_hostkey(ctx, "SSH_endpt_1", "hostkey", TESTS_DIR "/data/key_rsa", NULL, &tree); |
| 197 | assert_int_equal(ret, 0); |
| 198 | |
| 199 | ret = nc_server_config_new_address_port(ctx, "SSH_endpt_1", NC_TI_LIBSSH, "127.0.0.1", "10005", &tree); |
| 200 | assert_int_equal(ret, 0); |
| 201 | |
| 202 | ret = nc_config_new_ssh_endpoint_client_reference(ctx, "SSH_endpt_1", "SSH_endpt_2", &tree); |
| 203 | assert_int_equal(ret, 0); |
| 204 | |
| 205 | /* create the second SSH endpoint with a single client */ |
| 206 | ret = nc_server_config_new_ssh_hostkey(ctx, "SSH_endpt_2", "hostkey", TESTS_DIR "/data/key_rsa", NULL, &tree); |
| 207 | assert_int_equal(ret, 0); |
| 208 | |
| 209 | ret = nc_server_config_new_address_port(ctx, "SSH_endpt_2", NC_TI_LIBSSH, "127.0.0.1", "10006", &tree); |
| 210 | assert_int_equal(ret, 0); |
| 211 | |
| 212 | ret = nc_server_config_new_ssh_client_auth_pubkey(ctx, "SSH_endpt_2", "client", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 213 | assert_int_equal(ret, 0); |
| 214 | |
| 215 | /* configure the server based on the yang data */ |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 216 | ret = nc_server_config_setup_diff(tree); |
| 217 | assert_int_equal(ret, 0); |
| 218 | |
| 219 | /* initialize the server */ |
| 220 | ret = nc_server_init(); |
| 221 | assert_int_equal(ret, 0); |
| 222 | |
| 223 | lyd_free_all(tree); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | setup_tls(void **state) |
| 230 | { |
| 231 | int ret; |
| 232 | struct lyd_node *tree = NULL; |
| 233 | struct test_state *test_state; |
| 234 | |
| 235 | nc_verbosity(NC_VERB_VERBOSE); |
| 236 | |
| 237 | /* init barrier */ |
| 238 | test_state = malloc(sizeof *test_state); |
| 239 | assert_non_null(test_state); |
| 240 | |
| 241 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 242 | assert_int_equal(ret, 0); |
| 243 | |
| 244 | *state = test_state; |
| 245 | |
| 246 | /* create new context */ |
| 247 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 248 | assert_int_equal(ret, 0); |
| 249 | |
| 250 | /* load default modules into context */ |
| 251 | ret = nc_server_init_ctx(&ctx); |
| 252 | assert_int_equal(ret, 0); |
| 253 | |
| 254 | /* load ietf-netconf-server module and it's imports into context */ |
| 255 | ret = nc_server_config_load_modules(&ctx); |
| 256 | assert_int_equal(ret, 0); |
| 257 | |
| 258 | /* create the first TLS endpoint with a single end entity client cert and a CTN entry */ |
| 259 | ret = nc_server_config_new_tls_server_certificate(ctx, "TLS_endpt_1", NULL, |
| 260 | TESTS_DIR "/data/server.key", TESTS_DIR "/data/server.crt", &tree); |
| 261 | assert_int_equal(ret, 0); |
| 262 | |
| 263 | ret = nc_server_config_new_address_port(ctx, "TLS_endpt_1", NC_TI_OPENSSL, "127.0.0.1", "10007", &tree); |
| 264 | assert_int_equal(ret, 0); |
| 265 | |
| 266 | ret = nc_server_config_new_tls_client_certificate(ctx, "TLS_endpt_1", "cert_client", TESTS_DIR "/data/client.crt", &tree); |
| 267 | assert_int_equal(ret, 0); |
| 268 | |
| 269 | ret = nc_server_config_new_tls_client_ca(ctx, "TLS_endpt_1", "cert_ca", TESTS_DIR "/data/serverca.pem", &tree); |
| 270 | assert_int_equal(ret, 0); |
| 271 | |
| 272 | ret = nc_server_config_new_tls_ctn(ctx, "TLS_endpt_1", 1, |
| 273 | "04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D", |
| 274 | NC_TLS_CTN_SPECIFIED, "client", &tree); |
| 275 | assert_int_equal(ret, 0); |
| 276 | |
| 277 | /* create the second TLS endpoint with a reference to the first endpoint */ |
| 278 | ret = nc_server_config_new_tls_server_certificate(ctx, "TLS_endpt_2", NULL, |
| 279 | TESTS_DIR "/data/server.key", TESTS_DIR "/data/server.crt", &tree); |
| 280 | assert_int_equal(ret, 0); |
| 281 | |
| 282 | ret = nc_server_config_new_address_port(ctx, "TLS_endpt_2", NC_TI_OPENSSL, "127.0.0.1", "10008", &tree); |
| 283 | assert_int_equal(ret, 0); |
| 284 | |
| 285 | ret = nc_config_new_tls_endpoint_client_reference(ctx, "TLS_endpt_2", "TLS_endpt_1", &tree); |
| 286 | assert_int_equal(ret, 0); |
| 287 | |
| 288 | /* configure the server based on the yang data */ |
| 289 | ret = nc_server_config_setup_diff(tree); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 290 | assert_int_equal(ret, 0); |
| 291 | |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 292 | /* initialize the server */ |
| 293 | ret = nc_server_init(); |
| 294 | assert_int_equal(ret, 0); |
| 295 | |
| 296 | lyd_free_all(tree); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | teardown_f(void **state) |
| 303 | { |
| 304 | int ret = 0; |
| 305 | struct test_state *test_state; |
| 306 | |
| 307 | assert_non_null(state); |
| 308 | test_state = *state; |
| 309 | |
| 310 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 311 | assert_int_equal(ret, 0); |
| 312 | |
| 313 | free(*state); |
| 314 | nc_client_destroy(); |
| 315 | nc_server_destroy(); |
| 316 | ly_ctx_destroy(ctx); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int |
| 322 | main(void) |
| 323 | { |
| 324 | const struct CMUnitTest tests[] = { |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 325 | cmocka_unit_test_setup_teardown(nc_test_endpt_share_clients_ssh, setup_ssh, teardown_f), |
| 326 | cmocka_unit_test_setup_teardown(nc_test_endpt_share_clients_tls, setup_tls, teardown_f), |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 330 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 331 | } |