LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_lock_product.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 82.0 % 50 41
Test Date: 2025-11-13 17:46:01 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020 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_lock_product.c
      21              :  * @brief command to test LOCK /product
      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 /products/$ID" CMD.
      33              :  */
      34              : struct LockProductState
      35              : {
      36              : 
      37              :   /**
      38              :    * Handle for a "GET product" request.
      39              :    */
      40              :   struct TALER_MERCHANT_ProductLockHandle *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 product to run GET for.
      54              :    */
      55              :   const char *product_id;
      56              : 
      57              :   /**
      58              :    * UUID that identifies the client holding the lock
      59              :    */
      60              :   char *uuid;
      61              : 
      62              :   /**
      63              :    * duration how long should the lock be held
      64              :    */
      65              :   struct GNUNET_TIME_Relative duration;
      66              : 
      67              :   /**
      68              :    * how much product should be locked
      69              :    */
      70              :   uint32_t quantity;
      71              : 
      72              :   /**
      73              :    * Expected HTTP response code.
      74              :    */
      75              :   unsigned int http_status;
      76              : 
      77              : };
      78              : 
      79              : 
      80              : /**
      81              :  * Callback for a POST /products/$ID/lock operation.
      82              :  *
      83              :  * @param cls closure for this function
      84              :  * @param hr response being processed
      85              :  */
      86              : static void
      87            8 : lock_product_cb (void *cls,
      88              :                  const struct TALER_MERCHANT_HttpResponse *hr)
      89              : {
      90            8 :   struct LockProductState *pis = cls;
      91              : 
      92            8 :   pis->iph = NULL;
      93            8 :   if (pis->http_status != hr->http_status)
      94              :   {
      95            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      96              :                 "Unexpected response code %u (%d) to command %s\n",
      97              :                 hr->http_status,
      98              :                 (int) hr->ec,
      99              :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     100            0 :     TALER_TESTING_interpreter_fail (pis->is);
     101            0 :     return;
     102              :   }
     103            8 :   switch (hr->http_status)
     104              :   {
     105            4 :   case MHD_HTTP_NO_CONTENT:
     106            4 :     break;
     107            0 :   case MHD_HTTP_FORBIDDEN:
     108            0 :     break;
     109            2 :   case MHD_HTTP_NOT_FOUND:
     110            2 :     break;
     111            2 :   case MHD_HTTP_GONE:
     112            2 :     break;
     113            0 :   default:
     114            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     115              :                 "Unhandled HTTP status %u for lock product.\n",
     116              :                 hr->http_status);
     117              :   }
     118            8 :   TALER_TESTING_interpreter_next (pis->is);
     119              : }
     120              : 
     121              : 
     122              : /**
     123              :  * Run the "LOCK /products/$ID" CMD.
     124              :  *
     125              :  *
     126              :  * @param cls closure.
     127              :  * @param cmd command being run now.
     128              :  * @param is interpreter state.
     129              :  */
     130              : static void
     131            8 : lock_product_run (void *cls,
     132              :                   const struct TALER_TESTING_Command *cmd,
     133              :                   struct TALER_TESTING_Interpreter *is)
     134              : {
     135            8 :   struct LockProductState *pis = cls;
     136              : 
     137            8 :   pis->is = is;
     138            8 :   pis->iph = TALER_MERCHANT_product_lock (
     139              :     TALER_TESTING_interpreter_get_context (is),
     140              :     pis->merchant_url,
     141              :     pis->product_id,
     142            8 :     pis->uuid,
     143              :     pis->duration,
     144              :     pis->quantity,
     145              :     &lock_product_cb,
     146              :     pis);
     147            8 :   GNUNET_assert (NULL != pis->iph);
     148            8 : }
     149              : 
     150              : 
     151              : /**
     152              :  * Free the state of a "GET product" CMD, and possibly
     153              :  * cancel a pending operation thereof.
     154              :  *
     155              :  * @param cls closure.
     156              :  * @param cmd command being run.
     157              :  */
     158              : static void
     159            8 : lock_product_cleanup (void *cls,
     160              :                       const struct TALER_TESTING_Command *cmd)
     161              : {
     162            8 :   struct LockProductState *pis = cls;
     163              : 
     164            8 :   if (NULL != pis->iph)
     165              :   {
     166            0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     167              :                 "POST /product/$ID/lock operation did not complete\n");
     168            0 :     TALER_MERCHANT_product_lock_cancel (pis->iph);
     169              :   }
     170            8 :   GNUNET_free (pis->uuid);
     171            8 :   GNUNET_free (pis);
     172            8 : }
     173              : 
     174              : 
     175              : /**
     176              :  * Offer internal data to other commands.
     177              :  *
     178              :  * @param cls closure
     179              :  * @param[out] ret result (could be anything)
     180              :  * @param trait name of the trait
     181              :  * @param index index number of the object to extract.
     182              :  * @return #GNUNET_OK on success
     183              :  */
     184              : static enum GNUNET_GenericReturnValue
     185            2 : lock_product_traits (void *cls,
     186              :                      const void **ret,
     187              :                      const char *trait,
     188              :                      unsigned int index)
     189              : {
     190            2 :   struct LockProductState *lps = cls;
     191              :   struct TALER_TESTING_Trait traits[] = {
     192            2 :     TALER_TESTING_make_trait_lock_uuid (lps->uuid),
     193            2 :     TALER_TESTING_trait_end ()
     194              :   };
     195              : 
     196            2 :   return TALER_TESTING_get_trait (traits,
     197              :                                   ret,
     198              :                                   trait,
     199              :                                   index);
     200              : }
     201              : 
     202              : 
     203              : struct TALER_TESTING_Command
     204            8 : TALER_TESTING_cmd_merchant_lock_product (
     205              :   const char *label,
     206              :   const char *merchant_url,
     207              :   const char *product_id,
     208              :   struct GNUNET_TIME_Relative duration,
     209              :   uint32_t quantity,
     210              :   unsigned int http_status)
     211              : {
     212              :   struct LockProductState *pis;
     213              :   struct GNUNET_Uuid uuid;
     214              : 
     215            8 :   pis = GNUNET_new (struct LockProductState);
     216            8 :   pis->merchant_url = merchant_url;
     217            8 :   pis->product_id = product_id;
     218            8 :   pis->http_status = http_status;
     219            8 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     220              :                               &uuid,
     221              :                               sizeof (struct GNUNET_Uuid));
     222            8 :   pis->uuid = GNUNET_STRINGS_data_to_string_alloc (&uuid,
     223              :                                                    sizeof (uuid));
     224            8 :   pis->duration = duration;
     225            8 :   pis->quantity = quantity;
     226              : 
     227              :   {
     228            8 :     struct TALER_TESTING_Command cmd = {
     229              :       .cls = pis,
     230              :       .label = label,
     231              :       .run = &lock_product_run,
     232              :       .cleanup = &lock_product_cleanup,
     233              :       .traits = &lock_product_traits
     234              :     };
     235              : 
     236            8 :     return cmd;
     237              :   }
     238              : }
     239              : 
     240              : 
     241              : /* end of testing_api_cmd_lock_product.c */
        

Generated by: LCOV version 2.0-1