]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/unit/lib/event/app.c/app_ut.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / test / unit / lib / event / app.c / app_ut.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33
34 #include "spdk/stdinc.h"
35
36 #include "spdk_cunit.h"
37 #include "common/lib/test_env.c"
38 #include "event/app.c"
39
40 #define test_argc 6
41
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,
50 void *arg2), NULL);
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));
62
63
64 static void
65 unittest_usage(void)
66 {
67 }
68
69 static void
70 unittest_parse_args(int ch, char *arg)
71 {
72 }
73
74 static void
75 test_spdk_app_parse_args(void)
76 {
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",
81 "--wait-for-rpc",
82 "-d",
83 "-p0",
84 "-B",
85 "0000:81:00.0"
86 };
87 char *invalid_argv_BW[test_argc] = {"app_ut",
88 "-B",
89 "0000:81:00.0",
90 "-W",
91 "0000:82:00.0",
92 "-cspdk.conf"
93 };
94 /* currently use -z as our new option */
95 char *argv_added_short_opt[test_argc] = {"app_ut",
96 "-z",
97 "-d",
98 "--wait-for-rpc",
99 "-p0",
100 "-cspdk.conf"
101 };
102 char *argv_added_long_opt[test_argc] = {"app_ut",
103 "-cspdk.conf",
104 "-d",
105 "-r/var/tmp/spdk.sock",
106 "--test-long-opt",
107 "--wait-for-rpc"
108 };
109 char *invalid_argv_missing_option[test_argc] = {"app_ut",
110 "-d",
111 "-p",
112 "--wait-for-rpc",
113 "--silence-noticelog"
114 "-R"
115 };
116
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);
120 optind = 1;
121
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,
124 NULL);
125 CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
126 optind = 1;
127
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,
130 unittest_usage);
131 CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
132 optind = 1;
133
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,
136 NULL);
137 CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
138 optind = 1;
139
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,
143 unittest_usage);
144 CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
145 optind = 1;
146
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);
151 optind = 1;
152
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);
156 optind = 1;
157
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);
162 optind = 1;
163 }
164
165 int
166 main(int argc, char **argv)
167 {
168 CU_pSuite suite = NULL;
169 unsigned int num_failures;
170
171 if (CU_initialize_registry() != CUE_SUCCESS) {
172 return CU_get_error();
173 }
174
175 suite = CU_add_suite("app_suite", NULL, NULL);
176 if (suite == NULL) {
177 CU_cleanup_registry();
178 return CU_get_error();
179 }
180
181 if (
182 CU_add_test(suite, "test_spdk_app_parse_args",
183 test_spdk_app_parse_args) == NULL
184 ) {
185 CU_cleanup_registry();
186 return CU_get_error();
187 }
188
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();
193
194 return num_failures;
195 }