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