blob: d8d589097c2844c5def6cb3297b7976890e512f0 [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
Radek Krejcia146f472015-10-19 15:00:05 +020043struct wr {
44 struct nc_session *session;
45 struct nc_rpc *rpc;
46};
47
48static int
49setup_write(void **state)
50{
51 (void) state; /* unused */
52 int fd;
Radek Krejcia146f472015-10-19 15:00:05 +020053 struct wr *w;
54
55 w = malloc(sizeof *w);
56 w->session = calloc(1, sizeof *w->session);
Radek Krejci695d4fa2015-10-22 13:23:54 +020057 w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas");
58 w->session->ti_lock = malloc(sizeof *w->session->ti_lock);
59 pthread_mutex_init(w->session->ti_lock, NULL);
Radek Krejcia146f472015-10-19 15:00:05 +020060
61 /* ietf-netconf */
Radek Krejci695d4fa2015-10-22 13:23:54 +020062 fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY);
Radek Krejcia146f472015-10-19 15:00:05 +020063 if (fd == -1) {
Michal Vasko11d142a2016-01-19 15:58:24 +010064 free(w);
Radek Krejcia146f472015-10-19 15:00:05 +020065 return -1;
66 }
67
Michal Vasko608b52b2015-12-11 14:39:59 +010068 lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN);
Radek Krejcia146f472015-10-19 15:00:05 +020069 close(fd);
70
Radek Krejci695d4fa2015-10-22 13:23:54 +020071 w->session->status = NC_STATUS_RUNNING;
Radek Krejcia146f472015-10-19 15:00:05 +020072 w->session->version = NC_VERSION_10;
73 w->session->msgid = 999;
74 w->session->ti_type = NC_TI_FD;
Radek Krejci695d4fa2015-10-22 13:23:54 +020075 w->session->ti.fd.in = STDIN_FILENO;
76 w->session->ti.fd.out = STDOUT_FILENO;
Radek Krejcia146f472015-10-19 15:00:05 +020077
Radek Krejci695d4fa2015-10-22 13:23:54 +020078 /* get rpc to write */
79 w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
Radek Krejcia146f472015-10-19 15:00:05 +020080 assert_non_null(w->rpc);
81
Radek Krejcia146f472015-10-19 15:00:05 +020082 w->session->ti.fd.in = -1;
83
84 *state = w;
85
Radek Krejcice24ab82015-10-08 15:37:02 +020086 return 0;
87}
88
89static int
Radek Krejcia146f472015-10-19 15:00:05 +020090teardown_write(void **state)
Radek Krejcice24ab82015-10-08 15:37:02 +020091{
Radek Krejcia146f472015-10-19 15:00:05 +020092 struct wr *w = (struct wr *)*state;
Radek Krejcice24ab82015-10-08 15:37:02 +020093
Radek Krejcia53b3fe2015-10-19 17:25:04 +020094 nc_rpc_free(w->rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +020095 w->session->ti.fd.in = -1;
96 w->session->ti.fd.out = -1;
97 nc_session_free(w->session);
Radek Krejcia146f472015-10-19 15:00:05 +020098 free(w);
99 *state = NULL;
Radek Krejcice24ab82015-10-08 15:37:02 +0200100
101 return 0;
102}
103
104static void
Radek Krejcife0b3472015-10-12 13:43:42 +0200105test_write_rpc(void **state)
106{
Radek Krejcia146f472015-10-19 15:00:05 +0200107 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100108 uint64_t msgid;
Radek Krejcife0b3472015-10-12 13:43:42 +0200109 NC_MSG_TYPE type;
110
Radek Krejci695d4fa2015-10-22 13:23:54 +0200111 w->session->side = NC_CLIENT;
Radek Krejcife0b3472015-10-12 13:43:42 +0200112
113 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100114 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcife0b3472015-10-12 13:43:42 +0200115 } while(type == NC_MSG_WOULDBLOCK);
116
117 assert_int_equal(type, NC_MSG_RPC);
118
Radek Krejcia146f472015-10-19 15:00:05 +0200119 write(w->session->ti.fd.out, "\n", 1);
Radek Krejcice24ab82015-10-08 15:37:02 +0200120}
121
Radek Krejcia146f472015-10-19 15:00:05 +0200122static void
123test_write_rpc_10(void **state)
124{
125 struct wr *w = (struct wr *)*state;
126
127 w->session->version = NC_VERSION_10;
128
129 return test_write_rpc(state);
130}
131
132static void
133test_write_rpc_11(void **state)
134{
135 struct wr *w = (struct wr *)*state;
136
137 w->session->version = NC_VERSION_11;
138
139 return test_write_rpc(state);
140}
141
142static void
143test_write_rpc_bad(void **state)
144{
145 struct wr *w = (struct wr *)*state;
Michal Vaskof44f2482015-12-11 14:40:30 +0100146 uint64_t msgid;
Radek Krejcia146f472015-10-19 15:00:05 +0200147 NC_MSG_TYPE type;
148
Radek Krejci695d4fa2015-10-22 13:23:54 +0200149 w->session->side = NC_SERVER;
Radek Krejcia146f472015-10-19 15:00:05 +0200150
151 do {
Michal Vaskof44f2482015-12-11 14:40:30 +0100152 type = nc_send_rpc(w->session, w->rpc, 1000, &msgid);
Radek Krejcia146f472015-10-19 15:00:05 +0200153 } while(type == NC_MSG_WOULDBLOCK);
154
155 assert_int_equal(type, NC_MSG_ERROR);
Radek Krejcia146f472015-10-19 15:00:05 +0200156}
157
158static void
159test_write_rpc_10_bad(void **state)
160{
161 struct wr *w = (struct wr *)*state;
162
163 w->session->version = NC_VERSION_10;
164
165 return test_write_rpc_bad(state);
166}
167
168static void
169test_write_rpc_11_bad(void **state)
170{
171 struct wr *w = (struct wr *)*state;
172
173 w->session->version = NC_VERSION_11;
174
175 return test_write_rpc_bad(state);
176}
Radek Krejcice24ab82015-10-08 15:37:02 +0200177int main(void)
178{
Radek Krejcife0b3472015-10-12 13:43:42 +0200179 const struct CMUnitTest io[] = {
Radek Krejcia146f472015-10-19 15:00:05 +0200180 cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write),
181 cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write),
182 cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write),
183 cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)};
Radek Krejcice24ab82015-10-08 15:37:02 +0200184
185 return cmocka_run_group_tests(io, NULL, NULL);
186}