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