blob: 53cbbebf82824b73909ace71c3ba96a73601d7fd [file] [log] [blame]
Michal Vasko294d4c62016-02-01 10:10:27 +01001/**
Michal Vasko7227f352016-02-01 12:11:40 +01002 * \file test_fd_comm.c
Michal Vasko294d4c62016-02-01 10:10:27 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko7227f352016-02-01 12:11:40 +01004 * \brief libnetconf2 tests - file descriptor basic RPC communication
Michal Vasko294d4c62016-02-01 10:10:27 +01005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoad1cc6a2016-02-26 15:06:06 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko294d4c62016-02-01 10:10:27 +010013 */
14
15#include <errno.h>
16#include <fcntl.h>
17#include <pthread.h>
18#include <setjmp.h>
19#include <stdarg.h>
20#include <stddef.h>
21#include <stdint.h>
22#include <stdlib.h>
Michal Vasko294d4c62016-02-01 10:10:27 +010023#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020024#include <sys/socket.h>
Michal Vasko294d4c62016-02-01 10:10:27 +010025#include <sys/stat.h>
26#include <sys/types.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020027#include <unistd.h>
Michal Vasko294d4c62016-02-01 10:10:27 +010028
29#include <cmocka.h>
30#include <libyang/libyang.h>
31
Michal Vasko294d4c62016-02-01 10:10:27 +010032#include <messages_p.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020033#include <session_client.h>
34#include <session_p.h>
35#include <session_server.h>
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020036#include "tests/config.h"
Michal Vasko294d4c62016-02-01 10:10:27 +010037
38struct nc_session *server_session;
39struct nc_session *client_session;
Michal Vasko8c5518b2016-03-01 12:35:13 +010040struct ly_ctx *ctx;
Michal Vaskobbaa9b22019-03-14 12:35:43 +010041pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER;
42int glob_state;
Michal Vasko294d4c62016-02-01 10:10:27 +010043
44struct nc_server_reply *
45my_get_rpc_clb(struct lyd_node *rpc, struct nc_session *session)
46{
47 assert_string_equal(rpc->schema->name, "get");
48 assert_ptr_equal(session, server_session);
49
50 return nc_server_reply_ok();
51}
52
53struct nc_server_reply *
54my_getconfig_rpc_clb(struct lyd_node *rpc, struct nc_session *session)
55{
56 struct lyd_node *data;
Michal Vasko294d4c62016-02-01 10:10:27 +010057
58 assert_string_equal(rpc->schema->name, "get-config");
59 assert_ptr_equal(session, server_session);
60
Michal Vasko77367452021-02-16 16:32:18 +010061 lyd_new_path(NULL, session->ctx, "/ietf-netconf:get-config/data", NULL, LYD_NEW_PATH_OUTPUT, &data);
Michal Vasko294d4c62016-02-01 10:10:27 +010062 assert_non_null(data);
63
Radek Krejci36dfdb32016-09-01 16:56:35 +020064 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko294d4c62016-02-01 10:10:27 +010065}
66
Michal Vasko131120a2018-05-29 15:44:02 +020067struct nc_server_reply *
68my_commit_rpc_clb(struct lyd_node *rpc, struct nc_session *session)
69{
70 assert_string_equal(rpc->schema->name, "commit");
71 assert_ptr_equal(session, server_session);
72
73 /* update state */
Michal Vaskobbaa9b22019-03-14 12:35:43 +010074 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +020075 glob_state = 1;
76
77 /* wait until the client receives the notification */
78 while (glob_state != 3) {
Michal Vaskobbaa9b22019-03-14 12:35:43 +010079 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +020080 usleep(100000);
Michal Vaskobbaa9b22019-03-14 12:35:43 +010081 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +020082 }
Michal Vaskobbaa9b22019-03-14 12:35:43 +010083 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +020084
85 return nc_server_reply_ok();
86}
87
88static struct nc_session *
89test_new_session(NC_SIDE side)
90{
91 struct nc_session *sess;
92
93 sess = calloc(1, sizeof *sess);
94 if (!sess) {
95 return NULL;
96 }
97
98 sess->side = side;
99
100 if (side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100101 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
102 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
103 sess->opts.server.rpc_inuse = 0;
Michal Vasko131120a2018-05-29 15:44:02 +0200104 }
105
106 sess->io_lock = malloc(sizeof *sess->io_lock);
107 if (!sess->io_lock) {
108 goto error;
109 }
110 pthread_mutex_init(sess->io_lock, NULL);
111
112 return sess;
113
114error:
Michal Vasko131120a2018-05-29 15:44:02 +0200115 free(sess);
116 return NULL;
117}
118
Michal Vasko294d4c62016-02-01 10:10:27 +0100119static int
120setup_sessions(void **state)
121{
122 (void)state;
Michal Vasko294d4c62016-02-01 10:10:27 +0100123 int sock[2];
124
Michal Vasko294d4c62016-02-01 10:10:27 +0100125 /* create communication channel */
126 socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
127
Michal Vasko294d4c62016-02-01 10:10:27 +0100128 /* create server session */
Michal Vasko131120a2018-05-29 15:44:02 +0200129 server_session = test_new_session(NC_SERVER);
Michal Vasko294d4c62016-02-01 10:10:27 +0100130 server_session->status = NC_STATUS_RUNNING;
Michal Vasko294d4c62016-02-01 10:10:27 +0100131 server_session->id = 1;
Michal Vasko294d4c62016-02-01 10:10:27 +0100132 server_session->ti_type = NC_TI_FD;
Michal Vasko294d4c62016-02-01 10:10:27 +0100133 server_session->ti.fd.in = sock[0];
134 server_session->ti.fd.out = sock[0];
135 server_session->ctx = ctx;
136 server_session->flags = NC_SESSION_SHAREDCTX;
137
138 /* create client session */
Michal Vasko131120a2018-05-29 15:44:02 +0200139 client_session = test_new_session(NC_CLIENT);
Michal Vasko294d4c62016-02-01 10:10:27 +0100140 client_session->status = NC_STATUS_RUNNING;
Michal Vasko294d4c62016-02-01 10:10:27 +0100141 client_session->id = 1;
Michal Vasko294d4c62016-02-01 10:10:27 +0100142 client_session->ti_type = NC_TI_FD;
Michal Vasko294d4c62016-02-01 10:10:27 +0100143 client_session->ti.fd.in = sock[1];
144 client_session->ti.fd.out = sock[1];
145 client_session->ctx = ctx;
146 client_session->flags = NC_SESSION_SHAREDCTX;
Michal Vasko338f8472016-10-13 15:01:27 +0200147 client_session->opts.client.msgid = 50;
Michal Vasko294d4c62016-02-01 10:10:27 +0100148
149 return 0;
150}
151
152static int
153teardown_sessions(void **state)
154{
155 (void)state;
156
Michal Vasko294d4c62016-02-01 10:10:27 +0100157 close(server_session->ti.fd.in);
Michal Vasko77367452021-02-16 16:32:18 +0100158 server_session->ti.fd.in = -1;
Michal Vasko131120a2018-05-29 15:44:02 +0200159 nc_session_free(server_session, NULL);
Michal Vasko294d4c62016-02-01 10:10:27 +0100160
161 close(client_session->ti.fd.in);
Michal Vasko77367452021-02-16 16:32:18 +0100162 client_session->ti.fd.in = -1;
Michal Vasko131120a2018-05-29 15:44:02 +0200163 nc_session_free(client_session, NULL);
Michal Vasko294d4c62016-02-01 10:10:27 +0100164
165 return 0;
166}
167
168static void
Michal Vasko58d152f2016-02-01 11:44:49 +0100169test_send_recv_ok(void)
Michal Vasko294d4c62016-02-01 10:10:27 +0100170{
Michal Vasko294d4c62016-02-01 10:10:27 +0100171 int ret;
172 uint64_t msgid;
173 NC_MSG_TYPE msgtype;
174 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100175 struct lyd_node *envp, *op;
Michal Vasko294d4c62016-02-01 10:10:27 +0100176 struct nc_pollsession *ps;
177
178 /* client RPC */
179 rpc = nc_rpc_get(NULL, 0, 0);
180 assert_non_null(rpc);
181
182 msgtype = nc_send_rpc(client_session, rpc, 0, &msgid);
183 assert_int_equal(msgtype, NC_MSG_RPC);
184
185 /* server RPC, send reply */
186 ps = nc_ps_new();
187 assert_non_null(ps);
188 nc_ps_add_session(ps, server_session);
189
Michal Vasko71090fc2016-05-24 16:37:28 +0200190 ret = nc_ps_poll(ps, 0, NULL);
191 assert_int_equal(ret, NC_PSPOLL_RPC);
Michal Vasko294d4c62016-02-01 10:10:27 +0100192
193 /* server finished */
194 nc_ps_free(ps);
195
196 /* client reply */
Michal Vasko77367452021-02-16 16:32:18 +0100197 msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op);
Michal Vasko294d4c62016-02-01 10:10:27 +0100198 assert_int_equal(msgtype, NC_MSG_REPLY);
199
200 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100201 assert_null(op);
202 assert_string_equal(LYD_NAME(lyd_child(envp)), "ok");
203 lyd_free_tree(envp);
Michal Vasko294d4c62016-02-01 10:10:27 +0100204}
205
206static void
Michal Vasko58d152f2016-02-01 11:44:49 +0100207test_send_recv_ok_10(void **state)
Michal Vasko294d4c62016-02-01 10:10:27 +0100208{
209 (void)state;
Michal Vasko58d152f2016-02-01 11:44:49 +0100210
211 server_session->version = NC_VERSION_10;
212 client_session->version = NC_VERSION_10;
213
214 test_send_recv_ok();
215}
216
217static void
218test_send_recv_ok_11(void **state)
219{
220 (void)state;
221
222 server_session->version = NC_VERSION_11;
223 client_session->version = NC_VERSION_11;
224
225 test_send_recv_ok();
226}
227
228static void
229test_send_recv_error(void)
230{
Michal Vasko294d4c62016-02-01 10:10:27 +0100231 int ret;
232 uint64_t msgid;
233 NC_MSG_TYPE msgtype;
234 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100235 struct lyd_node *envp, *op, *node;
Michal Vasko294d4c62016-02-01 10:10:27 +0100236 struct nc_pollsession *ps;
237
238 /* client RPC */
239 rpc = nc_rpc_kill(1);
240 assert_non_null(rpc);
241
242 msgtype = nc_send_rpc(client_session, rpc, 0, &msgid);
243 assert_int_equal(msgtype, NC_MSG_RPC);
244
245 /* server RPC, send reply */
246 ps = nc_ps_new();
247 assert_non_null(ps);
248 nc_ps_add_session(ps, server_session);
249
Michal Vasko71090fc2016-05-24 16:37:28 +0200250 ret = nc_ps_poll(ps, 0, NULL);
251 assert_int_equal(ret, NC_PSPOLL_RPC | NC_PSPOLL_REPLY_ERROR);
Michal Vasko294d4c62016-02-01 10:10:27 +0100252
253 /* server finished */
254 nc_ps_free(ps);
255
256 /* client reply */
Michal Vasko77367452021-02-16 16:32:18 +0100257 msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op);
Michal Vasko294d4c62016-02-01 10:10:27 +0100258 assert_int_equal(msgtype, NC_MSG_REPLY);
259
260 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100261 assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error");
262 lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node);
263 assert_non_null(node);
264 assert_string_equal(((struct lyd_node_opaq *)node)->value, "operation-not-supported");
265 lyd_free_tree(envp);
266 assert_null(op);
Michal Vasko294d4c62016-02-01 10:10:27 +0100267}
268
269static void
Michal Vasko58d152f2016-02-01 11:44:49 +0100270test_send_recv_error_10(void **state)
Michal Vasko294d4c62016-02-01 10:10:27 +0100271{
272 (void)state;
Michal Vasko58d152f2016-02-01 11:44:49 +0100273
274 server_session->version = NC_VERSION_10;
275 client_session->version = NC_VERSION_10;
276
277 test_send_recv_error();
278}
279
280static void
281test_send_recv_error_11(void **state)
282{
283 (void)state;
284
285 server_session->version = NC_VERSION_11;
286 client_session->version = NC_VERSION_11;
287
288 test_send_recv_error();
289}
290
291static void
292test_send_recv_data(void)
293{
Michal Vasko294d4c62016-02-01 10:10:27 +0100294 int ret;
295 uint64_t msgid;
296 NC_MSG_TYPE msgtype;
297 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100298 struct lyd_node *envp, *op;
Michal Vasko294d4c62016-02-01 10:10:27 +0100299 struct nc_pollsession *ps;
300
301 /* client RPC */
302 rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, NULL, 0, 0);
303 assert_non_null(rpc);
304
305 msgtype = nc_send_rpc(client_session, rpc, 0, &msgid);
306 assert_int_equal(msgtype, NC_MSG_RPC);
307
308 /* server RPC, send reply */
309 ps = nc_ps_new();
310 assert_non_null(ps);
311 nc_ps_add_session(ps, server_session);
312
Michal Vasko71090fc2016-05-24 16:37:28 +0200313 ret = nc_ps_poll(ps, 0, NULL);
314 assert_int_equal(ret, NC_PSPOLL_RPC);
Michal Vasko294d4c62016-02-01 10:10:27 +0100315
316 /* server finished */
317 nc_ps_free(ps);
318
319 /* client reply */
Michal Vasko77367452021-02-16 16:32:18 +0100320 msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op);
Michal Vasko294d4c62016-02-01 10:10:27 +0100321 assert_int_equal(msgtype, NC_MSG_REPLY);
322
323 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100324 assert_non_null(envp);
325 lyd_free_tree(envp);
326 assert_non_null(op);
327 lyd_free_tree(op);
Michal Vasko294d4c62016-02-01 10:10:27 +0100328}
329
Michal Vasko58d152f2016-02-01 11:44:49 +0100330static void
331test_send_recv_data_10(void **state)
332{
333 (void)state;
334
335 server_session->version = NC_VERSION_10;
336 client_session->version = NC_VERSION_10;
337
338 test_send_recv_data();
339}
340
341static void
342test_send_recv_data_11(void **state)
343{
344 (void)state;
345
346 server_session->version = NC_VERSION_11;
347 client_session->version = NC_VERSION_11;
348
349 test_send_recv_data();
350}
351
Michal Vasko131120a2018-05-29 15:44:02 +0200352static void
Michal Vasko77367452021-02-16 16:32:18 +0100353test_notif_clb(struct nc_session *session, const struct lyd_node *UNUSED(envp), const struct lyd_node *op)
Michal Vasko131120a2018-05-29 15:44:02 +0200354{
355 assert_ptr_equal(session, client_session);
Michal Vasko77367452021-02-16 16:32:18 +0100356 assert_string_equal(op->schema->name, "notificationComplete");
Michal Vasko131120a2018-05-29 15:44:02 +0200357
358 /* client notification received, update state */
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100359 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200360 while (glob_state != 2) {
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100361 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200362 usleep(1000);
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100363 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200364 }
365 glob_state = 3;
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100366 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200367}
368
369static void *
370server_send_notif_thread(void *arg)
371{
372 NC_MSG_TYPE msg_type;
373 struct lyd_node *notif_tree;
374 struct nc_server_notif *notif;
Michal Vasko49eb3f42021-05-19 10:20:57 +0200375 struct timespec ts;
Michal Vasko131120a2018-05-29 15:44:02 +0200376 char *buf;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200377
Michal Vasko131120a2018-05-29 15:44:02 +0200378 (void)arg;
379
380 /* wait for the RPC callback to be called */
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100381 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200382 while (glob_state != 1) {
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100383 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200384 usleep(1000);
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100385 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200386 }
387
388 /* create notif */
Michal Vasko77367452021-02-16 16:32:18 +0100389 lyd_new_path(NULL, ctx, "/nc-notifications:notificationComplete", NULL, 0, &notif_tree);
Michal Vasko131120a2018-05-29 15:44:02 +0200390 assert_non_null(notif_tree);
Michal Vasko49eb3f42021-05-19 10:20:57 +0200391 clock_gettime(CLOCK_REALTIME, &ts);
Michal Vaskod7fb6df2021-05-19 11:27:35 +0200392 ly_time_ts2str(&ts, &buf);
393 notif = nc_server_notif_new(notif_tree, buf, NC_PARAMTYPE_FREE);
Michal Vasko131120a2018-05-29 15:44:02 +0200394 assert_non_null(notif);
395
396 /* send notif */
Michal Vasko71dbd772021-03-23 14:08:37 +0100397 nc_session_inc_notif_status(server_session);
Michal Vasko131120a2018-05-29 15:44:02 +0200398 msg_type = nc_server_notif_send(server_session, notif, 100);
399 nc_server_notif_free(notif);
400 assert_int_equal(msg_type, NC_MSG_NOTIF);
401
402 /* update state */
403 glob_state = 2;
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100404 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200405
406 return NULL;
407}
408
Michal Vasko294d4c62016-02-01 10:10:27 +0100409static void
Michal Vasko58d152f2016-02-01 11:44:49 +0100410test_send_recv_notif(void)
Michal Vasko294d4c62016-02-01 10:10:27 +0100411{
Michal Vasko131120a2018-05-29 15:44:02 +0200412 int ret;
413 pthread_t tid;
414 uint64_t msgid;
415 NC_MSG_TYPE msgtype;
416 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100417 struct lyd_node *envp, *op;
Michal Vasko131120a2018-05-29 15:44:02 +0200418 struct nc_pollsession *ps;
Michal Vasko294d4c62016-02-01 10:10:27 +0100419
Michal Vasko131120a2018-05-29 15:44:02 +0200420 /* client RPC */
421 rpc = nc_rpc_commit(0, 0, NULL, NULL, 0);
422 assert_non_null(rpc);
423
424 msgtype = nc_send_rpc(client_session, rpc, 0, &msgid);
425 assert_int_equal(msgtype, NC_MSG_RPC);
426
427 /* client subscription */
428 ret = nc_recv_notif_dispatch(client_session, test_notif_clb);
429 assert_int_equal(ret, 0);
430
431 /* create server */
432 ps = nc_ps_new();
433 assert_non_null(ps);
434 nc_ps_add_session(ps, server_session);
435
436 /* server will send a notification */
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100437 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200438 glob_state = 0;
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100439 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200440 ret = pthread_create(&tid, NULL, server_send_notif_thread, NULL);
441 assert_int_equal(ret, 0);
442
443 /* server blocked on RPC */
444 ret = nc_ps_poll(ps, 0, NULL);
445 assert_int_equal(ret, NC_PSPOLL_RPC);
446
447 /* RPC, notification finished fine */
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100448 pthread_mutex_lock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200449 assert_int_equal(glob_state, 3);
Michal Vaskobbaa9b22019-03-14 12:35:43 +0100450 pthread_mutex_unlock(&state_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200451
452 /* server finished */
453 ret = pthread_join(tid, NULL);
454 assert_int_equal(ret, 0);
455 nc_ps_free(ps);
456
457 /* client reply */
Michal Vasko77367452021-02-16 16:32:18 +0100458 msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op);
Michal Vasko131120a2018-05-29 15:44:02 +0200459 assert_int_equal(msgtype, NC_MSG_REPLY);
Michal Vasko131120a2018-05-29 15:44:02 +0200460 nc_rpc_free(rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100461
462 assert_string_equal(LYD_NAME(lyd_child(envp)), "ok");
463 lyd_free_tree(envp);
464 assert_null(op);
Michal Vasko131120a2018-05-29 15:44:02 +0200465}
466
467static void
468test_send_recv_notif_10(void **state)
469{
470 (void)state;
471
472 server_session->version = NC_VERSION_10;
473 client_session->version = NC_VERSION_10;
474
475 test_send_recv_notif();
476}
477
478static void
479test_send_recv_notif_11(void **state)
480{
481 (void)state;
482
483 server_session->version = NC_VERSION_11;
484 client_session->version = NC_VERSION_11;
485
486 test_send_recv_notif();
487}
Michal Vasko294d4c62016-02-01 10:10:27 +0100488
489int
490main(void)
491{
Michal Vasko8c5518b2016-03-01 12:35:13 +0100492 int ret;
493 const struct lys_module *module;
Michal Vasko77367452021-02-16 16:32:18 +0100494 struct lysc_node *node;
495 const char *nc_features[] = {"candidate", NULL};
Michal Vasko8c5518b2016-03-01 12:35:13 +0100496
497 /* create ctx */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200498 ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx);
Michal Vasko8c5518b2016-03-01 12:35:13 +0100499 assert_non_null(ctx);
500
501 /* load modules */
Michal Vasko77367452021-02-16 16:32:18 +0100502 module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL, NULL);
Michal Vasko8c5518b2016-03-01 12:35:13 +0100503 assert_non_null(module);
504
Michal Vasko77367452021-02-16 16:32:18 +0100505 module = ly_ctx_load_module(ctx, "ietf-netconf", NULL, nc_features);
Michal Vasko8c5518b2016-03-01 12:35:13 +0100506 assert_non_null(module);
Michal Vasko131120a2018-05-29 15:44:02 +0200507
Michal Vasko77367452021-02-16 16:32:18 +0100508 module = ly_ctx_load_module(ctx, "nc-notifications", NULL, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200509 assert_non_null(module);
Michal Vasko8c5518b2016-03-01 12:35:13 +0100510
511 /* set RPC callbacks */
Michal Vasko77367452021-02-16 16:32:18 +0100512 node = (struct lysc_node *)lys_find_path(module->ctx, NULL, "/ietf-netconf:get", 0);
Michal Vasko88639e92017-08-03 14:38:10 +0200513 assert_non_null(node);
Michal Vasko77367452021-02-16 16:32:18 +0100514 node->priv = my_get_rpc_clb;
Michal Vasko88639e92017-08-03 14:38:10 +0200515
Michal Vasko77367452021-02-16 16:32:18 +0100516 node = (struct lysc_node *)lys_find_path(module->ctx, NULL, "/ietf-netconf:get-config", 0);
Michal Vasko88639e92017-08-03 14:38:10 +0200517 assert_non_null(node);
Michal Vasko77367452021-02-16 16:32:18 +0100518 node->priv = my_getconfig_rpc_clb;
Michal Vasko8c5518b2016-03-01 12:35:13 +0100519
Michal Vasko77367452021-02-16 16:32:18 +0100520 node = (struct lysc_node *)lys_find_path(module->ctx, NULL, "/ietf-netconf:commit", 0);
Michal Vasko131120a2018-05-29 15:44:02 +0200521 assert_non_null(node);
Michal Vasko77367452021-02-16 16:32:18 +0100522 node->priv = my_commit_rpc_clb;
Michal Vasko131120a2018-05-29 15:44:02 +0200523
Michal Vasko8c5518b2016-03-01 12:35:13 +0100524 nc_server_init(ctx);
525
Michal Vasko294d4c62016-02-01 10:10:27 +0100526 const struct CMUnitTest comm[] = {
Michal Vasko58d152f2016-02-01 11:44:49 +0100527 cmocka_unit_test_setup_teardown(test_send_recv_ok_10, setup_sessions, teardown_sessions),
528 cmocka_unit_test_setup_teardown(test_send_recv_error_10, setup_sessions, teardown_sessions),
529 cmocka_unit_test_setup_teardown(test_send_recv_data_10, setup_sessions, teardown_sessions),
Michal Vasko131120a2018-05-29 15:44:02 +0200530 cmocka_unit_test_setup_teardown(test_send_recv_notif_10, setup_sessions, teardown_sessions),
Michal Vasko58d152f2016-02-01 11:44:49 +0100531 cmocka_unit_test_setup_teardown(test_send_recv_ok_11, setup_sessions, teardown_sessions),
532 cmocka_unit_test_setup_teardown(test_send_recv_error_11, setup_sessions, teardown_sessions),
Michal Vasko131120a2018-05-29 15:44:02 +0200533 cmocka_unit_test_setup_teardown(test_send_recv_data_11, setup_sessions, teardown_sessions),
534 cmocka_unit_test_setup_teardown(test_send_recv_notif_11, setup_sessions, teardown_sessions),
Michal Vasko294d4c62016-02-01 10:10:27 +0100535 };
536
Michal Vasko8c5518b2016-03-01 12:35:13 +0100537 ret = cmocka_run_group_tests(comm, NULL, NULL);
538
539 nc_server_destroy();
Michal Vasko1dcb7642021-04-14 15:28:23 +0200540 ly_ctx_destroy(ctx);
Michal Vasko8c5518b2016-03-01 12:35:13 +0100541
542 return ret;
Michal Vasko294d4c62016-02-01 10:10:27 +0100543}