LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_post_otp_devices.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 39 58 67.2 %
Date: 2025-06-23 16:22:09 Functions: 5 5 100.0 %

          Line data    Source code
       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_post_otp_devices.c
      21             :  * @brief command to test POST /otp-devices
      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 "POST /otp-devices" CMD.
      33             :  */
      34             : struct PostOtpDevicesState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET otp_device" request.
      39             :    */
      40             :   struct TALER_MERCHANT_OtpDevicesPostHandle *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 POST 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             :    * Option that add amount of the order
      69             :    */
      70             :   enum TALER_MerchantConfirmationAlgorithm otp_alg;
      71             : 
      72             :   /**
      73             :    * Counter at the OTP device.
      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 POST /otp-devices operation.
      87             :  *
      88             :  * @param cls closure for this function
      89             :  * @param hr response being processed
      90             :  */
      91             : static void
      92           4 : post_otp_devices_cb (void *cls,
      93             :                    const struct TALER_MERCHANT_HttpResponse *hr)
      94             : {
      95           4 :   struct PostOtpDevicesState *tis = cls;
      96             : 
      97           4 :   tis->iph = NULL;
      98           4 :   if (tis->http_status != hr->http_status)
      99             :   {
     100           0 :     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 (tis->is));
     105           0 :     TALER_TESTING_interpreter_fail (tis->is);
     106           0 :     return;
     107             :   }
     108           4 :   switch (hr->http_status)
     109             :   {
     110           4 :   case MHD_HTTP_NO_CONTENT:
     111           4 :     break;
     112           0 :   case MHD_HTTP_UNAUTHORIZED:
     113           0 :     break;
     114           0 :   case MHD_HTTP_FORBIDDEN:
     115           0 :     break;
     116           0 :   case MHD_HTTP_NOT_FOUND:
     117           0 :     break;
     118           0 :   case MHD_HTTP_CONFLICT:
     119           0 :     break;
     120           0 :   default:
     121           0 :     GNUNET_break (0);
     122           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     123             :                 "Unhandled HTTP status %u for POST /otp-devices.\n",
     124             :                 hr->http_status);
     125             :   }
     126           4 :   TALER_TESTING_interpreter_next (tis->is);
     127             : }
     128             : 
     129             : 
     130             : /**
     131             :  * Run the "POST /otp-devices" CMD.
     132             :  *
     133             :  *
     134             :  * @param cls closure.
     135             :  * @param cmd command being run now.
     136             :  * @param is interpreter state.
     137             :  */
     138             : static void
     139           4 : post_otp_devices_run (void *cls,
     140             :                     const struct TALER_TESTING_Command *cmd,
     141             :                     struct TALER_TESTING_Interpreter *is)
     142             : {
     143           4 :   struct PostOtpDevicesState *tis = cls;
     144             : 
     145           4 :   tis->is = is;
     146           4 :   tis->iph = TALER_MERCHANT_otp_devices_post (
     147             :     TALER_TESTING_interpreter_get_context (is),
     148             :     tis->merchant_url,
     149             :     tis->otp_device_id,
     150             :     tis->otp_device_description,
     151           4 :     tis->otp_key,
     152             :     tis->otp_alg,
     153             :     tis->otp_ctr,
     154             :     &post_otp_devices_cb,
     155             :     tis);
     156           4 :   if (NULL == tis->iph)
     157             :   {
     158           0 :     GNUNET_break (0);
     159           0 :     TALER_TESTING_interpreter_fail (tis->is);
     160           0 :     return;
     161             :   }
     162             : }
     163             : 
     164             : 
     165             : /**
     166             :  * Offers information from the POST /otp-devices CMD state to other
     167             :  * commands.
     168             :  *
     169             :  * @param cls closure
     170             :  * @param[out] ret result (could be anything)
     171             :  * @param trait name of the trait
     172             :  * @param index index number of the object to extract.
     173             :  * @return #GNUNET_OK on success
     174             :  */
     175             : static enum GNUNET_GenericReturnValue
     176           4 : post_otp_devices_traits (void *cls,
     177             :                        const void **ret,
     178             :                        const char *trait,
     179             :                        unsigned int index)
     180             : {
     181           4 :   struct PostOtpDevicesState *pts = cls;
     182             :   struct TALER_TESTING_Trait traits[] = {
     183           4 :     TALER_TESTING_make_trait_otp_device_description (pts->otp_device_description),
     184           4 :     TALER_TESTING_make_trait_otp_key (pts->otp_key),
     185           4 :     TALER_TESTING_make_trait_otp_alg (&pts->otp_alg),
     186           4 :     TALER_TESTING_make_trait_otp_id (pts->otp_device_id),
     187           4 :     TALER_TESTING_trait_end (),
     188             :   };
     189             : 
     190           4 :   return TALER_TESTING_get_trait (traits,
     191             :                                   ret,
     192             :                                   trait,
     193             :                                   index);
     194             : }
     195             : 
     196             : 
     197             : /**
     198             :  * Free the state of a "POST otp_device" CMD, and possibly
     199             :  * cancel a pending operation thereof.
     200             :  *
     201             :  * @param cls closure.
     202             :  * @param cmd command being run.
     203             :  */
     204             : static void
     205           4 : post_otp_devices_cleanup (void *cls,
     206             :                           const struct TALER_TESTING_Command *cmd)
     207             : {
     208           4 :   struct PostOtpDevicesState *tis = cls;
     209             : 
     210           4 :   if (NULL != tis->iph)
     211             :   {
     212           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     213             :                 "POST /otp-devices operation did not complete\n");
     214           0 :     TALER_MERCHANT_otp_devices_post_cancel (tis->iph);
     215             :   }
     216           4 :   GNUNET_free (tis->otp_key);
     217           4 :   GNUNET_free (tis);
     218           4 : }
     219             : 
     220             : 
     221             : struct TALER_TESTING_Command
     222           4 : TALER_TESTING_cmd_merchant_post_otp_devices (
     223             :   const char *label,
     224             :   const char *merchant_url,
     225             :   const char *otp_device_id,
     226             :   const char *otp_device_description,
     227             :   const char *otp_key,
     228             :   const enum TALER_MerchantConfirmationAlgorithm otp_alg,
     229             :   uint64_t otp_ctr,
     230             :   unsigned int http_status)
     231             : {
     232             :   struct PostOtpDevicesState *tis;
     233             : 
     234           4 :   tis = GNUNET_new (struct PostOtpDevicesState);
     235           4 :   tis->merchant_url = merchant_url;
     236           4 :   tis->otp_device_id = otp_device_id;
     237           4 :   tis->http_status = http_status;
     238           4 :   tis->otp_device_description = otp_device_description;
     239           4 :   tis->otp_key = GNUNET_strdup (otp_key);
     240           4 :   tis->otp_alg = otp_alg;
     241           4 :   tis->otp_ctr = otp_ctr;
     242             :   {
     243           4 :     struct TALER_TESTING_Command cmd = {
     244             :       .cls = tis,
     245             :       .label = label,
     246             :       .run = &post_otp_devices_run,
     247             :       .cleanup = &post_otp_devices_cleanup,
     248             :       .traits = &post_otp_devices_traits
     249             :     };
     250             : 
     251           4 :     return cmd;
     252             :   }
     253             : }
     254             : 
     255             : 
     256             : /* end of testing_api_cmd_post_otp_devices.c */

Generated by: LCOV version 1.16