anastasis

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

anastasis_testing_lib.h (21433B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020 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 include/anastasis_testing_lib.h
     18  * @brief API for writing an interpreter to test Taler components
     19  * @author Christian Grothoff <christian@grothoff.org>
     20  * @author Dennis Neufeld
     21  * @author Dominik Meister
     22  */
     23 #ifndef ANASTASIS_TESTING_LIB_H
     24 #define ANASTASIS_TESTING_LIB_H
     25 
     26 #include "anastasis.h"
     27 #include <taler/taler_testing_lib.h>
     28 #include <microhttpd.h>
     29 
     30 /* ********************* Helper functions ********************* */
     31 
     32 #define ANASTASIS_FAIL() \
     33   do {GNUNET_break (0); return NULL; } while (0)
     34 
     35 
     36 /**
     37  * Create headers for a trait with name @a name for
     38  * statically allocated data of type @a type.
     39  */
     40 #define ANASTASIS_TESTING_MAKE_DECL_SIMPLE_TRAIT(name,type)   \
     41   enum GNUNET_GenericReturnValue                          \
     42     ANASTASIS_TESTING_get_trait_ ## name (                    \
     43     const struct TALER_TESTING_Command *cmd,              \
     44     type **ret);                                          \
     45   struct TALER_TESTING_Trait                              \
     46     ANASTASIS_TESTING_make_trait_ ## name (                   \
     47     type * value);
     48 
     49 
     50 /**
     51  * Create C implementation for a trait with name @a name for statically
     52  * allocated data of type @a type.
     53  */
     54 #define ANASTASIS_TESTING_MAKE_IMPL_SIMPLE_TRAIT(name,type)  \
     55   enum GNUNET_GenericReturnValue                         \
     56     ANASTASIS_TESTING_get_trait_ ## name (                   \
     57     const struct TALER_TESTING_Command *cmd,             \
     58     type **ret)                                          \
     59   {                                                      \
     60     if (NULL == cmd->traits) return GNUNET_SYSERR;       \
     61     return cmd->traits (cmd->cls,                        \
     62                         (const void **) ret,             \
     63                         TALER_S (name),                  \
     64                         0);                              \
     65   }                                                      \
     66   struct TALER_TESTING_Trait                             \
     67     ANASTASIS_TESTING_make_trait_ ## name (                  \
     68     type * value)                                        \
     69   {                                                      \
     70     struct TALER_TESTING_Trait ret = {                   \
     71       .trait_name = TALER_S (name),                      \
     72       .ptr = (const void *) value                        \
     73     };                                                   \
     74     return ret;                                          \
     75   }
     76 
     77 
     78 /**
     79  * Create headers for a trait with name @a name for
     80  * statically allocated data of type @a type.
     81  */
     82 #define ANASTASIS_TESTING_MAKE_DECL_INDEXED_TRAIT(name,type)  \
     83   enum GNUNET_GenericReturnValue                          \
     84     ANASTASIS_TESTING_get_trait_ ## name (                    \
     85     const struct TALER_TESTING_Command *cmd,              \
     86     unsigned int index,                                   \
     87     type **ret);                                          \
     88   struct TALER_TESTING_Trait                              \
     89     ANASTASIS_TESTING_make_trait_ ## name (                   \
     90     unsigned int index,                                   \
     91     type * value);
     92 
     93 
     94 /**
     95  * Create C implementation for a trait with name @a name for statically
     96  * allocated data of type @a type.
     97  */
     98 #define ANASTASIS_TESTING_MAKE_IMPL_INDEXED_TRAIT(name,type) \
     99   enum GNUNET_GenericReturnValue                         \
    100     ANASTASIS_TESTING_get_trait_ ## name (                   \
    101     const struct TALER_TESTING_Command *cmd,             \
    102     unsigned int index,                                  \
    103     type **ret)                                          \
    104   {                                                      \
    105     if (NULL == cmd->traits) return GNUNET_SYSERR;       \
    106     return cmd->traits (cmd->cls,                        \
    107                         (const void **) ret,             \
    108                         TALER_S (name),                  \
    109                         index);                          \
    110   }                                                      \
    111   struct TALER_TESTING_Trait                             \
    112     ANASTASIS_TESTING_make_trait_ ## name (                  \
    113     unsigned int index,                                  \
    114     type * value)                                        \
    115   {                                                      \
    116     struct TALER_TESTING_Trait ret = {                   \
    117       .index = index,                                    \
    118       .trait_name = TALER_S (name),                      \
    119       .ptr = (const void *) value                        \
    120     };                                                   \
    121     return ret;                                          \
    122   }
    123 
    124 
    125 /**
    126  * Call #op on all simple traits.
    127  */
    128 #define ANASTASIS_TESTING_SIMPLE_TRAITS(op) \
    129   op (hash, const struct GNUNET_HashCode)  \
    130   op (truth, const struct ANASTASIS_Truth *)  \
    131   op (policy, const struct ANASTASIS_Policy *)  \
    132   op (provider_salt, const struct ANASTASIS_CRYPTO_ProviderSaltP)  \
    133   op (core_secret, const void)  \
    134   op (truth_key, const struct ANASTASIS_CRYPTO_TruthKeyP)  \
    135   op (account_pub, const struct ANASTASIS_CRYPTO_AccountPublicKeyP)  \
    136   op (account_priv, const struct ANASTASIS_CRYPTO_AccountPrivateKeyP)  \
    137   op (payment_secret, const struct ANASTASIS_PaymentSecretP)  \
    138   op (truth_uuid, const struct ANASTASIS_CRYPTO_TruthUUIDP)  \
    139   op (eks, const struct ANASTASIS_CRYPTO_EncryptedKeyShareP)  \
    140   op (code, const char) \
    141   op (filename, const char)
    142 
    143 
    144 /**
    145  * Call #op on all indexed traits.
    146  */
    147 #define ANASTASIS_TESTING_INDEXED_TRAITS(op)                         \
    148   op (challenges, const struct ANASTASIS_Challenge *)
    149 
    150 
    151 ANASTASIS_TESTING_SIMPLE_TRAITS (ANASTASIS_TESTING_MAKE_DECL_SIMPLE_TRAIT)
    152 
    153 ANASTASIS_TESTING_INDEXED_TRAITS (ANASTASIS_TESTING_MAKE_DECL_INDEXED_TRAIT)
    154 
    155 
    156 /**
    157  * Prepare the merchant execution.  Create tables and check if
    158  * the port is available.
    159  *
    160  * @param config_filename configuration filename.
    161  *
    162  * @return the base url, or NULL upon errors.  Must be freed
    163  *         by the caller.
    164  */
    165 char *
    166 TALER_TESTING_prepare_merchant (const char *config_filename);
    167 
    168 
    169 /**
    170  * Start the merchant backend process.  Assume the port
    171  * is available and the database is clean.  Use the "prepare
    172  * merchant" function to do such tasks.
    173  *
    174  * @param config_filename configuration filename.
    175  *
    176  * @return the process, or NULL if the process could not
    177  *         be started.
    178  */
    179 struct GNUNET_OS_Process *
    180 TALER_TESTING_run_merchant (const char *config_filename,
    181                             const char *merchant_url);
    182 
    183 
    184 /**
    185  * Start the anastasis backend process.  Assume the port
    186  * is available and the database is clean.  Use the "prepare
    187  * anastasis" function to do such tasks.
    188  *
    189  * @param config_filename configuration filename.
    190  * @param anastasis_url URL to use to confirm service running
    191  * @return the process, or NULL if the process could not
    192  *         be started.
    193  */
    194 struct GNUNET_OS_Process *
    195 ANASTASIS_TESTING_run_anastasis (const char *config_filename,
    196                                  const char *anastasis_url);
    197 
    198 
    199 /**
    200  * Prepare the anastasis execution.  Create tables and check if
    201  * the port is available.
    202  *
    203  * @param config_filename configuration filename.
    204  *
    205  * @return the base url, or NULL upon errors.  Must be freed
    206  *         by the caller.
    207  */
    208 char *
    209 ANASTASIS_TESTING_prepare_anastasis (const char *config_filename);
    210 
    211 
    212 /* ************** Specific interpreter commands ************ */
    213 
    214 
    215 /**
    216  * Types of options for performing the upload. Used as a bitmask.
    217  */
    218 enum ANASTASIS_TESTING_PolicyStoreOption
    219 {
    220   /**
    221    * Do everything by the book.
    222    */
    223   ANASTASIS_TESTING_PSO_NONE = 0,
    224 
    225   /**
    226    * Use random hash for previous upload instead of correct
    227    * previous hash.
    228    */
    229   ANASTASIS_TESTING_PSO_PREV_HASH_WRONG = 1,
    230 
    231   /**
    232    * Request payment.
    233    */
    234   ANASTASIS_TESTING_PSO_REQUEST_PAYMENT = 2,
    235 
    236   /**
    237    * Reference payment order ID from linked previous upload.
    238    */
    239   ANASTASIS_TESTING_PSO_REFERENCE_ORDER_ID = 4
    240 
    241 };
    242 
    243 
    244 /**
    245  * Make a "policy store" command.
    246  *
    247  * @param label command label
    248  * @param anastasis_url base URL of the anastasis serving
    249  *        the policy store request.
    250  * @param prev_upload reference to a previous upload we are
    251  *        supposed to update, NULL for none
    252  * @param http_status expected HTTP status.
    253  * @param pso policy store options
    254  * @param recovery_data recovery data to post
    255  * @param recovery_data_size size of recovery/policy data
    256  * @return the command
    257  */
    258 struct TALER_TESTING_Command
    259 ANASTASIS_TESTING_cmd_policy_store (
    260   const char *label,
    261   const char *anastasis_url,
    262   const char *prev_upload,
    263   unsigned int http_status,
    264   enum ANASTASIS_TESTING_PolicyStoreOption pso,
    265   const void *recovery_data,
    266   size_t recovery_data_size);
    267 
    268 
    269 /**
    270  * Make the "policy lookup" command.
    271  *
    272  * @param label command label
    273  * @param anastasis_url base URL of the ANASTASIS serving
    274  *        the policy store request.
    275  * @param http_status expected HTTP status.
    276  * @param upload_ref reference to upload command
    277  * @return the command
    278  */
    279 struct TALER_TESTING_Command
    280 ANASTASIS_TESTING_cmd_policy_lookup (const char *label,
    281                                      const char *anastasis_url,
    282                                      unsigned int http_status,
    283                                      const char *upload_ref);
    284 
    285 
    286 /**
    287  * Make the "policy lookup" command for a non-existent upload.
    288  *
    289  * @param label command label
    290  * @param anastasis_url base URL of the ANASTASIS serving
    291  *        the policy lookup request.
    292  * @return the command
    293  */
    294 struct TALER_TESTING_Command
    295 ANASTASIS_TESTING_cmd_policy_nx (const char *label,
    296                                  const char *anastasis_url);
    297 
    298 
    299 /**
    300  * Types of options for performing the upload. Used as a bitmask.
    301  */
    302 enum ANASTASIS_TESTING_TruthStoreOption
    303 {
    304   /**
    305    * Do everything by the book.
    306    */
    307   ANASTASIS_TESTING_TSO_NONE = 0,
    308 
    309   /**
    310    * Re-use UUID of previous upload instead of creating a random one.
    311    */
    312   ANASTASIS_TESTING_TSO_REFERENCE_UUID = 1,
    313 
    314   /**
    315    * Explicitly request payment.
    316    */
    317   ANASTASIS_TESTING_TSO_REQUEST_PAYMENT = 2,
    318 
    319   /**
    320    * Reference payment order ID from linked previous upload.
    321    */
    322   ANASTASIS_TESTING_TSO_REFERENCE_ORDER_ID = 4
    323 
    324 };
    325 
    326 
    327 /**
    328  * Make the "truth store" command.
    329  *
    330  * @param label command label
    331  * @param anastasis_url base URL of the anastasis serving
    332  *        the truth store request.
    333  * @param prev_upload reference to a previous upload to get a payment ID from
    334  * @param method what authentication method is being used
    335  * @param mime_type MIME type of @a truth_data
    336  * @param truth_data_size number of bytes in @a truth_data
    337  * @param truth_data recovery data to post /truth (in plaintext)
    338  * @param tso flags
    339  * @param http_status expected HTTP status.
    340  * @return the command
    341  */
    342 struct TALER_TESTING_Command
    343 ANASTASIS_TESTING_cmd_truth_store (const char *label,
    344                                    const char *anastasis_url,
    345                                    const char *prev_upload,
    346                                    const char *method,
    347                                    const char *mime_type,
    348                                    size_t truth_data_size,
    349                                    const void *truth_data,
    350                                    enum ANASTASIS_TESTING_TruthStoreOption tso,
    351                                    unsigned int http_status);
    352 
    353 
    354 /**
    355  * Make the "truth store" command for a secure question.
    356  *
    357  * @param label command label
    358  * @param anastasis_url base URL of the anastasis serving
    359  *        the truth store request.
    360  * @param prev_upload reference to a previous upload to get a payment ID from
    361  * @param answer the answer to the question
    362  * @param tso flags
    363  * @param http_status expected HTTP status.
    364  * @return the command
    365  */
    366 struct TALER_TESTING_Command
    367 ANASTASIS_TESTING_cmd_truth_question (
    368   const char *label,
    369   const char *anastasis_url,
    370   const char *prev_upload,
    371   const char *answer,
    372   enum ANASTASIS_TESTING_TruthStoreOption tso,
    373   unsigned int http_status);
    374 
    375 
    376 /**
    377  * Make a "truth challenge" command.
    378  *
    379  * @param label command label
    380  * @param anastasis_url base URL of the ANASTASIS serving
    381  *        the keyshare lookup request.
    382  * @param answer (response to challenge)
    383  * @param payment_ref reference to the payment request
    384  * @param upload_ref reference to upload command
    385  * @param http_status expected HTTP status
    386  * @return the command
    387  */
    388 struct TALER_TESTING_Command
    389 ANASTASIS_TESTING_cmd_truth_challenge (
    390   const char *label,
    391   const char *anastasis_url,
    392   const char *payment_ref,
    393   const char *upload_ref,
    394   unsigned int http_status);
    395 
    396 
    397 /**
    398  * Make a "truth solve" command.
    399  *
    400  * @param label command label
    401  * @param anastasis_url base URL of the ANASTASIS serving
    402  *        the keyshare lookup request.
    403  * @param answer (response to challenge)
    404  * @param payment_ref reference to the payment request
    405  * @param upload_ref reference to upload command
    406  * @param lookup_mode 0 for security question, 1 for
    407  *          code-based
    408  * @param http_status expected HTTP status
    409  * @return the command
    410  */
    411 struct TALER_TESTING_Command
    412 ANASTASIS_TESTING_cmd_truth_solve (
    413   const char *label,
    414   const char *anastasis_url,
    415   const char *answer,
    416   const char *payment_ref,
    417   const char *upload_ref,
    418   int lookup_mode,
    419   unsigned int http_status);
    420 
    421 
    422 /**
    423  * Make the "/config" command.
    424  *
    425  * @param label command label
    426  * @param anastasis_url base URL of the ANASTASIS serving
    427  *        the /config request.
    428  * @param http_status expected HTTP status.
    429  * @return the command
    430  */
    431 struct TALER_TESTING_Command
    432 ANASTASIS_TESTING_cmd_config (const char *label,
    433                               const char *anastasis_url,
    434                               unsigned int http_status);
    435 
    436 /* ********************* test truth upload ********************* */
    437 
    438 /**
    439  * Creates a sample of id_data.
    440  *
    441  * @param id_data some sample data (e.g. AHV, name, surname, ...)
    442  * @return truth in json format
    443  */
    444 json_t *
    445 ANASTASIS_TESTING_make_id_data_example (const char *id_data);
    446 
    447 
    448 /**
    449  * Make the "truth upload" command.
    450  *
    451  * @param label command label
    452  * @param anastasis_url base URL of the anastasis serving our requests.
    453  * @param id_data ID data to generate user identifier
    454  * @param method specifies escrow method
    455  * @param instructions specifies what the client/user has to do
    456  * @param mime_type mime type of truth_data
    457  * @param truth_data some truth data (e.g. hash of answer to a secret question)
    458  * @param truth_data_size size of truth_data
    459  * @param http_status expected HTTP status
    460  * @param tso truth upload options
    461  * @param upload_ref reference to the previous upload
    462  * @return the command
    463  */
    464 struct TALER_TESTING_Command
    465 ANASTASIS_TESTING_cmd_truth_upload (
    466   const char *label,
    467   const char *anastasis_url,
    468   const json_t *id_data,
    469   const char *method,
    470   const char *instructions,
    471   const char *mime_type,
    472   const void *truth_data,
    473   size_t truth_data_size,
    474   unsigned int http_status,
    475   enum ANASTASIS_TESTING_TruthStoreOption tso,
    476   const char *upload_ref);
    477 
    478 
    479 /**
    480  * Make the "truth upload" command for a security question.
    481  *
    482  * @param label command label
    483  * @param anastasis_url base URL of the anastasis serving our requests.
    484  * @param id_data ID data to generate user identifier
    485  * @param instructions specifies what the client/user has to do
    486  * @param mime_type mime type of truth_data
    487  * @param answer the answer to the security question
    488  * @param http_status expected HTTP status
    489  * @param tso truth upload options
    490  * @param salt_ref reference to command downloading provider salt
    491  * @return the command
    492  */
    493 struct TALER_TESTING_Command
    494 ANASTASIS_TESTING_cmd_truth_upload_question (
    495   const char *label,
    496   const char *anastasis_url,
    497   const json_t *id_data,
    498   const char *instructions,
    499   const char *mime_type,
    500   const void *answer,
    501   unsigned int http_status,
    502   enum ANASTASIS_TESTING_TruthStoreOption tso,
    503   const char *salt_ref);
    504 
    505 /* ********************* test policy create ********************* */
    506 
    507 
    508 /**
    509  * Make the "policy create" command.
    510  *
    511  * @param label command label
    512  * @param ... NULL-terminated list of truth upload commands
    513  * @return the command
    514  */
    515 struct TALER_TESTING_Command
    516 ANASTASIS_TESTING_cmd_policy_create (const char *label,
    517                                      ...);
    518 
    519 
    520 /* ********************* test secret share ********************* */
    521 
    522 
    523 /**
    524  * Types of options for performing the secret sharing. Used as a bitmask.
    525  */
    526 enum ANASTASIS_TESTING_SecretShareOption
    527 {
    528   /**
    529    * Do everything by the book.
    530    */
    531   ANASTASIS_TESTING_SSO_NONE = 0,
    532 
    533   /**
    534    * Request payment.
    535    */
    536   ANASTASIS_TESTING_SSO_REQUEST_PAYMENT = 2,
    537 
    538   /**
    539    * Reference payment order ID from linked previous upload.
    540    */
    541   ANASTASIS_TESTING_SSO_REFERENCE_ORDER_ID = 4
    542 
    543 };
    544 
    545 /**
    546  * Make the "secret share" command.
    547  *
    548  * @param label command label
    549  * @param anastasis_url base URL of the anastasis serving our requests.
    550  * @param config_ref reference to /config operation for @a anastasis_url
    551  * @param prev_secret_share reference to a previous secret share command
    552  * @param id_data ID data to generate user identifier
    553  * @param core_secret core secret to backup/recover
    554  * @param core_secret_size size of @a core_secret
    555  * @param want_status expected status.
    556  * @param sso secret share options
    557  * @param ... NULL-terminated list of policy create commands
    558  * @return the command
    559  */
    560 struct TALER_TESTING_Command
    561 ANASTASIS_TESTING_cmd_secret_share (
    562   const char *label,
    563   const char *anastasis_url,
    564   const char *config_ref,
    565   const char *prev_secret_share,
    566   const json_t *id_data,
    567   const void *core_secret,
    568   size_t core_secret_size,
    569   enum ANASTASIS_ShareStatus want_status,
    570   enum ANASTASIS_TESTING_SecretShareOption sso,
    571   ...);
    572 
    573 
    574 /* ********************* test recover secret ********************* */
    575 
    576 /**
    577  * Types of options for performing the secret recovery. Used as a bitmask.
    578  */
    579 enum ANASTASIS_TESTING_RecoverSecretOption
    580 {
    581   /**
    582    * Do everything by the book.
    583    */
    584   ANASTASIS_TESTING_RSO_NONE = 0,
    585 
    586   /**
    587    * Request payment.
    588    */
    589   ANASTASIS_TESTING_RSO_REQUEST_PAYMENT = 2,
    590 
    591   /**
    592    * Reference payment order ID from linked previous download.
    593    */
    594   ANASTASIS_TESTING_RSO_REFERENCE_ORDER_ID = 4
    595 
    596 };
    597 
    598 
    599 /**
    600  * Make the "recover secret" command.
    601  *
    602  * @param label command label
    603  * @param anastasis_url base URL of the anastasis serving our requests.
    604  * @param id_data identfication data from the user
    605  * @param version of the recovery document to download
    606  * @param rso recover secret options
    607  * @param download_ref salt download reference
    608  * @param core_secret_ref reference to core secret
    609  *         we expect to recover
    610  * @return the command
    611  */
    612 struct TALER_TESTING_Command
    613 ANASTASIS_TESTING_cmd_recover_secret (
    614   const char *label,
    615   const char *anastasis_url,
    616   const json_t *id_data,
    617   unsigned int version,
    618   enum ANASTASIS_TESTING_RecoverSecretOption rso,
    619   const char *download_ref,
    620   const char *core_secret_ref);
    621 
    622 
    623 /**
    624  * Make "recover secret finish" command.
    625  *
    626  * @param label command label
    627  * @param recover_label label of a "recover secret" command to wait for
    628  * @param timeout how long to wait at most
    629  * @return the command
    630  */
    631 struct TALER_TESTING_Command
    632 ANASTASIS_TESTING_cmd_recover_secret_finish (
    633   const char *label,
    634   const char *recover_label,
    635   struct GNUNET_TIME_Relative timeout);
    636 
    637 
    638 /* ********************* test challenge answer ********************* */
    639 
    640 /**
    641  * Create a "challenge start" command. Suitable for the "file"
    642  * authorization plugin.
    643  *
    644  * @param label command label
    645  * @param payment_ref reference to payment made for this challenge
    646  * @param challenge_ref reference to the recovery process
    647  * @param challenge_index defines the index of the trait to solve
    648  * @param expected_cs expected reply type
    649  * @return the command
    650  */
    651 struct TALER_TESTING_Command
    652 ANASTASIS_TESTING_cmd_challenge_start (
    653   const char *label,
    654   const char *payment_ref,
    655   const char *challenge_ref,
    656   unsigned int challenge_index,
    657   enum ANASTASIS_ChallengeStartStatus expected_cs);
    658 
    659 
    660 /**
    661  * Make the "challenge answer" command.
    662  *
    663  * @param label command label
    664  * @param payment_ref reference to payment made for this challenge
    665  * @param challenge_ref reference to the recovery process
    666  * @param challenge_index defines the index of the trait to solve
    667  * @param answer to the challenge
    668  * @param mode 0 for no plugin needed (security question)
    669  *             1 for plugin needed to authenticate
    670  * @param expected_cs expected reply type
    671  * @return the command
    672  */
    673 struct TALER_TESTING_Command
    674 ANASTASIS_TESTING_cmd_challenge_answer (
    675   const char *label,
    676   const char *payment_ref,
    677   const char *challenge_ref,
    678   unsigned int challenge_index,
    679   const char *answer,
    680   unsigned int mode,
    681   enum ANASTASIS_ChallengeAnswerStatus expected_cs);
    682 
    683 
    684 #endif