blob: 72dda366806d8e407bf78fe826da35f27b2cb87c [file] [log] [blame]
roman27215242023-03-10 14:55:00 +01001/**
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
roman9b1379c2023-03-31 10:11:10 +020016#define _GNU_SOURCE
17
roman27215242023-03-10 14:55:00 +010018#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
roman27215242023-03-10 14:55:00 +010026#include "tests/config.h"
27
28#define NC_ACCEPT_TIMEOUT 2000
29#define NC_PS_POLL_TIMEOUT 2000
30
31struct ly_ctx *ctx;
32
33struct test_state {
34 pthread_barrier_t barrier;
35};
36
37static void *
38server_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);
roman27215242023-03-10 14:55:00 +010064 return NULL;
65}
66
roman9b1379c2023-03-31 10:11:10 +020067static char *
68auth_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
roman27215242023-03-10 14:55:00 +010078static void *
roman9b1379c2023-03-31 10:11:10 +020079client_thread(void *arg)
roman27215242023-03-10 14:55:00 +010080{
81 int ret;
82 struct nc_session *session = NULL;
83 struct test_state *state = arg;
84
roman472420c2023-04-24 16:28:09 +020085 /* skip all hostkey and known_hosts checks */
86 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
87
roman27215242023-03-10 14:55:00 +010088 ret = nc_client_set_schema_searchpath(MODULES_DIR);
89 assert_int_equal(ret, 0);
90
roman9b1379c2023-03-31 10:11:10 +020091 ret = nc_client_ssh_set_username("client");
roman27215242023-03-10 14:55:00 +010092 assert_int_equal(ret, 0);
93
roman9b1379c2023-03-31 10:11:10 +020094 nc_client_ssh_set_auth_password_clb(auth_password, NULL);
roman27215242023-03-10 14:55:00 +010095
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);
roman27215242023-03-10 14:55:00 +0100101 return NULL;
102}
103
104static void
roman9b1379c2023-03-31 10:11:10 +0200105test_nc_config_new(void **state)
roman27215242023-03-10 14:55:00 +0100106{
107 int ret, i;
108 pthread_t tids[2];
109
110 assert_non_null(state);
111
roman9b1379c2023-03-31 10:11:10 +0200112 ret = pthread_create(&tids[0], NULL, client_thread, *state);
roman27215242023-03-10 14:55:00 +0100113 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
122static int
123setup_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
roman9b1379c2023-03-31 10:11:10 +0200140 /* new context */
roman27215242023-03-10 14:55:00 +0100141 ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
142 assert_int_equal(ret, 0);
143
roman9b1379c2023-03-31 10:11:10 +0200144 /* initialize the context by loading default modules */
roman27215242023-03-10 14:55:00 +0100145 ret = nc_server_init_ctx(&ctx);
146 assert_int_equal(ret, 0);
147
roman9b1379c2023-03-31 10:11:10 +0200148 /* load ietf-netconf-server module and it's imports */
roman27215242023-03-10 14:55:00 +0100149 ret = nc_server_config_load_modules(&ctx);
150 assert_int_equal(ret, 0);
151
roman9b1379c2023-03-31 10:11:10 +0200152 /* create new hostkey data */
roman3f9b65c2023-06-05 14:26:58 +0200153 ret = nc_server_config_new_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree);
roman27215242023-03-10 14:55:00 +0100154 assert_int_equal(ret, 0);
155
roman9b1379c2023-03-31 10:11:10 +0200156 /* create new address and port data */
roman142718b2023-06-29 09:15:29 +0200157 ret = nc_server_config_new_address_port(ctx, "endpt", NC_TI_LIBSSH, "127.0.0.1", 10005, &tree);
roman27215242023-03-10 14:55:00 +0100158 assert_int_equal(ret, 0);
159
roman9b1379c2023-03-31 10:11:10 +0200160 /* create the host-key algorithms data */
roman466719d2023-05-05 16:14:37 +0200161 ret = nc_server_config_new_ssh_host_key_algs(ctx, "endpt", &tree, 1, "rsa-sha2-512");
roman27215242023-03-10 14:55:00 +0100162 assert_int_equal(ret, 0);
163
roman9b1379c2023-03-31 10:11:10 +0200164 /* create the client authentication data, password only */
roman8ba6efa2023-07-12 15:27:52 +0200165 ret = nc_server_config_new_ssh_user_password(ctx, "endpt", "client", "testpassword123", &tree);
roman4f9bb442023-03-24 09:05:37 +0100166 assert_int_equal(ret, 0);
167
roman27215242023-03-10 14:55:00 +0100168 /* configure the server based on the data */
roman142718b2023-06-29 09:15:29 +0200169 ret = nc_server_config_setup_data(tree);
roman27215242023-03-10 14:55:00 +0100170 assert_int_equal(ret, 0);
171
roman27215242023-03-10 14:55:00 +0100172 ret = nc_server_init();
173 assert_int_equal(ret, 0);
174
romaneadc4782023-09-14 10:10:08 +0200175 /* initialize client */
176 ret = nc_client_init();
177 assert_int_equal(ret, 0);
178
roman27215242023-03-10 14:55:00 +0100179 lyd_free_all(tree);
180
181 return 0;
182}
183
184static int
185teardown_f(void **state)
186{
187 int ret = 0;
188 struct test_state *test_state;
189
190 assert_non_null(state);
191 test_state = *state;
192
193 ret = pthread_barrier_destroy(&test_state->barrier);
194 assert_int_equal(ret, 0);
195
196 free(*state);
197 nc_client_destroy();
198 nc_server_destroy();
199 ly_ctx_destroy(ctx);
200
201 return 0;
202}
203
204int
205main(void)
206{
207 const struct CMUnitTest tests[] = {
roman9b1379c2023-03-31 10:11:10 +0200208 cmocka_unit_test_setup_teardown(test_nc_config_new, setup_f, teardown_f),
roman27215242023-03-10 14:55:00 +0100209 };
210
211 setenv("CMOCKA_TEST_ABORT", "1", 1);
212 return cmocka_run_group_tests(tests, NULL, NULL);
213}