LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_instance_auth.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 43 0.0 %
Date: 2022-08-25 06:17:04 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) 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           0 : auth_instance_cb (void *cls,
      78             :                   const struct TALER_MERCHANT_HttpResponse *hr)
      79             : {
      80           0 :   struct AuthInstanceState *ais = cls;
      81             : 
      82           0 :   ais->iaph = NULL;
      83           0 :   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           0 :   switch (hr->http_status)
      94             :   {
      95           0 :   case MHD_HTTP_NO_CONTENT:
      96           0 :     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           0 :   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           0 : auth_instance_run (void *cls,
     122             :                    const struct TALER_TESTING_Command *cmd,
     123             :                    struct TALER_TESTING_Interpreter *is)
     124             : {
     125           0 :   struct AuthInstanceState *ais = cls;
     126             : 
     127           0 :   ais->is = is;
     128           0 :   ais->iaph = TALER_MERCHANT_instance_auth_post (is->ctx,
     129             :                                                  ais->merchant_url,
     130             :                                                  ais->instance_id,
     131             :                                                  ais->auth_token,
     132             :                                                  &auth_instance_cb,
     133             :                                                  ais);
     134           0 :   GNUNET_assert (NULL != ais->iaph);
     135           0 : }
     136             : 
     137             : 
     138             : /**
     139             :  * Free the state of a "POST instance auth" CMD, and possibly
     140             :  * cancel a pending operation thereof.
     141             :  *
     142             :  * @param cls closure.
     143             :  * @param cmd command being run.
     144             :  */
     145             : static void
     146           0 : auth_instance_cleanup (void *cls,
     147             :                        const struct TALER_TESTING_Command *cmd)
     148             : {
     149           0 :   struct AuthInstanceState *ais = cls;
     150             : 
     151           0 :   if (NULL != ais->iaph)
     152             :   {
     153           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     154             :                 "POST /instance/$ID/auth operation did not complete\n");
     155           0 :     TALER_MERCHANT_instance_auth_post_cancel (ais->iaph);
     156             :   }
     157           0 :   GNUNET_free (ais);
     158           0 : }
     159             : 
     160             : 
     161             : /**
     162             :  * Offer internal data to other commands.
     163             :  *
     164             :  * @param cls closure
     165             :  * @param[out] ret result (could be anything)
     166             :  * @param trait name of the trait
     167             :  * @param index index number of the object to extract.
     168             :  * @return #GNUNET_OK on success
     169             :  */
     170             : static enum GNUNET_GenericReturnValue
     171           0 : auth_instance_traits (void *cls,
     172             :                       const void **ret,
     173             :                       const char *trait,
     174             :                       unsigned int index)
     175             : {
     176           0 :   struct AuthInstanceState *ais = cls;
     177             :   struct TALER_TESTING_Trait traits[] = {
     178           0 :     TALER_TESTING_make_trait_auth_token (&ais->auth_token),
     179           0 :     TALER_TESTING_trait_end ()
     180             :   };
     181             : 
     182           0 :   return TALER_TESTING_get_trait (traits,
     183             :                                   ret,
     184             :                                   trait,
     185             :                                   index);
     186             : }
     187             : 
     188             : 
     189             : struct TALER_TESTING_Command
     190           0 : TALER_TESTING_cmd_merchant_post_instance_auth (const char *label,
     191             :                                                const char *merchant_url,
     192             :                                                const char *instance_id,
     193             :                                                const char *auth_token,
     194             :                                                unsigned int http_status)
     195             : {
     196             :   struct AuthInstanceState *ais;
     197             : 
     198           0 :   ais = GNUNET_new (struct AuthInstanceState);
     199           0 :   ais->merchant_url = merchant_url;
     200           0 :   ais->instance_id = instance_id;
     201           0 :   ais->auth_token = auth_token;
     202           0 :   ais->http_status = http_status;
     203             : 
     204             :   {
     205           0 :     struct TALER_TESTING_Command cmd = {
     206             :       .cls = ais,
     207             :       .label = label,
     208             :       .run = &auth_instance_run,
     209             :       .cleanup = &auth_instance_cleanup,
     210             :       .traits = &auth_instance_traits
     211             :     };
     212             : 
     213           0 :     return cmd;
     214             :   }
     215             : }
     216             : 
     217             : 
     218             : /* end of testing_api_cmd_auth_instance.c */

Generated by: LCOV version 1.14