LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_policy_store.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 73 115 63.5 %
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) 2014-2019 Taler Systems SA
       4             : 
       5             :   ANASTASIS 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             :   ANASTASIS 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 ANASTASIS; see the file COPYING.  If not, see
      17             :   <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file lib/testing_api_cmd_policy_store.c
      22             :  * @brief command to execute the anastasis backend service.
      23             :  * @author Christian Grothoff
      24             :  * @author Dominik Meister
      25             :  * @author Dennis Neufeld
      26             :  */
      27             : 
      28             : #include "platform.h"
      29             : #include "anastasis_testing_lib.h"
      30             : #include <taler/taler_util.h>
      31             : #include <taler/taler_testing_lib.h>
      32             : #include <taler/taler_merchant_service.h>
      33             : 
      34             : 
      35             : /**
      36             :  * State for a "policy store" CMD.
      37             :  */
      38             : struct PolicyStoreState
      39             : {
      40             :   /**
      41             :    * Claim token we got back, if any. Otherwise all zeros.
      42             :    */
      43             :   struct TALER_ClaimTokenP claim_token;
      44             : 
      45             :   /**
      46             :    * The policy data.
      47             :    */
      48             :   const void *recovery_data;
      49             : 
      50             :   /**
      51             :    * Number of bytes in @e recovery_data
      52             :    */
      53             :   size_t recovery_data_size;
      54             : 
      55             :   /**
      56             :    * Expected status code.
      57             :    */
      58             :   unsigned int http_status;
      59             : 
      60             :   /**
      61             :    * Eddsa Publickey.
      62             :    */
      63             :   struct ANASTASIS_CRYPTO_AccountPublicKeyP anastasis_pub;
      64             : 
      65             :   /**
      66             :    * Eddsa Privatekey.
      67             :    */
      68             :   struct ANASTASIS_CRYPTO_AccountPrivateKeyP anastasis_priv;
      69             : 
      70             :   /**
      71             :    * Hash of uploaded data, used to verify the response.
      72             :    */
      73             :   struct GNUNET_HashCode curr_hash;
      74             : 
      75             :   /**
      76             :    * The /policy POST operation handle.
      77             :    */
      78             :   struct ANASTASIS_PolicyStoreOperation *pso;
      79             : 
      80             :   /**
      81             :    * The nonce.
      82             :    */
      83             :   struct ANASTASIS_CRYPTO_NonceP nonce;
      84             : 
      85             :   /**
      86             :    * URL of the anastasis backend.
      87             :    */
      88             :   const char *anastasis_url;
      89             : 
      90             :   /**
      91             :    * The interpreter state.
      92             :    */
      93             :   struct TALER_TESTING_Interpreter *is;
      94             : 
      95             :   /**
      96             :    * Previous upload, or NULL for none. Used to calculate what THIS
      97             :    * upload is based on.
      98             :    */
      99             :   const char *prev_upload;
     100             : 
     101             :   /**
     102             :    * Payment order ID we are to provide in the request, or zero.
     103             :    */
     104             :   struct ANASTASIS_PaymentSecretP payment_secret_request;
     105             : 
     106             :   /**
     107             :    * The order ID, for making the payment.
     108             :    */
     109             :   char *order_id;
     110             : 
     111             :   /**
     112             :    * Payment order ID we are to provide in the response, or zero.
     113             :    */
     114             :   struct ANASTASIS_PaymentSecretP payment_secret_response;
     115             : 
     116             :   /**
     117             :    * Options for how we are supposed to do the upload.
     118             :    */
     119             :   enum ANASTASIS_TESTING_PolicyStoreOption psopt;
     120             : 
     121             :   /**
     122             :    * True if @e payment_secret_request is initialized.
     123             :    */
     124             :   bool payment_secret_set;
     125             : };
     126             : 
     127             : /**
     128             :  * Function called with the results of a #policy_store().
     129             :  *
     130             :  * @param cls closure
     131             :  * @param http_status HTTP status of the request
     132             :  * @param ud details about the upload operation
     133             :  */
     134             : static void
     135           2 : policy_store_cb (void *cls,
     136             :                  const struct ANASTASIS_UploadDetails *ud)
     137             : {
     138           2 :   struct PolicyStoreState *pss = cls;
     139             : 
     140           2 :   pss->pso = NULL;
     141           2 :   if (ud->http_status != pss->http_status)
     142             :   {
     143           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     144             :                 "Unexpected response code %u to command %s in %s:%u\n",
     145             :                 ud->http_status,
     146             :                 pss->is->commands[pss->is->ip].label,
     147             :                 __FILE__,
     148             :                 __LINE__);
     149           0 :     TALER_TESTING_interpreter_fail (pss->is);
     150           0 :     return;
     151             :   }
     152           2 :   switch (ud->us)
     153             :   {
     154           1 :   case ANASTASIS_US_SUCCESS:
     155           1 :     if (0 != GNUNET_memcmp (&pss->curr_hash,
     156             :                             ud->details.success.curr_backup_hash))
     157             :     {
     158           0 :       GNUNET_break (0);
     159           0 :       TALER_TESTING_interpreter_fail (pss->is);
     160           0 :       return;
     161             :     }
     162           1 :     break;
     163           1 :   case ANASTASIS_US_PAYMENT_REQUIRED:
     164           1 :     pss->payment_secret_response = ud->details.payment.ps;
     165             :     {
     166             :       struct TALER_MERCHANT_PayUriData pd;
     167             : 
     168           1 :       if (GNUNET_OK !=
     169           1 :           TALER_MERCHANT_parse_pay_uri (ud->details.payment.payment_request,
     170             :                                         &pd))
     171             :       {
     172           0 :         GNUNET_break (0);
     173           0 :         TALER_TESTING_interpreter_fail (pss->is);
     174           0 :         return;
     175             :       }
     176           1 :       pss->order_id = GNUNET_strdup (pd.order_id);
     177           1 :       if (NULL != pd.claim_token)
     178           0 :         pss->claim_token = *pd.claim_token;
     179           1 :       TALER_MERCHANT_parse_pay_uri_free (&pd);
     180             :     }
     181           1 :     break;
     182           0 :   case ANASTASIS_US_HTTP_ERROR:
     183           0 :     break;
     184           0 :   case ANASTASIS_US_CLIENT_ERROR:
     185           0 :     GNUNET_break (0);
     186           0 :     TALER_TESTING_interpreter_fail (pss->is);
     187           0 :     return;
     188           0 :   case ANASTASIS_US_SERVER_ERROR:
     189           0 :     GNUNET_break (0);
     190           0 :     TALER_TESTING_interpreter_fail (pss->is);
     191           0 :     return;
     192           0 :   default:
     193           0 :     GNUNET_break (0);
     194           0 :     TALER_TESTING_interpreter_fail (pss->is);
     195           0 :     return;
     196             :   }
     197           2 :   TALER_TESTING_interpreter_next (pss->is);
     198             : }
     199             : 
     200             : 
     201             : /**
     202             :  * Run a "policy store" CMD.
     203             :  *
     204             :  * @param cls closure.
     205             :  * @param cmd command currently being run.
     206             :  * @param is interpreter state.
     207             :  */
     208             : static void
     209           2 : policy_store_run (void *cls,
     210             :                   const struct TALER_TESTING_Command *cmd,
     211             :                   struct TALER_TESTING_Interpreter *is)
     212             : {
     213           2 :   struct PolicyStoreState *pss = cls;
     214             : 
     215           2 :   pss->is = is;
     216           2 :   if (NULL != pss->prev_upload)
     217             :   {
     218             :     const struct TALER_TESTING_Command *ref;
     219             : 
     220           1 :     ref = TALER_TESTING_interpreter_lookup_command (is,
     221             :                                                     pss->prev_upload);
     222           1 :     if (NULL == ref)
     223             :     {
     224           0 :       GNUNET_break (0);
     225           0 :       TALER_TESTING_interpreter_fail (pss->is);
     226           0 :       return;
     227             :     }
     228             :     {
     229             :       const struct ANASTASIS_CRYPTO_AccountPrivateKeyP *priv;
     230             : 
     231           1 :       if (GNUNET_OK !=
     232           1 :           ANASTASIS_TESTING_get_trait_account_priv (ref,
     233             :                                                     0,
     234             :                                                     &priv))
     235             :       {
     236           0 :         GNUNET_break (0);
     237           0 :         TALER_TESTING_interpreter_fail (pss->is);
     238           0 :         return;
     239             :       }
     240           1 :       pss->anastasis_priv = *priv;
     241             :     }
     242             :     {
     243             :       const struct ANASTASIS_CRYPTO_AccountPublicKeyP *pub;
     244             : 
     245           1 :       if (GNUNET_OK !=
     246           1 :           ANASTASIS_TESTING_get_trait_account_pub (ref,
     247             :                                                    0,
     248             :                                                    &pub))
     249             :       {
     250           0 :         GNUNET_break (0);
     251           0 :         TALER_TESTING_interpreter_fail (pss->is);
     252           0 :         return;
     253             :       }
     254           1 :       pss->anastasis_pub = *pub;
     255             :     }
     256             :     {
     257             :       const struct ANASTASIS_PaymentSecretP *ps;
     258             : 
     259           1 :       if (GNUNET_OK !=
     260           1 :           ANASTASIS_TESTING_get_trait_payment_secret (ref,
     261             :                                                       0,
     262             :                                                       &ps))
     263             :       {
     264           0 :         GNUNET_break (0);
     265           0 :         TALER_TESTING_interpreter_fail (pss->is);
     266           0 :         return;
     267             :       }
     268           1 :       pss->payment_secret_request = *ps;
     269           1 :       pss->payment_secret_set = true;
     270             :     }
     271             :   }
     272             :   else
     273             :   {
     274           1 :     GNUNET_CRYPTO_eddsa_key_create (&pss->anastasis_priv.priv);
     275           1 :     GNUNET_CRYPTO_eddsa_key_get_public (&pss->anastasis_priv.priv,
     276             :                                         &pss->anastasis_pub.pub);
     277             :   }
     278             : 
     279           2 :   GNUNET_CRYPTO_hash (pss->recovery_data,
     280             :                       pss->recovery_data_size,
     281             :                       &pss->curr_hash);
     282           4 :   pss->pso = ANASTASIS_policy_store (
     283             :     is->ctx,
     284             :     pss->anastasis_url,
     285           2 :     &pss->anastasis_priv,
     286             :     pss->recovery_data,
     287             :     pss->recovery_data_size,
     288           2 :     (0 != (ANASTASIS_TESTING_PSO_REQUEST_PAYMENT & pss->psopt)),
     289           2 :     pss->payment_secret_set ? &pss->payment_secret_request : NULL,
     290             :     GNUNET_TIME_UNIT_ZERO,
     291             :     &policy_store_cb,
     292             :     pss);
     293           2 :   if (NULL == pss->pso)
     294             :   {
     295           0 :     GNUNET_break (0);
     296           0 :     TALER_TESTING_interpreter_fail (pss->is);
     297           0 :     return;
     298             :   }
     299             : }
     300             : 
     301             : 
     302             : /**
     303             :  * Free the state of a "policy store" CMD, and possibly
     304             :  * cancel it if it did not complete.
     305             :  *
     306             :  * @param cls closure.
     307             :  * @param cmd command being freed.
     308             :  */
     309             : static void
     310           2 : policy_store_cleanup (void *cls,
     311             :                       const struct TALER_TESTING_Command *cmd)
     312             : {
     313           2 :   struct PolicyStoreState *pss = cls;
     314             : 
     315           2 :   if (NULL != pss->pso)
     316             :   {
     317           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     318             :                 "Command '%s' did not complete (policy post)\n",
     319             :                 cmd->label);
     320           0 :     ANASTASIS_policy_store_cancel (pss->pso);
     321           0 :     pss->pso = NULL;
     322             :   }
     323           2 :   GNUNET_free (pss->order_id);
     324           2 :   GNUNET_free (pss);
     325           2 : }
     326             : 
     327             : 
     328             : /**
     329             :  * Offer internal data to other commands.
     330             :  *
     331             :  * @param cls closure
     332             :  * @param[out] ret result (could be anything)
     333             :  * @param trait name of the trait
     334             :  * @param index index number of the object to extract.
     335             :  * @return #GNUNET_OK on success
     336             :  */
     337             : static int
     338           8 : policy_store_traits (void *cls,
     339             :                      const void **ret,
     340             :                      const char *trait,
     341             :                      unsigned int index)
     342             : {
     343           8 :   struct PolicyStoreState *pss = cls;
     344             :   struct TALER_TESTING_Trait traits[] = {
     345           8 :     TALER_TESTING_make_trait_claim_token (0,
     346           8 :                                           &pss->claim_token),
     347           8 :     TALER_TESTING_make_trait_order_id (0,
     348           8 :                                        pss->order_id),
     349           8 :     ANASTASIS_TESTING_make_trait_hash (0,
     350           8 :                                        &pss->curr_hash),
     351           8 :     ANASTASIS_TESTING_make_trait_account_pub (0,
     352           8 :                                               &pss->anastasis_pub),
     353           8 :     ANASTASIS_TESTING_make_trait_account_priv (0,
     354           8 :                                                &pss->anastasis_priv),
     355           8 :     ANASTASIS_TESTING_make_trait_payment_secret (0,
     356           8 :                                                  &pss->payment_secret_response),
     357           8 :     TALER_TESTING_trait_end ()
     358             :   };
     359             : 
     360           8 :   return TALER_TESTING_get_trait (traits,
     361             :                                   ret,
     362             :                                   trait,
     363             :                                   index);
     364             : }
     365             : 
     366             : 
     367             : struct TALER_TESTING_Command
     368           2 : ANASTASIS_TESTING_cmd_policy_store (
     369             :   const char *label,
     370             :   const char *anastasis_url,
     371             :   const char *prev_upload,
     372             :   unsigned int http_status,
     373             :   enum ANASTASIS_TESTING_PolicyStoreOption pso,
     374             :   const void *recovery_data,
     375             :   size_t recovery_data_size)
     376             : {
     377             :   struct PolicyStoreState *pss;
     378             : 
     379           2 :   pss = GNUNET_new (struct PolicyStoreState);
     380           2 :   pss->recovery_data = recovery_data;
     381           2 :   pss->recovery_data_size = recovery_data_size;
     382           2 :   pss->http_status = http_status;
     383           2 :   pss->psopt = pso;
     384           2 :   pss->anastasis_url = anastasis_url;
     385           2 :   pss->prev_upload = prev_upload;
     386             :   {
     387           2 :     struct TALER_TESTING_Command cmd = {
     388             :       .cls = pss,
     389             :       .label = label,
     390             :       .run = &policy_store_run,
     391             :       .cleanup = &policy_store_cleanup,
     392             :       .traits = &policy_store_traits
     393             :     };
     394             : 
     395           2 :     return cmd;
     396             :   }
     397             : }

Generated by: LCOV version 1.14