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>
2628ln2_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+ }
0 commit comments