|
| 1 | +/** |
| 2 | + * (C) Copyright 2025 Hewlett Packard Enterprise Development LP. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause-Patent |
| 5 | + */ |
| 6 | +#define D_LOGFAC DD_FAC(tests) |
| 7 | + |
| 8 | +#include <stddef.h> |
| 9 | +#include <stdarg.h> |
| 10 | +#include <setjmp.h> |
| 11 | +#include <cmocka.h> |
| 12 | + |
| 13 | +#include <daos/common.h> |
| 14 | + |
| 15 | +#include "../dlck_args.h" |
| 16 | + |
| 17 | +/** globals */ |
| 18 | + |
| 19 | +#define APP_NAME_MOCK "app_name" |
| 20 | +#define PARSER_FAILURE EINVAL |
| 21 | + |
| 22 | +extern struct argp argp_common; |
| 23 | +extern struct argp argp_file; |
| 24 | +extern struct argp argp_engine; |
| 25 | + |
| 26 | +struct dlck_control Ctrl; |
| 27 | + |
| 28 | +argp_parser_t Argp_engine_parser_real; |
| 29 | + |
| 30 | +/** wrappers and mocks */ |
| 31 | + |
| 32 | +void |
| 33 | +__wrap_argp_failure(const struct argp_state *__restrict __state, int __status, int __errnum, |
| 34 | + const char *__restrict __fmt, ...) |
| 35 | +{ |
| 36 | + check_expected(__state); |
| 37 | + assert_int_equal(__status, PARSER_FAILURE); |
| 38 | + assert_int_equal(__errnum, PARSER_FAILURE); |
| 39 | +} |
| 40 | + |
| 41 | +static error_t |
| 42 | +argp_common_parser_mock(int key, char *arg, struct argp_state *state) |
| 43 | +{ |
| 44 | + check_expected(key); |
| 45 | + assert_non_null(state); |
| 46 | + assert_ptr_equal(state->input, &Ctrl.common); |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +static error_t |
| 52 | +argp_file_parser_mock(int key, char *arg, struct argp_state *state) |
| 53 | +{ |
| 54 | + check_expected(key); |
| 55 | + assert_non_null(state); |
| 56 | + assert_ptr_equal(state->input, &Ctrl.files); |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static error_t |
| 62 | +argp_engine_parser_mock(int key, char *arg, struct argp_state *state) |
| 63 | +{ |
| 64 | + check_expected_ptr(key); |
| 65 | + assert_non_null(state); |
| 66 | + assert_ptr_equal(state->input, &Ctrl.engine); |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +/** setups & teardowns */ |
| 72 | + |
| 73 | +static int |
| 74 | +setup_engine_args_default(void **state_ptr) |
| 75 | +{ |
| 76 | + static struct dlck_args_engine args = {0}; |
| 77 | + static struct argp_state state = {0}; |
| 78 | + error_t ret; |
| 79 | + |
| 80 | + /** bind the input */ |
| 81 | + state.input = &args; |
| 82 | + |
| 83 | + /** set defaults */ |
| 84 | + ret = Argp_engine_parser_real(ARGP_KEY_INIT, NULL, &state); |
| 85 | + assert_int_equal(ret, 0); |
| 86 | + |
| 87 | + *state_ptr = &state; |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +/** tests */ |
| 93 | + |
| 94 | +/** |
| 95 | + * Test if all the children parsers are connected properly and if each of them receives all of |
| 96 | + * the exptected special key values. |
| 97 | + */ |
| 98 | +static void |
| 99 | +test_parser_children_connection(void **unused) |
| 100 | +{ |
| 101 | + /** special keys as they are provided for each of the parsers in order */ |
| 102 | + int keys[] = {ARGP_KEY_INIT, ARGP_KEY_NO_ARGS, ARGP_KEY_END, ARGP_KEY_SUCCESS, |
| 103 | + ARGP_KEY_FINI}; |
| 104 | + |
| 105 | + /** empty argument list */ |
| 106 | + int argc = 1; |
| 107 | + char *argv[] = {APP_NAME_MOCK}; |
| 108 | + |
| 109 | + for (int i = 0; i < ARRAY_SIZE(keys); ++i) { |
| 110 | + expect_value(argp_common_parser_mock, key, keys[i]); |
| 111 | + expect_value(argp_file_parser_mock, key, keys[i]); |
| 112 | + expect_value(argp_engine_parser_mock, key, keys[i]); |
| 113 | + } |
| 114 | + |
| 115 | + dlck_args_parse(argc, argv, &Ctrl); |
| 116 | +} |
| 117 | + |
| 118 | +static void |
| 119 | +test_engine_parser_END_no_storage_path_fail(void **state_ptr) |
| 120 | +{ |
| 121 | + struct argp_state *state = *state_ptr; |
| 122 | + error_t ret; |
| 123 | + |
| 124 | + expect_value(__wrap_argp_failure, __state, state); |
| 125 | + |
| 126 | + ret = Argp_engine_parser_real(ARGP_KEY_END, NULL, state); |
| 127 | + assert_int_equal(ret, PARSER_FAILURE); |
| 128 | +} |
| 129 | + |
| 130 | +static const struct CMUnitTest dlck_args_tests[] = { |
| 131 | + {"DLCK_ARGS100: parser - children connection", test_parser_children_connection, NULL, NULL}, |
| 132 | + {"DLCK_ARGS200: engine parser + ARGP_KEY_END + no storage path", |
| 133 | + test_engine_parser_END_no_storage_path_fail, setup_engine_args_default, NULL}, |
| 134 | +}; |
| 135 | + |
| 136 | +int |
| 137 | +main(int argc, char **argv) |
| 138 | +{ |
| 139 | + /** collect function pointers to real parsers */ |
| 140 | + Argp_engine_parser_real = argp_engine.parser; |
| 141 | + |
| 142 | + /** overwrite real parsers with mocks */ |
| 143 | + argp_common.parser = argp_common_parser_mock; |
| 144 | + argp_file.parser = argp_file_parser_mock; |
| 145 | + argp_engine.parser = argp_engine_parser_mock; |
| 146 | + |
| 147 | + return cmocka_run_group_tests_name("dlck_args_ut", dlck_args_tests, NULL, NULL); |
| 148 | +} |
0 commit comments