Skip to content

Commit 33d044d

Browse files
romanmichalvasko
authored andcommitted
ln2 test UPDATE add shared functionality
1 parent afee40e commit 33d044d

2 files changed

Lines changed: 171 additions & 2 deletions

File tree

tests/ln2_test.c

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#define _GNU_SOURCE
1717

18+
#include <assert.h>
19+
#include <pthread.h>
1820
#include <stdarg.h>
1921
#include <stdio.h>
2022
#include <stdlib.h>
@@ -26,8 +28,7 @@ int
2628
ln2_glob_test_get_ports(int port_count, ...)
2729
{
2830
va_list ap;
29-
int i, ret = 0;
30-
int *port_ptr;
31+
int i, ret = 0, *port_ptr;
3132
const char **port_str_ptr, *env;
3233
char *env_name = NULL;
3334

@@ -46,6 +47,7 @@ ln2_glob_test_get_ports(int port_count, ...)
4647
env = getenv(env_name);
4748
free(env_name);
4849
if (!env) {
50+
/* the default value will be used instead */
4951
continue;
5052
}
5153

@@ -57,3 +59,118 @@ ln2_glob_test_get_ports(int port_count, ...)
5759
va_end(ap);
5860
return ret;
5961
}
62+
63+
void *
64+
ln2_glob_test_server_thread(void *arg)
65+
{
66+
int ret;
67+
NC_MSG_TYPE msgtype;
68+
struct nc_session *session = NULL;
69+
struct nc_pollsession *ps = NULL;
70+
struct ln2_test_ctx *test_ctx = arg;
71+
72+
ps = nc_ps_new();
73+
assert(ps);
74+
75+
/* wait for the client to be ready to connect */
76+
pthread_barrier_wait(&test_ctx->barrier);
77+
78+
/* accept a session and add it to the poll session structure */
79+
msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
80+
assert(msgtype == NC_MSG_HELLO);
81+
82+
ret = nc_ps_add_session(ps, session);
83+
assert(!ret);
84+
85+
/* poll until the session is terminated by the client */
86+
do {
87+
ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
88+
assert(ret & NC_PSPOLL_RPC);
89+
} while (!(ret & NC_PSPOLL_SESSION_TERM));
90+
91+
nc_ps_clear(ps, 1, NULL);
92+
nc_ps_free(ps);
93+
return NULL;
94+
}
95+
96+
int
97+
ln2_glob_test_setup(struct ln2_test_ctx **test_ctx)
98+
{
99+
int ret;
100+
struct ln2_test_ctx *tmp_ctx;
101+
102+
*test_ctx = NULL;
103+
104+
tmp_ctx = calloc(1, sizeof *tmp_ctx);
105+
if (!tmp_ctx) {
106+
ret = 1;
107+
goto cleanup;
108+
}
109+
110+
/* set verbosity */
111+
nc_verbosity(NC_VERB_VERBOSE);
112+
113+
/* initialize server */
114+
ret = nc_server_init();
115+
if (ret) {
116+
goto cleanup;
117+
}
118+
119+
/* initialize client */
120+
ret = nc_client_init();
121+
if (ret) {
122+
goto cleanup;
123+
}
124+
125+
/* init barrier */
126+
ret = pthread_barrier_init(&tmp_ctx->barrier, NULL, 2);
127+
if (ret) {
128+
goto cleanup;
129+
}
130+
131+
/* create libyang context */
132+
ret = ly_ctx_new(MODULES_DIR, 0, &tmp_ctx->ctx);
133+
if (ret) {
134+
goto cleanup;
135+
}
136+
137+
/* load default yang modules */
138+
ret = nc_server_init_ctx(&tmp_ctx->ctx);
139+
if (ret) {
140+
goto cleanup;
141+
}
142+
ret = nc_server_config_load_modules(&tmp_ctx->ctx);
143+
if (ret) {
144+
goto cleanup;
145+
}
146+
147+
*test_ctx = tmp_ctx;
148+
149+
cleanup:
150+
return ret;
151+
}
152+
153+
int
154+
ln2_glob_test_teardown(void **state)
155+
{
156+
struct ln2_test_ctx *test_ctx = *state;
157+
158+
nc_client_destroy();
159+
nc_server_destroy();
160+
161+
if (test_ctx->free_test_data) {
162+
test_ctx->free_test_data(test_ctx->test_data);
163+
}
164+
165+
pthread_barrier_destroy(&test_ctx->barrier);
166+
ly_ctx_destroy(test_ctx->ctx);
167+
free(test_ctx);
168+
169+
return 0;
170+
}
171+
172+
void
173+
ln2_glob_test_free_test_data(void *test_data)
174+
{
175+
free(test_data);
176+
}

tests/ln2_test.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,62 @@
1616
#ifndef _LN2_TEST_H_
1717
#define _LN2_TEST_H_
1818

19+
#include <pthread.h>
1920
#include <stdarg.h>
2021

2122
#include "tests/config.h"
2223

24+
#define NC_ACCEPT_TIMEOUT 2000
25+
#define NC_PS_POLL_TIMEOUT 2000
26+
27+
/**
28+
* @brief Test context used for sharing data between the test and the server/client threads.
29+
*/
30+
struct ln2_test_ctx {
31+
pthread_barrier_t barrier; /**< Barrier for synchronizing the client and the server. */
32+
struct ly_ctx *ctx; /**< libyang context. */
33+
void *test_data; /**< Arbitrary test data. */
34+
void (*free_test_data)(void *); /**< Callback for freeing the test data. */
35+
};
36+
37+
/**
38+
* @brief Try to obtain ports from the TEST_PORT_X environment variables.
39+
*
40+
* @param[in] port_count Number of ports needed by the test.
41+
* @param[in] ... @p port_count number of (int *, const char **) pairs, which will be filled with the port numbers.
42+
* @return 0 on success, 1 on error.
43+
*/
2344
int ln2_glob_test_get_ports(int port_count, ...);
2445

46+
/**
47+
* @brief Default server thread for the tests.
48+
*
49+
* @param[in] arg Test context.
50+
* @return NULL.
51+
*/
52+
void * ln2_glob_test_server_thread(void *arg);
53+
54+
/**
55+
* @brief Default setup of the test context (init server, client, libyang context and a barrier).
56+
*
57+
* @param[out] test_ctx Test context.
58+
* @return 0 on success, non-zero on error.
59+
*/
60+
int ln2_glob_test_setup(struct ln2_test_ctx **test_ctx);
61+
62+
/**
63+
* @brief Default teardown of the test context (destroy server, client, test data, libyang context and a barrier).
64+
*
65+
* @param[in] state Test context.
66+
* @return 0.
67+
*/
68+
int ln2_glob_test_teardown(void **state);
69+
70+
/**
71+
* @brief Default callback for freeing test data.
72+
*
73+
* @param[in] test_data Test data.
74+
*/
75+
void ln2_glob_test_free_test_data(void *test_data);
76+
2577
#endif

0 commit comments

Comments
 (0)