From 7a9c8b206332c3500ffd733b79aa56b0ea01338a Mon Sep 17 00:00:00 2001 From: Jonathan Buchanan Date: Tue, 9 Jun 2020 19:49:10 -0400 Subject: created lookup_tips method for backenddb --- src/backenddb/test_merchantdb.c | 385 ++++++++++++++++++++++++++++++++-------- 1 file changed, 309 insertions(+), 76 deletions(-) (limited to 'src/backenddb/test_merchantdb.c') diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index 31856759..435182f6 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -2905,14 +2905,49 @@ test_lookup_reserves (const char *instance_id, } +struct TipData +{ + struct TALER_MERCHANTDB_TipDetails details; + const char *next_url; + struct GNUNET_TIME_Absolute expiration; +}; + + +static void +make_tip (struct TipData *tip) +{ + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("EUR:0.99", + &tip->details.total_amount)); + tip->details.reason = "because"; + tip->next_url = "https://taler.net"; +} + + static int -test_lookup_tip (const char *instance_id, - const struct GNUNET_HashCode *tip_id, - const struct TALER_Amount *expected_total_authorized, - const struct TALER_Amount *expected_total_picked_up, - const struct GNUNET_TIME_Absolute *expected_expiration, - const char *expected_exchange_url, - const struct TALER_ReservePrivateKeyP *expected_reserve_priv) +test_authorize_tip (const struct InstanceData *instance, + const struct ReserveData *reserve, + struct TipData *tip) +{ + TEST_COND_RET_ON_FAIL (TALER_EC_NONE == + plugin->authorize_tip (plugin->cls, + instance->instance.id, + &reserve->reserve_pub, + &tip->details.total_amount, + tip->details.reason, + tip->next_url, + &tip->details.tip_id, + &tip->expiration), + "Authorize tip failed\n"); + return 0; +} + + +static int +test_lookup_tip (const struct InstanceData *instance, + const struct ReserveData *reserve, + const struct TipData *tip, + const struct TALER_Amount *expected_total_picked_up) { struct TALER_Amount total_authorized; struct TALER_Amount total_picked_up; @@ -2920,8 +2955,8 @@ test_lookup_tip (const char *instance_id, char *exchange_url = NULL; struct TALER_ReservePrivateKeyP reserve_priv; if (1 != plugin->lookup_tip (plugin->cls, - instance_id, - tip_id, + instance->instance.id, + &tip->details.tip_id, &total_authorized, &total_picked_up, &expiration, @@ -2934,18 +2969,18 @@ test_lookup_tip (const char *instance_id, GNUNET_free (exchange_url); return 1; } - if ((GNUNET_OK != TALER_amount_cmp_currency (expected_total_authorized, + if ((GNUNET_OK != TALER_amount_cmp_currency (&tip->details.total_amount, &total_authorized)) || - (0 != TALER_amount_cmp (expected_total_authorized, + (0 != TALER_amount_cmp (&tip->details.total_amount, &total_authorized)) || (GNUNET_OK != TALER_amount_cmp_currency (expected_total_picked_up, &total_picked_up)) || (0 != TALER_amount_cmp (expected_total_picked_up, &total_picked_up)) || - (expected_expiration->abs_value_us != expiration.abs_value_us) || - (0 != strcmp (expected_exchange_url, + (tip->expiration.abs_value_us != expiration.abs_value_us) || + (0 != strcmp (reserve->exchange_url, exchange_url)) || - (0 != GNUNET_memcmp (expected_reserve_priv, + (0 != GNUNET_memcmp (&reserve->reserve_priv, &reserve_priv))) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -2961,14 +2996,10 @@ test_lookup_tip (const char *instance_id, static int -test_lookup_tip_details (const char *instance_id, - const struct GNUNET_HashCode tip_id, - const struct TALER_Amount *expected_total_authorized, +test_lookup_tip_details (const struct InstanceData *instance, + const struct ReserveData *reserve, + const struct TipData *tip, const struct TALER_Amount *expected_total_picked_up, - const char *expected_justification, - const struct GNUNET_TIME_Absolute *expected_expiration, - const struct - TALER_ReservePublicKeyP *expected_reserve_pub, unsigned int expected_pickups_length, const struct TALER_MERCHANTDB_PickupDetails *expected_pickups) @@ -2982,8 +3013,8 @@ test_lookup_tip_details (const char *instance_id, struct TALER_MERCHANTDB_PickupDetails *pickups = NULL; unsigned int results_matching[expected_pickups_length]; if (TALER_EC_NONE != plugin->lookup_tip_details (plugin->cls, - instance_id, - &tip_id, + instance->instance.id, + &tip->details.tip_id, true, &total_authorized, &total_picked_up, @@ -3001,18 +3032,18 @@ test_lookup_tip_details (const char *instance_id, GNUNET_free (pickups); return 1; } - if ((GNUNET_OK != TALER_amount_cmp_currency (expected_total_authorized, + if ((GNUNET_OK != TALER_amount_cmp_currency (&tip->details.total_amount, &total_authorized)) || - (0 != TALER_amount_cmp (expected_total_authorized, + (0 != TALER_amount_cmp (&tip->details.total_amount, &total_authorized)) || (GNUNET_OK != TALER_amount_cmp_currency (expected_total_picked_up, &total_picked_up)) || (0 != TALER_amount_cmp (expected_total_picked_up, &total_picked_up)) || - (0 != strcmp (expected_justification, + (0 != strcmp (tip->details.reason, justification)) || - (expected_expiration->abs_value_us != expiration.abs_value_us) || - (0 != GNUNET_memcmp (expected_reserve_pub, + (tip->expiration.abs_value_us != expiration.abs_value_us) || + (0 != GNUNET_memcmp (&reserve->reserve_pub, &reserve_pub)) || (expected_pickups_length != pickups_length)) { @@ -3152,6 +3183,108 @@ test_lookup_pickup (const char *instance_id, } +struct TestLookupTips_Closure +{ + unsigned int tips_to_cmp_length; + const struct TipData *tips_to_cmp; + unsigned int results_length; + bool *results_match; +}; + + +static void +lookup_tips_cb (void *cls, + uint64_t row_id, + struct GNUNET_HashCode tip_id, + struct TALER_Amount amount) +{ + struct TestLookupTips_Closure *cmp = cls; + if (NULL == cmp) + return; + unsigned int i = cmp->results_length; + cmp->results_length += 1; + if (cmp->tips_to_cmp_length > i) + { + if ((0 == GNUNET_memcmp (&cmp->tips_to_cmp[i].details.tip_id, + &tip_id)) && + (GNUNET_OK == TALER_amount_cmp_currency ( + &cmp->tips_to_cmp[i].details.total_amount, + &amount)) && + (0 == TALER_amount_cmp (&cmp->tips_to_cmp[i].details.total_amount, + &amount))) + cmp->results_match[i] = true; + else + cmp->results_match[i] = false; + } +} + + +static int +test_lookup_tips (const struct InstanceData *instance, + enum TALER_MERCHANTDB_YesNoAll expired, + int64_t limit, + uint64_t offset, + unsigned int tips_length, + const struct TipData *tips) +{ + bool results_match[tips_length]; + struct TestLookupTips_Closure cmp = { + .tips_to_cmp_length = tips_length, + .tips_to_cmp = tips, + .results_length = 0, + .results_match = results_match + }; + + memset (results_match, + 0, + sizeof (bool) * tips_length); + if (0 > plugin->lookup_tips (plugin->cls, + instance->instance.id, + expired, + limit, + offset, + &lookup_tips_cb, + &cmp)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Lookup tips failed\n"); + return 1; + } + if (tips_length != cmp.results_length) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Lookup tips failed: incorrect number of results (%d)\n", + cmp.results_length); + return 1; + } + for (unsigned int i = 0; i < tips_length; ++i) + { + if (true != cmp.results_match[i]) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Lookup tips failed: mismatched data\n"); + return 1; + } + } + return 0; +} + + +/** + * Convenience function for testing lookup tips with filters + */ +static void +reverse_tip_data_array (unsigned int tips_length, + struct TipData *tips) +{ + struct TipData tmp[tips_length]; + for (unsigned int i = 0; i < tips_length; ++i) + tmp[i] = tips[tips_length - 1 - i]; + for (unsigned int i = 0; i < tips_length; ++i) + tips[i] = tmp[i]; +} + + struct TestTips_Closure { struct InstanceData instance; @@ -3160,16 +3293,16 @@ struct TestTips_Closure * The tip reserve data */ struct ReserveData reserve; + struct ReserveData expired_reserve; /* Tip and pickup data */ - struct TALER_Amount tip_amount; - struct GNUNET_HashCode tip_id; + struct TipData tip; + struct TipData bigtip; + struct TipData tips[5]; + struct GNUNET_HashCode pickup_id; struct GNUNET_CRYPTO_RsaPrivateKey *pickup_priv; struct GNUNET_CRYPTO_RsaSignature *pickup_sig; - - struct TALER_Amount tip_2_amount; - struct GNUNET_HashCode tip_2_id; }; @@ -3192,23 +3325,40 @@ pre_test_tips (struct TestTips_Closure *cls) GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), GNUNET_TIME_UNIT_WEEKS); + GNUNET_CRYPTO_eddsa_key_create ( + &cls->expired_reserve.reserve_priv.eddsa_priv); + GNUNET_CRYPTO_eddsa_key_get_public ( + &cls->expired_reserve.reserve_priv.eddsa_priv, + &cls->expired_reserve.reserve_pub. + eddsa_pub); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("EUR:99.99", + &cls->expired_reserve.initial_amount)); + cls->expired_reserve.exchange_url = "exch-url"; + cls->expired_reserve.expiration = GNUNET_TIME_UNIT_ZERO_ABS; + /* Tip/pickup */ + make_tip (&cls->tip); + make_tip (&cls->bigtip); GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:0.99", - &cls->tip_amount)); - GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, - &cls->tip_id); - GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, - &cls->tip_id); + TALER_string_to_amount ("EUR:99.90", + &cls->bigtip.details.total_amount)); + for (unsigned int i = 0; i < 5; ++i) + { + char amount[16]; + make_tip (&cls->tips[i]); + GNUNET_snprintf (amount, + 16, + "EUR:0.%u0", + i + 1); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (amount, + &cls->tips[i].details.total_amount)); + } + cls->pickup_priv = GNUNET_CRYPTO_rsa_private_key_create (2048); cls->pickup_sig = GNUNET_CRYPTO_rsa_sign_fdh (cls->pickup_priv, &cls->pickup_id); - - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:99.90", - &cls->tip_2_amount)); - GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_STRONG, - &cls->tip_2_id); } @@ -3246,46 +3396,33 @@ run_test_tips (struct TestTips_Closure *cls) &cls->reserve.initial_amount), "Activate reserve failed\n"); /* Test inserting a tip */ - TEST_COND_RET_ON_FAIL (TALER_EC_NONE == - plugin->authorize_tip (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub, - &cls->tip_amount, - "because", - "https://taler.net", - &cls->tip_id, - &cls->reserve.expiration), - "Authorize tip failed\n"); + TEST_RET_ON_FAIL (test_authorize_tip (&cls->instance, + &cls->reserve, + &cls->tip)); /* Test lookup tip */ - TEST_RET_ON_FAIL (test_lookup_tip (cls->instance.instance.id, - &cls->tip_id, - &cls->tip_amount, - &zero, - &cls->reserve.expiration, - cls->reserve.exchange_url, - &cls->reserve.reserve_priv)); + TEST_RET_ON_FAIL (test_lookup_tip (&cls->instance, + &cls->reserve, + &cls->tip, + &zero)); /* Test lookup tip details */ - TEST_RET_ON_FAIL (test_lookup_tip_details (cls->instance.instance.id, - cls->tip_id, - &cls->tip_amount, + TEST_RET_ON_FAIL (test_lookup_tip_details (&cls->instance, + &cls->reserve, + &cls->tip, &zero, - "because", - &cls->reserve.expiration, - &cls->reserve.reserve_pub, 0, NULL)); /* Test insert pickup */ TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == plugin->insert_pickup (plugin->cls, cls->instance.instance.id, - &cls->tip_id, - &cls->tip_amount, + &cls->tip.details.tip_id, + &cls->tip.details.total_amount, &cls->pickup_id, - &cls->tip_amount), + &cls->tip.details.total_amount), "Insert pickup failed\n"); /* Test lookup pickup */ TEST_RET_ON_FAIL (test_lookup_pickup (cls->instance.instance.id, - &cls->tip_id, + &cls->tip.details.tip_id, &cls->pickup_id, cls->reserve.exchange_url, &cls->reserve.reserve_priv, @@ -3303,16 +3440,112 @@ run_test_tips (struct TestTips_Closure *cls) plugin->authorize_tip (plugin->cls, cls->instance.instance.id, &cls->reserve.reserve_pub, - &cls->tip_2_amount, - "because", - "https://taler.net", - &cls->tip_2_id, + &cls->bigtip.details. + total_amount, + cls->bigtip.details.reason, + cls->bigtip.next_url, + &cls->bigtip.details.tip_id, &cls->reserve.expiration), "Authorize tip failed\n"); + /* Test lookup tips */ + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_ALL, + 1, + 0, + 1, + &cls->tip)); /* Test lookup reserves */ TEST_RET_ON_FAIL (test_lookup_reserves (cls->instance.instance.id, 1, &cls->reserve)); + { + /* Test lookup tips & friends */ + struct TipData expected_tips[6]; + expected_tips[0] = cls->tip; + TEST_RET_ON_FAIL (test_insert_reserve (&cls->instance, + &cls->expired_reserve, + TALER_EC_NONE)); + TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == + plugin->activate_reserve (plugin->cls, + cls->instance.instance.id, + &cls->expired_reserve. + reserve_pub, + &cls->expired_reserve. + initial_amount), + "Activate reserve failed\n"); + for (unsigned int i = 0; i < 5; ++i) + { + if (i % 2 == 0) + { + TEST_RET_ON_FAIL (test_authorize_tip (&cls->instance, + &cls->expired_reserve, + &cls->tips[i])); + } + else + { + TEST_RET_ON_FAIL (test_authorize_tip (&cls->instance, + &cls->reserve, + &cls->tips[i])); + } + } + GNUNET_memcpy (&expected_tips[1], + cls->tips, + sizeof (struct TipData) * 5); + /* Test lookup tips inc */ + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_ALL, + 6, + 0, + 6, + expected_tips)); + reverse_tip_data_array (6, + expected_tips); + /* Test lookup tips dec */ + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_ALL, + -6, + 10, + 6, + expected_tips)); + /* Test lookup tips expired inc */ + expected_tips[0] = cls->tips[0]; + expected_tips[1] = cls->tips[2]; + expected_tips[2] = cls->tips[4]; + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_YES, + 6, + 0, + 3, + expected_tips)); + /* Test lookup tips expired dec */ + reverse_tip_data_array (3, + expected_tips); + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_YES, + -6, + 10, + 3, + expected_tips)); + /* Test lookup tips unexpired inc */ + expected_tips[0] = cls->tip; + expected_tips[1] = cls->tips[1]; + expected_tips[2] = cls->tips[3]; + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_NO, + 6, + 0, + 3, + expected_tips)); + /* Test lookup tips unexpired dec */ + reverse_tip_data_array (3, + expected_tips); + TEST_RET_ON_FAIL (test_lookup_tips (&cls->instance, + TALER_MERCHANTDB_YNA_NO, + -6, + 10, + 3, + expected_tips)); + } /* Test delete reserve private key */ TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == plugin->delete_reserve (plugin->cls, -- cgit v1.2.3