LCOV - code coverage report
Current view: top level - testing - testing_cmd_secret_share.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 90 127 70.9 %
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/testing_cmd_secret_share.c
      18             :  * @brief command to execute the anastasis secret share service
      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 "secret share" CMD.
      33             :  */
      34             : struct SecretShareState
      35             : {
      36             :   /**
      37             :    * Claim token we got back, if any. Otherwise all zeros.
      38             :    */
      39             :   struct TALER_ClaimTokenP token;
      40             : 
      41             :   /**
      42             :    * The interpreter state.
      43             :    */
      44             :   struct TALER_TESTING_Interpreter *is;
      45             : 
      46             :   /**
      47             :    * Label of this command.
      48             :    */
      49             :   const char *label;
      50             : 
      51             :   /**
      52             :    * References to commands of previous policy creations.
      53             :    */
      54             :   const char **cmd_label_array;
      55             : 
      56             :   /**
      57             :    * Data to derive user identifier from.
      58             :    */
      59             :   json_t *id_data;
      60             : 
      61             :   /**
      62             :    * The core secret to backup/recover.
      63             :    */
      64             :   const void *core_secret;
      65             : 
      66             :   /**
      67             :    * URL of the anastasis backend.
      68             :    */
      69             :   const char *anastasis_url;
      70             : 
      71             :   /**
      72             :    * URL of a /config command for the @e anastasis_url.
      73             :    */
      74             :   const char *config_ref;
      75             : 
      76             :   /**
      77             :    * The /truth GET operation handle.
      78             :    */
      79             :   struct ANASTASIS_SecretShare *sso;
      80             : 
      81             :   /**
      82             :    * Reference to previous secret share command we expect to lookup.
      83             :    */
      84             :   const char *prev_secret_share;
      85             : 
      86             :   /**
      87             :    * closure for the payment callback
      88             :    */
      89             :   void *spc_cls;
      90             : 
      91             :   /**
      92             :    * closure for the result callback
      93             :    */
      94             :   void *src_cls;
      95             : 
      96             :   /**
      97             :    * Payment order ID we got back, if any. Otherwise NULL.
      98             :    */
      99             :   char *payment_order_id;
     100             : 
     101             :   /**
     102             :    * Size of core_secret.
     103             :    */
     104             :   size_t core_secret_size;
     105             : 
     106             :   /**
     107             :    * Length of array of command labels (cmd_label_array).
     108             :    */
     109             :   unsigned int cmd_label_array_length;
     110             : 
     111             :   /**
     112             :    * Expected status code.
     113             :    */
     114             :   enum ANASTASIS_ShareStatus want_status;
     115             : 
     116             :   /**
     117             :    * Options for how we are supposed to do the upload.
     118             :    */
     119             :   enum ANASTASIS_TESTING_SecretShareOption ssopt;
     120             : };
     121             : 
     122             : 
     123             : /**
     124             :  * Function called with the results of a #ANASTASIS_secret_share().
     125             :  *
     126             :  * @param cls closure
     127             :  * @param sr result from the operation
     128             :  */
     129             : static void
     130           2 : secret_share_result_cb (void *cls,
     131             :                         const struct ANASTASIS_ShareResult *sr)
     132             : {
     133           2 :   struct SecretShareState *sss = cls;
     134             : 
     135           2 :   sss->sso = NULL;
     136           2 :   if (sr->ss != sss->want_status)
     137             :   {
     138           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     139             :                 "Unexpected response code %u to command %s in %s:%u\n",
     140             :                 sr->ss,
     141             :                 sss->is->commands[sss->is->ip].label,
     142             :                 __FILE__,
     143             :                 __LINE__);
     144           0 :     TALER_TESTING_interpreter_fail (sss->is);
     145           0 :     return;
     146             :   }
     147           2 :   switch (sr->ss)
     148             :   {
     149           1 :   case ANASTASIS_SHARE_STATUS_SUCCESS:
     150           1 :     break;
     151           1 :   case ANASTASIS_SHARE_STATUS_PAYMENT_REQUIRED:
     152             :     {
     153             :       struct TALER_MERCHANT_PayUriData pd;
     154             : 
     155           1 :       GNUNET_assert (0 < sr->details.payment_required.payment_requests_length);
     156           1 :       if (GNUNET_OK !=
     157           1 :           TALER_MERCHANT_parse_pay_uri (
     158           1 :             sr->details.payment_required.payment_requests[0].payment_request_url,
     159             :             &pd))
     160             :       {
     161           0 :         GNUNET_break (0);
     162           0 :         TALER_TESTING_interpreter_fail (sss->is);
     163           0 :         return;
     164             :       }
     165           1 :       sss->payment_order_id = GNUNET_strdup (pd.order_id);
     166           1 :       TALER_MERCHANT_parse_pay_uri_free (&pd);
     167           1 :       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     168             :                   "Order ID from Anastasis service is `%s'\n",
     169             :                   sss->payment_order_id);
     170             :     }
     171             :   case ANASTASIS_SHARE_STATUS_PROVIDER_FAILED:
     172           1 :     break;
     173             :   }
     174           2 :   TALER_TESTING_interpreter_next (sss->is);
     175             : }
     176             : 
     177             : 
     178             : /**
     179             :  * Run a "secret share" CMD.
     180             :  *
     181             :  * @param cls closure.
     182             :  * @param cmd command currently being run.
     183             :  * @param is interpreter state.
     184             :  */
     185             : static void
     186           2 : secret_share_run (void *cls,
     187             :                   const struct TALER_TESTING_Command *cmd,
     188             :                   struct TALER_TESTING_Interpreter *is)
     189           2 : {
     190           2 :   struct SecretShareState *sss = cls;
     191           2 :   const struct ANASTASIS_Policy *policies[sss->cmd_label_array_length];
     192             :   struct ANASTASIS_ProviderDetails pds;
     193             : 
     194           2 :   GNUNET_assert (sss->cmd_label_array_length > 0);
     195           2 :   GNUNET_assert (NULL != sss->cmd_label_array);
     196           2 :   sss->is = is;
     197           2 :   if (NULL != sss->cmd_label_array)
     198             :   {
     199           8 :     for (unsigned int i = 0; i < sss->cmd_label_array_length; i++)
     200             :     {
     201             :       const struct TALER_TESTING_Command *ref;
     202             :       const struct ANASTASIS_Policy *policy;
     203             : 
     204           6 :       ref = TALER_TESTING_interpreter_lookup_command (is,
     205           6 :                                                       sss->cmd_label_array[i]);
     206           6 :       if (NULL == ref)
     207             :       {
     208           0 :         GNUNET_break (0);
     209           0 :         TALER_TESTING_interpreter_fail (sss->is);
     210           0 :         return;
     211             :       }
     212           6 :       if (GNUNET_OK !=
     213           6 :           ANASTASIS_TESTING_get_trait_policy (ref,
     214             :                                               0,
     215             :                                               &policy))
     216             :       {
     217           0 :         GNUNET_break (0);
     218           0 :         TALER_TESTING_interpreter_fail (sss->is);
     219           0 :         return;
     220             :       }
     221           6 :       GNUNET_assert (NULL != policy);
     222           6 :       policies[i] = policy;
     223             :     }
     224             :   }
     225             : 
     226           2 :   if (NULL != sss->prev_secret_share)
     227             :   {
     228             :     const struct TALER_TESTING_Command *ref;
     229             :     const char *order_id;
     230             : 
     231           1 :     ref = TALER_TESTING_interpreter_lookup_command (is,
     232             :                                                     sss->prev_secret_share);
     233           1 :     if (NULL == ref)
     234             :     {
     235           0 :       GNUNET_break (0);
     236           0 :       TALER_TESTING_interpreter_fail (sss->is);
     237           0 :       return;
     238             :     }
     239           1 :     if (GNUNET_OK !=
     240           1 :         TALER_TESTING_get_trait_order_id (ref,
     241             :                                           0,
     242             :                                           &order_id))
     243             :     {
     244           0 :       GNUNET_break (0);
     245           0 :       TALER_TESTING_interpreter_fail (sss->is);
     246           0 :       return;
     247             :     }
     248           1 :     sss->payment_order_id = (char *) order_id;
     249             : 
     250           1 :     if (NULL == sss->payment_order_id)
     251             :     {
     252           0 :       GNUNET_break (0);
     253           0 :       TALER_TESTING_interpreter_fail (sss->is);
     254           0 :       return;
     255             :     }
     256             :   }
     257             : 
     258           2 :   memset (&pds,
     259             :           0,
     260             :           sizeof (pds));
     261           2 :   if (NULL != sss->payment_order_id)
     262             :   {
     263           1 :     if (GNUNET_OK !=
     264           1 :         GNUNET_STRINGS_string_to_data (
     265           1 :           sss->payment_order_id,
     266           1 :           strlen (sss->payment_order_id),
     267             :           &pds.payment_secret,
     268             :           sizeof (struct ANASTASIS_PaymentSecretP)))
     269             :     {
     270           0 :       GNUNET_break (0);
     271           0 :       TALER_TESTING_interpreter_fail (sss->is);
     272           0 :       GNUNET_free (sss->payment_order_id);
     273           0 :       return;
     274             :     }
     275           1 :     GNUNET_free (sss->payment_order_id);
     276             :   }
     277           2 :   pds.provider_url = sss->anastasis_url;
     278             :   {
     279             :     const struct TALER_TESTING_Command *ref;
     280             :     const struct ANASTASIS_CRYPTO_ProviderSaltP *salt;
     281             : 
     282           2 :     ref = TALER_TESTING_interpreter_lookup_command (is,
     283             :                                                     sss->config_ref);
     284           2 :     if (NULL == ref)
     285             :     {
     286           0 :       GNUNET_break (0);
     287           0 :       TALER_TESTING_interpreter_fail (sss->is);
     288           0 :       return;
     289             :     }
     290           2 :     if (GNUNET_OK !=
     291           2 :         ANASTASIS_TESTING_get_trait_salt (ref,
     292             :                                           0,
     293             :                                           &salt))
     294             :     {
     295           0 :       GNUNET_break (0);
     296           0 :       TALER_TESTING_interpreter_fail (sss->is);
     297           0 :       return;
     298             :     }
     299           2 :     pds.provider_salt = *salt;
     300             :   }
     301             : 
     302           2 :   sss->sso = ANASTASIS_secret_share (is->ctx,
     303           2 :                                      sss->id_data,
     304             :                                      &pds,
     305             :                                      1,
     306             :                                      policies,
     307             :                                      sss->cmd_label_array_length,
     308             :                                      false,
     309             :                                      GNUNET_TIME_UNIT_ZERO,
     310             :                                      &secret_share_result_cb,
     311             :                                      sss,
     312             :                                      sss->core_secret,
     313             :                                      sss->core_secret_size);
     314           2 :   if (NULL == sss->sso)
     315             :   {
     316           0 :     GNUNET_break (0);
     317           0 :     TALER_TESTING_interpreter_fail (sss->is);
     318           0 :     return;
     319             :   }
     320             : }
     321             : 
     322             : 
     323             : /**
     324             :  * Free the state of a "secret share" CMD, and possibly
     325             :  * cancel it if it did not complete.
     326             :  *
     327             :  * @param cls closure.
     328             :  * @param cmd command being freed.
     329             :  */
     330             : static void
     331           2 : secret_share_cleanup (void *cls,
     332             :                       const struct TALER_TESTING_Command *cmd)
     333             : {
     334           2 :   struct SecretShareState *sss = cls;
     335             : 
     336           2 :   if (NULL != sss->cmd_label_array)
     337             :   {
     338           2 :     GNUNET_free (sss->cmd_label_array);
     339             :   }
     340           2 :   if (NULL != sss->sso)
     341             :   {
     342           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     343             :                 "Command '%s' did not complete\n",
     344             :                 cmd->label);
     345           0 :     ANASTASIS_secret_share_cancel (sss->sso);
     346           0 :     sss->sso = NULL;
     347             :   }
     348           2 :   json_decref (sss->id_data);
     349           2 :   GNUNET_free (sss);
     350           2 : }
     351             : 
     352             : 
     353             : /**
     354             :  * Offer internal data to other commands.
     355             :  *
     356             :  * @param cls closure
     357             :  * @param ret[out] result (could be anything)
     358             :  * @param trait name of the trait
     359             :  * @param index index number of the object to extract.
     360             :  * @return #GNUNET_OK on success
     361             :  */
     362             : static int
     363           5 : secret_share_traits (void *cls,
     364             :                      const void **ret,
     365             :                      const char *trait,
     366             :                      unsigned int index)
     367             : {
     368           5 :   struct SecretShareState *sss = cls;
     369             :   struct TALER_TESTING_Trait traits[] = {
     370           5 :     TALER_TESTING_make_trait_claim_token (0,
     371           5 :                                           &sss->token),
     372           5 :     ANASTASIS_TESTING_make_trait_core_secret (0,
     373             :                                               sss->core_secret),
     374           5 :     TALER_TESTING_make_trait_order_id (0,
     375           5 :                                        sss->payment_order_id),
     376           5 :     TALER_TESTING_trait_end ()
     377             :   };
     378             : 
     379           5 :   return TALER_TESTING_get_trait (traits,
     380             :                                   ret,
     381             :                                   trait,
     382             :                                   index);
     383             : }
     384             : 
     385             : 
     386             : struct TALER_TESTING_Command
     387           2 : ANASTASIS_TESTING_cmd_secret_share (
     388             :   const char *label,
     389             :   const char *anastasis_url,
     390             :   const char *config_ref,
     391             :   const char *prev_secret_share,
     392             :   const json_t *id_data,
     393             :   const void *core_secret,
     394             :   size_t core_secret_size,
     395             :   enum ANASTASIS_ShareStatus want_status,
     396             :   enum ANASTASIS_TESTING_SecretShareOption sso,
     397             :   ...)
     398             : {
     399             :   struct SecretShareState *sss;
     400             : 
     401           2 :   sss = GNUNET_new (struct SecretShareState);
     402           2 :   sss->want_status = want_status;
     403           2 :   sss->ssopt = sso;
     404           2 :   sss->anastasis_url = anastasis_url;
     405           2 :   sss->config_ref = config_ref;
     406           2 :   sss->label = label;
     407           2 :   sss->id_data = json_incref ((json_t *) id_data);
     408           2 :   sss->core_secret = core_secret;
     409           2 :   sss->core_secret_size = core_secret_size;
     410           2 :   sss->prev_secret_share = prev_secret_share;
     411             : 
     412             :   {
     413             :     const char *policy_create_cmd;
     414             :     va_list ap;
     415             : 
     416           2 :     va_start (ap,
     417             :               sso);
     418           8 :     while (NULL != (policy_create_cmd = va_arg (ap, const char *)))
     419             :     {
     420           6 :       GNUNET_array_append (sss->cmd_label_array,
     421             :                            sss->cmd_label_array_length,
     422             :                            policy_create_cmd);
     423             :     }
     424           2 :     va_end (ap);
     425             :   }
     426             :   {
     427           2 :     struct TALER_TESTING_Command cmd = {
     428             :       .cls = sss,
     429             :       .label = label,
     430             :       .run = &secret_share_run,
     431             :       .cleanup = &secret_share_cleanup,
     432             :       .traits = &secret_share_traits
     433             :     };
     434             : 
     435           2 :     return cmd;
     436             :   }
     437             : }
     438             : 
     439             : 
     440             : /* end of testing_cmd_secret_share.c */

Generated by: LCOV version 1.14