@@ -171,6 +171,91 @@ static void test_json_key_not_in_descr(void)
171171 zassert_equal (ret , 0 , "No items should be decoded" );
172172}
173173
174+ static void test_json_escape (void )
175+ {
176+ char buf [42 ];
177+ char string [] = "\"abc"
178+ "\\1`23"
179+ "\bf'oo"
180+ "\fbar"
181+ "\nbaz"
182+ "\rquux"
183+ "\tfred\"" ;
184+ const char * expected = "\\\"abc"
185+ "\\\\1`23"
186+ "\\bf'oo"
187+ "\\fbar"
188+ "\\nbaz"
189+ "\\rquux"
190+ "\\tfred\\\"" ;
191+ size_t len ;
192+ ssize_t ret ;
193+
194+ strncpy (buf , string , sizeof (buf ) - 1 );
195+ len = strlen (buf );
196+
197+ ret = json_escape (buf , & len , sizeof (buf ));
198+ zassert_equal (ret , 0 , "Escape succeeded" );
199+ zassert_equal (len , sizeof (buf ) - 1 ,
200+ "Escaped length computed correctly" );
201+ zassert_true (!strcmp (buf , expected ),
202+ "Escaped value is correct" );
203+ }
204+
205+ /* Edge case: only one character, which must be escaped. */
206+ static void test_json_escape_one (void )
207+ {
208+ char buf [3 ] = {'\t' , '\0' , '\0' };
209+ const char * expected = "\\t" ;
210+ size_t len = strlen (buf );
211+ ssize_t ret ;
212+
213+ ret = json_escape (buf , & len , sizeof (buf ));
214+ zassert_equal (ret , 0 ,
215+ "Escaping one character succeded" );
216+ zassert_equal (len , sizeof (buf ) - 1 ,
217+ "Escaping one character length is correct" );
218+ zassert_true (!strcmp (buf , expected ),
219+ "Escaped value is correct" );
220+ }
221+
222+ static void test_json_escape_empty (void )
223+ {
224+ char empty [] = "" ;
225+ size_t len = sizeof (empty ) - 1 ;
226+ ssize_t ret ;
227+
228+ ret = json_escape (empty , & len , sizeof (empty ));
229+ zassert_equal (ret , 0 , "Escaped empty string" );
230+ zassert_equal (len , 0 , "Length of empty escaped string is zero" );
231+ zassert_equal (empty [0 ], '\0' , "Empty string remains empty" );
232+ }
233+
234+ static void test_json_escape_no_op (void )
235+ {
236+ char nothing_to_escape [] = "hello,world:!" ;
237+ const char * expected = "hello,world:!" ;
238+ size_t len = sizeof (nothing_to_escape ) - 1 ;
239+ ssize_t ret ;
240+
241+ ret = json_escape (nothing_to_escape , & len , sizeof (nothing_to_escape ));
242+ zassert_equal (ret , 0 , "Nothing to escape handled correctly" );
243+ zassert_equal (len , sizeof (nothing_to_escape ) - 1 ,
244+ "Didn't change length of already escaped string" );
245+ zassert_true (!strcmp (nothing_to_escape , expected ),
246+ "Didn't alter string with nothing to escape" );
247+ }
248+
249+ static void test_json_escape_bounds_check (void )
250+ {
251+ char not_enough_memory [] = "\tfoo" ;
252+ size_t len = sizeof (not_enough_memory ) - 1 ;
253+ ssize_t ret ;
254+
255+ ret = json_escape (not_enough_memory , & len , sizeof (not_enough_memory ));
256+ zassert_equal (ret , - ENOMEM , "Bounds check OK" );
257+ }
258+
174259void test_main (void )
175260{
176261 ztest_test_suite (lib_json_test ,
@@ -180,7 +265,12 @@ void test_main(void)
180265 ztest_unit_test (test_json_missing_quote ),
181266 ztest_unit_test (test_json_wrong_token ),
182267 ztest_unit_test (test_json_item_wrong_type ),
183- ztest_unit_test (test_json_key_not_in_descr )
268+ ztest_unit_test (test_json_key_not_in_descr ),
269+ ztest_unit_test (test_json_escape ),
270+ ztest_unit_test (test_json_escape_one ),
271+ ztest_unit_test (test_json_escape_empty ),
272+ ztest_unit_test (test_json_escape_no_op ),
273+ ztest_unit_test (test_json_escape_bounds_check )
184274 );
185275
186276 ztest_run_test_suite (lib_json_test );
0 commit comments