LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_lock_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 43 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 0.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 lib/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             :  */
      85             : static void
      86           0 : lock_product_cb (void *cls,
      87             :                  const struct TALER_MERCHANT_HttpResponse *hr)
      88             : {
      89           0 :   struct LockProductState *pis = cls;
      90             : 
      91           0 :   pis->iph = NULL;
      92           0 :   if (pis->http_status != hr->http_status)
      93             :   {
      94           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      95             :                 "Unexpected response code %u (%d) to command %s\n",
      96             :                 hr->http_status,
      97             :                 (int) hr->ec,
      98             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
      99           0 :     TALER_TESTING_interpreter_fail (pis->is);
     100           0 :     return;
     101             :   }
     102           0 :   switch (hr->http_status)
     103             :   {
     104           0 :   case MHD_HTTP_OK:
     105           0 :     break;
     106             :   // FIXME: add other legitimate states here...
     107           0 :   default:
     108           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     109             :                 "Unhandled HTTP status.\n");
     110             :   }
     111           0 :   TALER_TESTING_interpreter_next (pis->is);
     112             : }
     113             : 
     114             : 
     115             : /**
     116             :  * Run the "LOCK /products/$ID" CMD.
     117             :  *
     118             :  *
     119             :  * @param cls closure.
     120             :  * @param cmd command being run now.
     121             :  * @param is interpreter state.
     122             :  */
     123             : static void
     124           0 : lock_product_run (void *cls,
     125             :                   const struct TALER_TESTING_Command *cmd,
     126             :                   struct TALER_TESTING_Interpreter *is)
     127             : {
     128           0 :   struct LockProductState *pis = cls;
     129             : 
     130           0 :   pis->is = is;
     131           0 :   pis->iph = TALER_MERCHANT_product_lock (is->ctx,
     132             :                                           pis->merchant_url,
     133             :                                           pis->product_id,
     134           0 :                                           &pis->uuid,
     135             :                                           pis->duration,
     136             :                                           pis->quantity,
     137             :                                           &lock_product_cb,
     138             :                                           pis);
     139           0 :   GNUNET_assert (NULL != pis->iph);
     140           0 : }
     141             : 
     142             : 
     143             : /**
     144             :  * Free the state of a "GET product" CMD, and possibly
     145             :  * cancel a pending operation thereof.
     146             :  *
     147             :  * @param cls closure.
     148             :  * @param cmd command being run.
     149             :  */
     150             : static void
     151           0 : lock_product_cleanup (void *cls,
     152             :                       const struct TALER_TESTING_Command *cmd)
     153             : {
     154           0 :   struct LockProductState *pis = cls;
     155             : 
     156           0 :   if (NULL != pis->iph)
     157             :   {
     158           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     159             :                 "POST /product/$ID/lock operation did not complete\n");
     160           0 :     TALER_MERCHANT_product_lock_cancel (pis->iph);
     161             :   }
     162           0 :   GNUNET_free (pis);
     163           0 : }
     164             : 
     165             : 
     166             : /**
     167             :  * Offer internal data to other commands.
     168             :  *
     169             :  * @param cls closure
     170             :  * @param ret[out] 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 int
     176           0 : lock_product_traits (void *cls,
     177             :                      const void **ret,
     178             :                      const char *trait,
     179             :                      unsigned int index)
     180             : {
     181           0 :   struct LockProductState *lps = cls;
     182             : 
     183             :   struct TALER_TESTING_Trait traits[] = {
     184           0 :     TALER_TESTING_make_trait_uuid (0, &lps->uuid),
     185           0 :     TALER_TESTING_trait_end ()
     186             :   };
     187             : 
     188           0 :   return TALER_TESTING_get_trait (traits,
     189             :                                   ret,
     190             :                                   trait,
     191             :                                   index);
     192             : }
     193             : 
     194             : 
     195             : /**
     196             :  * Define a "LOCK /products/$ID" CMD.
     197             :  *
     198             :  * @param label command label.
     199             :  * @param merchant_url base URL of the merchant serving the
     200             :  *        LOCK /product request.
     201             :  * @param product_id the ID of the product to lock.
     202             :  * @param duration how long the lock should last.
     203             :  * @param quantity how money units to lock.
     204             :  * @param http_status expected HTTP response code.
     205             :  * @return the command.
     206             :  */
     207             : struct TALER_TESTING_Command
     208           0 : TALER_TESTING_cmd_merchant_lock_product (
     209             :   const char *label,
     210             :   const char *merchant_url,
     211             :   const char *product_id,
     212             :   struct GNUNET_TIME_Relative duration,
     213             :   uint32_t quantity,
     214             :   unsigned int http_status)
     215             : {
     216             :   struct LockProductState *pis;
     217             : 
     218           0 :   pis = GNUNET_new (struct LockProductState);
     219           0 :   pis->merchant_url = merchant_url;
     220           0 :   pis->product_id = product_id;
     221           0 :   pis->http_status = http_status;
     222           0 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     223           0 :                               &pis->uuid,
     224             :                               sizeof (struct GNUNET_Uuid));
     225           0 :   pis->duration = duration;
     226           0 :   pis->quantity = quantity;
     227             : 
     228             :   {
     229           0 :     struct TALER_TESTING_Command cmd = {
     230             :       .cls = pis,
     231             :       .label = label,
     232             :       .run = &lock_product_run,
     233             :       .cleanup = &lock_product_cleanup,
     234             :       .traits = &lock_product_traits
     235             :     };
     236             : 
     237           0 :     return cmd;
     238             :   }
     239             : }
     240             : 
     241             : 
     242             : /* end of testing_api_cmd_lock_product.c */

Generated by: LCOV version 1.14