| /** |
| * \file test_io.c |
| * \author Radek Krejci <rkrejci@cesnet.cz> |
| * \brief libnetconf2 tests - input/output functions |
| * |
| * Copyright (c) 2015 CESNET, z.s.p.o. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
| * are met: |
| * 1. Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * 2. Redistributions in binary form must reproduce the above copyright |
| * notice, this list of conditions and the following disclaimer in |
| * the documentation and/or other materials provided with the |
| * distribution. |
| * 3. Neither the name of the Company nor the names of its contributors |
| * may be used to endorse or promote products derived from this |
| * software without specific prior written permission. |
| * |
| */ |
| |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <pthread.h> |
| #include <setjmp.h> |
| #include <stdarg.h> |
| #include <stddef.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| |
| #include <cmocka.h> |
| #include <libyang/libyang.h> |
| |
| #include <session_p.h> |
| #include <session_client.h> |
| #include <messages_p.h> |
| #include "config.h" |
| |
| /*static int |
| setup_read(void **state) |
| { |
| int fd; |
| struct nc_session *session; |
| |
| session = calloc(1, sizeof *session); |
| * test IO with standard file descriptors * |
| session->ti_type = NC_TI_FD; |
| |
| session->status = NC_STATUS_RUNNING; |
| session->ctx = ly_ctx_new(TESTS_DIR"../schemas"); |
| session->ti_lock = malloc(sizeof *session->ti_lock); |
| pthread_mutex_init(session->ti_lock, NULL); |
| |
| * ietf-netconf * |
| fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY); |
| if (fd == -1) { |
| return -1; |
| } |
| |
| lys_parse_fd(session->ctx, fd, LYS_IN_YIN); |
| close(fd); |
| |
| *state = session; |
| return 0; |
| } |
| |
| static int |
| teardown_read(void **state) |
| { |
| struct nc_session *session = (struct nc_session *)*state; |
| |
| nc_session_free(session); |
| *state = NULL; |
| |
| return 0; |
| } |
| |
| static void |
| test_read_rpc_10(void **state) |
| { |
| struct nc_session *session = (struct nc_session *)*state; |
| struct nc_rpc_server *rpc = NULL; |
| NC_MSG_TYPE type; |
| |
| session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY); |
| session->version = NC_VERSION_10; |
| session->side = NC_SERVER; |
| |
| type = nc_recv_rpc(session, 1000, &rpc); |
| assert_int_equal(type, NC_MSG_RPC); |
| assert_non_null(rpc); |
| |
| nc_rpc_free((struct nc_rpc *)rpc); |
| } |
| |
| static void |
| test_read_rpc_10_bad(void **state) |
| { |
| struct nc_session *session = (struct nc_session *)*state; |
| struct nc_rpc_server *rpc = NULL; |
| NC_MSG_TYPE type; |
| |
| session->ti.fd.in = open(TESTS_DIR"/data/nc10/rpc-lock", O_RDONLY); |
| session->version = NC_VERSION_10; |
| session->side = NC_CLIENT; |
| |
| type = nc_recv_rpc(session, 1000, &rpc); |
| assert_int_equal(type, NC_MSG_ERROR); |
| assert_null(rpc); |
| |
| nc_rpc_free((struct nc_rpc *)rpc); |
| } |
| |
| static void |
| test_read_rpc_11(void **state) |
| { |
| struct nc_session *session = (struct nc_session *)*state; |
| struct nc_rpc_server *rpc = NULL; |
| NC_MSG_TYPE type; |
| |
| session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY); |
| session->version = NC_VERSION_11; |
| session->side = NC_SERVER; |
| |
| type = nc_recv_rpc(session, 1000, &rpc); |
| assert_int_equal(type, NC_MSG_RPC); |
| assert_non_null(rpc); |
| |
| nc_rpc_free((struct nc_rpc *)rpc); |
| } |
| |
| static void |
| test_read_rpc_11_bad(void **state) |
| { |
| struct nc_session *session = (struct nc_session *)*state; |
| struct nc_rpc_server *rpc = NULL; |
| NC_MSG_TYPE type; |
| |
| session->ti.fd.in = open(TESTS_DIR"/data/nc11/rpc-lock", O_RDONLY); |
| session->version = NC_VERSION_11; |
| session->side = NC_CLIENT; |
| |
| type = nc_recv_rpc(session, 1000, &rpc); |
| assert_int_equal(type, NC_MSG_ERROR); |
| assert_null(rpc); |
| |
| nc_rpc_free((struct nc_rpc *)rpc); |
| }*/ |
| |
| |
| struct wr { |
| struct nc_session *session; |
| struct nc_rpc *rpc; |
| }; |
| |
| static int |
| setup_write(void **state) |
| { |
| (void) state; /* unused */ |
| int fd; |
| struct wr *w; |
| |
| w = malloc(sizeof *w); |
| w->session = calloc(1, sizeof *w->session); |
| w->session->ctx = ly_ctx_new(TESTS_DIR"../schemas"); |
| w->session->ti_lock = malloc(sizeof *w->session->ti_lock); |
| pthread_mutex_init(w->session->ti_lock, NULL); |
| |
| /* ietf-netconf */ |
| fd = open(TESTS_DIR"../schemas/ietf-netconf.yin", O_RDONLY); |
| if (fd == -1) { |
| return -1; |
| } |
| |
| lys_parse_fd(w->session->ctx, fd, LYS_IN_YIN); |
| close(fd); |
| |
| w->session->status = NC_STATUS_RUNNING; |
| w->session->version = NC_VERSION_10; |
| w->session->msgid = 999; |
| w->session->ti_type = NC_TI_FD; |
| w->session->ti.fd.in = STDIN_FILENO; |
| w->session->ti.fd.out = STDOUT_FILENO; |
| |
| /* get rpc to write */ |
| w->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING); |
| assert_non_null(w->rpc); |
| |
| close(fd); |
| w->session->ti.fd.in = -1; |
| |
| *state = w; |
| |
| return 0; |
| } |
| |
| static int |
| teardown_write(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| |
| nc_rpc_free(w->rpc); |
| w->session->ti.fd.in = -1; |
| w->session->ti.fd.out = -1; |
| nc_session_free(w->session); |
| free(w); |
| *state = NULL; |
| |
| return 0; |
| } |
| |
| static void |
| test_write_rpc(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| uint64_t msgid; |
| NC_MSG_TYPE type; |
| |
| w->session->side = NC_CLIENT; |
| |
| do { |
| type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
| } while(type == NC_MSG_WOULDBLOCK); |
| |
| assert_int_equal(type, NC_MSG_RPC); |
| |
| write(w->session->ti.fd.out, "\n", 1); |
| } |
| |
| static void |
| test_write_rpc_10(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| |
| w->session->version = NC_VERSION_10; |
| |
| return test_write_rpc(state); |
| } |
| |
| static void |
| test_write_rpc_11(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| |
| w->session->version = NC_VERSION_11; |
| |
| return test_write_rpc(state); |
| } |
| |
| static void |
| test_write_rpc_bad(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| uint64_t msgid; |
| NC_MSG_TYPE type; |
| |
| w->session->side = NC_SERVER; |
| |
| do { |
| type = nc_send_rpc(w->session, w->rpc, 1000, &msgid); |
| } while(type == NC_MSG_WOULDBLOCK); |
| |
| assert_int_equal(type, NC_MSG_ERROR); |
| } |
| |
| static void |
| test_write_rpc_10_bad(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| |
| w->session->version = NC_VERSION_10; |
| |
| return test_write_rpc_bad(state); |
| } |
| |
| static void |
| test_write_rpc_11_bad(void **state) |
| { |
| struct wr *w = (struct wr *)*state; |
| |
| w->session->version = NC_VERSION_11; |
| |
| return test_write_rpc_bad(state); |
| } |
| int main(void) |
| { |
| const struct CMUnitTest io[] = { |
| /*cmocka_unit_test_setup_teardown(test_read_rpc_10, setup_read, teardown_read), |
| cmocka_unit_test_setup_teardown(test_read_rpc_10_bad, setup_read, teardown_read), |
| cmocka_unit_test_setup_teardown(test_read_rpc_11, setup_read, teardown_read), |
| cmocka_unit_test_setup_teardown(test_read_rpc_11_bad, setup_read, teardown_read),*/ |
| cmocka_unit_test_setup_teardown(test_write_rpc_10, setup_write, teardown_write), |
| cmocka_unit_test_setup_teardown(test_write_rpc_10_bad, setup_write, teardown_write), |
| cmocka_unit_test_setup_teardown(test_write_rpc_11, setup_write, teardown_write), |
| cmocka_unit_test_setup_teardown(test_write_rpc_11_bad, setup_write, teardown_write)}; |
| |
| return cmocka_run_group_tests(io, NULL, NULL); |
| } |