Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * @file set.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from context.c |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "tests/config.h" |
| 16 | #include "../../src/context.c" |
| 17 | |
| 18 | #include <stdarg.h> |
| 19 | #include <stddef.h> |
| 20 | #include <setjmp.h> |
| 21 | #include <cmocka.h> |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include "libyang.h" |
| 27 | |
| 28 | #define BUFSIZE 1024 |
| 29 | char logbuf[BUFSIZE] = {0}; |
| 30 | |
| 31 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 32 | #define ENABLE_LOGGER_CHECKING 1 |
| 33 | |
| 34 | static void |
| 35 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 36 | { |
| 37 | (void) level; /* unused */ |
| 38 | (void) path; /* unused */ |
| 39 | |
| 40 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | logger_setup(void **state) |
| 45 | { |
| 46 | (void) state; /* unused */ |
| 47 | #if ENABLE_LOGGER_CHECKING |
| 48 | ly_set_log_clb(logger, 0); |
| 49 | #endif |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | #if ENABLE_LOGGER_CHECKING |
| 54 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 55 | #else |
| 56 | # define logbuf_assert(str) |
| 57 | #endif |
| 58 | |
| 59 | int __real_ly_set_add(struct ly_set *set, void *object, int options); |
| 60 | int __wrap_ly_set_add(struct ly_set *set, void *object, int options) |
| 61 | { |
| 62 | int wrap = mock_type(int); |
| 63 | |
| 64 | if (wrap) { |
| 65 | /* error */ |
| 66 | return -1; |
| 67 | } else { |
| 68 | return __real_ly_set_add(set, object, options); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | test_searchdirs(void **state) |
| 74 | { |
| 75 | (void) state; /* unused */ |
| 76 | |
| 77 | struct ly_ctx *ctx; |
| 78 | const char * const *list; |
| 79 | |
| 80 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 81 | |
| 82 | /* invalid arguments */ |
| 83 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 84 | logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir())."); |
| 85 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 86 | logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs())."); |
| 87 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, 0)); |
| 88 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs())."); |
| 89 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, 0)); |
| 90 | logbuf_assert("Invalid argument index (ly_ctx_unset_searchdirs())."); |
| 91 | |
| 92 | /* readable and executable, but not a directory */ |
| 93 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context")); |
| 94 | logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory."); |
| 95 | /* not executable */ |
| 96 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_SRC"/src/context.c")); |
| 97 | logbuf_assert("Unable to use search directory \""TESTS_SRC"/src/context.c\" (Permission denied)"); |
| 98 | /* not existing */ |
| 99 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile")); |
| 100 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)"); |
| 101 | |
| 102 | /* ly_set_add() fails */ |
| 103 | will_return(__wrap_ly_set_add, 1); |
| 104 | assert_int_equal(LY_EMEM, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 105 | |
| 106 | /* correct path */ |
| 107 | will_return_always(__wrap_ly_set_add, 0); |
| 108 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 109 | assert_int_equal(1, ctx->search_paths.count); |
| 110 | assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]); |
| 111 | |
| 112 | /* duplicated paths - function succeeds, but context still contains just a single path */ |
| 113 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 114 | assert_int_equal(1, ctx->search_paths.count); |
| 115 | assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]); |
| 116 | |
| 117 | /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| 118 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles")); |
| 119 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src")); |
| 120 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules")); |
| 121 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc")); |
| 122 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC)); |
| 123 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN)); |
| 124 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/tmp")); |
| 125 | assert_int_equal(8, ctx->search_paths.count); |
| 126 | |
| 127 | /* get searchpaths */ |
| 128 | list = ly_ctx_get_searchdirs(ctx); |
| 129 | assert_non_null(list); |
| 130 | assert_string_equal(TESTS_BIN"/src", list[0]); |
| 131 | assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]); |
| 132 | assert_string_equal(TESTS_SRC, list[5]); |
| 133 | assert_string_equal(TESTS_BIN, list[6]); |
| 134 | assert_string_equal("/tmp", list[7]); |
| 135 | assert_null(list[8]); |
| 136 | |
| 137 | /* removing searchpaths */ |
| 138 | /* first */ |
| 139 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, 0)); |
| 140 | assert_string_not_equal(TESTS_BIN"/src", list[0]); |
| 141 | assert_int_equal(7, ctx->search_paths.count); |
| 142 | /* middle */ |
| 143 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, 4)); |
| 144 | assert_int_equal(6, ctx->search_paths.count); |
| 145 | /* last */ |
| 146 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, 5)); |
| 147 | assert_int_equal(5, ctx->search_paths.count); |
| 148 | /* all */ |
| 149 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, -5)); |
| 150 | assert_int_equal(0, ctx->search_paths.count); |
| 151 | |
| 152 | ly_ctx_destroy(ctx, NULL); |
| 153 | } |
| 154 | |
| 155 | int main(void) |
| 156 | { |
| 157 | const struct CMUnitTest tests[] = { |
| 158 | cmocka_unit_test_setup(test_searchdirs, logger_setup), |
| 159 | }; |
| 160 | |
| 161 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 162 | } |