test_sync_db.c (9570B)
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 true)) 95 { 96 result = 77; 97 return; 98 } 99 if (GNUNET_OK != 100 SYNCDB_drop_tables ()) 101 { 102 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 103 "Dropping tables failed\n"); 104 } 105 if (GNUNET_OK != 106 SYNCDB_create_tables ()) 107 { 108 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 109 "Creating tables failed\n"); 110 } 111 GNUNET_assert (GNUNET_OK == 112 SYNCDB_preflight ()); 113 memset (&account_pub, 1, sizeof (account_pub)); 114 memset (&account_sig, 2, sizeof (account_sig)); 115 memset (&token, 3, sizeof (token)); 116 GNUNET_CRYPTO_hash ("data", 4, &h); 117 GNUNET_CRYPTO_hash ("DATA", 4, &h2); 118 GNUNET_CRYPTO_hash ("ATAD", 4, &h3); 119 GNUNET_assert (GNUNET_OK == 120 TALER_string_to_amount ("EUR:1", 121 &amount)); 122 FAILIF (SYNC_DB_ONE_RESULT != 123 SYNCDB_store_payment_TR (&account_pub, 124 "fake-order", 125 &token, 126 &amount)); 127 FAILIF (SYNC_DB_ONE_RESULT != 128 SYNCDB_increment_lifetime_TR (&account_pub, 129 "fake-order", 130 GNUNET_TIME_UNIT_MINUTES)); 131 FAILIF (SYNC_DB_ONE_RESULT != 132 SYNCDB_store_backup_TR (&account_pub, 133 &account_sig, 134 &h, 135 4, 136 "data")); 137 FAILIF (SYNC_DB_NO_RESULTS != 138 SYNCDB_store_backup_TR (&account_pub, 139 &account_sig, 140 &h, 141 4, 142 "data")); 143 FAILIF (SYNC_DB_ONE_RESULT != 144 SYNCDB_update_backup_TR (&account_pub, 145 &h, 146 &account_sig, 147 &h2, 148 4, 149 "DATA")); 150 FAILIF (SYNC_DB_OLD_BACKUP_MISMATCH != 151 SYNCDB_update_backup_TR (&account_pub, 152 &h, 153 &account_sig, 154 &h3, 155 4, 156 "ATAD")); 157 FAILIF (SYNC_DB_NO_RESULTS != 158 SYNCDB_update_backup_TR (&account_pub, 159 &h, 160 &account_sig, 161 &h2, 162 4, 163 "DATA")); 164 FAILIF (SYNC_DB_ONE_RESULT != 165 SYNCDB_lookup_account_TR (&account_pub, 166 &r)); 167 FAILIF (0 != GNUNET_memcmp (&r, 168 &h2)); 169 FAILIF (SYNC_DB_ONE_RESULT != 170 SYNCDB_lookup_backup_TR (&account_pub, 171 &account_sig2, 172 &r, 173 &r2, 174 &bs, 175 &b)); 176 FAILIF (0 != GNUNET_memcmp (&r, 177 &h)); 178 FAILIF (0 != GNUNET_memcmp (&r2, 179 &h2)); 180 FAILIF (0 != GNUNET_memcmp (&account_sig2, 181 &account_sig)); 182 FAILIF (bs != 4); 183 FAILIF (0 != memcmp (b, 184 "DATA", 185 4)); 186 GNUNET_free (b); 187 b = NULL; 188 FAILIF (0 != 189 SYNCDB_lookup_pending_payments_by_account_TR (&account_pub, 190 &payment_it, 191 NULL)); 192 memset (&account_pub, 2, sizeof (account_pub)); 193 FAILIF (SYNC_DB_ONE_RESULT != 194 SYNCDB_store_payment_TR (&account_pub, 195 "fake-order-2", 196 &token, 197 &amount)); 198 FAILIF (1 != 199 SYNCDB_lookup_pending_payments_by_account_TR (&account_pub, 200 &payment_it, 201 NULL)); 202 FAILIF (SYNC_DB_PAYMENT_REQUIRED != 203 SYNCDB_store_backup_TR (&account_pub, 204 &account_sig, 205 &h, 206 4, 207 "data")); 208 FAILIF (SYNC_DB_ONE_RESULT != 209 SYNCDB_increment_lifetime_TR (&account_pub, 210 "fake-order-2", 211 GNUNET_TIME_UNIT_MINUTES)); 212 FAILIF (SYNC_DB_OLD_BACKUP_MISSING != 213 SYNCDB_update_backup_TR (&account_pub, 214 &h, 215 &account_sig, 216 &h2, 217 4, 218 "DATA")); 219 ts = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS); 220 FAILIF (0 > 221 SYNCDB_gc (ts, 222 ts)); 223 memset (&account_pub, 1, sizeof (account_pub)); 224 FAILIF (SYNC_DB_NO_RESULTS != 225 SYNCDB_lookup_backup_TR (&account_pub, 226 &account_sig2, 227 &r, 228 &r2, 229 &bs, 230 &b)); 231 232 result = 0; 233 drop: 234 GNUNET_free (b); 235 GNUNET_break (GNUNET_OK == 236 SYNCDB_drop_tables ()); 237 SYNCDB_fini (); 238 } 239 240 241 int 242 main (int argc, 243 char *const argv[]) 244 { 245 const char *plugin_name; 246 char *config_filename; 247 char *testname; 248 struct GNUNET_CONFIGURATION_Handle *cfg; 249 250 result = EXIT_FAILURE; 251 if (NULL == (plugin_name = strrchr (argv[0], (int) '-'))) 252 { 253 GNUNET_break (0); 254 return EXIT_FAILURE; 255 } 256 GNUNET_log_setup (argv[0], 257 "DEBUG", 258 NULL); 259 plugin_name++; 260 (void) GNUNET_asprintf (&testname, 261 "%s", 262 plugin_name); 263 (void) GNUNET_asprintf (&config_filename, 264 "test_sync_db_%s.conf", 265 testname); 266 cfg = GNUNET_CONFIGURATION_create (SYNC_project_data ()); 267 if (GNUNET_OK != 268 GNUNET_CONFIGURATION_parse (cfg, 269 config_filename)) 270 { 271 GNUNET_break (0); 272 GNUNET_free (config_filename); 273 GNUNET_free (testname); 274 return EXIT_NOTCONFIGURED; 275 } 276 GNUNET_SCHEDULER_run (&run, cfg); 277 GNUNET_CONFIGURATION_destroy (cfg); 278 GNUNET_free (config_filename); 279 GNUNET_free (testname); 280 return result; 281 } 282 283 284 /* end of test_sync_db.c */