LCOV - code coverage report
Current view: top level - /workdir/src/testing - test_sync_api.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 32 32
Test Date: 2025-12-22 20:19:38 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014-2024 Taler Systems SA
       4              : 
       5              :   TALER 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              :   TALER 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 TALER; see the file COPYING.  If not, see
      17              :   <http://www.gnu.org/licenses/>
      18              : */
      19              : /**
      20              :  * @file lib/test_sync_api.c
      21              :  * @brief testcase to test sync's HTTP API interface
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "platform.h"
      25              : #include <taler/taler_util.h>
      26              : #include <taler/taler_signatures.h>
      27              : #include <taler/taler_exchange_service.h>
      28              : #include <taler/taler_json_lib.h>
      29              : #include <gnunet/gnunet_util_lib.h>
      30              : #include <microhttpd.h>
      31              : #include <taler/taler_bank_service.h>
      32              : #include <taler/taler_fakebank_lib.h>
      33              : #include <taler/taler_testing_lib.h>
      34              : #include <taler/taler_merchant_testing_lib.h>
      35              : #include <taler/taler_error_codes.h>
      36              : #include "sync_service.h"
      37              : #include "sync_testing_lib.h"
      38              : 
      39              : /**
      40              :  * Configuration file we use.  One (big) configuration is used
      41              :  * for the various components for this test.
      42              :  */
      43              : #define CONFIG_FILE "test_sync_api.conf"
      44              : 
      45              : /**
      46              :  * Exchange base URL.  Could also be taken from config.
      47              :  */
      48              : #define EXCHANGE_URL "http://localhost:8081/"
      49              : 
      50              : /**
      51              :  * Account number of the exchange at the bank.
      52              :  */
      53              : #define EXCHANGE_ACCOUNT_NAME "2"
      54              : 
      55              : /**
      56              :  * Account number of some user.
      57              :  */
      58              : #define USER_ACCOUNT_NAME "62"
      59              : 
      60              : /**
      61              :  * Account number used by the merchant
      62              :  */
      63              : #define MERCHANT_ACCOUNT_NAME "3"
      64              : 
      65              : /**
      66              :  * Payto URI of the customer (payer).
      67              :  */
      68              : static struct TALER_FullPayto payer_payto;
      69              : 
      70              : /**
      71              :  * Payto URI of the exchange (escrow account).
      72              :  */
      73              : static struct TALER_FullPayto exchange_payto;
      74              : 
      75              : /**
      76              :  * Payto URI of the merchant (receiver).
      77              :  */
      78              : static struct TALER_FullPayto merchant_payto;
      79              : 
      80              : /**
      81              :  * Configuration of the bank.
      82              :  */
      83              : static struct TALER_TESTING_Credentials cred;
      84              : 
      85              : /**
      86              :  * Merchant base URL.
      87              :  */
      88              : static const char *merchant_url = "http://localhost:8080/";
      89              : 
      90              : /**
      91              :  * Sync base URL.
      92              :  */
      93              : static const char *sync_url = "http://localhost:8084/";
      94              : 
      95              : 
      96              : /**
      97              :  * Execute the taler-exchange-wirewatch command with
      98              :  * our configuration file.
      99              :  *
     100              :  * @param label label to use for the command.
     101              :  */
     102              : static struct TALER_TESTING_Command
     103            1 : cmd_exec_wirewatch (const char *label)
     104              : {
     105            1 :   return TALER_TESTING_cmd_exec_wirewatch (label,
     106              :                                            CONFIG_FILE);
     107              : }
     108              : 
     109              : 
     110              : /**
     111              :  * Run wire transfer of funds from some user's account to the
     112              :  * exchange.
     113              :  *
     114              :  * @param label label to use for the command.
     115              :  * @param amount amount to transfer, i.e. "EUR:1"
     116              :  * @param url exchange_url
     117              :  */
     118              : static struct TALER_TESTING_Command
     119            1 : cmd_transfer_to_exchange (const char *label,
     120              :                           const char *amount)
     121              : {
     122            1 :   return TALER_TESTING_cmd_admin_add_incoming (label,
     123              :                                                amount,
     124              :                                                &cred.ba,
     125              :                                                payer_payto);
     126              : }
     127              : 
     128              : 
     129              : /**
     130              :  * Main function that will tell the interpreter what commands to
     131              :  * run.
     132              :  *
     133              :  * @param cls closure
     134              :  */
     135              : static void
     136            1 : run (void *cls,
     137              :      struct TALER_TESTING_Interpreter *is)
     138              : {
     139              :   struct TALER_TESTING_Command commands[] = {
     140            1 :     TALER_TESTING_cmd_system_start ("start-taler",
     141              :                                     CONFIG_FILE,
     142              :                                     "-efms",
     143              :                                     "-u", "exchange-account-exchange",
     144              :                                     "-r", "merchant-exchange-default",
     145              :                                     NULL),
     146            1 :     TALER_TESTING_cmd_get_exchange ("get-exchange",
     147            1 :                                     cred.cfg,
     148              :                                     NULL,
     149              :                                     true,
     150              :                                     true),
     151            1 :     TALER_TESTING_cmd_merchant_post_instances ("instance-create-admin",
     152              :                                                merchant_url,
     153              :                                                "admin",
     154              :                                                MHD_HTTP_NO_CONTENT),
     155            1 :     TALER_TESTING_cmd_merchant_post_account (
     156              :       "instance-create-default-account",
     157              :       merchant_url,
     158              :       merchant_payto,
     159              :       NULL, NULL,
     160              :       MHD_HTTP_OK),
     161              : 
     162              :     /**
     163              :      * Move money to the exchange's bank account.
     164              :      */
     165            1 :     cmd_transfer_to_exchange ("create-reserve-1",
     166              :                               "EUR:10.02"),
     167              :     /**
     168              :      * Make a reserve exist, according to the previous
     169              :      * transfer.
     170              :      */
     171            1 :     cmd_exec_wirewatch ("wirewatch-1"),
     172            1 :     TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-1",
     173              :                                        "create-reserve-1",
     174              :                                        "EUR:5",
     175              :                                        0,
     176              :                                        MHD_HTTP_OK),
     177            1 :     TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-2",
     178              :                                        "create-reserve-1",
     179              :                                        "EUR:5",
     180              :                                        0,
     181              :                                        MHD_HTTP_OK),
     182              :     /* Failed download: no backup exists */
     183            1 :     SYNC_TESTING_cmd_backup_nx ("backup-download-nx",
     184              :                                 sync_url),
     185              :     /* Failed upload: need to pay */
     186            1 :     SYNC_TESTING_cmd_backup_upload ("backup-upload-1",
     187              :                                     sync_url,
     188              :                                     NULL /* prev upload */,
     189              :                                     NULL /* last upload */,
     190              :                                     SYNC_TESTING_UO_NONE,
     191              :                                     MHD_HTTP_PAYMENT_REQUIRED,
     192              :                                     "Test-1",
     193              :                                     strlen ("Test-1")),
     194              :     /* what would we have to pay? */
     195            1 :     TALER_TESTING_cmd_merchant_claim_order ("fetch-proposal",
     196              :                                             merchant_url,
     197              :                                             MHD_HTTP_OK,
     198              :                                             "backup-upload-1",
     199              :                                             NULL),
     200              :     /* make the payment */
     201            1 :     TALER_TESTING_cmd_merchant_pay_order ("pay-account",
     202              :                                           merchant_url,
     203              :                                           MHD_HTTP_OK,
     204              :                                           "fetch-proposal",
     205              :                                           "withdraw-coin-1",
     206              :                                           "EUR:5",
     207              :                                           "EUR:4.99", /* must match ANNUAL_FEE in config! */
     208              :                                           "session-id"),
     209              :     /* now upload should succeed */
     210            1 :     SYNC_TESTING_cmd_backup_upload ("backup-upload-2",
     211              :                                     sync_url,
     212              :                                     "backup-upload-1",
     213              :                                     NULL,
     214              :                                     SYNC_TESTING_UO_NONE,
     215              :                                     MHD_HTTP_NO_CONTENT,
     216              :                                     "Test-1",
     217              :                                     strlen ("Test-1")),
     218              :     /* now updated upload should succeed */
     219            1 :     SYNC_TESTING_cmd_backup_upload ("backup-upload-3",
     220              :                                     sync_url,
     221              :                                     "backup-upload-2",
     222              :                                     NULL,
     223              :                                     SYNC_TESTING_UO_NONE,
     224              :                                     MHD_HTTP_NO_CONTENT,
     225              :                                     "Test-3",
     226              :                                     strlen ("Test-3")),
     227              :     /* Test download: succeeds! */
     228            1 :     SYNC_TESTING_cmd_backup_download ("download-3",
     229              :                                       sync_url,
     230              :                                       MHD_HTTP_OK,
     231              :                                       "backup-upload-3"),
     232              :     /* now updated upload should fail (conflict) */
     233            1 :     SYNC_TESTING_cmd_backup_upload ("backup-upload-3b",
     234              :                                     sync_url,
     235              :                                     "backup-upload-2",
     236              :                                     "backup-upload-3",
     237              :                                     SYNC_TESTING_UO_NONE,
     238              :                                     MHD_HTTP_CONFLICT,
     239              :                                     "Test-3b",
     240              :                                     strlen ("Test-3b")),
     241              :     /* now updated upload should fail (payment requested) */
     242            1 :     SYNC_TESTING_cmd_backup_upload ("backup-upload-4",
     243              :                                     sync_url,
     244              :                                     "backup-upload-3",
     245              :                                     "backup-upload-3",
     246              :                                     SYNC_TESTING_UO_REQUEST_PAYMENT,
     247              :                                     MHD_HTTP_PAYMENT_REQUIRED,
     248              :                                     "Test-4",
     249              :                                     strlen ("Test-4")),
     250              :     /* Test download: previous did NOT change the data on the server! */
     251            1 :     SYNC_TESTING_cmd_backup_download ("download-3b",
     252              :                                       sync_url,
     253              :                                       MHD_HTTP_OK,
     254              :                                       "backup-upload-3"),
     255              : 
     256            1 :     TALER_TESTING_cmd_end ()
     257              :   };
     258              : 
     259            1 :   TALER_TESTING_run (is,
     260              :                      commands);
     261            1 : }
     262              : 
     263              : 
     264              : int
     265            1 : main (int argc,
     266              :       char *const *argv)
     267              : {
     268              :   (void) argc;
     269            1 :   payer_payto.full_payto =
     270              :     (char *) "payto://x-taler-bank/localhost/" USER_ACCOUNT_NAME
     271              :     "?receiver-name=user";
     272            1 :   exchange_payto.full_payto =
     273              :     (char *) "payto://x-taler-bank/localhost/" EXCHANGE_ACCOUNT_NAME
     274              :     "?receiver-name=exchange";
     275            1 :   merchant_payto.full_payto =
     276              :     (char *) "payto://x-taler-bank/localhost/" MERCHANT_ACCOUNT_NAME
     277              :     "?receiver-name=merchant";
     278            1 :   return TALER_TESTING_main (argv,
     279              :                              "DEBUG",
     280              :                              CONFIG_FILE,
     281              :                              "exchange-account-exchange",
     282              :                              TALER_TESTING_BS_FAKEBANK,
     283              :                              &cred,
     284              :                              &run,
     285              :                              NULL);
     286              : }
     287              : 
     288              : 
     289              : /* end of test_sync_api.c */
        

Generated by: LCOV version 2.0-1