From 0220c51bc986a4983234da9f323473ddc31ffd13 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Fri, 6 Mar 2015 01:15:46 +0100 Subject: [PATCH] db: Added testcase for reserves_in_insert() --- src/mint/mint_db.c | 16 +++++++++++----- src/mint/test_mint_db.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/mint/mint_db.c b/src/mint/mint_db.c index 71e222008..b102accff 100644 --- a/src/mint/mint_db.c +++ b/src/mint/mint_db.c @@ -43,7 +43,7 @@ static pthread_key_t db_conn_threadlocal; #define QUERY_ERR(result) \ - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s: query failed: %s\n", __FUNCTION__, PQresultErrorMessage (result)) + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Query failed at %s:%u: %s\n", __FILE__, __LINE__, PQresultErrorMessage (result)) /** * Database connection string, as read from @@ -77,6 +77,12 @@ static char *TALER_MINT_db_connection_cfg_str; PQclear (result); result = NULL; \ } while (0) +/** + * This the length of the currency strings (without 0-termination) we use. Note + * that we need to use this at the DB layer instead of TALER_CURRENCY_LEN as the + * DB only needs to store 3 bytes instead of 8 bytes. + */ +#define TALER_DB_CURRENCY_LEN 3 /** * Set the given connection to use a temporary schema @@ -737,7 +743,7 @@ TALER_MINT_DB_reserve_get (PGconn *db, TALER_DB_extract_amount (result, 0, "current_balance_value", "current_balance_fraction", - "current_balance_currency", + "balance_currency", &reserve->balance)); reserve->expiry.abs_value_us = GNUNET_ntohll (expiration_date_nbo); PQclear (result); @@ -837,11 +843,11 @@ TALER_MINT_DB_reserves_in_insert (PGconn *db, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reserve does not exist; creating a new one\n"); struct TALER_DB_QueryParam params[] = { - TALER_DB_QUERY_PARAM_PTR_SIZED (reserve->pub, sizeof (reserve->pub)), + TALER_DB_QUERY_PARAM_PTR (reserve->pub), TALER_DB_QUERY_PARAM_PTR (&balance_nbo.value), TALER_DB_QUERY_PARAM_PTR (&balance_nbo.fraction), TALER_DB_QUERY_PARAM_PTR_SIZED (balance_nbo.currency, - TALER_CURRENCY_LEN), + TALER_DB_CURRENCY_LEN), TALER_DB_QUERY_PARAM_PTR (&expiry_nbo), TALER_DB_QUERY_PARAM_END }; @@ -866,7 +872,7 @@ TALER_MINT_DB_reserves_in_insert (PGconn *db, result = TALER_DB_exec_prepared (db, "create_reserves_in_transaction", params); - if (PGRES_COMMAND_OK != result) + if (PGRES_COMMAND_OK != PQresultStatus(result)) { QUERY_ERR (result); goto rollback; diff --git a/src/mint/test_mint_db.c b/src/mint/test_mint_db.c index 4fa84eb38..195fa275b 100644 --- a/src/mint/test_mint_db.c +++ b/src/mint/test_mint_db.c @@ -25,6 +25,13 @@ static int result; +#define FAILIF(cond) \ + do { \ + if (!(cond)){ break;} \ + GNUNET_break (0); \ + goto drop; \ + } while (0) + /** * Main function that will be run by the scheduler. @@ -39,6 +46,10 @@ run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { PGconn *db; + struct GNUNET_CRYPTO_EddsaPublicKey pub; + struct Reserve reserve; + struct GNUNET_TIME_Absolute expiry; + struct TALER_Amount amount; db = NULL; if (GNUNET_OK != TALER_MINT_DB_init ("postgres:///taler")) @@ -56,6 +67,28 @@ run (void *cls, char *const *args, const char *cfgfile, result = 3; goto drop; } + GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, + &pub, sizeof (pub)); + reserve.pub = &pub; + amount.value = 1; + amount.fraction = 1; + strcpy (amount.currency, "EUR"); + expiry = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), + GNUNET_TIME_UNIT_HOURS); + result = 4; + FAILIF (GNUNET_OK != TALER_MINT_DB_reserves_in_insert (db, + &reserve, + amount, + expiry)); + { + struct Reserve g_reserve; + g_reserve.pub = &pub; + FAILIF (GNUNET_OK != TALER_MINT_DB_reserve_get (db, &g_reserve)); + FAILIF (amount.value != g_reserve.balance.value); + FAILIF (amount.fraction != g_reserve.balance.fraction); + FAILIF (0 != strcmp (amount.currency, g_reserve.balance.currency)); + FAILIF (expiry.abs_value_us != g_reserve.expiry.abs_value_us); + } result = 0; drop: if (NULL != db)