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 | * |
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 |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [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 <stdlib.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 22 | #include <unistd.h> |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <cmocka.h> |
| 28 | #include <libyang/libyang.h> |
| 29 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 30 | #include <messages_p.h> |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 31 | #include <session_p.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 32 | #include <session_client.h> |
Jan Kundrát | cf15d6c | 2017-10-26 18:07:56 +0200 | [diff] [blame] | 33 | #include "tests/config.h" |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 34 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 35 | struct wr { |
| 36 | struct nc_session *session; |
| 37 | struct nc_rpc *rpc; |
| 38 | }; |
| 39 | |
| 40 | static int |
| 41 | setup_write(void **state) |
| 42 | { |
| 43 | (void) state; /* unused */ |
Michal Vasko | 5ba14e5 | 2019-06-20 10:03:06 +0200 | [diff] [blame] | 44 | int fd, pipes[2]; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 45 | struct wr *w; |
| 46 | |
| 47 | w = malloc(sizeof *w); |
| 48 | w->session = calloc(1, sizeof *w->session); |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 49 | w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas", 0); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 50 | |
| 51 | /* ietf-netconf */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 52 | fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 53 | if (fd == -1) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 54 | free(w); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | |
Michal Vasko | 608b52b | 2015-12-11 14:39:59 +0100 | [diff] [blame] | 58 | lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 59 | close(fd); |
| 60 | |
Michal Vasko | 5ba14e5 | 2019-06-20 10:03:06 +0200 | [diff] [blame] | 61 | pipe(pipes); |
| 62 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 63 | w->session->status = NC_STATUS_RUNNING; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 64 | w->session->version = NC_VERSION_10; |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 65 | w->session->opts.client.msgid = 999; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 66 | w->session->ti_type = NC_TI_FD; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 67 | w->session->io_lock = malloc(sizeof *w->session->io_lock); |
| 68 | pthread_mutex_init(w->session->io_lock, NULL); |
Michal Vasko | 5ba14e5 | 2019-06-20 10:03:06 +0200 | [diff] [blame] | 69 | w->session->ti.fd.in = pipes[0]; |
| 70 | w->session->ti.fd.out = pipes[1]; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 71 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 72 | /* get rpc to write */ |
| 73 | w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 74 | assert_non_null(w->rpc); |
| 75 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 76 | *state = w; |
| 77 | |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 82 | teardown_write(void **state) |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 83 | { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 84 | struct wr *w = (struct wr *)*state; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 85 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 86 | nc_rpc_free(w->rpc); |
Michal Vasko | 5ba14e5 | 2019-06-20 10:03:06 +0200 | [diff] [blame] | 87 | close(w->session->ti.fd.in); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 88 | w->session->ti.fd.in = -1; |
Michal Vasko | 5ba14e5 | 2019-06-20 10:03:06 +0200 | [diff] [blame] | 89 | close(w->session->ti.fd.out); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 90 | w->session->ti.fd.out = -1; |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 91 | nc_session_free(w->session, NULL); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 92 | free(w); |
| 93 | *state = NULL; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static void |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 99 | test_write_rpc(void **state) |
| 100 | { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 101 | struct wr *w = (struct wr *)*state; |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 102 | uint64_t msgid; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 103 | NC_MSG_TYPE type; |
| 104 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 105 | w->session->side = NC_CLIENT; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 106 | |
| 107 | do { |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 108 | type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 109 | } while(type == NC_MSG_WOULDBLOCK); |
| 110 | |
| 111 | assert_int_equal(type, NC_MSG_RPC); |
| 112 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 113 | write(w->session->ti.fd.out, "\n", 1); |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 116 | static void |
| 117 | test_write_rpc_10(void **state) |
| 118 | { |
| 119 | struct wr *w = (struct wr *)*state; |
| 120 | |
| 121 | w->session->version = NC_VERSION_10; |
| 122 | |
| 123 | return test_write_rpc(state); |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | test_write_rpc_11(void **state) |
| 128 | { |
| 129 | struct wr *w = (struct wr *)*state; |
| 130 | |
| 131 | w->session->version = NC_VERSION_11; |
| 132 | |
| 133 | return test_write_rpc(state); |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | test_write_rpc_bad(void **state) |
| 138 | { |
| 139 | struct wr *w = (struct wr *)*state; |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 140 | uint64_t msgid; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 141 | NC_MSG_TYPE type; |
| 142 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 143 | w->session->side = NC_SERVER; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 144 | w->session->opts.server.rpc_lock = malloc(sizeof *w->session->opts.server.rpc_lock); |
| 145 | pthread_mutex_init(w->session->opts.server.rpc_lock, NULL); |
| 146 | w->session->opts.server.rpc_cond = malloc(sizeof *w->session->opts.server.rpc_cond); |
| 147 | pthread_cond_init(w->session->opts.server.rpc_cond, NULL); |
| 148 | w->session->opts.server.rpc_inuse = malloc(sizeof *w->session->opts.server.rpc_inuse); |
| 149 | *w->session->opts.server.rpc_inuse = 0; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 150 | |
| 151 | do { |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 152 | type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 153 | } while(type == NC_MSG_WOULDBLOCK); |
| 154 | |
| 155 | assert_int_equal(type, NC_MSG_ERROR); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void |
| 159 | test_write_rpc_10_bad(void **state) |
| 160 | { |
| 161 | struct wr *w = (struct wr *)*state; |
| 162 | |
| 163 | w->session->version = NC_VERSION_10; |
| 164 | |
| 165 | return test_write_rpc_bad(state); |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | test_write_rpc_11_bad(void **state) |
| 170 | { |
| 171 | struct wr *w = (struct wr *)*state; |
| 172 | |
| 173 | w->session->version = NC_VERSION_11; |
| 174 | |
| 175 | return test_write_rpc_bad(state); |
| 176 | } |
Michal Vasko | 275a302 | 2017-02-07 10:58:12 +0100 | [diff] [blame] | 177 | |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 178 | int main(void) |
| 179 | { |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 180 | const struct CMUnitTest io[] = { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 181 | cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write), |
| 182 | cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write), |
| 183 | cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write), |
| 184 | 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] | 185 | |
| 186 | return cmocka_run_group_tests(io, NULL, NULL); |
| 187 | } |