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 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 61 | lyd_new_path(NULL, session->ctx, "/ietf-netconf:get-config/data", NULL, LYD_NEW_PATH_OUTPUT, &data); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 62 | assert_non_null(data); |
| 63 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 64 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 67 | struct nc_server_reply * |
| 68 | my_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 Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 74 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 75 | glob_state = 1; |
| 76 | |
| 77 | /* wait until the client receives the notification */ |
| 78 | while (glob_state != 3) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 79 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 80 | usleep(100000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 81 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 82 | } |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 83 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 84 | |
| 85 | return nc_server_reply_ok(); |
| 86 | } |
| 87 | |
| 88 | static struct nc_session * |
| 89 | test_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 Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 101 | 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 Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 104 | } |
| 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 | |
| 114 | error: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 115 | free(sess); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 119 | static int |
| 120 | setup_sessions(void **state) |
| 121 | { |
| 122 | (void)state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 123 | int sock[2]; |
| 124 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 125 | /* create communication channel */ |
| 126 | socketpair(AF_UNIX, SOCK_STREAM, 0, sock); |
| 127 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 128 | /* create server session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 129 | server_session = test_new_session(NC_SERVER); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 130 | server_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 131 | server_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 132 | server_session->ti_type = NC_TI_FD; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 133 | 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 Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 139 | client_session = test_new_session(NC_CLIENT); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 140 | client_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 141 | client_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 142 | client_session->ti_type = NC_TI_FD; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 143 | 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 Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 147 | client_session->opts.client.msgid = 50; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int |
| 153 | teardown_sessions(void **state) |
| 154 | { |
| 155 | (void)state; |
| 156 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 157 | close(server_session->ti.fd.in); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 158 | server_session->ti.fd.in = -1; |
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 | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 162 | client_session->ti.fd.in = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 163 | nc_session_free(client_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 169 | test_send_recv_ok(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 170 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 171 | int ret; |
| 172 | uint64_t msgid; |
| 173 | NC_MSG_TYPE msgtype; |
| 174 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 175 | struct lyd_node *envp, *op; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 176 | 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 Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 190 | ret = nc_ps_poll(ps, 0, NULL); |
| 191 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 192 | |
| 193 | /* server finished */ |
| 194 | nc_ps_free(ps); |
| 195 | |
| 196 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 197 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 198 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 199 | |
| 200 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 201 | assert_null(op); |
| 202 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 203 | lyd_free_tree(envp); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 207 | test_send_recv_ok_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 208 | { |
| 209 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 210 | |
| 211 | server_session->version = NC_VERSION_10; |
| 212 | client_session->version = NC_VERSION_10; |
| 213 | |
| 214 | test_send_recv_ok(); |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | test_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 | |
| 228 | static void |
| 229 | test_send_recv_error(void) |
| 230 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 231 | int ret; |
| 232 | uint64_t msgid; |
| 233 | NC_MSG_TYPE msgtype; |
| 234 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 235 | struct lyd_node *envp, *op, *node; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 236 | 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 Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 250 | ret = nc_ps_poll(ps, 0, NULL); |
| 251 | assert_int_equal(ret, NC_PSPOLL_RPC | NC_PSPOLL_REPLY_ERROR); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 252 | |
| 253 | /* server finished */ |
| 254 | nc_ps_free(ps); |
| 255 | |
| 256 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 257 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 258 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 259 | |
| 260 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 261 | 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 Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 270 | test_send_recv_error_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 271 | { |
| 272 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 273 | |
| 274 | server_session->version = NC_VERSION_10; |
| 275 | client_session->version = NC_VERSION_10; |
| 276 | |
| 277 | test_send_recv_error(); |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | test_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 | |
| 291 | static void |
| 292 | test_send_recv_data(void) |
| 293 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 294 | int ret; |
| 295 | uint64_t msgid; |
| 296 | NC_MSG_TYPE msgtype; |
| 297 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 298 | struct lyd_node *envp, *op; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 299 | 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 Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 313 | ret = nc_ps_poll(ps, 0, NULL); |
| 314 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 315 | |
| 316 | /* server finished */ |
| 317 | nc_ps_free(ps); |
| 318 | |
| 319 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 320 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 321 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 322 | |
| 323 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 324 | assert_non_null(envp); |
| 325 | lyd_free_tree(envp); |
| 326 | assert_non_null(op); |
| 327 | lyd_free_tree(op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 330 | static void |
| 331 | test_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 | |
| 341 | static void |
| 342 | test_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 Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 352 | static void |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 353 | test_notif_clb(struct nc_session *session, const struct lyd_node *UNUSED(envp), const struct lyd_node *op) |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 354 | { |
| 355 | assert_ptr_equal(session, client_session); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 356 | assert_string_equal(op->schema->name, "notificationComplete"); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 357 | |
| 358 | /* client notification received, update state */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 359 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 360 | while (glob_state != 2) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 361 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 362 | usleep(1000); |
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 | } |
| 365 | glob_state = 3; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 366 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void * |
| 370 | server_send_notif_thread(void *arg) |
| 371 | { |
| 372 | NC_MSG_TYPE msg_type; |
| 373 | struct lyd_node *notif_tree; |
| 374 | struct nc_server_notif *notif; |
| 375 | char *buf; |
| 376 | (void)arg; |
| 377 | |
| 378 | /* wait for the RPC callback to be called */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 379 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 380 | while (glob_state != 1) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 381 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 382 | usleep(1000); |
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 | } |
| 385 | |
| 386 | /* create notif */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 387 | lyd_new_path(NULL, ctx, "/nc-notifications:notificationComplete", NULL, 0, ¬if_tree); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 388 | assert_non_null(notif_tree); |
| 389 | buf = malloc(64); |
| 390 | assert_non_null(buf); |
| 391 | notif = nc_server_notif_new(notif_tree, nc_time2datetime(time(NULL), NULL, buf), NC_PARAMTYPE_FREE); |
| 392 | assert_non_null(notif); |
| 393 | |
| 394 | /* send notif */ |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame^] | 395 | nc_session_inc_notif_status(server_session); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 396 | msg_type = nc_server_notif_send(server_session, notif, 100); |
| 397 | nc_server_notif_free(notif); |
| 398 | assert_int_equal(msg_type, NC_MSG_NOTIF); |
| 399 | |
| 400 | /* update state */ |
| 401 | glob_state = 2; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 402 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 403 | |
| 404 | return NULL; |
| 405 | } |
| 406 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 407 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 408 | test_send_recv_notif(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 409 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 410 | int ret; |
| 411 | pthread_t tid; |
| 412 | uint64_t msgid; |
| 413 | NC_MSG_TYPE msgtype; |
| 414 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 415 | struct lyd_node *envp, *op; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 416 | struct nc_pollsession *ps; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 417 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 418 | /* client RPC */ |
| 419 | rpc = nc_rpc_commit(0, 0, NULL, NULL, 0); |
| 420 | assert_non_null(rpc); |
| 421 | |
| 422 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 423 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 424 | |
| 425 | /* client subscription */ |
| 426 | ret = nc_recv_notif_dispatch(client_session, test_notif_clb); |
| 427 | assert_int_equal(ret, 0); |
| 428 | |
| 429 | /* create server */ |
| 430 | ps = nc_ps_new(); |
| 431 | assert_non_null(ps); |
| 432 | nc_ps_add_session(ps, server_session); |
| 433 | |
| 434 | /* server will send a notification */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 435 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 436 | glob_state = 0; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 437 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 438 | ret = pthread_create(&tid, NULL, server_send_notif_thread, NULL); |
| 439 | assert_int_equal(ret, 0); |
| 440 | |
| 441 | /* server blocked on RPC */ |
| 442 | ret = nc_ps_poll(ps, 0, NULL); |
| 443 | assert_int_equal(ret, NC_PSPOLL_RPC); |
| 444 | |
| 445 | /* RPC, notification finished fine */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 446 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 447 | assert_int_equal(glob_state, 3); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 448 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 449 | |
| 450 | /* server finished */ |
| 451 | ret = pthread_join(tid, NULL); |
| 452 | assert_int_equal(ret, 0); |
| 453 | nc_ps_free(ps); |
| 454 | |
| 455 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 456 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 457 | assert_int_equal(msgtype, NC_MSG_REPLY); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 458 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 459 | |
| 460 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 461 | lyd_free_tree(envp); |
| 462 | assert_null(op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static void |
| 466 | test_send_recv_notif_10(void **state) |
| 467 | { |
| 468 | (void)state; |
| 469 | |
| 470 | server_session->version = NC_VERSION_10; |
| 471 | client_session->version = NC_VERSION_10; |
| 472 | |
| 473 | test_send_recv_notif(); |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | test_send_recv_notif_11(void **state) |
| 478 | { |
| 479 | (void)state; |
| 480 | |
| 481 | server_session->version = NC_VERSION_11; |
| 482 | client_session->version = NC_VERSION_11; |
| 483 | |
| 484 | test_send_recv_notif(); |
| 485 | } |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 486 | |
| 487 | int |
| 488 | main(void) |
| 489 | { |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 490 | int ret; |
| 491 | const struct lys_module *module; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 492 | struct lysc_node *node; |
| 493 | const char *nc_features[] = {"candidate", NULL}; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 494 | |
| 495 | /* create ctx */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 496 | ly_ctx_new(TESTS_DIR"/data/modules", 0, &ctx); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 497 | assert_non_null(ctx); |
| 498 | |
| 499 | /* load modules */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 500 | module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL, NULL); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 501 | assert_non_null(module); |
| 502 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 503 | module = ly_ctx_load_module(ctx, "ietf-netconf", NULL, nc_features); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 504 | assert_non_null(module); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 505 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 506 | module = ly_ctx_load_module(ctx, "nc-notifications", NULL, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 507 | assert_non_null(module); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 508 | |
| 509 | /* set RPC callbacks */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 510 | node = (struct lysc_node *)lys_find_path(module->ctx, NULL, "/ietf-netconf:get", 0); |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 511 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 512 | node->priv = my_get_rpc_clb; |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 513 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 514 | node = (struct lysc_node *)lys_find_path(module->ctx, NULL, "/ietf-netconf:get-config", 0); |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 515 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 516 | node->priv = my_getconfig_rpc_clb; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 517 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 518 | node = (struct lysc_node *)lys_find_path(module->ctx, NULL, "/ietf-netconf:commit", 0); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 519 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 520 | node->priv = my_commit_rpc_clb; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 521 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 522 | nc_server_init(ctx); |
| 523 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 524 | const struct CMUnitTest comm[] = { |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 525 | cmocka_unit_test_setup_teardown(test_send_recv_ok_10, setup_sessions, teardown_sessions), |
| 526 | cmocka_unit_test_setup_teardown(test_send_recv_error_10, setup_sessions, teardown_sessions), |
| 527 | 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] | 528 | 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] | 529 | cmocka_unit_test_setup_teardown(test_send_recv_ok_11, setup_sessions, teardown_sessions), |
| 530 | 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] | 531 | cmocka_unit_test_setup_teardown(test_send_recv_data_11, setup_sessions, teardown_sessions), |
| 532 | 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] | 533 | }; |
| 534 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 535 | ret = cmocka_run_group_tests(comm, NULL, NULL); |
| 536 | |
| 537 | nc_server_destroy(); |
| 538 | ly_ctx_destroy(ctx, NULL); |
| 539 | |
| 540 | return ret; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 541 | } |