blob: bd301cf234a1059f5b359d955036c9173210210d [file] [log] [blame]
Radek Krejcice24ab82015-10-08 15:37:02 +02001/**
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
Michal Vaskof44f2482015-12-11 14:40:30 +010041/*static int
Radek Krejcia146f472015-10-19 15:00:05 +020042setup_read(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +020043{
Radek Krejcice24ab82015-10-08 15:37:02 +020044 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +020045 struct nc_session *session;
Radek Krejcice24ab82015-10-08 15:37:02 +020046
Radek Krejcia146f472015-10-19 15:00:05 +020047 session = calloc(1, sizeof *session);
Michal Vaskof44f2482015-12-11 14:40:30 +010048 * test IO with standard file descriptors *
Radek Krejcia146f472015-10-19 15:00:05 +020049 session->ti_type = NC_TI_FD;
Radek Krejcia146f472015-10-19 15:00:05 +020050
Radek Krejci695d4fa2015-10-22 13:23:54 +020051 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 Krejcice24ab82015-10-08 15:37:02 +020055
Michal Vaskof44f2482015-12-11 14:40:30 +010056 * ietf-netconf *
Radek Krejci695d4fa2015-10-22 13:23:54 +020057 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcice24ab82015-10-08 15:37:02 +020058 if (fd == -1) {
59 return -1;
60 }
61
Michal Vasko608b52b2015-12-11 14:39:59 +010062 lys_parse_fd(session->ctx, fd, LYS_IN_YIN);
Radek Krejcice24ab82015-10-08 15:37:02 +020063 close(fd);
64
Radek Krejcia146f472015-10-19 15:00:05 +020065 *state = session;
66 return 0;
67}
68
69static int
70teardown_read(void **state)
71{
72 struct nc_session *session = (struct nc_session *)*state;
73
Radek Krejci695d4fa2015-10-22 13:23:54 +020074 nc_session_free(session);
Radek Krejcia146f472015-10-19 15:00:05 +020075 *state = NULL;
76
77 return 0;
78}
79
Michal Vaskof44f2482015-12-11 14:40:30 +010080static void
Radek Krejcia146f472015-10-19 15:00:05 +020081test_read_rpc_10(void **state)
82{
83 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +020084 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +020085 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 Krejci695d4fa2015-10-22 13:23:54 +020089 session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +020090
91 type = nc_recv_rpc(session, 1000, &rpc);
92 assert_int_equal(type, NC_MSG_RPC);
93 assert_non_null(rpc);
94
Radek Krejci695d4fa2015-10-22 13:23:54 +020095 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +020096}
97
98static void
99test_read_rpc_10_bad(void **state)
100{
101 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200102 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200103 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 Krejci695d4fa2015-10-22 13:23:54 +0200107 session->side = NC_CLIENT;
Radek Krejcia146f472015-10-19 15:00:05 +0200108
109 type = nc_recv_rpc(session, 1000, &rpc);
110 assert_int_equal(type, NC_MSG_ERROR);
111 assert_null(rpc);
112
Radek Krejci695d4fa2015-10-22 13:23:54 +0200113 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200114}
115
116static void
117test_read_rpc_11(void **state)
118{
119 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200120 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200121 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 Krejci695d4fa2015-10-22 13:23:54 +0200125 session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200126
127 type = nc_recv_rpc(session, 1000, &rpc);
128 assert_int_equal(type, NC_MSG_RPC);
129 assert_non_null(rpc);
130
Radek Krejci695d4fa2015-10-22 13:23:54 +0200131 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200132}
133
134static void
135test_read_rpc_11_bad(void **state)
136{
137 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200138 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200139 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 Krejci695d4fa2015-10-22 13:23:54 +0200143 session->side = NC_CLIENT;
Radek Krejcia146f472015-10-19 15:00:05 +0200144
145 type = nc_recv_rpc(session, 1000, &rpc);
146 assert_int_equal(type, NC_MSG_ERROR);
147 assert_null(rpc);
148
Radek Krejci695d4fa2015-10-22 13:23:54 +0200149 nc_rpc_free((struct nc_rpc *)rpc);
Michal Vaskoad611702015-12-03 13:41:51 +0100150}*/
Radek Krejcia146f472015-10-19 15:00:05 +0200151
152
153struct wr {
154 struct nc_session *session;
155 struct nc_rpc *rpc;
156};
157
158static int
159setup_write(void **state)
160{
161 (void) state; /* unused */
162 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +0200163 struct wr *w;
164
165 w = malloc(sizeof *w);
166 w->session = calloc(1, sizeof *w->session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200167 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 Krejcia146f472015-10-19 15:00:05 +0200170
171 /* ietf-netconf */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200172 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcia146f472015-10-19 15:00:05 +0200173 if (fd == -1) {
174 return -1;
175 }
176
Michal Vasko608b52b2015-12-11 14:39:59 +0100177 lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN);
Radek Krejcia146f472015-10-19 15:00:05 +0200178 close(fd);
179
Radek Krejci695d4fa2015-10-22 13:23:54 +0200180 w->session->status = NC_STATUS_RUNNING;
Radek Krejcia146f472015-10-19 15:00:05 +0200181 w->session->version = NC_VERSION_10;
182 w->session->msgid = 999;
183 w->session->ti_type = NC_TI_FD;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200184 w->session->ti.fd.in = STDIN_FILENO;
185 w->session->ti.fd.out = STDOUT_FILENO;
Radek Krejcia146f472015-10-19 15:00:05 +0200186
Radek Krejci695d4fa2015-10-22 13:23:54 +0200187 /* get rpc to write */
188 w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
Radek Krejcia146f472015-10-19 15:00:05 +0200189 assert_non_null(w->rpc);
190
191 close(fd);
192 w->session->ti.fd.in = -1;
193
194 *state = w;
195
Radek Krejcice24ab82015-10-08 15:37:02 +0200196 return 0;
197}
198
199static int
Radek Krejcia146f472015-10-19 15:00:05 +0200200teardown_write(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +0200201{
Radek Krejcia146f472015-10-19 15:00:05 +0200202 struct wr *w = (struct wr *)*state;
Radek Krejcice24ab82015-10-08 15:37:02 +0200203
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200204 nc_rpc_free(w->rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200205 w->session->ti.fd.in = -1;
206 w->session->ti.fd.out = -1;
207 nc_session_free(w->session);
Radek Krejcia146f472015-10-19 15:00:05 +0200208 free(w);
209 *state = NULL;
Radek Krejcice24ab82015-10-08 15:37:02 +0200210
211 return 0;
212}
213
214static void
Radek Krejcife0b3472015-10-12 13:43:42 +0200215test_write_rpc(void **state)
216{
Radek Krejcia146f472015-10-19 15:00:05 +0200217 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100218 uint64_t msgid;
Radek Krejcife0b3472015-10-12 13:43:42 +0200219 NC_MSG_TYPE type;
220
Radek Krejci695d4fa2015-10-22 13:23:54 +0200221 w->session->side = NC_CLIENT;
Radek Krejcife0b3472015-10-12 13:43:42 +0200222
223 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100224 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcife0b3472015-10-12 13:43:42 +0200225 } while(type == NC_MSG_WOULDBLOCK);
226
227 assert_int_equal(type, NC_MSG_RPC);
228
Radek Krejcia146f472015-10-19 15:00:05 +0200229 write(w->session->ti.fd.out, "\n", 1);
Radek Krejcice24ab82015-10-08 15:37:02 +0200230}
231
Radek Krejcia146f472015-10-19 15:00:05 +0200232static void
233test_write_rpc_10(void **state)
234{
235 struct wr *w = (struct wr *)*state;
236
237 w->session->version = NC_VERSION_10;
238
239 return test_write_rpc(state);
240}
241
242static void
243test_write_rpc_11(void **state)
244{
245 struct wr *w = (struct wr *)*state;
246
247 w->session->version = NC_VERSION_11;
248
249 return test_write_rpc(state);
250}
251
252static void
253test_write_rpc_bad(void **state)
254{
255 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100256 uint64_t msgid;
Radek Krejcia146f472015-10-19 15:00:05 +0200257 NC_MSG_TYPE type;
258
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259 w->session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200260
261 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100262 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcia146f472015-10-19 15:00:05 +0200263 } while(type == NC_MSG_WOULDBLOCK);
264
265 assert_int_equal(type, NC_MSG_ERROR);
Radek Krejcia146f472015-10-19 15:00:05 +0200266}
267
268static void
269test_write_rpc_10_bad(void **state)
270{
271 struct wr *w = (struct wr *)*state;
272
273 w->session->version = NC_VERSION_10;
274
275 return test_write_rpc_bad(state);
276}
277
278static void
279test_write_rpc_11_bad(void **state)
280{
281 struct wr *w = (struct wr *)*state;
282
283 w->session->version = NC_VERSION_11;
284
285 return test_write_rpc_bad(state);
286}
Radek Krejcice24ab82015-10-08 15:37:02 +0200287int main(void)
288{
Radek Krejcife0b3472015-10-12 13:43:42 +0200289 const struct CMUnitTest io[] = {
Michal Vaskof44f2482015-12-11 14:40:30 +0100290 /*cmocka_unit_test_setup_teardown(test_read_rpc_10, setup_read, teardown_read),
Radek Krejcia146f472015-10-19 15:00:05 +0200291 cmocka_unit_test_setup_teardown(test_read_rpc_10_bad, setup_read, teardown_read),
292 cmocka_unit_test_setup_teardown(test_read_rpc_11, setup_read, teardown_read),
Michal Vaskof44f2482015-12-11 14:40:30 +0100293 cmocka_unit_test_setup_teardown(test_read_rpc_11_bad, setup_read, teardown_read),*/
Radek Krejcia146f472015-10-19 15:00:05 +0200294 cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write),
295 cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write),
296 cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write),
297 cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)};
Radek Krejcice24ab82015-10-08 15:37:02 +0200298
299 return cmocka_run_group_tests(io, NULL, NULL);
300}