LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_transfers.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 60 103 58.3 %
Date: 2021-08-30 06:54:17 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018, 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_get_transfers.c
      21             :  * @brief command to test GET /transfers.
      22             :  * @author Marcello Stanisci
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include <taler/taler_exchange_service.h>
      27             : #include <taler/taler_testing_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "taler_merchant_testing_lib.h"
      30             : 
      31             : 
      32             : /**
      33             :  * State of a GET transfers CMD.
      34             :  */
      35             : struct GetTransfersState
      36             : {
      37             : 
      38             :   /**
      39             :    * Handle for a "get transfer" request.
      40             :    */
      41             :   struct TALER_MERCHANT_GetTransfersHandle *gth;
      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             :    * payto URI of the merchant to filter by.
      55             :    */
      56             :   const char *payto_uri;
      57             : 
      58             :   /**
      59             :    * Expected HTTP response code.
      60             :    */
      61             :   unsigned int http_status;
      62             : 
      63             :   /**
      64             :    * Reference for a "check bank" CMD.  It offers the
      65             :    * WTID to get.
      66             :    */
      67             :   const char *check_bank_reference;
      68             : 
      69             :   /**
      70             :    * Array of POST /transfer command labels we expect to see listed.
      71             :    */
      72             :   const char **transfers;
      73             : 
      74             :   /**
      75             :    * Length of @e transfers.
      76             :    */
      77             :   unsigned int transfers_length;
      78             : 
      79             : };
      80             : 
      81             : 
      82             : /**
      83             :  * Check the result of our GET /transfers request to a merchant
      84             :  *
      85             :  * @param cls closure
      86             :  * @param hr HTTP response details
      87             :  * @param transfers_length length of the @a transfers array
      88             :  * @param transfers array with details about the transfers we received
      89             :  */
      90             : static void
      91           1 : get_transfers_cb (
      92             :   void *cls,
      93             :   const struct TALER_MERCHANT_HttpResponse *hr,
      94             :   unsigned int transfers_length,
      95             :   const struct TALER_MERCHANT_TransferData transfers[])
      96             : {
      97           1 :   struct GetTransfersState *gts = cls;
      98             : 
      99           1 :   gts->gth = NULL;
     100           1 :   if (gts->http_status != hr->http_status)
     101             :   {
     102           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     103             :                 "Unexpected response code %u (%d) to command %s\n",
     104             :                 hr->http_status,
     105             :                 (int) hr->ec,
     106             :                 TALER_TESTING_interpreter_get_current_label (gts->is));
     107           0 :     TALER_TESTING_interpreter_fail (gts->is);
     108           0 :     return;
     109             :   }
     110           1 :   switch (hr->http_status)
     111             :   {
     112           1 :   case MHD_HTTP_OK:
     113           1 :     if (transfers_length != gts->transfers_length)
     114             :     {
     115           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     116             :                   "Transfers length does not match\n");
     117           0 :       TALER_TESTING_interpreter_fail (gts->is);
     118           0 :       return;
     119             :     }
     120           3 :     for (unsigned int i = 0; i < transfers_length; ++i)
     121             :     {
     122             :       const struct TALER_TESTING_Command *transfer_cmd;
     123             : 
     124           2 :       transfer_cmd = TALER_TESTING_interpreter_lookup_command (
     125             :         gts->is,
     126           2 :         gts->transfers[i]);
     127           2 :       if (NULL == transfer_cmd)
     128             :       {
     129           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                     "Command `%s' not found!\n",
     131             :                     gts->transfers[i]);
     132           0 :         TALER_TESTING_interpreter_fail (gts->is);
     133           0 :         return;
     134             :       }
     135             :       {
     136             :         const struct TALER_WireTransferIdentifierRawP *wtid;
     137             : 
     138           2 :         if (GNUNET_OK !=
     139           2 :             TALER_TESTING_get_trait_wtid (transfer_cmd,
     140             :                                           0,
     141             :                                           &wtid))
     142             :         {
     143           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     144             :                       "Could not fetch wire transfer id\n");
     145           0 :           TALER_TESTING_interpreter_fail (gts->is);
     146           0 :           return;
     147             :         }
     148           2 :         if (0 != GNUNET_memcmp (wtid,
     149             :                                 &transfers[i].wtid))
     150             :         {
     151           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     152             :                       "Wire transfer id does not match\n");
     153           0 :           TALER_TESTING_interpreter_fail (gts->is);
     154           0 :           return;
     155             :         }
     156           2 :         TALER_TESTING_cmd_merchant_post_transfer_set_serial (
     157             :           (struct TALER_TESTING_Command *) transfer_cmd,
     158           2 :           transfers[i].credit_serial);
     159             :       }
     160             :       {
     161             :         const char *payto_uri;
     162             : 
     163           2 :         if (GNUNET_OK !=
     164           2 :             TALER_TESTING_get_trait_string (transfer_cmd,
     165             :                                             0,
     166             :                                             &payto_uri))
     167             :         {
     168           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     169             :                       "Could not fetch wire transfer payto uri\n");
     170           0 :           TALER_TESTING_interpreter_fail (gts->is);
     171           0 :           return;
     172             :         }
     173           2 :         if (0 != strcmp (payto_uri,
     174           2 :                          transfers[i].payto_uri))
     175             :         {
     176           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     177             :                       "Wire transfer payto uri does not match: %s != %s\n",
     178             :                       payto_uri,
     179             :                       transfers[i].payto_uri);
     180           0 :           TALER_TESTING_interpreter_fail (gts->is);
     181           0 :           return;
     182             :         }
     183             :       }
     184             :       {
     185             :         const struct TALER_Amount *credit_amount;
     186             : 
     187           2 :         if (GNUNET_OK !=
     188           2 :             TALER_TESTING_get_trait_amount_obj (transfer_cmd,
     189             :                                                 0,
     190             :                                                 &credit_amount))
     191             :         {
     192           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     193             :                       "Could not fetch wire transfer credit amount\n");
     194           0 :           TALER_TESTING_interpreter_fail (gts->is);
     195           0 :           return;
     196             :         }
     197           2 :         if ((GNUNET_OK != TALER_amount_cmp_currency (credit_amount,
     198           2 :                                                      &transfers[i].credit_amount))
     199           2 :             ||
     200           2 :             (0 != TALER_amount_cmp (credit_amount,
     201           2 :                                     &transfers[i].credit_amount)))
     202             :         {
     203           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     204             :                       "Wire transfer credit amount does not match\n");
     205           0 :           TALER_TESTING_interpreter_fail (gts->is);
     206           0 :           return;
     207             :         }
     208             :       }
     209             :       {
     210             :         const char *exchange_url;
     211             : 
     212           2 :         if (GNUNET_OK !=
     213           2 :             TALER_TESTING_get_trait_string (transfer_cmd,
     214             :                                             1,
     215             :                                             &exchange_url))
     216             :         {
     217           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     218             :                       "Could not fetch wire transfer exchange url\n");
     219           0 :           TALER_TESTING_interpreter_fail (gts->is);
     220           0 :           return;
     221             :         }
     222           2 :         if (0 != strcmp (exchange_url,
     223           2 :                          transfers[i].exchange_url))
     224             :         {
     225           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     226             :                       "Wire transfer exchange url does not match\n");
     227           0 :           TALER_TESTING_interpreter_fail (gts->is);
     228           0 :           return;
     229             :         }
     230             :       }
     231             :       {
     232             :         const struct GNUNET_TIME_Absolute *execution_time;
     233             : 
     234           2 :         if (GNUNET_OK !=
     235           2 :             TALER_TESTING_get_trait_absolute_time (transfer_cmd,
     236             :                                                    0,
     237             :                                                    &execution_time))
     238             :         {
     239           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     240             :                       "Could not fetch wire transfer execution time\n");
     241           0 :           TALER_TESTING_interpreter_fail (gts->is);
     242           0 :           return;
     243             :         }
     244           2 :         if (execution_time->abs_value_us !=
     245           2 :             transfers[i].execution_time.abs_value_us)
     246             :         {
     247           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     248             :                       "Wire transfer execution time does not match\n");
     249           0 :           TALER_TESTING_interpreter_fail (gts->is);
     250           0 :           return;
     251             :         }
     252             :       }
     253             :     }
     254           1 :     break;
     255           0 :   default:
     256           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     257             :                 "Unhandled HTTP status.\n");
     258             :   }
     259           1 :   TALER_TESTING_interpreter_next (gts->is);
     260             : }
     261             : 
     262             : 
     263             : /**
     264             :  * Run the "get transfer" CMD.
     265             :  *
     266             :  * @param cls closure.
     267             :  * @param cmd command being run now.
     268             :  * @param is interpreter state.
     269             :  */
     270             : static void
     271           1 : get_transfers_run (void *cls,
     272             :                    const struct TALER_TESTING_Command *cmd,
     273             :                    struct TALER_TESTING_Interpreter *is)
     274             : {
     275           1 :   struct GetTransfersState *gts = cls;
     276             : 
     277           1 :   gts->is = is;
     278           1 :   gts->gth = TALER_MERCHANT_transfers_get (is->ctx,
     279             :                                            gts->merchant_url,
     280             :                                            gts->payto_uri,
     281             :                                            GNUNET_TIME_UNIT_FOREVER_ABS,
     282             :                                            GNUNET_TIME_UNIT_ZERO_ABS,
     283             :                                            INT64_MAX,
     284             :                                            0,
     285             :                                            TALER_EXCHANGE_YNA_ALL,
     286             :                                            &get_transfers_cb,
     287             :                                            gts);
     288           1 :   GNUNET_assert (NULL != gts->gth);
     289           1 : }
     290             : 
     291             : 
     292             : /**
     293             :  * Free the state of a "get transfer" CMD, and possibly
     294             :  * cancel a pending operation thereof.
     295             :  *
     296             :  * @param cls closure.
     297             :  * @param cmd command being run.
     298             :  */
     299             : static void
     300           1 : get_transfers_cleanup (void *cls,
     301             :                        const struct TALER_TESTING_Command *cmd)
     302             : {
     303           1 :   struct GetTransfersState *gts = cls;
     304             : 
     305           1 :   if (NULL != gts->gth)
     306             :   {
     307           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     308             :                 "GET /transfer operation did not complete\n");
     309           0 :     TALER_MERCHANT_transfers_get_cancel (gts->gth);
     310             :   }
     311           1 :   GNUNET_array_grow (gts->transfers,
     312             :                      gts->transfers_length,
     313             :                      0);
     314           1 :   GNUNET_free (gts);
     315           1 : }
     316             : 
     317             : 
     318             : struct TALER_TESTING_Command
     319           1 : TALER_TESTING_cmd_merchant_get_transfers (const char *label,
     320             :                                           const char *merchant_url,
     321             :                                           const char *payto_uri,
     322             :                                           unsigned int http_code,
     323             :                                           ...)
     324             : {
     325             :   struct GetTransfersState *gts;
     326             : 
     327           1 :   gts = GNUNET_new (struct GetTransfersState);
     328           1 :   gts->merchant_url = merchant_url;
     329           1 :   gts->payto_uri = payto_uri;
     330           1 :   gts->http_status = http_code;
     331             :   {
     332             :     const char *clabel;
     333             :     va_list ap;
     334             : 
     335           1 :     va_start (ap, http_code);
     336           3 :     while (NULL != (clabel = va_arg (ap, const char *)))
     337             :     {
     338           2 :       GNUNET_array_append (gts->transfers,
     339             :                            gts->transfers_length,
     340             :                            clabel);
     341             :     }
     342           1 :     va_end (ap);
     343             :   }
     344             :   {
     345           1 :     struct TALER_TESTING_Command cmd = {
     346             :       .cls = gts,
     347             :       .label = label,
     348             :       .run = &get_transfers_run,
     349             :       .cleanup = &get_transfers_cleanup
     350             :     };
     351             : 
     352           1 :     return cmd;
     353             :   }
     354             : }
     355             : 
     356             : 
     357             : /* end of testing_api_cmd_get_transfers.c */

Generated by: LCOV version 1.14