| /* |
| * @file set.c |
| * @author: Radek Krejci <rkrejci@cesnet.cz> |
| * @brief unit tests for functions from context.c |
| * |
| * Copyright (c) 2018 CESNET, z.s.p.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 <stdarg.h> |
| #include <stddef.h> |
| #include <setjmp.h> |
| #include <cmocka.h> |
| |
| #include <string.h> |
| #include <stdio.h> |
| |
| #include "common.h" |
| #include "context.h" |
| #include "parser.h" |
| #include "tests/config.h" |
| #include "tree_schema_internal.h" |
| |
| #define BUFSIZE 1024 |
| char logbuf[BUFSIZE] = {0}; |
| int store = -1; /* negative for infinite logging, positive for limited logging */ |
| |
| /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| #define ENABLE_LOGGER_CHECKING 1 |
| |
| static void |
| logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| { |
| (void) level; /* unused */ |
| if (store) { |
| if (path && path[0]) { |
| snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| } else { |
| strncpy(logbuf, msg, BUFSIZE - 1); |
| } |
| if (store > 0) { |
| --store; |
| } |
| } |
| } |
| |
| static int |
| logger_setup(void **state) |
| { |
| (void) state; /* unused */ |
| #if ENABLE_LOGGER_CHECKING |
| ly_set_log_clb(logger, 1); |
| #endif |
| return 0; |
| } |
| |
| static int |
| logger_teardown(void **state) |
| { |
| (void) state; /* unused */ |
| #if ENABLE_LOGGER_CHECKING |
| if (*state) { |
| fprintf(stderr, "%s\n", logbuf); |
| } |
| #endif |
| return 0; |
| } |
| |
| #if ENABLE_LOGGER_CHECKING |
| # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| #else |
| # define logbuf_assert(str) |
| #endif |
| |
| static void |
| test_searchdirs(void **state) |
| { |
| *state = test_searchdirs; |
| |
| struct ly_ctx *ctx; |
| const char * const *list; |
| |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| |
| /* invalid arguments */ |
| assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir())."); |
| assert_null(ly_ctx_get_searchdirs(NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs())."); |
| assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdir())."); |
| |
| /* readable and executable, but not a directory */ |
| assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utest_context")); |
| logbuf_assert("Given search directory \""TESTS_BIN"/utest_context\" is not a directory."); |
| /* not executable */ |
| assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__)); |
| logbuf_assert("Unable to fully access search directory \""__FILE__"\" (Permission denied)."); |
| /* not existing */ |
| assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile")); |
| logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)."); |
| |
| /* ly_set_add() fails */ |
| /* no change */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL)); |
| |
| /* correct path */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests")); |
| assert_int_equal(1, ctx->search_paths.count); |
| assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]); |
| |
| /* duplicated paths */ |
| assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests")); |
| assert_int_equal(1, ctx->search_paths.count); |
| assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]); |
| |
| /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles")); |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src")); |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules")); |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc")); |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC)); |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN)); |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home")); |
| assert_int_equal(8, ctx->search_paths.count); |
| |
| /* get searchpaths */ |
| list = ly_ctx_get_searchdirs(ctx); |
| assert_non_null(list); |
| assert_string_equal(TESTS_BIN"/utests", list[0]); |
| assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]); |
| assert_string_equal(TESTS_SRC, list[5]); |
| assert_string_equal(TESTS_BIN, list[6]); |
| assert_string_equal("/home", list[7]); |
| assert_null(list[8]); |
| |
| /* removing searchpaths */ |
| /* nonexisting */ |
| assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(ctx, "/nonexistingfile")); |
| logbuf_assert("Invalid argument value (ly_ctx_unset_searchdir())."); |
| /* first */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_BIN"/utests")); |
| assert_string_not_equal(TESTS_BIN"/utests", list[0]); |
| assert_int_equal(7, ctx->search_paths.count); |
| /* middle */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_SRC)); |
| assert_int_equal(6, ctx->search_paths.count); |
| /* last */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, "/home")); |
| assert_int_equal(5, ctx->search_paths.count); |
| /* all */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, NULL)); |
| assert_int_equal(0, ctx->search_paths.count); |
| |
| /* again - no change */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, NULL)); |
| |
| /* cleanup */ |
| ly_ctx_destroy(ctx, NULL); |
| |
| /* test searchdir list in ly_ctx_new() */ |
| assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx)); |
| logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)."); |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/home:/home:"TESTS_SRC, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| assert_int_equal(2, ctx->search_paths.count); |
| assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]); |
| assert_string_equal("/home", ctx->search_paths.objs[1]); |
| |
| /* cleanup */ |
| *state = NULL; |
| ly_ctx_destroy(ctx, NULL); |
| } |
| |
| static void |
| test_options(void **state) |
| { |
| *state = test_options; |
| |
| struct ly_ctx *ctx; |
| |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &ctx)); |
| |
| /* invalid arguments */ |
| assert_int_equal(0, ly_ctx_get_options(NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_get_options())."); |
| |
| assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_set_options())."); |
| assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_unset_options())."); |
| |
| /* option not allowed to be changed */ |
| assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NOYANGLIBRARY)); |
| logbuf_assert("Invalid argument option (ly_ctx_set_options())."); |
| assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NOYANGLIBRARY)); |
| logbuf_assert("Invalid argument option (ly_ctx_set_options())."); |
| |
| |
| /* unset */ |
| /* LY_CTX_ALLIMPLEMENTED */ |
| assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED)); |
| assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| |
| /* LY_CTX_DISABLE_SEARCHDIRS */ |
| assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIRS)); |
| assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| |
| /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
| assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| |
| /* LY_CTX_PREFER_SEARCHDIRS */ |
| assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_PREFER_SEARCHDIRS)); |
| assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| |
| /* LY_CTX_TRUSTED */ |
| assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_TRUSTED)); |
| assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| |
| assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| |
| /* set back */ |
| /* LY_CTX_ALLIMPLEMENTED */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED)); |
| assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| |
| /* LY_CTX_DISABLE_SEARCHDIRS */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIRS)); |
| assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| |
| /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
| assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| |
| /* LY_CTX_PREFER_SEARCHDIRS */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_PREFER_SEARCHDIRS)); |
| assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| |
| /* LY_CTX_TRUSTED */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_TRUSTED)); |
| assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| |
| assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| |
| /* cleanup */ |
| *state = NULL; |
| ly_ctx_destroy(ctx, NULL); |
| } |
| |
| static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| { |
| *module_data = user_data; |
| *format = LYS_IN_YANG; |
| *free_module_data = NULL; |
| return LY_SUCCESS; |
| } |
| |
| static void |
| test_models(void **state) |
| { |
| *state = test_models; |
| |
| struct ly_ctx *ctx; |
| struct ly_in *in; |
| const char *str; |
| struct lys_module *mod1, *mod2; |
| |
| /* invalid arguments */ |
| assert_int_equal(0, ly_ctx_get_module_set_id(NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id())."); |
| |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx)); |
| |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in)); |
| assert_int_equal(LY_EINVAL, lys_create_module(ctx, in, 4, 1, NULL, NULL, &mod1)); |
| ly_in_free(in, 0); |
| logbuf_assert("Invalid schema input format."); |
| |
| /* import callback */ |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test")); |
| assert_ptr_equal(test_imp_clb, ctx->imp_clb); |
| assert_ptr_equal(str, ctx->imp_clb_data); |
| assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str)); |
| assert_string_equal("test", str); |
| |
| ly_ctx_set_module_imp_clb(ctx, NULL, NULL); |
| assert_null(ctx->imp_clb); |
| assert_null(ctx->imp_clb_data); |
| |
| /* name collision of module and submodule */ |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}"); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in)); |
| assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
| ly_in_free(in, 0); |
| logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1."); |
| |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in)); |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
| ly_in_free(in, 0); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in)); |
| assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
| ly_in_free(in, 0); |
| logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1."); |
| |
| store = 1; |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}"); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in)); |
| assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
| ly_in_free(in, 0); |
| logbuf_assert("Name collision between submodules of name \"y\". Line number 1."); |
| store = -1; |
| |
| /* selecting correct revision of the submodules */ |
| ly_ctx_reset_latests(ctx); |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}"); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in)); |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod2)); |
| ly_in_free(in, 0); |
| assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date); |
| |
| /* reloading module in case only the compiled module resists in the context */ |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in)); |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod1)); |
| ly_in_free(in, 0); |
| mod1->implemented = 1; |
| assert_int_equal(LY_SUCCESS, lys_compile(mod1, LYSC_OPT_FREE_SP)); |
| assert_non_null(mod1->compiled); |
| assert_null(mod1->parsed); |
| |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", &in)); |
| /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */ |
| ly_ctx_set_module_imp_clb(ctx, NULL, NULL); |
| assert_int_equal(LY_ENOTFOUND, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2)); |
| /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/ |
| logbuf_assert("Recompilation of module \"w\" failed."); |
| assert_null(mod2); |
| ly_in_free(in, 0); |
| |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory("module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", &in)); |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}"); |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2)); |
| ly_in_free(in, 0); |
| assert_non_null(mod2); |
| assert_non_null(mod1->parsed); |
| assert_string_equal("w", mod1->name); |
| |
| /* cleanup */ |
| *state = NULL; |
| ly_ctx_destroy(ctx, NULL); |
| } |
| |
| static void |
| test_imports(void **state) |
| { |
| *state = test_imports; |
| |
| struct ly_ctx *ctx; |
| const struct lys_module *mod1, *mod2, *import; |
| |
| /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision |
| * from the callback should be loaded into the context and used as an import */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
| assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", |
| LYS_IN_YANG, &mod1)); |
| assert_int_equal(1, mod1->latest_revision); |
| assert_int_equal(1, mod1->implemented); |
| assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| LYS_IN_YANG, &mod2)); |
| import = mod2->parsed->imports[0].module; |
| assert_int_equal(2, import->latest_revision); |
| assert_int_equal(0, mod1->latest_revision); |
| assert_ptr_not_equal(mod1, import); |
| assert_string_equal("2019-09-17", import->revision); |
| assert_int_equal(0, import->implemented); |
| assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16")); |
| ly_ctx_destroy(ctx, NULL); |
| |
| /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision |
| * already present in the context should be selected and the callback's revision should not be loaded into the context */ |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
| assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}", |
| LYS_IN_YANG, &mod1)); |
| assert_int_equal(1, mod1->latest_revision); |
| assert_int_equal(1, mod1->implemented); |
| assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| LYS_IN_YANG, &mod2)); |
| import = mod2->parsed->imports[0].module; |
| assert_ptr_equal(mod1, import); |
| assert_int_equal(2, import->latest_revision); |
| assert_int_equal(1, import->implemented); |
| assert_string_equal("2019-09-18", import->revision); |
| assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17")); |
| ly_ctx_destroy(ctx, NULL); |
| |
| /* cleanup */ |
| *state = NULL; |
| } |
| |
| static void |
| test_get_models(void **state) |
| { |
| *state = test_get_models; |
| |
| struct ly_ctx *ctx; |
| struct lys_module *mod, *mod2; |
| const char *str0 = "module a {namespace urn:a;prefix a;}"; |
| const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}"; |
| const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}"; |
| struct ly_in *in0, *in1, *in2; |
| |
| unsigned int index = 0; |
| const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"}; |
| |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0)); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2)); |
| assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| |
| /* invalid arguments */ |
| assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_get_module())."); |
| assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL)); |
| logbuf_assert("Invalid argument name (ly_ctx_get_module())."); |
| assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL)); |
| logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns())."); |
| assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL)); |
| logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns())."); |
| assert_null(ly_ctx_get_module(ctx, "nonsence", NULL)); |
| |
| /* internal modules */ |
| assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types")); |
| mod = ly_ctx_get_module_implemented(ctx, "yang"); |
| assert_non_null(mod); |
| assert_non_null(mod->parsed); |
| assert_string_equal("yang", mod->name); |
| mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns); |
| assert_ptr_equal(mod, mod2); |
| assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05")); |
| assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15")); |
| assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15")); |
| assert_non_null(ly_ctx_get_module_ns(ctx, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14")); |
| |
| /* select module by revision */ |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod)); |
| /* invalid attempts - implementing module of the same name and inserting the same module */ |
| assert_int_equal(LY_EDENIED, lys_create_module(ctx, in2, LYS_IN_YANG, 1, NULL, NULL, NULL)); |
| logbuf_assert("Module \"a\" is already implemented in the context."); |
| ly_in_reset(in1); |
| assert_int_equal(LY_EEXIST, lys_create_module(ctx, in1, LYS_IN_YANG, 0, NULL, NULL, NULL)); |
| logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context."); |
| /* insert the second module only as imported, not implemented */ |
| ly_in_reset(in2); |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in2, LYS_IN_YANG, 0, NULL, NULL, &mod2)); |
| assert_non_null(mod2); |
| assert_ptr_not_equal(mod, mod2); |
| mod = ly_ctx_get_module_latest(ctx, "a"); |
| assert_ptr_equal(mod, mod2); |
| mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns); |
| assert_ptr_equal(mod, mod2); |
| /* work with module with no revision */ |
| assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in0, LYS_IN_YANG, 0, NULL, NULL, &mod)); |
| assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL)); |
| assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a")); |
| |
| str1 = "submodule b {belongs-to a {prefix a;}}"; |
| ly_in_free(in1, 0); |
| assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
| assert_int_equal(LY_EINVAL, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod)); |
| logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module."); |
| |
| while ((mod = (struct lys_module *)ly_ctx_get_module_iter(ctx, &index))) { |
| assert_string_equal(names[index - 1], mod->name); |
| } |
| assert_int_equal(9, index); |
| |
| /* cleanup */ |
| *state = NULL; |
| ly_in_free(in0, 0); |
| ly_in_free(in1, 0); |
| ly_in_free(in2, 0); |
| ly_ctx_destroy(ctx, NULL); |
| } |
| |
| int main(void) |
| { |
| const struct CMUnitTest tests[] = { |
| cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown), |
| cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown), |
| cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown), |
| cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown), |
| cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown), |
| }; |
| |
| return cmocka_run_group_tests(tests, NULL, NULL); |
| } |