| /** |
| * \file test_server_thread.c |
| * \author Michal Vasko <mvasko@cesnet.cz> |
| * \brief libnetconf2 tests - thread-safety of all server functions |
| * |
| * Copyright (c) 2015 CESNET, z.s.p.o. |
| * |
| * This source code is licensed under BSD 3-Clause License (the "License"). |
| * You may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * https://opensource.org/licenses/BSD-3-Clause |
| */ |
| |
| #include <pthread.h> |
| #include <unistd.h> |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <sys/types.h> |
| #include <sys/wait.h> |
| |
| #include <libyang/libyang.h> |
| |
| #include <session_client.h> |
| #include <session_server.h> |
| #include <log.h> |
| #include "config.h" |
| |
| /* millisec */ |
| #define NC_ACCEPT_TIMEOUT 5000 |
| /* millisec */ |
| #define NC_PS_POLL_TIMEOUT 5000 |
| /* sec */ |
| #define CLIENT_SSH_AUTH_TIMEOUT 10 |
| |
| #define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); } |
| |
| pthread_barrier_t barrier; |
| |
| #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| |
| static void * |
| server_thread(void *arg) |
| { |
| (void)arg; |
| NC_MSG_TYPE msgtype; |
| int ret; |
| struct nc_pollsession *ps; |
| struct nc_session *session; |
| |
| ps = nc_ps_new(); |
| nc_assert(ps); |
| |
| pthread_barrier_wait(&barrier); |
| |
| #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session); |
| nc_assert(msgtype == NC_MSG_HELLO); |
| |
| nc_ps_add_session(ps, session); |
| ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| nc_assert(ret & NC_PSPOLL_RPC); |
| nc_ps_clear(ps, 0, NULL); |
| #endif |
| |
| msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session); |
| nc_assert(msgtype == NC_MSG_HELLO); |
| |
| nc_ps_add_session(ps, session); |
| ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| nc_assert(ret & NC_PSPOLL_RPC); |
| nc_ps_clear(ps, 0, NULL); |
| |
| nc_ps_free(ps); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| |
| #ifdef NC_ENABLED_SSH |
| |
| static void * |
| add_endpt_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| ret = nc_server_add_endpt("tertiary", NC_TI_LIBSSH); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| del_endpt_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_del_endpt("secondary"); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_set_hostkey_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_add_hostkey("main_ssh", TESTS_DIR"/data/key_dsa"); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_set_banner_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_set_banner("main_ssh", "Howdy, partner!"); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_set_auth_methods_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_set_auth_methods("main_ssh", NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_set_auth_attempts_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_set_auth_attempts("main_ssh", 2); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_set_auth_timeout_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_set_auth_timeout("main_ssh", 5); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_add_authkey_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_add_authkey("main_ssh", TESTS_DIR"/data/key_rsa.pub", "test3"); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| ssh_endpt_del_authkey_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_ssh_endpt_del_authkey("main_ssh", TESTS_DIR"/data/key_ecdsa.pub", "test2"); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static int |
| ssh_hostkey_check_clb(const char *hostname, ssh_session session) |
| { |
| (void)hostname; |
| (void)session; |
| |
| return 0; |
| } |
| |
| static void * |
| ssh_client_thread(void *arg) |
| { |
| int ret, read_pipe = *(int *)arg; |
| char buf[9]; |
| struct nc_session *session; |
| |
| ret = read(read_pipe, buf, 9); |
| nc_assert(ret == 9); |
| nc_assert(!strncmp(buf, "ssh_ready", 9)); |
| |
| /* skip the knownhost check */ |
| nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb); |
| |
| ret = nc_client_ssh_set_username("test"); |
| nc_assert(!ret); |
| |
| ret = nc_client_ssh_add_keypair(TESTS_DIR"/data/key_dsa.pub", TESTS_DIR"/data/key_dsa"); |
| nc_assert(!ret); |
| |
| nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1); |
| nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| |
| session = nc_connect_ssh("127.0.0.1", 6001, NULL); |
| nc_assert(session); |
| |
| nc_session_free(session, NULL); |
| |
| fprintf(stdout, "SSH client finished.\n"); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| #endif /* NC_ENABLED_SSH */ |
| |
| #ifdef NC_ENABLED_TLS |
| |
| static void * |
| endpt_set_address_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_endpt_set_address("quaternary", "0.0.0.0"); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| endpt_set_port_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_endpt_set_port("quaternary", 6003); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_set_cert_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_set_cert("quaternary", "MIIEKjCCAxICCQDqSTPpuoUZkzANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJB\n" |
| "VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\n" |
| "cyBQdHkgTHRkMREwDwYDVQQDDAhzZXJ2ZXJjYTAeFw0xNjAyMDgxMTE0MzdaFw0y\n" |
| "NjAyMDUxMTE0MzdaMFYxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRl\n" |
| "MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDzANBgNVBAMMBnNl\n" |
| "cnZlcjCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOqI7Y3w5r8kD9WZ\n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
| "mbQIcTc7uNDE5chQWV8Z3Vxkp4yuqZM3HdLskoo4IgFDOoj8eCAi+58+YRuKpaEQ\n" |
| "fWt+A9rvlaOApWryMW4="); |
| nc_assert(!ret); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_set_key_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_set_key("quaternary", "MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n" |
| "ArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6al\n" |
| "wf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4g\n" |
| "fNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+Zc\n" |
| "zSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+d\n" |
| "KYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FK\n" |
| "tGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo\n" |
| "4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/f\n" |
| "t1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBT\n" |
| "oE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yV\n" |
| "ONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1NqBVkCAwEA\n" |
| "AQKCAgAeRZw75Oszoqj0jfMmMILdD3Cfad+dY3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Y9SMgLXYASO4S\n" |
| "SBz9wfGOmQIPk6KXNJkdV4kC7nNjIi75iwLLCgjHgUiHTrDq5sWekpeNnUoWsinb\n" |
| "Tsdsjnv3zHG9GyiClyLGxMbs4M5eyYECggEBAKuC8ZMpdIrjk6tERYB6g0LnQ7mW\n" |
| "8XYbDFAmLYMLs9yfG2jcjVbsW9Kugsr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPo\n" |
| "Swr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JB\n" |
| "dOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/K\n" |
| "qDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8T\n" |
| "bstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=", 1); |
| nc_assert(!ret); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_add_trusted_cert_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_add_trusted_cert("quaternary", "cert1", "MIIDgzCCAmugAwIBAgIJAL+y0WMRGax0MA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV\n" |
| "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n" |
| "aWRnaXRzIFB0eSBMdGQxETAPBgNVBAMMCGNsaWVudGNhMB4XDTE2MDExMTEyMTAx\n" |
| "OVoXDTE4MTAzMTEyMTAxOVowWDELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUt\n" |
| "U3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDERMA8GA1UE\n" |
| "AwwIY2xpZW50Y2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCw7Eyq\n" |
| "5T5tX6tAv5DHHfWNuaD/a3gVIBlGRWMAXkFWWJEa3o6leIjKxoDnL6tcBWNVJ+Gw\n" |
| "32MHerpHY6o5czsEHQ2XsOgodyFqe5cvx0kjQbjYQqnIMrslcdvSYuNe/ItqFP/w\n" |
| "uxb6kQbCYnCQKd/qhdhfoXjIHcnXpZzMCPKQ/uqls7LANJymtQkAuzydlf3+UqoG\n" |
| "4oo04GXK1Dc0A12cgCXxf+kWx7x34ctx2VEvDsJzw6LiZm8czOWjMFcuqqm/+kla\n" |
| "N3+6O7Z1kZlft/KNSrOYtc45xKNoSVrdVwFLkxipVDfOql6/DmWfE8iVmlX3QflO\n" |
| "u3+fzZZQpR5jYzUNAgMBAAGjUDBOMB0GA1UdDgQWBBTjBbQJ6p/mjnjBWXLgXXXW\n" |
| "a3ieoTAfBgNVHSMEGDAWgBTjBbQJ6p/mjnjBWXLgXXXWa3ieoTAMBgNVHRMEBTAD\n" |
| "AQH/MA0GCSqGSIb3DQEBBQUAA4IBAQAZr9b0YTaDV5XZr/QQPP1pvHkN3Ezbm9F4\n" |
| "MiYe4e0QnM9JtjNLDKq1dDnqVDQ/BYdupWWh0398tObFACssWkm4aubPG7LVh5Ck\n" |
| "O8I8i/GHiXYLmYT22hslWe5dFvidUICkTXoj1h5X2vwfBrNTI1+gnVXXw842xCvU\n" |
| "sgq28vGMSXLSYKBNaP/llXNmqW35oLs6CwVuiCL7Go0IDIOmiXN2bssb87hZSw3B\n" |
| "6iwU78wYshJUGZjLaK9PuMvFYJLFWSAePA2Yb+aEv80wMbX1oANSryU7Uf5BJk8V\n" |
| "kO3mlRDh2b1/5Gb5xA2vU2z3ReHdPNy6qSx0Mk4XJvQw9FsVHZ13"); |
| nc_assert(!ret); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_set_trusted_ca_paths_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR"/data/serverca.pem", "data"); |
| nc_assert(!ret); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_del_trusted_cert_thread(void *arg) |
| { |
| (void)arg; |
| |
| pthread_barrier_wait(&barrier); |
| |
| nc_server_tls_endpt_del_trusted_cert("quaternary", "cert1"); |
| |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_set_crl_paths_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data"); |
| nc_assert(!ret); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_clear_crls_thread(void *arg) |
| { |
| (void)arg; |
| |
| pthread_barrier_wait(&barrier); |
| |
| nc_server_tls_endpt_clear_crls("quaternary"); |
| |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_add_ctn_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_add_ctn("main_tls", 0, "02:F0:F1:F2:F3:F4:F5:F6:F7:F8:F9:10:11:12:EE:FF:A0:A1:A2:A3", |
| NC_TLS_CTN_SAN_IP_ADDRESS, NULL); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| tls_endpt_del_ctn_thread(void *arg) |
| { |
| (void)arg; |
| int ret; |
| |
| pthread_barrier_wait(&barrier); |
| |
| ret = nc_server_tls_endpt_del_ctn("main_tls", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL); |
| nc_assert(!ret); |
| |
| return NULL; |
| } |
| |
| static void * |
| tls_client_thread(void *arg) |
| { |
| int ret, read_pipe = *(int *)arg; |
| char buf[9]; |
| struct nc_session *session; |
| |
| ret = read(read_pipe, buf, 9); |
| nc_assert(ret == 9); |
| nc_assert(!strncmp(buf, "tls_ready", 9)); |
| |
| ret = nc_client_tls_set_cert_key_paths(TESTS_DIR"/data/client.crt", TESTS_DIR"/data/client.key"); |
| nc_assert(!ret); |
| ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR"/data"); |
| nc_assert(!ret); |
| |
| session = nc_connect_tls("127.0.0.1", 6501, NULL); |
| nc_assert(session); |
| |
| nc_session_free(session, NULL); |
| |
| fprintf(stdout, "TLS client finished.\n"); |
| |
| nc_thread_destroy(); |
| return NULL; |
| } |
| |
| #endif /* NC_ENABLED_TLS */ |
| |
| static void *(*thread_funcs[])(void *) = { |
| #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| server_thread, |
| add_endpt_thread, |
| del_endpt_thread, |
| endpt_set_address_thread, |
| endpt_set_port_thread, |
| #endif |
| #ifdef NC_ENABLED_SSH |
| ssh_endpt_set_hostkey_thread, |
| ssh_endpt_set_banner_thread, |
| ssh_endpt_set_auth_methods_thread, |
| ssh_endpt_set_auth_attempts_thread, |
| ssh_endpt_set_auth_timeout_thread, |
| ssh_endpt_add_authkey_thread, |
| ssh_endpt_del_authkey_thread, |
| #endif |
| #ifdef NC_ENABLED_TLS |
| tls_endpt_set_cert_thread, |
| tls_endpt_set_key_thread, |
| tls_endpt_add_trusted_cert_thread, |
| tls_endpt_set_trusted_ca_paths_thread, |
| tls_endpt_del_trusted_cert_thread, |
| tls_endpt_set_crl_paths_thread, |
| tls_endpt_clear_crls_thread, |
| tls_endpt_add_ctn_thread, |
| tls_endpt_del_ctn_thread, |
| #endif |
| }; |
| |
| const int thread_count = sizeof thread_funcs / sizeof *thread_funcs; |
| |
| #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| const int client_count = 2; |
| pid_t pids[2]; |
| int pipes[4]; |
| #else |
| const int client_count = 1; |
| pid_t pids[1]; |
| int pipes[2]; |
| #endif |
| |
| static void |
| client_fork(void) |
| { |
| int ret, clients = 0; |
| |
| #ifdef NC_ENABLED_SSH |
| pipe(pipes + clients * 2); |
| |
| if (!(pids[clients] = fork())) { |
| nc_client_init(); |
| |
| ret = nc_client_set_schema_searchpath(TESTS_DIR"/../schemas"); |
| nc_assert(!ret); |
| |
| /* close write */ |
| close(pipes[clients * 2 + 1]); |
| ssh_client_thread(&pipes[clients * 2]); |
| close(pipes[clients * 2]); |
| nc_client_destroy(); |
| exit(0); |
| } |
| /* close read */ |
| close(pipes[clients * 2]); |
| |
| ++clients; |
| #endif |
| |
| #ifdef NC_ENABLED_TLS |
| pipe(pipes + clients * 2); |
| |
| if (!(pids[clients] = fork())) { |
| nc_client_init(); |
| |
| ret = nc_client_set_schema_searchpath(TESTS_DIR"/../schemas"); |
| nc_assert(!ret); |
| |
| /* close write */ |
| close(pipes[clients * 2 + 1]); |
| tls_client_thread(&pipes[clients * 2]); |
| close(pipes[clients * 2]); |
| nc_client_destroy(); |
| exit(0); |
| } |
| /* close read */ |
| close(pipes[clients * 2]); |
| |
| ++clients; |
| #endif |
| } |
| |
| int |
| main(void) |
| { |
| struct ly_ctx *ctx; |
| int ret, i, clients = 0; |
| pthread_t tids[thread_count]; |
| |
| nc_verbosity(NC_VERB_VERBOSE); |
| |
| client_fork(); |
| |
| ctx = ly_ctx_new(TESTS_DIR"/../schemas"); |
| nc_assert(ctx); |
| ly_ctx_load_module(ctx, "ietf-netconf", NULL); |
| nc_server_init(ctx); |
| |
| pthread_barrier_init(&barrier, NULL, thread_count); |
| |
| #ifdef NC_ENABLED_SSH |
| /* do first, so that client can connect on SSH */ |
| ret = nc_server_add_endpt("main_ssh", NC_TI_LIBSSH); |
| nc_assert(!ret); |
| ret = nc_server_endpt_set_address("main_ssh", "0.0.0.0"); |
| nc_assert(!ret); |
| ret = nc_server_endpt_set_port("main_ssh", 6001); |
| nc_assert(!ret); |
| ret = nc_server_ssh_endpt_add_authkey("main_ssh", TESTS_DIR"/data/key_dsa.pub", "test"); |
| nc_assert(!ret); |
| ret = nc_server_ssh_endpt_add_hostkey("main_ssh", TESTS_DIR"/data/key_rsa"); |
| nc_assert(!ret); |
| |
| /* client ready */ |
| ret = write(pipes[clients * 2 + 1], "ssh_ready", 9); |
| nc_assert(ret == 9); |
| ++clients; |
| |
| /* for ssh_endpt_del_authkey */ |
| ret = nc_server_ssh_endpt_add_authkey("main_ssh", TESTS_DIR"/data/key_ecdsa.pub", "test2"); |
| nc_assert(!ret); |
| |
| ret = nc_server_add_endpt("secondary", NC_TI_LIBSSH); |
| nc_assert(!ret); |
| #endif |
| |
| #ifdef NC_ENABLED_TLS |
| /* do first, so that client can connect on TLS */ |
| ret = nc_server_add_endpt("main_tls", NC_TI_OPENSSL); |
| nc_assert(!ret); |
| ret = nc_server_endpt_set_address("main_tls", "0.0.0.0"); |
| nc_assert(!ret); |
| ret = nc_server_endpt_set_port("main_tls", 6501); |
| nc_assert(!ret); |
| ret = nc_server_tls_endpt_set_cert_path("main_tls", TESTS_DIR"/data/server.crt"); |
| nc_assert(!ret); |
| ret = nc_server_tls_endpt_set_key_path("main_tls", TESTS_DIR"/data/server.key"); |
| nc_assert(!ret); |
| ret = nc_server_tls_endpt_add_trusted_cert_path("main_tls", "client", TESTS_DIR"/data/client.crt"); |
| nc_assert(!ret); |
| ret = nc_server_tls_endpt_add_ctn("main_tls", 0, "02:D3:03:0E:77:21:E2:14:1F:E5:75:48:98:6B:FD:8A:63:BB:DE:40:34", NC_TLS_CTN_SPECIFIED, "test"); |
| nc_assert(!ret); |
| |
| /* client ready */ |
| ret = write(pipes[clients * 2 + 1], "tls_ready", 9); |
| nc_assert(ret == 9); |
| ++clients; |
| |
| /* for tls_endpt_del_ctn */ |
| ret = nc_server_tls_endpt_add_ctn("main_tls", 0, "02:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:A0:A1:A2:A3", NC_TLS_CTN_SAN_ANY, NULL); |
| nc_assert(!ret); |
| |
| ret = nc_server_add_endpt("quaternary", NC_TI_OPENSSL); |
| nc_assert(!ret); |
| #endif |
| |
| /* threads'n'stuff */ |
| ret = 0; |
| for (i = 0; i < thread_count; ++i) { |
| ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL); |
| } |
| nc_assert(!ret); |
| |
| /* cleanup */ |
| for (i = 0; i < thread_count; ++i) { |
| pthread_join(tids[i], NULL); |
| } |
| for (i = 0; i < client_count; ++i) { |
| waitpid(pids[i], NULL, 0); |
| close(pipes[i * 2 + 1]); |
| } |
| |
| pthread_barrier_destroy(&barrier); |
| |
| nc_server_destroy(); |
| ly_ctx_destroy(ctx, NULL); |
| |
| return 0; |
| } |