4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #include "spdk_cunit.h"
37 #include "common/lib/test_env.c"
38 #include "event/app.c"
42 DEFINE_STUB_V(spdk_rpc_initialize
, (const char *listen_addr
));
43 DEFINE_STUB_V(spdk_rpc_finish
, (void));
44 DEFINE_STUB_V(spdk_event_call
, (struct spdk_event
*event
));
45 DEFINE_STUB_V(spdk_reactors_start
, (void));
46 DEFINE_STUB_V(spdk_reactors_stop
, (void *arg1
, void *arg2
));
47 DEFINE_STUB(spdk_reactors_init
, int, (unsigned int max_delay_us
), 0);
48 DEFINE_STUB_V(spdk_reactors_fini
, (void));
49 DEFINE_STUB(spdk_event_allocate
, struct spdk_event
*, (uint32_t core
, spdk_event_fn fn
, void *arg1
,
51 DEFINE_STUB(spdk_env_get_current_core
, uint32_t, (void), 0);
52 DEFINE_STUB(spdk_app_get_core_mask
, struct spdk_cpuset
*, (void), NULL
);
53 DEFINE_STUB_V(spdk_subsystem_config
, (FILE *fp
));
54 DEFINE_STUB_V(spdk_subsystem_init
, (struct spdk_event
*app_start_event
));
55 DEFINE_STUB_V(spdk_subsystem_fini
, (struct spdk_event
*app_stop_event
));
56 DEFINE_STUB(spdk_env_init
, int, (const struct spdk_env_opts
*opts
), 0);
57 DEFINE_STUB_V(spdk_env_opts_init
, (struct spdk_env_opts
*opts
));
58 DEFINE_STUB(spdk_env_get_core_count
, uint32_t, (void), 1);
59 DEFINE_STUB_V(spdk_rpc_register_method
, (const char *method
, spdk_rpc_method_handler func
,
60 uint32_t state_mask
));
61 DEFINE_STUB_V(spdk_rpc_set_state
, (uint32_t state
));
70 unittest_parse_args(int ch
, char *arg
)
75 test_spdk_app_parse_args(void)
77 spdk_app_parse_args_rvals_t rc
;
78 struct spdk_app_opts opts
= {};
79 struct option my_options
[2] = {};
80 char *valid_argv
[test_argc
] = {"app_ut",
87 char *invalid_argv_BW
[test_argc
] = {"app_ut",
94 /* currently use -z as our new option */
95 char *argv_added_short_opt
[test_argc
] = {"app_ut",
102 char *argv_added_long_opt
[test_argc
] = {"app_ut",
105 "-r/var/tmp/spdk.sock",
109 char *invalid_argv_missing_option
[test_argc
] = {"app_ut",
113 "--silence-noticelog"
117 /* Test valid arguments. Expected result: PASS */
118 rc
= spdk_app_parse_args(test_argc
, valid_argv
, &opts
, "", NULL
, unittest_parse_args
, NULL
);
119 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_SUCCESS
);
122 /* Test invalid short option Expected result: FAIL */
123 rc
= spdk_app_parse_args(test_argc
, argv_added_short_opt
, &opts
, "", NULL
, unittest_parse_args
,
125 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
128 /* Test valid global and local options. Expected result: PASS */
129 rc
= spdk_app_parse_args(test_argc
, argv_added_short_opt
, &opts
, "z", NULL
, unittest_parse_args
,
131 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_SUCCESS
);
134 /* Test invalid long option Expected result: FAIL */
135 rc
= spdk_app_parse_args(test_argc
, argv_added_long_opt
, &opts
, "", NULL
, unittest_parse_args
,
137 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
140 /* Test valid global and local options. Expected result: PASS */
141 my_options
[0].name
= "test-long-opt";
142 rc
= spdk_app_parse_args(test_argc
, argv_added_long_opt
, &opts
, "", my_options
, unittest_parse_args
,
144 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_SUCCESS
);
147 /* Test overlapping global and local options. Expected result: FAIL */
148 rc
= spdk_app_parse_args(test_argc
, valid_argv
, &opts
, SPDK_APP_GETOPT_STRING
, NULL
,
149 unittest_parse_args
, NULL
);
150 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
153 /* Specify -B and -W options at the same time. Expected result: FAIL */
154 rc
= spdk_app_parse_args(test_argc
, invalid_argv_BW
, &opts
, "", NULL
, unittest_parse_args
, NULL
);
155 SPDK_CU_ASSERT_FATAL(rc
== SPDK_APP_PARSE_ARGS_FAIL
);
158 /* Omit necessary argument to option */
159 rc
= spdk_app_parse_args(test_argc
, invalid_argv_missing_option
, &opts
, "", NULL
,
160 unittest_parse_args
, NULL
);
161 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
166 main(int argc
, char **argv
)
168 CU_pSuite suite
= NULL
;
169 unsigned int num_failures
;
171 if (CU_initialize_registry() != CUE_SUCCESS
) {
172 return CU_get_error();
175 suite
= CU_add_suite("app_suite", NULL
, NULL
);
177 CU_cleanup_registry();
178 return CU_get_error();
182 CU_add_test(suite
, "test_spdk_app_parse_args",
183 test_spdk_app_parse_args
) == NULL
185 CU_cleanup_registry();
186 return CU_get_error();
189 CU_basic_set_mode(CU_BRM_VERBOSE
);
190 CU_basic_run_tests();
191 num_failures
= CU_get_number_of_failures();
192 CU_cleanup_registry();