anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

test_anastasis.c (16364B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020-2023 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU 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 testing/test_anastasis.c
     18  * @brief testcase to test anastasis
     19  * @author Christian Grothoff
     20  * @author Dennis Neufeld
     21  * @author Dominik Meister
     22  */
     23 #include "platform.h"
     24 #include "anastasis_testing_lib.h"
     25 #include <taler/taler_merchant_testing_lib.h>
     26 
     27 
     28 /**
     29  * Configuration file we use.  One (big) configuration is used
     30  * for the various components for this test.
     31  */
     32 #define CONFIG_FILE "test_anastasis_api.conf"
     33 
     34 /**
     35  * Exchange base URL.  Could also be taken from config.
     36  */
     37 #define EXCHANGE_URL "http://localhost:8081/"
     38 
     39 /**
     40  * Account number of the exchange at the bank.
     41  */
     42 #define EXCHANGE_ACCOUNT_NAME "2"
     43 
     44 /**
     45  * Account number of some user.
     46  */
     47 #define USER_ACCOUNT_NAME "62"
     48 
     49 /**
     50  * Account number used by the merchant
     51  */
     52 #define MERCHANT_ACCOUNT_NAME "3"
     53 
     54 /**
     55  * Credentials for the test.
     56  */
     57 static struct TALER_TESTING_Credentials cred;
     58 
     59 /**
     60  * Payto URI of the customer (payer).
     61  */
     62 static struct TALER_FullPayto payer_payto;
     63 
     64 /**
     65  * Payto URI of the exchange (escrow account).
     66  */
     67 static struct TALER_FullPayto exchange_payto;
     68 
     69 /**
     70  * Payto URI of the merchant (receiver).
     71  */
     72 static struct TALER_FullPayto merchant_payto;
     73 
     74 /**
     75  * Merchant base URL.
     76  */
     77 static const char *merchant_url;
     78 
     79 /**
     80  * Anastasis base URL.
     81  */
     82 static char *anastasis_url;
     83 
     84 /**
     85  * Name of the file for exchanging the secret.
     86  */
     87 static char *file_secret;
     88 
     89 /**
     90  * Anastasis process.
     91  */
     92 static struct GNUNET_OS_Process *anastasisd;
     93 
     94 /**
     95  * Identity to use for testing.
     96  */
     97 static json_t *id_data;
     98 
     99 
    100 /**
    101  * Execute the taler-exchange-wirewatch command with
    102  * our configuration file.
    103  *
    104  * @param label label to use for the command.
    105  */
    106 static struct TALER_TESTING_Command
    107 cmd_exec_wirewatch (const char *label)
    108 {
    109   return TALER_TESTING_cmd_exec_wirewatch (label,
    110                                            CONFIG_FILE);
    111 }
    112 
    113 
    114 /**
    115  * Run wire transfer of funds from some user's account to the
    116  * exchange.
    117  *
    118  * @param label label to use for the command.
    119  * @param amount amount to transfer, i.e. "EUR:1"
    120  * @param url exchange_url
    121  */
    122 static struct TALER_TESTING_Command
    123 cmd_transfer_to_exchange (const char *label,
    124                           const char *amount)
    125 {
    126   return TALER_TESTING_cmd_admin_add_incoming (label,
    127                                                amount,
    128                                                &cred.ba,
    129                                                payer_payto);
    130 }
    131 
    132 
    133 /**
    134  * Main function that will tell the interpreter what commands to
    135  * run.
    136  *
    137  * @param cls closure
    138  */
    139 static void
    140 run (void *cls,
    141      struct TALER_TESTING_Interpreter *is)
    142 {
    143   struct TALER_TESTING_Command pay[] = {
    144     /**
    145      * Move money to the exchange's bank account.
    146      */
    147     cmd_transfer_to_exchange ("create-reserve-1",
    148                               "EUR:10.02"),
    149     /**
    150      * Make a reserve exist, according to the previous
    151      * transfer.
    152      */
    153     cmd_exec_wirewatch ("wirewatch-1"),
    154     TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-1",
    155                                        "create-reserve-1",
    156                                        "EUR:5",
    157                                        0, /* age */
    158                                        MHD_HTTP_OK),
    159     TALER_TESTING_cmd_withdraw_amount ("withdraw-coin-2",
    160                                        "create-reserve-1",
    161                                        "EUR:5",
    162                                        0, /* age */
    163                                        MHD_HTTP_OK),
    164     /**
    165      * Check the reserve is depleted.
    166      */
    167     TALER_TESTING_cmd_status ("withdraw-status-1",
    168                               "create-reserve-1",
    169                               "EUR:0",
    170                               MHD_HTTP_OK),
    171     TALER_TESTING_cmd_end ()
    172   };
    173 
    174   struct TALER_TESTING_Command anastasis[] = {
    175     ANASTASIS_TESTING_cmd_config ("salt-request-1",
    176                                   anastasis_url,
    177                                   MHD_HTTP_OK),
    178     ANASTASIS_TESTING_cmd_truth_upload_question ("truth-create-1",
    179                                                  anastasis_url,
    180                                                  id_data,
    181                                                  "answer the question",
    182                                                  "text/plain",
    183                                                  "SomeTruth1",
    184                                                  MHD_HTTP_NO_CONTENT,
    185                                                  ANASTASIS_TESTING_TSO_NONE,
    186                                                  "salt-request-1"),
    187     ANASTASIS_TESTING_cmd_truth_upload_question ("truth-create-2",
    188                                                  anastasis_url,
    189                                                  id_data,
    190                                                  "answer the question",
    191                                                  "text/plain",
    192                                                  "SomeTruth2",
    193                                                  MHD_HTTP_NO_CONTENT,
    194                                                  ANASTASIS_TESTING_TSO_NONE,
    195                                                  "salt-request-1"),
    196     ANASTASIS_TESTING_cmd_truth_upload ("truth-create-3",
    197                                         anastasis_url,
    198                                         id_data,
    199                                         "file",
    200                                         "read the file",
    201                                         "text/plain",
    202                                         file_secret,
    203                                         strlen (file_secret),
    204                                         MHD_HTTP_NO_CONTENT,
    205                                         ANASTASIS_TESTING_TSO_NONE,
    206                                         "salt-request-1"),
    207     ANASTASIS_TESTING_cmd_policy_create ("policy-create-1",
    208                                          "truth-create-1",
    209                                          "truth-create-2",
    210                                          NULL),
    211     ANASTASIS_TESTING_cmd_policy_create ("policy-create-2",
    212                                          "truth-create-1",
    213                                          "truth-create-3",
    214                                          NULL),
    215     ANASTASIS_TESTING_cmd_policy_create ("policy-create-3",
    216                                          "truth-create-2",
    217                                          "truth-create-3",
    218                                          NULL),
    219     ANASTASIS_TESTING_cmd_secret_share ("secret-share-1",
    220                                         anastasis_url,
    221                                         "salt-request-1",
    222                                         NULL,
    223                                         id_data,
    224                                         "core secret",
    225                                         strlen ("core secret"),
    226                                         ANASTASIS_SHARE_STATUS_PAYMENT_REQUIRED,
    227                                         ANASTASIS_TESTING_SSO_NONE,
    228                                         "policy-create-1",
    229                                         "policy-create-2",
    230                                         "policy-create-3",
    231                                         NULL),
    232     /* what would we have to pay? */
    233     TALER_TESTING_cmd_merchant_claim_order ("fetch-proposal",
    234                                             merchant_url,
    235                                             MHD_HTTP_OK,
    236                                             "secret-share-1",
    237                                             NULL),
    238     /* make the payment */
    239     TALER_TESTING_cmd_merchant_pay_order ("pay-account",
    240                                           merchant_url,
    241                                           MHD_HTTP_OK,
    242                                           "fetch-proposal",
    243                                           "withdraw-coin-1",
    244                                           "EUR:5",
    245                                           "EUR:4.99", /* must match ANNUAL_FEE in config! */
    246                                           NULL),
    247     ANASTASIS_TESTING_cmd_secret_share ("secret-share-2",
    248                                         anastasis_url,
    249                                         "salt-request-1",
    250                                         "secret-share-1",
    251                                         id_data,
    252                                         "core secret",
    253                                         strlen ("core secret"),
    254                                         ANASTASIS_SHARE_STATUS_SUCCESS,
    255                                         ANASTASIS_TESTING_SSO_NONE,
    256                                         "policy-create-1",
    257                                         "policy-create-2",
    258                                         "policy-create-3",
    259                                         NULL),
    260     ANASTASIS_TESTING_cmd_recover_secret ("recover-secret-1",
    261                                           anastasis_url,
    262                                           id_data,
    263                                           0, /* version */
    264                                           ANASTASIS_TESTING_RSO_NONE,
    265                                           "salt-request-1",
    266                                           "secret-share-2"),
    267     ANASTASIS_TESTING_cmd_challenge_answer ("challenge-answer-1",
    268                                             NULL, /* payment ref */
    269                                             "recover-secret-1", /* challenge ref */
    270                                             0, /* challenge index */
    271                                             "SomeTruth1",
    272                                             0,  /* mode */
    273                                             ANASTASIS_CHALLENGE_ANSWER_STATUS_SOLVED),
    274 #if 0
    275     ANASTASIS_TESTING_cmd_challenge_answer ("challenge-answer-2",
    276                                             NULL, /* payment ref */
    277                                             "recover-secret-1",
    278                                             1, /* challenge index */
    279                                             "SomeTruth2",
    280                                             0, /* mode */
    281                                             ANASTASIS_CHALLENGE_ANSWER_STATUS_SOLVED),
    282 #endif
    283     ANASTASIS_TESTING_cmd_challenge_start ("challenge-start-3-pay",
    284                                            NULL,  /* payment ref */
    285                                            "recover-secret-1",
    286                                            2,  /* challenge index */
    287                                            ANASTASIS_CHALLENGE_START_STATUS_PAYMENT_REQUIRED),
    288     TALER_TESTING_cmd_merchant_claim_order ("fetch-challenge-pay-proposal",
    289                                             merchant_url,
    290                                             MHD_HTTP_OK,
    291                                             "challenge-start-3-pay",
    292                                             NULL),
    293     TALER_TESTING_cmd_merchant_pay_order ("pay-file-challenge",
    294                                           merchant_url,
    295                                           MHD_HTTP_OK,
    296                                           "fetch-challenge-pay-proposal",
    297                                           "withdraw-coin-2",
    298                                           "EUR:1",
    299                                           "EUR:1", /* must match COST in config! */
    300                                           NULL),
    301     ANASTASIS_TESTING_cmd_challenge_start ("challenge-start-3-paid",
    302                                            "challenge-start-3-pay",  /* payment ref */
    303                                            "recover-secret-1",
    304                                            2,  /* challenge index */
    305                                            ANASTASIS_CHALLENGE_START_STATUS_FILENAME_PROVIDED),
    306     ANASTASIS_TESTING_cmd_challenge_answer ("challenge-answer-3",
    307                                             "challenge-start-3-pay", /* payment ref */
    308                                             "recover-secret-1",
    309                                             2, /* challenge index */
    310                                             "challenge-start-3-paid", /* answer */
    311                                             1, /* mode */
    312                                             ANASTASIS_CHALLENGE_ANSWER_STATUS_SOLVED),
    313     ANASTASIS_TESTING_cmd_recover_secret_finish ("recover-finish-1",
    314                                                  "recover-secret-1",
    315                                                  GNUNET_TIME_UNIT_SECONDS),
    316     TALER_TESTING_cmd_end ()
    317   };
    318 
    319   struct TALER_TESTING_Command commands[] = {
    320     /* general setup */
    321     TALER_TESTING_cmd_run_fakebank ("run-fakebank",
    322                                     cred.cfg,
    323                                     "exchange-account-exchange"),
    324     TALER_TESTING_cmd_system_start ("start-taler",
    325                                     CONFIG_FILE,
    326                                     "-em",
    327                                     "-u", "exchange-account-exchange",
    328                                     NULL),
    329     TALER_TESTING_cmd_get_exchange ("get-exchange",
    330                                     cred.cfg,
    331                                     NULL,
    332                                     true,
    333                                     true),
    334     TALER_TESTING_cmd_merchant_post_instances ("instance-create-admin",
    335                                                merchant_url,
    336                                                "admin",
    337                                                MHD_HTTP_NO_CONTENT),
    338     TALER_TESTING_cmd_merchant_post_account (
    339       "instance-create-default-account",
    340       merchant_url,
    341       merchant_payto,
    342       NULL, NULL,
    343       MHD_HTTP_OK),
    344     TALER_TESTING_cmd_batch ("pay",
    345                              pay),
    346     TALER_TESTING_cmd_batch ("anastasis",
    347                              anastasis),
    348     TALER_TESTING_cmd_end ()
    349   };
    350 
    351   TALER_TESTING_run (is,
    352                      commands);
    353 }
    354 
    355 
    356 int
    357 main (int argc,
    358       char *const *argv)
    359 {
    360   int ret;
    361   {
    362     char dir[] = "/tmp/test-anastasis-file-XXXXXX";
    363 
    364     if (NULL == mkdtemp (dir))
    365     {
    366       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
    367                                 "mkdtemp",
    368                                 dir);
    369       return 77;
    370     }
    371     GNUNET_asprintf (&file_secret,
    372                      "%s/.secret",
    373                      dir);
    374   }
    375   id_data = ANASTASIS_TESTING_make_id_data_example ("MaxMuster123456789");
    376   payer_payto.full_payto =
    377     (char *) "payto://x-taler-bank/localhost/" USER_ACCOUNT_NAME
    378     "?receiver-name=62";
    379   exchange_payto.full_payto =
    380     (char *) "payto://x-taler-bank/localhost/" EXCHANGE_ACCOUNT_NAME
    381     "?receiver-name=exchange";
    382   merchant_payto.full_payto =
    383     (char *) "payto://x-taler-bank/localhost/" MERCHANT_ACCOUNT_NAME
    384     "?receiver-name=merchant";
    385   merchant_url = "http://localhost:8080/";
    386 
    387   if (NULL ==
    388       (anastasis_url = ANASTASIS_TESTING_prepare_anastasis (CONFIG_FILE)))
    389     return 77;
    390   if (NULL == (anastasisd =
    391                  ANASTASIS_TESTING_run_anastasis (CONFIG_FILE,
    392                                                   anastasis_url)))
    393   {
    394     GNUNET_break (0);
    395     return 1;
    396   }
    397   ret = TALER_TESTING_main (argv,
    398                             "INFO",
    399                             CONFIG_FILE,
    400                             "exchange-account-exchange",
    401                             TALER_TESTING_BS_FAKEBANK,
    402                             &cred,
    403                             &run,
    404                             NULL);
    405   GNUNET_OS_process_kill (anastasisd,
    406                           SIGTERM);
    407   GNUNET_OS_process_wait (anastasisd);
    408   GNUNET_OS_process_destroy (anastasisd);
    409   GNUNET_free (anastasis_url);
    410   return ret;
    411 }
    412 
    413 
    414 /* end of test_anastasis.c */