LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_transfers.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 97 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 0.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 lib/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           0 : 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           0 :   struct GetTransfersState *gts = cls;
      98             : 
      99           0 :   gts->gth = NULL;
     100           0 :   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           0 :   switch (hr->http_status)
     111             :   {
     112           0 :   case MHD_HTTP_OK:
     113           0 :     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           0 :     for (unsigned int i = 0; i < transfers_length; ++i)
     121             :     {
     122             :       const struct TALER_TESTING_Command *transfer_cmd;
     123             : 
     124           0 :       transfer_cmd = TALER_TESTING_interpreter_lookup_command (
     125             :         gts->is,
     126           0 :         gts->transfers[i]);
     127             :       {
     128             :         const struct TALER_WireTransferIdentifierRawP *wtid;
     129             : 
     130           0 :         if (GNUNET_OK !=
     131           0 :             TALER_TESTING_get_trait_wtid (transfer_cmd,
     132             :                                           0,
     133             :                                           &wtid))
     134             :         {
     135           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     136             :                       "Could not fetch wire transfer id\n");
     137           0 :           TALER_TESTING_interpreter_fail (gts->is);
     138           0 :           return;
     139             :         }
     140           0 :         if (0 != GNUNET_memcmp (wtid,
     141             :                                 &transfers[i].wtid))
     142             :         {
     143           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     144             :                       "Wire transfer id does not match\n");
     145           0 :           TALER_TESTING_interpreter_fail (gts->is);
     146           0 :           return;
     147             :         }
     148             :       }
     149             :       {
     150             :         const char *payto_uri;
     151             : 
     152           0 :         if (GNUNET_OK !=
     153           0 :             TALER_TESTING_get_trait_string (transfer_cmd,
     154             :                                             0,
     155             :                                             &payto_uri))
     156             :         {
     157           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     158             :                       "Could not fetch wire transfer payto uri\n");
     159           0 :           TALER_TESTING_interpreter_fail (gts->is);
     160           0 :           return;
     161             :         }
     162           0 :         if (0 != strcmp (payto_uri,
     163           0 :                          transfers[i].payto_uri))
     164             :         {
     165           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     166             :                       "Wire transfer payto uri does not match\n");
     167           0 :           TALER_TESTING_interpreter_fail (gts->is);
     168           0 :           return;
     169             :         }
     170             :       }
     171             :       {
     172             :         const struct TALER_Amount *credit_amount;
     173             : 
     174           0 :         if (GNUNET_OK !=
     175           0 :             TALER_TESTING_get_trait_amount_obj (transfer_cmd,
     176             :                                                 0,
     177             :                                                 &credit_amount))
     178             :         {
     179           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     180             :                       "Could not fetch wire transfer credit amount\n");
     181           0 :           TALER_TESTING_interpreter_fail (gts->is);
     182           0 :           return;
     183             :         }
     184           0 :         if ((GNUNET_OK != TALER_amount_cmp_currency (credit_amount,
     185           0 :                                                      &transfers[i].credit_amount))
     186           0 :             ||
     187           0 :             (0 != TALER_amount_cmp (credit_amount,
     188           0 :                                     &transfers[i].credit_amount)))
     189             :         {
     190           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     191             :                       "Wire transfer credit amount does not match\n");
     192           0 :           TALER_TESTING_interpreter_fail (gts->is);
     193           0 :           return;
     194             :         }
     195             :       }
     196             :       {
     197             :         const char *exchange_url;
     198             : 
     199           0 :         if (GNUNET_OK !=
     200           0 :             TALER_TESTING_get_trait_string (transfer_cmd,
     201             :                                             1,
     202             :                                             &exchange_url))
     203             :         {
     204           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     205             :                       "Could not fetch wire transfer exchange url\n");
     206           0 :           TALER_TESTING_interpreter_fail (gts->is);
     207           0 :           return;
     208             :         }
     209           0 :         if (0 != strcmp (exchange_url,
     210           0 :                          transfers[i].exchange_url))
     211             :         {
     212           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     213             :                       "Wire transfer exchange url does not match\n");
     214           0 :           TALER_TESTING_interpreter_fail (gts->is);
     215           0 :           return;
     216             :         }
     217             :       }
     218             :       {
     219             :         const struct GNUNET_TIME_Absolute *execution_time;
     220             : 
     221           0 :         if (GNUNET_OK !=
     222           0 :             TALER_TESTING_get_trait_absolute_time (transfer_cmd,
     223             :                                                    0,
     224             :                                                    &execution_time))
     225             :         {
     226           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     227             :                       "Could not fetch wire transfer execution time\n");
     228           0 :           TALER_TESTING_interpreter_fail (gts->is);
     229           0 :           return;
     230             :         }
     231           0 :         if (execution_time->abs_value_us !=
     232           0 :             transfers[i].execution_time.abs_value_us)
     233             :         {
     234           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     235             :                       "Wire transfer execution time does not match\n");
     236           0 :           TALER_TESTING_interpreter_fail (gts->is);
     237           0 :           return;
     238             :         }
     239             :       }
     240             :     }
     241           0 :     break;
     242           0 :   default:
     243           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     244             :                 "Unhandled HTTP status.\n");
     245             :   }
     246           0 :   TALER_TESTING_interpreter_next (gts->is);
     247             : }
     248             : 
     249             : 
     250             : /**
     251             :  * Run the "get transfer" CMD.
     252             :  *
     253             :  * @param cls closure.
     254             :  * @param cmd command being run now.
     255             :  * @param is interpreter state.
     256             :  */
     257             : static void
     258           0 : get_transfers_run (void *cls,
     259             :                    const struct TALER_TESTING_Command *cmd,
     260             :                    struct TALER_TESTING_Interpreter *is)
     261             : {
     262           0 :   struct GetTransfersState *gts = cls;
     263             : 
     264           0 :   gts->is = is;
     265           0 :   gts->gth = TALER_MERCHANT_transfers_get (is->ctx,
     266             :                                            gts->merchant_url,
     267             :                                            gts->payto_uri,
     268             :                                            GNUNET_TIME_UNIT_FOREVER_ABS,
     269             :                                            GNUNET_TIME_UNIT_ZERO_ABS,
     270             :                                            INT64_MAX,
     271             :                                            0,
     272             :                                            TALER_EXCHANGE_YNA_ALL,
     273             :                                            &get_transfers_cb,
     274             :                                            gts);
     275           0 :   GNUNET_assert (NULL != gts->gth);
     276           0 : }
     277             : 
     278             : 
     279             : /**
     280             :  * Free the state of a "get transfer" CMD, and possibly
     281             :  * cancel a pending operation thereof.
     282             :  *
     283             :  * @param cls closure.
     284             :  * @param cmd command being run.
     285             :  */
     286             : static void
     287           0 : get_transfers_cleanup (void *cls,
     288             :                        const struct TALER_TESTING_Command *cmd)
     289             : {
     290           0 :   struct GetTransfersState *gts = cls;
     291             : 
     292           0 :   if (NULL != gts->gth)
     293             :   {
     294           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     295             :                 "GET /transfer operation did not complete\n");
     296           0 :     TALER_MERCHANT_transfers_get_cancel (gts->gth);
     297             :   }
     298           0 :   GNUNET_array_grow (gts->transfers,
     299             :                      gts->transfers_length,
     300             :                      0);
     301           0 :   GNUNET_free (gts);
     302           0 : }
     303             : 
     304             : 
     305             : /**
     306             :  * Define a GET /transfers CMD.
     307             :  *
     308             :  * @param label command label.
     309             :  * @param merchant_url base URL of the backend serving the
     310             :  *        "refund increase" request.
     311             :  * @param payto_uri payto URI to filter by, NULL for no filter
     312             :  * @param http_code expected HTTP response code
     313             :  * @param ... NULL-terminated list of labels (const char *) of
     314             :  *        transfer (commands) we expect to be returned in the list
     315             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     316             :  * @return the command.
     317             :  */
     318             : struct TALER_TESTING_Command
     319           0 : 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           0 :   gts = GNUNET_new (struct GetTransfersState);
     328           0 :   gts->merchant_url = merchant_url;
     329           0 :   gts->payto_uri = payto_uri;
     330           0 :   gts->http_status = http_code;
     331             :   {
     332             :     const char *clabel;
     333             :     va_list ap;
     334             : 
     335           0 :     va_start (ap, http_code);
     336           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     337             :     {
     338           0 :       GNUNET_array_append (gts->transfers,
     339             :                            gts->transfers_length,
     340             :                            clabel);
     341             :     }
     342           0 :     va_end (ap);
     343             :   }
     344             :   {
     345           0 :     struct TALER_TESTING_Command cmd = {
     346             :       .cls = gts,
     347             :       .label = label,
     348             :       .run = &get_transfers_run,
     349             :       .cleanup = &get_transfers_cleanup
     350             :     };
     351             : 
     352           0 :     return cmd;
     353             :   }
     354             : }
     355             : 
     356             : 
     357             : /* end of testing_api_cmd_get_transfers.c */

Generated by: LCOV version 1.14