Skip to content

Commit 2ca058f

Browse files
romanmichalvasko
authored andcommitted
tests UPDATE use shared ln2_test functionality
1 parent 33d044d commit 2ca058f

19 files changed

Lines changed: 346 additions & 1320 deletions

tests/CMakeLists.txt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,31 @@ function(libnetconf2_test)
6060
endfunction()
6161

6262
# all the tests that don't require SSH and TLS
63-
libnetconf2_test(NAME test_unix_socket)
63+
libnetconf2_test(NAME test_client_messages)
6464
libnetconf2_test(NAME test_client_thread)
6565
libnetconf2_test(NAME test_fd_comm)
6666
libnetconf2_test(NAME test_io)
6767
libnetconf2_test(NAME test_thread_messages)
68-
libnetconf2_test(NAME test_client_messages)
68+
libnetconf2_test(NAME test_unix_socket)
6969

7070
# tests depending on SSH/TLS
7171
if(ENABLE_SSH_TLS)
7272
libnetconf2_test(NAME test_auth)
73-
libnetconf2_test(NAME test_two_channels)
74-
libnetconf2_test(NAME test_ks_ts)
73+
libnetconf2_test(NAME test_authkeys)
74+
libnetconf2_test(NAME test_ch PORT_COUNT 2)
7575
libnetconf2_test(NAME test_ec)
7676
libnetconf2_test(NAME test_ed25519)
77-
libnetconf2_test(NAME test_replace)
7877
libnetconf2_test(NAME test_endpt_share_clients PORT_COUNT 4)
79-
libnetconf2_test(NAME test_tls)
80-
libnetconf2_test(NAME test_ch PORT_COUNT 2)
81-
libnetconf2_test(NAME test_runtime_changes PORT_COUNT 2)
82-
libnetconf2_test(NAME test_authkeys)
78+
libnetconf2_test(NAME test_ks_ts)
8379
if (LIBPAM_HAVE_CONFDIR)
8480
libnetconf2_test(NAME test_pam WRAP_FUNCS pam_start)
8581
endif()
82+
libnetconf2_test(NAME test_replace)
83+
libnetconf2_test(NAME test_runtime_changes PORT_COUNT 2)
84+
libnetconf2_test(NAME test_tls)
85+
libnetconf2_test(NAME test_two_channels)
8686
endif()
8787

88-
8988
include_directories(${CMAKE_SOURCE_DIR}/src ${PROJECT_BINARY_DIR})
9089
configure_file("${PROJECT_SOURCE_DIR}/tests/config.h.in" "${PROJECT_BINARY_DIR}/tests/config.h" ESCAPE_QUOTES @ONLY)
9190

tests/test_auth.c

Lines changed: 22 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* @file test_auth.c
3-
* @author Roman Janota <xjanot04@fit.vutbr.cz>
3+
* @author Roman Janota <janota@cesnet.cz>
44
* @brief libnetconf2 SSH authentication methods test
55
*
66
* @copyright
7-
* Copyright (c) 2023 CESNET, z.s.p.o.
7+
* Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
88
*
99
* This source code is licensed under BSD 3-Clause License (the "License").
1010
* You may not use this file except in compliance with the License.
@@ -26,52 +26,10 @@
2626
#include <cmocka.h>
2727

2828
#include "ln2_test.h"
29-
#include "tests/config.h"
30-
31-
#define NC_ACCEPT_TIMEOUT 2000
32-
#define NC_PS_POLL_TIMEOUT 2000
33-
34-
struct ly_ctx *ctx;
35-
36-
struct test_state {
37-
pthread_barrier_t barrier;
38-
};
3929

4030
int TEST_PORT = 10050;
4131
const char *TEST_PORT_STR = "10050";
4232

43-
static void *
44-
server_thread(void *arg)
45-
{
46-
int ret;
47-
NC_MSG_TYPE msgtype;
48-
struct nc_session *session;
49-
struct nc_pollsession *ps;
50-
struct test_state *state = arg;
51-
52-
(void) arg;
53-
54-
ps = nc_ps_new();
55-
assert_non_null(ps);
56-
57-
/* accept a session and add it to the poll session structure */
58-
pthread_barrier_wait(&state->barrier);
59-
msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
60-
assert_int_equal(msgtype, NC_MSG_HELLO);
61-
62-
ret = nc_ps_add_session(ps, session);
63-
assert_int_equal(ret, 0);
64-
65-
do {
66-
ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
67-
assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
68-
} while (!(ret & NC_PSPOLL_SESSION_TERM));
69-
70-
nc_ps_clear(ps, 1, NULL);
71-
nc_ps_free(ps);
72-
return NULL;
73-
}
74-
7533
static char *
7634
auth_password(const char *username, const char *hostname, void *priv)
7735
{
@@ -91,7 +49,7 @@ client_thread_password(void *arg)
9149
{
9250
int ret;
9351
struct nc_session *session = NULL;
94-
struct test_state *state = arg;
52+
struct ln2_test_ctx *test_ctx = arg;
9553

9654
/* skip all hostkey and known_hosts checks */
9755
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -108,7 +66,7 @@ client_thread_password(void *arg)
10866
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, 1);
10967
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
11068

111-
pthread_barrier_wait(&state->barrier);
69+
pthread_barrier_wait(&test_ctx->barrier);
11270
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
11371
assert_non_null(session);
11472

@@ -126,7 +84,7 @@ test_nc_auth_password(void **state)
12684

12785
ret = pthread_create(&tids[0], NULL, client_thread_password, *state);
12886
assert_int_equal(ret, 0);
129-
ret = pthread_create(&tids[1], NULL, server_thread, *state);
87+
ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
13088
assert_int_equal(ret, 0);
13189

13290
for (i = 0; i < 2; i++) {
@@ -139,7 +97,7 @@ client_thread_pubkey(void *arg)
13997
{
14098
int ret;
14199
struct nc_session *session = NULL;
142-
struct test_state *state = arg;
100+
struct ln2_test_ctx *test_ctx = arg;
143101

144102
/* skip all hostkey and known_hosts checks */
145103
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -157,7 +115,7 @@ client_thread_pubkey(void *arg)
157115
ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa");
158116
assert_int_equal(ret, 0);
159117

160-
pthread_barrier_wait(&state->barrier);
118+
pthread_barrier_wait(&test_ctx->barrier);
161119
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
162120
assert_non_null(session);
163121

@@ -175,7 +133,7 @@ test_nc_auth_pubkey(void **state)
175133

176134
ret = pthread_create(&tids[0], NULL, client_thread_pubkey, *state);
177135
assert_int_equal(ret, 0);
178-
ret = pthread_create(&tids[1], NULL, server_thread, *state);
136+
ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
179137
assert_int_equal(ret, 0);
180138

181139
for (i = 0; i < 2; i++) {
@@ -188,7 +146,7 @@ client_thread_none(void *arg)
188146
{
189147
int ret;
190148
struct nc_session *session = NULL;
191-
struct test_state *state = arg;
149+
struct ln2_test_ctx *test_ctx = arg;
192150

193151
/* skip all hostkey and known_hosts checks */
194152
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -199,7 +157,7 @@ client_thread_none(void *arg)
199157
ret = nc_client_ssh_set_username("test_none");
200158
assert_int_equal(ret, 0);
201159

202-
pthread_barrier_wait(&state->barrier);
160+
pthread_barrier_wait(&test_ctx->barrier);
203161
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
204162
assert_non_null(session);
205163

@@ -217,7 +175,7 @@ test_nc_auth_none(void **state)
217175

218176
ret = pthread_create(&tids[0], NULL, client_thread_none, *state);
219177
assert_int_equal(ret, 0);
220-
ret = pthread_create(&tids[1], NULL, server_thread, *state);
178+
ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
221179
assert_int_equal(ret, 0);
222180

223181
for (i = 0; i < 2; i++) {
@@ -230,87 +188,45 @@ setup_f(void **state)
230188
{
231189
int ret;
232190
struct lyd_node *tree = NULL;
233-
struct test_state *test_state;
234-
235-
nc_verbosity(NC_VERB_VERBOSE);
236-
237-
/* init barrier */
238-
test_state = malloc(sizeof *test_state);
239-
assert_non_null(test_state);
191+
struct ln2_test_ctx *test_ctx;
240192

241-
ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
193+
ret = ln2_glob_test_setup(&test_ctx);
242194
assert_int_equal(ret, 0);
243195

244-
*state = test_state;
245-
246-
ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
247-
assert_int_equal(ret, 0);
196+
*state = test_ctx;
248197

249-
ret = nc_server_init_ctx(&ctx);
198+
ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
250199
assert_int_equal(ret, 0);
251200

252-
ret = nc_server_config_load_modules(&ctx);
201+
ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
253202
assert_int_equal(ret, 0);
254203

255-
ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
204+
ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_pk", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
256205
assert_int_equal(ret, 0);
257206

258-
ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
207+
ret = nc_server_config_add_ssh_user_password(test_ctx->ctx, "endpt", "test_pw", "testpw", &tree);
259208
assert_int_equal(ret, 0);
260209

261-
ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_pk", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
262-
assert_int_equal(ret, 0);
263-
264-
ret = nc_server_config_add_ssh_user_password(ctx, "endpt", "test_pw", "testpw", &tree);
265-
assert_int_equal(ret, 0);
266-
267-
ret = lyd_new_path(tree, ctx, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='endpt']/ssh/"
210+
ret = lyd_new_path(tree, test_ctx->ctx, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='endpt']/ssh/"
268211
"ssh-server-parameters/client-authentication/users/user[name='test_none']/none", NULL, 0, NULL);
269212
assert_int_equal(ret, 0);
270213

271214
/* configure the server based on the data */
272215
ret = nc_server_config_setup_data(tree);
273216
assert_int_equal(ret, 0);
274217

275-
ret = nc_server_init();
276-
assert_int_equal(ret, 0);
277-
278-
/* initialize client */
279-
ret = nc_client_init();
280-
assert_int_equal(ret, 0);
281-
282218
lyd_free_all(tree);
283219

284220
return 0;
285221
}
286222

287-
static int
288-
teardown_f(void **state)
289-
{
290-
int ret = 0;
291-
struct test_state *test_state;
292-
293-
assert_non_null(state);
294-
test_state = *state;
295-
296-
ret = pthread_barrier_destroy(&test_state->barrier);
297-
assert_int_equal(ret, 0);
298-
299-
free(*state);
300-
nc_client_destroy();
301-
nc_server_destroy();
302-
ly_ctx_destroy(ctx);
303-
304-
return 0;
305-
}
306-
307223
int
308224
main(void)
309225
{
310226
const struct CMUnitTest tests[] = {
311-
cmocka_unit_test_setup_teardown(test_nc_auth_pubkey, setup_f, teardown_f),
312-
cmocka_unit_test_setup_teardown(test_nc_auth_password, setup_f, teardown_f),
313-
cmocka_unit_test_setup_teardown(test_nc_auth_none, setup_f, teardown_f)
227+
cmocka_unit_test_setup_teardown(test_nc_auth_pubkey, setup_f, ln2_glob_test_teardown),
228+
cmocka_unit_test_setup_teardown(test_nc_auth_password, setup_f, ln2_glob_test_teardown),
229+
cmocka_unit_test_setup_teardown(test_nc_auth_none, setup_f, ln2_glob_test_teardown)
314230
};
315231

316232
/* try to get ports from the environment, otherwise use the default */

0 commit comments

Comments
 (0)