tests UPDATE use shared ln2_test functionality
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index c5f98a8..d2142a0 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -60,32 +60,31 @@
endfunction()
# all the tests that don't require SSH and TLS
-libnetconf2_test(NAME test_unix_socket)
+libnetconf2_test(NAME test_client_messages)
libnetconf2_test(NAME test_client_thread)
libnetconf2_test(NAME test_fd_comm)
libnetconf2_test(NAME test_io)
libnetconf2_test(NAME test_thread_messages)
-libnetconf2_test(NAME test_client_messages)
+libnetconf2_test(NAME test_unix_socket)
# tests depending on SSH/TLS
if(ENABLE_SSH_TLS)
libnetconf2_test(NAME test_auth)
- libnetconf2_test(NAME test_two_channels)
- libnetconf2_test(NAME test_ks_ts)
+ libnetconf2_test(NAME test_authkeys)
+ libnetconf2_test(NAME test_ch PORT_COUNT 2)
libnetconf2_test(NAME test_ec)
libnetconf2_test(NAME test_ed25519)
- libnetconf2_test(NAME test_replace)
libnetconf2_test(NAME test_endpt_share_clients PORT_COUNT 4)
- libnetconf2_test(NAME test_tls)
- libnetconf2_test(NAME test_ch PORT_COUNT 2)
- libnetconf2_test(NAME test_runtime_changes PORT_COUNT 2)
- libnetconf2_test(NAME test_authkeys)
+ libnetconf2_test(NAME test_ks_ts)
if (LIBPAM_HAVE_CONFDIR)
libnetconf2_test(NAME test_pam WRAP_FUNCS pam_start)
endif()
+ libnetconf2_test(NAME test_replace)
+ libnetconf2_test(NAME test_runtime_changes PORT_COUNT 2)
+ libnetconf2_test(NAME test_tls)
+ libnetconf2_test(NAME test_two_channels)
endif()
-
include_directories(${CMAKE_SOURCE_DIR}/src ${PROJECT_BINARY_DIR})
configure_file("${PROJECT_SOURCE_DIR}/tests/config.h.in" "${PROJECT_BINARY_DIR}/tests/config.h" ESCAPE_QUOTES @ONLY)
diff --git a/tests/test_auth.c b/tests/test_auth.c
index 67f182a..b48b566 100644
--- a/tests/test_auth.c
+++ b/tests/test_auth.c
@@ -1,10 +1,10 @@
/**
* @file test_auth.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 SSH authentication methods test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,52 +26,10 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
-static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- (void) arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
static char *
auth_password(const char *username, const char *hostname, void *priv)
{
@@ -91,7 +49,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -108,7 +66,7 @@
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, 1);
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -126,7 +84,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_password, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -139,7 +97,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -157,7 +115,7 @@
ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -175,7 +133,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_pubkey, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -188,7 +146,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -199,7 +157,7 @@
ret = nc_client_ssh_set_username("test_none");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -217,7 +175,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_none, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -230,41 +188,26 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ *state = test_ctx;
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_load_modules(&ctx);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_pk", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
+ ret = nc_server_config_add_ssh_user_password(test_ctx->ctx, "endpt", "test_pw", "testpw", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_pk", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_password(ctx, "endpt", "test_pw", "testpw", &tree);
- assert_int_equal(ret, 0);
-
- ret = lyd_new_path(tree, ctx, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='endpt']/ssh/"
+ ret = lyd_new_path(tree, test_ctx->ctx, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='endpt']/ssh/"
"ssh-server-parameters/client-authentication/users/user[name='test_none']/none", NULL, 0, NULL);
assert_int_equal(ret, 0);
@@ -272,45 +215,18 @@
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_auth_pubkey, setup_f, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_auth_password, setup_f, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_auth_none, setup_f, teardown_f)
+ cmocka_unit_test_setup_teardown(test_nc_auth_pubkey, setup_f, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_auth_password, setup_f, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_auth_none, setup_f, ln2_glob_test_teardown)
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_authkeys.c b/tests/test_authkeys.c
index 04eb0c6..25b8862 100644
--- a/tests/test_authkeys.c
+++ b/tests/test_authkeys.c
@@ -1,6 +1,6 @@
/**
* @file test_authkeys.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 SSH authentication using mocked system authorized_keys
*
* @copyright
@@ -26,15 +26,8 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
+struct test_authkey_data {
const char *pubkey_path;
const char *privkey_path;
int expect_ok;
@@ -50,17 +43,18 @@
NC_MSG_TYPE msgtype;
struct nc_session *session;
struct nc_pollsession *ps;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
+ struct test_authkey_data *test_data = test_ctx->test_data;
ps = nc_ps_new();
assert_non_null(ps);
/* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
+ pthread_barrier_wait(&test_ctx->barrier);
+ msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
/* only continue if we expect to authenticate successfully */
- if (state->expect_ok) {
+ if (test_data->expect_ok) {
assert_int_equal(msgtype, NC_MSG_HELLO);
} else {
assert_int_equal(msgtype, NC_MSG_ERROR);
@@ -86,7 +80,8 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
+ struct test_authkey_data *test_data = test_ctx->test_data;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -96,17 +91,17 @@
assert_int_equal(ret, 0);
/* add client's key pair */
- ret = nc_client_ssh_add_keypair(state->pubkey_path, state->privkey_path);
+ ret = nc_client_ssh_add_keypair(test_data->pubkey_path, test_data->privkey_path);
assert_int_equal(ret, 0);
/* set ssh username */
ret = nc_client_ssh_set_username("test");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
- if (state->expect_ok) {
+ if (test_data->expect_ok) {
assert_non_null(session);
} else {
assert_null(session);
@@ -121,29 +116,27 @@
{
int ret, i;
pthread_t tids[2];
- struct test_state *state;
+ struct test_authkey_data *test_data;
- assert_non_null(arg);
-
- state = *(struct test_state **)arg;
+ test_data = (*(struct ln2_test_ctx **)arg)->test_data;
/* set the path to the test's authorized_keys file */
ret = nc_server_ssh_set_authkey_path_format(TESTS_DIR "/data/authorized_keys");
assert_int_equal(ret, 0);
/* set pubkey and privkey path, the pubkey matches the one in authorized keys */
- state->pubkey_path = TESTS_DIR "/data/id_ed25519.pub";
- state->privkey_path = TESTS_DIR "/data/id_ed25519";
+ test_data->pubkey_path = TESTS_DIR "/data/id_ed25519.pub";
+ test_data->privkey_path = TESTS_DIR "/data/id_ed25519";
/* expect ok result */
- state->expect_ok = 1;
+ test_data->expect_ok = 1;
/* client */
- ret = pthread_create(&tids[0], NULL, client_thread, state);
+ ret = pthread_create(&tids[0], NULL, client_thread, *arg);
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, state);
+ ret = pthread_create(&tids[1], NULL, server_thread, *arg);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -156,29 +149,27 @@
{
int ret, i;
pthread_t tids[2];
- struct test_state *state;
+ struct test_authkey_data *test_data;
- assert_non_null(arg);
-
- state = *(struct test_state **)arg;
+ test_data = (*(struct ln2_test_ctx **)arg)->test_data;
/* set the path to the test's authorized_keys file */
ret = nc_server_ssh_set_authkey_path_format(TESTS_DIR "/data/authorized_keys");
assert_int_equal(ret, 0);
/* set pubkey and privkey path, the pubkey doesn't match the one in authorized keys */
- state->pubkey_path = TESTS_DIR "/data/id_ecdsa521.pub";
- state->privkey_path = TESTS_DIR "/data/id_ecdsa521";
+ test_data->pubkey_path = TESTS_DIR "/data/id_ecdsa521.pub";
+ test_data->privkey_path = TESTS_DIR "/data/id_ecdsa521";
/* expect fail */
- state->expect_ok = 0;
+ test_data->expect_ok = 0;
/* client */
- ret = pthread_create(&tids[0], NULL, client_thread, state);
+ ret = pthread_create(&tids[0], NULL, client_thread, *arg);
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, state);
+ ret = pthread_create(&tids[1], NULL, server_thread, *arg);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -191,29 +182,30 @@
{
int ret, i;
pthread_t tids[2];
- struct test_state *state;
+ struct ln2_test_ctx *test_ctx;
+ struct test_authkey_data *test_data;
assert_non_null(arg);
-
- state = *(struct test_state **)arg;
+ test_ctx = *arg;
+ test_data = test_ctx->test_data;
/* set the path to the test's authorized_keys file */
ret = nc_server_ssh_set_authkey_path_format(TESTS_DIR "/some/bad/path");
assert_int_equal(ret, 0);
/* set pubkey and privkey path, the pubkey doesn't match the one in authorized keys */
- state->pubkey_path = TESTS_DIR "/data/id_ed25519.pub";
- state->privkey_path = TESTS_DIR "/data/id_ed25519";
+ test_data->pubkey_path = TESTS_DIR "/data/id_ed25519.pub";
+ test_data->privkey_path = TESTS_DIR "/data/id_ed25519";
/* expect fail */
- state->expect_ok = 0;
+ test_data->expect_ok = 0;
/* client */
- ret = pthread_create(&tids[0], NULL, client_thread, state);
+ ret = pthread_create(&tids[0], NULL, client_thread, test_ctx);
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, state);
+ ret = pthread_create(&tids[1], NULL, server_thread, test_ctx);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -226,84 +218,44 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
+ struct test_authkey_data *test_data;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ test_data = calloc(1, sizeof *test_data);
+ assert_non_null(test_data);
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ test_ctx->test_data = test_data;
+ test_ctx->free_test_data = ln2_glob_test_free_test_data;
+ *state = test_ctx;
+
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree);
assert_int_equal(ret, 0);
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_authkey(ctx, "endpt", "test", &tree);
+ ret = nc_server_config_add_ssh_user_authkey(test_ctx->ctx, "endpt", "test", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_authkey_ok, setup_f, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_authkey_bad_key, setup_f, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_authkey_bad_path, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_authkey_ok, setup_f, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_authkey_bad_key, setup_f, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_authkey_bad_path, setup_f, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_ch.c b/tests/test_ch.c
index 076c2d9..def7da9 100644
--- a/tests/test_ch.c
+++ b/tests/test_ch.c
@@ -1,10 +1,10 @@
/**
* @file test_ch.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 Call-home test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,18 +26,9 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-#define NC_PS_POLL_TIMEOUT 2000
-
-#define NC_ACCEPT_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
- struct lyd_node *ssh_tree;
- struct lyd_node *tls_tree;
+struct test_ch_data {
+ struct lyd_node *tree;
};
char buffer[512];
@@ -63,8 +54,7 @@
const struct ly_ctx *
ch_session_acquire_ctx_cb(void *cb_data)
{
- (void) cb_data;
- return ctx;
+ return ((struct ln2_test_ctx *)cb_data)->ctx;
}
/* release ctx cb for dispatch */
@@ -93,8 +83,9 @@
server_thread_ssh(void *arg)
{
int ret;
- struct test_state *state = arg;
struct nc_pollsession *ps;
+ struct ln2_test_ctx *test_ctx = arg;
+ struct test_ch_data *test_data = test_ctx->test_data;
/* set print clb so we get access to messages */
nc_set_print_clb_session(test_msg_callback);
@@ -102,17 +93,17 @@
strcpy(expected, "reconnecting in");
/* prepare data for deleting the call-home client */
- ret = nc_server_config_del_ch_client("ch_ssh", &state->ssh_tree);
+ ret = nc_server_config_del_ch_client("ch_ssh", &test_data->tree);
assert_int_equal(ret, 0);
/* new poll session */
ps = nc_ps_new();
assert_non_null(ps);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* create the call-home client thread */
ret = nc_connect_ch_client_dispatch("ch_ssh", ch_session_acquire_ctx_cb,
- ch_session_release_ctx_cb, NULL, ch_new_session_cb, ps);
+ ch_session_release_ctx_cb, test_ctx, ch_new_session_cb, ps);
assert_int_equal(ret, 0);
/* poll */
@@ -124,12 +115,12 @@
} while (!strlen(buffer));
/* delete the call-home client, the thread should end */
- ret = nc_server_config_setup_data(state->ssh_tree);
+ ret = nc_server_config_setup_data(test_data->tree);
assert_int_equal(ret, 0);
nc_ps_clear(ps, 1, NULL);
nc_ps_free(ps);
- nc_server_destroy();
+
return NULL;
}
@@ -138,7 +129,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -159,7 +150,7 @@
ret = nc_client_ssh_ch_add_bind_listen("127.0.0.1", TEST_PORT);
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
ret = nc_accept_callhome(NC_ACCEPT_TIMEOUT, NULL, &session);
assert_int_equal(ret, 1);
@@ -171,89 +162,59 @@
return NULL;
}
+static void
+test_nc_ch_free_test_data(void *test_data)
+{
+ struct test_ch_data *test_ch_data;
+
+ test_ch_data = test_data;
+ lyd_free_tree(test_ch_data->tree);
+ free(test_ch_data);
+}
+
static int
setup_ssh(void **state)
{
int ret;
- struct test_state *test_state;
+ struct lyd_node *tree = NULL;
+ struct ln2_test_ctx *test_ctx;
+ struct test_ch_data *test_data;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- test_state->ssh_tree = NULL;
- *state = test_state;
+ test_data = calloc(1, sizeof *test_data);
+ assert_non_null(test_data);
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
-
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
+ test_ctx->test_data = test_data;
+ test_ctx->free_test_data = test_nc_ch_free_test_data;
+ *state = test_ctx;
/* set call-home address and port */
- ret = nc_server_config_add_ch_address_port(ctx, "ch_ssh", "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT_STR, &test_state->ssh_tree);
+ ret = nc_server_config_add_ch_address_port(test_ctx->ctx, "ch_ssh", "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT_STR, &tree);
assert_int_equal(ret, 0);
/* set connection type to persistent */
- ret = nc_server_config_add_ch_persistent(ctx, "ch_ssh", &test_state->ssh_tree);
+ ret = nc_server_config_add_ch_persistent(test_ctx->ctx, "ch_ssh", &tree);
assert_int_equal(ret, 0);
/* set the period of the periodic connection type, this should remove the persistent connection type */
- ret = nc_server_config_add_ch_period(ctx, "ch_ssh", 3, &test_state->ssh_tree);
+ ret = nc_server_config_add_ch_period(test_ctx->ctx, "ch_ssh", 3, &tree);
assert_int_equal(ret, 0);
/* set call-home server hostkey */
- ret = nc_server_config_add_ch_ssh_hostkey(ctx, "ch_ssh", "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &test_state->ssh_tree);
+ ret = nc_server_config_add_ch_ssh_hostkey(test_ctx->ctx, "ch_ssh", "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
assert_int_equal(ret, 0);
/* set call-home client's pubkey */
- ret = nc_server_config_add_ch_ssh_user_pubkey(ctx, "ch_ssh", "endpt", "test_ch_ssh", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &test_state->ssh_tree);
+ ret = nc_server_config_add_ch_ssh_user_pubkey(test_ctx->ctx, "ch_ssh", "endpt", "test_ch_ssh", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
- ret = nc_server_config_setup_data(test_state->ssh_tree);
+ ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
- return 0;
-}
-
-static int
-teardown_ssh(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- lyd_free_tree(test_state->ssh_tree);
-
- free(*state);
- nc_client_destroy();
- ly_ctx_destroy(ctx);
-
+ test_data->tree = tree;
return 0;
}
@@ -282,21 +243,22 @@
server_thread_tls(void *arg)
{
int ret;
- struct test_state *state = arg;
struct nc_pollsession *ps;
+ struct ln2_test_ctx *test_ctx = arg;
+ struct test_ch_data *test_data = test_ctx->test_data;
/* prepare data for deleting the call-home client */
- ret = nc_server_config_del_ch_client("ch_tls", &state->tls_tree);
+ ret = nc_server_config_del_ch_client("ch_tls", &test_data->tree);
assert_int_equal(ret, 0);
/* new poll session */
ps = nc_ps_new();
assert_non_null(ps);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* create the call-home client thread */
ret = nc_connect_ch_client_dispatch("ch_tls", ch_session_acquire_ctx_cb,
- ch_session_release_ctx_cb, NULL, ch_new_session_cb, ps);
+ ch_session_release_ctx_cb, test_ctx, ch_new_session_cb, ps);
assert_int_equal(ret, 0);
/* poll */
@@ -308,12 +270,11 @@
} while (!(ret & NC_PSPOLL_SESSION_TERM));
/* delete the call-home client, the thread should end */
- ret = nc_server_config_setup_data(state->tls_tree);
+ ret = nc_server_config_setup_data(test_data->tree);
assert_int_equal(ret, 0);
nc_ps_clear(ps, 1, NULL);
nc_ps_free(ps);
- nc_server_destroy();
return NULL;
}
@@ -322,7 +283,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* set directory where to search for modules */
ret = nc_client_set_schema_searchpath(MODULES_DIR);
@@ -340,7 +301,7 @@
ret = nc_client_tls_ch_add_bind_listen("127.0.0.1", TEST_PORT_2);
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
ret = nc_accept_callhome(NC_ACCEPT_TIMEOUT, NULL, &session);
assert_int_equal(ret, 1);
@@ -377,87 +338,47 @@
setup_tls(void **state)
{
int ret;
- struct test_state *test_state;
+ struct lyd_node *tree = NULL;
+ struct ln2_test_ctx *test_ctx;
+ struct test_ch_data *test_data;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- test_state->tls_tree = NULL;
- *state = test_state;
+ test_data = calloc(1, sizeof *test_data);
+ assert_non_null(test_data);
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
-
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
+ test_ctx->test_data = test_data;
+ test_ctx->free_test_data = test_nc_ch_free_test_data;
+ *state = test_ctx;
/* set call-home address and port */
- ret = nc_server_config_add_ch_address_port(ctx, "ch_tls", "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT_2_STR, &test_state->tls_tree);
+ ret = nc_server_config_add_ch_address_port(test_ctx->ctx, "ch_tls", "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT_2_STR, &tree);
assert_int_equal(ret, 0);
/* set call-home server certificate */
- ret = nc_server_config_add_ch_tls_server_cert(ctx, "ch_tls", "endpt", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &test_state->tls_tree);
+ ret = nc_server_config_add_ch_tls_server_cert(test_ctx->ctx, "ch_tls", "endpt", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree);
assert_int_equal(ret, 0);
/* set call-home client end entity certificate */
- ret = nc_server_config_add_ch_tls_client_cert(ctx, "ch_tls", "endpt", "ee-cert", TESTS_DIR "/data/client.crt", &test_state->tls_tree);
+ ret = nc_server_config_add_ch_tls_client_cert(test_ctx->ctx, "ch_tls", "endpt", "ee-cert", TESTS_DIR "/data/client.crt", &tree);
assert_int_equal(ret, 0);
/* set call-home client certificate authority certificate */
- ret = nc_server_config_add_ch_tls_ca_cert(ctx, "ch_tls", "endpt", "ca-cert", TESTS_DIR "/data/serverca.pem", &test_state->tls_tree);
+ ret = nc_server_config_add_ch_tls_ca_cert(test_ctx->ctx, "ch_tls", "endpt", "ca-cert", TESTS_DIR "/data/serverca.pem", &tree);
assert_int_equal(ret, 0);
/* set call-home CTN */
- ret = nc_server_config_add_ch_tls_ctn(ctx, "ch_tls", "endpt", 1,
+ ret = nc_server_config_add_ch_tls_ctn(test_ctx->ctx, "ch_tls", "endpt", 1,
"04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D",
- NC_TLS_CTN_SPECIFIED, "ch_client_tls", &test_state->tls_tree);
+ NC_TLS_CTN_SPECIFIED, "ch_client_tls", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
- ret = nc_server_config_setup_data(test_state->tls_tree);
+ ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
- return 0;
-}
-
-static int
-teardown_tls(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- lyd_free_tree(test_state->tls_tree);
-
- free(*state);
- nc_client_destroy();
- ly_ctx_destroy(ctx);
-
+ test_data->tree = tree;
return 0;
}
@@ -465,8 +386,8 @@
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_ch_ssh, setup_ssh, teardown_ssh),
- cmocka_unit_test_setup_teardown(test_nc_ch_tls, setup_tls, teardown_tls),
+ cmocka_unit_test_setup_teardown(test_nc_ch_ssh, setup_ssh, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_ch_tls, setup_tls, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_client_messages.c b/tests/test_client_messages.c
index 989c325..d5ae2f7 100644
--- a/tests/test_client_messages.c
+++ b/tests/test_client_messages.c
@@ -23,12 +23,9 @@
#include <sys/types.h>
#include <cmocka.h>
-#include <config.h>
-#include <libyang/libyang.h>
-#include <log.h>
#include <messages_p.h>
-#include <session_client.h>
-#include "tests/config.h"
+
+#include "ln2_test.h"
static int
setup_f(void **state)
diff --git a/tests/test_client_thread.c b/tests/test_client_thread.c
index bede794..9853785 100644
--- a/tests/test_client_thread.c
+++ b/tests/test_client_thread.c
@@ -24,8 +24,7 @@
#include <libyang/libyang.h>
-#include <session_client.h>
-#include "tests/config.h"
+#include "ln2_test.h"
#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
diff --git a/tests/test_ec.c b/tests/test_ec.c
index 7a8c944..ba29d76 100644
--- a/tests/test_ec.c
+++ b/tests/test_ec.c
@@ -1,10 +1,10 @@
/**
* @file test_ec.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 EC keys authentication test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,15 +26,8 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
+struct test_ec_data {
const char *client_username;
const char *client_privkey;
const char *client_pubkey;
@@ -44,43 +37,12 @@
const char *TEST_PORT_STR = "10050";
static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- (void) arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
-static void *
client_thread(void *arg)
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
+ struct test_ec_data *test_data = test_ctx->test_data;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -90,14 +52,14 @@
assert_int_equal(ret, 0);
/* set ssh username */
- ret = nc_client_ssh_set_username(state->client_username);
+ ret = nc_client_ssh_set_username(test_data->client_username);
assert_int_equal(ret, 0);
/* add client's key pair */
- ret = nc_client_ssh_add_keypair(state->client_pubkey, state->client_privkey);
+ ret = nc_client_ssh_add_keypair(test_data->client_pubkey, test_data->client_privkey);
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -111,21 +73,23 @@
{
int ret, i;
pthread_t tids[2];
- struct test_state *client;
+ struct ln2_test_ctx *test_ctx;
+ struct test_ec_data *test_data;
/* set specific data for the client */
assert_non_null(state);
- client = *state;
+ test_ctx = *state;
+ test_data = test_ctx->test_data;
/* client */
- client->client_username = "test_ec256";
- client->client_pubkey = TESTS_DIR "/data/id_ecdsa256.pub";
- client->client_privkey = TESTS_DIR "/data/id_ecdsa256";
- ret = pthread_create(&tids[0], NULL, client_thread, client);
+ test_data->client_username = "test_ec256";
+ test_data->client_pubkey = TESTS_DIR "/data/id_ecdsa256.pub";
+ test_data->client_privkey = TESTS_DIR "/data/id_ecdsa256";
+ ret = pthread_create(&tids[0], NULL, client_thread, test_ctx);
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, test_ctx);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -138,21 +102,23 @@
{
int ret, i;
pthread_t tids[2];
- struct test_state *client;
+ struct ln2_test_ctx *test_ctx;
+ struct test_ec_data *test_data;
/* set specific data for the client */
assert_non_null(state);
- client = *state;
+ test_ctx = *state;
+ test_data = test_ctx->test_data;
/* client */
- client->client_username = "test_ec384";
- client->client_pubkey = TESTS_DIR "/data/id_ecdsa384.pub";
- client->client_privkey = TESTS_DIR "/data/id_ecdsa384";
- ret = pthread_create(&tids[0], NULL, client_thread, client);
+ test_data->client_username = "test_ec384";
+ test_data->client_pubkey = TESTS_DIR "/data/id_ecdsa384.pub";
+ test_data->client_privkey = TESTS_DIR "/data/id_ecdsa384";
+ ret = pthread_create(&tids[0], NULL, client_thread, test_ctx);
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, test_ctx);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -165,21 +131,23 @@
{
int ret, i;
pthread_t tids[2];
- struct test_state *client;
+ struct ln2_test_ctx *test_ctx;
+ struct test_ec_data *test_data;
/* set specific data for the client */
assert_non_null(state);
- client = *state;
+ test_ctx = *state;
+ test_data = test_ctx->test_data;
/* client */
- client->client_username = "test_ec521";
- client->client_pubkey = TESTS_DIR "/data/id_ecdsa521.pub";
- client->client_privkey = TESTS_DIR "/data/id_ecdsa521";
- ret = pthread_create(&tids[0], NULL, client_thread, client);
+ test_data->client_username = "test_ec521";
+ test_data->client_pubkey = TESTS_DIR "/data/id_ecdsa521.pub";
+ test_data->client_privkey = TESTS_DIR "/data/id_ecdsa521";
+ ret = pthread_create(&tids[0], NULL, client_thread, test_ctx);
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, test_ctx);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -192,90 +160,50 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
+ struct test_ec_data *test_data;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ test_data = calloc(1, sizeof *test_data);
+ assert_non_null(test_data);
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ test_ctx->test_data = test_data;
+ test_ctx->free_test_data = ln2_glob_test_free_test_data;
+ *state = test_ctx;
+
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
assert_int_equal(ret, 0);
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_ec256", "pubkey", TESTS_DIR "/data/id_ecdsa256.pub", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_ec384", "pubkey", TESTS_DIR "/data/id_ecdsa384.pub", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_ec256", "pubkey", TESTS_DIR "/data/id_ecdsa256.pub", &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_ec384", "pubkey", TESTS_DIR "/data/id_ecdsa384.pub", &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_ec521", "pubkey", TESTS_DIR "/data/id_ecdsa521.pub", &tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_ec521", "pubkey", TESTS_DIR "/data/id_ecdsa521.pub", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_ec256, setup_f, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_ec384, setup_f, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_ec521, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_ec256, setup_f, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_ec384, setup_f, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_ec521, setup_f, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_ed25519.c b/tests/test_ed25519.c
index cc260a2..24d0ade 100644
--- a/tests/test_ed25519.c
+++ b/tests/test_ed25519.c
@@ -1,10 +1,10 @@
/**
* @file test_ed25519.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 ED25519 key authentication test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,58 +26,16 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- (void) arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
-static void *
client_thread(void *arg)
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -94,7 +52,7 @@
ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ed25519.pub", TESTS_DIR "/data/id_ed25519");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -116,7 +74,7 @@
assert_int_equal(ret, 0);
/* server */
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -129,82 +87,36 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ *state = test_ctx;
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree);
assert_int_equal(ret, 0);
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_ed25519", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "test_ed25519", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_ed25519, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_ed25519, setup_f, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_endpt_share_clients.c b/tests/test_endpt_share_clients.c
index 0cbccff..ec22496 100644
--- a/tests/test_endpt_share_clients.c
+++ b/tests/test_endpt_share_clients.c
@@ -1,10 +1,10 @@
/**
* @file test_endpt_share_clients.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 Sharing clients between endpoints test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,56 +26,16 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050, TEST_PORT_2 = 10051, TEST_PORT_3 = 10052, TEST_PORT_4 = 10053;
const char *TEST_PORT_STR = "10050", *TEST_PORT_2_STR = "10051", *TEST_PORT_3_STR = "10052", *TEST_PORT_4_STR = "10053";
static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
-static void *
client_thread_ssh(void *arg)
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -93,7 +53,7 @@
assert_int_equal(ret, 0);
/* wait for the server to reach polling */
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
@@ -113,7 +73,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_ssh, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -126,7 +86,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* set directory where to search for modules */
ret = nc_client_set_schema_searchpath(MODULES_DIR);
@@ -140,7 +100,7 @@
ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_tls("127.0.0.1", TEST_PORT_4, NULL);
assert_non_null(session);
@@ -158,7 +118,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_tls, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -171,63 +131,37 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
-
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
-
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
+ *state = test_ctx;
/* create the first SSH endpoint with a client reference to the second endpoint */
- ret = nc_server_config_add_ssh_hostkey(ctx, "SSH_endpt_1", "hostkey", TESTS_DIR "/data/key_rsa", NULL, &tree);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "SSH_endpt_1", "hostkey", TESTS_DIR "/data/key_rsa", NULL, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "SSH_endpt_1", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "SSH_endpt_1", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_endpoint_client_ref(ctx, "SSH_endpt_1", "SSH_endpt_2", &tree);
+ ret = nc_server_config_add_ssh_endpoint_client_ref(test_ctx->ctx, "SSH_endpt_1", "SSH_endpt_2", &tree);
assert_int_equal(ret, 0);
/* create the second SSH endpoint with a single client */
- ret = nc_server_config_add_ssh_hostkey(ctx, "SSH_endpt_2", "hostkey", TESTS_DIR "/data/key_rsa", NULL, &tree);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "SSH_endpt_2", "hostkey", TESTS_DIR "/data/key_rsa", NULL, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "SSH_endpt_2", NC_TI_SSH, "127.0.0.1", TEST_PORT_2, &tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "SSH_endpt_2", NC_TI_SSH, "127.0.0.1", TEST_PORT_2, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "SSH_endpt_2", "client", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "SSH_endpt_2", "client", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the yang data */
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize the server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
@@ -238,103 +172,57 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
-
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
-
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
+ *state = test_ctx;
/* create the first TLS endpoint with a single end entity client cert and a CTN entry */
- ret = nc_server_config_add_tls_server_cert(ctx, "TLS_endpt_1", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree);
+ ret = nc_server_config_add_tls_server_cert(test_ctx->ctx, "TLS_endpt_1", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "TLS_endpt_1", NC_TI_TLS, "127.0.0.1", TEST_PORT_3, &tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "TLS_endpt_1", NC_TI_TLS, "127.0.0.1", TEST_PORT_3, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_tls_client_cert(ctx, "TLS_endpt_1", "cert_client", TESTS_DIR "/data/client.crt", &tree);
+ ret = nc_server_config_add_tls_client_cert(test_ctx->ctx, "TLS_endpt_1", "cert_client", TESTS_DIR "/data/client.crt", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_tls_ca_cert(ctx, "TLS_endpt_1", "cert_ca", TESTS_DIR "/data/serverca.pem", &tree);
+ ret = nc_server_config_add_tls_ca_cert(test_ctx->ctx, "TLS_endpt_1", "cert_ca", TESTS_DIR "/data/serverca.pem", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_tls_ctn(ctx, "TLS_endpt_1", 1,
+ ret = nc_server_config_add_tls_ctn(test_ctx->ctx, "TLS_endpt_1", 1,
"04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D",
NC_TLS_CTN_SPECIFIED, "client", &tree);
assert_int_equal(ret, 0);
/* create the second TLS endpoint with a reference to the first endpoint */
- ret = nc_server_config_add_tls_server_cert(ctx, "TLS_endpt_2",
+ ret = nc_server_config_add_tls_server_cert(test_ctx->ctx, "TLS_endpt_2",
TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "TLS_endpt_2", NC_TI_TLS, "127.0.0.1", TEST_PORT_4, &tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "TLS_endpt_2", NC_TI_TLS, "127.0.0.1", TEST_PORT_4, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_tls_endpoint_client_ref(ctx, "TLS_endpt_2", "TLS_endpt_1", &tree);
+ ret = nc_server_config_add_tls_endpoint_client_ref(test_ctx->ctx, "TLS_endpt_2", "TLS_endpt_1", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the yang data */
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- /* initialize the server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(nc_test_endpt_share_clients_ssh, setup_ssh, teardown_f),
- cmocka_unit_test_setup_teardown(nc_test_endpt_share_clients_tls, setup_tls, teardown_f),
+ cmocka_unit_test_setup_teardown(nc_test_endpt_share_clients_ssh, setup_ssh, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(nc_test_endpt_share_clients_tls, setup_tls, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_fd_comm.c b/tests/test_fd_comm.c
index c2eb746..31e3293 100644
--- a/tests/test_fd_comm.c
+++ b/tests/test_fd_comm.c
@@ -32,10 +32,9 @@
#include <libyang/libyang.h>
#include <messages_p.h>
-#include <session_client.h>
#include <session_p.h>
-#include <session_server.h>
-#include "tests/config.h"
+
+#include "ln2_test.h"
struct nc_session *server_session;
struct nc_session *client_session;
diff --git a/tests/test_io.c b/tests/test_io.c
index a8de474..8e059c3 100644
--- a/tests/test_io.c
+++ b/tests/test_io.c
@@ -30,9 +30,9 @@
#include <libyang/libyang.h>
#include <messages_p.h>
-#include <session_client.h>
#include <session_p.h>
-#include "tests/config.h"
+
+#include "ln2_test.h"
struct wr {
struct nc_session *session;
diff --git a/tests/test_ks_ts.c b/tests/test_ks_ts.c
index 4c9bebe..8c6aa79 100644
--- a/tests/test_ks_ts.c
+++ b/tests/test_ks_ts.c
@@ -1,10 +1,10 @@
/**
* @file test_ks_ts.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 Keystore and trustore usage test.
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,56 +26,16 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
-static void *
client_thread_ssh(void *arg)
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -89,7 +49,7 @@
ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/id_ed25519.pub", TESTS_DIR "/data/id_ed25519");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -107,7 +67,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_ssh, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -120,54 +80,32 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ *state = test_ctx;
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_ssh_keystore_ref(test_ctx->ctx, "endpt", "hostkey", "test_keystore", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_load_modules(&ctx);
+ ret = nc_server_config_add_ssh_truststore_ref(test_ctx->ctx, "endpt", "client", "test_truststore", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
+ ret = nc_server_config_add_keystore_asym_key(test_ctx->ctx, NC_TI_SSH, "test_keystore", TESTS_DIR "/data/key_rsa", NULL, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_keystore_ref(ctx, "endpt", "hostkey", "test_keystore", &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_truststore_ref(ctx, "endpt", "client", "test_truststore", &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_keystore_asym_key(ctx, NC_TI_SSH, "test_keystore", TESTS_DIR "/data/key_rsa", NULL, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_truststore_pubkey(ctx, "test_truststore", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
+ ret = nc_server_config_add_truststore_pubkey(test_ctx->ctx, "test_truststore", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
@@ -178,7 +116,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
ret = nc_client_set_schema_searchpath(MODULES_DIR);
assert_int_equal(ret, 0);
@@ -191,7 +129,7 @@
ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_tls("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -209,7 +147,7 @@
ret = pthread_create(&tids[0], NULL, client_thread_tls, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -222,65 +160,47 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
-
- /* new ctx */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- /* init ctx */
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
-
- /* load ietf netconf server module and its requisities */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
+ *state = test_ctx;
/* new tls bind */
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
/* new keystore asym key pair */
- ret = nc_server_config_add_keystore_asym_key(ctx, NC_TI_TLS, "server_key", TESTS_DIR "/data/server.key", NULL, &tree);
+ ret = nc_server_config_add_keystore_asym_key(test_ctx->ctx, NC_TI_TLS, "server_key", TESTS_DIR "/data/server.key", NULL, &tree);
assert_int_equal(ret, 0);
/* new keystore cert belonging to the key pair */
- ret = nc_server_config_add_keystore_cert(ctx, "server_key", "server_cert", TESTS_DIR "/data/server.crt", &tree);
+ ret = nc_server_config_add_keystore_cert(test_ctx->ctx, "server_key", "server_cert", TESTS_DIR "/data/server.crt", &tree);
assert_int_equal(ret, 0);
/* new truststore client cert */
- ret = nc_server_config_add_truststore_cert(ctx, "ee_cert_bag", "ee_cert", TESTS_DIR "/data/client.crt", &tree);
+ ret = nc_server_config_add_truststore_cert(test_ctx->ctx, "ee_cert_bag", "ee_cert", TESTS_DIR "/data/client.crt", &tree);
assert_int_equal(ret, 0);
/* new truststore client CA cert */
- ret = nc_server_config_add_truststore_cert(ctx, "ca_cert_bag", "ca_cert", TESTS_DIR "/data/serverca.pem", &tree);
+ ret = nc_server_config_add_truststore_cert(test_ctx->ctx, "ca_cert_bag", "ca_cert", TESTS_DIR "/data/serverca.pem", &tree);
assert_int_equal(ret, 0);
/* new keystore ref for the TLS server cert */
- ret = nc_server_config_add_tls_keystore_ref(ctx, "endpt", "server_key", "server_cert", &tree);
+ ret = nc_server_config_add_tls_keystore_ref(test_ctx->ctx, "endpt", "server_key", "server_cert", &tree);
assert_int_equal(ret, 0);
/* new truststore ref for the client cert */
- ret = nc_server_config_add_tls_client_cert_truststore_ref(ctx, "endpt", "ee_cert_bag", &tree);
+ ret = nc_server_config_add_tls_client_cert_truststore_ref(test_ctx->ctx, "endpt", "ee_cert_bag", &tree);
assert_int_equal(ret, 0);
/* new truststore ref for the client CA cert */
- ret = nc_server_config_add_tls_ca_cert_truststore_ref(ctx, "endpt", "ca_cert_bag", &tree);
+ ret = nc_server_config_add_tls_ca_cert_truststore_ref(test_ctx->ctx, "endpt", "ca_cert_bag", &tree);
assert_int_equal(ret, 0);
/* new cert-to-name */
- ret = nc_server_config_add_tls_ctn(ctx, "endpt", 1,
+ ret = nc_server_config_add_tls_ctn(test_ctx->ctx, "endpt", 1,
"04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D",
NC_TLS_CTN_SPECIFIED, "client", &tree);
assert_int_equal(ret, 0);
@@ -289,44 +209,17 @@
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_ks_ts_ssh, setup_ssh, teardown_f),
- cmocka_unit_test_setup_teardown(test_nc_ks_ts_tls, setup_tls, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_ks_ts_ssh, setup_ssh, ln2_glob_test_teardown),
+ cmocka_unit_test_setup_teardown(test_nc_ks_ts_tls, setup_tls, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_pam.c b/tests/test_pam.c
index bb90604..a867c10 100644
--- a/tests/test_pam.c
+++ b/tests/test_pam.c
@@ -1,10 +1,10 @@
/**
* @file test_pam.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 SSH Keyboard Interactive auth using PAM test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -28,16 +28,6 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
@@ -50,36 +40,6 @@
return pam_start_confdir(service_name, user, pam_conversation, BUILD_DIR "/tests", pamh);
}
-static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
static char *
auth_interactive(const char *UNUSED(auth_name), const char *UNUSED(instruction),
const char *prompt, int UNUSED(echo), void *UNUSED(priv))
@@ -99,7 +59,7 @@
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -113,7 +73,7 @@
/* set keyboard-interactive authentication callback */
nc_client_ssh_set_auth_interactive_clb(auth_interactive, NULL);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -131,7 +91,7 @@
ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -144,35 +104,20 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ *state = test_ctx;
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_interactive(ctx, "endpt", "test", &tree);
+ ret = nc_server_config_add_ssh_user_interactive(test_ctx->ctx, "endpt", "test", &tree);
assert_int_equal(ret, 0);
ret = nc_server_ssh_set_pam_conf_filename("netconf.conf");
@@ -182,43 +127,16 @@
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_pam, setup_f, teardown_f)
+ cmocka_unit_test_setup_teardown(test_nc_pam, setup_f, ln2_glob_test_teardown)
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_replace.c b/tests/test_replace.c
index 246d3e8..43edd2f 100644
--- a/tests/test_replace.c
+++ b/tests/test_replace.c
@@ -1,10 +1,10 @@
/**
* @file test_replace.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 Non-diff YANG data configuration test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,56 +26,16 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
-static void *
client_thread(void *arg)
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -93,7 +53,7 @@
assert_int_equal(ret, 0);
/* wait for the server to reach polling */
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
/* connect */
session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
@@ -113,7 +73,7 @@
ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -126,51 +86,33 @@
{
int ret;
struct lyd_node *old_tree = NULL, *new_tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
+ *state = test_ctx;
- /* create new context */
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "old", NC_TI_SSH, "127.0.0.1", TEST_PORT, &old_tree);
assert_int_equal(ret, 0);
- /* load default modules into context */
- ret = nc_server_init_ctx(&ctx);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "old", "old_key", TESTS_DIR "/data/key_rsa", NULL, &old_tree);
assert_int_equal(ret, 0);
- /* load ietf-netconf-server module and it's imports into context */
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_address_port(ctx, "old", NC_TI_SSH, "127.0.0.1", TEST_PORT, &old_tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_hostkey(ctx, "old", "old_key", TESTS_DIR "/data/key_rsa", NULL, &old_tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_password(ctx, "old", "old_client", "passwd", &old_tree);
+ ret = nc_server_config_add_ssh_user_password(test_ctx->ctx, "old", "old_client", "passwd", &old_tree);
assert_int_equal(ret, 0);
/* configure the server based on the yang data, treat them as if every node had replace operation */
ret = nc_server_config_setup_data(old_tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "new", NC_TI_SSH, "127.0.0.1", TEST_PORT, &new_tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "new", NC_TI_SSH, "127.0.0.1", TEST_PORT, &new_tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_hostkey(ctx, "new", "new_key", TESTS_DIR "/data/key_rsa", NULL, &new_tree);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "new", "new_key", TESTS_DIR "/data/key_rsa", NULL, &new_tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "new", "new_client", "pubkey", TESTS_DIR "/data/key_rsa.pub", &new_tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "new", "new_client", "pubkey", TESTS_DIR "/data/key_rsa.pub", &new_tree);
assert_int_equal(ret, 0);
/* configure the server based on the yang data, meaning
@@ -179,45 +121,17 @@
ret = nc_server_config_setup_data(new_tree);
assert_int_equal(ret, 0);
- /* initialize the server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(old_tree);
lyd_free_all(new_tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(nc_test_replace, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(nc_test_replace, setup_f, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_runtime_changes.c b/tests/test_runtime_changes.c
index 9f24773..fc59ed2 100644
--- a/tests/test_runtime_changes.c
+++ b/tests/test_runtime_changes.c
@@ -4,7 +4,7 @@
* @brief libnetconf2 Runtime changes test.
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -24,10 +24,6 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
struct test_state {
pthread_barrier_t start_barrier;
diff --git a/tests/test_thread_messages.c b/tests/test_thread_messages.c
index 89eaa02..0930cc6 100644
--- a/tests/test_thread_messages.c
+++ b/tests/test_thread_messages.c
@@ -25,17 +25,8 @@
#include <libyang/libyang.h>
-#include <log.h>
-#include <messages_p.h>
-#include <messages_server.h>
-#include <session_client.h>
-#include <session_server.h>
-#include "tests/config.h"
+#include "ln2_test.h"
-/* millisec */
-#define NC_ACCEPT_TIMEOUT 5000
-/* millisec */
-#define NC_PS_POLL_TIMEOUT 5000
/* sec */
#define CLIENT_SSH_AUTH_TIMEOUT 10
diff --git a/tests/test_tls.c b/tests/test_tls.c
index c39da50..c6d7c5e 100644
--- a/tests/test_tls.c
+++ b/tests/test_tls.c
@@ -4,7 +4,7 @@
* @brief libnetconf2 TLS authentication test
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -24,58 +24,16 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- (void) arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
-
-static void *
client_thread(void *arg)
{
int ret;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
ret = nc_client_set_schema_searchpath(MODULES_DIR);
assert_int_equal(ret, 0);
@@ -88,7 +46,7 @@
ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_tls("127.0.0.1", TEST_PORT, NULL);
assert_non_null(session);
@@ -106,7 +64,7 @@
ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -119,46 +77,31 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
-
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_load_modules(&ctx);
- assert_int_equal(ret, 0);
+ *state = test_ctx;
/* create new address and port data */
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree);
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
/* create new server certificate data */
- ret = nc_server_config_add_tls_server_cert(ctx, "endpt", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree);
+ ret = nc_server_config_add_tls_server_cert(test_ctx->ctx, "endpt", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &tree);
assert_int_equal(ret, 0);
/* create new end entity client cert data */
- ret = nc_server_config_add_tls_client_cert(ctx, "endpt", "client_cert", TESTS_DIR "/data/client.crt", &tree);
+ ret = nc_server_config_add_tls_client_cert(test_ctx->ctx, "endpt", "client_cert", TESTS_DIR "/data/client.crt", &tree);
assert_int_equal(ret, 0);
/* create new client ca data */
- ret = nc_server_config_add_tls_ca_cert(ctx, "endpt", "client_ca", TESTS_DIR "/data/serverca.pem", &tree);
+ ret = nc_server_config_add_tls_ca_cert(test_ctx->ctx, "endpt", "client_ca", TESTS_DIR "/data/serverca.pem", &tree);
assert_int_equal(ret, 0);
/* create new cert-to-name */
- ret = nc_server_config_add_tls_ctn(ctx, "endpt", 1,
+ ret = nc_server_config_add_tls_ctn(test_ctx->ctx, "endpt", 1,
"04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D",
NC_TLS_CTN_SPECIFIED, "client", &tree);
assert_int_equal(ret, 0);
@@ -167,43 +110,16 @@
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_tls, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_tls, setup_f, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_two_channels.c b/tests/test_two_channels.c
index ebe6198..9d9e97d 100644
--- a/tests/test_two_channels.c
+++ b/tests/test_two_channels.c
@@ -1,10 +1,10 @@
/**
* @file test_two_channels.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 Openning a new session on an established SSH channel test.
*
* @copyright
- * Copyright (c) 2023 CESNET, z.s.p.o.
+ * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -26,14 +26,9 @@
#include <cmocka.h>
#include "ln2_test.h"
-#include "tests/config.h"
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
#define BACKOFF_TIMEOUT_USECS 100
-struct ly_ctx *ctx;
-
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
@@ -44,14 +39,13 @@
NC_MSG_TYPE msgtype;
struct nc_session *session, *new_session;
struct nc_pollsession *ps;
-
- (void) arg;
+ struct ln2_test_ctx *test_ctx = arg;
ps = nc_ps_new();
assert_non_null(ps);
while (del_session_count < 2) {
- msgtype = nc_accept(0, ctx, &new_session);
+ msgtype = nc_accept(0, test_ctx->ctx, &new_session);
if (msgtype == NC_MSG_HELLO) {
ret = nc_ps_add_session(ps, new_session);
@@ -84,10 +78,11 @@
static void *
client_thread(void *arg)
{
- (void) arg;
int ret;
struct nc_session *session_cl1, *session_cl2;
+ (void) arg;
+
/* skip all hostkey and known_hosts checks */
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
@@ -115,7 +110,6 @@
session_cl2 = nc_connect_ssh_channel(session_cl1, NULL);
assert_non_null(session_cl2);
- nc_client_destroy();
nc_session_free(session_cl1, NULL);
nc_session_free(session_cl2, NULL);
return NULL;
@@ -127,11 +121,9 @@
int ret, i;
pthread_t tids[2];
- (void) state;
-
- ret = pthread_create(&tids[0], NULL, client_thread, NULL);
+ ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, NULL);
+ ret = pthread_create(&tids[1], NULL, server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -144,63 +136,38 @@
{
int ret;
struct lyd_node *tree = NULL;
+ struct ln2_test_ctx *test_ctx;
- (void) state;
-
- nc_verbosity(NC_VERB_VERBOSE);
-
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- ret = nc_server_init_ctx(&ctx);
+ *state = test_ctx;
+
+ ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_load_modules(&ctx);
+ ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "client_1", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
assert_int_equal(ret, 0);
- ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "client_1", "pubkey", TESTS_DIR "/data/id_ed25519.pub", &tree);
- assert_int_equal(ret, 0);
-
- ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "client_2", "pubkey", TESTS_DIR "/data/id_ecdsa521.pub", &tree);
+ ret = nc_server_config_add_ssh_user_pubkey(test_ctx->ctx, "endpt", "client_2", "pubkey", TESTS_DIR "/data/id_ecdsa521.pub", &tree);
assert_int_equal(ret, 0);
ret = nc_server_config_setup_data(tree);
assert_int_equal(ret, 0);
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- (void) state;
-
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_two_channels, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_two_channels, setup_f, ln2_glob_test_teardown),
};
/* try to get ports from the environment, otherwise use the default */
diff --git a/tests/test_unix_socket.c b/tests/test_unix_socket.c
index a6c81d7..75ccff9 100644
--- a/tests/test_unix_socket.c
+++ b/tests/test_unix_socket.c
@@ -1,10 +1,10 @@
/**
* @file test_unix_socket.c
- * @author Roman Janota <xjanot04@fit.vutbr.cz>
+ * @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 UNIX socket test
*
* @copyright
- * Copyright (c) 2022 CESNET, z.s.p.o.
+ * Copyright (c) 2022 - 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -25,58 +25,19 @@
#include <cmocka.h>
-#include "tests/config.h"
-
-#define NC_ACCEPT_TIMEOUT 2000
-#define NC_PS_POLL_TIMEOUT 2000
-
-struct ly_ctx *ctx;
-
-struct test_state {
- pthread_barrier_t barrier;
-};
-
-static void *
-server_thread(void *arg)
-{
- int ret;
- NC_MSG_TYPE msgtype;
- struct nc_session *session;
- struct nc_pollsession *ps;
- struct test_state *state = arg;
-
- ps = nc_ps_new();
- assert_non_null(ps);
-
- /* accept a session and add it to the poll session structure */
- pthread_barrier_wait(&state->barrier);
- msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
- assert_int_equal(msgtype, NC_MSG_HELLO);
-
- ret = nc_ps_add_session(ps, session);
- assert_int_equal(ret, 0);
-
- do {
- ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
- assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
- } while (!(ret & NC_PSPOLL_SESSION_TERM));
-
- nc_ps_clear(ps, 1, NULL);
- nc_ps_free(ps);
- return NULL;
-}
+#include "ln2_test.h"
static void *
client_thread(void *arg)
{
int ret = 0;
struct nc_session *session = NULL;
- struct test_state *state = arg;
+ struct ln2_test_ctx *test_ctx = arg;
ret = nc_client_set_schema_searchpath(MODULES_DIR);
assert_int_equal(ret, 0);
- pthread_barrier_wait(&state->barrier);
+ pthread_barrier_wait(&test_ctx->barrier);
session = nc_connect_unix("/tmp/nc2_test_unix_sock", NULL);
assert_non_null(session);
@@ -94,7 +55,7 @@
ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
- ret = pthread_create(&tids[1], NULL, server_thread, *state);
+ ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
@@ -107,67 +68,26 @@
{
int ret;
struct lyd_node *tree = NULL;
- struct test_state *test_state;
+ struct ln2_test_ctx *test_ctx;
- nc_verbosity(NC_VERB_VERBOSE);
-
- /* init barrier */
- test_state = malloc(sizeof *test_state);
- assert_non_null(test_state);
-
- ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
+ ret = ln2_glob_test_setup(&test_ctx);
assert_int_equal(ret, 0);
- *state = test_state;
-
- ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
- assert_int_equal(ret, 0);
-
- /* initialize context */
- ret = nc_server_init_ctx(&ctx);
- assert_int_equal(ret, 0);
+ *state = test_ctx;
/* create the UNIX socket */
ret = nc_server_add_endpt_unix_socket_listen("unix", "/tmp/nc2_test_unix_sock", 0700, -1, -1);
assert_int_equal(ret, 0);
- /* initialize server */
- ret = nc_server_init();
- assert_int_equal(ret, 0);
-
- /* initialize client */
- ret = nc_client_init();
- assert_int_equal(ret, 0);
-
lyd_free_all(tree);
return 0;
}
-static int
-teardown_f(void **state)
-{
- int ret = 0;
- struct test_state *test_state;
-
- assert_non_null(state);
- test_state = *state;
-
- ret = pthread_barrier_destroy(&test_state->barrier);
- assert_int_equal(ret, 0);
-
- free(*state);
- nc_client_destroy();
- nc_server_destroy();
- ly_ctx_destroy(ctx);
-
- return 0;
-}
-
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test_setup_teardown(test_nc_connect_unix_socket, setup_f, teardown_f),
+ cmocka_unit_test_setup_teardown(test_nc_connect_unix_socket, setup_f, ln2_glob_test_teardown),
};
setenv("CMOCKA_TEST_ABORT", "1", 1);