xref: /spdk/test/unit/lib/event/app.c/app_ut.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
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_V(spdk_subsystem_init, (spdk_subsystem_init_fn cb_fn, void *cb_arg));
46  DEFINE_STUB_V(spdk_subsystem_fini, (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_register_alias_deprecated, (const char *method, const char *alias));
50  DEFINE_STUB_V(spdk_rpc_set_state, (uint32_t state));
51  DEFINE_STUB(spdk_rpc_get_state, uint32_t, (void), SPDK_RPC_RUNTIME);
52  DEFINE_STUB_V(spdk_rpc_initialize, (const char *listen_addr));
53  DEFINE_STUB_V(spdk_rpc_finish, (void));
54  DEFINE_STUB_V(spdk_app_json_config_load, (const char *json_config_file, const char *rpc_addr,
55  		spdk_subsystem_init_fn cb_fn, void *cb_arg, bool stop_on_error));
56  DEFINE_STUB_V(spdk_reactors_start, (void));
57  DEFINE_STUB_V(spdk_reactors_stop, (void *arg1));
58  DEFINE_STUB(spdk_reactors_init, int, (void), 0);
59  DEFINE_STUB_V(spdk_reactors_fini, (void));
60  
61  static void
62  unittest_usage(void)
63  {
64  }
65  
66  static int
67  unittest_parse_args(int ch, char *arg)
68  {
69  	return 0;
70  }
71  
72  static void
73  clean_opts(struct spdk_app_opts *opts)
74  {
75  	free(opts->pci_allowed);
76  	opts->pci_allowed = NULL;
77  	free(opts->pci_blocked);
78  	opts->pci_blocked = NULL;
79  	memset(opts, 0, sizeof(struct spdk_app_opts));
80  }
81  
82  static void
83  test_spdk_app_parse_args(void)
84  {
85  	spdk_app_parse_args_rvals_t rc;
86  	struct spdk_app_opts opts = {};
87  	struct option my_options[2] = {};
88  	char *valid_argv[test_argc] = {"app_ut",
89  				       "--single-file-segments",
90  				       "-d",
91  				       "-p0",
92  				       "-B",
93  				       "0000:81:00.0"
94  				      };
95  	char *invalid_argv_BW[test_argc] = {"app_ut",
96  					    "-B",
97  					    "0000:81:00.0",
98  					    "-W",
99  					    "0000:82:00.0",
100  					    "-cspdk.conf"
101  					   };
102  	/* currently use -z as our new option */
103  	char *argv_added_short_opt[test_argc] = {"app_ut",
104  						 "-z",
105  						 "-d",
106  						 "--single-file-segments",
107  						 "-p0",
108  						 "-cspdk.conf"
109  						};
110  	char *argv_added_long_opt[test_argc] = {"app_ut",
111  						"-cspdk.conf",
112  						"-d",
113  						"-r/var/tmp/spdk.sock",
114  						"--test-long-opt",
115  						"--single-file-segments"
116  					       };
117  	char *invalid_argv_missing_option[test_argc] = {"app_ut",
118  							"-d",
119  							"-p",
120  							"--single-file-segments",
121  							"--silence-noticelog"
122  							"-R"
123  						       };
124  
125  	/* Test valid arguments. Expected result: PASS */
126  	rc = spdk_app_parse_args(test_argc, valid_argv, &opts, "", NULL, unittest_parse_args, NULL);
127  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
128  	optind = 1;
129  	clean_opts(&opts);
130  
131  	/* Test invalid short option Expected result: FAIL */
132  	rc = spdk_app_parse_args(test_argc, argv_added_short_opt, &opts, "", NULL, unittest_parse_args,
133  				 NULL);
134  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
135  	optind = 1;
136  	clean_opts(&opts);
137  
138  	/* Test valid global and local options. Expected result: PASS */
139  	rc = spdk_app_parse_args(test_argc, argv_added_short_opt, &opts, "z", NULL, unittest_parse_args,
140  				 unittest_usage);
141  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
142  	optind = 1;
143  	clean_opts(&opts);
144  
145  	/* Test invalid long option Expected result: FAIL */
146  	rc = spdk_app_parse_args(test_argc, argv_added_long_opt, &opts, "", NULL, unittest_parse_args,
147  				 NULL);
148  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
149  	optind = 1;
150  	clean_opts(&opts);
151  
152  	/* Test valid global and local options. Expected result: PASS */
153  	my_options[0].name = "test-long-opt";
154  	rc = spdk_app_parse_args(test_argc, argv_added_long_opt, &opts, "", my_options, unittest_parse_args,
155  				 unittest_usage);
156  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
157  	optind = 1;
158  	clean_opts(&opts);
159  
160  	/* Test overlapping global and local options. Expected result: FAIL */
161  	rc = spdk_app_parse_args(test_argc, valid_argv, &opts, SPDK_APP_GETOPT_STRING, NULL,
162  				 unittest_parse_args, NULL);
163  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
164  	optind = 1;
165  	clean_opts(&opts);
166  
167  	/* Specify -B and -W options at the same time. Expected result: FAIL */
168  	rc = spdk_app_parse_args(test_argc, invalid_argv_BW, &opts, "", NULL, unittest_parse_args, NULL);
169  	SPDK_CU_ASSERT_FATAL(rc == SPDK_APP_PARSE_ARGS_FAIL);
170  	optind = 1;
171  	clean_opts(&opts);
172  
173  	/* Omit necessary argument to option */
174  	rc = spdk_app_parse_args(test_argc, invalid_argv_missing_option, &opts, "", NULL,
175  				 unittest_parse_args, NULL);
176  	CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
177  	optind = 1;
178  	clean_opts(&opts);
179  }
180  
181  int
182  main(int argc, char **argv)
183  {
184  	CU_pSuite suite = NULL;
185  	unsigned int num_failures;
186  
187  	CU_set_error_action(CUEA_ABORT);
188  	CU_initialize_registry();
189  
190  	suite = CU_add_suite("app_suite", NULL, NULL);
191  
192  	CU_ADD_TEST(suite, test_spdk_app_parse_args);
193  
194  	CU_basic_set_mode(CU_BRM_VERBOSE);
195  	CU_basic_run_tests();
196  	num_failures = CU_get_number_of_failures();
197  	CU_cleanup_registry();
198  
199  	return num_failures;
200  }
201