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