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> |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 23 | #include <string.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame^] | 24 | #include <sys/socket.h> |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame^] | 27 | #include <unistd.h> |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 28 | |
| 29 | #include <cmocka.h> |
| 30 | #include <libyang/libyang.h> |
| 31 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 32 | #include <messages_p.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame^] | 33 | #include <session_client.h> |
| 34 | #include <session_p.h> |
| 35 | #include <session_server.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; |
Michal Vasko | 49eb3f4 | 2021-05-19 10:20:57 +0200 | [diff] [blame] | 375 | struct timespec ts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 376 | char *buf; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame^] | 377 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 378 | (void)arg; |
| 379 | |
| 380 | /* wait for the RPC callback to be called */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 381 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 382 | while (glob_state != 1) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 383 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 384 | usleep(1000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 385 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* create notif */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 389 | lyd_new_path(NULL, ctx, "/nc-notifications:notificationComplete", NULL, 0, ¬if_tree); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 390 | assert_non_null(notif_tree); |
Michal Vasko | 49eb3f4 | 2021-05-19 10:20:57 +0200 | [diff] [blame] | 391 | clock_gettime(CLOCK_REALTIME, &ts); |
Michal Vasko | d7fb6df | 2021-05-19 11:27:35 +0200 | [diff] [blame] | 392 | ly_time_ts2str(&ts, &buf); |
| 393 | notif = nc_server_notif_new(notif_tree, buf, NC_PARAMTYPE_FREE); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 394 | assert_non_null(notif); |
| 395 | |
| 396 | /* send notif */ |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 397 | nc_session_inc_notif_status(server_session); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 398 | 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 Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 404 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 405 | |
| 406 | return NULL; |
| 407 | } |
| 408 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 409 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 410 | test_send_recv_notif(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 411 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 412 | int ret; |
| 413 | pthread_t tid; |
| 414 | uint64_t msgid; |
| 415 | NC_MSG_TYPE msgtype; |
| 416 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 417 | struct lyd_node *envp, *op; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 418 | struct nc_pollsession *ps; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 419 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 420 | /* 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 Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 437 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 438 | glob_state = 0; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 439 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 440 | 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 Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 448 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 449 | assert_int_equal(glob_state, 3); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 450 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 451 | |
| 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 Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 458 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 459 | assert_int_equal(msgtype, NC_MSG_REPLY); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 460 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 461 | |
| 462 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 463 | lyd_free_tree(envp); |
| 464 | assert_null(op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void |
| 468 | test_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 | |
| 478 | static void |
| 479 | test_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 Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 488 | |
| 489 | int |
| 490 | main(void) |
| 491 | { |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 492 | int ret; |
| 493 | const struct lys_module *module; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 494 | struct lysc_node *node; |
| 495 | const char *nc_features[] = {"candidate", NULL}; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 496 | |
| 497 | /* create ctx */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame^] | 498 | ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 499 | assert_non_null(ctx); |
| 500 | |
| 501 | /* load modules */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 502 | module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL, NULL); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 503 | assert_non_null(module); |
| 504 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 505 | module = ly_ctx_load_module(ctx, "ietf-netconf", NULL, nc_features); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 506 | assert_non_null(module); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 507 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 508 | module = ly_ctx_load_module(ctx, "nc-notifications", NULL, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 509 | assert_non_null(module); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 510 | |
| 511 | /* set RPC callbacks */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 512 | 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] | 513 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 514 | node->priv = my_get_rpc_clb; |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 515 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 516 | 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] | 517 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 518 | node->priv = my_getconfig_rpc_clb; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 519 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 520 | 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] | 521 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 522 | node->priv = my_commit_rpc_clb; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 523 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 524 | nc_server_init(ctx); |
| 525 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 526 | const struct CMUnitTest comm[] = { |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 527 | 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 Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 530 | 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] | 531 | 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 Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 533 | 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 Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 535 | }; |
| 536 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 537 | ret = cmocka_run_group_tests(comm, NULL, NULL); |
| 538 | |
| 539 | nc_server_destroy(); |
Michal Vasko | 1dcb764 | 2021-04-14 15:28:23 +0200 | [diff] [blame] | 540 | ly_ctx_destroy(ctx); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 541 | |
| 542 | return ret; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 543 | } |