merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_webhook.c (4415B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 3,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file testing/testing_api_cmd_webhook.c
     21  * @brief run the taler-merchant-webhook command
     22  * @author Priscilla HUANG
     23  */
     24 #include "taler/platform.h"
     25 #include "taler/taler_json_lib.h"
     26 #include <gnunet/gnunet_curl_lib.h>
     27 #include "taler/taler_signatures.h"
     28 #include "taler/taler_testing_lib.h"
     29 #include "taler/taler_merchant_testing_lib.h"
     30 
     31 
     32 /**
     33  * State for a "webhook" CMD.
     34  */
     35 struct WebhookState
     36 {
     37 
     38   /**
     39    * Process for the webhook.
     40    */
     41   struct GNUNET_Process *webhook_proc;
     42 
     43   /**
     44    * Configuration file used by the webhook.
     45    */
     46   const char *config_filename;
     47 };
     48 
     49 
     50 /**
     51  * Run the command; use the `taler-merchant-webhook' program.
     52  *
     53  * @param cls closure.
     54  * @param cmd command currently being executed.
     55  * @param is interpreter state.
     56  */
     57 static void
     58 webhook_run (void *cls,
     59              const struct TALER_TESTING_Command *cmd,
     60              struct TALER_TESTING_Interpreter *is)
     61 {
     62   struct WebhookState *ws = cls;
     63 
     64   (void) cmd;
     65   ws->webhook_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ERR);
     66   if (GNUNET_OK !=
     67       GNUNET_process_run_command_va (ws->webhook_proc,
     68                                      "taler-merchant-webhook",
     69                                      "taler-merchant-webhook",
     70                                      "-c", ws->config_filename,
     71                                      "-t",   /* exit when done */
     72                                      "-L", "DEBUG",
     73                                      NULL))
     74   {
     75     GNUNET_break (0);
     76     GNUNET_process_destroy (ws->webhook_proc);
     77     ws->webhook_proc = NULL;
     78     TALER_TESTING_interpreter_fail (is);
     79     return;
     80   }
     81   TALER_TESTING_wait_for_sigchld (is);
     82 }
     83 
     84 
     85 /**
     86  * Free the state of a "webhook" CMD, and possibly
     87  * kills its process if it did not terminate regularly.
     88  *
     89  * @param cls closure.
     90  * @param cmd the command being freed.
     91  */
     92 static void
     93 webhook_cleanup (void *cls,
     94                  const struct TALER_TESTING_Command *cmd)
     95 {
     96   struct WebhookState *ws = cls;
     97 
     98   (void) cmd;
     99   if (NULL != ws->webhook_proc)
    100   {
    101     GNUNET_break (GNUNET_OK ==
    102                   GNUNET_process_kill (ws->webhook_proc,
    103                                        SIGKILL));
    104     GNUNET_break (GNUNET_OK ==
    105                   GNUNET_process_wait (ws->webhook_proc,
    106                                        true,
    107                                        NULL,
    108                                        NULL));
    109     GNUNET_process_destroy (ws->webhook_proc);
    110     ws->webhook_proc = NULL;
    111   }
    112   GNUNET_free (ws);
    113 }
    114 
    115 
    116 /**
    117  * Offer "webhook" CMD internal data to other commands.
    118  *
    119  * @param cls closure.
    120  * @param[out] ret result.
    121  * @param trait name of the trait.
    122  * @param index index number of the object to offer.
    123  * @return #GNUNET_OK on success.
    124  */
    125 static enum GNUNET_GenericReturnValue
    126 webhook_traits (void *cls,
    127                 const void **ret,
    128                 const char *trait,
    129                 unsigned int index)
    130 {
    131   struct WebhookState *ws = cls;
    132   struct TALER_TESTING_Trait traits[] = {
    133     TALER_TESTING_make_trait_process (&ws->webhook_proc),
    134     TALER_TESTING_trait_end ()
    135   };
    136 
    137   return TALER_TESTING_get_trait (traits,
    138                                   ret,
    139                                   trait,
    140                                   index);
    141 }
    142 
    143 
    144 struct TALER_TESTING_Command
    145 TALER_TESTING_cmd_webhook (const char *label,
    146                            const char *config_filename)
    147 {
    148   struct WebhookState *ws;
    149 
    150   ws = GNUNET_new (struct WebhookState);
    151   ws->config_filename = config_filename;
    152 
    153   {
    154     struct TALER_TESTING_Command cmd = {
    155       .cls = ws,
    156       .label = label,
    157       .run = &webhook_run,
    158       .cleanup = &webhook_cleanup,
    159       .traits = &webhook_traits
    160     };
    161 
    162     return cmd;
    163   }
    164 }
    165 
    166 
    167 /* end of testing_api_cmd_webhook.c */