LCOV - code coverage report
Current view: top level - testing - testing_cmd_truth_upload.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 62 89 69.7 %
Date: 2021-06-16 06:33:01 Functions: 7 7 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_truth_upload.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             : 
      29             : 
      30             : /**
      31             :  * State for a "truth upload" CMD.
      32             :  */
      33             : struct TruthUploadState
      34             : {
      35             :   /**
      36             :    * The interpreter state.
      37             :    */
      38             :   struct TALER_TESTING_Interpreter *is;
      39             : 
      40             :   /**
      41             :    * URL of the anastasis backend.
      42             :    */
      43             :   const char *anastasis_url;
      44             : 
      45             :   /**
      46             :    * Label of this command.
      47             :    */
      48             :   const char *label;
      49             : 
      50             :   /**
      51             :    * The ID data to generate user identifier
      52             :    */
      53             :   json_t *id_data;
      54             : 
      55             :   /**
      56             :    * The escrow method
      57             :    */
      58             :   const char *method;
      59             : 
      60             :   /**
      61             :    * Instructions to be returned to client/user
      62             :    * (e.g. "Look at your smartphone. SMS was sent to you")
      63             :    */
      64             :   const char *instructions;
      65             : 
      66             :   /**
      67             :    * Mime type of truth_data (eg. jpeg, string etc.)
      68             :    */
      69             :   const char *mime_type;
      70             : 
      71             :   /**
      72             :    * The truth_data (e.g. hash of answer to a secure question)
      73             :    */
      74             :   void *truth_data;
      75             : 
      76             :   /**
      77             :    * Requested order ID for this upload (if unpaid).
      78             :    */
      79             :   struct ANASTASIS_PaymentSecretP payment_secret_response;
      80             : 
      81             :   /**
      82             :    * Size of truth_data
      83             :    */
      84             :   size_t truth_data_size;
      85             : 
      86             :   /**
      87             :    * Expected status code.
      88             :    */
      89             :   unsigned int http_status;
      90             : 
      91             :   /**
      92             :    * The /truth POST operation handle.
      93             :    */
      94             :   struct ANASTASIS_TruthUpload *tuo;
      95             : 
      96             :   /**
      97             :    * closure for the payment callback
      98             :    */
      99             :   void *tpc_cls;
     100             : 
     101             :   /**
     102             :    * Reference to salt download.
     103             :    */
     104             :   const char *salt_reference;
     105             : 
     106             :   /**
     107             :    * Options for how we are supposed to do the upload.
     108             :    */
     109             :   enum ANASTASIS_TESTING_TruthStoreOption tsopt;
     110             : 
     111             :   /**
     112             :    * Truth object
     113             :    */
     114             :   struct ANASTASIS_Truth *truth;
     115             : };
     116             : 
     117             : 
     118             : /**
     119             :  * Upload information
     120             :  * caller MUST free 't' using ANASTASIS_truth_free()
     121             :  *
     122             :  * @param cls closure for callback
     123             :  * @param t Truth object (contains provider url and truth public key)
     124             :  * @param ud upload details, useful to continue in case of errors, NULL on success
     125             :  */
     126             : static void
     127           3 : truth_upload_cb (void *cls,
     128             :                  struct ANASTASIS_Truth *t,
     129             :                  const struct ANASTASIS_UploadDetails *ud)
     130             : {
     131           3 :   struct TruthUploadState *tus = cls;
     132             : 
     133           3 :   tus->tuo = NULL;
     134           3 :   if (NULL == ud)
     135             :   {
     136           0 :     GNUNET_break (0);
     137           0 :     TALER_TESTING_interpreter_fail (tus->is);
     138           0 :     return;
     139             :   }
     140           3 :   if (ud->http_status != tus->http_status)
     141             :   {
     142           0 :     GNUNET_break (0);
     143           0 :     TALER_TESTING_interpreter_fail (tus->is);
     144           0 :     return;
     145             :   }
     146           3 :   if (MHD_HTTP_PAYMENT_REQUIRED == ud->http_status)
     147             :   {
     148           0 :     if (ANASTASIS_US_PAYMENT_REQUIRED != ud->us)
     149             :     {
     150           0 :       GNUNET_break (0);
     151           0 :       TALER_TESTING_interpreter_fail (tus->is);
     152           0 :       return;
     153             :     }
     154           0 :     tus->payment_secret_response = ud->details.payment.ps;
     155           0 :     TALER_TESTING_interpreter_next (tus->is);
     156           0 :     return;
     157             :   }
     158           3 :   if ( (ANASTASIS_US_SUCCESS == ud->us) &&
     159             :        (NULL == t) )
     160             :   {
     161           0 :     GNUNET_break (0);
     162           0 :     TALER_TESTING_interpreter_next (tus->is);
     163           0 :     return;
     164             :   }
     165           3 :   tus->truth = t;
     166           3 :   TALER_TESTING_interpreter_next (tus->is);
     167             : }
     168             : 
     169             : 
     170             : /**
     171             :  * Run a "truth upload" CMD.
     172             :  *
     173             :  * @param cls closure.
     174             :  * @param cmd command currently being run.
     175             :  * @param is interpreter state.
     176             :  */
     177             : static void
     178           3 : truth_upload_run (void *cls,
     179             :                   const struct TALER_TESTING_Command *cmd,
     180             :                   struct TALER_TESTING_Interpreter *is)
     181             : {
     182           3 :   struct TruthUploadState *tus = cls;
     183             :   const struct TALER_TESTING_Command *ref;
     184             :   const struct ANASTASIS_CRYPTO_ProviderSaltP *salt;
     185             :   struct ANASTASIS_CRYPTO_UserIdentifierP user_id;
     186             : 
     187           3 :   tus->is = is;
     188           3 :   if (NULL != tus->salt_reference)
     189             :   {
     190           3 :     ref = TALER_TESTING_interpreter_lookup_command
     191             :             (is,
     192             :             tus->salt_reference);
     193           3 :     if (NULL == ref)
     194             :     {
     195           0 :       GNUNET_break (0);
     196           0 :       TALER_TESTING_interpreter_fail (tus->is);
     197           0 :       return;
     198             :     }
     199           3 :     if (GNUNET_OK !=
     200           3 :         ANASTASIS_TESTING_get_trait_salt (ref,
     201             :                                           0,
     202             :                                           &salt))
     203             :     {
     204           0 :       GNUNET_break (0);
     205           0 :       TALER_TESTING_interpreter_fail (tus->is);
     206           0 :       return;
     207             :     }
     208             :   }
     209             : 
     210           3 :   ANASTASIS_CRYPTO_user_identifier_derive (tus->id_data,
     211             :                                            salt,
     212             :                                            &user_id);
     213             : 
     214           3 :   tus->tuo = ANASTASIS_truth_upload (is->ctx,
     215             :                                      &user_id,
     216             :                                      tus->anastasis_url,
     217             :                                      tus->method,
     218             :                                      tus->instructions,
     219             :                                      tus->mime_type,
     220             :                                      salt,
     221           3 :                                      tus->truth_data,
     222             :                                      tus->truth_data_size,
     223             :                                      false, /* force payment */
     224             :                                      GNUNET_TIME_UNIT_ZERO,
     225             :                                      &truth_upload_cb,
     226             :                                      tus);
     227           3 :   if (NULL == tus->tuo)
     228             :   {
     229           0 :     GNUNET_break (0);
     230           0 :     TALER_TESTING_interpreter_fail (tus->is);
     231             :   }
     232             : }
     233             : 
     234             : 
     235             : /**
     236             :  * Free the state of a "truth upload" CMD, and possibly
     237             :  * cancel it if it did not complete.
     238             :  *
     239             :  * @param cls closure.
     240             :  * @param cmd command being freed.
     241             :  */
     242             : static void
     243           3 : truth_upload_cleanup (void *cls,
     244             :                       const struct TALER_TESTING_Command *cmd)
     245             : {
     246           3 :   struct TruthUploadState *tus = cls;
     247             : 
     248           3 :   if (NULL != tus->tuo)
     249             :   {
     250           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     251             :                 "Command '%s' did not complete\n",
     252             :                 cmd->label);
     253           0 :     ANASTASIS_truth_upload_cancel (tus->tuo);
     254           0 :     tus->tuo = NULL;
     255             :   }
     256           3 :   if (NULL != tus->id_data)
     257             :   {
     258           3 :     json_decref (tus->id_data);
     259           3 :     tus->id_data = NULL;
     260             :   }
     261           3 :   if (NULL != tus->truth)
     262             :   {
     263           3 :     ANASTASIS_truth_free (tus->truth);
     264           3 :     tus->truth = NULL;
     265             :   }
     266           3 :   GNUNET_free (tus->truth_data);
     267           3 :   GNUNET_free (tus);
     268           3 : }
     269             : 
     270             : 
     271             : /**
     272             :  * Offer internal data to other commands.
     273             :  *
     274             :  * @param cls closure
     275             :  * @param ret[out] result (could be anything)
     276             :  * @param trait name of the trait
     277             :  * @param index index number of the object to extract.
     278             :  * @return #GNUNET_OK on success
     279             :  */
     280             : static int
     281           6 : truth_upload_traits (void *cls,
     282             :                      const void **ret,
     283             :                      const char *trait,
     284             :                      unsigned int index)
     285             : {
     286           6 :   struct TruthUploadState *tus = cls;
     287             :   struct TALER_TESTING_Trait traits[] = {
     288           6 :     ANASTASIS_TESTING_make_trait_truth (0,
     289           6 :                                         tus->truth),
     290           6 :     ANASTASIS_TESTING_make_trait_payment_secret (0,
     291           6 :                                                  &tus->payment_secret_response),
     292           6 :     TALER_TESTING_trait_end ()
     293             :   };
     294             : 
     295           6 :   return TALER_TESTING_get_trait (traits,
     296             :                                   ret,
     297             :                                   trait,
     298             :                                   index);
     299             : }
     300             : 
     301             : 
     302             : json_t *
     303           1 : ANASTASIS_TESTING_make_id_data_example (const char *id_data)
     304             : {
     305             :   json_t *id;
     306             : 
     307           1 :   id = json_pack ("{s:s}",
     308             :                   "id_data", id_data);
     309           1 :   GNUNET_assert (NULL != id);
     310           1 :   return id;
     311             : }
     312             : 
     313             : 
     314             : struct TALER_TESTING_Command
     315           3 : ANASTASIS_TESTING_cmd_truth_upload (
     316             :   const char *label,
     317             :   const char *anastasis_url,
     318             :   const json_t *id_data,
     319             :   const char *method,
     320             :   const char *instructions,
     321             :   const char *mime_type,
     322             :   const void *truth_data,
     323             :   size_t truth_data_size,
     324             :   unsigned int http_status,
     325             :   enum ANASTASIS_TESTING_TruthStoreOption tso,
     326             :   const char *salt_ref)
     327             : {
     328             :   struct TruthUploadState *tus;
     329             : 
     330           3 :   tus = GNUNET_new (struct TruthUploadState);
     331           3 :   tus->label = label;
     332           3 :   tus->http_status = http_status;
     333           3 :   tus->tsopt = tso;
     334           3 :   tus->anastasis_url = anastasis_url;
     335           3 :   tus->salt_reference = salt_ref;
     336           3 :   tus->id_data = json_incref ((json_t *) id_data);
     337           3 :   tus->method = method;
     338           3 :   tus->instructions = instructions;
     339           3 :   tus->mime_type = mime_type;
     340           3 :   tus->truth_data_size = truth_data_size;
     341           3 :   tus->truth_data = GNUNET_memdup (truth_data,
     342             :                                    truth_data_size);
     343             :   {
     344           3 :     struct TALER_TESTING_Command cmd = {
     345             :       .cls = tus,
     346             :       .label = label,
     347             :       .run = &truth_upload_run,
     348             :       .cleanup = &truth_upload_cleanup,
     349             :       .traits = &truth_upload_traits
     350             :     };
     351             : 
     352           3 :     return cmd;
     353             :   }
     354             : }
     355             : 
     356             : 
     357             : struct TALER_TESTING_Command
     358           2 : ANASTASIS_TESTING_cmd_truth_upload_question (
     359             :   const char *label,
     360             :   const char *anastasis_url,
     361             :   const json_t *id_data,
     362             :   const char *instructions,
     363             :   const char *mime_type,
     364             :   const void *answer,
     365             :   unsigned int http_status,
     366             :   enum ANASTASIS_TESTING_TruthStoreOption tso,
     367             :   const char *salt_ref)
     368             : {
     369           2 :   return ANASTASIS_TESTING_cmd_truth_upload (label,
     370             :                                              anastasis_url,
     371             :                                              id_data,
     372             :                                              "question",
     373             :                                              instructions,
     374             :                                              mime_type,
     375             :                                              answer,
     376             :                                              strlen (answer),
     377             :                                              http_status,
     378             :                                              tso,
     379             :                                              salt_ref);
     380             : }
     381             : 
     382             : 
     383             : /* end of testing_cmd_truth_upload.c */

Generated by: LCOV version 1.14