merchant

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

testing_api_cmd_patch_otp_device.c (6886B)


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