sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

test_sync_db.c (10747B)


      1 /*
      2   This file is part of
      3   (C) 2019 Taler Systems SA
      4 
      5   TALER 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   TALER 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   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file sync/test_sync_db.c
     18  * @brief testcase for sync postgres db plugin
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <gnunet/gnunet_util_lib.h>
     23 #include <taler/taler_util.h>
     24 #include "sync_service.h"
     25 #include "sync_database_plugin.h"
     26 #include "sync_database_lib.h"
     27 #include "sync_util.h"
     28 
     29 
     30 #define FAILIF(cond)                            \
     31         do {                                          \
     32           if (! (cond)) { break;}                       \
     33           GNUNET_break (0);                           \
     34           goto drop;                                     \
     35         } while (0)
     36 
     37 #define RND_BLK(ptr)                                                    \
     38         GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (* \
     39                                                                              ptr))
     40 
     41 /**
     42  * Global return value for the test.  Initially -1, set to 0 upon
     43  * completion.   Other values indicate some kind of error.
     44  */
     45 static int result;
     46 
     47 /**
     48  * Handle to the plugin we are testing.
     49  */
     50 static struct SYNC_DatabasePlugin *plugin;
     51 
     52 
     53 /**
     54  * Function called on all pending payments for an account.
     55  *
     56  * @param cls closure
     57  * @param timestamp for how long have we been waiting
     58  * @param order_id order id in the backend
     59  * @param token claim token, or NULL for none
     60  * @param amount how much is the order for
     61  */
     62 static void
     63 payment_it (void *cls,
     64             struct GNUNET_TIME_Timestamp timestamp,
     65             const char *order_id,
     66             const struct TALER_ClaimTokenP *token,
     67             const struct TALER_Amount *amount)
     68 {
     69   GNUNET_assert (NULL == cls);
     70   GNUNET_assert (0 == strcmp (order_id,
     71                               "fake-order-2"));
     72 }
     73 
     74 
     75 /**
     76  * Main function that will be run by the scheduler.
     77  *
     78  * @param cls closure with config
     79  */
     80 static void
     81 run (void *cls)
     82 {
     83   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
     84   struct TALER_Amount amount;
     85   struct SYNC_AccountPublicKeyP account_pub;
     86   struct SYNC_AccountSignatureP account_sig;
     87   struct SYNC_AccountSignatureP account_sig2;
     88   struct GNUNET_HashCode h;
     89   struct GNUNET_HashCode h2;
     90   struct GNUNET_HashCode h3;
     91   struct GNUNET_HashCode r;
     92   struct GNUNET_HashCode r2;
     93   struct GNUNET_TIME_Absolute ts;
     94   struct TALER_ClaimTokenP token;
     95   size_t bs;
     96   void *b = NULL;
     97 
     98   if (NULL == (plugin = SYNC_DB_plugin_load (cfg,
     99                                              true)))
    100   {
    101     result = 77;
    102     return;
    103   }
    104   if (GNUNET_OK !=
    105       plugin->drop_tables (plugin->cls))
    106   {
    107     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    108                 "Dropping tables failed\n");
    109   }
    110   if (GNUNET_OK !=
    111       plugin->create_tables (plugin->cls))
    112   {
    113     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    114                 "Creating tables failed\n");
    115   }
    116   GNUNET_assert (GNUNET_OK ==
    117                  plugin->preflight (plugin->cls));
    118   memset (&account_pub, 1, sizeof (account_pub));
    119   memset (&account_sig, 2, sizeof (account_sig));
    120   memset (&token, 3, sizeof (token));
    121   GNUNET_CRYPTO_hash ("data", 4, &h);
    122   GNUNET_CRYPTO_hash ("DATA", 4, &h2);
    123   GNUNET_CRYPTO_hash ("ATAD", 4, &h3);
    124   GNUNET_assert (GNUNET_OK ==
    125                  TALER_string_to_amount ("EUR:1",
    126                                          &amount));
    127   FAILIF (SYNC_DB_ONE_RESULT !=
    128           plugin->store_payment_TR (plugin->cls,
    129                                     &account_pub,
    130                                     "fake-order",
    131                                     &token,
    132                                     &amount));
    133   FAILIF (SYNC_DB_ONE_RESULT !=
    134           plugin->increment_lifetime_TR (plugin->cls,
    135                                          &account_pub,
    136                                          "fake-order",
    137                                          GNUNET_TIME_UNIT_MINUTES));
    138   FAILIF (SYNC_DB_ONE_RESULT !=
    139           plugin->store_backup_TR (plugin->cls,
    140                                    &account_pub,
    141                                    &account_sig,
    142                                    &h,
    143                                    4,
    144                                    "data"));
    145   FAILIF (SYNC_DB_NO_RESULTS !=
    146           plugin->store_backup_TR (plugin->cls,
    147                                    &account_pub,
    148                                    &account_sig,
    149                                    &h,
    150                                    4,
    151                                    "data"));
    152   FAILIF (SYNC_DB_ONE_RESULT !=
    153           plugin->update_backup_TR (plugin->cls,
    154                                     &account_pub,
    155                                     &h,
    156                                     &account_sig,
    157                                     &h2,
    158                                     4,
    159                                     "DATA"));
    160   FAILIF (SYNC_DB_OLD_BACKUP_MISMATCH !=
    161           plugin->update_backup_TR (plugin->cls,
    162                                     &account_pub,
    163                                     &h,
    164                                     &account_sig,
    165                                     &h3,
    166                                     4,
    167                                     "ATAD"));
    168   FAILIF (SYNC_DB_NO_RESULTS !=
    169           plugin->update_backup_TR (plugin->cls,
    170                                     &account_pub,
    171                                     &h,
    172                                     &account_sig,
    173                                     &h2,
    174                                     4,
    175                                     "DATA"));
    176   FAILIF (SYNC_DB_ONE_RESULT !=
    177           plugin->lookup_account_TR (plugin->cls,
    178                                      &account_pub,
    179                                      &r));
    180   FAILIF (0 != GNUNET_memcmp (&r,
    181                               &h2));
    182   FAILIF (SYNC_DB_ONE_RESULT !=
    183           plugin->lookup_backup_TR (plugin->cls,
    184                                     &account_pub,
    185                                     &account_sig2,
    186                                     &r,
    187                                     &r2,
    188                                     &bs,
    189                                     &b));
    190   FAILIF (0 != GNUNET_memcmp (&r,
    191                               &h));
    192   FAILIF (0 != GNUNET_memcmp (&r2,
    193                               &h2));
    194   FAILIF (0 != GNUNET_memcmp (&account_sig2,
    195                               &account_sig));
    196   FAILIF (bs != 4);
    197   FAILIF (0 != memcmp (b,
    198                        "DATA",
    199                        4));
    200   GNUNET_free (b);
    201   b = NULL;
    202   FAILIF (0 !=
    203           plugin->lookup_pending_payments_by_account_TR (plugin->cls,
    204                                                          &account_pub,
    205                                                          &payment_it,
    206                                                          NULL));
    207   memset (&account_pub, 2, sizeof (account_pub));
    208   FAILIF (SYNC_DB_ONE_RESULT !=
    209           plugin->store_payment_TR (plugin->cls,
    210                                     &account_pub,
    211                                     "fake-order-2",
    212                                     &token,
    213                                     &amount));
    214   FAILIF (1 !=
    215           plugin->lookup_pending_payments_by_account_TR (plugin->cls,
    216                                                          &account_pub,
    217                                                          &payment_it,
    218                                                          NULL));
    219   FAILIF (SYNC_DB_PAYMENT_REQUIRED !=
    220           plugin->store_backup_TR (plugin->cls,
    221                                    &account_pub,
    222                                    &account_sig,
    223                                    &h,
    224                                    4,
    225                                    "data"));
    226   FAILIF (SYNC_DB_ONE_RESULT !=
    227           plugin->increment_lifetime_TR (plugin->cls,
    228                                          &account_pub,
    229                                          "fake-order-2",
    230                                          GNUNET_TIME_UNIT_MINUTES));
    231   FAILIF (SYNC_DB_OLD_BACKUP_MISSING !=
    232           plugin->update_backup_TR (plugin->cls,
    233                                     &account_pub,
    234                                     &h,
    235                                     &account_sig,
    236                                     &h2,
    237                                     4,
    238                                     "DATA"));
    239   ts = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
    240   FAILIF (0 >
    241           plugin->gc (plugin->cls,
    242                       ts,
    243                       ts));
    244   memset (&account_pub, 1, sizeof (account_pub));
    245   FAILIF (SYNC_DB_NO_RESULTS !=
    246           plugin->lookup_backup_TR (plugin->cls,
    247                                     &account_pub,
    248                                     &account_sig2,
    249                                     &r,
    250                                     &r2,
    251                                     &bs,
    252                                     &b));
    253 
    254   result = 0;
    255 drop:
    256   GNUNET_free (b);
    257   GNUNET_break (GNUNET_OK ==
    258                 plugin->drop_tables (plugin->cls));
    259   SYNC_DB_plugin_unload (plugin);
    260   plugin = NULL;
    261 }
    262 
    263 
    264 int
    265 main (int argc,
    266       char *const argv[])
    267 {
    268   const char *plugin_name;
    269   char *config_filename;
    270   char *testname;
    271   struct GNUNET_CONFIGURATION_Handle *cfg;
    272 
    273   result = EXIT_FAILURE;
    274   if (NULL == (plugin_name = strrchr (argv[0], (int) '-')))
    275   {
    276     GNUNET_break (0);
    277     return EXIT_FAILURE;
    278   }
    279   GNUNET_log_setup (argv[0],
    280                     "DEBUG",
    281                     NULL);
    282   plugin_name++;
    283   (void) GNUNET_asprintf (&testname,
    284                           "%s",
    285                           plugin_name);
    286   (void) GNUNET_asprintf (&config_filename,
    287                           "test_sync_db_%s.conf",
    288                           testname);
    289   cfg = GNUNET_CONFIGURATION_create (SYNC_project_data ());
    290   if (GNUNET_OK !=
    291       GNUNET_CONFIGURATION_parse (cfg,
    292                                   config_filename))
    293   {
    294     GNUNET_break (0);
    295     GNUNET_free (config_filename);
    296     GNUNET_free (testname);
    297     return EXIT_NOTCONFIGURED;
    298   }
    299   GNUNET_SCHEDULER_run (&run, cfg);
    300   GNUNET_CONFIGURATION_destroy (cfg);
    301   GNUNET_free (config_filename);
    302   GNUNET_free (testname);
    303   return result;
    304 }
    305 
    306 
    307 /* end of test_sync_db.c */