blob: 78a512f5eb048d4368df7fbfd644c6fee7b1ee9c [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 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoad1cc6a2016-02-26 15:06:06 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcice24ab82015-10-08 15:37:02 +020013 */
14
15#include <errno.h>
16#include <fcntl.h>
17#include <pthread.h>
18#include <setjmp.h>
19#include <stdarg.h>
20#include <stddef.h>
21#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010022#include <unistd.h>
Radek Krejcice24ab82015-10-08 15:37:02 +020023#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <cmocka.h>
28#include <libyang/libyang.h>
29
Radek Krejci93e80222016-10-03 13:34:25 +020030#include <messages_p.h>
Radek Krejcice24ab82015-10-08 15:37:02 +020031#include <session_p.h>
Michal Vasko086311b2016-01-08 09:53:11 +010032#include <session_client.h>
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020033#include "tests/config.h"
Radek Krejcice24ab82015-10-08 15:37:02 +020034
Radek Krejcia146f472015-10-19 15:00:05 +020035struct wr {
36 struct nc_session *session;
37 struct nc_rpc *rpc;
38};
39
40static int
41setup_write(void **state)
42{
43 (void) state; /* unused */
44 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +020045 struct wr *w;
46
47 w = malloc(sizeof *w);
48 w->session = calloc(1, sizeof *w->session);
Radek Krejci3222b7d2017-09-21 16:04:30 +020049 w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas", 0);
Radek Krejcia146f472015-10-19 15:00:05 +020050
51 /* ietf-netconf */
Radek Krejci695d4fa2015-10-22 13:23:54 +020052 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcia146f472015-10-19 15:00:05 +020053 if (fd == -1) {
Michal Vasko11d142a2016-01-19 15:58:24 +010054 free(w);
Radek Krejcia146f472015-10-19 15:00:05 +020055 return -1;
56 }
57
Michal Vasko608b52b2015-12-11 14:39:59 +010058 lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN);
Radek Krejcia146f472015-10-19 15:00:05 +020059 close(fd);
60
Radek Krejci695d4fa2015-10-22 13:23:54 +020061 w->session->status = NC_STATUS_RUNNING;
Radek Krejcia146f472015-10-19 15:00:05 +020062 w->session->version = NC_VERSION_10;
Michal Vasko338f8472016-10-13 15:01:27 +020063 w->session->opts.client.msgid = 999;
Radek Krejcia146f472015-10-19 15:00:05 +020064 w->session->ti_type = NC_TI_FD;
Michal Vasko131120a2018-05-29 15:44:02 +020065 w->session->io_lock = malloc(sizeof *w->session->io_lock);
66 pthread_mutex_init(w->session->io_lock, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +020067 w->session->ti.fd.in = STDIN_FILENO;
68 w->session->ti.fd.out = STDOUT_FILENO;
Radek Krejcia146f472015-10-19 15:00:05 +020069
Radek Krejci695d4fa2015-10-22 13:23:54 +020070 /* get rpc to write */
71 w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
Radek Krejcia146f472015-10-19 15:00:05 +020072 assert_non_null(w->rpc);
73
Radek Krejcia146f472015-10-19 15:00:05 +020074 *state = w;
75
Radek Krejcice24ab82015-10-08 15:37:02 +020076 return 0;
77}
78
79static int
Radek Krejcia146f472015-10-19 15:00:05 +020080teardown_write(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +020081{
Radek Krejcia146f472015-10-19 15:00:05 +020082 struct wr *w = (struct wr *)*state;
Radek Krejcice24ab82015-10-08 15:37:02 +020083
Radek Krejcia53b3fe2015-10-19 17:25:04 +020084 nc_rpc_free(w->rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +020085 w->session->ti.fd.in = -1;
86 w->session->ti.fd.out = -1;
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010087 nc_session_free(w->session, NULL);
Radek Krejcia146f472015-10-19 15:00:05 +020088 free(w);
89 *state = NULL;
Radek Krejcice24ab82015-10-08 15:37:02 +020090
91 return 0;
92}
93
94static void
Radek Krejcife0b3472015-10-12 13:43:42 +020095test_write_rpc(void **state)
96{
Radek Krejcia146f472015-10-19 15:00:05 +020097 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +010098 uint64_t msgid;
Radek Krejcife0b3472015-10-12 13:43:42 +020099 NC_MSG_TYPE type;
100
Radek Krejci695d4fa2015-10-22 13:23:54 +0200101 w->session->side = NC_CLIENT;
Radek Krejcife0b3472015-10-12 13:43:42 +0200102
103 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100104 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcife0b3472015-10-12 13:43:42 +0200105 } while(type == NC_MSG_WOULDBLOCK);
106
107 assert_int_equal(type, NC_MSG_RPC);
108
Radek Krejcia146f472015-10-19 15:00:05 +0200109 write(w->session->ti.fd.out, "\n", 1);
Radek Krejcice24ab82015-10-08 15:37:02 +0200110}
111
Radek Krejcia146f472015-10-19 15:00:05 +0200112static void
113test_write_rpc_10(void **state)
114{
115 struct wr *w = (struct wr *)*state;
116
117 w->session->version = NC_VERSION_10;
118
119 return test_write_rpc(state);
120}
121
122static void
123test_write_rpc_11(void **state)
124{
125 struct wr *w = (struct wr *)*state;
126
127 w->session->version = NC_VERSION_11;
128
129 return test_write_rpc(state);
130}
131
132static void
133test_write_rpc_bad(void **state)
134{
135 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100136 uint64_t msgid;
Radek Krejcia146f472015-10-19 15:00:05 +0200137 NC_MSG_TYPE type;
138
Radek Krejci695d4fa2015-10-22 13:23:54 +0200139 w->session->side = NC_SERVER;
Michal Vasko131120a2018-05-29 15:44:02 +0200140 w->session->opts.server.rpc_lock = malloc(sizeof *w->session->opts.server.rpc_lock);
141 pthread_mutex_init(w->session->opts.server.rpc_lock, NULL);
142 w->session->opts.server.rpc_cond = malloc(sizeof *w->session->opts.server.rpc_cond);
143 pthread_cond_init(w->session->opts.server.rpc_cond, NULL);
144 w->session->opts.server.rpc_inuse = malloc(sizeof *w->session->opts.server.rpc_inuse);
145 *w->session->opts.server.rpc_inuse = 0;
Radek Krejcia146f472015-10-19 15:00:05 +0200146
147 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100148 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcia146f472015-10-19 15:00:05 +0200149 } while(type == NC_MSG_WOULDBLOCK);
150
151 assert_int_equal(type, NC_MSG_ERROR);
Radek Krejcia146f472015-10-19 15:00:05 +0200152}
153
154static void
155test_write_rpc_10_bad(void **state)
156{
157 struct wr *w = (struct wr *)*state;
158
159 w->session->version = NC_VERSION_10;
160
161 return test_write_rpc_bad(state);
162}
163
164static void
165test_write_rpc_11_bad(void **state)
166{
167 struct wr *w = (struct wr *)*state;
168
169 w->session->version = NC_VERSION_11;
170
171 return test_write_rpc_bad(state);
172}
Michal Vasko275a3022017-02-07 10:58:12 +0100173
Radek Krejcice24ab82015-10-08 15:37:02 +0200174int main(void)
175{
Radek Krejcife0b3472015-10-12 13:43:42 +0200176 const struct CMUnitTest io[] = {
Radek Krejcia146f472015-10-19 15:00:05 +0200177 cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write),
178 cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write),
179 cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write),
180 cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)};
Radek Krejcice24ab82015-10-08 15:37:02 +0200181
182 return cmocka_run_group_tests(io, NULL, NULL);
183}