LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_keyshare_lookup.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 92 155 59.4 %
Date: 2021-06-16 06:33:01 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of Anastasis
       3             :   Copyright (C) 2020 Taler Systems SA
       4             : 
       5             :   Anastasis is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   Anastasis 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             :   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file lib/anastasis_api_keyshare_lookup.c
      18             :  * @brief Testing of Implementation of the /truth GET
      19             :  * @author Christian Grothoff
      20             :  * @author Dennis Neufeld
      21             :  * @author Dominik Meister
      22             :  */
      23             : 
      24             : #include "platform.h"
      25             : #include "anastasis_testing_lib.h"
      26             : #include <taler/taler_util.h>
      27             : #include <taler/taler_testing_lib.h>
      28             : #include <taler/taler_merchant_service.h>
      29             : 
      30             : 
      31             : /**
      32             :  * State for a "keyshare lookup" CMD.
      33             :  */
      34             : struct KeyShareLookupState
      35             : {
      36             :   /**
      37             :    * The interpreter state.
      38             :    */
      39             :   struct TALER_TESTING_Interpreter *is;
      40             : 
      41             :   /**
      42             :    * URL of the anastasis backend.
      43             :    */
      44             :   const char *anastasis_url;
      45             : 
      46             :   /**
      47             :    * Expected status code.
      48             :    */
      49             :   enum ANASTASIS_KeyShareDownloadStatus expected_ksdd;
      50             : 
      51             :   /**
      52             :    * The /truth GET operation handle.
      53             :    */
      54             :   struct ANASTASIS_KeyShareLookupOperation *kslo;
      55             : 
      56             :   /**
      57             :    * answer to a challenge
      58             :    */
      59             :   const char *answer;
      60             : 
      61             :   /**
      62             :    * Reference to upload command we expect to lookup.
      63             :    */
      64             :   const char *upload_reference;
      65             : 
      66             :   /**
      67             :    * Reference to upload command we expect to lookup.
      68             :    */
      69             :   const char *payment_reference;
      70             : 
      71             :   /**
      72             :    * Payment secret requested by the service, if any.
      73             :    */
      74             :   struct ANASTASIS_PaymentSecretP payment_secret_response;
      75             : 
      76             :   /**
      77             :    * Taler-URI with payment request, if any.
      78             :    */
      79             :   char *pay_uri;
      80             : 
      81             :   /**
      82             :    * Order ID for payment request, if any.
      83             :    */
      84             :   char *order_id;
      85             : 
      86             :   /**
      87             :    * Redirect-URI for challenge, if any.
      88             :    */
      89             :   char *redirect_uri;
      90             : 
      91             :   /**
      92             :    * "code" returned by service, if any.
      93             :    */
      94             :   char *code;
      95             : 
      96             :   /**
      97             :    * "instructions" for how to solve the challenge as returned by service, if any.
      98             :    */
      99             :   char *instructions;
     100             : 
     101             :   /**
     102             :    * Name of the file where the service will write the challenge, if method is "file".
     103             :    * Otherwise NULL.
     104             :    */
     105             :   char *filename;
     106             : 
     107             :   /**
     108             :    * Mode for the lookup(0 = question, 1 = code based)
     109             :    */
     110             :   int lookup_mode;
     111             : 
     112             : };
     113             : 
     114             : 
     115             : static void
     116           5 : keyshare_lookup_cb (void *cls,
     117             :                     const struct ANASTASIS_KeyShareDownloadDetails *dd)
     118             : {
     119           5 :   struct KeyShareLookupState *ksls = cls;
     120             : 
     121           5 :   ksls->kslo = NULL;
     122           5 :   if (dd->status != ksls->expected_ksdd)
     123             :   {
     124           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     125             :                 "Unexpected response code %u to command %s in %s:%u\n",
     126             :                 dd->status,
     127             :                 ksls->is->commands[ksls->is->ip].label,
     128             :                 __FILE__,
     129             :                 __LINE__);
     130           0 :     TALER_TESTING_interpreter_fail (ksls->is);
     131           0 :     return;
     132             :   }
     133           5 :   switch (dd->status)
     134             :   {
     135           2 :   case ANASTASIS_KSD_SUCCESS:
     136           2 :     break;
     137           1 :   case ANASTASIS_KSD_PAYMENT_REQUIRED:
     138           1 :     ksls->pay_uri = GNUNET_strdup (dd->details.payment_required.taler_pay_uri);
     139           1 :     ksls->payment_secret_response = dd->details.payment_required.payment_secret;
     140             :     {
     141             :       struct TALER_MERCHANT_PayUriData pd;
     142             : 
     143           1 :       if (GNUNET_OK !=
     144           1 :           TALER_MERCHANT_parse_pay_uri (ksls->pay_uri,
     145             :                                         &pd))
     146             :       {
     147           0 :         GNUNET_break (0);
     148           0 :         TALER_TESTING_interpreter_fail (ksls->is);
     149           0 :         return;
     150             :       }
     151           1 :       ksls->order_id = GNUNET_strdup (pd.order_id);
     152           1 :       TALER_MERCHANT_parse_pay_uri_free (&pd);
     153             :     }
     154             : 
     155           1 :     break;
     156           2 :   case ANASTASIS_KSD_INVALID_ANSWER:
     157           2 :     if (ksls->filename)
     158             :     {
     159             :       FILE *file;
     160             :       char code[22];
     161             : 
     162           1 :       file = fopen (ksls->filename,
     163             :                     "r");
     164           1 :       if (NULL == file)
     165             :       {
     166           0 :         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     167             :                                   "open",
     168             :                                   ksls->filename);
     169           0 :         TALER_TESTING_interpreter_fail (ksls->is);
     170           0 :         return;
     171             :       }
     172           1 :       if (0 == fscanf (file,
     173             :                        "%21s",
     174             :                        code))
     175             :       {
     176           0 :         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
     177             :                                   "fscanf",
     178             :                                   ksls->filename);
     179           0 :         GNUNET_break (0 == fclose (file));
     180           0 :         TALER_TESTING_interpreter_fail (ksls->is);
     181           0 :         return;
     182             :       }
     183           1 :       GNUNET_break (0 == fclose (file));
     184           1 :       ksls->code = GNUNET_strdup (code);
     185           1 :       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     186             :                   "Read code `%s'\n",
     187             :                   code);
     188             :     }
     189             :     else
     190             :     {
     191           1 :       ksls->instructions = GNUNET_strndup (
     192             :         dd->details.open_challenge.body,
     193             :         dd->details.open_challenge.body_size);
     194             :     }
     195           2 :     break;
     196           0 :   case ANASTASIS_KSD_REDIRECT_FOR_AUTHENTICATION:
     197           0 :     ksls->redirect_uri = GNUNET_strdup (dd->details.redirect_url);
     198           0 :     break;
     199           0 :   case ANASTASIS_KSD_SERVER_ERROR:
     200           0 :     break;
     201           0 :   case ANASTASIS_KSD_CLIENT_FAILURE:
     202           0 :     break;
     203           0 :   case ANASTASIS_KSD_TRUTH_UNKNOWN:
     204           0 :     break;
     205           0 :   case ANASTASIS_KSD_RATE_LIMIT_EXCEEDED:
     206           0 :     break;
     207             :   }
     208           5 :   TALER_TESTING_interpreter_next (ksls->is);
     209             : }
     210             : 
     211             : 
     212             : static void
     213           5 : keyshare_lookup_run (void *cls,
     214             :                      const struct TALER_TESTING_Command *cmd,
     215             :                      struct TALER_TESTING_Interpreter *is)
     216             : {
     217           5 :   struct KeyShareLookupState *ksls = cls;
     218             :   const struct ANASTASIS_CRYPTO_TruthKeyP *truth_key;
     219             :   const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid;
     220             :   const struct ANASTASIS_PaymentSecretP *payment_secret;
     221             :   const char *answer;
     222             : 
     223           5 :   ksls->is = is;
     224           5 :   if (NULL == ksls->upload_reference)
     225             :   {
     226           0 :     GNUNET_break (0);
     227           0 :     TALER_TESTING_interpreter_fail (ksls->is);
     228           0 :     return;
     229             :   }
     230             :   {
     231             :     const struct TALER_TESTING_Command *upload_cmd;
     232             : 
     233           5 :     upload_cmd = TALER_TESTING_interpreter_lookup_command (
     234             :       is,
     235             :       ksls->upload_reference);
     236           5 :     if (NULL == upload_cmd)
     237             :     {
     238           0 :       GNUNET_break (0);
     239           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     240           0 :       return;
     241             :     }
     242             :     {
     243             :       const char *fn;
     244             : 
     245           5 :       if (GNUNET_OK !=
     246           5 :           TALER_TESTING_get_trait_string (upload_cmd,
     247             :                                           0,
     248             :                                           &fn))
     249             :       {
     250           0 :         GNUNET_break (0);
     251           0 :         TALER_TESTING_interpreter_fail (ksls->is);
     252           0 :         return;
     253             :       }
     254           5 :       if (NULL != fn)
     255           3 :         ksls->filename = GNUNET_strdup (fn);
     256             :     }
     257           5 :     if (GNUNET_OK !=
     258           5 :         ANASTASIS_TESTING_get_trait_truth_uuid (upload_cmd,
     259             :                                                 0,
     260             :                                                 &truth_uuid))
     261             :     {
     262           0 :       GNUNET_break (0);
     263           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     264           0 :       return;
     265             :     }
     266           5 :     if (NULL == truth_uuid)
     267             :     {
     268           0 :       GNUNET_break (0);
     269           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     270           0 :       return;
     271             :     }
     272           5 :     if (GNUNET_OK !=
     273           5 :         ANASTASIS_TESTING_get_trait_truth_key (upload_cmd,
     274             :                                                0,
     275             :                                                &truth_key))
     276             :     {
     277           0 :       GNUNET_break (0);
     278           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     279           0 :       return;
     280             :     }
     281           5 :     if (NULL == truth_key)
     282             :     {
     283           0 :       GNUNET_break (0);
     284           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     285           0 :       return;
     286             :     }
     287             :   }
     288             : 
     289           5 :   if (ksls->lookup_mode == 1)
     290             :   {
     291             :     const struct TALER_TESTING_Command *download_cmd;
     292             : 
     293           1 :     download_cmd = TALER_TESTING_interpreter_lookup_command (is,
     294             :                                                              ksls->answer);
     295           1 :     if (NULL == download_cmd)
     296             :     {
     297           0 :       GNUNET_break (0);
     298           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     299           0 :       return;
     300             :     }
     301           1 :     if (GNUNET_OK !=
     302           1 :         ANASTASIS_TESTING_get_trait_code (download_cmd,
     303             :                                           0,
     304             :                                           &answer))
     305             :     {
     306           0 :       GNUNET_break (0);
     307           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     308           0 :       return;
     309             :     }
     310           1 :     if (NULL == answer)
     311             :     {
     312           0 :       GNUNET_break (0);
     313           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     314           0 :       return;
     315             :     }
     316             :   }
     317             :   else
     318             :   {
     319             :     /* answer is the answer */
     320           4 :     answer = ksls->answer;
     321             :   }
     322             : 
     323           5 :   if (NULL != ksls->payment_reference)
     324             :   {
     325             :     const struct TALER_TESTING_Command *payment_cmd;
     326             : 
     327           2 :     payment_cmd = TALER_TESTING_interpreter_lookup_command
     328             :                     (is,
     329             :                     ksls->payment_reference);
     330           2 :     if (GNUNET_OK !=
     331           2 :         ANASTASIS_TESTING_get_trait_payment_secret (payment_cmd,
     332             :                                                     0,
     333             :                                                     &payment_secret))
     334             :     {
     335           0 :       GNUNET_break (0);
     336           0 :       TALER_TESTING_interpreter_fail (ksls->is);
     337           0 :       return;
     338             :     }
     339             :   }
     340             :   else
     341             :   {
     342           3 :     payment_secret = NULL;
     343             :   }
     344             : 
     345             :   {
     346             :     struct GNUNET_HashCode h_answer;
     347             : 
     348           5 :     if (NULL != answer)
     349           3 :       GNUNET_CRYPTO_hash (answer,
     350             :                           strlen (answer),
     351             :                           &h_answer);
     352           5 :     ksls->kslo = ANASTASIS_keyshare_lookup (is->ctx,
     353             :                                             ksls->anastasis_url,
     354             :                                             truth_uuid,
     355             :                                             truth_key,
     356             :                                             payment_secret,
     357             :                                             GNUNET_TIME_UNIT_ZERO,
     358           5 :                                             (NULL != answer)
     359             :                                             ? &h_answer
     360             :                                             : NULL,
     361             :                                             &keyshare_lookup_cb,
     362             :                                             ksls);
     363             :   }
     364           5 :   if (NULL == ksls->kslo)
     365             :   {
     366           0 :     GNUNET_break (0);
     367           0 :     TALER_TESTING_interpreter_fail (ksls->is);
     368           0 :     return;
     369             :   }
     370             : }
     371             : 
     372             : 
     373             : static void
     374           5 : keyshare_lookup_cleanup (void *cls,
     375             :                          const struct TALER_TESTING_Command *cmd)
     376             : {
     377           5 :   struct KeyShareLookupState *ksls = cls;
     378             : 
     379           5 :   if (NULL != ksls->kslo)
     380             :   {
     381           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     382             :                 "Command '%s' did not complete (keyshare lookup)\n",
     383             :                 cmd->label);
     384           0 :     ANASTASIS_keyshare_lookup_cancel (ksls->kslo);
     385           0 :     ksls->kslo = NULL;
     386             :   }
     387           5 :   GNUNET_free (ksls->pay_uri);
     388           5 :   GNUNET_free (ksls->order_id);
     389           5 :   GNUNET_free (ksls->code);
     390           5 :   GNUNET_free (ksls->instructions);
     391           5 :   GNUNET_free (ksls->redirect_uri);
     392           5 :   GNUNET_free (ksls);
     393           5 : }
     394             : 
     395             : 
     396             : /**
     397             :  * Offer internal data to other commands.
     398             :  *
     399             :  * @param cls closure
     400             :  * @param[out] ret result (could be anything)
     401             :  * @param[out] trait name of the trait
     402             :  * @param index index number of the object to extract.
     403             :  * @return #GNUNET_OK on success
     404             :  */
     405             : static int
     406           6 : keyshare_lookup_traits (void *cls,
     407             :                         const void **ret,
     408             :                         const char *trait,
     409             :                         unsigned int index)
     410             : {
     411           6 :   struct KeyShareLookupState *ksls = cls;
     412             :   struct TALER_TESTING_Trait traits[] = {
     413           6 :     ANASTASIS_TESTING_make_trait_payment_secret (0,
     414           6 :                                                  &ksls->payment_secret_response),
     415           6 :     TALER_TESTING_make_trait_url (TALER_TESTING_UT_TALER_URL,
     416           6 :                                   ksls->pay_uri),
     417           6 :     TALER_TESTING_make_trait_order_id (0,
     418           6 :                                        ksls->order_id),
     419           6 :     ANASTASIS_TESTING_make_trait_code (0,
     420           6 :                                        ksls->code),
     421           6 :     TALER_TESTING_trait_end ()
     422             :   };
     423             : 
     424           6 :   return TALER_TESTING_get_trait (traits,
     425             :                                   ret,
     426             :                                   trait,
     427             :                                   index);
     428             : }
     429             : 
     430             : 
     431             : struct TALER_TESTING_Command
     432           5 : ANASTASIS_TESTING_cmd_keyshare_lookup (
     433             :   const char *label,
     434             :   const char *anastasis_url,
     435             :   const char *answer,
     436             :   const char *payment_ref,
     437             :   const char *upload_ref,
     438             :   int lookup_mode,
     439             :   enum ANASTASIS_KeyShareDownloadStatus ksdd)
     440             : {
     441             :   struct KeyShareLookupState *ksls;
     442             : 
     443           5 :   GNUNET_assert (NULL != upload_ref);
     444           5 :   ksls = GNUNET_new (struct KeyShareLookupState);
     445           5 :   ksls->expected_ksdd = ksdd;
     446           5 :   ksls->anastasis_url = anastasis_url;
     447           5 :   ksls->upload_reference = upload_ref;
     448           5 :   ksls->payment_reference = payment_ref;
     449           5 :   ksls->answer = answer;
     450           5 :   ksls->lookup_mode = lookup_mode;
     451             :   {
     452           5 :     struct TALER_TESTING_Command cmd = {
     453             :       .cls = ksls,
     454             :       .label = label,
     455             :       .run = &keyshare_lookup_run,
     456             :       .cleanup = &keyshare_lookup_cleanup,
     457             :       .traits = &keyshare_lookup_traits
     458             :     };
     459             : 
     460           5 :     return cmd;
     461             :   }
     462             : }
     463             : 
     464             : 
     465             : /* end of testing_api_cmd_keyshare_lookup.c */

Generated by: LCOV version 1.14