LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_lock_product.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 40 49 81.6 %
Date: 2021-08-30 06:54:17 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          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             :   struct GNUNET_Uuid 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           4 : lock_product_cb (void *cls,
      88             :                  const struct TALER_MERCHANT_HttpResponse *hr)
      89             : {
      90           4 :   struct LockProductState *pis = cls;
      91             : 
      92           4 :   pis->iph = NULL;
      93           4 :   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           4 :   switch (hr->http_status)
     104             :   {
     105           2 :   case MHD_HTTP_NO_CONTENT:
     106           2 :     break;
     107           0 :   case MHD_HTTP_FORBIDDEN:
     108           0 :     break;
     109           1 :   case MHD_HTTP_NOT_FOUND:
     110           1 :     break;
     111           1 :   case MHD_HTTP_GONE:
     112           1 :     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           4 :   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           4 : lock_product_run (void *cls,
     132             :                   const struct TALER_TESTING_Command *cmd,
     133             :                   struct TALER_TESTING_Interpreter *is)
     134             : {
     135           4 :   struct LockProductState *pis = cls;
     136             : 
     137           4 :   pis->is = is;
     138           8 :   pis->iph = TALER_MERCHANT_product_lock (is->ctx,
     139             :                                           pis->merchant_url,
     140             :                                           pis->product_id,
     141           4 :                                           &pis->uuid,
     142             :                                           pis->duration,
     143             :                                           pis->quantity,
     144             :                                           &lock_product_cb,
     145             :                                           pis);
     146           4 :   GNUNET_assert (NULL != pis->iph);
     147           4 : }
     148             : 
     149             : 
     150             : /**
     151             :  * Free the state of a "GET product" CMD, and possibly
     152             :  * cancel a pending operation thereof.
     153             :  *
     154             :  * @param cls closure.
     155             :  * @param cmd command being run.
     156             :  */
     157             : static void
     158           4 : lock_product_cleanup (void *cls,
     159             :                       const struct TALER_TESTING_Command *cmd)
     160             : {
     161           4 :   struct LockProductState *pis = cls;
     162             : 
     163           4 :   if (NULL != pis->iph)
     164             :   {
     165           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     166             :                 "POST /product/$ID/lock operation did not complete\n");
     167           0 :     TALER_MERCHANT_product_lock_cancel (pis->iph);
     168             :   }
     169           4 :   GNUNET_free (pis);
     170           4 : }
     171             : 
     172             : 
     173             : /**
     174             :  * Offer internal data to other 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 int
     183          16 : lock_product_traits (void *cls,
     184             :                      const void **ret,
     185             :                      const char *trait,
     186             :                      unsigned int index)
     187             : {
     188          16 :   struct LockProductState *lps = cls;
     189             : 
     190             :   struct TALER_TESTING_Trait traits[] = {
     191          16 :     TALER_TESTING_make_trait_uuid (0, &lps->uuid),
     192          16 :     TALER_TESTING_trait_end ()
     193             :   };
     194             : 
     195          16 :   return TALER_TESTING_get_trait (traits,
     196             :                                   ret,
     197             :                                   trait,
     198             :                                   index);
     199             : }
     200             : 
     201             : 
     202             : struct TALER_TESTING_Command
     203           4 : TALER_TESTING_cmd_merchant_lock_product (
     204             :   const char *label,
     205             :   const char *merchant_url,
     206             :   const char *product_id,
     207             :   struct GNUNET_TIME_Relative duration,
     208             :   uint32_t quantity,
     209             :   unsigned int http_status)
     210             : {
     211             :   struct LockProductState *pis;
     212             : 
     213           4 :   pis = GNUNET_new (struct LockProductState);
     214           4 :   pis->merchant_url = merchant_url;
     215           4 :   pis->product_id = product_id;
     216           4 :   pis->http_status = http_status;
     217           4 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     218           4 :                               &pis->uuid,
     219             :                               sizeof (struct GNUNET_Uuid));
     220           4 :   pis->duration = duration;
     221           4 :   pis->quantity = quantity;
     222             : 
     223             :   {
     224           4 :     struct TALER_TESTING_Command cmd = {
     225             :       .cls = pis,
     226             :       .label = label,
     227             :       .run = &lock_product_run,
     228             :       .cleanup = &lock_product_cleanup,
     229             :       .traits = &lock_product_traits
     230             :     };
     231             : 
     232           4 :     return cmd;
     233             :   }
     234             : }
     235             : 
     236             : 
     237             : /* end of testing_api_cmd_lock_product.c */

Generated by: LCOV version 1.14