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_event_call
, (struct spdk_event
*event
));
43 DEFINE_STUB(spdk_event_allocate
, struct spdk_event
*, (uint32_t core
, spdk_event_fn fn
, void *arg1
,
45 DEFINE_STUB(spdk_env_get_current_core
, uint32_t, (void), 0);
46 DEFINE_STUB_V(spdk_subsystem_init
, (spdk_msg_fn cb_fn
, void *cb_arg
));
47 DEFINE_STUB_V(spdk_rpc_register_method
, (const char *method
, spdk_rpc_method_handler func
,
48 uint32_t state_mask
));
49 DEFINE_STUB_V(spdk_rpc_set_state
, (uint32_t state
));
50 DEFINE_STUB(spdk_rpc_get_state
, uint32_t, (void), SPDK_RPC_RUNTIME
);
51 DEFINE_STUB_V(spdk_app_json_config_load
, (const char *json_config_file
, const char *rpc_addr
,
52 spdk_msg_fn cb_fn
, void *cb_arg
));
60 unittest_parse_args(int ch
, char *arg
)
66 clean_opts(struct spdk_app_opts
*opts
)
68 free(opts
->pci_whitelist
);
69 opts
->pci_whitelist
= NULL
;
70 free(opts
->pci_blacklist
);
71 opts
->pci_blacklist
= NULL
;
72 memset(opts
, 0, sizeof(struct spdk_app_opts
));
76 test_spdk_app_parse_args(void)
78 spdk_app_parse_args_rvals_t rc
;
79 struct spdk_app_opts opts
= {};
80 struct option my_options
[2] = {};
81 char *valid_argv
[test_argc
] = {"app_ut",
88 char *invalid_argv_BW
[test_argc
] = {"app_ut",
95 /* currently use -z as our new option */
96 char *argv_added_short_opt
[test_argc
] = {"app_ut",
103 char *argv_added_long_opt
[test_argc
] = {"app_ut",
106 "-r/var/tmp/spdk.sock",
110 char *invalid_argv_missing_option
[test_argc
] = {"app_ut",
114 "--silence-noticelog"
118 /* Test valid arguments. Expected result: PASS */
119 rc
= spdk_app_parse_args(test_argc
, valid_argv
, &opts
, "", NULL
, unittest_parse_args
, NULL
);
120 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_SUCCESS
);
124 /* Test invalid short option Expected result: FAIL */
125 rc
= spdk_app_parse_args(test_argc
, argv_added_short_opt
, &opts
, "", NULL
, unittest_parse_args
,
127 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
131 /* Test valid global and local options. Expected result: PASS */
132 rc
= spdk_app_parse_args(test_argc
, argv_added_short_opt
, &opts
, "z", NULL
, unittest_parse_args
,
134 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_SUCCESS
);
138 /* Test invalid long option Expected result: FAIL */
139 rc
= spdk_app_parse_args(test_argc
, argv_added_long_opt
, &opts
, "", NULL
, unittest_parse_args
,
141 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
145 /* Test valid global and local options. Expected result: PASS */
146 my_options
[0].name
= "test-long-opt";
147 rc
= spdk_app_parse_args(test_argc
, argv_added_long_opt
, &opts
, "", my_options
, unittest_parse_args
,
149 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_SUCCESS
);
153 /* Test overlapping global and local options. Expected result: FAIL */
154 rc
= spdk_app_parse_args(test_argc
, valid_argv
, &opts
, SPDK_APP_GETOPT_STRING
, NULL
,
155 unittest_parse_args
, NULL
);
156 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
160 /* Specify -B and -W options at the same time. Expected result: FAIL */
161 rc
= spdk_app_parse_args(test_argc
, invalid_argv_BW
, &opts
, "", NULL
, unittest_parse_args
, NULL
);
162 SPDK_CU_ASSERT_FATAL(rc
== SPDK_APP_PARSE_ARGS_FAIL
);
166 /* Omit necessary argument to option */
167 rc
= spdk_app_parse_args(test_argc
, invalid_argv_missing_option
, &opts
, "", NULL
,
168 unittest_parse_args
, NULL
);
169 CU_ASSERT_EQUAL(rc
, SPDK_APP_PARSE_ARGS_FAIL
);
175 main(int argc
, char **argv
)
177 CU_pSuite suite
= NULL
;
178 unsigned int num_failures
;
180 if (CU_initialize_registry() != CUE_SUCCESS
) {
181 return CU_get_error();
184 suite
= CU_add_suite("app_suite", NULL
, NULL
);
186 CU_cleanup_registry();
187 return CU_get_error();
191 CU_add_test(suite
, "test_spdk_app_parse_args",
192 test_spdk_app_parse_args
) == NULL
194 CU_cleanup_registry();
195 return CU_get_error();
198 CU_basic_set_mode(CU_BRM_VERBOSE
);
199 CU_basic_run_tests();
200 num_failures
= CU_get_number_of_failures();
201 CU_cleanup_registry();