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 | |
Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <pthread.h> |
| 20 | #include <setjmp.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 25 | #include <string.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 26 | #include <sys/socket.h> |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 29 | #include <unistd.h> |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 30 | |
| 31 | #include <cmocka.h> |
| 32 | #include <libyang/libyang.h> |
| 33 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 34 | #include <messages_p.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 35 | #include <session_client.h> |
| 36 | #include <session_p.h> |
| 37 | #include <session_server.h> |
Jan Kundrát | cf15d6c | 2017-10-26 18:07:56 +0200 | [diff] [blame] | 38 | #include "tests/config.h" |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 39 | |
| 40 | struct nc_session *server_session; |
| 41 | struct nc_session *client_session; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 42 | struct ly_ctx *ctx; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 43 | pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 44 | pthread_barrier_t barrier; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 45 | int glob_state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 46 | |
| 47 | struct nc_server_reply * |
| 48 | my_get_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 49 | { |
| 50 | assert_string_equal(rpc->schema->name, "get"); |
| 51 | assert_ptr_equal(session, server_session); |
| 52 | |
| 53 | return nc_server_reply_ok(); |
| 54 | } |
| 55 | |
| 56 | struct nc_server_reply * |
| 57 | my_getconfig_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 58 | { |
| 59 | struct lyd_node *data; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 60 | |
| 61 | assert_string_equal(rpc->schema->name, "get-config"); |
| 62 | assert_ptr_equal(session, server_session); |
| 63 | |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 64 | lyd_new_path(NULL, session->ctx, "/ietf-netconf:get-config/data", NULL, LYD_NEW_VAL_OUTPUT, &data); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 65 | assert_non_null(data); |
| 66 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 67 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 70 | struct nc_server_reply * |
| 71 | my_commit_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 72 | { |
| 73 | assert_string_equal(rpc->schema->name, "commit"); |
| 74 | assert_ptr_equal(session, server_session); |
| 75 | |
| 76 | /* update state */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 77 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 78 | glob_state = 1; |
| 79 | |
| 80 | /* wait until the client receives the notification */ |
| 81 | while (glob_state != 3) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 82 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 83 | usleep(100000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 84 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 85 | } |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 86 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 87 | |
| 88 | return nc_server_reply_ok(); |
| 89 | } |
| 90 | |
| 91 | static struct nc_session * |
| 92 | test_new_session(NC_SIDE side) |
| 93 | { |
| 94 | struct nc_session *sess; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 95 | struct timespec ts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 96 | |
| 97 | sess = calloc(1, sizeof *sess); |
| 98 | if (!sess) { |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | sess->side = side; |
| 103 | |
| 104 | if (side == NC_SERVER) { |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 105 | pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 106 | pthread_mutex_init(&sess->opts.server.rpc_lock, NULL); |
| 107 | pthread_cond_init(&sess->opts.server.rpc_cond, NULL); |
| 108 | sess->opts.server.rpc_inuse = 0; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 109 | |
| 110 | nc_timeouttime_get(&ts, 0); |
| 111 | sess->opts.server.last_rpc = ts.tv_sec; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | sess->io_lock = malloc(sizeof *sess->io_lock); |
| 115 | if (!sess->io_lock) { |
| 116 | goto error; |
| 117 | } |
| 118 | pthread_mutex_init(sess->io_lock, NULL); |
| 119 | |
| 120 | return sess; |
| 121 | |
| 122 | error: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 123 | free(sess); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 127 | static int |
| 128 | setup_sessions(void **state) |
| 129 | { |
| 130 | (void)state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 131 | int sock[2]; |
| 132 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 133 | /* create communication channel */ |
| 134 | socketpair(AF_UNIX, SOCK_STREAM, 0, sock); |
| 135 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 136 | /* create server session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 137 | server_session = test_new_session(NC_SERVER); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 138 | server_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 139 | server_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 140 | server_session->ti_type = NC_TI_FD; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 141 | server_session->ti.fd.in = sock[0]; |
| 142 | server_session->ti.fd.out = sock[0]; |
| 143 | server_session->ctx = ctx; |
| 144 | server_session->flags = NC_SESSION_SHAREDCTX; |
| 145 | |
| 146 | /* create client session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 147 | client_session = test_new_session(NC_CLIENT); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 148 | client_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 149 | client_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 150 | client_session->ti_type = NC_TI_FD; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 151 | client_session->ti.fd.in = sock[1]; |
| 152 | client_session->ti.fd.out = sock[1]; |
| 153 | client_session->ctx = ctx; |
| 154 | client_session->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 155 | client_session->opts.client.msgid = 50; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int |
| 161 | teardown_sessions(void **state) |
| 162 | { |
| 163 | (void)state; |
| 164 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 165 | close(server_session->ti.fd.in); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 166 | server_session->ti.fd.in = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 167 | nc_session_free(server_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 168 | |
| 169 | close(client_session->ti.fd.in); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 170 | client_session->ti.fd.in = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 171 | nc_session_free(client_session, NULL); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 177 | test_send_recv_ok(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 178 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 179 | int ret; |
| 180 | uint64_t msgid; |
| 181 | NC_MSG_TYPE msgtype; |
| 182 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 183 | struct lyd_node *envp, *op; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 184 | struct nc_pollsession *ps; |
| 185 | |
| 186 | /* client RPC */ |
| 187 | rpc = nc_rpc_get(NULL, 0, 0); |
| 188 | assert_non_null(rpc); |
| 189 | |
| 190 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 191 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 192 | |
| 193 | /* server RPC, send reply */ |
| 194 | ps = nc_ps_new(); |
| 195 | assert_non_null(ps); |
| 196 | nc_ps_add_session(ps, server_session); |
| 197 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 198 | ret = nc_ps_poll(ps, 0, NULL); |
| 199 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 200 | |
| 201 | /* server finished */ |
| 202 | nc_ps_free(ps); |
| 203 | |
| 204 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 205 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 206 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 207 | |
| 208 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 209 | assert_null(op); |
| 210 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 211 | lyd_free_tree(envp); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 215 | test_send_recv_ok_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 216 | { |
| 217 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 218 | |
| 219 | server_session->version = NC_VERSION_10; |
| 220 | client_session->version = NC_VERSION_10; |
| 221 | |
| 222 | test_send_recv_ok(); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | test_send_recv_ok_11(void **state) |
| 227 | { |
| 228 | (void)state; |
| 229 | |
| 230 | server_session->version = NC_VERSION_11; |
| 231 | client_session->version = NC_VERSION_11; |
| 232 | |
| 233 | test_send_recv_ok(); |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | test_send_recv_error(void) |
| 238 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 239 | int ret; |
| 240 | uint64_t msgid; |
| 241 | NC_MSG_TYPE msgtype; |
| 242 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 243 | struct lyd_node *envp, *op, *node; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 244 | struct nc_pollsession *ps; |
| 245 | |
| 246 | /* client RPC */ |
| 247 | rpc = nc_rpc_kill(1); |
| 248 | assert_non_null(rpc); |
| 249 | |
| 250 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 251 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 252 | |
| 253 | /* server RPC, send reply */ |
| 254 | ps = nc_ps_new(); |
| 255 | assert_non_null(ps); |
| 256 | nc_ps_add_session(ps, server_session); |
| 257 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 258 | ret = nc_ps_poll(ps, 0, NULL); |
| 259 | assert_int_equal(ret, NC_PSPOLL_RPC | NC_PSPOLL_REPLY_ERROR); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 260 | |
| 261 | /* server finished */ |
| 262 | nc_ps_free(ps); |
| 263 | |
| 264 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 265 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 266 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 267 | |
| 268 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 269 | assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error"); |
| 270 | lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node); |
| 271 | assert_non_null(node); |
| 272 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "operation-not-supported"); |
| 273 | lyd_free_tree(envp); |
| 274 | assert_null(op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 278 | test_send_recv_error_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 279 | { |
| 280 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 281 | |
| 282 | server_session->version = NC_VERSION_10; |
| 283 | client_session->version = NC_VERSION_10; |
| 284 | |
| 285 | test_send_recv_error(); |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | test_send_recv_error_11(void **state) |
| 290 | { |
| 291 | (void)state; |
| 292 | |
| 293 | server_session->version = NC_VERSION_11; |
| 294 | client_session->version = NC_VERSION_11; |
| 295 | |
| 296 | test_send_recv_error(); |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | test_send_recv_data(void) |
| 301 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 302 | int ret; |
| 303 | uint64_t msgid; |
| 304 | NC_MSG_TYPE msgtype; |
| 305 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 306 | struct lyd_node *envp, *op; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 307 | struct nc_pollsession *ps; |
| 308 | |
| 309 | /* client RPC */ |
| 310 | rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, NULL, 0, 0); |
| 311 | assert_non_null(rpc); |
| 312 | |
| 313 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 314 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 315 | |
| 316 | /* server RPC, send reply */ |
| 317 | ps = nc_ps_new(); |
| 318 | assert_non_null(ps); |
| 319 | nc_ps_add_session(ps, server_session); |
| 320 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 321 | ret = nc_ps_poll(ps, 0, NULL); |
| 322 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 323 | |
| 324 | /* server finished */ |
| 325 | nc_ps_free(ps); |
| 326 | |
| 327 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 328 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 329 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 330 | |
| 331 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 332 | assert_non_null(envp); |
| 333 | lyd_free_tree(envp); |
| 334 | assert_non_null(op); |
| 335 | lyd_free_tree(op); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 338 | static void |
| 339 | test_send_recv_data_10(void **state) |
| 340 | { |
| 341 | (void)state; |
| 342 | |
| 343 | server_session->version = NC_VERSION_10; |
| 344 | client_session->version = NC_VERSION_10; |
| 345 | |
| 346 | test_send_recv_data(); |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | test_send_recv_data_11(void **state) |
| 351 | { |
| 352 | (void)state; |
| 353 | |
| 354 | server_session->version = NC_VERSION_11; |
| 355 | client_session->version = NC_VERSION_11; |
| 356 | |
| 357 | test_send_recv_data(); |
| 358 | } |
| 359 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 360 | static void * |
| 361 | server_send_notif_thread(void *arg) |
| 362 | { |
| 363 | NC_MSG_TYPE msg_type; |
| 364 | struct lyd_node *notif_tree; |
| 365 | struct nc_server_notif *notif; |
Michal Vasko | 49eb3f4 | 2021-05-19 10:20:57 +0200 | [diff] [blame] | 366 | struct timespec ts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 367 | char *buf; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 368 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 369 | (void)arg; |
| 370 | |
| 371 | /* wait for the RPC callback to be called */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 372 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 373 | while (glob_state != 1) { |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 374 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 375 | usleep(1000); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 376 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* create notif */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 380 | lyd_new_path(NULL, ctx, "/nc-notifications:notificationComplete", NULL, 0, ¬if_tree); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 381 | assert_non_null(notif_tree); |
Michal Vasko | 49eb3f4 | 2021-05-19 10:20:57 +0200 | [diff] [blame] | 382 | clock_gettime(CLOCK_REALTIME, &ts); |
Michal Vasko | d7fb6df | 2021-05-19 11:27:35 +0200 | [diff] [blame] | 383 | ly_time_ts2str(&ts, &buf); |
| 384 | notif = nc_server_notif_new(notif_tree, buf, NC_PARAMTYPE_FREE); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 385 | assert_non_null(notif); |
| 386 | |
| 387 | /* send notif */ |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 388 | nc_session_inc_notif_status(server_session); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 389 | msg_type = nc_server_notif_send(server_session, notif, 100); |
| 390 | nc_server_notif_free(notif); |
| 391 | assert_int_equal(msg_type, NC_MSG_NOTIF); |
| 392 | |
| 393 | /* update state */ |
| 394 | glob_state = 2; |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 395 | pthread_barrier_wait(&barrier); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 396 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 397 | |
| 398 | return NULL; |
| 399 | } |
| 400 | |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 401 | static void * |
| 402 | thread_recv_notif(void *arg) |
| 403 | { |
| 404 | struct nc_session *session = (struct nc_session *)arg; |
| 405 | struct lyd_node *envp; |
| 406 | struct lyd_node *op; |
| 407 | NC_MSG_TYPE msgtype; |
| 408 | |
| 409 | pthread_barrier_wait(&barrier); |
| 410 | msgtype = nc_recv_notif(session, 1000, &envp, &op); |
| 411 | assert_int_equal(msgtype, NC_MSG_NOTIF); |
| 412 | assert_string_equal(op->schema->name, "notificationComplete"); |
| 413 | |
| 414 | lyd_free_tree(envp); |
| 415 | lyd_free_tree(op); |
| 416 | |
| 417 | pthread_mutex_lock(&state_lock); |
| 418 | glob_state = 3; |
| 419 | pthread_mutex_unlock(&state_lock); |
| 420 | |
| 421 | return (void *)0; |
| 422 | } |
| 423 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 424 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 425 | test_send_recv_notif(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 426 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 427 | int ret; |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 428 | pthread_t tid[2]; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 429 | uint64_t msgid; |
| 430 | NC_MSG_TYPE msgtype; |
| 431 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 432 | struct lyd_node *envp, *op; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 433 | struct nc_pollsession *ps; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 434 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 435 | /* client RPC */ |
| 436 | rpc = nc_rpc_commit(0, 0, NULL, NULL, 0); |
| 437 | assert_non_null(rpc); |
| 438 | |
| 439 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 440 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 441 | |
| 442 | /* client subscription */ |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 443 | pthread_create(&tid[0], NULL, thread_recv_notif, client_session); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 444 | |
| 445 | /* create server */ |
| 446 | ps = nc_ps_new(); |
| 447 | assert_non_null(ps); |
| 448 | nc_ps_add_session(ps, server_session); |
| 449 | |
| 450 | /* server will send a notification */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 451 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 452 | glob_state = 0; |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 453 | pthread_mutex_unlock(&state_lock); |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 454 | ret = pthread_create(&tid[1], NULL, server_send_notif_thread, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 455 | assert_int_equal(ret, 0); |
| 456 | |
| 457 | /* server blocked on RPC */ |
| 458 | ret = nc_ps_poll(ps, 0, NULL); |
| 459 | assert_int_equal(ret, NC_PSPOLL_RPC); |
| 460 | |
| 461 | /* RPC, notification finished fine */ |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 462 | pthread_mutex_lock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 463 | assert_int_equal(glob_state, 3); |
Michal Vasko | bbaa9b2 | 2019-03-14 12:35:43 +0100 | [diff] [blame] | 464 | pthread_mutex_unlock(&state_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 465 | |
| 466 | /* server finished */ |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 467 | ret = 0; |
| 468 | ret |= pthread_join(tid[0], NULL); |
| 469 | ret |= pthread_join(tid[1], NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 470 | assert_int_equal(ret, 0); |
| 471 | nc_ps_free(ps); |
| 472 | |
| 473 | /* client reply */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 474 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, &envp, &op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 475 | assert_int_equal(msgtype, NC_MSG_REPLY); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 476 | nc_rpc_free(rpc); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 477 | |
| 478 | assert_string_equal(LYD_NAME(lyd_child(envp)), "ok"); |
| 479 | lyd_free_tree(envp); |
| 480 | assert_null(op); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void |
| 484 | test_send_recv_notif_10(void **state) |
| 485 | { |
| 486 | (void)state; |
| 487 | |
| 488 | server_session->version = NC_VERSION_10; |
| 489 | client_session->version = NC_VERSION_10; |
| 490 | |
| 491 | test_send_recv_notif(); |
| 492 | } |
| 493 | |
| 494 | static void |
| 495 | test_send_recv_notif_11(void **state) |
| 496 | { |
| 497 | (void)state; |
| 498 | |
| 499 | server_session->version = NC_VERSION_11; |
| 500 | client_session->version = NC_VERSION_11; |
| 501 | |
| 502 | test_send_recv_notif(); |
| 503 | } |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 504 | |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 505 | static void |
| 506 | test_send_recv_malformed_10(void **state) |
| 507 | { |
| 508 | int ret; |
| 509 | struct nc_pollsession *ps; |
| 510 | struct nc_rpc *rpc; |
| 511 | struct lyd_node *envp, *op, *node; |
| 512 | NC_MSG_TYPE msgtype; |
| 513 | const char *msg; |
| 514 | |
| 515 | (void)state; |
| 516 | |
| 517 | server_session->version = NC_VERSION_10; |
| 518 | client_session->version = NC_VERSION_10; |
| 519 | |
| 520 | /* write malformed message */ |
| 521 | msg = |
| 522 | "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" |
| 523 | " <nc:commit/>" |
| 524 | "</nc:rpc>" |
| 525 | "]]>]]>"; |
| 526 | assert_int_equal(write(client_session->ti.fd.out, msg, strlen(msg)), strlen(msg)); |
| 527 | rpc = nc_rpc_commit(0, 0, NULL, NULL, 0); |
| 528 | assert_non_null(rpc); |
| 529 | |
| 530 | /* server RPC, send reply */ |
| 531 | ps = nc_ps_new(); |
| 532 | assert_non_null(ps); |
| 533 | nc_ps_add_session(ps, server_session); |
| 534 | |
| 535 | ret = nc_ps_poll(ps, 0, NULL); |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 536 | assert_int_equal(ret, NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR); |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 537 | |
| 538 | /* server finished */ |
| 539 | nc_ps_free(ps); |
| 540 | |
| 541 | /* client reply */ |
| 542 | msgtype = nc_recv_reply(client_session, rpc, 0, 0, &envp, &op); |
| 543 | assert_int_equal(msgtype, NC_MSG_REPLY_ERR_MSGID); |
| 544 | |
| 545 | nc_rpc_free(rpc); |
| 546 | assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error"); |
| 547 | lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node); |
| 548 | assert_non_null(node); |
| 549 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "missing-attribute"); |
| 550 | lyd_free_tree(envp); |
| 551 | assert_null(op); |
| 552 | } |
| 553 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 554 | int |
| 555 | main(void) |
| 556 | { |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 557 | int ret; |
| 558 | const struct lys_module *module; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 559 | struct lysc_node *node; |
| 560 | const char *nc_features[] = {"candidate", NULL}; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 561 | |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 562 | pthread_barrier_init(&barrier, NULL, 2); |
| 563 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 564 | /* create ctx */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 565 | ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 566 | assert_non_null(ctx); |
| 567 | |
| 568 | /* load modules */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 569 | module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL, NULL); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 570 | assert_non_null(module); |
| 571 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 572 | module = ly_ctx_load_module(ctx, "ietf-netconf", NULL, nc_features); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 573 | assert_non_null(module); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 574 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 575 | module = ly_ctx_load_module(ctx, "nc-notifications", NULL, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 576 | assert_non_null(module); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 577 | |
| 578 | /* set RPC callbacks */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 579 | 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] | 580 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 581 | node->priv = my_get_rpc_clb; |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 582 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 583 | 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] | 584 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 585 | node->priv = my_getconfig_rpc_clb; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 586 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 587 | 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] | 588 | assert_non_null(node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 589 | node->priv = my_commit_rpc_clb; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 590 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 591 | nc_server_init(); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 592 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 593 | const struct CMUnitTest comm[] = { |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 594 | cmocka_unit_test_setup_teardown(test_send_recv_ok_10, setup_sessions, teardown_sessions), |
| 595 | cmocka_unit_test_setup_teardown(test_send_recv_error_10, setup_sessions, teardown_sessions), |
| 596 | 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] | 597 | 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] | 598 | 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] | 599 | cmocka_unit_test_setup_teardown(test_send_recv_ok_11, setup_sessions, teardown_sessions), |
| 600 | 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] | 601 | cmocka_unit_test_setup_teardown(test_send_recv_data_11, setup_sessions, teardown_sessions), |
| 602 | 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] | 603 | }; |
| 604 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 605 | ret = cmocka_run_group_tests(comm, NULL, NULL); |
| 606 | |
| 607 | nc_server_destroy(); |
Michal Vasko | 1dcb764 | 2021-04-14 15:28:23 +0200 | [diff] [blame] | 608 | ly_ctx_destroy(ctx); |
tadeas-vintrlik | 2e803db | 2021-08-25 12:12:07 +0200 | [diff] [blame] | 609 | pthread_barrier_destroy(&barrier); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 610 | |
| 611 | return ret; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 612 | } |