LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_tips.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 78 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_get_tips.c
      21             :  * @brief command to test GET /private/tips
      22             :  * @author Jonathan Buchanan
      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 "GET tips" CMD.
      33             :  */
      34             : struct GetTipsState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET tips" request.
      39             :    */
      40             :   struct TALER_MERCHANT_TipsGetHandle *tgh;
      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             :    * Row to start querying the database from.
      54             :    */
      55             :   uint64_t offset;
      56             : 
      57             :   /**
      58             :    * How many rows to return (with direction).
      59             :    */
      60             :   int64_t limit;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : 
      67             :   /**
      68             :    * Length of @e tips.
      69             :    */
      70             :   unsigned int tips_length;
      71             : 
      72             :   /**
      73             :    * References to tips that we expect to be found.
      74             :    */
      75             :   const char **tips;
      76             : 
      77             : };
      78             : 
      79             : /**
      80             :  * Callback for a GET /private/tips operation.
      81             :  *
      82             :  * @param cls closure for this function
      83             :  * @param hr HTTP response details
      84             :  * @param tips_length length of the @a tips array
      85             :  * @param tips array of tips
      86             :  */
      87             : static void
      88           0 : get_tips_cb (void *cls,
      89             :              const struct TALER_MERCHANT_HttpResponse *hr,
      90             :              unsigned int tips_length,
      91             :              const struct TALER_MERCHANT_TipEntry tips[])
      92             : {
      93           0 :   struct GetTipsState *gts = cls;
      94             : 
      95           0 :   gts->tgh = NULL;
      96           0 :   if (gts->http_status != hr->http_status)
      97             :   {
      98           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      99             :                 "Unexpected response code %u (%d) to command %s\n",
     100             :                 hr->http_status,
     101             :                 (int) hr->ec,
     102             :                 TALER_TESTING_interpreter_get_current_label (gts->is));
     103           0 :     TALER_TESTING_interpreter_fail (gts->is);
     104           0 :     return;
     105             :   }
     106           0 :   switch (hr->http_status)
     107             :   {
     108           0 :   case MHD_HTTP_OK:
     109           0 :     if (tips_length != gts->tips_length)
     110             :     {
     111           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     112             :                   "Tips length does not match\n");
     113           0 :       TALER_TESTING_interpreter_fail (gts->is);
     114           0 :       return;
     115             :     }
     116           0 :     for (unsigned int i = 0; i < tips_length; ++i)
     117             :     {
     118             :       const struct TALER_TESTING_Command *tip_cmd;
     119             : 
     120           0 :       tip_cmd = TALER_TESTING_interpreter_lookup_command (
     121             :         gts->is,
     122           0 :         gts->tips[i]);
     123             :       {
     124             :         const struct GNUNET_HashCode *tip_id;
     125             : 
     126           0 :         if (GNUNET_OK !=
     127           0 :             TALER_TESTING_get_trait_tip_id (tip_cmd,
     128             :                                             0,
     129             :                                             &tip_id))
     130             :         {
     131           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     132             :                       "Could not fetch tip id\n");
     133           0 :           TALER_TESTING_interpreter_fail (gts->is);
     134           0 :           return;
     135             :         }
     136           0 :         if (0 != GNUNET_memcmp (tip_id,
     137             :                                 &tips[i].tip_id))
     138             :         {
     139           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     140             :                       "Tip id does not match\n");
     141           0 :           TALER_TESTING_interpreter_fail (gts->is);
     142           0 :           return;
     143             :         }
     144             :       }
     145             :       {
     146             :         const struct TALER_Amount *tip_amount;
     147             : 
     148           0 :         if (GNUNET_OK !=
     149           0 :             TALER_TESTING_get_trait_amount_obj (tip_cmd,
     150             :                                                 0,
     151             :                                                 &tip_amount))
     152             :         {
     153           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     154             :                       "Could not fetch tip amount\n");
     155           0 :           TALER_TESTING_interpreter_fail (gts->is);
     156           0 :           return;
     157             :         }
     158           0 :         if ((GNUNET_OK != TALER_amount_cmp_currency (tip_amount,
     159           0 :                                                      &tips[i].tip_amount)) ||
     160           0 :             (0 != TALER_amount_cmp (tip_amount,
     161           0 :                                     &tips[i].tip_amount)))
     162             :         {
     163           0 :           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     164             :                       "Tip amount does not match\n");
     165           0 :           TALER_TESTING_interpreter_fail (gts->is);
     166           0 :           return;
     167             :         }
     168             :       }
     169             :     }
     170           0 :     break;
     171           0 :   default:
     172           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     173             :                 "Unhandled HTTP status.\n");
     174             :   }
     175           0 :   TALER_TESTING_interpreter_next (gts->is);
     176             : }
     177             : 
     178             : 
     179             : /**
     180             :  * Run the "GET /private/tips" CMD.
     181             :  *
     182             :  * @param cls closure.
     183             :  * @param cmd command being run now.
     184             :  * @param is interpreter state.
     185             :  */
     186             : static void
     187           0 : get_tips_run (void *cls,
     188             :               const struct TALER_TESTING_Command *cmd,
     189             :               struct TALER_TESTING_Interpreter *is)
     190             : {
     191           0 :   struct GetTipsState *gts = cls;
     192             : 
     193           0 :   gts->is = is;
     194           0 :   gts->tgh = TALER_MERCHANT_tips_get2 (is->ctx,
     195             :                                        gts->merchant_url,
     196             :                                        TALER_EXCHANGE_YNA_NO,
     197             :                                        gts->limit,
     198             :                                        gts->offset,
     199             :                                        &get_tips_cb,
     200             :                                        gts);
     201             : 
     202           0 :   GNUNET_assert (NULL != gts->tgh);
     203           0 : }
     204             : 
     205             : 
     206             : /**
     207             :  * Free the state of a "GET tips" CMD, and possibly
     208             :  * cancel a pending operation thereof.
     209             :  *
     210             :  * @param cls closure.
     211             :  * @param cmd command being run.
     212             :  */
     213             : static void
     214           0 : get_tips_cleanup (void *cls,
     215             :                   const struct TALER_TESTING_Command *cmd)
     216             : {
     217           0 :   struct GetTipsState *gts = cls;
     218             : 
     219           0 :   if (NULL != gts->tgh)
     220             :   {
     221           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     222             :                 "GET /private/tips operation did not complete\n");
     223           0 :     TALER_MERCHANT_tips_get_cancel (gts->tgh);
     224             :   }
     225           0 :   GNUNET_free (gts);
     226           0 : }
     227             : 
     228             : 
     229             : /**
     230             :  * Define a get tips CMD.
     231             :  *
     232             :  * @param label the command label
     233             :  * @param merchant_url base URL of the merchant which will
     234             :  *        server the /tip-query request.
     235             :  * @param http_status expected HTTP response code for the
     236             :  *        /tip-query request.
     237             :  * @param ... NULL-terminated list of labels (const char *) of
     238             :  *        tip (commands) we expect to be returned in the list
     239             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     240             :  */
     241             : struct TALER_TESTING_Command
     242           0 : TALER_TESTING_cmd_get_tips (const char *label,
     243             :                             const char *merchant_url,
     244             :                             unsigned int http_status,
     245             :                             ...)
     246             : {
     247             :   struct GetTipsState *gts;
     248             : 
     249           0 :   gts = GNUNET_new (struct GetTipsState);
     250           0 :   gts->merchant_url = merchant_url;
     251           0 :   gts->offset = INT64_MAX;
     252           0 :   gts->limit = -20;
     253           0 :   gts->http_status = http_status;
     254             :   {
     255             :     const char *clabel;
     256             :     va_list ap;
     257             : 
     258           0 :     va_start (ap, http_status);
     259           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     260             :     {
     261           0 :       GNUNET_array_append (gts->tips,
     262             :                            gts->tips_length,
     263             :                            clabel);
     264             :     }
     265           0 :     va_end (ap);
     266             :   }
     267             :   {
     268           0 :     struct TALER_TESTING_Command cmd = {
     269             :       .cls = gts,
     270             :       .label = label,
     271             :       .run = &get_tips_run,
     272             :       .cleanup = &get_tips_cleanup
     273             :     };
     274             : 
     275           0 :     return cmd;
     276             :   }
     277             : }
     278             : 
     279             : 
     280             : /**
     281             :  * Define a get tips CMD.
     282             :  *
     283             :  * @param label the command label
     284             :  * @param merchant_url base URL of the merchant which will
     285             :  *        server the /tip-query request.
     286             :  * @param http_status expected HTTP response code for the
     287             :  *        /tip-query request.
     288             :  * @param offset row to start querying the database from.
     289             :  * @param limit how many rows to return (with direction).
     290             :  * @param ... NULL-terminated list of labels (const char *) of
     291             :  *        tip (commands) we expect to be returned in the list
     292             :  *        (assuming @a http_code is #MHD_HTTP_OK)
     293             :  */
     294             : struct TALER_TESTING_Command
     295           0 : TALER_TESTING_cmd_get_tips2 (const char *label,
     296             :                              const char *merchant_url,
     297             :                              uint64_t offset,
     298             :                              int64_t limit,
     299             :                              unsigned int http_status,
     300             :                              ...)
     301             : {
     302             :   struct GetTipsState *gts;
     303             : 
     304           0 :   gts = GNUNET_new (struct GetTipsState);
     305           0 :   gts->merchant_url = merchant_url;
     306           0 :   gts->offset = offset;
     307           0 :   gts->limit = limit;
     308           0 :   gts->http_status = http_status;
     309             :   {
     310             :     const char *clabel;
     311             :     va_list ap;
     312             : 
     313           0 :     va_start (ap, http_status);
     314           0 :     while (NULL != (clabel = va_arg (ap, const char *)))
     315             :     {
     316           0 :       GNUNET_array_append (gts->tips,
     317             :                            gts->tips_length,
     318             :                            clabel);
     319             :     }
     320           0 :     va_end (ap);
     321             :   }
     322             :   {
     323           0 :     struct TALER_TESTING_Command cmd = {
     324             :       .cls = gts,
     325             :       .label = label,
     326             :       .run = &get_tips_run,
     327             :       .cleanup = &get_tips_cleanup
     328             :     };
     329             : 
     330           0 :     return cmd;
     331             :   }
     332             : }
     333             : 
     334             : 
     335             : /* end of testing_api_cmd_get_tips.c */

Generated by: LCOV version 1.14