LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_policy_lookup.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 35 64 54.7 %
Date: 2021-06-16 06:33:01 Functions: 4 5 80.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_api_cmd_policy_lookup.c
      18             :  * @brief command to execute the anastasis backend service.
      19             :  * @author Dennis Neufeld
      20             :  * @author Dominik Meister
      21             :  */
      22             : 
      23             : #include "platform.h"
      24             : #include "anastasis_testing_lib.h"
      25             : #include <taler/taler_util.h>
      26             : #include <taler/taler_testing_lib.h>
      27             : 
      28             : 
      29             : /**
      30             :  * State for a "policy lookup" CMD.
      31             :  */
      32             : struct PolicyLookupState
      33             : {
      34             :   /**
      35             :    * The interpreter state.
      36             :    */
      37             :   struct TALER_TESTING_Interpreter *is;
      38             : 
      39             :   /**
      40             :    * Eddsa Publickey.
      41             :    */
      42             :   struct ANASTASIS_CRYPTO_AccountPublicKeyP anastasis_pub;
      43             : 
      44             :   /**
      45             :    * Hash of the upload (all zeros if there was no upload).
      46             :    */
      47             :   const struct GNUNET_HashCode *upload_hash;
      48             : 
      49             :   /**
      50             :    * URL of the anastasis backend.
      51             :    */
      52             :   const char *anastasis_url;
      53             : 
      54             :   /**
      55             :    * Expected status code.
      56             :    */
      57             :   unsigned int http_status;
      58             : 
      59             :   /**
      60             :    * Reference to upload command we expect to lookup.
      61             :    */
      62             :   const char *upload_reference;
      63             : 
      64             :   /**
      65             :    * The /policy GET operation handle.
      66             :    */
      67             :   struct ANASTASIS_PolicyLookupOperation *plo;
      68             : };
      69             : 
      70             : 
      71             : /**
      72             :  * Function called with the results of a #ANASTASIS_lookup().
      73             :  *
      74             :  * @param cls closure
      75             :  * @param http_status HTTP status of the request
      76             :  * @param ud details about the lookup operation
      77             :  */
      78             : static void
      79           1 : policy_lookup_cb (void *cls,
      80             :                   unsigned int http_status,
      81             :                   const struct ANASTASIS_DownloadDetails *dd)
      82             : {
      83           1 :   struct PolicyLookupState *pls = cls;
      84             : 
      85           1 :   pls->plo = NULL;
      86           1 :   if (http_status != pls->http_status)
      87             :   {
      88           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      89             :                 "Unexpected response code %u to command %s in %s:%u\n",
      90             :                 http_status,
      91             :                 pls->is->commands[pls->is->ip].label,
      92             :                 __FILE__,
      93             :                 __LINE__);
      94           0 :     TALER_TESTING_interpreter_fail (pls->is);
      95           0 :     return;
      96             :   }
      97           1 :   if (NULL != pls->upload_reference)
      98             :   {
      99           1 :     if ( (MHD_HTTP_OK == http_status) &&
     100           1 :          (0 != GNUNET_memcmp (&dd->curr_policy_hash,
     101             :                               pls->upload_hash)) )
     102             :     {
     103           0 :       GNUNET_break (0);
     104           0 :       TALER_TESTING_interpreter_fail (pls->is);
     105           0 :       return;
     106             :     }
     107             :   }
     108           1 :   TALER_TESTING_interpreter_next (pls->is);
     109             : }
     110             : 
     111             : 
     112             : /**
     113             :  * Run a "policy lookup" CMD.
     114             :  *
     115             :  * @param cls closure.
     116             :  * @param cmd command currently being run.
     117             :  * @param is interpreter state.
     118             :  */
     119             : static void
     120           1 : policy_lookup_run (void *cls,
     121             :                    const struct TALER_TESTING_Command *cmd,
     122             :                    struct TALER_TESTING_Interpreter *is)
     123             : {
     124           1 :   struct PolicyLookupState *pls = cls;
     125             : 
     126           1 :   pls->is = is;
     127           1 :   if (NULL != pls->upload_reference)
     128             :   {
     129             :     const struct TALER_TESTING_Command *upload_cmd;
     130             :     const struct ANASTASIS_CRYPTO_AccountPublicKeyP *anastasis_pub;
     131             : 
     132           1 :     upload_cmd = TALER_TESTING_interpreter_lookup_command
     133             :                    (is,
     134             :                    pls->upload_reference);
     135           1 :     if (NULL == upload_cmd)
     136             :     {
     137           0 :       GNUNET_break (0);
     138           0 :       TALER_TESTING_interpreter_fail (pls->is);
     139           0 :       return;
     140             :     }
     141           1 :     if (GNUNET_OK !=
     142           1 :         ANASTASIS_TESTING_get_trait_hash (upload_cmd,
     143             :                                           ANASTASIS_TESTING_TRAIT_HASH_CURRENT,
     144             :                                           &pls->upload_hash))
     145             :     {
     146           0 :       GNUNET_break (0);
     147           0 :       TALER_TESTING_interpreter_fail (pls->is);
     148           0 :       return;
     149             :     }
     150           1 :     if (GNUNET_OK !=
     151           1 :         ANASTASIS_TESTING_get_trait_account_pub (upload_cmd,
     152             :                                                  0,
     153             :                                                  &anastasis_pub))
     154             :     {
     155           0 :       GNUNET_break (0);
     156           0 :       TALER_TESTING_interpreter_fail (pls->is);
     157           0 :       return;
     158             :     }
     159           1 :     pls->anastasis_pub = *anastasis_pub;
     160             :   }
     161           2 :   pls->plo = ANASTASIS_policy_lookup (is->ctx,
     162             :                                       pls->anastasis_url,
     163           1 :                                       &pls->anastasis_pub,
     164             :                                       &policy_lookup_cb,
     165             :                                       pls);
     166           1 :   if (NULL == pls->plo)
     167             :   {
     168           0 :     GNUNET_break (0);
     169           0 :     TALER_TESTING_interpreter_fail (pls->is);
     170           0 :     return;
     171             :   }
     172             : }
     173             : 
     174             : 
     175             : /**
     176             :  * Free the state of a "policy lookup" CMD, and possibly
     177             :  * cancel it if it did not complete.
     178             :  *
     179             :  * @param cls closure.
     180             :  * @param cmd command being freed.
     181             :  */
     182             : static void
     183           1 : policy_lookup_cleanup (void *cls,
     184             :                        const struct TALER_TESTING_Command *cmd)
     185             : {
     186           1 :   struct PolicyLookupState *pls = cls;
     187             : 
     188           1 :   if (NULL != pls->plo)
     189             :   {
     190           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     191             :                 "Command '%s' did not complete (policy lookup)\n",
     192             :                 cmd->label);
     193           0 :     ANASTASIS_policy_lookup_cancel (pls->plo);
     194           0 :     pls->plo = NULL;
     195             :   }
     196           1 :   GNUNET_free (pls);
     197           1 : }
     198             : 
     199             : 
     200             : /**
     201             :  * Make the "policy lookup" command.
     202             :  *
     203             :  * @param label command label
     204             :  * @param anastasis_url base URL of the ANASTASIS serving
     205             :  *        the policy store request.
     206             :  * @param http_status expected HTTP status.
     207             :  * @param upload_ref reference to upload command
     208             :  * @return the command
     209             :  */
     210             : struct TALER_TESTING_Command
     211           1 : ANASTASIS_TESTING_cmd_policy_lookup (const char *label,
     212             :                                      const char *anastasis_url,
     213             :                                      unsigned int http_status,
     214             :                                      const char *upload_ref)
     215             : {
     216             :   struct PolicyLookupState *pls;
     217             : 
     218           1 :   GNUNET_assert (NULL != upload_ref);
     219           1 :   pls = GNUNET_new (struct PolicyLookupState);
     220           1 :   pls->http_status = http_status;
     221           1 :   pls->anastasis_url = anastasis_url;
     222           1 :   pls->upload_reference = upload_ref;
     223             :   {
     224           1 :     struct TALER_TESTING_Command cmd = {
     225             :       .cls = pls,
     226             :       .label = label,
     227             :       .run = &policy_lookup_run,
     228             :       .cleanup = &policy_lookup_cleanup
     229             :     };
     230             : 
     231           1 :     return cmd;
     232             :   }
     233             : }
     234             : 
     235             : 
     236             : /**
     237             :  * Make the "policy lookup" command for a non-existent upload.
     238             :  *
     239             :  * @param label command label
     240             :  * @param anastasis_url base URL of the ANASTASIS serving
     241             :  *        the policy lookup request.
     242             :  * @return the command
     243             :  */
     244             : struct TALER_TESTING_Command
     245           0 : ANASTASIS_TESTING_cmd_policy_nx (const char *label,
     246             :                                  const char *anastasis_url)
     247             : {
     248             :   struct PolicyLookupState *pls;
     249             :   struct GNUNET_CRYPTO_EddsaPrivateKey priv;
     250             : 
     251           0 :   pls = GNUNET_new (struct PolicyLookupState);
     252           0 :   pls->http_status = MHD_HTTP_NOT_FOUND;
     253           0 :   pls->anastasis_url = anastasis_url;
     254           0 :   GNUNET_CRYPTO_eddsa_key_create (&priv);
     255           0 :   GNUNET_CRYPTO_eddsa_key_get_public (&priv,
     256             :                                       &pls->anastasis_pub.pub);
     257             :   {
     258           0 :     struct TALER_TESTING_Command cmd = {
     259             :       .cls = pls,
     260             :       .label = label,
     261             :       .run = &policy_lookup_run,
     262             :       .cleanup = &policy_lookup_cleanup
     263             :     };
     264             : 
     265           0 :     return cmd;
     266             :   }
     267             : }

Generated by: LCOV version 1.14