LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_instance_auth.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 43 62.8 %
Date: 2025-06-23 16:22:09 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2021 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_instance_auth.c
      21             :  * @brief command to test /private/auth POSTing
      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 /instances/$ID/private/auth" CMD.
      33             :  */
      34             : struct AuthInstanceState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "POST auth" request.
      39             :    */
      40             :   struct TALER_MERCHANT_InstanceAuthPostHandle *iaph;
      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 instance to run GET for.
      54             :    */
      55             :   const char *instance_id;
      56             : 
      57             :   /**
      58             :    * Desired token. Can be NULL
      59             :    */
      60             :   const char *auth_token;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : 
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Callback for a POST /instances/$ID/private/auth operation.
      72             :  *
      73             :  * @param cls closure for this function
      74             :  * @param hr response being processed
      75             :  */
      76             : static void
      77           6 : auth_instance_cb (void *cls,
      78             :                   const struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80           6 :   struct AuthInstanceState *ais = cls;
      81             : 
      82           6 :   ais->iaph = NULL;
      83           6 :   if (ais->http_status != hr->http_status)
      84             :   {
      85           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      86             :                 "Unexpected response code %u (%d) to command %s\n",
      87             :                 hr->http_status,
      88             :                 (int) hr->ec,
      89             :                 TALER_TESTING_interpreter_get_current_label (ais->is));
      90           0 :     TALER_TESTING_interpreter_fail (ais->is);
      91           0 :     return;
      92             :   }
      93           6 :   switch (hr->http_status)
      94             :   {
      95           6 :   case MHD_HTTP_NO_CONTENT:
      96           6 :     break;
      97           0 :   case MHD_HTTP_BAD_REQUEST:
      98             :     /* likely invalid auth_token value, we do not check client-side */
      99           0 :     break;
     100           0 :   case MHD_HTTP_FORBIDDEN:
     101           0 :     break;
     102           0 :   default:
     103           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     104             :                 "Unhandled HTTP status %u (%d) returned from /private/auth operation.\n",
     105             :                 hr->http_status,
     106             :                 hr->ec);
     107             :   }
     108           6 :   TALER_TESTING_interpreter_next (ais->is);
     109             : }
     110             : 
     111             : 
     112             : /**
     113             :  * Run the "AUTH /instances/$ID" CMD.
     114             :  *
     115             :  *
     116             :  * @param cls closure.
     117             :  * @param cmd command being run now.
     118             :  * @param is interpreter state.
     119             :  */
     120             : static void
     121           6 : auth_instance_run (void *cls,
     122             :                    const struct TALER_TESTING_Command *cmd,
     123             :                    struct TALER_TESTING_Interpreter *is)
     124             : {
     125           6 :   struct AuthInstanceState *ais = cls;
     126             : 
     127           6 :   ais->is = is;
     128           6 :   ais->iaph = TALER_MERCHANT_instance_auth_post (
     129             :     TALER_TESTING_interpreter_get_context (is),
     130             :     ais->merchant_url,
     131             :     ais->instance_id,
     132             :     ais->auth_token,
     133             :     &auth_instance_cb,
     134             :     ais);
     135           6 :   GNUNET_assert (NULL != ais->iaph);
     136           6 : }
     137             : 
     138             : 
     139             : /**
     140             :  * Free the state of a "POST instance auth" CMD, and possibly
     141             :  * cancel a pending operation thereof.
     142             :  *
     143             :  * @param cls closure.
     144             :  * @param cmd command being run.
     145             :  */
     146             : static void
     147           6 : auth_instance_cleanup (void *cls,
     148             :                        const struct TALER_TESTING_Command *cmd)
     149             : {
     150           6 :   struct AuthInstanceState *ais = cls;
     151             : 
     152           6 :   if (NULL != ais->iaph)
     153             :   {
     154           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     155             :                 "POST /instance/$ID/auth operation did not complete\n");
     156           0 :     TALER_MERCHANT_instance_auth_post_cancel (ais->iaph);
     157             :   }
     158           6 :   GNUNET_free (ais);
     159           6 : }
     160             : 
     161             : 
     162             : /**
     163             :  * Offer internal data to other commands.
     164             :  *
     165             :  * @param cls closure
     166             :  * @param[out] ret result (could be anything)
     167             :  * @param trait name of the trait
     168             :  * @param index index number of the object to extract.
     169             :  * @return #GNUNET_OK on success
     170             :  */
     171             : static enum GNUNET_GenericReturnValue
     172           0 : auth_instance_traits (void *cls,
     173             :                       const void **ret,
     174             :                       const char *trait,
     175             :                       unsigned int index)
     176             : {
     177           0 :   struct AuthInstanceState *ais = cls;
     178             :   struct TALER_TESTING_Trait traits[] = {
     179           0 :     TALER_TESTING_make_trait_auth_token (ais->auth_token),
     180           0 :     TALER_TESTING_trait_end ()
     181             :   };
     182             : 
     183           0 :   return TALER_TESTING_get_trait (traits,
     184             :                                   ret,
     185             :                                   trait,
     186             :                                   index);
     187             : }
     188             : 
     189             : 
     190             : struct TALER_TESTING_Command
     191           6 : TALER_TESTING_cmd_merchant_post_instance_auth (const char *label,
     192             :                                                const char *merchant_url,
     193             :                                                const char *instance_id,
     194             :                                                const char *auth_token,
     195             :                                                unsigned int http_status)
     196             : {
     197             :   struct AuthInstanceState *ais;
     198             : 
     199           6 :   ais = GNUNET_new (struct AuthInstanceState);
     200           6 :   ais->merchant_url = merchant_url;
     201           6 :   ais->instance_id = instance_id;
     202           6 :   ais->auth_token = auth_token;
     203           6 :   ais->http_status = http_status;
     204             : 
     205             :   {
     206           6 :     struct TALER_TESTING_Command cmd = {
     207             :       .cls = ais,
     208             :       .label = label,
     209             :       .run = &auth_instance_run,
     210             :       .cleanup = &auth_instance_cleanup,
     211             :       .traits = &auth_instance_traits
     212             :     };
     213             : 
     214           6 :     return cmd;
     215             :   }
     216             : }
     217             : 
     218             : 
     219             : /* end of testing_api_cmd_auth_instance.c */

Generated by: LCOV version 1.16