LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_backup_upload.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 143 0.0 %
Date: 2020-10-21 06:15:51 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of SYNC
       3             :   Copyright (C) 2014-2019 Taler Systems SA
       4             : 
       5             :   SYNC 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             :   SYNC 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 SYNC; see the file COPYING.  If not, see
      17             :   <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file lib/testing_api_cmd_backup_upload.c
      21             :  * @brief command to upload data to the sync backend service.
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include "sync_service.h"
      26             : #include "sync_testing_lib.h"
      27             : #include <taler/taler_util.h>
      28             : #include <taler/taler_testing_lib.h>
      29             : #include <taler/taler_merchant_service.h>
      30             : #include "sync_testing_lib.h"
      31             : 
      32             : /**
      33             :  * State for a "backup upload" CMD.
      34             :  */
      35             : struct BackupUploadState
      36             : {
      37             : 
      38             :   /**
      39             :    * Eddsa private key.
      40             :    */
      41             :   struct SYNC_AccountPrivateKeyP sync_priv;
      42             : 
      43             :   /**
      44             :    * Eddsa public key.
      45             :    */
      46             :   struct SYNC_AccountPublicKeyP sync_pub;
      47             : 
      48             :   /**
      49             :    * Hash of the previous upload (maybe bogus if
      50             :    * #SYNC_TESTING_UO_PREV_HASH_WRONG is set in @e uo).
      51             :    * Maybe all zeros if there was no previous upload.
      52             :    */
      53             :   struct GNUNET_HashCode prev_hash;
      54             : 
      55             :   /**
      56             :    * Hash of the current upload.
      57             :    */
      58             :   struct GNUNET_HashCode curr_hash;
      59             : 
      60             :   /**
      61             :    * The /backups POST operation handle.
      62             :    */
      63             :   struct SYNC_UploadOperation *uo;
      64             : 
      65             :   /**
      66             :    * URL of the sync backend.
      67             :    */
      68             :   const char *sync_url;
      69             : 
      70             :   /**
      71             :    * Previous upload, or NULL for none. Used to calculate what THIS
      72             :    * upload is based on.
      73             :    */
      74             :   const char *prev_upload;
      75             : 
      76             :   /**
      77             :    * Last upload, or NULL for none, usually same as @e prev_upload.
      78             :    * Used to check the response on #MHD_HTTP_CONFLICT.
      79             :    */
      80             :   const char *last_upload;
      81             : 
      82             :   /**
      83             :    * Payment order ID we got back, if any. Otherwise NULL.
      84             :    */
      85             :   char *payment_order_id;
      86             : 
      87             :   /**
      88             :    * Claim token we got back, if any. Otherwise all zeros.
      89             :    */
      90             :   struct TALER_ClaimTokenP token;
      91             : 
      92             :   /**
      93             :    * Payment order ID we are to provide in the request, may be NULL.
      94             :    */
      95             :   const char *payment_order_req;
      96             : 
      97             :   /**
      98             :    * The interpreter state.
      99             :    */
     100             :   struct TALER_TESTING_Interpreter *is;
     101             : 
     102             :   /**
     103             :    * The backup data we are uploading.
     104             :    */
     105             :   const void *backup;
     106             : 
     107             :   /**
     108             :    * Number of bytes in @e backup.
     109             :    */
     110             :   size_t backup_size;
     111             : 
     112             :   /**
     113             :    * Expected status code.
     114             :    */
     115             :   unsigned int http_status;
     116             : 
     117             :   /**
     118             :    * Options for how we are supposed to do the upload.
     119             :    */
     120             :   enum SYNC_TESTING_UploadOption uopt;
     121             : 
     122             : };
     123             : 
     124             : 
     125             : /**
     126             :  * Function called with the results of a #SYNC_upload().
     127             :  *
     128             :  * @param cls closure
     129             :  * @param ec Taler error code
     130             :  * @param http_status HTTP status of the request
     131             :  * @param ud details about the upload operation
     132             :  */
     133             : static void
     134           0 : backup_upload_cb (void *cls,
     135             :                   enum TALER_ErrorCode ec,
     136             :                   unsigned int http_status,
     137             :                   const struct SYNC_UploadDetails *ud)
     138             : {
     139           0 :   struct BackupUploadState *bus = cls;
     140             : 
     141           0 :   bus->uo = NULL;
     142           0 :   if (http_status != bus->http_status)
     143             :   {
     144           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     145             :                 "Unexpected response code %u to command %s in %s:%u\n",
     146             :                 http_status,
     147             :                 bus->is->commands[bus->is->ip].label,
     148             :                 __FILE__,
     149             :                 __LINE__);
     150           0 :     TALER_TESTING_interpreter_fail (bus->is);
     151           0 :     return;
     152             :   }
     153           0 :   if (NULL != ud)
     154             :   {
     155           0 :     switch (ud->us)
     156             :     {
     157           0 :     case SYNC_US_SUCCESS:
     158           0 :       if (0 != GNUNET_memcmp (&bus->curr_hash,
     159             :                               ud->details.curr_backup_hash))
     160             :       {
     161           0 :         GNUNET_break (0);
     162           0 :         TALER_TESTING_interpreter_fail (bus->is);
     163           0 :         return;
     164             :       }
     165           0 :       break;
     166           0 :     case SYNC_US_PAYMENT_REQUIRED:
     167             :       {
     168             :         struct TALER_MERCHANT_PayUriData pd;
     169             : 
     170           0 :         if (GNUNET_OK !=
     171           0 :             TALER_MERCHANT_parse_pay_uri (ud->details.payment_request,
     172             :                                           &pd))
     173             :         {
     174           0 :           GNUNET_break (0);
     175           0 :           TALER_TESTING_interpreter_fail (bus->is);
     176           0 :           return;
     177             :         }
     178           0 :         bus->payment_order_id = GNUNET_strdup (pd.order_id);
     179           0 :         if (NULL != pd.claim_token)
     180           0 :           bus->token = *pd.claim_token;
     181           0 :         TALER_MERCHANT_parse_pay_uri_free (&pd);
     182           0 :         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     183             :                     "Order ID from Sync service is `%s'\n",
     184             :                     bus->payment_order_id);
     185           0 :         memset (&bus->curr_hash,
     186             :                 0,
     187             :                 sizeof (struct GNUNET_HashCode));
     188             :       }
     189           0 :       break;
     190           0 :     case SYNC_US_CONFLICTING_BACKUP:
     191             :       {
     192             :         const struct TALER_TESTING_Command *ref;
     193             :         const struct GNUNET_HashCode *h;
     194             : 
     195           0 :         ref = TALER_TESTING_interpreter_lookup_command
     196             :                 (bus->is,
     197             :                 bus->last_upload);
     198           0 :         GNUNET_assert (NULL != ref);
     199           0 :         GNUNET_assert (GNUNET_OK ==
     200             :                        SYNC_TESTING_get_trait_hash (ref,
     201             :                                                     SYNC_TESTING_TRAIT_HASH_CURRENT,
     202             :                                                     &h));
     203           0 :         if (0 != GNUNET_memcmp (h,
     204             :                                 &ud->details.recovered_backup.
     205             :                                 existing_backup_hash))
     206             :         {
     207           0 :           GNUNET_break (0);
     208           0 :           TALER_TESTING_interpreter_fail (bus->is);
     209           0 :           return;
     210             :         }
     211             :       }
     212             :     case SYNC_US_HTTP_ERROR:
     213           0 :       break;
     214           0 :     case SYNC_US_CLIENT_ERROR:
     215           0 :       GNUNET_break (0);
     216           0 :       TALER_TESTING_interpreter_fail (bus->is);
     217           0 :       return;
     218           0 :     case SYNC_US_SERVER_ERROR:
     219           0 :       GNUNET_break (0);
     220           0 :       TALER_TESTING_interpreter_fail (bus->is);
     221           0 :       return;
     222             :     }
     223           0 :   }
     224           0 :   TALER_TESTING_interpreter_next (bus->is);
     225             : }
     226             : 
     227             : 
     228             : /**
     229             :  * Run a "backup upload" CMD.
     230             :  *
     231             :  * @param cls closure.
     232             :  * @param cmd command currently being run.
     233             :  * @param is interpreter state.
     234             :  */
     235             : static void
     236           0 : backup_upload_run (void *cls,
     237             :                    const struct TALER_TESTING_Command *cmd,
     238             :                    struct TALER_TESTING_Interpreter *is)
     239             : {
     240           0 :   struct BackupUploadState *bus = cls;
     241             : 
     242           0 :   bus->is = is;
     243           0 :   if (NULL != bus->prev_upload)
     244             :   {
     245             :     const struct TALER_TESTING_Command *ref;
     246             : 
     247           0 :     ref = TALER_TESTING_interpreter_lookup_command (
     248             :       is,
     249             :       bus->prev_upload);
     250           0 :     if (NULL == ref)
     251             :     {
     252           0 :       GNUNET_break (0);
     253           0 :       TALER_TESTING_interpreter_fail (bus->is);
     254           0 :       return;
     255             :     }
     256             :     {
     257             :       const struct GNUNET_HashCode *h;
     258             : 
     259           0 :       if (GNUNET_OK ==
     260           0 :           SYNC_TESTING_get_trait_hash (ref,
     261             :                                        SYNC_TESTING_TRAIT_HASH_CURRENT,
     262             :                                        &h))
     263             :       {
     264           0 :         bus->prev_hash = *h;
     265             :       }
     266             :     }
     267             :     {
     268             :       const struct SYNC_AccountPrivateKeyP *priv;
     269             : 
     270           0 :       if (GNUNET_OK !=
     271           0 :           SYNC_TESTING_get_trait_account_priv (ref,
     272             :                                                0,
     273             :                                                &priv))
     274             :       {
     275           0 :         GNUNET_break (0);
     276           0 :         TALER_TESTING_interpreter_fail (bus->is);
     277           0 :         return;
     278             :       }
     279           0 :       bus->sync_priv = *priv;
     280             :     }
     281             :     {
     282             :       const struct SYNC_AccountPublicKeyP *pub;
     283             : 
     284           0 :       if (GNUNET_OK !=
     285           0 :           SYNC_TESTING_get_trait_account_pub (ref,
     286             :                                               0,
     287             :                                               &pub))
     288             :       {
     289           0 :         GNUNET_break (0);
     290           0 :         TALER_TESTING_interpreter_fail (bus->is);
     291           0 :         return;
     292             :       }
     293           0 :       bus->sync_pub = *pub;
     294             :     }
     295           0 :     if (0 != (SYNC_TESTING_UO_REFERENCE_ORDER_ID & bus->uopt))
     296             :     {
     297             :       const char *order_id;
     298             : 
     299           0 :       if (GNUNET_OK !=
     300           0 :           TALER_TESTING_get_trait_order_id (ref,
     301             :                                             0,
     302             :                                             &order_id))
     303             :       {
     304           0 :         GNUNET_break (0);
     305           0 :         TALER_TESTING_interpreter_fail (bus->is);
     306           0 :         return;
     307             :       }
     308           0 :       bus->payment_order_req = order_id;
     309           0 :       if (NULL == bus->payment_order_req)
     310             :       {
     311           0 :         GNUNET_break (0);
     312           0 :         TALER_TESTING_interpreter_fail (bus->is);
     313           0 :         return;
     314             :       }
     315             :     }
     316             :   }
     317             :   else
     318             :   {
     319           0 :     GNUNET_CRYPTO_eddsa_key_create (&bus->sync_priv.eddsa_priv);
     320           0 :     GNUNET_CRYPTO_eddsa_key_get_public (&bus->sync_priv.eddsa_priv,
     321             :                                         &bus->sync_pub.eddsa_pub);
     322             :   }
     323           0 :   if (0 != (SYNC_TESTING_UO_PREV_HASH_WRONG & bus->uopt))
     324           0 :     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     325           0 :                                 &bus->prev_hash,
     326             :                                 sizeof (struct GNUNET_HashCode));
     327           0 :   GNUNET_CRYPTO_hash (bus->backup,
     328             :                       bus->backup_size,
     329             :                       &bus->curr_hash);
     330           0 :   bus->uo = SYNC_upload (is->ctx,
     331             :                          bus->sync_url,
     332             :                          &bus->sync_priv,
     333           0 :                          ( ( (NULL != bus->prev_upload) &&
     334           0 :                              (GNUNET_NO == GNUNET_is_zero (
     335           0 :                                 &bus->prev_hash)) ) ||
     336             :                            (0 != (SYNC_TESTING_UO_PREV_HASH_WRONG
     337           0 :                                   & bus->uopt)) )
     338             :                          ? &bus->prev_hash
     339             :                          : NULL,
     340             :                          bus->backup_size,
     341             :                          bus->backup,
     342           0 :                          (0 != (SYNC_TESTING_UO_REQUEST_PAYMENT & bus->uopt)),
     343             :                          bus->payment_order_req,
     344             :                          &backup_upload_cb,
     345             :                          bus);
     346           0 :   if (NULL == bus->uo)
     347             :   {
     348           0 :     GNUNET_break (0);
     349           0 :     TALER_TESTING_interpreter_fail (bus->is);
     350           0 :     return;
     351             :   }
     352             : }
     353             : 
     354             : 
     355             : /**
     356             :  * Free the state of a "backup upload" CMD, and possibly
     357             :  * cancel it if it did not complete.
     358             :  *
     359             :  * @param cls closure.
     360             :  * @param cmd command being freed.
     361             :  */
     362             : static void
     363           0 : backup_upload_cleanup (void *cls,
     364             :                        const struct TALER_TESTING_Command *cmd)
     365             : {
     366           0 :   struct BackupUploadState *bus = cls;
     367             : 
     368           0 :   if (NULL != bus->uo)
     369             :   {
     370           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     371             :                 "Command '%s' did not complete (backup upload)\n",
     372             :                 cmd->label);
     373           0 :     SYNC_upload_cancel (bus->uo);
     374           0 :     bus->uo = NULL;
     375             :   }
     376           0 :   GNUNET_free (bus->payment_order_id);
     377           0 :   GNUNET_free (bus);
     378           0 : }
     379             : 
     380             : 
     381             : /**
     382             :  * Offer internal data to other commands.
     383             :  *
     384             :  * @param cls closure
     385             :  * @param ret[out] result (could be anything)
     386             :  * @param trait name of the trait
     387             :  * @param index index number of the object to extract.
     388             :  * @return #GNUNET_OK on success
     389             :  */
     390             : static int
     391           0 : backup_upload_traits (void *cls,
     392             :                       const void **ret,
     393             :                       const char *trait,
     394             :                       unsigned int index)
     395             : {
     396           0 :   struct BackupUploadState *bus = cls;
     397             :   struct TALER_TESTING_Trait straits[] = {
     398           0 :     SYNC_TESTING_make_trait_hash (SYNC_TESTING_TRAIT_HASH_CURRENT,
     399           0 :                                   &bus->curr_hash),
     400           0 :     SYNC_TESTING_make_trait_hash (SYNC_TESTING_TRAIT_HASH_PREVIOUS,
     401           0 :                                   &bus->prev_hash),
     402           0 :     TALER_TESTING_make_trait_claim_token (0,
     403           0 :                                           &bus->token),
     404           0 :     SYNC_TESTING_make_trait_account_pub (0,
     405           0 :                                          &bus->sync_pub),
     406           0 :     SYNC_TESTING_make_trait_account_priv (0,
     407           0 :                                           &bus->sync_priv),
     408           0 :     TALER_TESTING_make_trait_order_id (0,
     409           0 :                                        bus->payment_order_id),
     410           0 :     TALER_TESTING_trait_end ()
     411             :   };
     412             :   struct TALER_TESTING_Trait ftraits[] = {
     413           0 :     TALER_TESTING_make_trait_claim_token (0,
     414           0 :                                           &bus->token),
     415           0 :     SYNC_TESTING_make_trait_account_pub (0,
     416           0 :                                          &bus->sync_pub),
     417           0 :     SYNC_TESTING_make_trait_account_priv (0,
     418           0 :                                           &bus->sync_priv),
     419           0 :     TALER_TESTING_make_trait_order_id (0,
     420           0 :                                        bus->payment_order_id),
     421           0 :     TALER_TESTING_trait_end ()
     422             :   };
     423             : 
     424             : 
     425           0 :   return TALER_TESTING_get_trait ((NULL != bus->payment_order_req)
     426             :                                   ? ftraits
     427             :                                   : straits,
     428             :                                   ret,
     429             :                                   trait,
     430             :                                   index);
     431             : }
     432             : 
     433             : 
     434             : /**
     435             :  * Make the "backup upload" command.
     436             :  *
     437             :  * @param label command label
     438             :  * @param sync_url base URL of the sync serving
     439             :  *        the policy store request.
     440             :  * @param prev_upload reference to a previous upload we are
     441             :  *        supposed to update, NULL for none
     442             :  * @param last_upload reference to the last upload for the
     443             :  *          same account, used to check result on MHD_HTTP_CONFLICT
     444             :  * @param uo upload options
     445             :  * @param http_status expected HTTP status.
     446             :  * @param backup_data data to upload
     447             :  * @param backup_data_size number of bytes in @a backup_data
     448             :  * @return the command
     449             :  */
     450             : struct TALER_TESTING_Command
     451           0 : SYNC_TESTING_cmd_backup_upload (const char *label,
     452             :                                 const char *sync_url,
     453             :                                 const char *prev_upload,
     454             :                                 const char *last_upload,
     455             :                                 enum SYNC_TESTING_UploadOption uo,
     456             :                                 unsigned int http_status,
     457             :                                 const void *backup_data,
     458             :                                 size_t backup_data_size)
     459             : {
     460             :   struct BackupUploadState *bus;
     461             : 
     462           0 :   bus = GNUNET_new (struct BackupUploadState);
     463           0 :   bus->http_status = http_status;
     464           0 :   bus->prev_upload = prev_upload;
     465           0 :   bus->last_upload = last_upload;
     466           0 :   bus->uopt = uo;
     467           0 :   bus->sync_url = sync_url;
     468           0 :   bus->backup = backup_data;
     469           0 :   bus->backup_size = backup_data_size;
     470             :   {
     471           0 :     struct TALER_TESTING_Command cmd = {
     472             :       .cls = bus,
     473             :       .label = label,
     474             :       .run = &backup_upload_run,
     475             :       .cleanup = &backup_upload_cleanup,
     476             :       .traits = &backup_upload_traits
     477             :     };
     478             : 
     479           0 :     return cmd;
     480             :   }
     481             : }

Generated by: LCOV version 1.14