From 632ead7ef3c9a793d555a008419a658b918cf320 Mon Sep 17 00:00:00 2001 From: Mislav Novakovic Date: Wed, 2 Mar 2016 23:29:24 +0100 Subject: [PATCH] tests FEATURE simple unit test of libyang.h functions Signed-off-by: Mislav Novakovic --- tests/CMakeLists.txt | 2 +- tests/data/files/a.xml | 3 + tests/data/test_libyang.c | 431 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 435 insertions(+), 1 deletion(-) create mode 100644 tests/data/files/a.xml create mode 100644 tests/data/test_libyang.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7184c1b4e..03d2a05d6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 2.6) -set(data_tests test_data_initialization test_leafref_remove test_instid_remove) +set(data_tests test_data_initialization test_leafref_remove test_instid_remove test_libyang) set(schema_yin_tests test_ietf test_augment test_print_transform) foreach(test_name IN LISTS data_tests) diff --git a/tests/data/files/a.xml b/tests/data/files/a.xml new file mode 100644 index 000000000..7d8250ab0 --- /dev/null +++ b/tests/data/files/a.xml @@ -0,0 +1,3 @@ + + test + diff --git a/tests/data/test_libyang.c b/tests/data/test_libyang.c new file mode 100644 index 000000000..49f543ab8 --- /dev/null +++ b/tests/data/test_libyang.c @@ -0,0 +1,431 @@ +/** + * @file test_data_initialization.c + * @author Mislav Novakovic + * @brief Cmocka data test initialization. + * + * Copyright (c) 2015 Sartura d.o.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../config.h" +#include "../../src/libyang.h" + +struct ly_ctx *ctx = NULL; +struct lyd_node *root = NULL; + +int +generic_init(char *config_file, char *yang_file, char *yang_folder) +{ + LYS_INFORMAT yang_format; + LYD_FORMAT in_format; + char *schema = NULL; + char *config = NULL; + struct stat sb_schema, sb_config; + int fd = -1; + + if (!config_file || !yang_file || !yang_folder) { + goto error; + } + + yang_format = LYS_IN_YIN; + in_format = LYD_XML; + + ctx = ly_ctx_new(yang_folder); + if (!ctx) { + goto error; + } + + fd = open(yang_file, O_RDONLY); + if (fd == -1 || fstat(fd, &sb_schema) == -1 || !S_ISREG(sb_schema.st_mode)) { + goto error; + } + + schema = mmap(NULL, sb_schema.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + close(fd); + + fd = open(config_file, O_RDONLY); + if (fd == -1 || fstat(fd, &sb_config) == -1 || !S_ISREG(sb_config.st_mode)) { + goto error; + } + + config = mmap(NULL, sb_config.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + close(fd); + fd = -1; + + if (!lys_parse_mem(ctx, schema, yang_format)) { + goto error; + } + + root = lyd_parse_mem(ctx, config, in_format, LYD_OPT_STRICT); + if (!root) { + goto error; + } + + /* cleanup */ + munmap(config, sb_config.st_size); + munmap(schema, sb_schema.st_size); + + return 0; + +error: + if (schema) { + munmap(schema, sb_schema.st_size); + } + if (config) { + munmap(config, sb_config.st_size); + } + if (fd != -1) { + close(fd); + } + + return -1; +} + +static int +setup_f(void **state) +{ + (void) state; /* unused */ + char *config_file = TESTS_DIR"/data/files/a.xml"; + char *yang_file = TESTS_DIR"/schema/yin/files/a.yin"; + char *yang_folder = TESTS_DIR"/schema/yin/files"; + int rc; + + rc = generic_init(config_file, yang_file, yang_folder); + + if (rc) { + return -1; + } + + return 0; +} + +static int +teardown_f(void **state) +{ + (void) state; /* unused */ + if (root) + lyd_free(root); + if (ctx) + ly_ctx_destroy(ctx, NULL); + + return 0; +} + +static void +test_ly_ctx_new(void **state) +{ + char *yang_folder = TESTS_DIR"/data/files"; + (void) state; /* unused */ + ctx = ly_ctx_new(yang_folder); + if (!ctx) { + fail(); + } + + ly_ctx_destroy(ctx, NULL); +} + +static void +test_ly_ctx_new_invalid(void **state) +{ + char *yang_folder = "INVALID_PATH"; + (void) state; /* unused */ + ctx = ly_ctx_new(yang_folder); + if (ctx) { + fail(); + } +} + +static void +test_ly_ctx_get_searchdir(void **state) +{ + const char *result; + char *yang_folder = TESTS_DIR"/data/files"; + (void) state; /* unused */ + ctx = ly_ctx_new(yang_folder); + if (!ctx) { + fail(); + } + + result = ly_ctx_get_searchdir(ctx); + if (!result) { + fail(); + } + assert_string_equal(yang_folder, result); + + ly_ctx_destroy(ctx, NULL); +} + +static void +test_ly_ctx_set_searchdir(void **state) +{ + const char *result; + char *yang_folder = TESTS_DIR"/data/files"; + char *new_yang_folder = TESTS_DIR"/schema/yin"; + (void) state; /* unused */ + ctx = ly_ctx_new(yang_folder); + if (!ctx) { + fail(); + } + + ly_ctx_set_searchdir(ctx, new_yang_folder); + result = ly_ctx_get_searchdir(ctx); + if (!result) { + fail(); + } + + assert_string_equal(new_yang_folder, result); + + ly_ctx_destroy(ctx, NULL); +} + +static void +test_ly_ctx_set_searchdir_invalid(void **state) +{ + const char *result; + char *yang_folder = TESTS_DIR"/data/files"; + char *new_yang_folder = "INVALID_PATH"; + (void) state; /* unused */ + ctx = ly_ctx_new(yang_folder); + if (!ctx) { + fail(); + } + + ly_ctx_set_searchdir(ctx, new_yang_folder); + result = ly_ctx_get_searchdir(ctx); + if (!result) { + fail(); + } + + assert_string_equal(yang_folder, result); + + ly_ctx_destroy(ctx, NULL); +} + +static void +test_ly_ctx_info(void **state) +{ + struct lyd_node *node; + char *yang_folder = TESTS_DIR"/data/files"; + (void) state; /* unused */ + ctx = ly_ctx_new(yang_folder); + if (!ctx) { + fail(); + } + + node = ly_ctx_info(ctx); + + assert_int_equal(LYD_VAL_OK, node->validity); + + lyd_free(node); +} + +static void +test_ly_ctx_get_module_name(void **state) +{ + (void) state; /* unused */ + const char **result; + + result = ly_ctx_get_module_names(ctx); + if (!result) { + fail(); + } + + assert_string_equal("yang", *result); + + free(result); +} + +static void +test_ly_ctx_get_submodule_name(void **state) +{ + (void) state; /* unused */ + const char **result; + const char *module_name = "a"; + + result = ly_ctx_get_submodule_names(ctx, module_name); + + assert_string_equal("asub", *result); + + free(result); +} + +static void +test_ly_ctx_get_module(void **state) +{ + (void) state; /* unused */ + const struct lys_module *module; + const char *name = "a"; + const char *revision = NULL; + + module = ly_ctx_get_module(ctx, name, revision); + if (!module) { + fail(); + } + + assert_string_equal("a", module->name); +} + +static void +test_ly_ctx_get_module_by_ns(void **state) +{ + (void) state; /* unused */ + const struct lys_module *module; + const char *ns = "urn:a"; + const char *revision = NULL; + + module = ly_ctx_get_module_by_ns(ctx, ns, revision); + if (!module) { + fail(); + } + + assert_string_equal("a", module->name); +} + +static void +test_ly_ctx_get_submodule(void **state) +{ + (void) state; /* unused */ + const struct lys_module *module; + const struct lys_submodule *submodule; + const char *mod_name = "a"; + const char *sub_name = "asub"; + const char *revision = NULL; + + module = ly_ctx_get_module(ctx, mod_name, revision); + if (!module) { + fail(); + } + + submodule = ly_ctx_get_submodule(module, sub_name, revision); + if (!submodule) { + fail(); + } + + assert_string_equal("asub", submodule->name); +} + +static void +test_ly_set_new(void **state) +{ + (void) state; /* unused */ + struct ly_set *set; + + set = ly_set_new(); + if (!set) { + fail(); + } + + free(set); +} + +static void +test_ly_set_free(void **state) +{ + (void) state; /* unused */ + struct ly_set *set; + + set = ly_set_new(); + if (!set) { + fail(); + } + + ly_set_free(set); + + if (!set) { + fail(); + } +} + +static void +test_ly_errno_location(void **state) +{ + (void) state; /* unused */ + char *yang_folder = "INVALID_PATH"; + + LY_ERR *error; + + error = ly_errno_location(); + + assert_int_equal(LY_SUCCESS, *error); + + ctx = ly_ctx_new(yang_folder); + if (ctx) { + fail(); + } + + error = ly_errno_location(); + + assert_int_equal(LY_ESYS, *error); +} + +static void +test_ly_errmsg(void **state) +{ + (void) state; /* unused */ + const char *msg; + char *yang_folder = "INVALID_PATH"; + char *compare = "Unable to use search directory \"INVALID_PATH\" (No such file or directory)"; + + ctx = ly_ctx_new(yang_folder); + if (ctx) { + fail(); + } + + msg = ly_errmsg(); + + assert_string_equal(compare, msg); +} + +static void +test_ly_errpath(void **state) +{ + (void) state; /* unused */ + const char *path; + char *compare = ""; + + path = ly_errpath(); + + assert_string_equal(compare, path); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_ly_ctx_new), + cmocka_unit_test(test_ly_ctx_new_invalid), + cmocka_unit_test(test_ly_ctx_get_searchdir), + cmocka_unit_test(test_ly_ctx_set_searchdir), + cmocka_unit_test(test_ly_ctx_set_searchdir_invalid), + cmocka_unit_test_teardown(test_ly_ctx_info, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_ctx_get_module_name, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_ctx_get_submodule_name, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_ctx_get_module, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_ctx_get_module_by_ns, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_ctx_get_submodule, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_set_new, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_set_free, setup_f, teardown_f), + cmocka_unit_test_setup_teardown(test_ly_errno_location, setup_f, teardown_f), + cmocka_unit_test(test_ly_errmsg), + cmocka_unit_test_setup_teardown(test_ly_errpath, setup_f, teardown_f), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}