test_conversion.c (3551B)
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/platform.h" 22 #include "taler/taler_util.h" 23 #include <gnunet/gnunet_json_lib.h> 24 #include "taler/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 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 ec = NULL; 56 global_ret = 3; 57 if (42 != code) 58 { 59 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 60 "Unexpected return value from helper: %u\n", 61 (unsigned int) code); 62 return; 63 } 64 expect = GNUNET_JSON_PACK ( 65 GNUNET_JSON_pack_string ("foo", 66 "arg") 67 ); 68 GNUNET_assert (NULL != expect); 69 if (1 == json_equal (expect, 70 result)) 71 { 72 global_ret = 0; 73 } 74 else 75 { 76 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 77 "Unexpected JSON result\n"); 78 json_dumpf (result, 79 stderr, 80 JSON_INDENT (2)); 81 global_ret = 4; 82 } 83 json_decref (expect); 84 } 85 86 87 /** 88 * Function called on shutdown/CTRL-C. 89 * 90 * @param cls NULL 91 */ 92 static void 93 do_shutdown (void *cls) 94 { 95 (void) cls; 96 if (NULL != ec) 97 { 98 GNUNET_break (0); 99 global_ret = 2; 100 TALER_JSON_external_conversion_stop (ec); 101 ec = NULL; 102 } 103 } 104 105 106 /** 107 * Main test function. 108 * 109 * @param cls NULL 110 */ 111 static void 112 run (void *cls) 113 { 114 json_t *input; 115 const char *argv[] = { 116 "test_conversion.sh", 117 "arg", 118 NULL 119 }; 120 121 (void) cls; 122 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 123 NULL); 124 input = GNUNET_JSON_PACK ( 125 GNUNET_JSON_pack_string ("key", 126 "foo") 127 ); 128 GNUNET_assert (NULL != input); 129 ec = TALER_JSON_external_conversion_start (input, 130 &conv_cb, 131 NULL, 132 "./test_conversion.sh", 133 argv); 134 json_decref (input); 135 GNUNET_assert (NULL != ec); 136 } 137 138 139 int 140 main (int argc, 141 const char *const argv[]) 142 { 143 (void) argc; 144 (void) argv; 145 unsetenv ("XDG_DATA_HOME"); 146 unsetenv ("XDG_CONFIG_HOME"); 147 GNUNET_log_setup ("test-conversion", 148 "INFO", 149 NULL); 150 global_ret = 1; 151 GNUNET_SCHEDULER_run (&run, 152 NULL); 153 return global_ret; 154 }