Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 7227f35 | 2016-02-01 12:11:40 +0100 | [diff] [blame] | 2 | * \file test_fd_comm.c |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 7227f35 | 2016-02-01 12:11:40 +0100 | [diff] [blame] | 4 | * \brief libnetconf2 tests - file descriptor basic RPC communication |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | ad1cc6a | 2016-02-26 15:06:06 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <pthread.h> |
| 18 | #include <setjmp.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | |
| 29 | #include <cmocka.h> |
| 30 | #include <libyang/libyang.h> |
| 31 | |
| 32 | #include <session_client.h> |
| 33 | #include <session_server.h> |
| 34 | #include <session_p.h> |
| 35 | #include <messages_p.h> |
Jan Kundrát | cf15d6c | 2017-10-26 18:07:56 +0200 | [diff] [blame] | 36 | #include "tests/config.h" |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 37 | |
| 38 | struct nc_session *server_session; |
| 39 | struct nc_session *client_session; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 40 | struct ly_ctx *ctx; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 41 | |
| 42 | struct nc_server_reply * |
| 43 | my_get_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 44 | { |
| 45 | assert_string_equal(rpc->schema->name, "get"); |
| 46 | assert_ptr_equal(session, server_session); |
| 47 | |
| 48 | return nc_server_reply_ok(); |
| 49 | } |
| 50 | |
| 51 | struct nc_server_reply * |
| 52 | my_getconfig_rpc_clb(struct lyd_node *rpc, struct nc_session *session) |
| 53 | { |
| 54 | struct lyd_node *data; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 55 | |
| 56 | assert_string_equal(rpc->schema->name, "get-config"); |
| 57 | assert_ptr_equal(session, server_session); |
| 58 | |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 59 | data = lyd_new_path(NULL, session->ctx, "/ietf-netconf:get-config/data", NULL, LYD_ANYDATA_CONSTSTRING, |
| 60 | LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 61 | assert_non_null(data); |
| 62 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 63 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int |
| 67 | setup_sessions(void **state) |
| 68 | { |
| 69 | (void)state; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 70 | int sock[2]; |
| 71 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 72 | /* create communication channel */ |
| 73 | socketpair(AF_UNIX, SOCK_STREAM, 0, sock); |
| 74 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 75 | /* create server session */ |
| 76 | server_session = calloc(1, sizeof *server_session); |
| 77 | server_session->status = NC_STATUS_RUNNING; |
| 78 | server_session->side = NC_SERVER; |
| 79 | server_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 80 | server_session->ti_type = NC_TI_FD; |
| 81 | server_session->ti_lock = malloc(sizeof *server_session->ti_lock); |
| 82 | pthread_mutex_init(server_session->ti_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 83 | server_session->ti_cond = malloc(sizeof *server_session->ti_cond); |
| 84 | pthread_cond_init(server_session->ti_cond, NULL); |
| 85 | server_session->ti_inuse = malloc(sizeof *server_session->ti_inuse); |
| 86 | *server_session->ti_inuse = 0; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 87 | server_session->ti.fd.in = sock[0]; |
| 88 | server_session->ti.fd.out = sock[0]; |
| 89 | server_session->ctx = ctx; |
| 90 | server_session->flags = NC_SESSION_SHAREDCTX; |
| 91 | |
| 92 | /* create client session */ |
| 93 | client_session = calloc(1, sizeof *server_session); |
| 94 | client_session->status = NC_STATUS_RUNNING; |
| 95 | client_session->side = NC_CLIENT; |
| 96 | client_session->id = 1; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 97 | client_session->ti_type = NC_TI_FD; |
| 98 | client_session->ti_lock = malloc(sizeof *client_session->ti_lock); |
| 99 | pthread_mutex_init(client_session->ti_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 100 | client_session->ti_cond = malloc(sizeof *client_session->ti_cond); |
| 101 | pthread_cond_init(client_session->ti_cond, NULL); |
| 102 | client_session->ti_inuse = malloc(sizeof *client_session->ti_inuse); |
| 103 | *client_session->ti_inuse = 0; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 104 | client_session->ti.fd.in = sock[1]; |
| 105 | client_session->ti.fd.out = sock[1]; |
| 106 | client_session->ctx = ctx; |
| 107 | client_session->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 108 | client_session->opts.client.msgid = 50; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int |
| 114 | teardown_sessions(void **state) |
| 115 | { |
| 116 | (void)state; |
| 117 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 118 | close(server_session->ti.fd.in); |
| 119 | pthread_mutex_destroy(server_session->ti_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 120 | pthread_cond_destroy(server_session->ti_cond); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 121 | free(server_session->ti_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 122 | free(server_session->ti_cond); |
| 123 | free((int *)server_session->ti_inuse); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 124 | free(server_session); |
| 125 | |
| 126 | close(client_session->ti.fd.in); |
| 127 | pthread_mutex_destroy(client_session->ti_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 128 | pthread_cond_destroy(client_session->ti_cond); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 129 | free(client_session->ti_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 130 | free(client_session->ti_cond); |
| 131 | free((int *)client_session->ti_inuse); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 132 | free(client_session); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 138 | test_send_recv_ok(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 139 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 140 | int ret; |
| 141 | uint64_t msgid; |
| 142 | NC_MSG_TYPE msgtype; |
| 143 | struct nc_rpc *rpc; |
| 144 | struct nc_reply *reply; |
| 145 | struct nc_pollsession *ps; |
| 146 | |
| 147 | /* client RPC */ |
| 148 | rpc = nc_rpc_get(NULL, 0, 0); |
| 149 | assert_non_null(rpc); |
| 150 | |
| 151 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 152 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 153 | |
| 154 | /* server RPC, send reply */ |
| 155 | ps = nc_ps_new(); |
| 156 | assert_non_null(ps); |
| 157 | nc_ps_add_session(ps, server_session); |
| 158 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 159 | ret = nc_ps_poll(ps, 0, NULL); |
| 160 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 161 | |
| 162 | /* server finished */ |
| 163 | nc_ps_free(ps); |
| 164 | |
| 165 | /* client reply */ |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 166 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 167 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 168 | |
| 169 | nc_rpc_free(rpc); |
| 170 | assert_int_equal(reply->type, NC_RPL_OK); |
| 171 | nc_reply_free(reply); |
| 172 | } |
| 173 | |
| 174 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 175 | test_send_recv_ok_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 176 | { |
| 177 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 178 | |
| 179 | server_session->version = NC_VERSION_10; |
| 180 | client_session->version = NC_VERSION_10; |
| 181 | |
| 182 | test_send_recv_ok(); |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | test_send_recv_ok_11(void **state) |
| 187 | { |
| 188 | (void)state; |
| 189 | |
| 190 | server_session->version = NC_VERSION_11; |
| 191 | client_session->version = NC_VERSION_11; |
| 192 | |
| 193 | test_send_recv_ok(); |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | test_send_recv_error(void) |
| 198 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 199 | int ret; |
| 200 | uint64_t msgid; |
| 201 | NC_MSG_TYPE msgtype; |
| 202 | struct nc_rpc *rpc; |
| 203 | struct nc_reply *reply; |
| 204 | struct nc_pollsession *ps; |
| 205 | |
| 206 | /* client RPC */ |
| 207 | rpc = nc_rpc_kill(1); |
| 208 | assert_non_null(rpc); |
| 209 | |
| 210 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 211 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 212 | |
| 213 | /* server RPC, send reply */ |
| 214 | ps = nc_ps_new(); |
| 215 | assert_non_null(ps); |
| 216 | nc_ps_add_session(ps, server_session); |
| 217 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 218 | ret = nc_ps_poll(ps, 0, NULL); |
| 219 | assert_int_equal(ret, NC_PSPOLL_RPC | NC_PSPOLL_REPLY_ERROR); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 220 | |
| 221 | /* server finished */ |
| 222 | nc_ps_free(ps); |
| 223 | |
| 224 | /* client reply */ |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 225 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 226 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 227 | |
| 228 | nc_rpc_free(rpc); |
| 229 | assert_int_equal(reply->type, NC_RPL_ERROR); |
| 230 | assert_string_equal(((struct nc_reply_error *)reply)->err->tag, "operation-not-supported"); |
| 231 | nc_reply_free(reply); |
| 232 | } |
| 233 | |
| 234 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 235 | test_send_recv_error_10(void **state) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 236 | { |
| 237 | (void)state; |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 238 | |
| 239 | server_session->version = NC_VERSION_10; |
| 240 | client_session->version = NC_VERSION_10; |
| 241 | |
| 242 | test_send_recv_error(); |
| 243 | } |
| 244 | |
| 245 | static void |
| 246 | test_send_recv_error_11(void **state) |
| 247 | { |
| 248 | (void)state; |
| 249 | |
| 250 | server_session->version = NC_VERSION_11; |
| 251 | client_session->version = NC_VERSION_11; |
| 252 | |
| 253 | test_send_recv_error(); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | test_send_recv_data(void) |
| 258 | { |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 259 | int ret; |
| 260 | uint64_t msgid; |
| 261 | NC_MSG_TYPE msgtype; |
| 262 | struct nc_rpc *rpc; |
| 263 | struct nc_reply *reply; |
| 264 | struct nc_pollsession *ps; |
| 265 | |
| 266 | /* client RPC */ |
| 267 | rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, NULL, 0, 0); |
| 268 | assert_non_null(rpc); |
| 269 | |
| 270 | msgtype = nc_send_rpc(client_session, rpc, 0, &msgid); |
| 271 | assert_int_equal(msgtype, NC_MSG_RPC); |
| 272 | |
| 273 | /* server RPC, send reply */ |
| 274 | ps = nc_ps_new(); |
| 275 | assert_non_null(ps); |
| 276 | nc_ps_add_session(ps, server_session); |
| 277 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 278 | ret = nc_ps_poll(ps, 0, NULL); |
| 279 | assert_int_equal(ret, NC_PSPOLL_RPC); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 280 | |
| 281 | /* server finished */ |
| 282 | nc_ps_free(ps); |
| 283 | |
| 284 | /* client reply */ |
Radek Krejci | 8c8e124 | 2016-09-07 16:43:54 +0200 | [diff] [blame] | 285 | msgtype = nc_recv_reply(client_session, rpc, msgid, 0, 0, &reply); |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 286 | assert_int_equal(msgtype, NC_MSG_REPLY); |
| 287 | |
| 288 | nc_rpc_free(rpc); |
| 289 | assert_int_equal(reply->type, NC_RPL_DATA); |
| 290 | nc_reply_free(reply); |
| 291 | } |
| 292 | |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 293 | static void |
| 294 | test_send_recv_data_10(void **state) |
| 295 | { |
| 296 | (void)state; |
| 297 | |
| 298 | server_session->version = NC_VERSION_10; |
| 299 | client_session->version = NC_VERSION_10; |
| 300 | |
| 301 | test_send_recv_data(); |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | test_send_recv_data_11(void **state) |
| 306 | { |
| 307 | (void)state; |
| 308 | |
| 309 | server_session->version = NC_VERSION_11; |
| 310 | client_session->version = NC_VERSION_11; |
| 311 | |
| 312 | test_send_recv_data(); |
| 313 | } |
| 314 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 315 | /* TODO |
| 316 | static void |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 317 | test_send_recv_notif(void) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 318 | { |
| 319 | |
| 320 | }*/ |
| 321 | |
| 322 | int |
| 323 | main(void) |
| 324 | { |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 325 | int ret; |
| 326 | const struct lys_module *module; |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 327 | const struct lys_node *node; |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 328 | |
| 329 | /* create ctx */ |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 330 | ctx = ly_ctx_new(TESTS_DIR"../schemas", 0); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 331 | assert_non_null(ctx); |
| 332 | |
| 333 | /* load modules */ |
| 334 | module = ly_ctx_load_module(ctx, "ietf-netconf-acm", NULL); |
| 335 | assert_non_null(module); |
| 336 | |
| 337 | module = ly_ctx_load_module(ctx, "ietf-netconf", NULL); |
| 338 | assert_non_null(module); |
| 339 | |
| 340 | /* set RPC callbacks */ |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 341 | node = ly_ctx_get_node(module->ctx, NULL, "/ietf-netconf:get", 0); |
| 342 | assert_non_null(node); |
| 343 | lys_set_private(node, my_get_rpc_clb); |
| 344 | |
| 345 | node = ly_ctx_get_node(module->ctx, NULL, "/ietf-netconf:get-config", 0); |
| 346 | assert_non_null(node); |
| 347 | lys_set_private(node, my_getconfig_rpc_clb); |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 348 | |
| 349 | nc_server_init(ctx); |
| 350 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 351 | const struct CMUnitTest comm[] = { |
Michal Vasko | 58d152f | 2016-02-01 11:44:49 +0100 | [diff] [blame] | 352 | cmocka_unit_test_setup_teardown(test_send_recv_ok_10, setup_sessions, teardown_sessions), |
| 353 | cmocka_unit_test_setup_teardown(test_send_recv_error_10, setup_sessions, teardown_sessions), |
| 354 | cmocka_unit_test_setup_teardown(test_send_recv_data_10, setup_sessions, teardown_sessions), |
| 355 | cmocka_unit_test_setup_teardown(test_send_recv_ok_11, setup_sessions, teardown_sessions), |
| 356 | cmocka_unit_test_setup_teardown(test_send_recv_error_11, setup_sessions, teardown_sessions), |
| 357 | cmocka_unit_test_setup_teardown(test_send_recv_data_11, setup_sessions, teardown_sessions) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 358 | }; |
| 359 | |
Michal Vasko | 8c5518b | 2016-03-01 12:35:13 +0100 | [diff] [blame] | 360 | ret = cmocka_run_group_tests(comm, NULL, NULL); |
| 361 | |
| 362 | nc_server_destroy(); |
| 363 | ly_ctx_destroy(ctx, NULL); |
| 364 | |
| 365 | return ret; |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 366 | } |