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