blob: e42b6f786673af5a6880dff4de9a49792f1359bb [file] [log] [blame]
romand348b942023-10-13 14:32:19 +02001/**
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
16#define _GNU_SOURCE
17
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
26#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);
64 return NULL;
65}
66
67static 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
78static void *
79client_thread(void *arg)
80{
81 int ret;
82 struct nc_session *session = NULL;
83 struct test_state *state = arg;
84
85 /* skip all hostkey and known_hosts checks */
86 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
87
88 ret = nc_client_set_schema_searchpath(MODULES_DIR);
89 assert_int_equal(ret, 0);
90
91 ret = nc_client_ssh_set_username("client");
92 assert_int_equal(ret, 0);
93
94 nc_client_ssh_set_auth_password_clb(auth_password, NULL);
95
96 pthread_barrier_wait(&state->barrier);
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020097 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
romand348b942023-10-13 14:32:19 +020098 assert_non_null(session);
99
100 nc_session_free(session, NULL);
101 return NULL;
102}
103
104static void
105test_nc_config_new(void **state)
106{
107 int ret, i;
108 pthread_t tids[2];
109
110 assert_non_null(state);
111
112 ret = pthread_create(&tids[0], NULL, client_thread, *state);
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
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
140 /* new context */
141 ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
142 assert_int_equal(ret, 0);
143
144 /* initialize the context by loading default modules */
145 ret = nc_server_init_ctx(&ctx);
146 assert_int_equal(ret, 0);
147
148 /* load ietf-netconf-server module and it's imports */
149 ret = nc_server_config_load_modules(&ctx);
150 assert_int_equal(ret, 0);
151
152 /* create new hostkey data */
153 ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree);
154 assert_int_equal(ret, 0);
155
156 /* create new address and port data */
romand8973d12024-04-25 14:57:18 +0200157 ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
romand348b942023-10-13 14:32:19 +0200158 assert_int_equal(ret, 0);
159
160 /* create the host-key algorithms data */
161 ret = nc_server_config_add_ssh_host_key_algs(ctx, "endpt", &tree, 1, "rsa-sha2-512");
162 assert_int_equal(ret, 0);
163
164 /* create the client authentication data, password only */
165 ret = nc_server_config_add_ssh_user_password(ctx, "endpt", "client", "testpassword123", &tree);
166 assert_int_equal(ret, 0);
167
168 /* configure the server based on the data */
169 ret = nc_server_config_setup_data(tree);
170 assert_int_equal(ret, 0);
171
172 ret = nc_server_init();
173 assert_int_equal(ret, 0);
174
175 /* initialize client */
176 ret = nc_client_init();
177 assert_int_equal(ret, 0);
178
179 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[] = {
208 cmocka_unit_test_setup_teardown(test_nc_config_new, setup_f, teardown_f),
209 };
210
211 setenv("CMOCKA_TEST_ABORT", "1", 1);
212 return cmocka_run_group_tests(tests, NULL, NULL);
213}