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; |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 42 | pthread_barrier_t barrier; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 43 | int glob_state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 44 | |
| 45 | struct nc_server_reply * |
| 46 | my_get_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 47 | { |
| 48 | assert_string_equal(rpc->schema->name, "get"); |
| 49 | assert_ptr_equal(session, server_session); |
| 50 | |
| 51 | return nc_server_reply_ok(); |
| 52 | } |
| 53 | |
| 54 | struct nc_server_reply * |
| 55 | my_getconfig_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 56 | { |
| 57 | struct lyd_node *data; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 58 | |
| 59 | assert_string_equal(rpc->schema->name, "get-config"); |
| 60 | assert_ptr_equal(session, server_session); |
| 61 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 62 | 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] | 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 | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 159 | server_session->ti.fd.in = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 160 | nc_session_free(server_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 161 | |
| 162 | close(client_session->ti.fd.in); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 163 | client_session->ti.fd.in = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 164 | nc_session_free(client_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 170 | test_send_recv_ok(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 171 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 172 | int ret; |
| 173 | uint64_t msgid; |
| 174 | NC_MSG_TYPE msgtype; |
| 175 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 176 | struct lyd_node *envp, *op; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 177 | struct nc_pollsession *ps; |
| 178 | |
| 179 | /* client RPC */ |
| 180 | rpc = nc_rpc_get(NULL, 0, 0); |
| 181 | assert_non_null(rpc); |
| 182 | |
| 183 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 184 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 185 | |
| 186 | /* server RPC, send reply */ |
| 187 | ps = nc_ps_new(); |
| 188 | assert_non_null(ps); |
| 189 | nc_ps_add_session(ps, server_session); |
| 190 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 191 | ret = nc_ps_poll(ps, 0, NULL); |
| 192 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 193 | |
| 194 | /* server finished */ |
| 195 | nc_ps_free(ps); |
| 196 | |
| 197 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 198 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 199 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 200 | |
| 201 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 202 | assert_null(op); |
| 203 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 204 | lyd_free_tree(envp); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 208 | test_send_recv_ok_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 209 | { |
| 210 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 211 | |
| 212 | server_session->version = NC_VERSION_10; |
| 213 | client_session->version = NC_VERSION_10; |
| 214 | |
| 215 | test_send_recv_ok(); |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | test_send_recv_ok_11(void **state) |
| 220 | { |
| 221 | (void)state; |
| 222 | |
| 223 | server_session->version = NC_VERSION_11; |
| 224 | client_session->version = NC_VERSION_11; |
| 225 | |
| 226 | test_send_recv_ok(); |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | test_send_recv_error(void) |
| 231 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 232 | int ret; |
| 233 | uint64_t msgid; |
| 234 | NC_MSG_TYPE msgtype; |
| 235 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 236 | struct lyd_node *envp, *op, *node; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 237 | struct nc_pollsession *ps; |
| 238 | |
| 239 | /* client RPC */ |
| 240 | rpc = nc_rpc_kill(1); |
| 241 | assert_non_null(rpc); |
| 242 | |
| 243 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 244 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 245 | |
| 246 | /* server RPC, send reply */ |
| 247 | ps = nc_ps_new(); |
| 248 | assert_non_null(ps); |
| 249 | nc_ps_add_session(ps, server_session); |
| 250 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 251 | ret = nc_ps_poll(ps, 0, NULL); |
| 252 | assert_int_equal(ret, NC_PSPOLL_RPC | NC_PSPOLL_REPLY_ERROR); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 253 | |
| 254 | /* server finished */ |
| 255 | nc_ps_free(ps); |
| 256 | |
| 257 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 258 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 259 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 260 | |
| 261 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 262 | assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error"); |
| 263 | lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node); |
| 264 | assert_non_null(node); |
| 265 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "operation-not-supported"); |
| 266 | lyd_free_tree(envp); |
| 267 | assert_null(op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 271 | test_send_recv_error_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 272 | { |
| 273 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 274 | |
| 275 | server_session->version = NC_VERSION_10; |
| 276 | client_session->version = NC_VERSION_10; |
| 277 | |
| 278 | test_send_recv_error(); |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | test_send_recv_error_11(void **state) |
| 283 | { |
| 284 | (void)state; |
| 285 | |
| 286 | server_session->version = NC_VERSION_11; |
| 287 | client_session->version = NC_VERSION_11; |
| 288 | |
| 289 | test_send_recv_error(); |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | test_send_recv_data(void) |
| 294 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 295 | int ret; |
| 296 | uint64_t msgid; |
| 297 | NC_MSG_TYPE msgtype; |
| 298 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 299 | struct lyd_node *envp, *op; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 300 | struct nc_pollsession *ps; |
| 301 | |
| 302 | /* client RPC */ |
| 303 | rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, NULL, 0, 0); |
| 304 | assert_non_null(rpc); |
| 305 | |
| 306 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 307 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 308 | |
| 309 | /* server RPC, send reply */ |
| 310 | ps = nc_ps_new(); |
| 311 | assert_non_null(ps); |
| 312 | nc_ps_add_session(ps, server_session); |
| 313 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 314 | ret = nc_ps_poll(ps, 0, NULL); |
| 315 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 316 | |
| 317 | /* server finished */ |
| 318 | nc_ps_free(ps); |
| 319 | |
| 320 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 321 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 322 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 323 | |
| 324 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 325 | assert_non_null(envp); |
| 326 | lyd_free_tree(envp); |
| 327 | assert_non_null(op); |
| 328 | lyd_free_tree(op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 331 | static void |
| 332 | test_send_recv_data_10(void **state) |
| 333 | { |
| 334 | (void)state; |
| 335 | |
| 336 | server_session->version = NC_VERSION_10; |
| 337 | client_session->version = NC_VERSION_10; |
| 338 | |
| 339 | test_send_recv_data(); |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | test_send_recv_data_11(void **state) |
| 344 | { |
| 345 | (void)state; |
| 346 | |
| 347 | server_session->version = NC_VERSION_11; |
| 348 | client_session->version = NC_VERSION_11; |
| 349 | |
| 350 | test_send_recv_data(); |
| 351 | } |
| 352 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 353 | static void * |
| 354 | server_send_notif_thread(void *arg) |
| 355 | { |
| 356 | NC_MSG_TYPE msg_type; |
| 357 | struct lyd_node *notif_tree; |
| 358 | struct nc_server_notif *notif; |
Michal Vasko | 49eb3f4 | 2021-05-19 10:20:57 +0200 | [diff] [blame] | 359 | struct timespec ts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 360 | char *buf; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 361 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 362 | (void)arg; |
| 363 | |
| 364 | /* wait for the RPC callback to be called */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 365 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 366 | while (glob_state != 1) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 367 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 368 | usleep(1000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 369 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* create notif */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 373 | lyd_new_path(NULL, ctx, "/nc-notifications:notificationComplete", NULL, 0, ¬if_tree); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 374 | assert_non_null(notif_tree); |
Michal Vasko | 49eb3f4 | 2021-05-19 10:20:57 +0200 | [diff] [blame] | 375 | clock_gettime(CLOCK_REALTIME, &ts); |
Michal Vasko | d7fb6df | 2021-05-19 11:27:35 +0200 | [diff] [blame] | 376 | ly_time_ts2str(&ts, &buf); |
| 377 | notif = nc_server_notif_new(notif_tree, buf, NC_PARAMTYPE_FREE); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 378 | assert_non_null(notif); |
| 379 | |
| 380 | /* send notif */ |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 381 | nc_session_inc_notif_status(server_session); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 382 | msg_type = nc_server_notif_send(server_session, notif, 100); |
| 383 | nc_server_notif_free(notif); |
| 384 | assert_int_equal(msg_type, NC_MSG_NOTIF); |
| 385 | |
| 386 | /* update state */ |
| 387 | glob_state = 2; |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 388 | pthread_barrier_wait(&barrier); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 389 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 390 | |
| 391 | return NULL; |
| 392 | } |
| 393 | |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 394 | static void * |
| 395 | thread_recv_notif(void *arg) |
| 396 | { |
| 397 | struct nc_session *session = (struct nc_session *)arg; |
| 398 | struct lyd_node *envp; |
| 399 | struct lyd_node *op; |
| 400 | NC_MSG_TYPE msgtype; |
| 401 | |
| 402 | pthread_barrier_wait(&barrier); |
| 403 | msgtype = nc_recv_notif(session, 1000, &envp, &op); |
| 404 | assert_int_equal(msgtype, NC_MSG_NOTIF); |
| 405 | assert_string_equal(op->schema->name, "notificationComplete"); |
| 406 | |
| 407 | lyd_free_tree(envp); |
| 408 | lyd_free_tree(op); |
| 409 | |
| 410 | pthread_mutex_lock(&state_lock); |
| 411 | glob_state = 3; |
| 412 | pthread_mutex_unlock(&state_lock); |
| 413 | |
| 414 | return (void *)0; |
| 415 | } |
| 416 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 417 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 418 | test_send_recv_notif(void) |
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 | int ret; |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 421 | pthread_t tid[2]; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 422 | uint64_t msgid; |
| 423 | NC_MSG_TYPE msgtype; |
| 424 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 425 | struct lyd_node *envp, *op; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 426 | struct nc_pollsession *ps; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 427 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 428 | /* client RPC */ |
| 429 | rpc = nc_rpc_commit(0, 0, NULL, NULL, 0); |
| 430 | assert_non_null(rpc); |
| 431 | |
| 432 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 433 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 434 | |
| 435 | /* client subscription */ |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 436 | pthread_create(&tid[0], NULL, thread_recv_notif, client_session); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 437 | |
| 438 | /* create server */ |
| 439 | ps = nc_ps_new(); |
| 440 | assert_non_null(ps); |
| 441 | nc_ps_add_session(ps, server_session); |
| 442 | |
| 443 | /* server will send a notification */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 444 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 445 | glob_state = 0; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 446 | pthread_mutex_unlock(&state_lock); |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 447 | ret = pthread_create(&tid[1], NULL, server_send_notif_thread, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 448 | assert_int_equal(ret, 0); |
| 449 | |
| 450 | /* server blocked on RPC */ |
| 451 | ret = nc_ps_poll(ps, 0, NULL); |
| 452 | assert_int_equal(ret, NC_PSPOLL_RPC); |
| 453 | |
| 454 | /* RPC, notification finished fine */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 455 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 456 | assert_int_equal(glob_state, 3); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 457 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 458 | |
| 459 | /* server finished */ |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 460 | ret = 0; |
| 461 | ret |= pthread_join(tid[0], NULL); |
| 462 | ret |= pthread_join(tid[1], NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 463 | assert_int_equal(ret, 0); |
| 464 | nc_ps_free(ps); |
| 465 | |
| 466 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 467 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 468 | assert_int_equal(msgtype, NC_MSG_REPLY); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 469 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 470 | |
| 471 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 472 | lyd_free_tree(envp); |
| 473 | assert_null(op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static void |
| 477 | test_send_recv_notif_10(void **state) |
| 478 | { |
| 479 | (void)state; |
| 480 | |
| 481 | server_session->version = NC_VERSION_10; |
| 482 | client_session->version = NC_VERSION_10; |
| 483 | |
| 484 | test_send_recv_notif(); |
| 485 | } |
| 486 | |
| 487 | static void |
| 488 | test_send_recv_notif_11(void **state) |
| 489 | { |
| 490 | (void)state; |
| 491 | |
| 492 | server_session->version = NC_VERSION_11; |
| 493 | client_session->version = NC_VERSION_11; |
| 494 | |
| 495 | test_send_recv_notif(); |
| 496 | } |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 497 | |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 498 | static void |
| 499 | test_send_recv_malformed_10(void **state) |
| 500 | { |
| 501 | int ret; |
| 502 | struct nc_pollsession *ps; |
| 503 | struct nc_rpc *rpc; |
| 504 | struct lyd_node *envp, *op, *node; |
| 505 | NC_MSG_TYPE msgtype; |
| 506 | const char *msg; |
| 507 | |
| 508 | (void)state; |
| 509 | |
| 510 | server_session->version = NC_VERSION_10; |
| 511 | client_session->version = NC_VERSION_10; |
| 512 | |
| 513 | /* write malformed message */ |
| 514 | msg = |
| 515 | "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" |
| 516 | " <nc:commit/>" |
| 517 | "</nc:rpc>" |
| 518 | "]]>]]>"; |
| 519 | assert_int_equal(write(client_session->ti.fd.out, msg, strlen(msg)), strlen(msg)); |
| 520 | rpc = nc_rpc_commit(0, 0, NULL, NULL, 0); |
| 521 | assert_non_null(rpc); |
| 522 | |
| 523 | /* server RPC, send reply */ |
| 524 | ps = nc_ps_new(); |
| 525 | assert_non_null(ps); |
| 526 | nc_ps_add_session(ps, server_session); |
| 527 | |
| 528 | ret = nc_ps_poll(ps, 0, NULL); |
| 529 | assert_int_equal(ret, NC_PSPOLL_BAD_RPC); |
| 530 | |
| 531 | /* server finished */ |
| 532 | nc_ps_free(ps); |
| 533 | |
| 534 | /* client reply */ |
| 535 | msgtype = nc_recv_reply(client_session, rpc, 0, 0, &envp, &op); |
| 536 | assert_int_equal(msgtype, NC_MSG_REPLY_ERR_MSGID); |
| 537 | |
| 538 | nc_rpc_free(rpc); |
| 539 | assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error"); |
| 540 | lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node); |
| 541 | assert_non_null(node); |
| 542 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "missing-attribute"); |
| 543 | lyd_free_tree(envp); |
| 544 | assert_null(op); |
| 545 | } |
| 546 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 547 | int |
| 548 | main(void) |
| 549 | { |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 550 | int ret; |
| 551 | const struct lys_module *module; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 552 | struct lysc_node *node; |
| 553 | const char *nc_features[] = {"candidate", NULL}; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 554 | |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 555 | pthread_barrier_init(&barrier, NULL, 2); |
| 556 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 557 | /* create ctx */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 558 | ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 559 | assert_non_null(ctx); |
| 560 | |
| 561 | /* load modules */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 562 | module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL, NULL); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 563 | assert_non_null(module); |
| 564 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 565 | module = ly_ctx_load_module(ctx, "ietf-netconf", NULL, nc_features); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 566 | assert_non_null(module); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 567 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 568 | module = ly_ctx_load_module(ctx, "nc-notifications", NULL, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 569 | assert_non_null(module); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 570 | |
| 571 | /* set RPC callbacks */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 572 | 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] | 573 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 574 | node->priv = my_get_rpc_clb; |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 575 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 576 | 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] | 577 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 578 | node->priv = my_getconfig_rpc_clb; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 579 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 580 | 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] | 581 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 582 | node->priv = my_commit_rpc_clb; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 583 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 584 | nc_server_init(); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 585 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 586 | const struct CMUnitTest comm[] = { |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 587 | cmocka_unit_test_setup_teardown(test_send_recv_ok_10, setup_sessions, teardown_sessions), |
| 588 | cmocka_unit_test_setup_teardown(test_send_recv_error_10, setup_sessions, teardown_sessions), |
| 589 | 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] | 590 | cmocka_unit_test_setup_teardown(test_send_recv_notif_10, setup_sessions, teardown_sessions), |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 591 | cmocka_unit_test_setup_teardown(test_send_recv_malformed_10, setup_sessions, teardown_sessions), |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 592 | cmocka_unit_test_setup_teardown(test_send_recv_ok_11, setup_sessions, teardown_sessions), |
| 593 | 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] | 594 | cmocka_unit_test_setup_teardown(test_send_recv_data_11, setup_sessions, teardown_sessions), |
| 595 | 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] | 596 | }; |
| 597 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 598 | ret = cmocka_run_group_tests(comm, NULL, NULL); |
| 599 | |
| 600 | nc_server_destroy(); |
Michal Vasko | 1dcb764 | 2021-04-14 15:28:23 +0200 | [diff] [blame] | 601 | ly_ctx_destroy(ctx); |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 602 | pthread_barrier_destroy(&barrier); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 603 | |
| 604 | return ret; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 605 | } |