LCOV - code coverage report
Current view: top level - test - suite_json.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 57 57 100.0 %
Date: 2024-12-05 21:00:54 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /**   @file  suite_json.c
       2             :  *   @brief  Check testsuite template
       3             :  *  @author  François Cerbelle (Fanfan), francois@cerbelle.net
       4             :  *
       5             :  *  @internal
       6             :  *       Created:  20/03/2022
       7             :  *      Revision:  none
       8             :  * Last modified:  2024-11-06 11:23
       9             :  *      Compiler:  gcc
      10             :  *  Organization:  Cerbelle.net
      11             :  *     Copyright:  Copyright (c) 2024, François Cerbelle
      12             :  *
      13             :  *  This source code is released for free distribution under the terms of the
      14             :  *  GNU General Public License as published by the Free Software Foundation.
      15             :  */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include <check.h>
      22             : #include <stdlib.h>
      23             : #include <stdio.h>                              /* fprintf */
      24             : #include "checktools.inc"
      25             : 
      26             : #include "json.h"
      27             : 
      28             : /** Dummy test to trigger fixtures
      29             :  *
      30             :  * This test is defined and added to all testcases in order to execute at
      31             :  * least one test test in each testcase of the testsuite, trigger the
      32             :  * fixtures and have a decent coverage report.
      33             :  */
      34           3 : START_TEST(json_test) {
      35             :     /* This test is supposed to trigger a SIGABRT(6) and will crash the
      36             :      * whole testsuite if not caught or not in a child process */
      37           3 :     forktest_only;
      38             : 
      39           2 :     abort();
      40             : }
      41             : END_TEST
      42             : 
      43          30 : int testjsontext(char* jsonstr, char* expectedstr) {
      44          30 :     cJSON* val_json = cJSON_Parse(jsonstr);
      45          30 :     char* val_text=NULL;
      46             :     int retval;
      47          30 :     val_text = json2text(val_json);
      48          30 :     retval=strcmp(val_text,expectedstr);
      49          30 :     if (retval) fprintf(stderr, "%s -> %s (not %s)\n",jsonstr, val_text, expectedstr);
      50          30 :     free(val_text);
      51          30 :     cJSON_Delete(val_json);
      52          30 :     return retval;
      53             : }
      54             : 
      55           3 : START_TEST(json_json2text) {
      56             :     cJSON* json;
      57             :     char* text;
      58             : 
      59           3 :     json=NULL;
      60           3 :     text=NULL;
      61           3 :     text = json2text(json);
      62           3 :     ck_assert(0==strcmp(text,"Invalid data"));
      63           3 :     free(text);
      64             : 
      65           3 :     json = cJSON_CreateObject();
      66           3 :     text=NULL;
      67           3 :     text = json2text(json);
      68           3 :     ck_assert(0==strcmp(text,"{}"));
      69           3 :     free(text);
      70           3 :     cJSON_Delete(json);
      71             : 
      72           3 :     ck_assert(0==testjsontext("{}","{}"));
      73           3 :     ck_assert(0==testjsontext("[]","[]"));
      74           3 :     ck_assert(0==testjsontext("\"ab\"","ab"));
      75           3 :     ck_assert(0==testjsontext("\"1\"","1"));
      76           3 :     ck_assert(0==testjsontext("1","1"));
      77           3 :     ck_assert(0==testjsontext("null","null"));
      78           3 :     ck_assert(0==testjsontext("true","true"));
      79           3 :     ck_assert(0==testjsontext("false","false"));
      80           3 :     ck_assert(0==testjsontext("a","Invalid data"));
      81           3 :     ck_assert(0==testjsontext("","Invalid data"));
      82           3 : }
      83             : END_TEST
      84             : 
      85             : /************************************************************************/
      86             : /* Begining of test (potentially in child) */
      87           6 : void json_checked_uninitialized_setup() {
      88           6 :     signals_catch();
      89           6 :     forktest_gprofdir();
      90           6 : }
      91             : 
      92             : /* End of test (potentially in child) */
      93           4 : void json_checked_uninitialized_teardown() {
      94           4 :     signals_release();
      95           4 : }
      96             : /************************************************************************/
      97           7 : void json_unchecked_common_setup() {
      98           7 :     forktest_init();
      99           7 : }
     100           3 : void json_unchecked_common_teardown() {
     101           3 : }
     102             : /************************************************************************/
     103           8 : Suite* json_suite() {
     104             :     Suite *s;
     105             :     TCase *tc;
     106             : 
     107           8 :     s = suite_create("JSON");
     108             : 
     109           8 :     tc = tcase_create("json2text");
     110           8 :     tcase_set_tags(tc,"JSONTag JSONTag2");
     111             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     112           8 :     tcase_add_checked_fixture(tc, json_checked_uninitialized_setup, json_checked_uninitialized_teardown);
     113           8 :     tcase_add_unchecked_fixture(tc, json_unchecked_common_setup, json_unchecked_common_teardown);
     114           8 :     suite_add_tcase(s, tc);
     115           8 :     tcase_add_test_raise_signal(tc, json_test,6);
     116           8 :     tcase_add_test(tc, json_json2text);
     117             : 
     118           8 :     return s;
     119             : }
     120             : /* vim: set tw=80: */

Generated by: LCOV version 1.16