blob: 8551575f28d91d01c5de0718625e5f200c0686bc [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>
Michal Vasko3512e402016-01-28 16:22:34 +010030#include <unistd.h>
Radek Krejcice24ab82015-10-08 15:37:02 +020031#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 Vasko086311b2016-01-08 09:53:11 +010039#include <session_client.h>
Radek Krejcice24ab82015-10-08 15:37:02 +020040#include <messages_p.h>
41#include "config.h"
42
Michal Vaskof44f2482015-12-11 14:40:30 +010043/*static int
Radek Krejcia146f472015-10-19 15:00:05 +020044setup_read(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +020045{
Radek Krejcice24ab82015-10-08 15:37:02 +020046 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +020047 struct nc_session *session;
Radek Krejcice24ab82015-10-08 15:37:02 +020048
Radek Krejcia146f472015-10-19 15:00:05 +020049 session = calloc(1, sizeof *session);
Michal Vaskof44f2482015-12-11 14:40:30 +010050 * test IO with standard file descriptors *
Radek Krejcia146f472015-10-19 15:00:05 +020051 session->ti_type = NC_TI_FD;
Radek Krejcia146f472015-10-19 15:00:05 +020052
Radek Krejci695d4fa2015-10-22 13:23:54 +020053 session->status = NC_STATUS_RUNNING;
54 session->ctx = ly_ctx_new(TESTS_DIR"../schemas");
55 session->ti_lock = malloc(sizeof *session->ti_lock);
56 pthread_mutex_init(session->ti_lock, NULL);
Radek Krejcice24ab82015-10-08 15:37:02 +020057
Michal Vaskof44f2482015-12-11 14:40:30 +010058 * ietf-netconf *
Radek Krejci695d4fa2015-10-22 13:23:54 +020059 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcice24ab82015-10-08 15:37:02 +020060 if (fd == -1) {
61 return -1;
62 }
63
Michal Vasko608b52b2015-12-11 14:39:59 +010064 lys_parse_fd(session->ctx, fd, LYS_IN_YIN);
Radek Krejcice24ab82015-10-08 15:37:02 +020065 close(fd);
66
Radek Krejcia146f472015-10-19 15:00:05 +020067 *state = session;
68 return 0;
69}
70
71static int
72teardown_read(void **state)
73{
74 struct nc_session *session = (struct nc_session *)*state;
75
Radek Krejci695d4fa2015-10-22 13:23:54 +020076 nc_session_free(session);
Radek Krejcia146f472015-10-19 15:00:05 +020077 *state = NULL;
78
79 return 0;
80}
81
Michal Vaskof44f2482015-12-11 14:40:30 +010082static void
Radek Krejcia146f472015-10-19 15:00:05 +020083test_read_rpc_10(void **state)
84{
85 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +020086 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +020087 NC_MSG_TYPE type;
88
89 session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY);
90 session->version = NC_VERSION_10;
Radek Krejci695d4fa2015-10-22 13:23:54 +020091 session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +020092
93 type = nc_recv_rpc(session, 1000, &rpc);
94 assert_int_equal(type, NC_MSG_RPC);
95 assert_non_null(rpc);
96
Radek Krejci695d4fa2015-10-22 13:23:54 +020097 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +020098}
99
100static void
101test_read_rpc_10_bad(void **state)
102{
103 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200104 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200105 NC_MSG_TYPE type;
106
107 session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY);
108 session->version = NC_VERSION_10;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200109 session->side = NC_CLIENT;
Radek Krejcia146f472015-10-19 15:00:05 +0200110
111 type = nc_recv_rpc(session, 1000, &rpc);
112 assert_int_equal(type, NC_MSG_ERROR);
113 assert_null(rpc);
114
Radek Krejci695d4fa2015-10-22 13:23:54 +0200115 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200116}
117
118static void
119test_read_rpc_11(void **state)
120{
121 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200122 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200123 NC_MSG_TYPE type;
124
125 session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY);
126 session->version = NC_VERSION_11;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200127 session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200128
129 type = nc_recv_rpc(session, 1000, &rpc);
130 assert_int_equal(type, NC_MSG_RPC);
131 assert_non_null(rpc);
132
Radek Krejci695d4fa2015-10-22 13:23:54 +0200133 nc_rpc_free((struct nc_rpc *)rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200134}
135
136static void
137test_read_rpc_11_bad(void **state)
138{
139 struct nc_session *session = (struct nc_session *)*state;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200140 struct nc_rpc_server *rpc = NULL;
Radek Krejcia146f472015-10-19 15:00:05 +0200141 NC_MSG_TYPE type;
142
143 session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY);
144 session->version = NC_VERSION_11;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200145 session->side = NC_CLIENT;
Radek Krejcia146f472015-10-19 15:00:05 +0200146
147 type = nc_recv_rpc(session, 1000, &rpc);
148 assert_int_equal(type, NC_MSG_ERROR);
149 assert_null(rpc);
150
Radek Krejci695d4fa2015-10-22 13:23:54 +0200151 nc_rpc_free((struct nc_rpc *)rpc);
Michal Vaskoad611702015-12-03 13:41:51 +0100152}*/
Radek Krejcia146f472015-10-19 15:00:05 +0200153
154
155struct wr {
156 struct nc_session *session;
157 struct nc_rpc *rpc;
158};
159
160static int
161setup_write(void **state)
162{
163 (void) state; /* unused */
164 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +0200165 struct wr *w;
166
167 w = malloc(sizeof *w);
168 w->session = calloc(1, sizeof *w->session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200169 w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas");
170 w->session->ti_lock = malloc(sizeof *w->session->ti_lock);
171 pthread_mutex_init(w->session->ti_lock, NULL);
Radek Krejcia146f472015-10-19 15:00:05 +0200172
173 /* ietf-netconf */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200174 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcia146f472015-10-19 15:00:05 +0200175 if (fd == -1) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100176 free(w);
Radek Krejcia146f472015-10-19 15:00:05 +0200177 return -1;
178 }
179
Michal Vasko608b52b2015-12-11 14:39:59 +0100180 lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN);
Radek Krejcia146f472015-10-19 15:00:05 +0200181 close(fd);
182
Radek Krejci695d4fa2015-10-22 13:23:54 +0200183 w->session->status = NC_STATUS_RUNNING;
Radek Krejcia146f472015-10-19 15:00:05 +0200184 w->session->version = NC_VERSION_10;
185 w->session->msgid = 999;
186 w->session->ti_type = NC_TI_FD;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200187 w->session->ti.fd.in = STDIN_FILENO;
188 w->session->ti.fd.out = STDOUT_FILENO;
Radek Krejcia146f472015-10-19 15:00:05 +0200189
Radek Krejci695d4fa2015-10-22 13:23:54 +0200190 /* get rpc to write */
191 w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
Radek Krejcia146f472015-10-19 15:00:05 +0200192 assert_non_null(w->rpc);
193
Radek Krejcia146f472015-10-19 15:00:05 +0200194 w->session->ti.fd.in = -1;
195
196 *state = w;
197
Radek Krejcice24ab82015-10-08 15:37:02 +0200198 return 0;
199}
200
201static int
Radek Krejcia146f472015-10-19 15:00:05 +0200202teardown_write(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +0200203{
Radek Krejcia146f472015-10-19 15:00:05 +0200204 struct wr *w = (struct wr *)*state;
Radek Krejcice24ab82015-10-08 15:37:02 +0200205
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200206 nc_rpc_free(w->rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200207 w->session->ti.fd.in = -1;
208 w->session->ti.fd.out = -1;
209 nc_session_free(w->session);
Radek Krejcia146f472015-10-19 15:00:05 +0200210 free(w);
211 *state = NULL;
Radek Krejcice24ab82015-10-08 15:37:02 +0200212
213 return 0;
214}
215
216static void
Radek Krejcife0b3472015-10-12 13:43:42 +0200217test_write_rpc(void **state)
218{
Radek Krejcia146f472015-10-19 15:00:05 +0200219 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100220 uint64_t msgid;
Radek Krejcife0b3472015-10-12 13:43:42 +0200221 NC_MSG_TYPE type;
222
Radek Krejci695d4fa2015-10-22 13:23:54 +0200223 w->session->side = NC_CLIENT;
Radek Krejcife0b3472015-10-12 13:43:42 +0200224
225 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100226 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcife0b3472015-10-12 13:43:42 +0200227 } while(type == NC_MSG_WOULDBLOCK);
228
229 assert_int_equal(type, NC_MSG_RPC);
230
Radek Krejcia146f472015-10-19 15:00:05 +0200231 write(w->session->ti.fd.out, "\n", 1);
Radek Krejcice24ab82015-10-08 15:37:02 +0200232}
233
Radek Krejcia146f472015-10-19 15:00:05 +0200234static void
235test_write_rpc_10(void **state)
236{
237 struct wr *w = (struct wr *)*state;
238
239 w->session->version = NC_VERSION_10;
240
241 return test_write_rpc(state);
242}
243
244static void
245test_write_rpc_11(void **state)
246{
247 struct wr *w = (struct wr *)*state;
248
249 w->session->version = NC_VERSION_11;
250
251 return test_write_rpc(state);
252}
253
254static void
255test_write_rpc_bad(void **state)
256{
257 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100258 uint64_t msgid;
Radek Krejcia146f472015-10-19 15:00:05 +0200259 NC_MSG_TYPE type;
260
Radek Krejci695d4fa2015-10-22 13:23:54 +0200261 w->session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200262
263 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100264 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcia146f472015-10-19 15:00:05 +0200265 } while(type == NC_MSG_WOULDBLOCK);
266
267 assert_int_equal(type, NC_MSG_ERROR);
Radek Krejcia146f472015-10-19 15:00:05 +0200268}
269
270static void
271test_write_rpc_10_bad(void **state)
272{
273 struct wr *w = (struct wr *)*state;
274
275 w->session->version = NC_VERSION_10;
276
277 return test_write_rpc_bad(state);
278}
279
280static void
281test_write_rpc_11_bad(void **state)
282{
283 struct wr *w = (struct wr *)*state;
284
285 w->session->version = NC_VERSION_11;
286
287 return test_write_rpc_bad(state);
288}
Radek Krejcice24ab82015-10-08 15:37:02 +0200289int main(void)
290{
Radek Krejcife0b3472015-10-12 13:43:42 +0200291 const struct CMUnitTest io[] = {
Michal Vaskof44f2482015-12-11 14:40:30 +0100292 /*cmocka_unit_test_setup_teardown(test_read_rpc_10, setup_read, teardown_read),
Radek Krejcia146f472015-10-19 15:00:05 +0200293 cmocka_unit_test_setup_teardown(test_read_rpc_10_bad, setup_read, teardown_read),
294 cmocka_unit_test_setup_teardown(test_read_rpc_11, setup_read, teardown_read),
Michal Vaskof44f2482015-12-11 14:40:30 +0100295 cmocka_unit_test_setup_teardown(test_read_rpc_11_bad, setup_read, teardown_read),*/
Radek Krejcia146f472015-10-19 15:00:05 +0200296 cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write),
297 cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write),
298 cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write),
299 cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)};
Radek Krejcice24ab82015-10-08 15:37:02 +0200300
301 return cmocka_run_group_tests(io, NULL, NULL);
302}