blob: 67fce5803114e35268e705e4cdf329cda3d4b0d5 [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
Radek Krejcice24ab82015-10-08 15:37:02 +020041static 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);
48 /* test IO with standard file descriptors */
49 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
56 /* 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 Vaskoad611702015-12-03 13:41:51 +010080/*static 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;
Radek Krejcife0b3472015-10-12 13:43:42 +0200218 NC_MSG_TYPE type;
219
Radek Krejci695d4fa2015-10-22 13:23:54 +0200220 w->session->side = NC_CLIENT;
Radek Krejcife0b3472015-10-12 13:43:42 +0200221
222 do {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200223 type = nc_send_rpc(w->session, w->rpc);
Radek Krejcife0b3472015-10-12 13:43:42 +0200224 } while(type == NC_MSG_WOULDBLOCK);
225
226 assert_int_equal(type, NC_MSG_RPC);
227
Radek Krejcia146f472015-10-19 15:00:05 +0200228 write(w->session->ti.fd.out, "\n", 1);
Radek Krejcice24ab82015-10-08 15:37:02 +0200229}
230
Radek Krejcia146f472015-10-19 15:00:05 +0200231static void
232test_write_rpc_10(void **state)
233{
234 struct wr *w = (struct wr *)*state;
235
236 w->session->version = NC_VERSION_10;
237
238 return test_write_rpc(state);
239}
240
241static void
242test_write_rpc_11(void **state)
243{
244 struct wr *w = (struct wr *)*state;
245
246 w->session->version = NC_VERSION_11;
247
248 return test_write_rpc(state);
249}
250
251static void
252test_write_rpc_bad(void **state)
253{
254 struct wr *w = (struct wr *)*state;
255 NC_MSG_TYPE type;
256
Radek Krejci695d4fa2015-10-22 13:23:54 +0200257 w->session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200258
259 do {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200260 type = nc_send_rpc(w->session, w->rpc);
Radek Krejcia146f472015-10-19 15:00:05 +0200261 } while(type == NC_MSG_WOULDBLOCK);
262
263 assert_int_equal(type, NC_MSG_ERROR);
Radek Krejcia146f472015-10-19 15:00:05 +0200264}
265
266static void
267test_write_rpc_10_bad(void **state)
268{
269 struct wr *w = (struct wr *)*state;
270
271 w->session->version = NC_VERSION_10;
272
273 return test_write_rpc_bad(state);
274}
275
276static void
277test_write_rpc_11_bad(void **state)
278{
279 struct wr *w = (struct wr *)*state;
280
281 w->session->version = NC_VERSION_11;
282
283 return test_write_rpc_bad(state);
284}
Radek Krejcice24ab82015-10-08 15:37:02 +0200285int main(void)
286{
Radek Krejcife0b3472015-10-12 13:43:42 +0200287 const struct CMUnitTest io[] = {
Radek Krejcia146f472015-10-19 15:00:05 +0200288 cmocka_unit_test_setup_teardown(test_read_rpc_10, setup_read, teardown_read),
289 cmocka_unit_test_setup_teardown(test_read_rpc_10_bad, setup_read, teardown_read),
290 cmocka_unit_test_setup_teardown(test_read_rpc_11, setup_read, teardown_read),
291 cmocka_unit_test_setup_teardown(test_read_rpc_11_bad, setup_read, teardown_read),
292 cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write),
293 cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write),
294 cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write),
295 cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)};
Radek Krejcice24ab82015-10-08 15:37:02 +0200296
297 return cmocka_run_group_tests(io, NULL, NULL);
298}