merchant

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

testing_api_cmd_patch_otp_device.c (6783B)


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