LCOV - code coverage report
Current view: top level - lib - exchange_api_management_auditor_disable.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 50 72 69.4 %
Date: 2021-08-30 06:43:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2015-2021 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/exchange_api_management_auditor_disable.c
      19             :  * @brief functions to disable an auditor
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "taler_json_lib.h"
      24             : #include <gnunet/gnunet_curl_lib.h>
      25             : #include "taler_exchange_service.h"
      26             : #include "taler_signatures.h"
      27             : #include "taler_curl_lib.h"
      28             : #include "taler_json_lib.h"
      29             : 
      30             : /**
      31             :  * @brief Handle for a POST /management/auditors/$AUDITOR_PUB/disable request.
      32             :  */
      33             : struct TALER_EXCHANGE_ManagementAuditorDisableHandle
      34             : {
      35             : 
      36             :   /**
      37             :    * The url for this request.
      38             :    */
      39             :   char *url;
      40             : 
      41             :   /**
      42             :    * Minor context that holds body and headers.
      43             :    */
      44             :   struct TALER_CURL_PostContext post_ctx;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_EXCHANGE_ManagementAuditorDisableCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Function called when we're done processing the
      70             :  * HTTP /management/auditors/%s/disable request.
      71             :  *
      72             :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorDisableHandle *`
      73             :  * @param response_code HTTP response code, 0 on error
      74             :  * @param response response body, NULL if not in JSON
      75             :  */
      76             : static void
      77           4 : handle_auditor_disable_finished (void *cls,
      78             :                                  long response_code,
      79             :                                  const void *response)
      80             : {
      81           4 :   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah = cls;
      82           4 :   const json_t *json = response;
      83           4 :   struct TALER_EXCHANGE_HttpResponse hr = {
      84           4 :     .http_status = (unsigned int) response_code,
      85             :     .reply = json
      86             :   };
      87             : 
      88           4 :   ah->job = NULL;
      89           4 :   switch (response_code)
      90             :   {
      91           2 :   case MHD_HTTP_NO_CONTENT:
      92           2 :     break;
      93           1 :   case MHD_HTTP_FORBIDDEN:
      94           1 :     hr.ec = TALER_JSON_get_error_code (json);
      95           1 :     hr.hint = TALER_JSON_get_error_hint (json);
      96           1 :     break;
      97           1 :   case MHD_HTTP_NOT_FOUND:
      98           1 :     hr.ec = TALER_JSON_get_error_code (json);
      99           1 :     hr.hint = TALER_JSON_get_error_hint (json);
     100           1 :     break;
     101           0 :   case MHD_HTTP_CONFLICT:
     102           0 :     hr.ec = TALER_JSON_get_error_code (json);
     103           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     104           0 :     break;
     105           0 :   default:
     106             :     /* unexpected response code */
     107           0 :     GNUNET_break_op (0);
     108           0 :     hr.ec = TALER_JSON_get_error_code (json);
     109           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     111             :                 "Unexpected response code %u/%d for exchange management auditor disable\n",
     112             :                 (unsigned int) response_code,
     113             :                 (int) hr.ec);
     114           0 :     break;
     115             :   }
     116           4 :   if (NULL != ah->cb)
     117             :   {
     118           4 :     ah->cb (ah->cb_cls,
     119             :             &hr);
     120           4 :     ah->cb = NULL;
     121             :   }
     122           4 :   TALER_EXCHANGE_management_disable_auditor_cancel (ah);
     123           4 : }
     124             : 
     125             : 
     126             : struct TALER_EXCHANGE_ManagementAuditorDisableHandle *
     127           4 : TALER_EXCHANGE_management_disable_auditor (
     128             :   struct GNUNET_CURL_Context *ctx,
     129             :   const char *url,
     130             :   const struct TALER_AuditorPublicKeyP *auditor_pub,
     131             :   struct GNUNET_TIME_Absolute validity_end,
     132             :   const struct TALER_MasterSignatureP *master_sig,
     133             :   TALER_EXCHANGE_ManagementAuditorDisableCallback cb,
     134             :   void *cb_cls)
     135             : {
     136             :   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah;
     137             :   CURL *eh;
     138             :   json_t *body;
     139             : 
     140           4 :   ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorDisableHandle);
     141           4 :   ah->cb = cb;
     142           4 :   ah->cb_cls = cb_cls;
     143           4 :   ah->ctx = ctx;
     144             :   {
     145             :     char epub_str[sizeof (*auditor_pub) * 2];
     146             :     char arg_str[sizeof (epub_str) + 64];
     147             :     char *end;
     148             : 
     149           4 :     end = GNUNET_STRINGS_data_to_string (auditor_pub,
     150             :                                          sizeof (*auditor_pub),
     151             :                                          epub_str,
     152             :                                          sizeof (epub_str));
     153           4 :     *end = '\0';
     154           4 :     GNUNET_snprintf (arg_str,
     155             :                      sizeof (arg_str),
     156             :                      "management/auditors/%s/disable",
     157             :                      epub_str);
     158           4 :     ah->url = TALER_url_join (url,
     159             :                               arg_str,
     160             :                               NULL);
     161             :   }
     162           4 :   if (NULL == ah->url)
     163             :   {
     164           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     165             :                 "Could not construct request URL.\n");
     166           0 :     GNUNET_free (ah);
     167           0 :     return NULL;
     168             :   }
     169           4 :   body = GNUNET_JSON_PACK (
     170             :     GNUNET_JSON_pack_data_auto ("master_sig",
     171             :                                 master_sig),
     172             :     GNUNET_JSON_pack_time_abs ("validity_end",
     173             :                                validity_end));
     174           4 :   eh = curl_easy_init ();
     175           4 :   GNUNET_assert (NULL != eh);
     176           4 :   if (GNUNET_OK !=
     177           4 :       TALER_curl_easy_post (&ah->post_ctx,
     178             :                             eh,
     179             :                             body))
     180             :   {
     181           0 :     GNUNET_break (0);
     182           0 :     json_decref (body);
     183           0 :     GNUNET_free (ah->url);
     184           0 :     GNUNET_free (eh);
     185           0 :     return NULL;
     186             :   }
     187           4 :   json_decref (body);
     188           4 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     189             :               "Requesting URL '%s'\n",
     190             :               ah->url);
     191           4 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     192             :                                                CURLOPT_URL,
     193             :                                                ah->url));
     194           8 :   ah->job = GNUNET_CURL_job_add2 (ctx,
     195             :                                   eh,
     196           4 :                                   ah->post_ctx.headers,
     197             :                                   &handle_auditor_disable_finished,
     198             :                                   ah);
     199           4 :   if (NULL == ah->job)
     200             :   {
     201           0 :     TALER_EXCHANGE_management_disable_auditor_cancel (ah);
     202           0 :     return NULL;
     203             :   }
     204           4 :   return ah;
     205             : }
     206             : 
     207             : 
     208             : void
     209           4 : TALER_EXCHANGE_management_disable_auditor_cancel (
     210             :   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah)
     211             : {
     212           4 :   if (NULL != ah->job)
     213             :   {
     214           0 :     GNUNET_CURL_job_cancel (ah->job);
     215           0 :     ah->job = NULL;
     216             :   }
     217           4 :   TALER_curl_easy_post_finished (&ah->post_ctx);
     218           4 :   GNUNET_free (ah->url);
     219           4 :   GNUNET_free (ah);
     220           4 : }

Generated by: LCOV version 1.14