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 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 43 | struct wr { |
| 44 | struct nc_session *session; |
| 45 | struct nc_rpc *rpc; |
| 46 | }; |
| 47 | |
| 48 | static int |
| 49 | setup_write(void **state) |
| 50 | { |
| 51 | (void) state; /* unused */ |
| 52 | int fd; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 53 | struct wr *w; |
| 54 | |
| 55 | w = malloc(sizeof *w); |
| 56 | w->session = calloc(1, sizeof *w->session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 57 | w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas"); |
| 58 | w->session->ti_lock = malloc(sizeof *w->session->ti_lock); |
| 59 | pthread_mutex_init(w->session->ti_lock, NULL); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 60 | |
| 61 | /* ietf-netconf */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 62 | fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 63 | if (fd == -1) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 64 | free(w); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 65 | return -1; |
| 66 | } |
| 67 | |
Michal Vasko | 608b52b | 2015-12-11 14:39:59 +0100 | [diff] [blame] | 68 | lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 69 | close(fd); |
| 70 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 71 | w->session->status = NC_STATUS_RUNNING; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 72 | w->session->version = NC_VERSION_10; |
| 73 | w->session->msgid = 999; |
| 74 | w->session->ti_type = NC_TI_FD; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 75 | w->session->ti.fd.in = STDIN_FILENO; |
| 76 | w->session->ti.fd.out = STDOUT_FILENO; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 77 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 78 | /* get rpc to write */ |
| 79 | w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 80 | assert_non_null(w->rpc); |
| 81 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 82 | w->session->ti.fd.in = -1; |
| 83 | |
| 84 | *state = w; |
| 85 | |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 90 | teardown_write(void **state) |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 91 | { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 92 | struct wr *w = (struct wr *)*state; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 93 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 94 | nc_rpc_free(w->rpc); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 95 | w->session->ti.fd.in = -1; |
| 96 | w->session->ti.fd.out = -1; |
| 97 | nc_session_free(w->session); |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 98 | free(w); |
| 99 | *state = NULL; |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static void |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 105 | test_write_rpc(void **state) |
| 106 | { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 107 | struct wr *w = (struct wr *)*state; |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 108 | uint64_t msgid; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 109 | NC_MSG_TYPE type; |
| 110 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 111 | w->session->side = NC_CLIENT; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 112 | |
| 113 | do { |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 114 | type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 115 | } while(type == NC_MSG_WOULDBLOCK); |
| 116 | |
| 117 | assert_int_equal(type, NC_MSG_RPC); |
| 118 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 119 | write(w->session->ti.fd.out, "\n", 1); |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 122 | static void |
| 123 | test_write_rpc_10(void **state) |
| 124 | { |
| 125 | struct wr *w = (struct wr *)*state; |
| 126 | |
| 127 | w->session->version = NC_VERSION_10; |
| 128 | |
| 129 | return test_write_rpc(state); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | test_write_rpc_11(void **state) |
| 134 | { |
| 135 | struct wr *w = (struct wr *)*state; |
| 136 | |
| 137 | w->session->version = NC_VERSION_11; |
| 138 | |
| 139 | return test_write_rpc(state); |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | test_write_rpc_bad(void **state) |
| 144 | { |
| 145 | struct wr *w = (struct wr *)*state; |
Michal Vasko | f44f248 | 2015-12-11 14:40:30 +0100 | [diff] [blame] | 146 | uint64_t msgid; |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 147 | NC_MSG_TYPE type; |
| 148 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 149 | w->session->side = NC_SERVER; |
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 | } |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 177 | int main(void) |
| 178 | { |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 179 | const struct CMUnitTest io[] = { |
Radek Krejci | a146f47 | 2015-10-19 15:00:05 +0200 | [diff] [blame] | 180 | cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write), |
| 181 | cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write), |
| 182 | cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write), |
| 183 | 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] | 184 | |
| 185 | return cmocka_run_group_tests(io, NULL, NULL); |
| 186 | } |