donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_donation_statement_get.c (6532B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 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/testing_api_cmd_donation_statement_get.c
     21  * @brief Implement the GET /donation-statement/$YEAR/$HASH_DONOR_ID test command.
     22  * @author Marcello Stanisci
     23  * @author Lukas Matyja
     24  */
     25 #include <donau_config.h>
     26 #include <taler/taler_json_lib.h>
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include <taler/taler_testing_lib.h>
     29 #include "donau_testing_lib.h"
     30 
     31 
     32 /**
     33  * State for a "status" CMD.
     34  */
     35 struct StatusState
     36 {
     37   /**
     38    * Handle to the "charity status" operation.
     39    */
     40   struct DONAU_DonationStatementGetHandle *dsgh;
     41 
     42   /**
     43    * Expected HTTP response code.
     44    */
     45   unsigned int expected_response_code;
     46 
     47   /**
     48    * Interpreter state.
     49    */
     50   struct TALER_TESTING_Interpreter *is;
     51 
     52   /**
     53    * The Donation Statement
     54    */
     55   struct DONAU_DonationStatement donation_statement;
     56 
     57   /**
     58    * The Donau keys
     59    */
     60   struct DONAU_Keys *keys;
     61 
     62 };
     63 
     64 /**
     65  * Check that the reserve balance and HTTP response code are
     66  * both acceptable.
     67  *
     68  * @param cls closure.
     69  * @param dsr HTTP response details
     70  */
     71 static void
     72 donation_statement_status_cb (void *cls,
     73                               const struct DONAU_DonationStatementResponse *dsr)
     74 {
     75   struct StatusState *ss = cls;
     76 
     77   ss->dsgh = NULL;
     78   if (ss->expected_response_code != dsr->hr.http_status)
     79   {
     80     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     81                 "Unexpected HTTP response code: %d in %s:%u\n",
     82                 dsr->hr.http_status,
     83                 __FILE__,
     84                 __LINE__);
     85     json_dumpf (dsr->hr.reply,
     86                 stderr,
     87                 0);
     88     TALER_TESTING_interpreter_fail (ss->is);
     89     return;
     90   }
     91 
     92   /* Get donau keys from trait */
     93   {
     94     const struct TALER_TESTING_Command *keys_cmd;
     95     struct DONAU_Keys *keys;
     96 
     97     keys_cmd = TALER_TESTING_interpreter_lookup_command (ss->is,
     98                                                          "get-donau");
     99 
    100     if (GNUNET_OK !=
    101         TALER_TESTING_get_trait_donau_keys (keys_cmd,
    102                                             &keys))
    103     {
    104       GNUNET_break (0);
    105       TALER_TESTING_interpreter_fail (ss->is);
    106       return;
    107     }
    108     ss->keys = keys;
    109   }
    110 
    111   ss->donation_statement.total_amount = dsr->details.ok.total_amount;
    112   ss->donation_statement.donation_statement_sig =
    113     dsr->details.ok.donation_statement_sig;
    114 
    115   for (unsigned int i = 0; i < ss->keys->num_sign_keys; i++)
    116   {
    117     if (GNUNET_OK == DONAU_donation_statement_verify (
    118           &ss->donation_statement.total_amount,
    119           ss->donation_statement.year,
    120           ss->donation_statement.
    121           h_donor_tax_id,
    122           &dsr->details.ok.donau_pub,
    123           &ss->donation_statement.
    124           donation_statement_sig))
    125     {
    126       char *qr_string = DONAU_generate_qr_string (&dsr->details.ok.donau_pub,
    127                                                   &ss->donation_statement);
    128       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    129                   "qr-string: %s\n", qr_string);
    130       GNUNET_free (qr_string);
    131 
    132       TALER_TESTING_interpreter_next (ss->is);
    133       return;
    134     }
    135   }
    136   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    137               "Verify donation statement signature failed!");
    138   TALER_TESTING_interpreter_fail (ss->is);
    139   return;
    140 }
    141 
    142 
    143 /**
    144  * Run the command.
    145  *
    146  * @param cls closure.
    147  * @param cmd the command being executed.
    148  * @param is the interpreter state.
    149  */
    150 static void
    151 status_run (void *cls,
    152             const struct TALER_TESTING_Command *cmd,
    153             struct TALER_TESTING_Interpreter *is)
    154 {
    155   struct StatusState *ss = cls;
    156 
    157   (void) cmd;
    158   ss->is = is;
    159   /* Get charity salted tax id hash from trait */
    160   {
    161     const struct TALER_TESTING_Command *issue_receipts_cmd;
    162     const struct DONAU_HashDonorTaxId *h_donor_tax_id;
    163     const char *donor_salt;
    164     const char *donor_tax_id;
    165 
    166     issue_receipts_cmd = TALER_TESTING_interpreter_lookup_command (is,
    167                                                                    "issue-receipts");
    168 
    169     if (GNUNET_OK != TALER_TESTING_get_trait_salted_tax_id_hash
    170           (issue_receipts_cmd, &h_donor_tax_id) ||
    171         GNUNET_OK != TALER_TESTING_get_trait_donor_salt
    172           (issue_receipts_cmd, &donor_salt) ||
    173         GNUNET_OK != TALER_TESTING_get_trait_donor_tax_id
    174           (issue_receipts_cmd, &donor_tax_id))
    175     {
    176       GNUNET_break (0);
    177       TALER_TESTING_interpreter_fail (is);
    178       return;
    179     }
    180     ss->donation_statement.donor_tax_id = donor_tax_id;
    181     ss->donation_statement.salt = donor_salt;
    182     ss->donation_statement.h_donor_tax_id = h_donor_tax_id;
    183   }
    184 
    185   ss->dsgh = DONAU_donation_statement_get (
    186     TALER_TESTING_interpreter_get_context (is),
    187     TALER_TESTING_get_donau_url (is),
    188     ss->donation_statement.year,
    189     ss->donation_statement.h_donor_tax_id,
    190     &donation_statement_status_cb,
    191     ss);
    192 }
    193 
    194 
    195 /**
    196  * Cleanup the state from a "reserve status" CMD, and possibly
    197  * cancel a pending operation thereof.
    198  *
    199  * @param cls closure.
    200  * @param cmd the command which is being cleaned up.
    201  */
    202 static void
    203 status_cleanup (void *cls,
    204                 const struct TALER_TESTING_Command *cmd)
    205 {
    206   struct StatusState *ss = cls;
    207 
    208   if (NULL != ss->dsgh)
    209   {
    210     // log incomplete command
    211     TALER_TESTING_command_incomplete (ss->is,
    212                                       cmd->label);
    213     DONAU_donation_statement_get_cancel (ss->dsgh);
    214     ss->dsgh = NULL;
    215   }
    216   GNUNET_free (ss);
    217 }
    218 
    219 
    220 struct TALER_TESTING_Command
    221 TALER_TESTING_cmd_donation_statement_get (const char *label,
    222                                           uint64_t year,
    223                                           unsigned int expected_response_code)
    224 {
    225   struct StatusState *ss;
    226   ss = GNUNET_new (struct StatusState);
    227   ss->donation_statement.year = year;
    228   ss->expected_response_code = expected_response_code;
    229   {
    230     struct TALER_TESTING_Command cmd = {
    231       .cls = ss,
    232       .label = label,
    233       .run = &status_run,
    234       .cleanup = &status_cleanup
    235     };
    236 
    237     return cmd;
    238   }
    239 }