LCOV - code coverage report
Current view: top level - json - test_conversion.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 37 75.7 %
Date: 2025-06-05 21:03:14 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2023 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file util/test_conversion.c
      18             :  * @brief Tests for conversion logic
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_util.h"
      23             : #include <gnunet/gnunet_json_lib.h>
      24             : #include "taler_json_lib.h"
      25             : 
      26             : /**
      27             :  * Return value from main().
      28             :  */
      29             : static int global_ret;
      30             : 
      31             : /**
      32             :  * Handle to our helper.
      33             :  */
      34             : static struct TALER_JSON_ExternalConversion *ec;
      35             : 
      36             : 
      37             : /**
      38             :  * Type of a callback that receives a JSON @a result.
      39             :  *
      40             :  * @param cls closure
      41             :  * @param status_type how did the process die
      42             :  * @apram code termination status code from the process
      43             :  * @param result some JSON result, NULL if we failed to get an JSON output
      44             :  */
      45             : static void
      46           1 : conv_cb (void *cls,
      47             :          enum GNUNET_OS_ProcessStatusType status_type,
      48             :          unsigned long code,
      49             :          const json_t *result)
      50             : {
      51             :   json_t *expect;
      52             : 
      53             :   (void) cls;
      54             :   (void) status_type;
      55           1 :   ec = NULL;
      56           1 :   global_ret = 3;
      57           1 :   if (42 != code)
      58             :   {
      59           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      60             :                 "Unexpected return value from helper: %u\n",
      61             :                 (unsigned int) code);
      62           0 :     return;
      63             :   }
      64           1 :   expect = GNUNET_JSON_PACK (
      65             :     GNUNET_JSON_pack_string ("foo",
      66             :                              "arg")
      67             :     );
      68           1 :   GNUNET_assert (NULL != expect);
      69           1 :   if (1 == json_equal (expect,
      70             :                        result))
      71             :   {
      72           1 :     global_ret = 0;
      73             :   }
      74             :   else
      75             :   {
      76           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      77             :                 "Unexpected JSON result\n");
      78           0 :     json_dumpf (result,
      79             :                 stderr,
      80             :                 JSON_INDENT (2));
      81           0 :     global_ret = 4;
      82             :   }
      83           1 :   json_decref (expect);
      84             : }
      85             : 
      86             : 
      87             : /**
      88             :  * Function called on shutdown/CTRL-C.
      89             :  *
      90             :  * @param cls NULL
      91             :  */
      92             : static void
      93           1 : do_shutdown (void *cls)
      94             : {
      95             :   (void) cls;
      96           1 :   if (NULL != ec)
      97             :   {
      98           0 :     GNUNET_break (0);
      99           0 :     global_ret = 2;
     100           0 :     TALER_JSON_external_conversion_stop (ec);
     101           0 :     ec = NULL;
     102             :   }
     103           1 : }
     104             : 
     105             : 
     106             : /**
     107             :  * Main test function.
     108             :  *
     109             :  * @param cls NULL
     110             :  */
     111             : static void
     112           1 : run (void *cls)
     113             : {
     114             :   json_t *input;
     115           1 :   const char *argv[] = {
     116             :     "test_conversion.sh",
     117             :     "arg",
     118             :     NULL
     119             :   };
     120             : 
     121             :   (void) cls;
     122           1 :   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
     123             :                                  NULL);
     124           1 :   input = GNUNET_JSON_PACK (
     125             :     GNUNET_JSON_pack_string ("key",
     126             :                              "foo")
     127             :     );
     128           1 :   GNUNET_assert (NULL != input);
     129           1 :   ec = TALER_JSON_external_conversion_start (input,
     130             :                                              &conv_cb,
     131             :                                              NULL,
     132             :                                              "./test_conversion.sh",
     133             :                                              argv);
     134           1 :   json_decref (input);
     135           1 :   GNUNET_assert (NULL != ec);
     136           1 : }
     137             : 
     138             : 
     139             : int
     140           1 : main (int argc,
     141             :       const char *const argv[])
     142             : {
     143             :   (void) argc;
     144             :   (void) argv;
     145           1 :   unsetenv ("XDG_DATA_HOME");
     146           1 :   unsetenv ("XDG_CONFIG_HOME");
     147           1 :   GNUNET_log_setup ("test-conversion",
     148             :                     "INFO",
     149             :                     NULL);
     150           1 :   global_ret = 1;
     151           1 :   GNUNET_SCHEDULER_run (&run,
     152             :                         NULL);
     153           1 :   return global_ret;
     154             : }

Generated by: LCOV version 1.16