xref: /spdk/test/unit/lib/event/app.c/app_ut.c (revision fa2d95b3fe66e7f5c543eaef89fa00d4eaa0e6e7)
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_event_call, (struct spdk_event *event));
43 DEFINE_STUB(spdk_event_allocate, struct spdk_event *, (uint32_t core, spdk_event_fn fn, void *arg1,
44 		void *arg2), NULL);
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));
53 
54 static void
55 unittest_usage(void)
56 {
57 }
58 
59 static int
60 unittest_parse_args(int ch, char *arg)
61 {
62 	return 0;
63 }
64 
65 static void
66 clean_opts(struct spdk_app_opts *opts)
67 {
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));
73 }
74 
75 static void
76 test_spdk_app_parse_args(void)
77 {
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",
82 				       "--wait-for-rpc",
83 				       "-d",
84 				       "-p0",
85 				       "-B",
86 				       "0000:81:00.0"
87 				      };
88 	char *invalid_argv_BW[test_argc] = {"app_ut",
89 					    "-B",
90 					    "0000:81:00.0",
91 					    "-W",
92 					    "0000:82:00.0",
93 					    "-cspdk.conf"
94 					   };
95 	/* currently use -z as our new option */
96 	char *argv_added_short_opt[test_argc] = {"app_ut",
97 						 "-z",
98 						 "-d",
99 						 "--wait-for-rpc",
100 						 "-p0",
101 						 "-cspdk.conf"
102 						};
103 	char *argv_added_long_opt[test_argc] = {"app_ut",
104 						"-cspdk.conf",
105 						"-d",
106 						"-r/var/tmp/spdk.sock",
107 						"--test-long-opt",
108 						"--wait-for-rpc"
109 					       };
110 	char *invalid_argv_missing_option[test_argc] = {"app_ut",
111 							"-d",
112 							"-p",
113 							"--wait-for-rpc",
114 							"--silence-noticelog"
115 							"-R"
116 						       };
117 
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);
121 	optind = 1;
122 	clean_opts(&opts);
123 
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,
126 				 NULL);
127 	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
128 	optind = 1;
129 	clean_opts(&opts);
130 
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,
133 				 unittest_usage);
134 	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
135 	optind = 1;
136 	clean_opts(&opts);
137 
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,
140 				 NULL);
141 	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
142 	optind = 1;
143 	clean_opts(&opts);
144 
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,
148 				 unittest_usage);
149 	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
150 	optind = 1;
151 	clean_opts(&opts);
152 
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);
157 	optind = 1;
158 	clean_opts(&opts);
159 
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);
163 	optind = 1;
164 	clean_opts(&opts);
165 
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);
170 	optind = 1;
171 	clean_opts(&opts);
172 }
173 
174 int
175 main(int argc, char **argv)
176 {
177 	CU_pSuite suite = NULL;
178 	unsigned int num_failures;
179 
180 	if (CU_initialize_registry() != CUE_SUCCESS) {
181 		return CU_get_error();
182 	}
183 
184 	suite = CU_add_suite("app_suite", NULL, NULL);
185 	if (suite == NULL) {
186 		CU_cleanup_registry();
187 		return CU_get_error();
188 	}
189 
190 	if (
191 		CU_add_test(suite, "test_spdk_app_parse_args",
192 			    test_spdk_app_parse_args) == NULL
193 	) {
194 		CU_cleanup_registry();
195 		return CU_get_error();
196 	}
197 
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();
202 
203 	return num_failures;
204 }
205