xref: /spdk/test/unit/lib/jsonrpc/jsonrpc_server.c/jsonrpc_server_ut.c (revision 877573897ad52be4fa8989f7617bd655b87e05c4)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk_cunit.h"
9 
10 #include "jsonrpc/jsonrpc_server.c"
11 
12 static struct spdk_jsonrpc_request *g_request;
13 static int g_parse_error;
14 const struct spdk_json_val *g_method;
15 const struct spdk_json_val *g_params;
16 
17 const struct spdk_json_val *g_cur_param;
18 
19 #define PARSE_PASS(in, trailing) \
20 	CU_ASSERT(g_cur_param == NULL); \
21 	g_cur_param = NULL; \
22 	CU_ASSERT(jsonrpc_parse_request(conn, in, sizeof(in) - 1) == sizeof(in) - sizeof(trailing))
23 
24 #define REQ_BEGIN(expected_error) \
25 	if (expected_error != 0 ) { \
26 		CU_ASSERT(g_parse_error == expected_error); \
27 		CU_ASSERT(g_params == NULL); \
28 	}
29 
30 #define PARSE_FAIL(in) \
31 	CU_ASSERT(jsonrpc_parse_request(conn, in, sizeof(in) - 1) < 0);
32 
33 #define REQ_BEGIN_VALID() \
34 		REQ_BEGIN(0); \
35 		SPDK_CU_ASSERT_FATAL(g_params != NULL);
36 
37 #define REQ_BEGIN_INVALID(expected_error) \
38 	REQ_BEGIN(expected_error); \
39 	REQ_METHOD_MISSING(); \
40 	REQ_ID_MISSING(); \
41 	REQ_PARAMS_MISSING()
42 
43 
44 #define REQ_METHOD(name) \
45 	CU_ASSERT(g_method && spdk_json_strequal(g_method, name) == true)
46 
47 #define REQ_METHOD_MISSING() \
48 	CU_ASSERT(g_method == NULL)
49 
50 #define REQ_ID_NUM(num) \
51 	CU_ASSERT(g_request->id && g_request->id->type == SPDK_JSON_VAL_NUMBER); \
52 	CU_ASSERT(g_request->id && memcmp(g_request->id->start, num, sizeof(num) - 1) == 0)
53 
54 
55 #define REQ_ID_STRING(str) \
56 	CU_ASSERT(g_request->id && g_request->id->type == SPDK_JSON_VAL_STRING); \
57 	CU_ASSERT(g_request->id && memcmp(g_request->id->start, num, strlen(num) - 1) == 0))
58 
59 #define REQ_ID_NULL() \
60 	CU_ASSERT(g_request->id && g_request->id->type == SPDK_JSON_VAL_NULL)
61 
62 #define REQ_ID_MISSING() \
63 	CU_ASSERT(g_request->id == NULL)
64 
65 #define REQ_PARAMS_MISSING() \
66 	CU_ASSERT(g_params == NULL)
67 
68 #define REQ_PARAMS_BEGIN() \
69 	SPDK_CU_ASSERT_FATAL(g_params != NULL); \
70 	CU_ASSERT(g_cur_param == NULL); \
71 	g_cur_param = g_params
72 
73 #define PARAM_ARRAY_BEGIN() \
74 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_ARRAY_BEGIN); \
75 	g_cur_param++
76 
77 #define PARAM_ARRAY_END() \
78 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_ARRAY_END); \
79 	g_cur_param++
80 
81 #define PARAM_OBJECT_BEGIN() \
82 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_OBJECT_BEGIN); \
83 	g_cur_param++
84 
85 #define PARAM_OBJECT_END() \
86 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_OBJECT_END); \
87 	g_cur_param++
88 
89 #define PARAM_NUM(num) \
90 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_NUMBER); \
91 	CU_ASSERT(g_cur_param->len == sizeof(num) - 1); \
92 	CU_ASSERT(memcmp(g_cur_param->start, num, sizeof(num) - 1) == 0); \
93 	g_cur_param++
94 
95 #define PARAM_NAME(str) \
96 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_NAME); \
97 	CU_ASSERT(g_cur_param->len == sizeof(str) - 1); \
98 	CU_ASSERT(g_cur_param && memcmp(g_cur_param->start, str, sizeof(str) - 1) == 0); \
99 	g_cur_param++
100 
101 #define PARAM_STRING(str) \
102 	CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_STRING); \
103 	CU_ASSERT(g_cur_param->len == sizeof(str) - 1); \
104 	CU_ASSERT(memcmp(g_cur_param->start, str, g_params->len) == 0); \
105 	g_cur_param++
106 
107 #define FREE_REQUEST() \
108 	ut_jsonrpc_free_request(g_request, g_parse_error); \
109 	g_request = NULL; \
110 	g_cur_param = NULL; \
111 	g_parse_error = 0; \
112 	g_method = NULL; \
113 	g_cur_param = g_params = NULL
114 
115 static void
116 ut_jsonrpc_free_request(struct spdk_jsonrpc_request *request, int err)
117 {
118 	struct spdk_json_write_ctx *w;
119 
120 	if (!request) {
121 		return;
122 	}
123 
124 	/* Need to emulate response to get the response write contex free */
125 	if (err == 0) {
126 		w = spdk_jsonrpc_begin_result(request);
127 		spdk_json_write_string(w, "UT PASS response");
128 		spdk_jsonrpc_end_result(request, w);
129 	} else {
130 		spdk_jsonrpc_send_error_response_fmt(request, err, "UT error response");
131 	}
132 
133 	jsonrpc_free_request(request);
134 }
135 
136 static void
137 ut_handle(struct spdk_jsonrpc_request *request, int error, const struct spdk_json_val *method,
138 	  const struct spdk_json_val *params)
139 {
140 	CU_ASSERT(g_request == NULL);
141 	g_request = request;
142 	g_parse_error = error;
143 	g_method = method;
144 	g_params = params;
145 }
146 
147 void
148 jsonrpc_server_handle_error(struct spdk_jsonrpc_request *request, int error)
149 {
150 	ut_handle(request, error, NULL, NULL);
151 }
152 
153 void
154 jsonrpc_server_handle_request(struct spdk_jsonrpc_request *request,
155 			      const struct spdk_json_val *method, const struct spdk_json_val *params)
156 {
157 	ut_handle(request, 0, method, params);
158 }
159 
160 void
161 jsonrpc_server_send_response(struct spdk_jsonrpc_request *request)
162 {
163 }
164 
165 static void
166 test_parse_request(void)
167 {
168 	struct spdk_jsonrpc_server *server;
169 	struct spdk_jsonrpc_server_conn *conn;
170 
171 	server = calloc(1, sizeof(*server));
172 	SPDK_CU_ASSERT_FATAL(server != NULL);
173 
174 	conn = calloc(1, sizeof(*conn));
175 	SPDK_CU_ASSERT_FATAL(conn != NULL);
176 
177 	conn->server = server;
178 
179 	/* rpc call with no parameters. */
180 	PARSE_PASS("{   }", "");
181 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
182 	FREE_REQUEST();
183 
184 	/* rpc call with method that is not a string. */
185 	PARSE_PASS("{\"jsonrpc\":\"2.0\", \"method\": null  }", "");
186 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
187 	FREE_REQUEST();
188 
189 	/* rpc call with invalid JSON RPC version. */
190 	PARSE_PASS("{\"jsonrpc\":\"42\", \"method\": \"subtract\"}", "");
191 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
192 	FREE_REQUEST();
193 
194 	/* rpc call with embedded zeros. */
195 	PARSE_FAIL("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":{\"bar\": \"\0\0baz\"}}");
196 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_PARSE_ERROR);
197 	FREE_REQUEST();
198 
199 	/* rpc call with positional parameters */
200 	PARSE_PASS("{\"jsonrpc\":\"2.0\",\"method\":\"subtract\",\"params\":[42,23],\"id\":1}", "");
201 	REQ_BEGIN_VALID();
202 	REQ_METHOD("subtract");
203 	REQ_ID_NUM("1");
204 	REQ_PARAMS_BEGIN();
205 	PARAM_ARRAY_BEGIN();
206 	PARAM_NUM("42");
207 	PARAM_NUM("23");
208 	PARAM_ARRAY_END();
209 	FREE_REQUEST();
210 
211 	/* rpc call with named parameters */
212 	PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": {\"subtrahend\": 23, \"minuend\": 42}, \"id\": 3}",
213 		   "");
214 	REQ_BEGIN_VALID();
215 	REQ_METHOD("subtract");
216 	REQ_ID_NUM("3");
217 	REQ_PARAMS_BEGIN();
218 	PARAM_OBJECT_BEGIN();
219 	PARAM_NAME("subtrahend");
220 	PARAM_NUM("23");
221 	PARAM_NAME("minuend");
222 	PARAM_NUM("42");
223 	PARAM_OBJECT_END();
224 	FREE_REQUEST();
225 
226 	/* notification */
227 	PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": \"update\", \"params\": [1,2,3,4,5]}", "");
228 	REQ_BEGIN_VALID();
229 	REQ_METHOD("update");
230 	REQ_ID_MISSING();
231 	REQ_PARAMS_BEGIN();
232 	PARAM_ARRAY_BEGIN();
233 	PARAM_NUM("1");
234 	PARAM_NUM("2");
235 	PARAM_NUM("3");
236 	PARAM_NUM("4");
237 	PARAM_NUM("5");
238 	PARAM_ARRAY_END();
239 	FREE_REQUEST();
240 
241 	/* notification with explicit NULL ID. This is discouraged by JSON RPC spec but allowed. */
242 	PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": \"update\", \"params\": [1,2,3,4,5], \"id\": null}",
243 		   "");
244 	REQ_BEGIN_VALID();
245 	REQ_METHOD("update");
246 	REQ_ID_NULL();
247 	REQ_PARAMS_BEGIN();
248 	PARAM_ARRAY_BEGIN();
249 	PARAM_NUM("1");
250 	PARAM_NUM("2");
251 	PARAM_NUM("3");
252 	PARAM_NUM("4");
253 	PARAM_NUM("5");
254 	PARAM_ARRAY_END();
255 	FREE_REQUEST();
256 
257 	/* invalid JSON */
258 	PARSE_FAIL("{\"jsonrpc\": \"2.0\", \"method\": \"foobar, \"params\": \"bar\", \"baz]");
259 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_PARSE_ERROR);
260 	FREE_REQUEST();
261 
262 	/* invalid request (method must be a string; params must be array or object) */
263 	PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": 1, \"params\": \"bar\"}", "");
264 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
265 	FREE_REQUEST();
266 
267 	/* batch, invalid JSON */
268 	PARSE_FAIL(
269 		"["
270 		"{\"jsonrpc\": \"2.0\", \"method\": \"sum\", \"params\": [1,2,4], \"id\": \"1\"},"
271 		"{\"jsonrpc\": \"2.0\", \"method\""
272 		"]");
273 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_PARSE_ERROR);
274 	FREE_REQUEST();
275 
276 	/* empty array */
277 	PARSE_PASS("[]", "");
278 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
279 	FREE_REQUEST();
280 
281 	/* batch - not supported */
282 	PARSE_PASS(
283 		"["
284 		"{\"jsonrpc\": \"2.0\", \"method\": \"sum\", \"params\": [1,2,4], \"id\": \"1\"},"
285 		"{\"jsonrpc\": \"2.0\", \"method\": \"notify_hello\", \"params\": [7]},"
286 		"{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42,23], \"id\": \"2\"},"
287 		"{\"foo\": \"boo\"},"
288 		"{\"jsonrpc\": \"2.0\", \"method\": \"foo.get\", \"params\": {\"name\": \"myself\"}, \"id\": \"5\"},"
289 		"{\"jsonrpc\": \"2.0\", \"method\": \"get_data\", \"id\": \"9\"}"
290 		"]", "");
291 
292 	REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
293 	FREE_REQUEST();
294 
295 	CU_ASSERT(conn->outstanding_requests == 0);
296 	free(conn);
297 	free(server);
298 }
299 
300 static void
301 test_parse_request_streaming(void)
302 {
303 	struct spdk_jsonrpc_server *server;
304 	struct spdk_jsonrpc_server_conn *conn;
305 	const char *json_req;
306 	size_t len, i;
307 
308 	server = calloc(1, sizeof(*server));
309 	SPDK_CU_ASSERT_FATAL(server != NULL);
310 
311 	conn = calloc(1, sizeof(*conn));
312 	SPDK_CU_ASSERT_FATAL(conn != NULL);
313 
314 	conn->server = server;
315 
316 
317 	/*
318 	 * Two valid requests end to end in the same buffer.
319 	 * Parse should return the first one and point to the beginning of the second one.
320 	 */
321 	PARSE_PASS(
322 		"{\"jsonrpc\":\"2.0\",\"method\":\"a\",\"params\":[1],\"id\":1}"
323 		"{\"jsonrpc\":\"2.0\",\"method\":\"b\",\"params\":[2],\"id\":2}",
324 		"{\"jsonrpc\":\"2.0\",\"method\":\"b\",\"params\":[2],\"id\":2}");
325 
326 	REQ_BEGIN_VALID();
327 	REQ_METHOD("a");
328 	REQ_ID_NUM("1");
329 	REQ_PARAMS_BEGIN();
330 	PARAM_ARRAY_BEGIN();
331 	PARAM_NUM("1");
332 	PARAM_ARRAY_END();
333 	FREE_REQUEST();
334 
335 	/* Partial (but not invalid) requests - parse should not consume anything. */
336 	json_req = "    {\"jsonrpc\":\"2.0\",\"method\":\"b\",\"params\":[2],\"id\":2}";
337 	len = strlen(json_req);
338 
339 	/* Try every partial length up to the full request length */
340 	for (i = 0; i < len; i++) {
341 		int rc = jsonrpc_parse_request(conn, json_req, i);
342 		/* Partial request - no data consumed */
343 		CU_ASSERT(rc == 0);
344 		CU_ASSERT(g_request == NULL);
345 
346 		/* In case of failed, don't fload console with useless CU assert fails. */
347 		FREE_REQUEST();
348 	}
349 
350 	/* Verify that full request can be parsed successfully */
351 	CU_ASSERT(jsonrpc_parse_request(conn, json_req, len) == (ssize_t)len);
352 	FREE_REQUEST();
353 
354 	CU_ASSERT(conn->outstanding_requests == 0);
355 	free(conn);
356 	free(server);
357 }
358 
359 int
360 main(int argc, char **argv)
361 {
362 	CU_pSuite	suite = NULL;
363 	unsigned int	num_failures;
364 
365 	CU_set_error_action(CUEA_ABORT);
366 	CU_initialize_registry();
367 
368 	suite = CU_add_suite("jsonrpc", NULL, NULL);
369 
370 	CU_ADD_TEST(suite, test_parse_request);
371 	CU_ADD_TEST(suite, test_parse_request_streaming);
372 	CU_basic_set_mode(CU_BRM_VERBOSE);
373 
374 	CU_basic_run_tests();
375 
376 	num_failures = CU_get_number_of_failures();
377 	CU_cleanup_registry();
378 
379 	/* This is for ASAN. Don't know why but if pointer is left in global variable
380 	 * it won't be detected as leak. */
381 	g_request = NULL;
382 	return num_failures;
383 }
384