1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "spdk/stdinc.h"
#include "spdk_cunit.h"
#include "jsonrpc/jsonrpc_server.c"
static struct spdk_jsonrpc_request *g_request;
static int g_parse_error;
const struct spdk_json_val *g_method;
const struct spdk_json_val *g_params;
const struct spdk_json_val *g_cur_param;
#define PARSE_PASS(in, trailing) \
CU_ASSERT(g_cur_param == NULL); \
g_cur_param = NULL; \
CU_ASSERT(jsonrpc_parse_request(conn, in, sizeof(in) - 1) == sizeof(in) - sizeof(trailing))
#define REQ_BEGIN(expected_error) \
if (expected_error != 0 ) { \
CU_ASSERT(g_parse_error == expected_error); \
CU_ASSERT(g_params == NULL); \
}
#define PARSE_FAIL(in) \
CU_ASSERT(jsonrpc_parse_request(conn, in, sizeof(in) - 1) < 0);
#define REQ_BEGIN_VALID() \
REQ_BEGIN(0); \
SPDK_CU_ASSERT_FATAL(g_params != NULL);
#define REQ_BEGIN_INVALID(expected_error) \
REQ_BEGIN(expected_error); \
REQ_METHOD_MISSING(); \
REQ_ID_MISSING(); \
REQ_PARAMS_MISSING()
#define REQ_METHOD(name) \
CU_ASSERT(g_method && spdk_json_strequal(g_method, name) == true)
#define REQ_METHOD_MISSING() \
CU_ASSERT(g_method == NULL)
#define REQ_ID_NUM(num) \
CU_ASSERT(g_request->id && g_request->id->type == SPDK_JSON_VAL_NUMBER); \
CU_ASSERT(g_request->id && memcmp(g_request->id->start, num, sizeof(num) - 1) == 0)
#define REQ_ID_STRING(str) \
CU_ASSERT(g_request->id && g_request->id->type == SPDK_JSON_VAL_STRING); \
CU_ASSERT(g_request->id && memcmp(g_request->id->start, num, strlen(num) - 1) == 0))
#define REQ_ID_NULL() \
CU_ASSERT(g_request->id && g_request->id->type == SPDK_JSON_VAL_NULL)
#define REQ_ID_MISSING() \
CU_ASSERT(g_request->id == NULL)
#define REQ_PARAMS_MISSING() \
CU_ASSERT(g_params == NULL)
#define REQ_PARAMS_BEGIN() \
SPDK_CU_ASSERT_FATAL(g_params != NULL); \
CU_ASSERT(g_cur_param == NULL); \
g_cur_param = g_params
#define PARAM_ARRAY_BEGIN() \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_ARRAY_BEGIN); \
g_cur_param++
#define PARAM_ARRAY_END() \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_ARRAY_END); \
g_cur_param++
#define PARAM_OBJECT_BEGIN() \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_OBJECT_BEGIN); \
g_cur_param++
#define PARAM_OBJECT_END() \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_OBJECT_END); \
g_cur_param++
#define PARAM_NUM(num) \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_NUMBER); \
CU_ASSERT(g_cur_param->len == sizeof(num) - 1); \
CU_ASSERT(memcmp(g_cur_param->start, num, sizeof(num) - 1) == 0); \
g_cur_param++
#define PARAM_NAME(str) \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_NAME); \
CU_ASSERT(g_cur_param->len == sizeof(str) - 1); \
CU_ASSERT(g_cur_param && memcmp(g_cur_param->start, str, sizeof(str) - 1) == 0); \
g_cur_param++
#define PARAM_STRING(str) \
CU_ASSERT(g_cur_param->type == SPDK_JSON_VAL_STRING); \
CU_ASSERT(g_cur_param->len == sizeof(str) - 1); \
CU_ASSERT(memcmp(g_cur_param->start, str, g_params->len) == 0); \
g_cur_param++
#define FREE_REQUEST() \
ut_jsonrpc_free_request(g_request, g_parse_error); \
g_request = NULL; \
g_cur_param = NULL; \
g_parse_error = 0; \
g_method = NULL; \
g_cur_param = g_params = NULL
static void
ut_jsonrpc_free_request(struct spdk_jsonrpc_request *request, int err)
{
struct spdk_json_write_ctx *w;
if (!request) {
return;
}
/* Need to emulate response to get the response write contex free */
if (err == 0) {
w = spdk_jsonrpc_begin_result(request);
spdk_json_write_string(w, "UT PASS response");
spdk_jsonrpc_end_result(request, w);
} else {
spdk_jsonrpc_send_error_response_fmt(request, err, "UT error response");
}
jsonrpc_free_request(request);
}
static void
ut_handle(struct spdk_jsonrpc_request *request, int error, const struct spdk_json_val *method,
const struct spdk_json_val *params)
{
CU_ASSERT(g_request == NULL);
g_request = request;
g_parse_error = error;
g_method = method;
g_params = params;
}
void
jsonrpc_server_handle_error(struct spdk_jsonrpc_request *request, int error)
{
ut_handle(request, error, NULL, NULL);
}
void
jsonrpc_server_handle_request(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *method, const struct spdk_json_val *params)
{
ut_handle(request, 0, method, params);
}
void
jsonrpc_server_send_response(struct spdk_jsonrpc_request *request)
{
}
static void
test_parse_request(void)
{
struct spdk_jsonrpc_server *server;
struct spdk_jsonrpc_server_conn *conn;
server = calloc(1, sizeof(*server));
SPDK_CU_ASSERT_FATAL(server != NULL);
conn = calloc(1, sizeof(*conn));
SPDK_CU_ASSERT_FATAL(conn != NULL);
conn->server = server;
/* rpc call with no parameters. */
PARSE_PASS("{ }", "");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
FREE_REQUEST();
/* rpc call with method that is not a string. */
PARSE_PASS("{\"jsonrpc\":\"2.0\", \"method\": null }", "");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
FREE_REQUEST();
/* rpc call with invalid JSON RPC version. */
PARSE_PASS("{\"jsonrpc\":\"42\", \"method\": \"subtract\"}", "");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
FREE_REQUEST();
/* rpc call with embedded zeros. */
PARSE_FAIL("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":{\"bar\": \"\0\0baz\"}}");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_PARSE_ERROR);
FREE_REQUEST();
/* rpc call with positional parameters */
PARSE_PASS("{\"jsonrpc\":\"2.0\",\"method\":\"subtract\",\"params\":[42,23],\"id\":1}", "");
REQ_BEGIN_VALID();
REQ_METHOD("subtract");
REQ_ID_NUM("1");
REQ_PARAMS_BEGIN();
PARAM_ARRAY_BEGIN();
PARAM_NUM("42");
PARAM_NUM("23");
PARAM_ARRAY_END();
FREE_REQUEST();
/* rpc call with named parameters */
PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": {\"subtrahend\": 23, \"minuend\": 42}, \"id\": 3}",
"");
REQ_BEGIN_VALID();
REQ_METHOD("subtract");
REQ_ID_NUM("3");
REQ_PARAMS_BEGIN();
PARAM_OBJECT_BEGIN();
PARAM_NAME("subtrahend");
PARAM_NUM("23");
PARAM_NAME("minuend");
PARAM_NUM("42");
PARAM_OBJECT_END();
FREE_REQUEST();
/* notification */
PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": \"update\", \"params\": [1,2,3,4,5]}", "");
REQ_BEGIN_VALID();
REQ_METHOD("update");
REQ_ID_MISSING();
REQ_PARAMS_BEGIN();
PARAM_ARRAY_BEGIN();
PARAM_NUM("1");
PARAM_NUM("2");
PARAM_NUM("3");
PARAM_NUM("4");
PARAM_NUM("5");
PARAM_ARRAY_END();
FREE_REQUEST();
/* notification with explicit NULL ID. This is discouraged by JSON RPC spec but allowed. */
PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": \"update\", \"params\": [1,2,3,4,5], \"id\": null}",
"");
REQ_BEGIN_VALID();
REQ_METHOD("update");
REQ_ID_NULL();
REQ_PARAMS_BEGIN();
PARAM_ARRAY_BEGIN();
PARAM_NUM("1");
PARAM_NUM("2");
PARAM_NUM("3");
PARAM_NUM("4");
PARAM_NUM("5");
PARAM_ARRAY_END();
FREE_REQUEST();
/* invalid JSON */
PARSE_FAIL("{\"jsonrpc\": \"2.0\", \"method\": \"foobar, \"params\": \"bar\", \"baz]");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_PARSE_ERROR);
FREE_REQUEST();
/* invalid request (method must be a string; params must be array or object) */
PARSE_PASS("{\"jsonrpc\": \"2.0\", \"method\": 1, \"params\": \"bar\"}", "");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
FREE_REQUEST();
/* batch, invalid JSON */
PARSE_FAIL(
"["
"{\"jsonrpc\": \"2.0\", \"method\": \"sum\", \"params\": [1,2,4], \"id\": \"1\"},"
"{\"jsonrpc\": \"2.0\", \"method\""
"]");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_PARSE_ERROR);
FREE_REQUEST();
/* empty array */
PARSE_PASS("[]", "");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
FREE_REQUEST();
/* batch - not supported */
PARSE_PASS(
"["
"{\"jsonrpc\": \"2.0\", \"method\": \"sum\", \"params\": [1,2,4], \"id\": \"1\"},"
"{\"jsonrpc\": \"2.0\", \"method\": \"notify_hello\", \"params\": [7]},"
"{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42,23], \"id\": \"2\"},"
"{\"foo\": \"boo\"},"
"{\"jsonrpc\": \"2.0\", \"method\": \"foo.get\", \"params\": {\"name\": \"myself\"}, \"id\": \"5\"},"
"{\"jsonrpc\": \"2.0\", \"method\": \"get_data\", \"id\": \"9\"}"
"]", "");
REQ_BEGIN_INVALID(SPDK_JSONRPC_ERROR_INVALID_REQUEST);
FREE_REQUEST();
CU_ASSERT(conn->outstanding_requests == 0);
free(conn);
free(server);
}
static void
test_parse_request_streaming(void)
{
struct spdk_jsonrpc_server *server;
struct spdk_jsonrpc_server_conn *conn;
const char *json_req;
size_t len, i;
server = calloc(1, sizeof(*server));
SPDK_CU_ASSERT_FATAL(server != NULL);
conn = calloc(1, sizeof(*conn));
SPDK_CU_ASSERT_FATAL(conn != NULL);
conn->server = server;
/*
* Two valid requests end to end in the same buffer.
* Parse should return the first one and point to the beginning of the second one.
*/
PARSE_PASS(
"{\"jsonrpc\":\"2.0\",\"method\":\"a\",\"params\":[1],\"id\":1}"
"{\"jsonrpc\":\"2.0\",\"method\":\"b\",\"params\":[2],\"id\":2}",
"{\"jsonrpc\":\"2.0\",\"method\":\"b\",\"params\":[2],\"id\":2}");
REQ_BEGIN_VALID();
REQ_METHOD("a");
REQ_ID_NUM("1");
REQ_PARAMS_BEGIN();
PARAM_ARRAY_BEGIN();
PARAM_NUM("1");
PARAM_ARRAY_END();
FREE_REQUEST();
/* Partial (but not invalid) requests - parse should not consume anything. */
json_req = " {\"jsonrpc\":\"2.0\",\"method\":\"b\",\"params\":[2],\"id\":2}";
len = strlen(json_req);
/* Try every partial length up to the full request length */
for (i = 0; i < len; i++) {
int rc = jsonrpc_parse_request(conn, json_req, i);
/* Partial request - no data consumed */
CU_ASSERT(rc == 0);
CU_ASSERT(g_request == NULL);
/* In case of faile, don't fload console with ussless CU assert fails. */
FREE_REQUEST();
}
/* Verify that full request can be parsed successfully */
CU_ASSERT(jsonrpc_parse_request(conn, json_req, len) == (ssize_t)len);
FREE_REQUEST();
CU_ASSERT(conn->outstanding_requests == 0);
free(conn);
free(server);
}
int main(int argc, char **argv)
{
CU_pSuite suite = NULL;
unsigned int num_failures;
CU_set_error_action(CUEA_ABORT);
CU_initialize_registry();
suite = CU_add_suite("jsonrpc", NULL, NULL);
CU_ADD_TEST(suite, test_parse_request);
CU_ADD_TEST(suite, test_parse_request_streaming);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
num_failures = CU_get_number_of_failures();
CU_cleanup_registry();
/* This is for ASAN. Don't know why but if pointer is left in global varaible
* it won't be detected as leak. */
g_request = NULL;
return num_failures;
}
|