xref: /spdk/test/unit/lib/rpc/rpc.c/rpc_ut.c (revision 6e5d6032a09ca918509e7c6f28d6d2e20b8dc832)
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 #include "spdk_cunit.h"
36 #include "spdk/jsonrpc.h"
37 #include "common/lib/test_env.c"
38 #include "spdk/log.h"
39 
40 #include "rpc/rpc.c"
41 
42 static int g_rpc_err;
43 void fn_rpc_method_handler(struct spdk_jsonrpc_request *request,
44 			   const struct spdk_json_val *params);
45 
46 DEFINE_STUB_V(spdk_jsonrpc_end_result, (struct spdk_jsonrpc_request *request,
47 					struct spdk_json_write_ctx *w));
48 DEFINE_STUB(spdk_json_write_array_begin, int, (struct spdk_json_write_ctx *w), 0);
49 DEFINE_STUB(spdk_json_write_string, int, (struct spdk_json_write_ctx *w, const char *val), 0);
50 DEFINE_STUB(spdk_json_write_object_begin, int, (struct spdk_json_write_ctx *w), 0);
51 DEFINE_STUB(spdk_json_write_named_string_fmt, int, (struct spdk_json_write_ctx *w, const char *name,
52 		const char *fmt, ...), 0);
53 DEFINE_STUB(spdk_json_write_named_object_begin, int, (struct spdk_json_write_ctx *w,
54 		const char *name), 0);
55 DEFINE_STUB(spdk_json_write_named_uint32, int, (struct spdk_json_write_ctx *w, const char *name,
56 		uint32_t val), 0);
57 DEFINE_STUB(spdk_json_write_object_end, int, (struct spdk_json_write_ctx *w), 0);
58 DEFINE_STUB(spdk_json_write_array_end, int, (struct spdk_json_write_ctx *w), 0);
59 DEFINE_STUB(spdk_jsonrpc_begin_result, struct spdk_json_write_ctx *,
60 	    (struct spdk_jsonrpc_request *request), (void *)1);
61 DEFINE_STUB(spdk_json_decode_bool, int, (const struct spdk_json_val *val, void *out), 0);
62 DEFINE_STUB(spdk_jsonrpc_server_listen, struct spdk_jsonrpc_server *, (int domain, int protocol,
63 		struct sockaddr *listen_addr, socklen_t addrlen, spdk_jsonrpc_handle_request_fn handle_request),
64 	    (struct spdk_jsonrpc_server *)0Xdeaddead);
65 DEFINE_STUB(spdk_jsonrpc_server_poll, int, (struct spdk_jsonrpc_server *server), 0);
66 DEFINE_STUB_V(spdk_jsonrpc_server_shutdown, (struct spdk_jsonrpc_server *server));
67 
68 int spdk_json_decode_object(const struct spdk_json_val *values,
69 			    const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
70 {
71 	if (values ->type == SPDK_JSON_VAL_INVALID) {
72 		return 1;
73 	}
74 	return 0;
75 }
76 
77 bool
78 spdk_json_strequal(const struct spdk_json_val *val, const char *str)
79 {
80 	size_t len;
81 
82 	if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
83 		return false;
84 	}
85 
86 	len = strlen(str);
87 	if (val->len != len) {
88 		return false;
89 	}
90 
91 	return memcmp(val->start, str, len) == 0;
92 }
93 
94 void
95 spdk_jsonrpc_send_error_response(struct spdk_jsonrpc_request *request,
96 				 int error_code, const char *msg)
97 {
98 	g_rpc_err = error_code;
99 }
100 
101 void
102 spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request,
103 				     int error_code, const char *fmt, ...)
104 {
105 	g_rpc_err = error_code;
106 }
107 
108 void fn_rpc_method_handler(struct spdk_jsonrpc_request *request,
109 			   const struct spdk_json_val *params)
110 {
111 	g_rpc_err = 0;
112 }
113 
114 static void
115 test_jsonrpc_handler(void)
116 {
117 	struct spdk_jsonrpc_request *request = (struct spdk_jsonrpc_request *)0xdeadbeef;
118 	struct spdk_json_val method = {};
119 	struct spdk_json_val params = {};
120 	char *str = "test";
121 	struct spdk_rpc_method m = {
122 		.name = "test",
123 	};
124 
125 	struct spdk_rpc_method is_alias_of = {
126 		.name = "aliastest",
127 		.is_deprecated = false,
128 		.deprecation_warning_printed = false,
129 		.func = fn_rpc_method_handler,
130 		.state_mask = SPDK_RPC_STARTUP,
131 	};
132 
133 	/* Case 1: Method not found */
134 	method.type = SPDK_JSON_VAL_INVALID;
135 	jsonrpc_handler(request, &method, &params);
136 	CU_ASSERT(g_rpc_err == SPDK_JSONRPC_ERROR_METHOD_NOT_FOUND);
137 
138 	/* Case 2:  Method is alias */
139 	method.type = SPDK_JSON_VAL_STRING;
140 	method.start = str;
141 	method.len = 4;
142 	m.is_alias_of = &is_alias_of;
143 	m.is_deprecated = true;
144 	m.deprecation_warning_printed = false;
145 	m.state_mask = SPDK_RPC_STARTUP;
146 	SLIST_INSERT_HEAD(&g_rpc_methods, &m, slist);
147 
148 	/* m->state_mask & g_rpc_state == g_rpc_state */
149 	g_rpc_err = -1;
150 	g_rpc_state = SPDK_RPC_STARTUP;
151 	jsonrpc_handler(request, &method, &params);
152 	CU_ASSERT(g_rpc_err == 0);
153 
154 	/* g_rpc_state == SPDK_RPC_STARTUP */
155 	is_alias_of.state_mask = SPDK_RPC_RUNTIME;
156 	g_rpc_err = -1;
157 	g_rpc_state = SPDK_RPC_STARTUP;
158 	jsonrpc_handler(request, &method, &params);
159 	CU_ASSERT(g_rpc_err == SPDK_JSONRPC_ERROR_INVALID_STATE);
160 
161 	/* SPDK_RPC_RUNTIME is invalid for the aliastest RPC */
162 	is_alias_of.state_mask = SPDK_RPC_STARTUP;
163 	g_rpc_err = -1;
164 	g_rpc_state = SPDK_RPC_RUNTIME;
165 	jsonrpc_handler(request, &method, &params);
166 	CU_ASSERT(g_rpc_err == SPDK_JSONRPC_ERROR_INVALID_STATE);
167 
168 	SLIST_REMOVE_HEAD(&g_rpc_methods, slist);
169 }
170 
171 static void
172 test_spdk_rpc_is_method_allowed(void)
173 {
174 	const char method[] = "test";
175 	uint32_t state_mask = SPDK_RPC_STARTUP;
176 	struct spdk_rpc_method m = {};
177 	int rc = 0;
178 
179 	/* Case 1: Expect return -EPERM */
180 	m.name = method;
181 	m.state_mask = SPDK_RPC_RUNTIME;
182 	SLIST_INSERT_HEAD(&g_rpc_methods, &m, slist);
183 	rc = spdk_rpc_is_method_allowed(method, state_mask);
184 	CU_ASSERT(rc == -EPERM);
185 
186 	/* Case 2: Expect return 0 */
187 	state_mask = SPDK_RPC_RUNTIME;
188 	rc = spdk_rpc_is_method_allowed(method, state_mask);
189 	CU_ASSERT(rc == 0);
190 
191 	/* Case 3: Expect return -ENOENT */
192 	SLIST_REMOVE_HEAD(&g_rpc_methods, slist);
193 	rc = spdk_rpc_is_method_allowed(method, state_mask);
194 	CU_ASSERT(rc == -ENOENT);
195 }
196 
197 static void
198 test_rpc_get_methods(void)
199 {
200 	struct spdk_jsonrpc_request *request = (struct spdk_jsonrpc_request *)0xbeefbeef;
201 	struct spdk_json_val params = {};
202 	struct spdk_rpc_method m = {};
203 
204 	/* Case 1: spdk_json_decode_object failed */
205 	g_rpc_err = -1;
206 	params.type = SPDK_JSON_VAL_INVALID;
207 	rpc_get_methods(request, &params);
208 	CU_ASSERT(g_rpc_err == SPDK_JSONRPC_ERROR_INVALID_PARAMS);
209 
210 	/* Case 2: Expect pass */
211 	params.type = SPDK_JSON_VAL_TRUE;
212 	m.state_mask = SPDK_RPC_RUNTIME;
213 	g_rpc_state = SPDK_RPC_STARTUP;
214 	SLIST_INSERT_HEAD(&g_rpc_methods, &m, slist);
215 	rpc_get_methods(request, &params);
216 	SLIST_REMOVE_HEAD(&g_rpc_methods, slist);
217 }
218 
219 static  void
220 test_rpc_spdk_get_version(void)
221 {
222 	struct spdk_jsonrpc_request *request = (struct spdk_jsonrpc_request *)0xdeadbeef;
223 	struct spdk_json_val params = {};
224 
225 	/* Case 1: spdk_get_version method requires no parameters */
226 	g_rpc_err = -1;
227 	params.type = SPDK_JSON_VAL_INVALID;
228 	rpc_spdk_get_version(request, &params);
229 	CU_ASSERT(g_rpc_err == SPDK_JSONRPC_ERROR_INVALID_PARAMS);
230 
231 	/* Case 2: Expect pass */
232 	rpc_spdk_get_version(request, NULL);
233 }
234 
235 static void
236 test_spdk_rpc_listen_close(void)
237 {
238 	const char listen_addr[128] = "10.67.12.34";
239 	char rpc_lock_path[128] = {};
240 
241 	spdk_rpc_listen(listen_addr);
242 	snprintf(rpc_lock_path, sizeof(g_rpc_lock_path), "%s.lock",
243 		 g_rpc_listen_addr_unix.sun_path);
244 
245 	CU_ASSERT(g_rpc_listen_addr_unix.sun_family == AF_UNIX);
246 	CU_ASSERT(strcmp(g_rpc_listen_addr_unix.sun_path, listen_addr) == 0);
247 	CU_ASSERT(strcmp(g_rpc_lock_path, rpc_lock_path) == 0);
248 	CU_ASSERT(g_jsonrpc_server == (struct spdk_jsonrpc_server *)0Xdeaddead);
249 
250 	spdk_rpc_close();
251 
252 	CU_ASSERT(g_rpc_listen_addr_unix.sun_path[0] == '\0');
253 	CU_ASSERT(g_jsonrpc_server == NULL);
254 	CU_ASSERT(g_rpc_lock_fd == -1);
255 	CU_ASSERT(g_rpc_lock_path[0] == '\0');
256 }
257 
258 int main(int argc, char **argv)
259 {
260 	CU_pSuite	suite = NULL;
261 	unsigned int	num_failures;
262 
263 	CU_set_error_action(CUEA_ABORT);
264 	CU_initialize_registry();
265 
266 	suite = CU_add_suite("rpc", NULL, NULL);
267 
268 	CU_ADD_TEST(suite, test_jsonrpc_handler);
269 	CU_ADD_TEST(suite, test_spdk_rpc_is_method_allowed);
270 	CU_ADD_TEST(suite, test_rpc_get_methods);
271 	CU_ADD_TEST(suite, test_rpc_spdk_get_version);
272 	CU_ADD_TEST(suite, test_spdk_rpc_listen_close);
273 
274 	CU_basic_set_mode(CU_BRM_VERBOSE);
275 	CU_basic_run_tests();
276 	num_failures = CU_get_number_of_failures();
277 	CU_cleanup_registry();
278 	return num_failures;
279 }
280