blob: 1a86c082f7a2e26a30d9d42a07475dc1b8063dbe [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>
Michal Vasko086311b2016-01-08 09:53:11 +010038#include <session_client.h>
Radek Krejcice24ab82015-10-08 15:37:02 +020039#include <messages_p.h>
40#include "config.h"
41
Michal Vaskof44f2482015-12-11 14:40:30 +010042/*static int
Radek Krejcia146f472015-10-19 15:00:05 +020043setup_read(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +020044{
Radek Krejcice24ab82015-10-08 15:37:02 +020045 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +020046 struct nc_session *session;
Radek Krejcice24ab82015-10-08 15:37:02 +020047
Radek Krejcia146f472015-10-19 15:00:05 +020048 session = calloc(1, sizeof *session);
Michal Vaskof44f2482015-12-11 14:40:30 +010049 * test IO with standard file descriptors *
Radek Krejcia146f472015-10-19 15:00:05 +020050 session->ti_type = NC_TI_FD;
Radek Krejcia146f472015-10-19 15:00:05 +020051
Radek Krejci695d4fa2015-10-22 13:23:54 +020052 session->status = NC_STATUS_RUNNING;
53 session->ctx = ly_ctx_new(TESTS_DIR"../schemas");
54 session->ti_lock = malloc(sizeof *session->ti_lock);
55 pthread_mutex_init(session->ti_lock, NULL);
Radek Krejcice24ab82015-10-08 15:37:02 +020056
Michal Vaskof44f2482015-12-11 14:40:30 +010057 * ietf-netconf *
Radek Krejci695d4fa2015-10-22 13:23:54 +020058 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcice24ab82015-10-08 15:37:02 +020059 if (fd == -1) {
60 return -1;
61 }
62
Michal Vasko608b52b2015-12-11 14:39:59 +010063 lys_parse_fd(session->ctx, fd, LYS_IN_YIN);
Radek Krejcice24ab82015-10-08 15:37:02 +020064 close(fd);
65
Radek Krejcia146f472015-10-19 15:00:05 +020066 *state = session;
67 return 0;
68}
69
70static int
71teardown_read(void **state)
72{
73 struct nc_session *session = (struct nc_session *)*state;
74
Radek Krejci695d4fa2015-10-22 13:23:54 +020075 nc_session_free(session);
Radek Krejcia146f472015-10-19 15:00:05 +020076 *state = NULL;
77
78 return 0;
79}
80
Michal Vaskof44f2482015-12-11 14:40:30 +010081static void
Radek Krejcia146f472015-10-19 15:00:05 +020082test_read_rpc_10(void **state)
83{
84 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +020085 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +020086 NC_MSG_TYPE type;
87
88 session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY);
89 session->version = NC_VERSION_10;
Radek Krejci695d4fa2015-10-22 13:23:54 +020090 session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +020091
92 type = nc_recv_rpc(session, 1000, &rpc);
93 assert_int_equal(type, NC_MSG_RPC);
94 assert_non_null(rpc);
95
Radek Krejci695d4fa2015-10-22 13:23:54 +020096 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +020097}
98
99static void
100test_read_rpc_10_bad(void **state)
101{
102 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200103 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200104 NC_MSG_TYPE type;
105
106 session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY);
107 session->version = NC_VERSION_10;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200108 session->side = NC_CLIENT;
Radek Krejcia146f472015-10-19 15:00:05 +0200109
110 type = nc_recv_rpc(session, 1000, &rpc);
111 assert_int_equal(type, NC_MSG_ERROR);
112 assert_null(rpc);
113
Radek Krejci695d4fa2015-10-22 13:23:54 +0200114 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200115}
116
117static void
118test_read_rpc_11(void **state)
119{
120 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200121 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200122 NC_MSG_TYPE type;
123
124 session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY);
125 session->version = NC_VERSION_11;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200126 session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200127
128 type = nc_recv_rpc(session, 1000, &rpc);
129 assert_int_equal(type, NC_MSG_RPC);
130 assert_non_null(rpc);
131
Radek Krejci695d4fa2015-10-22 13:23:54 +0200132 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200133}
134
135static void
136test_read_rpc_11_bad(void **state)
137{
138 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200139 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200140 NC_MSG_TYPE type;
141
142 session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY);
143 session->version = NC_VERSION_11;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200144 session->side = NC_CLIENT;
Radek Krejcia146f472015-10-19 15:00:05 +0200145
146 type = nc_recv_rpc(session, 1000, &rpc);
147 assert_int_equal(type, NC_MSG_ERROR);
148 assert_null(rpc);
149
Radek Krejci695d4fa2015-10-22 13:23:54 +0200150 nc_rpc_free((struct nc_rpc *)rpc);
Michal Vaskoad611702015-12-03 13:41:51 +0100151}*/
Radek Krejcia146f472015-10-19 15:00:05 +0200152
153
154struct wr {
155 struct nc_session *session;
156 struct nc_rpc *rpc;
157};
158
159static int
160setup_write(void **state)
161{
162 (void) state; /* unused */
163 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +0200164 struct wr *w;
165
166 w = malloc(sizeof *w);
167 w->session = calloc(1, sizeof *w->session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200168 w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas");
169 w->session->ti_lock = malloc(sizeof *w->session->ti_lock);
170 pthread_mutex_init(w->session->ti_lock, NULL);
Radek Krejcia146f472015-10-19 15:00:05 +0200171
172 /* ietf-netconf */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200173 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcia146f472015-10-19 15:00:05 +0200174 if (fd == -1) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100175 free(w);
Radek Krejcia146f472015-10-19 15:00:05 +0200176 return -1;
177 }
178
Michal Vasko608b52b2015-12-11 14:39:59 +0100179 lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN);
Radek Krejcia146f472015-10-19 15:00:05 +0200180 close(fd);
181
Radek Krejci695d4fa2015-10-22 13:23:54 +0200182 w->session->status = NC_STATUS_RUNNING;
Radek Krejcia146f472015-10-19 15:00:05 +0200183 w->session->version = NC_VERSION_10;
184 w->session->msgid = 999;
185 w->session->ti_type = NC_TI_FD;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200186 w->session->ti.fd.in = STDIN_FILENO;
187 w->session->ti.fd.out = STDOUT_FILENO;
Radek Krejcia146f472015-10-19 15:00:05 +0200188
Radek Krejci695d4fa2015-10-22 13:23:54 +0200189 /* get rpc to write */
190 w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
Radek Krejcia146f472015-10-19 15:00:05 +0200191 assert_non_null(w->rpc);
192
Radek Krejcia146f472015-10-19 15:00:05 +0200193 w->session->ti.fd.in = -1;
194
195 *state = w;
196
Radek Krejcice24ab82015-10-08 15:37:02 +0200197 return 0;
198}
199
200static int
Radek Krejcia146f472015-10-19 15:00:05 +0200201teardown_write(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +0200202{
Radek Krejcia146f472015-10-19 15:00:05 +0200203 struct wr *w = (struct wr *)*state;
Radek Krejcice24ab82015-10-08 15:37:02 +0200204
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200205 nc_rpc_free(w->rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200206 w->session->ti.fd.in = -1;
207 w->session->ti.fd.out = -1;
208 nc_session_free(w->session);
Radek Krejcia146f472015-10-19 15:00:05 +0200209 free(w);
210 *state = NULL;
Radek Krejcice24ab82015-10-08 15:37:02 +0200211
212 return 0;
213}
214
215static void
Radek Krejcife0b3472015-10-12 13:43:42 +0200216test_write_rpc(void **state)
217{
Radek Krejcia146f472015-10-19 15:00:05 +0200218 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100219 uint64_t msgid;
Radek Krejcife0b3472015-10-12 13:43:42 +0200220 NC_MSG_TYPE type;
221
Radek Krejci695d4fa2015-10-22 13:23:54 +0200222 w->session->side = NC_CLIENT;
Radek Krejcife0b3472015-10-12 13:43:42 +0200223
224 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100225 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcife0b3472015-10-12 13:43:42 +0200226 } while(type == NC_MSG_WOULDBLOCK);
227
228 assert_int_equal(type, NC_MSG_RPC);
229
Radek Krejcia146f472015-10-19 15:00:05 +0200230 write(w->session->ti.fd.out, "\n", 1);
Radek Krejcice24ab82015-10-08 15:37:02 +0200231}
232
Radek Krejcia146f472015-10-19 15:00:05 +0200233static void
234test_write_rpc_10(void **state)
235{
236 struct wr *w = (struct wr *)*state;
237
238 w->session->version = NC_VERSION_10;
239
240 return test_write_rpc(state);
241}
242
243static void
244test_write_rpc_11(void **state)
245{
246 struct wr *w = (struct wr *)*state;
247
248 w->session->version = NC_VERSION_11;
249
250 return test_write_rpc(state);
251}
252
253static void
254test_write_rpc_bad(void **state)
255{
256 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100257 uint64_t msgid;
Radek Krejcia146f472015-10-19 15:00:05 +0200258 NC_MSG_TYPE type;
259
Radek Krejci695d4fa2015-10-22 13:23:54 +0200260 w->session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200261
262 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100263 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcia146f472015-10-19 15:00:05 +0200264 } while(type == NC_MSG_WOULDBLOCK);
265
266 assert_int_equal(type, NC_MSG_ERROR);
Radek Krejcia146f472015-10-19 15:00:05 +0200267}
268
269static void
270test_write_rpc_10_bad(void **state)
271{
272 struct wr *w = (struct wr *)*state;
273
274 w->session->version = NC_VERSION_10;
275
276 return test_write_rpc_bad(state);
277}
278
279static void
280test_write_rpc_11_bad(void **state)
281{
282 struct wr *w = (struct wr *)*state;
283
284 w->session->version = NC_VERSION_11;
285
286 return test_write_rpc_bad(state);
287}
Radek Krejcice24ab82015-10-08 15:37:02 +0200288int main(void)
289{
Radek Krejcife0b3472015-10-12 13:43:42 +0200290 const struct CMUnitTest io[] = {
Michal Vaskof44f2482015-12-11 14:40:30 +0100291 /*cmocka_unit_test_setup_teardown(test_read_rpc_10, setup_read, teardown_read),
Radek Krejcia146f472015-10-19 15:00:05 +0200292 cmocka_unit_test_setup_teardown(test_read_rpc_10_bad, setup_read, teardown_read),
293 cmocka_unit_test_setup_teardown(test_read_rpc_11, setup_read, teardown_read),
Michal Vaskof44f2482015-12-11 14:40:30 +0100294 cmocka_unit_test_setup_teardown(test_read_rpc_11_bad, setup_read, teardown_read),*/
Radek Krejcia146f472015-10-19 15:00:05 +0200295 cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write),
296 cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write),
297 cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write),
298 cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)};
Radek Krejcice24ab82015-10-08 15:37:02 +0200299
300 return cmocka_run_group_tests(io, NULL, NULL);
301}