merchant

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

testing_api_cmd_patch_otp_device.c (6186B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022 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, or
      8   (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   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_api_cmd_patch_otp_device.c
     21  * @brief command to test PATCH /otp-device
     22  * @author Christian Grothoff
     23  */
     24 #include "platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler_merchant_service.h"
     28 #include "taler_merchant_testing_lib.h"
     29 
     30 
     31 /**
     32  * State of a "PATCH /otp-device" CMD.
     33  */
     34 struct PatchOtpDeviceState
     35 {
     36 
     37   /**
     38    * Handle for a "GET otp_device" request.
     39    */
     40   struct TALER_MERCHANT_OtpDevicePatchHandle *iph;
     41 
     42   /**
     43    * The interpreter state.
     44    */
     45   struct TALER_TESTING_Interpreter *is;
     46 
     47   /**
     48    * Base URL of the merchant serving the request.
     49    */
     50   const char *merchant_url;
     51 
     52   /**
     53    * ID of the otp_device to run GET for.
     54    */
     55   const char *otp_device_id;
     56 
     57   /**
     58    * description of the otp_device
     59    */
     60   const char *otp_device_description;
     61 
     62   /**
     63    * base64-encoded key
     64    */
     65   char *otp_key;
     66 
     67   /**
     68    * Algorithm used by the OTP device
     69    */
     70   enum TALER_MerchantConfirmationAlgorithm otp_alg;
     71 
     72   /**
     73    * Counter of the device (if in counter mode).
     74    */
     75   uint64_t otp_ctr;
     76 
     77   /**
     78    * Expected HTTP response code.
     79    */
     80   unsigned int http_status;
     81 
     82 };
     83 
     84 
     85 /**
     86  * Callback for a PATCH /otp-devices/$ID operation.
     87  *
     88  * @param cls closure for this function
     89  * @param hr response being processed
     90  */
     91 static void
     92 patch_otp_device_cb (void *cls,
     93                      const struct TALER_MERCHANT_HttpResponse *hr)
     94 {
     95   struct PatchOtpDeviceState *pis = cls;
     96 
     97   pis->iph = NULL;
     98   if (pis->http_status != hr->http_status)
     99   {
    100     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    101                 "Unexpected response code %u (%d) to command %s\n",
    102                 hr->http_status,
    103                 (int) hr->ec,
    104                 TALER_TESTING_interpreter_get_current_label (pis->is));
    105     TALER_TESTING_interpreter_fail (pis->is);
    106     return;
    107   }
    108   switch (hr->http_status)
    109   {
    110   case MHD_HTTP_NO_CONTENT:
    111     break;
    112   case MHD_HTTP_UNAUTHORIZED:
    113     break;
    114   case MHD_HTTP_FORBIDDEN:
    115     break;
    116   case MHD_HTTP_NOT_FOUND:
    117     break;
    118   case MHD_HTTP_CONFLICT:
    119     break;
    120   default:
    121     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    122                 "Unhandled HTTP status %u for PATCH /otp-devices/ID.\n",
    123                 hr->http_status);
    124   }
    125   TALER_TESTING_interpreter_next (pis->is);
    126 }
    127 
    128 
    129 /**
    130  * Run the "PATCH /otp-devices/$ID" CMD.
    131  *
    132  *
    133  * @param cls closure.
    134  * @param cmd command being run now.
    135  * @param is interpreter state.
    136  */
    137 static void
    138 patch_otp_device_run (void *cls,
    139                     const struct TALER_TESTING_Command *cmd,
    140                     struct TALER_TESTING_Interpreter *is)
    141 {
    142   struct PatchOtpDeviceState *pis = cls;
    143 
    144   pis->is = is;
    145   pis->iph = TALER_MERCHANT_otp_device_patch (
    146     TALER_TESTING_interpreter_get_context (is),
    147     pis->merchant_url,
    148     pis->otp_device_id,
    149     pis->otp_device_description,
    150     pis->otp_key,
    151     pis->otp_alg,
    152     pis->otp_ctr,
    153     &patch_otp_device_cb,
    154     pis);
    155   GNUNET_assert (NULL != pis->iph);
    156 }
    157 
    158 
    159 /**
    160  * Offers information from the PATCH /otp-devices CMD state to other
    161  * commands.
    162  *
    163  * @param cls closure
    164  * @param[out] ret result (could be anything)
    165  * @param trait name of the trait
    166  * @param index index number of the object to extract.
    167  * @return #GNUNET_OK on success
    168  */
    169 static enum GNUNET_GenericReturnValue
    170 patch_otp_device_traits (void *cls,
    171                          const void **ret,
    172                          const char *trait,
    173                          unsigned int index)
    174 {
    175   struct PatchOtpDeviceState *pts = cls;
    176   struct TALER_TESTING_Trait traits[] = {
    177     TALER_TESTING_make_trait_otp_device_description (pts->otp_device_description),
    178     TALER_TESTING_make_trait_otp_key (pts->otp_key),
    179     TALER_TESTING_make_trait_otp_alg (&pts->otp_alg),
    180     TALER_TESTING_make_trait_otp_id (pts->otp_device_id),
    181     TALER_TESTING_trait_end (),
    182   };
    183 
    184   return TALER_TESTING_get_trait (traits,
    185                                   ret,
    186                                   trait,
    187                                   index);
    188 }
    189 
    190 
    191 /**
    192  * Free the state of a "GET otp_device" CMD, and possibly
    193  * cancel a pending operation thereof.
    194  *
    195  * @param cls closure.
    196  * @param cmd command being run.
    197  */
    198 static void
    199 patch_otp_device_cleanup (void *cls,
    200                           const struct TALER_TESTING_Command *cmd)
    201 {
    202   struct PatchOtpDeviceState *pis = cls;
    203 
    204   if (NULL != pis->iph)
    205   {
    206     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    207                 "PATCH /otp-devices/$ID operation did not complete\n");
    208     TALER_MERCHANT_otp_device_patch_cancel (pis->iph);
    209   }
    210   GNUNET_free (pis->otp_key);
    211   GNUNET_free (pis);
    212 }
    213 
    214 
    215 struct TALER_TESTING_Command
    216 TALER_TESTING_cmd_merchant_patch_otp_device (
    217   const char *label,
    218   const char *merchant_url,
    219   const char *otp_device_id,
    220   const char *otp_device_description,
    221   const char *otp_key,
    222   const enum TALER_MerchantConfirmationAlgorithm otp_alg,
    223   uint64_t otp_ctr,
    224   unsigned int http_status)
    225 {
    226   struct PatchOtpDeviceState *pis;
    227 
    228   pis = GNUNET_new (struct PatchOtpDeviceState);
    229   pis->merchant_url = merchant_url;
    230   pis->otp_device_id = otp_device_id;
    231   pis->http_status = http_status;
    232   pis->otp_device_description = otp_device_description;
    233   pis->otp_key = GNUNET_strdup (otp_key);
    234   pis->otp_alg = otp_alg;
    235   pis->otp_ctr = otp_ctr;
    236   {
    237     struct TALER_TESTING_Command cmd = {
    238       .cls = pis,
    239       .label = label,
    240       .run = &patch_otp_device_run,
    241       .cleanup = &patch_otp_device_cleanup,
    242       .traits = &patch_otp_device_traits
    243     };
    244 
    245     return cmd;
    246   }
    247 }
    248 
    249 
    250 /* end of testing_api_cmd_patch_otp_device.c */