@@ -98,7 +98,7 @@ static const struct json_obj_descr array_array_descr[] = {
9898static void test_json_encoding (void )
9999{
100100 struct test_struct ts = {
101- .some_string = "zephyr 123" ,
101+ .some_string = "zephyr 123\uABCD " ,
102102 .some_int = 42 ,
103103 .some_bool = true,
104104 .some_nested_struct = {
@@ -125,7 +125,7 @@ static void test_json_encoding(void)
125125 .nested_string = "no escape necessary" ,
126126 },
127127 };
128- char encoded [] = "{\"some_string\":\"zephyr 123\","
128+ char encoded [] = "{\"some_string\":\"zephyr 123\uABCD\ ","
129129 "\"some_int\":42,\"some_bool\":true,"
130130 "\"some_nested_struct\":{\"nested_int\":-1234,"
131131 "\"nested_bool\":false,\"nested_string\":"
@@ -140,6 +140,10 @@ static void test_json_encoding(void)
140140 "}" ;
141141 char buffer [sizeof (encoded )];
142142 int ret ;
143+ ssize_t len ;
144+
145+ len = json_calc_encoded_len (test_descr , ARRAY_SIZE (test_descr ), & ts );
146+ zassert_equal (len , strlen (encoded ), "encoded size mismatch" );
143147
144148 ret = json_obj_encode_buf (test_descr , ARRAY_SIZE (test_descr ),
145149 & ts , buffer , sizeof (buffer ));
@@ -152,7 +156,7 @@ static void test_json_encoding(void)
152156static void test_json_decoding (void )
153157{
154158 struct test_struct ts ;
155- char encoded [] = "{\"some_string\":\"zephyr 123\","
159+ char encoded [] = "{\"some_string\":\"zephyr 123\\uABCD456\ ","
156160 "\"some_int\":\t42\n,"
157161 "\"some_bool\":true \t "
158162 "\n"
@@ -168,7 +172,7 @@ static void test_json_decoding(void)
168172 "\"4nother_ne$+\":{\"nested_int\":1234,"
169173 "\"nested_bool\":true,"
170174 "\"nested_string\":\"no escape necessary\"}"
171- "}" ;
175+ "}\n " ;
172176 const int expected_array [] = { 11 , 22 , 33 , 45 , 299 };
173177 const int expected_other_array [] = { 2 , 3 , 5 , 7 };
174178 int ret ;
@@ -179,7 +183,7 @@ static void test_json_decoding(void)
179183 zassert_equal (ret , (1 << ARRAY_SIZE (test_descr )) - 1 ,
180184 "All fields decoded correctly" );
181185
182- zassert_true (!strcmp (ts .some_string , "zephyr 123" ),
186+ zassert_true (!strcmp (ts .some_string , "zephyr 123\\uABCD456 " ),
183187 "String decoded correctly" );
184188 zassert_equal (ts .some_int , 42 , "Positive integer decoded correctly" );
185189 zassert_equal (ts .some_bool , true, "Boolean decoded correctly" );
@@ -312,57 +316,78 @@ static void test_json_obj_arr_decoding(void)
312316 "Array of object fields decoded correctly" );
313317 zassert_equal (oa .num_elements , 10 ,
314318 "Number of object fields decoded correctly" );
315- zassert_true (!strcmp (oa .elements [0 ].name , expected .elements [0 ].name ),
316- "Element 0 name decoded correctly" );
317- zassert_equal (oa .elements [0 ].height , expected .elements [0 ].height ,
318- "Element 0 height decoded correctly" );
319- zassert_true (!strcmp (oa .elements [1 ].name , expected .elements [1 ].name ),
320- "Element 1 name decoded correctly" );
321- zassert_equal (oa .elements [1 ].height , expected .elements [1 ].height ,
322- "Element 1 height decoded correctly" );
323- zassert_true (!strcmp (oa .elements [2 ].name , expected .elements [2 ].name ),
324- "Element 2 name decoded correctly" );
325- zassert_equal (oa .elements [2 ].height , expected .elements [2 ].height ,
326- "Element 2 height decoded correctly" );
327- zassert_true (!strcmp (oa .elements [3 ].name , expected .elements [3 ].name ),
328- "Element 3 name decoded correctly" );
329- zassert_equal (oa .elements [3 ].height , expected .elements [3 ].height ,
330- "Element 3 height decoded correctly" );
331- zassert_true (!strcmp (oa .elements [4 ].name , expected .elements [4 ].name ),
332- "Element 4 name decoded correctly" );
333- zassert_equal (oa .elements [4 ].height , expected .elements [4 ].height ,
334- "Element 4 height decoded correctly" );
335- zassert_true (!strcmp (oa .elements [5 ].name , expected .elements [5 ].name ),
336- "Element 5 name decoded correctly" );
337- zassert_equal (oa .elements [5 ].height , expected .elements [5 ].height ,
338- "Element 5 height decoded correctly" );
339- zassert_true (!strcmp (oa .elements [6 ].name , expected .elements [6 ].name ),
340- "Element 6 name decoded correctly" );
341- zassert_equal (oa .elements [6 ].height , expected .elements [6 ].height ,
342- "Element 6 height decoded correctly" );
343- zassert_true (!strcmp (oa .elements [7 ].name , expected .elements [7 ].name ),
344- "Element 7 name decoded correctly" );
345- zassert_equal (oa .elements [7 ].height , expected .elements [7 ].height ,
346- "Element 7 height decoded correctly" );
347- zassert_true (!strcmp (oa .elements [8 ].name , expected .elements [8 ].name ),
348- "Element 8 name decoded correctly" );
349- zassert_equal (oa .elements [8 ].height , expected .elements [8 ].height ,
350- "Element 8 height decoded correctly" );
351- zassert_true (!strcmp (oa .elements [9 ].name , expected .elements [9 ].name ),
352- "Element 9 name decoded correctly" );
353- zassert_equal (oa .elements [9 ].height , expected .elements [9 ].height ,
354- "Element 9 height decoded correctly" );
319+
320+ for (int i = 0 ; i < expected .num_elements ; i ++ ) {
321+ zassert_true (!strcmp (oa .elements [i ].name ,
322+ expected .elements [i ].name ),
323+ "Element %d name decoded correctly" , i );
324+ zassert_equal (oa .elements [i ].height ,
325+ expected .elements [i ].height ,
326+ "Element %d height decoded correctly" , i );
327+ }
355328}
356329
357- static void test_json_invalid_unicode (void )
330+ struct encoding_test {
331+ char * str ;
332+ int result ;
333+ };
334+
335+ static void parse_harness (struct encoding_test encoded [], size_t size )
358336{
359337 struct test_struct ts ;
360- char encoded [] = "{\"some_string\":\"\\uABC@\"}" ;
361338 int ret ;
362339
363- ret = json_obj_parse (encoded , sizeof (encoded ) - 1 , test_descr ,
364- ARRAY_SIZE (test_descr ), & ts );
365- zassert_equal (ret , - EINVAL , "Decoding has to fail" );
340+ for (int i = 0 ; i < size ; i ++ ) {
341+ ret = json_obj_parse (encoded [i ].str , strlen (encoded [i ].str ),
342+ test_descr , ARRAY_SIZE (test_descr ), & ts );
343+ zassert_equal (ret , encoded [i ].result ,
344+ "Decoding '%s' result %d, expected %d" ,
345+ encoded [i ].str , ret , encoded [i ].result );
346+ }
347+ }
348+
349+ static void test_json_invalid_string (void )
350+ {
351+ struct encoding_test encoded [] = {
352+ { "{\"some_string\":\"\\u@@@@\"}" , - EINVAL },
353+ { "{\"some_string\":\"\\uA@@@\"}" , - EINVAL },
354+ { "{\"some_string\":\"\\uAB@@\"}" , - EINVAL },
355+ { "{\"some_string\":\"\\uABC@\"}" , - EINVAL },
356+ { "{\"some_string\":\"\\X\"}" , - EINVAL }
357+ };
358+
359+ parse_harness (encoded , ARRAY_SIZE (encoded ));
360+ }
361+
362+ static void test_json_invalid_bool (void )
363+ {
364+ struct encoding_test encoded [] = {
365+ { "{\"some_bool\":truffle }" , - EINVAL },
366+ { "{\"some_bool\":fallacy }" , - EINVAL },
367+ };
368+
369+ parse_harness (encoded , ARRAY_SIZE (encoded ));
370+ }
371+
372+ static void test_json_invalid_null (void )
373+ {
374+ struct encoding_test encoded [] = {
375+ /* Parser will recognize 'null', but refuse to decode it */
376+ { "{\"some_string\":null }" , - EINVAL },
377+ /* Null spelled wrong */
378+ { "{\"some_string\":nutella }" , - EINVAL },
379+ };
380+
381+ parse_harness (encoded , ARRAY_SIZE (encoded ));
382+ }
383+
384+ static void test_json_invalid_number (void )
385+ {
386+ struct encoding_test encoded [] = {
387+ { "{\"some_int\":xxx }" , - EINVAL },
388+ };
389+
390+ parse_harness (encoded , ARRAY_SIZE (encoded ));
366391}
367392
368393static void test_json_missing_quote (void )
@@ -502,7 +527,10 @@ void test_main(void)
502527 ztest_unit_test (test_json_decoding_array_array ),
503528 ztest_unit_test (test_json_obj_arr_encoding ),
504529 ztest_unit_test (test_json_obj_arr_decoding ),
505- ztest_unit_test (test_json_invalid_unicode ),
530+ ztest_unit_test (test_json_invalid_string ),
531+ ztest_unit_test (test_json_invalid_bool ),
532+ ztest_unit_test (test_json_invalid_null ),
533+ ztest_unit_test (test_json_invalid_number ),
506534 ztest_unit_test (test_json_missing_quote ),
507535 ztest_unit_test (test_json_wrong_token ),
508536 ztest_unit_test (test_json_item_wrong_type ),
0 commit comments