Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 7227f35 | 2016-02-01 12:11:40 +0100 | [diff] [blame] | 2 | * \file test_fd_comm.c |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 7227f35 | 2016-02-01 12:11:40 +0100 | [diff] [blame] | 4 | * \brief libnetconf2 tests - file descriptor basic RPC communication |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | ad1cc6a | 2016-02-26 15:06:06 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 13 | */ |
| 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> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | |
| 29 | #include <cmocka.h> |
| 30 | #include <libyang/libyang.h> |
| 31 | |
| 32 | #include <session_client.h> |
| 33 | #include <session_server.h> |
| 34 | #include <session_p.h> |
| 35 | #include <messages_p.h> |
Jan Kundrát | cf15d6c | 2017-10-26 18:07:56 +0200 | [diff] [blame] | 36 | #include "tests/config.h" |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 37 | |
| 38 | struct nc_session *server_session; |
| 39 | struct nc_session *client_session; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 40 | struct ly_ctx *ctx; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 41 | pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; |
| 42 | int glob_state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 43 | |
| 44 | struct nc_server_reply * |
| 45 | my_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 | |
| 53 | struct nc_server_reply * |
| 54 | my_getconfig_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 55 | { |
| 56 | struct lyd_node *data; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 57 | |
| 58 | assert_string_equal(rpc->schema->name, "get-config"); |
| 59 | assert_ptr_equal(session, server_session); |
| 60 | |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 61 | data = lyd_new_path(NULL, session->ctx, "/ietf-netconf:get-config/data", NULL, LYD_ANYDATA_CONSTSTRING, |
| 62 | LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 63 | assert_non_null(data); |
| 64 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 65 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 68 | struct nc_server_reply * |
| 69 | my_commit_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 70 | { |
| 71 | assert_string_equal(rpc->schema->name, "commit"); |
| 72 | assert_ptr_equal(session, server_session); |
| 73 | |
| 74 | /* update state */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 75 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 76 | glob_state = 1; |
| 77 | |
| 78 | /* wait until the client receives the notification */ |
| 79 | while (glob_state != 3) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 80 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 81 | usleep(100000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 82 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 83 | } |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 84 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 85 | |
| 86 | return nc_server_reply_ok(); |
| 87 | } |
| 88 | |
| 89 | static struct nc_session * |
| 90 | test_new_session(NC_SIDE side) |
| 91 | { |
| 92 | struct nc_session *sess; |
| 93 | |
| 94 | sess = calloc(1, sizeof *sess); |
| 95 | if (!sess) { |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | sess->side = side; |
| 100 | |
| 101 | if (side == NC_SERVER) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 102 | pthread_mutex_init(&sess->opts.server.rpc_lock, NULL); |
| 103 | pthread_cond_init(&sess->opts.server.rpc_cond, NULL); |
| 104 | sess->opts.server.rpc_inuse = 0; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | sess->io_lock = malloc(sizeof *sess->io_lock); |
| 108 | if (!sess->io_lock) { |
| 109 | goto error; |
| 110 | } |
| 111 | pthread_mutex_init(sess->io_lock, NULL); |
| 112 | |
| 113 | return sess; |
| 114 | |
| 115 | error: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 116 | free(sess); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 120 | static int |
| 121 | setup_sessions(void **state) |
| 122 | { |
| 123 | (void)state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 124 | int sock[2]; |
| 125 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 126 | /* create communication channel */ |
| 127 | socketpair(AF_UNIX, SOCK_STREAM, 0, sock); |
| 128 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 129 | /* create server session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 130 | server_session = test_new_session(NC_SERVER); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 131 | server_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 132 | server_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 133 | server_session->ti_type = NC_TI_FD; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 134 | server_session->ti.fd.in = sock[0]; |
| 135 | server_session->ti.fd.out = sock[0]; |
| 136 | server_session->ctx = ctx; |
| 137 | server_session->flags = NC_SESSION_SHAREDCTX; |
| 138 | |
| 139 | /* create client session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 140 | client_session = test_new_session(NC_CLIENT); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 141 | client_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 142 | client_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 143 | client_session->ti_type = NC_TI_FD; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 144 | client_session->ti.fd.in = sock[1]; |
| 145 | client_session->ti.fd.out = sock[1]; |
| 146 | client_session->ctx = ctx; |
| 147 | client_session->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 148 | client_session->opts.client.msgid = 50; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int |
| 154 | teardown_sessions(void **state) |
| 155 | { |
| 156 | (void)state; |
| 157 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 158 | close(server_session->ti.fd.in); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 159 | nc_session_free(server_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 160 | |
| 161 | close(client_session->ti.fd.in); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 162 | nc_session_free(client_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 168 | test_send_recv_ok(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 169 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 170 | int ret; |
| 171 | uint64_t msgid; |
| 172 | NC_MSG_TYPE msgtype; |
| 173 | struct nc_rpc *rpc; |
| 174 | struct nc_reply *reply; |
| 175 | struct nc_pollsession *ps; |
| 176 | |
| 177 | /* client RPC */ |
| 178 | rpc = nc_rpc_get(NULL, 0, 0); |
| 179 | assert_non_null(rpc); |
| 180 | |
| 181 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 182 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 183 | |
| 184 | /* server RPC, send reply */ |
| 185 | ps = nc_ps_new(); |
| 186 | assert_non_null(ps); |
| 187 | nc_ps_add_session(ps, server_session); |
| 188 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 189 | ret = nc_ps_poll(ps, 0, NULL); |
| 190 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 191 | |
| 192 | /* server finished */ |
| 193 | nc_ps_free(ps); |
| 194 | |
| 195 | /* client reply */ |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 196 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 197 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 198 | |
| 199 | nc_rpc_free(rpc); |
| 200 | assert_int_equal(reply->type, NC_RPL_OK); |
| 201 | nc_reply_free(reply); |
| 202 | } |
| 203 | |
| 204 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 205 | test_send_recv_ok_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 206 | { |
| 207 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 208 | |
| 209 | server_session->version = NC_VERSION_10; |
| 210 | client_session->version = NC_VERSION_10; |
| 211 | |
| 212 | test_send_recv_ok(); |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | test_send_recv_ok_11(void **state) |
| 217 | { |
| 218 | (void)state; |
| 219 | |
| 220 | server_session->version = NC_VERSION_11; |
| 221 | client_session->version = NC_VERSION_11; |
| 222 | |
| 223 | test_send_recv_ok(); |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | test_send_recv_error(void) |
| 228 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 229 | int ret; |
| 230 | uint64_t msgid; |
| 231 | NC_MSG_TYPE msgtype; |
| 232 | struct nc_rpc *rpc; |
| 233 | struct nc_reply *reply; |
| 234 | struct nc_pollsession *ps; |
| 235 | |
| 236 | /* client RPC */ |
| 237 | rpc = nc_rpc_kill(1); |
| 238 | assert_non_null(rpc); |
| 239 | |
| 240 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 241 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 242 | |
| 243 | /* server RPC, send reply */ |
| 244 | ps = nc_ps_new(); |
| 245 | assert_non_null(ps); |
| 246 | nc_ps_add_session(ps, server_session); |
| 247 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 248 | ret = nc_ps_poll(ps, 0, NULL); |
| 249 | assert_int_equal(ret, NC_PSPOLL_RPC | NC_PSPOLL_REPLY_ERROR); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 250 | |
| 251 | /* server finished */ |
| 252 | nc_ps_free(ps); |
| 253 | |
| 254 | /* client reply */ |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 255 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 256 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 257 | |
| 258 | nc_rpc_free(rpc); |
| 259 | assert_int_equal(reply->type, NC_RPL_ERROR); |
| 260 | assert_string_equal(((struct nc_reply_error *)reply)->err->tag, "operation-not-supported"); |
| 261 | nc_reply_free(reply); |
| 262 | } |
| 263 | |
| 264 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 265 | test_send_recv_error_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 266 | { |
| 267 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 268 | |
| 269 | server_session->version = NC_VERSION_10; |
| 270 | client_session->version = NC_VERSION_10; |
| 271 | |
| 272 | test_send_recv_error(); |
| 273 | } |
| 274 | |
| 275 | static void |
| 276 | test_send_recv_error_11(void **state) |
| 277 | { |
| 278 | (void)state; |
| 279 | |
| 280 | server_session->version = NC_VERSION_11; |
| 281 | client_session->version = NC_VERSION_11; |
| 282 | |
| 283 | test_send_recv_error(); |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | test_send_recv_data(void) |
| 288 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 289 | int ret; |
| 290 | uint64_t msgid; |
| 291 | NC_MSG_TYPE msgtype; |
| 292 | struct nc_rpc *rpc; |
| 293 | struct nc_reply *reply; |
| 294 | struct nc_pollsession *ps; |
| 295 | |
| 296 | /* client RPC */ |
| 297 | rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, NULL, 0, 0); |
| 298 | assert_non_null(rpc); |
| 299 | |
| 300 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 301 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 302 | |
| 303 | /* server RPC, send reply */ |
| 304 | ps = nc_ps_new(); |
| 305 | assert_non_null(ps); |
| 306 | nc_ps_add_session(ps, server_session); |
| 307 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 308 | ret = nc_ps_poll(ps, 0, NULL); |
| 309 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 310 | |
| 311 | /* server finished */ |
| 312 | nc_ps_free(ps); |
| 313 | |
| 314 | /* client reply */ |
Radek Krejci | 8c8e124 | 2016-09-07 16:43:54 +0200 | [diff] [blame] | 315 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 316 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 317 | |
| 318 | nc_rpc_free(rpc); |
| 319 | assert_int_equal(reply->type, NC_RPL_DATA); |
| 320 | nc_reply_free(reply); |
| 321 | } |
| 322 | |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 323 | static void |
| 324 | test_send_recv_data_10(void **state) |
| 325 | { |
| 326 | (void)state; |
| 327 | |
| 328 | server_session->version = NC_VERSION_10; |
| 329 | client_session->version = NC_VERSION_10; |
| 330 | |
| 331 | test_send_recv_data(); |
| 332 | } |
| 333 | |
| 334 | static void |
| 335 | test_send_recv_data_11(void **state) |
| 336 | { |
| 337 | (void)state; |
| 338 | |
| 339 | server_session->version = NC_VERSION_11; |
| 340 | client_session->version = NC_VERSION_11; |
| 341 | |
| 342 | test_send_recv_data(); |
| 343 | } |
| 344 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 345 | static void |
| 346 | test_notif_clb(struct nc_session *session, const struct nc_notif *notif) |
| 347 | { |
| 348 | assert_ptr_equal(session, client_session); |
| 349 | assert_string_equal(notif->tree->schema->name, "notificationComplete"); |
| 350 | |
| 351 | /* client notification received, update state */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 352 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 353 | while (glob_state != 2) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 354 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 355 | usleep(1000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 356 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 357 | } |
| 358 | glob_state = 3; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 359 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void * |
| 363 | server_send_notif_thread(void *arg) |
| 364 | { |
| 365 | NC_MSG_TYPE msg_type; |
| 366 | struct lyd_node *notif_tree; |
| 367 | struct nc_server_notif *notif; |
| 368 | char *buf; |
| 369 | (void)arg; |
| 370 | |
| 371 | /* wait for the RPC callback to be called */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 372 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 373 | while (glob_state != 1) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 374 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 375 | usleep(1000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 376 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* create notif */ |
| 380 | notif_tree = lyd_new_path(NULL, ctx, "/nc-notifications:notificationComplete", NULL, 0, 0); |
| 381 | assert_non_null(notif_tree); |
| 382 | buf = malloc(64); |
| 383 | assert_non_null(buf); |
| 384 | notif = nc_server_notif_new(notif_tree, nc_time2datetime(time(NULL), NULL, buf), NC_PARAMTYPE_FREE); |
| 385 | assert_non_null(notif); |
| 386 | |
| 387 | /* send notif */ |
| 388 | nc_session_set_notif_status(server_session, 1); |
| 389 | msg_type = nc_server_notif_send(server_session, notif, 100); |
| 390 | nc_server_notif_free(notif); |
| 391 | assert_int_equal(msg_type, NC_MSG_NOTIF); |
| 392 | |
| 393 | /* update state */ |
| 394 | glob_state = 2; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 395 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 396 | |
| 397 | return NULL; |
| 398 | } |
| 399 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 400 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 401 | test_send_recv_notif(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 402 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 403 | int ret; |
| 404 | pthread_t tid; |
| 405 | uint64_t msgid; |
| 406 | NC_MSG_TYPE msgtype; |
| 407 | struct nc_rpc *rpc; |
| 408 | struct nc_reply *reply; |
| 409 | struct nc_pollsession *ps; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 410 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 411 | /* client RPC */ |
| 412 | rpc = nc_rpc_commit(0, 0, NULL, NULL, 0); |
| 413 | assert_non_null(rpc); |
| 414 | |
| 415 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 416 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 417 | |
| 418 | /* client subscription */ |
| 419 | ret = nc_recv_notif_dispatch(client_session, test_notif_clb); |
| 420 | assert_int_equal(ret, 0); |
| 421 | |
| 422 | /* create server */ |
| 423 | ps = nc_ps_new(); |
| 424 | assert_non_null(ps); |
| 425 | nc_ps_add_session(ps, server_session); |
| 426 | |
| 427 | /* server will send a notification */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 428 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 429 | glob_state = 0; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 430 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 431 | ret = pthread_create(&tid, NULL, server_send_notif_thread, NULL); |
| 432 | assert_int_equal(ret, 0); |
| 433 | |
| 434 | /* server blocked on RPC */ |
| 435 | ret = nc_ps_poll(ps, 0, NULL); |
| 436 | assert_int_equal(ret, NC_PSPOLL_RPC); |
| 437 | |
| 438 | /* RPC, notification finished fine */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 439 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 440 | assert_int_equal(glob_state, 3); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 441 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 442 | |
| 443 | /* server finished */ |
| 444 | ret = pthread_join(tid, NULL); |
| 445 | assert_int_equal(ret, 0); |
| 446 | nc_ps_free(ps); |
| 447 | |
| 448 | /* client reply */ |
| 449 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
| 450 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 451 | |
| 452 | nc_rpc_free(rpc); |
| 453 | assert_int_equal(reply->type, NC_RPL_OK); |
| 454 | nc_reply_free(reply); |
| 455 | } |
| 456 | |
| 457 | static void |
| 458 | test_send_recv_notif_10(void **state) |
| 459 | { |
| 460 | (void)state; |
| 461 | |
| 462 | server_session->version = NC_VERSION_10; |
| 463 | client_session->version = NC_VERSION_10; |
| 464 | |
| 465 | test_send_recv_notif(); |
| 466 | } |
| 467 | |
| 468 | static void |
| 469 | test_send_recv_notif_11(void **state) |
| 470 | { |
| 471 | (void)state; |
| 472 | |
| 473 | server_session->version = NC_VERSION_11; |
| 474 | client_session->version = NC_VERSION_11; |
| 475 | |
| 476 | test_send_recv_notif(); |
| 477 | } |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 478 | |
| 479 | int |
| 480 | main(void) |
| 481 | { |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 482 | int ret; |
| 483 | const struct lys_module *module; |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 484 | const struct lys_node *node; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 485 | |
| 486 | /* create ctx */ |
Michal Vasko | 5a44647 | 2020-05-06 12:43:42 +0200 | [diff] [blame] | 487 | ctx = ly_ctx_new(TESTS_DIR"/data/modules", 0); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 488 | assert_non_null(ctx); |
| 489 | |
| 490 | /* load modules */ |
| 491 | module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL); |
| 492 | assert_non_null(module); |
| 493 | |
| 494 | module = ly_ctx_load_module(ctx, "ietf-netconf", NULL); |
| 495 | assert_non_null(module); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 496 | ret = lys_features_enable(module, "candidate"); |
| 497 | assert_int_equal(ret, 0); |
| 498 | |
| 499 | module = ly_ctx_load_module(ctx, "nc-notifications", NULL); |
| 500 | assert_non_null(module); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 501 | |
| 502 | /* set RPC callbacks */ |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 503 | node = ly_ctx_get_node(module->ctx, NULL, "/ietf-netconf:get", 0); |
| 504 | assert_non_null(node); |
| 505 | lys_set_private(node, my_get_rpc_clb); |
| 506 | |
| 507 | node = ly_ctx_get_node(module->ctx, NULL, "/ietf-netconf:get-config", 0); |
| 508 | assert_non_null(node); |
| 509 | lys_set_private(node, my_getconfig_rpc_clb); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 510 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 511 | node = ly_ctx_get_node(module->ctx, NULL, "/ietf-netconf:commit", 0); |
| 512 | assert_non_null(node); |
| 513 | lys_set_private(node, my_commit_rpc_clb); |
| 514 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 515 | nc_server_init(ctx); |
| 516 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 517 | const struct CMUnitTest comm[] = { |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 518 | cmocka_unit_test_setup_teardown(test_send_recv_ok_10, setup_sessions, teardown_sessions), |
| 519 | cmocka_unit_test_setup_teardown(test_send_recv_error_10, setup_sessions, teardown_sessions), |
| 520 | cmocka_unit_test_setup_teardown(test_send_recv_data_10, setup_sessions, teardown_sessions), |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 521 | cmocka_unit_test_setup_teardown(test_send_recv_notif_10, setup_sessions, teardown_sessions), |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 522 | cmocka_unit_test_setup_teardown(test_send_recv_ok_11, setup_sessions, teardown_sessions), |
| 523 | cmocka_unit_test_setup_teardown(test_send_recv_error_11, setup_sessions, teardown_sessions), |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 524 | cmocka_unit_test_setup_teardown(test_send_recv_data_11, setup_sessions, teardown_sessions), |
| 525 | cmocka_unit_test_setup_teardown(test_send_recv_notif_11, setup_sessions, teardown_sessions), |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 526 | }; |
| 527 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 528 | ret = cmocka_run_group_tests(comm, NULL, NULL); |
| 529 | |
| 530 | nc_server_destroy(); |
| 531 | ly_ctx_destroy(ctx, NULL); |
| 532 | |
| 533 | return ret; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 534 | } |