roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file test_keystore.c |
| 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
| 4 | * @brief libnetconf2 Linux PAM keyboard-interactive authentication test |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2022 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 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 18 | #include <pthread.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include <cmocka.h> |
| 25 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 26 | #include "tests/config.h" |
| 27 | |
| 28 | #define NC_ACCEPT_TIMEOUT 2000 |
| 29 | #define NC_PS_POLL_TIMEOUT 2000 |
| 30 | |
| 31 | struct ly_ctx *ctx; |
| 32 | |
| 33 | struct test_state { |
| 34 | pthread_barrier_t barrier; |
| 35 | }; |
| 36 | |
| 37 | static void * |
| 38 | server_thread(void *arg) |
| 39 | { |
| 40 | int ret; |
| 41 | NC_MSG_TYPE msgtype; |
| 42 | struct nc_session *session; |
| 43 | struct nc_pollsession *ps; |
| 44 | struct test_state *state = arg; |
| 45 | |
| 46 | ps = nc_ps_new(); |
| 47 | assert_non_null(ps); |
| 48 | |
| 49 | /* accept a session and add it to the poll session structure */ |
| 50 | pthread_barrier_wait(&state->barrier); |
| 51 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 52 | assert_int_equal(msgtype, NC_MSG_HELLO); |
| 53 | |
| 54 | ret = nc_ps_add_session(ps, session); |
| 55 | assert_int_equal(ret, 0); |
| 56 | |
| 57 | do { |
| 58 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 59 | assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC); |
| 60 | } while (!(ret & NC_PSPOLL_SESSION_TERM)); |
| 61 | |
| 62 | nc_ps_clear(ps, 1, NULL); |
| 63 | nc_ps_free(ps); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
| 66 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 67 | static char * |
| 68 | auth_password(const char *username, const char *hostname, void *priv) |
| 69 | { |
| 70 | (void) username; |
| 71 | (void) hostname; |
| 72 | (void) priv; |
| 73 | |
| 74 | /* set the reply to password authentication */ |
| 75 | return strdup("testpassword123"); |
| 76 | } |
| 77 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 78 | static void * |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 79 | client_thread(void *arg) |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 80 | { |
| 81 | int ret; |
| 82 | struct nc_session *session = NULL; |
| 83 | struct test_state *state = arg; |
| 84 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 85 | /* skip all hostkey and known_hosts checks */ |
| 86 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 87 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 88 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 89 | assert_int_equal(ret, 0); |
| 90 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 91 | ret = nc_client_ssh_set_username("client"); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 92 | assert_int_equal(ret, 0); |
| 93 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 94 | nc_client_ssh_set_auth_password_clb(auth_password, NULL); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 95 | |
| 96 | pthread_barrier_wait(&state->barrier); |
| 97 | session = nc_connect_ssh("127.0.0.1", 10005, NULL); |
| 98 | assert_non_null(session); |
| 99 | |
| 100 | nc_session_free(session, NULL); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | static void |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 105 | test_nc_config_new(void **state) |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 106 | { |
| 107 | int ret, i; |
| 108 | pthread_t tids[2]; |
| 109 | |
| 110 | assert_non_null(state); |
| 111 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 112 | ret = pthread_create(&tids[0], NULL, client_thread, *state); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 113 | assert_int_equal(ret, 0); |
| 114 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 115 | assert_int_equal(ret, 0); |
| 116 | |
| 117 | for (i = 0; i < 2; i++) { |
| 118 | pthread_join(tids[i], NULL); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | setup_f(void **state) |
| 124 | { |
| 125 | int ret; |
| 126 | struct lyd_node *tree = NULL; |
| 127 | struct test_state *test_state; |
| 128 | |
| 129 | nc_verbosity(NC_VERB_VERBOSE); |
| 130 | |
| 131 | /* init barrier */ |
| 132 | test_state = malloc(sizeof *test_state); |
| 133 | assert_non_null(test_state); |
| 134 | |
| 135 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 136 | assert_int_equal(ret, 0); |
| 137 | |
| 138 | *state = test_state; |
| 139 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 140 | /* new context */ |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 141 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 142 | assert_int_equal(ret, 0); |
| 143 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 144 | /* initialize the context by loading default modules */ |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 145 | ret = nc_server_init_ctx(&ctx); |
| 146 | assert_int_equal(ret, 0); |
| 147 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 148 | /* load ietf-netconf-server module and it's imports */ |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 149 | ret = nc_server_config_load_modules(&ctx); |
| 150 | assert_int_equal(ret, 0); |
| 151 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 152 | /* create new hostkey data */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 153 | ret = nc_server_config_new_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 154 | assert_int_equal(ret, 0); |
| 155 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 156 | /* create new address and port data */ |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 157 | ret = nc_server_config_new_address_port(ctx, "endpt", NC_TI_LIBSSH, "127.0.0.1", 10005, &tree); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 158 | assert_int_equal(ret, 0); |
| 159 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 160 | /* create the host-key algorithms data */ |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame] | 161 | ret = nc_server_config_new_ssh_host_key_algs(ctx, "endpt", &tree, 1, "rsa-sha2-512"); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 162 | assert_int_equal(ret, 0); |
| 163 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 164 | /* create the client authentication data, password only */ |
roman | 8ba6efa | 2023-07-12 15:27:52 +0200 | [diff] [blame] | 165 | ret = nc_server_config_new_ssh_user_password(ctx, "endpt", "client", "testpassword123", &tree); |
roman | 4f9bb44 | 2023-03-24 09:05:37 +0100 | [diff] [blame] | 166 | assert_int_equal(ret, 0); |
| 167 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 168 | /* configure the server based on the data */ |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 169 | ret = nc_server_config_setup_data(tree); |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 170 | assert_int_equal(ret, 0); |
| 171 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 172 | ret = nc_server_init(); |
| 173 | assert_int_equal(ret, 0); |
| 174 | |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 175 | lyd_free_all(tree); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int |
| 181 | teardown_f(void **state) |
| 182 | { |
| 183 | int ret = 0; |
| 184 | struct test_state *test_state; |
| 185 | |
| 186 | assert_non_null(state); |
| 187 | test_state = *state; |
| 188 | |
| 189 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 190 | assert_int_equal(ret, 0); |
| 191 | |
| 192 | free(*state); |
| 193 | nc_client_destroy(); |
| 194 | nc_server_destroy(); |
| 195 | ly_ctx_destroy(ctx); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int |
| 201 | main(void) |
| 202 | { |
| 203 | const struct CMUnitTest tests[] = { |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 204 | cmocka_unit_test_setup_teardown(test_nc_config_new, setup_f, teardown_f), |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 208 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 209 | } |