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