test_conversion.c (3523B)
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 "taler/taler_util.h" 22 #include <gnunet/gnunet_json_lib.h> 23 #include "taler/taler_json_lib.h" 24 25 /** 26 * Return value from main(). 27 */ 28 static int global_ret; 29 30 /** 31 * Handle to our helper. 32 */ 33 static struct TALER_JSON_ExternalConversion *ec; 34 35 36 /** 37 * Type of a callback that receives a JSON @a result. 38 * 39 * @param cls closure 40 * @param status_type how did the process die 41 * @apram code termination status code from the process 42 * @param result some JSON result, NULL if we failed to get an JSON output 43 */ 44 static void 45 conv_cb (void *cls, 46 enum GNUNET_OS_ProcessStatusType status_type, 47 unsigned long code, 48 const json_t *result) 49 { 50 json_t *expect; 51 52 (void) cls; 53 (void) status_type; 54 ec = NULL; 55 global_ret = 3; 56 if (42 != code) 57 { 58 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 59 "Unexpected return value from helper: %u\n", 60 (unsigned int) code); 61 return; 62 } 63 expect = GNUNET_JSON_PACK ( 64 GNUNET_JSON_pack_string ("foo", 65 "arg") 66 ); 67 GNUNET_assert (NULL != expect); 68 if (1 == json_equal (expect, 69 result)) 70 { 71 global_ret = 0; 72 } 73 else 74 { 75 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 76 "Unexpected JSON result\n"); 77 json_dumpf (result, 78 stderr, 79 JSON_INDENT (2)); 80 global_ret = 4; 81 } 82 json_decref (expect); 83 } 84 85 86 /** 87 * Function called on shutdown/CTRL-C. 88 * 89 * @param cls NULL 90 */ 91 static void 92 do_shutdown (void *cls) 93 { 94 (void) cls; 95 if (NULL != ec) 96 { 97 GNUNET_break (0); 98 global_ret = 2; 99 TALER_JSON_external_conversion_stop (ec); 100 ec = NULL; 101 } 102 } 103 104 105 /** 106 * Main test function. 107 * 108 * @param cls NULL 109 */ 110 static void 111 run (void *cls) 112 { 113 json_t *input; 114 const char *argv[] = { 115 "test_conversion.sh", 116 "arg", 117 NULL 118 }; 119 120 (void) cls; 121 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 122 NULL); 123 input = GNUNET_JSON_PACK ( 124 GNUNET_JSON_pack_string ("key", 125 "foo") 126 ); 127 GNUNET_assert (NULL != input); 128 ec = TALER_JSON_external_conversion_start (input, 129 &conv_cb, 130 NULL, 131 "./test_conversion.sh", 132 argv); 133 json_decref (input); 134 GNUNET_assert (NULL != ec); 135 } 136 137 138 int 139 main (int argc, 140 const char *const argv[]) 141 { 142 (void) argc; 143 (void) argv; 144 unsetenv ("XDG_DATA_HOME"); 145 unsetenv ("XDG_CONFIG_HOME"); 146 GNUNET_log_setup ("test-conversion", 147 "INFO", 148 NULL); 149 global_ret = 1; 150 GNUNET_SCHEDULER_run (&run, 151 NULL); 152 return global_ret; 153 }