blob: c53fa2918a6f3e059fe562dcc8411322ec849753 [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
41struct nc_session session = {0};
42
43static int
44setup_f(void **state)
45{
46 (void) state; /* unused */
47 int fd;
48
49 session.ctx = ly_ctx_new(TESTS_DIR"/models");
50 pthread_mutex_init(&session.ti_lock, NULL);
51
52 /* ietf-netconf */
53 fd = open(TESTS_DIR"/models/ietf-netconf.yin", O_RDONLY);
54 if (fd == -1) {
55 return -1;
56 }
57
58 lys_read(session.ctx, fd, LYS_IN_YIN);
59 close(fd);
60
61 return 0;
62}
63
64static int
65teardown_f(void **state)
66{
67 (void) state; /* unused */
68
69 ly_ctx_destroy(session.ctx);
70
71 return 0;
72}
73
74static void
75test_read_rpc(void **state)
76{
77 (void) state; /* unused */
78 struct nc_rpc *rpc;
79 NC_MSG_TYPE type;
80
81 /* test IO with standard file descriptors */
82 session.ti_type = NC_TI_FD;
83 session.ti.fd.c = 0;
84 session.side = NC_SIDE_SERVER;
85 session.version = NC_VERSION_10;
86
87 session.ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY);
88 if (session.ti.fd.in == -1) {
89 fail_msg(" Openning \"%s\" failed (%s)", TESTS_DIR"/data/nc10/rpc-lock", strerror(errno));
90 }
91
92 type = nc_recv_rpc(&session, 1000, &rpc);
93 assert_int_equal(type, NC_MSG_RPC);
94 assert_non_null(rpc);
95
96 lyxml_free_elem(session.ctx, rpc->root);
97 lyd_free(rpc->tree);
98 free(rpc);
99}
100
101int main(void)
102{
103 const struct CMUnitTest io[] = {cmocka_unit_test_setup_teardown(test_read_rpc, setup_f, teardown_f)};
104
105 return cmocka_run_group_tests(io, NULL, NULL);
106}