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