Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file test_io.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 tests - input/output functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <pthread.h> |
| 26 | #include <setjmp.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <stddef.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | |
| 34 | #include <cmocka.h> |
| 35 | #include <libyang/libyang.h> |
| 36 | |
| 37 | #include <session_p.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 38 | #include <session_client.h> |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 39 | #include <messages_p.h> |
| 40 | #include "config.h" |
| 41 | |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 42 | /*static int |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 43 | setup_read(void **state) |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 44 | { |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 45 | int fd; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 46 | struct nc_session *session; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 47 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 48 | session = calloc(1, sizeof *session); |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 49 | * test IO with standard file descriptors * |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 50 | session->ti_type = NC_TI_FD; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 51 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 52 | session->status = NC_STATUS_RUNNING; |
| 53 | session->ctx = ly_ctx_new(TESTS_DIR"../schemas"); |
| 54 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 55 | pthread_mutex_init(session->ti_lock, NULL); |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 56 | |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 57 | * ietf-netconf * |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 58 | fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY); |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 59 | if (fd == -1) { |
| 60 | return -1; |
| 61 | } |
| 62 | |
Michal Vasko | 608b52b | 2015-12-11 14:39:59 +0100 | [diff] [blame] | 63 | lys_parse_fd(session->ctx, fd, LYS_IN_YIN); |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 64 | close(fd); |
| 65 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 66 | *state = session; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | teardown_read(void **state) |
| 72 | { |
| 73 | struct nc_session *session = (struct nc_session *)*state; |
| 74 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 75 | nc_session_free(session); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 76 | *state = NULL; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 81 | static void |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 82 | test_read_rpc_10(void **state) |
| 83 | { |
| 84 | struct nc_session *session = (struct nc_session *)*state; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 85 | struct nc_rpc_server *rpc = NULL; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 86 | NC_MSG_TYPE type; |
| 87 | |
| 88 | session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY); |
| 89 | session->version = NC_VERSION_10; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 90 | session->side = NC_SERVER; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 91 | |
| 92 | type = nc_recv_rpc(session, 1000, &rpc); |
| 93 | assert_int_equal(type, NC_MSG_RPC); |
| 94 | assert_non_null(rpc); |
| 95 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 96 | nc_rpc_free((struct nc_rpc *)rpc); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void |
| 100 | test_read_rpc_10_bad(void **state) |
| 101 | { |
| 102 | struct nc_session *session = (struct nc_session *)*state; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 103 | struct nc_rpc_server *rpc = NULL; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 104 | NC_MSG_TYPE type; |
| 105 | |
| 106 | session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY); |
| 107 | session->version = NC_VERSION_10; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 108 | session->side = NC_CLIENT; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 109 | |
| 110 | type = nc_recv_rpc(session, 1000, &rpc); |
| 111 | assert_int_equal(type, NC_MSG_ERROR); |
| 112 | assert_null(rpc); |
| 113 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 114 | nc_rpc_free((struct nc_rpc *)rpc); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void |
| 118 | test_read_rpc_11(void **state) |
| 119 | { |
| 120 | struct nc_session *session = (struct nc_session *)*state; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 121 | struct nc_rpc_server *rpc = NULL; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 122 | NC_MSG_TYPE type; |
| 123 | |
| 124 | session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY); |
| 125 | session->version = NC_VERSION_11; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 126 | session->side = NC_SERVER; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 127 | |
| 128 | type = nc_recv_rpc(session, 1000, &rpc); |
| 129 | assert_int_equal(type, NC_MSG_RPC); |
| 130 | assert_non_null(rpc); |
| 131 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 132 | nc_rpc_free((struct nc_rpc *)rpc); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void |
| 136 | test_read_rpc_11_bad(void **state) |
| 137 | { |
| 138 | struct nc_session *session = (struct nc_session *)*state; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 139 | struct nc_rpc_server *rpc = NULL; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 140 | NC_MSG_TYPE type; |
| 141 | |
| 142 | session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY); |
| 143 | session->version = NC_VERSION_11; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 144 | session->side = NC_CLIENT; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 145 | |
| 146 | type = nc_recv_rpc(session, 1000, &rpc); |
| 147 | assert_int_equal(type, NC_MSG_ERROR); |
| 148 | assert_null(rpc); |
| 149 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 150 | nc_rpc_free((struct nc_rpc *)rpc); |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 151 | }*/ |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 152 | |
| 153 | |
| 154 | struct wr { |
| 155 | struct nc_session *session; |
| 156 | struct nc_rpc *rpc; |
| 157 | }; |
| 158 | |
| 159 | static int |
| 160 | setup_write(void **state) |
| 161 | { |
| 162 | (void) state; /* unused */ |
| 163 | int fd; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 164 | struct wr *w; |
| 165 | |
| 166 | w = malloc(sizeof *w); |
| 167 | w->session = calloc(1, sizeof *w->session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 168 | w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas"); |
| 169 | w->session->ti_lock = malloc(sizeof *w->session->ti_lock); |
| 170 | pthread_mutex_init(w->session->ti_lock, NULL); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 171 | |
| 172 | /* ietf-netconf */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 173 | fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 174 | if (fd == -1) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 175 | free(w); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 176 | return -1; |
| 177 | } |
| 178 | |
Michal Vasko | 608b52b | 2015-12-11 14:39:59 +0100 | [diff] [blame] | 179 | lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 180 | close(fd); |
| 181 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 182 | w->session->status = NC_STATUS_RUNNING; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 183 | w->session->version = NC_VERSION_10; |
| 184 | w->session->msgid = 999; |
| 185 | w->session->ti_type = NC_TI_FD; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 186 | w->session->ti.fd.in = STDIN_FILENO; |
| 187 | w->session->ti.fd.out = STDOUT_FILENO; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 188 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 189 | /* get rpc to write */ |
| 190 | w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 191 | assert_non_null(w->rpc); |
| 192 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 193 | w->session->ti.fd.in = -1; |
| 194 | |
| 195 | *state = w; |
| 196 | |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 201 | teardown_write(void **state) |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 202 | { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 203 | struct wr *w = (struct wr *)*state; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 204 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 205 | nc_rpc_free(w->rpc); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 206 | w->session->ti.fd.in = -1; |
| 207 | w->session->ti.fd.out = -1; |
| 208 | nc_session_free(w->session); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 209 | free(w); |
| 210 | *state = NULL; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static void |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 216 | test_write_rpc(void **state) |
| 217 | { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 218 | struct wr *w = (struct wr *)*state; |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 219 | uint64_t msgid; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 220 | NC_MSG_TYPE type; |
| 221 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 222 | w->session->side = NC_CLIENT; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 223 | |
| 224 | do { |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 225 | type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 226 | } while(type == NC_MSG_WOULDBLOCK); |
| 227 | |
| 228 | assert_int_equal(type, NC_MSG_RPC); |
| 229 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 230 | write(w->session->ti.fd.out, "\n", 1); |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 233 | static void |
| 234 | test_write_rpc_10(void **state) |
| 235 | { |
| 236 | struct wr *w = (struct wr *)*state; |
| 237 | |
| 238 | w->session->version = NC_VERSION_10; |
| 239 | |
| 240 | return test_write_rpc(state); |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | test_write_rpc_11(void **state) |
| 245 | { |
| 246 | struct wr *w = (struct wr *)*state; |
| 247 | |
| 248 | w->session->version = NC_VERSION_11; |
| 249 | |
| 250 | return test_write_rpc(state); |
| 251 | } |
| 252 | |
| 253 | static void |
| 254 | test_write_rpc_bad(void **state) |
| 255 | { |
| 256 | struct wr *w = (struct wr *)*state; |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 257 | uint64_t msgid; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 258 | NC_MSG_TYPE type; |
| 259 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 260 | w->session->side = NC_SERVER; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 261 | |
| 262 | do { |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 263 | type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 264 | } while(type == NC_MSG_WOULDBLOCK); |
| 265 | |
| 266 | assert_int_equal(type, NC_MSG_ERROR); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void |
| 270 | test_write_rpc_10_bad(void **state) |
| 271 | { |
| 272 | struct wr *w = (struct wr *)*state; |
| 273 | |
| 274 | w->session->version = NC_VERSION_10; |
| 275 | |
| 276 | return test_write_rpc_bad(state); |
| 277 | } |
| 278 | |
| 279 | static void |
| 280 | test_write_rpc_11_bad(void **state) |
| 281 | { |
| 282 | struct wr *w = (struct wr *)*state; |
| 283 | |
| 284 | w->session->version = NC_VERSION_11; |
| 285 | |
| 286 | return test_write_rpc_bad(state); |
| 287 | } |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 288 | int main(void) |
| 289 | { |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 290 | const struct CMUnitTest io[] = { |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 291 | /*cmocka_unit_test_setup_teardown(test_read_rpc_10, setup_read, teardown_read), |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 292 | cmocka_unit_test_setup_teardown(test_read_rpc_10_bad, setup_read, teardown_read), |
| 293 | cmocka_unit_test_setup_teardown(test_read_rpc_11, setup_read, teardown_read), |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 294 | cmocka_unit_test_setup_teardown(test_read_rpc_11_bad, setup_read, teardown_read),*/ |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 295 | cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write), |
| 296 | cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write), |
| 297 | cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write), |
| 298 | cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)}; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 299 | |
| 300 | return cmocka_run_group_tests(io, NULL, NULL); |
| 301 | } |