blob: 24e7353a02f75da549f9dc2c5dec779fd95daebb [file] [log] [blame]
romanc1d2b092023-02-02 08:58:27 +01001/**
roman142718b2023-06-29 09:15:29 +02002 * @file test_two_channels.c
romanc1d2b092023-02-02 08:58:27 +01003 * @author Roman Janota <xjanot04@fit.vutbr.cz>
roman142718b2023-06-29 09:15:29 +02004 * @brief libnetconf2 Openning a new session on an established SSH channel test.
romanc1d2b092023-02-02 08:58:27 +01005 *
6 * @copyright
roman142718b2023-06-29 09:15:29 +02007 * Copyright (c) 2023 CESNET, z.s.p.o.
romanc1d2b092023-02-02 08:58:27 +01008 *
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 */
roman9b1379c2023-03-31 10:11:10 +020015#define _GNU_SOURCE
romanc1d2b092023-02-02 08:58:27 +010016
17#include <errno.h>
18#include <pthread.h>
romanc1d2b092023-02-02 08:58:27 +010019#include <setjmp.h>
roman9b1379c2023-03-31 10:11:10 +020020#include <stdarg.h>
romanc1d2b092023-02-02 08:58:27 +010021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
roman9b1379c2023-03-31 10:11:10 +020024#include <unistd.h>
romanc1d2b092023-02-02 08:58:27 +010025
26#include <cmocka.h>
27
romanc1d2b092023-02-02 08:58:27 +010028#include "tests/config.h"
29
roman83683fb2023-02-24 09:15:23 +010030#define NC_ACCEPT_TIMEOUT 2000
31#define NC_PS_POLL_TIMEOUT 2000
romanc1d2b092023-02-02 08:58:27 +010032#define BACKOFF_TIMEOUT_USECS 100
33
34struct ly_ctx *ctx;
romanc1d2b092023-02-02 08:58:27 +010035
36static void *
37server_thread(void *arg)
38{
39 int ret, del_session_count = 0, sleep_count = 0;
40 NC_MSG_TYPE msgtype;
41 struct nc_session *session, *new_session;
42 struct nc_pollsession *ps;
43
44 (void) arg;
45
46 ps = nc_ps_new();
47 assert_non_null(ps);
48
49 while (del_session_count < 2) {
50 msgtype = nc_accept(0, ctx, &session);
51
52 if (msgtype == NC_MSG_HELLO) {
53 ret = nc_ps_add_session(ps, session);
54 assert_int_equal(ret, 0);
55 }
56
57 ret = nc_ps_poll(ps, 0, &new_session);
58
59 if (ret & NC_PSPOLL_SESSION_TERM) {
60 nc_ps_del_session(ps, new_session);
61 nc_session_free(new_session, NULL);
62 del_session_count++;
63 } else if (ret & NC_PSPOLL_SSH_CHANNEL) {
64 msgtype = nc_session_accept_ssh_channel(session, &new_session);
65 if (msgtype == NC_MSG_HELLO) {
66 ret = nc_ps_add_session(ps, new_session);
67 assert_int_equal(ret, 0);
68 }
69 } else if (ret & NC_PS_POLL_TIMEOUT) {
70 usleep(BACKOFF_TIMEOUT_USECS);
71 sleep_count++;
72 assert_int_not_equal(sleep_count, 50000);
73 }
74 }
75
76 nc_ps_free(ps);
romanc1d2b092023-02-02 08:58:27 +010077 return NULL;
78}
79
romanc1d2b092023-02-02 08:58:27 +010080static void *
81client_thread(void *arg)
82{
83 (void) arg;
84 int ret;
85 struct nc_session *session_cl1, *session_cl2;
86
roman472420c2023-04-24 16:28:09 +020087 /* skip all hostkey and known_hosts checks */
88 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
89
romanc1d2b092023-02-02 08:58:27 +010090 ret = nc_client_set_schema_searchpath(MODULES_DIR);
91 assert_int_equal(ret, 0);
romanc1d2b092023-02-02 08:58:27 +010092
roman142718b2023-06-29 09:15:29 +020093 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ed25519.pub", TESTS_DIR "/data/id_ed25519");
romanc1d2b092023-02-02 08:58:27 +010094 assert_int_equal(ret, 0);
95
roman142718b2023-06-29 09:15:29 +020096 ret = nc_client_ssh_set_username("client_1");
romanc1d2b092023-02-02 08:58:27 +010097 assert_int_equal(ret, 0);
98
99 session_cl1 = nc_connect_ssh("127.0.0.1", 10005, NULL);
100 assert_non_null(session_cl1);
101
roman142718b2023-06-29 09:15:29 +0200102 ret = nc_client_ssh_set_username("client_2");
103 assert_int_equal(ret, 0);
104
105 ret = nc_client_ssh_del_keypair(0);
106 assert_int_equal(ret, 0);
107
108 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ecdsa521.pub", TESTS_DIR "/data/id_ecdsa521");
romanc1d2b092023-02-02 08:58:27 +0100109 assert_int_equal(ret, 0);
110
111 session_cl2 = nc_connect_ssh_channel(session_cl1, NULL);
112 assert_non_null(session_cl2);
113
114 nc_client_destroy();
115 nc_session_free(session_cl1, NULL);
116 nc_session_free(session_cl2, NULL);
romanc1d2b092023-02-02 08:58:27 +0100117 return NULL;
118}
119
120static void
121test_nc_two_channels(void **state)
122{
123 int ret, i;
124 pthread_t tids[2];
125
126 (void) state;
127
128 ret = pthread_create(&tids[0], NULL, client_thread, NULL);
129 assert_int_equal(ret, 0);
130 ret = pthread_create(&tids[1], NULL, server_thread, NULL);
131 assert_int_equal(ret, 0);
132
133 for (i = 0; i < 2; i++) {
134 pthread_join(tids[i], NULL);
135 }
136}
137
138static int
139setup_f(void **state)
140{
141 int ret;
roman142718b2023-06-29 09:15:29 +0200142 struct lyd_node *tree = NULL;
romanc1d2b092023-02-02 08:58:27 +0100143
144 (void) state;
145
146 nc_verbosity(NC_VERB_VERBOSE);
147
148 ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
149 assert_int_equal(ret, 0);
150
151 ret = nc_server_init_ctx(&ctx);
152 assert_int_equal(ret, 0);
153
154 ret = nc_server_config_load_modules(&ctx);
155 assert_int_equal(ret, 0);
156
Roytakb2794852023-10-18 14:30:22 +0200157 ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_LIBSSH, "127.0.0.1", 10005, &tree);
romanc1d2b092023-02-02 08:58:27 +0100158 assert_int_equal(ret, 0);
159
Roytakb2794852023-10-18 14:30:22 +0200160 ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
roman142718b2023-06-29 09:15:29 +0200161 assert_int_equal(ret, 0);
162
Roytakb2794852023-10-18 14:30:22 +0200163 ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "client_1", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
roman142718b2023-06-29 09:15:29 +0200164 assert_int_equal(ret, 0);
165
Roytakb2794852023-10-18 14:30:22 +0200166 ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "client_2", "pubkey", TESTS_DIR "/data/id_ecdsa521.pub", &tree);
roman142718b2023-06-29 09:15:29 +0200167 assert_int_equal(ret, 0);
168
169 ret = nc_server_config_setup_data(tree);
romanc1d2b092023-02-02 08:58:27 +0100170 assert_int_equal(ret, 0);
171
172 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
romanc1d2b092023-02-02 08:58:27 +0100179 lyd_free_all(tree);
180
181 return 0;
182}
183
184static int
185teardown_f(void **state)
186{
187 (void) state;
188
189 nc_server_destroy();
190 ly_ctx_destroy(ctx);
191
192 return 0;
193}
194
195int
196main(void)
197{
198 const struct CMUnitTest tests[] = {
199 cmocka_unit_test_setup_teardown(test_nc_two_channels, setup_f, teardown_f),
200 };
201
202 setenv("CMOCKA_TEST_ABORT", "1", 1);
203 return cmocka_run_group_tests(tests, NULL, NULL);
204}