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-11-11 21:44:55 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           1 : 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           1 :     forktest_only;
      38             : 
      39           1 :     abort();
      40             : }
      41             : END_TEST
      42             : 
      43          10 : int testjsontext(char* jsonstr, char* expectedstr) {
      44          10 :     cJSON* val_json = cJSON_Parse(jsonstr);
      45          10 :     char* val_text=NULL;
      46             :     int retval;
      47          10 :     val_text = json2text(val_json);
      48          10 :     retval=strcmp(val_text,expectedstr);
      49          10 :     if (retval) fprintf(stderr, "%s -> %s (not %s)\n",jsonstr, val_text, expectedstr);
      50          10 :     free(val_text);
      51          10 :     cJSON_Delete(val_json);
      52          10 :     return retval;
      53             : }
      54             : 
      55           1 : START_TEST(json_json2text) {
      56             :     cJSON* json;
      57             :     char* text;
      58             : 
      59           1 :     json=NULL;
      60           1 :     text=NULL;
      61           1 :     text = json2text(json);
      62           1 :     ck_assert(0==strcmp(text,"Invalid data"));
      63           1 :     free(text);
      64             : 
      65           1 :     json = cJSON_CreateObject();
      66           1 :     text=NULL;
      67           1 :     text = json2text(json);
      68           1 :     ck_assert(0==strcmp(text,"{}"));
      69           1 :     free(text);
      70           1 :     cJSON_Delete(json);
      71             : 
      72           1 :     ck_assert(0==testjsontext("{}","{}"));
      73           1 :     ck_assert(0==testjsontext("[]","[]"));
      74           1 :     ck_assert(0==testjsontext("\"ab\"","ab"));
      75           1 :     ck_assert(0==testjsontext("\"1\"","1"));
      76           1 :     ck_assert(0==testjsontext("1","1"));
      77           1 :     ck_assert(0==testjsontext("null","null"));
      78           1 :     ck_assert(0==testjsontext("true","true"));
      79           1 :     ck_assert(0==testjsontext("false","false"));
      80           1 :     ck_assert(0==testjsontext("a","Invalid data"));
      81           1 :     ck_assert(0==testjsontext("","Invalid data"));
      82           1 : }
      83             : END_TEST
      84             : 
      85             : /************************************************************************/
      86             : /* Begining of test (potentially in child) */
      87           2 : void json_checked_uninitialized_setup() {
      88           2 :     signals_catch();
      89           2 :     forktest_gprofdir();
      90           2 : }
      91             : 
      92             : /* End of test (potentially in child) */
      93           1 : void json_checked_uninitialized_teardown() {
      94           1 :     signals_release();
      95           1 : }
      96             : /************************************************************************/
      97           3 : void json_unchecked_common_setup() {
      98           3 :     forktest_init();
      99           3 : }
     100           1 : void json_unchecked_common_teardown() {
     101           1 : }
     102             : /************************************************************************/
     103           3 : Suite* json_suite() {
     104             :     Suite *s;
     105             :     TCase *tc;
     106             : 
     107           3 :     s = suite_create("JSON");
     108             : 
     109           3 :     tc = tcase_create("json2text");
     110           3 :     tcase_set_tags(tc,"JSONTag JSONTag2");
     111             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     112           3 :     tcase_add_checked_fixture(tc, json_checked_uninitialized_setup, json_checked_uninitialized_teardown);
     113           3 :     tcase_add_unchecked_fixture(tc, json_unchecked_common_setup, json_unchecked_common_teardown);
     114           3 :     suite_add_tcase(s, tc);
     115           3 :     tcase_add_test_raise_signal(tc, json_test,6);
     116           3 :     tcase_add_test(tc, json_json2text);
     117             : 
     118           3 :     return s;
     119             : }
     120             : /* vim: set tw=80: */

Generated by: LCOV version 1.16