diff options
author | Christian Grothoff <christian@grothoff.org> | 2024-02-09 20:05:25 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2024-02-09 20:05:38 +0100 |
commit | 49bbea1c6bbc64f48ccffa6595acbde204c2e867 (patch) | |
tree | 60cfa2fd371b4d6d101175c4a08428bf1e15b6c6 /src/backenddb/test_merchantdb.c | |
parent | 231d1b19d14e8aba67795e3d147a58331bafc9aa (diff) |
remove reserve/rewards
Diffstat (limited to 'src/backenddb/test_merchantdb.c')
-rw-r--r-- | src/backenddb/test_merchantdb.c | 1312 |
1 files changed, 2 insertions, 1310 deletions
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index dbec520b..2b9368f1 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -2029,7 +2029,7 @@ run_test_orders (struct TestOrders_Closure *cls) return 1; } } - /* Test lookups on mulrewardle orders */ + /* Test lookups on multiple orders */ TEST_RET_ON_FAIL (test_insert_order (&cls->instance, &cls->orders[1], GNUNET_DB_STATUS_SUCCESS_ONE_RESULT)); @@ -3131,7 +3131,7 @@ run_test_deposits (struct TestDeposits_Closure *cls) &cls->deposits[0].coin_pub, 1, cls->deposits)); - /* Test mulrewardle deposits */ + /* Test multiple deposits */ TEST_RET_ON_FAIL (test_insert_deposit (&cls->instance, &cls->signkey, &cls->deposits[1], @@ -4349,1313 +4349,6 @@ test_transfers (void) } -/* Reserves and rewards */ - - -struct ReserveData -{ - /** - * The reserve public key - */ - struct TALER_ReservePublicKeyP reserve_pub; - - /** - * The reserve private key - */ - struct TALER_ReservePrivateKeyP reserve_priv; - - /** - * The reserve initial amount - */ - struct TALER_Amount initial_amount; - - /** - * The exchange url - */ - const char *exchange_url; - - /** - * The exchange master public key. - */ - struct TALER_MasterPublicKeyP master_pub; - - /** - * The expiration date - */ - struct GNUNET_TIME_Timestamp expiration; -}; - - -/** - * Tests inserting a reserve into the database. - * @paper instance the instance the reserve is for. - * @param reserve the reserve to insert. - * @param expected_result the result we expect to receive from the db. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_insert_reserve (const struct InstanceData *instance, - const struct ReserveData *reserve, - enum TALER_ErrorCode expected_result) -{ - TEST_COND_RET_ON_FAIL (expected_result == - plugin->insert_reserve (plugin->cls, - instance->instance.id, - &reserve->reserve_priv, - &reserve->reserve_pub, - &reserve->master_pub, - reserve->exchange_url, - &reserve->initial_amount, - reserve->expiration), - "Insert reserve failed\n"); - return 0; -} - - -/** - * Container for looking up reserves. - */ -struct TestLookupReserve_Closure -{ - /** - * The reserve we expect to find. - */ - const struct ReserveData *reserve_to_cmp; - - /** - * The length of @e rewards. - */ - unsigned int rewards_length; - - /** - * The rewards that have been authorized from the reserve. - */ - const struct TALER_MERCHANTDB_RewardDetails *rewards; - - /** - * 1 if the result matches, 0 otherwise. - */ - int result_matches; -}; - - -/** - * Called after test_lookup_reserve. - * @param cls a pointer to TestLookupReserve_Closure. - * @param creation_time time when the reserve was setup - * @param expiration_time time when the reserve will be closed by the exchange - * @param merchant_initial_amount initial amount that the merchant claims to have filled the - * reserve with - * @param exchange_initial_amount initial amount that the exchange claims to have received - * @param picked_up_amount total of rewards that were picked up from this reserve - * @param committed_amount total of rewards that the merchant committed to, but that were not - * picked up yet - * @param active true if the reserve is still active (we have the private key) - * @param exchange_url base URL of the exchange hosting the reserve, NULL if not @a active - * @param payto_uri URI to use to fund the reserve, NULL if not @a active - * @param rewards_length length of the @a rewards array - * @param rewards information about the rewards created by this reserve - * - * @return 0 on success, 1 otherwise. - */ -static void -lookup_reserve_cb (void *cls, - struct GNUNET_TIME_Timestamp creation_time, - struct GNUNET_TIME_Timestamp expiration_time, - const struct TALER_Amount *merchant_initial_amount, - const struct TALER_Amount *exchange_initial_amount, - const struct TALER_Amount *picked_up_amount, - const struct TALER_Amount *committed_amount, - bool active, - const struct TALER_MasterPublicKeyP *master_pub, - const char *exchange_url, - unsigned int rewards_length, - const struct TALER_MERCHANTDB_RewardDetails *rewards) -{ - struct TestLookupReserve_Closure *cmp = cls; - unsigned int reward_cmp_results[GNUNET_NZL (rewards_length)]; - - if (NULL == cmp) - return; - if (GNUNET_TIME_timestamp_cmp (cmp->reserve_to_cmp->expiration, - !=, - expiration_time) || - (GNUNET_OK != - TALER_amount_cmp_currency ( - &cmp->reserve_to_cmp->initial_amount, - merchant_initial_amount)) || - (0 != TALER_amount_cmp (&cmp->reserve_to_cmp->initial_amount, - merchant_initial_amount)) || - (cmp->rewards_length != rewards_length)) - { - cmp->result_matches = 1; - return; - } - if (0 != strcmp (exchange_url, - "https://exch-url/")) - { - GNUNET_break (0); - cmp->result_matches = 0; - return; - } - if (0 != GNUNET_memcmp (&cmp->reserve_to_cmp->master_pub, - master_pub)) - { - GNUNET_break (0); - cmp->result_matches = 0; - return; - } - memset (reward_cmp_results, - 0, - sizeof (reward_cmp_results)); - for (unsigned int i = 0; rewards_length > i; ++i) - { - for (unsigned int j = 0; rewards_length > j; ++j) - { - if ((GNUNET_OK == TALER_amount_cmp_currency ( - &cmp->rewards[i].total_amount, - &rewards[j].total_amount)) && - (0 == TALER_amount_cmp (&cmp->rewards[i].total_amount, - &rewards[j].total_amount)) && - (0 == strcmp (cmp->rewards[i].reason, - rewards[j].reason))) - { - reward_cmp_results[i] += 1; - } - } - } - for (unsigned int i = 0; rewards_length > i; ++i) - { - if (1 != reward_cmp_results[i]) - { - cmp->result_matches = 1; - return; - } - } - cmp->result_matches = 0; -} - - -/** - * Tests looking up details of a reserve from the database. - * @param instance the instance to lookup the reserve from. - * @param reserve_pub the public key of the reserve we are looking for. - * @param reserve the data we expect to find. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_reserve (const struct InstanceData *instance, - const struct TALER_ReservePublicKeyP *reserve_pub, - const struct ReserveData *reserve) -{ - struct TestLookupReserve_Closure cmp = { - .reserve_to_cmp = reserve, - .rewards_length = 0, - .rewards = NULL, - .result_matches = 0 - }; - if (1 != plugin->lookup_reserve (plugin->cls, - instance->instance.id, - reserve_pub, - false, - &lookup_reserve_cb, - &cmp)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reserve failed\n"); - return 1; - } - if (0 != cmp.result_matches) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reserve failed: result does not match\n"); - return 1; - } - return 0; -} - - -/** - * Container for looking up mulrewardle reserves. - */ -struct TestLookupReserves_Closure -{ - /** - * The length of @e reserves_to_cmp. - */ - unsigned int reserves_to_cmp_length; - - /** - * The reserves we expect to find from the lookup. - */ - const struct ReserveData *reserves_to_cmp; - - /** - * The number of results matching each reserve we were looking for. - */ - unsigned int *results_matching; - - /** - * The total number of results found from the lookup. - */ - unsigned int results_length; -}; - - -/** - * Called after test_lookup_reserves. - * @param cls pointer to a TestLookupReserves_Closure. - * @param reserve_pub public key of the reserve - * @param creation_time time when the reserve was setup - * @param expiration_time time when the reserve will be closed by the exchange - * @param merchant_initial_amount initial amount that the merchant claims to have filled the - * reserve with - * @param exchange_initial_amount initial amount that the exchange claims to have received - * @param pickup_amount total of rewards that were picked up from this reserve - * @param committed_amount total of rewards that the merchant committed to, but that were not - * picked up yet - * @param active true if the reserve is still active (we have the private key) - */ -static void -lookup_reserves_cb (void *cls, - const struct TALER_ReservePublicKeyP *reserve_pub, - struct GNUNET_TIME_Timestamp creation_time, - struct GNUNET_TIME_Timestamp expiration_time, - const struct TALER_Amount *merchant_initial_amount, - const struct TALER_Amount *exchange_initial_amount, - const struct TALER_Amount *pickup_amount, - const struct TALER_Amount *committed_amount, - bool active) -{ - struct TestLookupReserves_Closure *cmp = cls; - if (NULL == cmp) - return; - for (unsigned int i = 0; cmp->reserves_to_cmp_length > i; ++i) - { - if ((0 == - GNUNET_memcmp (&cmp->reserves_to_cmp[i].reserve_pub, - reserve_pub)) && - (GNUNET_TIME_timestamp_cmp (cmp->reserves_to_cmp[i].expiration, - ==, - expiration_time)) && - (GNUNET_OK == - TALER_amount_cmp_currency ( - &cmp->reserves_to_cmp[i].initial_amount, - merchant_initial_amount)) && - (0 == TALER_amount_cmp (&cmp->reserves_to_cmp[i].initial_amount, - merchant_initial_amount))) - { - cmp->results_matching[i] += 1; - } - } - cmp->results_length += 1; -} - - -/** - * Test looking up reserves for an instance. - * @param instance the instance to get the reserves from. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_reserves (const struct InstanceData *instance, - unsigned int reserves_length, - const struct ReserveData *reserves) -{ - unsigned int results_matching[reserves_length]; - struct TestLookupReserves_Closure cmp = { - .reserves_to_cmp_length = reserves_length, - .reserves_to_cmp = reserves, - .results_matching = results_matching, - .results_length = 0 - }; - memset (results_matching, 0, sizeof (unsigned int) * reserves_length); - if (1 != plugin->lookup_reserves (plugin->cls, - instance->instance.id, - GNUNET_TIME_UNIT_ZERO_TS, - TALER_EXCHANGE_YNA_ALL, - TALER_EXCHANGE_YNA_ALL, - &lookup_reserves_cb, - &cmp)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reserves failed\n"); - return 1; - } - if (reserves_length != cmp.results_length) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reserves failed: incorrect number of results (%d)\n", - cmp.results_length); - return 1; - } - for (unsigned int i = 0; reserves_length > i; ++i) - { - if (1 != cmp.results_matching[i]) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reserves failed: mismatched data\n"); - return 1; - } - } - return 0; -} - - -/** - * Called after test_lookup_pending_reserves. - * @param cls pointer to a TestLookupReserves_Closure. - * @param instance_id the id of the instance the reserve belongs to. - * @param exchange_url url of the exchange for this reserve. - * @param reserve_pub public key of this reserve. - * @param expected_amount what the amount in the reserve is, according to the db. - */ -static void -lookup_pending_reserves_cb (void *cls, - const char *instance_id, - const char *exchange_url, - const struct TALER_ReservePublicKeyP *reserve_pub, - const struct TALER_Amount *expected_amount) -{ - struct TestLookupReserves_Closure *cmp = cls; - if (NULL == cmp) - return; - for (unsigned int i = 0; cmp->reserves_to_cmp_length > i; ++i) - { - if ((0 == GNUNET_memcmp (&cmp->reserves_to_cmp[i].reserve_pub, - reserve_pub)) && - (0 == strcmp (cmp->reserves_to_cmp[i].exchange_url, - exchange_url)) && - (GNUNET_OK == TALER_amount_cmp_currency ( - &cmp->reserves_to_cmp[i].initial_amount, - expected_amount)) && - (0 == TALER_amount_cmp (&cmp->reserves_to_cmp[i].initial_amount, - expected_amount))) - { - cmp->results_matching[i] += 1; - } - } - cmp->results_length += 1; -} - - -/** - * Tests looking up reserves that are not activated from the database. - * @param reserves_length length of @e reserves. - * @param reserves the reserves that the db is expected to return. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_pending_reserves (unsigned int reserves_length, - const struct ReserveData *reserves) -{ - unsigned int results_matching[GNUNET_NZL (reserves_length)]; - struct TestLookupReserves_Closure cmp = { - .reserves_to_cmp_length = reserves_length, - .reserves_to_cmp = reserves, - .results_matching = results_matching, - .results_length = 0 - }; - memset (results_matching, 0, sizeof (unsigned int) * reserves_length); - if (0 > plugin->lookup_pending_reserves (plugin->cls, - &lookup_pending_reserves_cb, - &cmp)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup pending reserves failed\n"); - return 1; - } - if (reserves_length != cmp.results_length) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup pending reserves failed: incorrect number of results (%d)\n", - cmp.results_length); - return 1; - } - for (unsigned int i = 0; reserves_length > i; ++i) - { - if (1 != cmp.results_matching[i]) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup pending reserves failed: mismatched data\n"); - return 1; - } - } - return 0; -} - - -/** - * Container for all reward data relevant to the database. - */ -struct RewardData -{ - /** - * The details of the reward. - */ - struct TALER_MERCHANTDB_RewardDetails details; - - /** - * Where the user should be redirected. - */ - const char *next_url; - - /** - * When the reward expires. - */ - struct GNUNET_TIME_Timestamp expiration; -}; - - -/** - * Creates a reward for testing. - * @param reward the reward to fill with data. - */ -static void -make_reward (struct RewardData *reward) -{ - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:0.99", - &reward->details.total_amount)); - reward->details.reason = "because"; - reward->next_url = "https://taler.net"; -} - - -/** - * Tests authorizing a reward. - * @param instance the instance authorizing the reward. - * @param reserve where the reward is coming from. - * @param reward the reward to authorize. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_authorize_reward (const struct InstanceData *instance, - const struct ReserveData *reserve, - struct RewardData *reward) -{ - TEST_COND_RET_ON_FAIL (TALER_EC_NONE == - plugin->authorize_reward (plugin->cls, - instance->instance.id, - &reserve->reserve_pub, - &reward->details.total_amount, - reward->details.reason, - reward->next_url, - &reward->details.reward_id, - &reward->expiration), - "Authorize reward failed\n"); - return 0; -} - - -/** - * Tests looking up a reward from the database. - * @param instance the instance to look up rewards from. - * @param reserve the reserve to look up rewards from. - * @param reward the reward we expect to find (uses @e reward_id to perform lookup). - * @param expected_total_picked_up how much of the reward should have been - * picked up. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_reward (const struct InstanceData *instance, - const struct ReserveData *reserve, - const struct RewardData *reward, - const struct TALER_Amount *expected_total_picked_up) -{ - struct TALER_Amount total_authorized; - struct TALER_Amount total_picked_up; - struct GNUNET_TIME_Timestamp expiration; - char *exchange_url = NULL; - char *next_url = NULL; - struct TALER_ReservePrivateKeyP reserve_priv; - - if (1 != plugin->lookup_reward (plugin->cls, - instance->instance.id, - &reward->details.reward_id, - &total_authorized, - &total_picked_up, - &expiration, - &exchange_url, - &next_url, - &reserve_priv)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reward failed\n"); - GNUNET_free (exchange_url); - return 1; - } - if ((GNUNET_OK != - TALER_amount_cmp_currency (&reward->details.total_amount, - &total_authorized)) || - (0 != TALER_amount_cmp (&reward->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)) || - (GNUNET_TIME_timestamp_cmp (reward->expiration, - !=, - expiration)) || - (0 != strcmp (reserve->exchange_url, - exchange_url)) || - (0 != GNUNET_memcmp (&reserve->reserve_priv, - &reserve_priv))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reward failed: mismatched data\n"); - GNUNET_free (exchange_url); - GNUNET_free (next_url); - return 1; - } - GNUNET_free (exchange_url); - GNUNET_free (next_url); - return 0; -} - - -/** - * Tests looking up the details of a reward from the database. - * - * @param instance the instance the reward is in. - * @param reserve the reserve the reward was authorized from. - * @param reward the reward we expect to find (uses @e reward_id to perform lookup). - * @param expected_total_picked_up how much of the reward should have been - * picked up. - * @param expected_pickups_length the length of @e expected_pickups. - * @param expected_pickups the pickups that we expect to be associated with - * the reward. - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_reward_details ( - const struct InstanceData *instance, - const struct ReserveData *reserve, - const struct RewardData *reward, - const struct TALER_Amount *expected_total_picked_up, - unsigned int expected_pickups_length, - const struct TALER_MERCHANTDB_PickupDetails *expected_pickups) -{ - struct TALER_Amount total_authorized; - struct TALER_Amount total_picked_up; - char *justification = NULL; - struct GNUNET_TIME_Timestamp expiration; - struct TALER_ReservePublicKeyP reserve_pub; - unsigned int pickups_length; - struct TALER_MERCHANTDB_PickupDetails *pickups = NULL; - unsigned int results_matching[GNUNET_NZL (expected_pickups_length)]; - - if (0 > - plugin->lookup_reward_details (plugin->cls, - instance->instance.id, - &reward->details.reward_id, - true, - &total_authorized, - &total_picked_up, - &justification, - &expiration, - &reserve_pub, - &pickups_length, - &pickups)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reward details failed\n"); - GNUNET_free (justification); - GNUNET_free (pickups); - return 1; - } - if ( (GNUNET_OK != - TALER_amount_cmp_currency (&reward->details.total_amount, - &total_authorized)) || - (0 != TALER_amount_cmp (&reward->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 (reward->details.reason, - justification)) || - (GNUNET_TIME_timestamp_cmp (reward->expiration, - !=, - expiration)) || - (0 != GNUNET_memcmp (&reserve->reserve_pub, - &reserve_pub)) || - (expected_pickups_length != pickups_length) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reward details failed: mismatched data\n"); - GNUNET_free (justification); - GNUNET_free (pickups); - return 1; - } - memset (results_matching, - 0, - sizeof (unsigned int) * expected_pickups_length); - for (unsigned int i = 0; expected_pickups_length > i; ++i) - { - for (unsigned int j = 0; pickups_length > j; ++j) - { - /* Compare expected_pickups[i] with pickups[j] */ - if ((0 == GNUNET_memcmp (&expected_pickups[i].pickup_id, - &pickups[j].pickup_id)) && - (GNUNET_OK == TALER_amount_cmp_currency ( - &expected_pickups[i].requested_amount, - &pickups[j].requested_amount)) && - (0 == TALER_amount_cmp (&expected_pickups[i].requested_amount, - &pickups[j].requested_amount)) && - (expected_pickups[i].num_planchets == pickups[j].num_planchets)) - { - results_matching[i] += 1; - } - } - } - for (unsigned int i = 0; expected_pickups_length > i; ++i) - { - if (1 != results_matching[i]) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup reward details failed: mismatched data\n"); - GNUNET_free (justification); - GNUNET_free (pickups); - return 1; - } - } - GNUNET_free (justification); - GNUNET_free (pickups); - return 0; -} - - -/** - * Utility function for freeing an array of RSA signatures. - * @param sigs_length length of @e sigs. - * @param sigs the signatures to free. - */ -static void -free_signature_array (unsigned int sigs_length, - struct TALER_BlindedDenominationSignature *sigs) -{ - for (unsigned int i = 0; sigs_length > i; ++i) - TALER_blinded_denom_sig_free (&sigs[i]); -} - - -/** - * Tests looking up a reward pickup. - * @param instance the instance to look up from. - * @param reward the reward the pickup was made for. - * @param pickup_id id of the pickup to look up. - * @param expected_exchange_url exchange url for the pickup. - * @param expected_reserve_priv reserve private key for the pickup. - * @param expected_sigs_length length of @e expected_sigs. - * @param expected_sigs the signatures we expect to be made for the pickup. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_pickup ( - const struct InstanceData *instance, - const struct RewardData *reward, - const struct TALER_PickupIdentifierP *pickup_id, - const char *expected_exchange_url, - const struct TALER_ReservePrivateKeyP *expected_reserve_priv, - unsigned int expected_sigs_length, - const struct TALER_BlindedDenominationSignature *expected_sigs) -{ - char *exchange_url = NULL; - struct TALER_ReservePrivateKeyP reserve_priv; - struct TALER_BlindedDenominationSignature sigs[GNUNET_NZL ( - expected_sigs_length)]; - unsigned int results_matching[GNUNET_NZL (expected_sigs_length)]; - - memset (sigs, - 0, - sizeof (sigs)); - if (0 > plugin->lookup_pickup (plugin->cls, - instance->instance.id, - &reward->details.reward_id, - pickup_id, - &exchange_url, - &reserve_priv, - expected_sigs_length, - sigs)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup pickup failed\n"); - GNUNET_free (exchange_url); - free_signature_array (expected_sigs_length, - sigs); - return 1; - } - if ((0 != strcmp (expected_exchange_url, - exchange_url)) || - (0 != GNUNET_memcmp (expected_reserve_priv, - &reserve_priv))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup pickup failed: mismatched data\n"); - GNUNET_free (exchange_url); - free_signature_array (expected_sigs_length, - sigs); - return 1; - } - memset (results_matching, - 0, - sizeof (unsigned int) * expected_sigs_length); - for (unsigned int i = 0; expected_sigs_length > i; ++i) - { - for (unsigned int j = 0; expected_sigs_length > j; ++j) - { - /* compare expected_sigs[i] to sigs[j] */ - if (0 == - TALER_blinded_denom_sig_cmp (&expected_sigs[i], - &sigs[j])) - { - results_matching[i] += 1; - } - } - } - for (unsigned int i = 0; expected_sigs_length > i; ++i) - { - if (1 != results_matching[i]) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup pickup failed: mismatched data\n"); - GNUNET_free (exchange_url); - free_signature_array (expected_sigs_length, - sigs); - return 1; - } - } - GNUNET_free (exchange_url); - free_signature_array (expected_sigs_length, - sigs); - return 0; -} - - -/** - * Closure for testing lookup_rewards. - */ -struct TestLookupRewards_Closure -{ - /** - * The length of @e rewards_to_cmp. - */ - unsigned int rewards_to_cmp_length; - - /** - * The rewards that we are expecting to find. - */ - const struct RewardData *rewards_to_cmp; - - /** - * The number of results found from the lookup. - */ - unsigned int results_length; - - /** - * Whether each result matches with the corresponding reward in @rewards_to_cmp. - */ - bool *results_match; -}; - - -/** - * Called after test_lookup_rewards. - * @param cls pointer to a TestLookupRewards_Closure. - * @param row_id the row id of the reward. - * @param reward_id the id of the reward. - * @param amount the amount of the reward. - */ -static void -lookup_rewards_cb (void *cls, - uint64_t row_id, - struct TALER_RewardIdentifierP reward_id, - struct TALER_Amount amount) -{ - struct TestLookupRewards_Closure *cmp = cls; - if (NULL == cmp) - return; - unsigned int i = cmp->results_length; - cmp->results_length += 1; - if (cmp->rewards_to_cmp_length > i) - { - if ((0 == GNUNET_memcmp (&cmp->rewards_to_cmp[i].details.reward_id, - &reward_id)) && - (GNUNET_OK == TALER_amount_cmp_currency ( - &cmp->rewards_to_cmp[i].details.total_amount, - &amount)) && - (0 == TALER_amount_cmp (&cmp->rewards_to_cmp[i].details.total_amount, - &amount))) - cmp->results_match[i] = true; - else - cmp->results_match[i] = false; - } -} - - -/** - * Tests looking up the rewards from the database. - * @param instance the instance to look up rewards from. - * @param expired how to filter expired rewards. - * @param offset where to start retrieving rewards. - * @param rewards_length length of @e rewards. - * @param rewards the rewards that we expect to find. - * - * @return 0 on success, 1 otherwise. - */ -static int -test_lookup_rewards (const struct InstanceData *instance, - enum TALER_EXCHANGE_YesNoAll expired, - int64_t limit, - uint64_t offset, - unsigned int rewards_length, - const struct RewardData *rewards) -{ - bool results_match[rewards_length]; - struct TestLookupRewards_Closure cmp = { - .rewards_to_cmp_length = rewards_length, - .rewards_to_cmp = rewards, - .results_length = 0, - .results_match = results_match - }; - - memset (results_match, - 0, - sizeof (bool) * rewards_length); - if (0 > plugin->lookup_rewards (plugin->cls, - instance->instance.id, - expired, - limit, - offset, - &lookup_rewards_cb, - &cmp)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup rewards failed\n"); - return 1; - } - if (rewards_length != cmp.results_length) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup rewards failed: incorrect number of results (%d)\n", - cmp.results_length); - return 1; - } - for (unsigned int i = 0; i < rewards_length; ++i) - { - if (true != cmp.results_match[i]) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Lookup rewards failed: mismatched data\n"); - return 1; - } - } - return 0; -} - - -/** - * Convenience function for testing lookup rewards with filters - * @param rewards_length length of @e rewards. - * @param rewards the array of rewards to reverse. - */ -static void -reverse_reward_data_array (unsigned int rewards_length, - struct RewardData *rewards) -{ - struct RewardData tmp[rewards_length]; - for (unsigned int i = 0; i < rewards_length; ++i) - tmp[i] = rewards[rewards_length - 1 - i]; - for (unsigned int i = 0; i < rewards_length; ++i) - rewards[i] = tmp[i]; -} - - -/** - * Container for data for testing rewards. - */ -struct TestRewards_Closure -{ - /** - * The instance. - */ - struct InstanceData instance; - - /** - * The reward reserve data. - */ - struct ReserveData reserve; - - /** - * Reserve data that is expired. - */ - struct ReserveData expired_reserve; - - /** - * A normal reward. - */ - struct RewardData reward; - - /** - * A reward that is too large to authorize. - */ - struct RewardData bigreward; - - /** - * Array of rewards for testing lookups. - */ - struct RewardData rewards[5]; - - /** - * Id of a pickup. - */ - struct TALER_PickupIdentifierP pickup_id; - - /** - * Private key of the pickup. - */ - struct GNUNET_CRYPTO_RsaPrivateKey *pickup_priv; - - /** - * Signature for the pickup. - */ - struct TALER_BlindedDenominationSignature pickup_sig; -}; - - -/** - * Prepares for testing rewards functionality. - * @param cls the data to prepare. - */ -static void -pre_test_rewards (struct TestRewards_Closure *cls) -{ - struct GNUNET_CRYPTO_BlindedSignature *us; - - /* Instance */ - make_instance ("test_inst_rewards", - &cls->instance); - - /* Reserve */ - GNUNET_CRYPTO_eddsa_key_create (&cls->reserve.reserve_priv.eddsa_priv); - GNUNET_CRYPTO_eddsa_key_get_public (&cls->reserve.reserve_priv.eddsa_priv, - &cls->reserve.reserve_pub.eddsa_pub); - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:99.99", - &cls->reserve.initial_amount)); - cls->reserve.exchange_url = "https://exch-url/"; - GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, - &cls->reserve.master_pub, - sizeof (cls->reserve.master_pub)); - GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, - &cls->pickup_id, - sizeof (cls->pickup_id)); - cls->reserve.expiration = GNUNET_TIME_relative_to_timestamp ( - 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"; - GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, - &cls->expired_reserve.master_pub, - sizeof (cls->expired_reserve.master_pub)); - cls->expired_reserve.expiration = GNUNET_TIME_UNIT_ZERO_TS; - - /* Reward/pickup */ - make_reward (&cls->reward); - make_reward (&cls->bigreward); - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:99.90", - &cls->bigreward.details.total_amount)); - for (unsigned int i = 0; i < 5; ++i) - { - char amount[16]; - make_reward (&cls->rewards[i]); - GNUNET_snprintf (amount, - 16, - "EUR:0.%u0", - i + 1); - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount (amount, - &cls->rewards[i].details.total_amount)); - } - - cls->pickup_priv = GNUNET_CRYPTO_rsa_private_key_create (2048); - us = GNUNET_new (struct GNUNET_CRYPTO_BlindedSignature); - cls->pickup_sig.blinded_sig = us; - us->cipher = GNUNET_CRYPTO_BSA_RSA; - us->rc = 1; - us->details.blinded_rsa_signature - = GNUNET_CRYPTO_rsa_sign_fdh (cls->pickup_priv, - &cls->pickup_id.hash, - sizeof (cls->pickup_id.hash)); -} - - -/** - * Cleans up after testing rewards. - * @param cls the data to clean up. - */ -static void -post_test_rewards (struct TestRewards_Closure *cls) -{ - free_instance_data (&cls->instance); - GNUNET_CRYPTO_rsa_private_key_free (cls->pickup_priv); - TALER_blinded_denom_sig_free (&cls->pickup_sig); -} - - -/** - * Runs tests for rewards. - * @param cls container of test data. - * - * @return 0 on success, 1 on failure. - */ -static int -run_test_rewards (struct TestRewards_Closure *cls) -{ - struct TALER_Amount zero; - - GNUNET_assert (GNUNET_OK == - TALER_amount_set_zero ("EUR", - &zero)); - TEST_RET_ON_FAIL (test_insert_instance (&cls->instance, - GNUNET_DB_STATUS_SUCCESS_ONE_RESULT)); - /* Test insert reserve */ - TEST_RET_ON_FAIL (test_insert_reserve (&cls->instance, - &cls->reserve, - TALER_EC_NONE)); - /* Test lookup reserve */ - TEST_RET_ON_FAIL (test_lookup_reserve (&cls->instance, - &cls->reserve.reserve_pub, - &cls->reserve)); - /* Test lookup pending reserves */ - TEST_RET_ON_FAIL (test_lookup_pending_reserves (1, - &cls->reserve)); - /* Test reserve activation */ - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == - plugin->activate_reserve (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub, - &cls->reserve.initial_amount), - "Activate reserve failed\n"); - TEST_RET_ON_FAIL (test_lookup_pending_reserves (0, - NULL)); - /* Test inserting a reward */ - TEST_RET_ON_FAIL (test_authorize_reward (&cls->instance, - &cls->reserve, - &cls->reward)); - /* Test lookup reward */ - TEST_RET_ON_FAIL (test_lookup_reward (&cls->instance, - &cls->reserve, - &cls->reward, - &zero)); - /* Test lookup reward details */ - TEST_RET_ON_FAIL (test_lookup_reward_details (&cls->instance, - &cls->reserve, - &cls->reward, - &zero, - 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->reward.details.reward_id, - &cls->reward.details. - total_amount, - &cls->pickup_id, - &cls->reward.details. - total_amount), - "Insert pickup failed\n"); - /* Test lookup pickup */ - TEST_RET_ON_FAIL (test_lookup_pickup (&cls->instance, - &cls->reward, - &cls->pickup_id, - cls->reserve.exchange_url, - &cls->reserve.reserve_priv, - 0, - NULL)); - /* Test insert pickup blind signature */ - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == - plugin->insert_pickup_blind_signature (plugin->cls, - &cls->pickup_id, - 0, - &cls->pickup_sig), - "Insert pickup blind signature failed\n"); - /* Test that overdrawing the reserve fails */ - TEST_COND_RET_ON_FAIL (TALER_EC_NONE != - plugin->authorize_reward (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub, - &cls->bigreward.details. - total_amount, - cls->bigreward.details.reason, - cls->bigreward.next_url, - &cls->bigreward.details. - reward_id, - &cls->reserve.expiration), - "Authorize reward failed\n"); - /* Test lookup rewards */ - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_ALL, - 1, - 0, - 1, - &cls->reward)); - /* Test lookup reserves */ - TEST_RET_ON_FAIL (test_lookup_reserves (&cls->instance, - 1, - &cls->reserve)); - { - /* Test lookup rewards & friends */ - struct RewardData expected_rewards[6]; - expected_rewards[0] = cls->reward; - 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_reward (&cls->instance, - &cls->expired_reserve, - &cls->rewards[i])); - } - else - { - TEST_RET_ON_FAIL (test_authorize_reward (&cls->instance, - &cls->reserve, - &cls->rewards[i])); - } - } - GNUNET_memcpy (&expected_rewards[1], - cls->rewards, - sizeof (struct RewardData) * 5); - /* Test lookup rewards inc */ - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_ALL, - 6, - 0, - 6, - expected_rewards)); - reverse_reward_data_array (6, - expected_rewards); - /* Test lookup rewards dec */ - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_ALL, - -6, - 10, - 6, - expected_rewards)); - /* Test lookup rewards expired inc */ - expected_rewards[0] = cls->rewards[0]; - expected_rewards[1] = cls->rewards[2]; - expected_rewards[2] = cls->rewards[4]; - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_YES, - 6, - 0, - 3, - expected_rewards)); - /* Test lookup rewards expired dec */ - reverse_reward_data_array (3, - expected_rewards); - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_YES, - -6, - 10, - 3, - expected_rewards)); - /* Test lookup rewards unexpired inc */ - expected_rewards[0] = cls->reward; - expected_rewards[1] = cls->rewards[1]; - expected_rewards[2] = cls->rewards[3]; - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_NO, - 6, - 0, - 3, - expected_rewards)); - /* Test lookup rewards unexpired dec */ - reverse_reward_data_array (3, - expected_rewards); - TEST_RET_ON_FAIL (test_lookup_rewards (&cls->instance, - TALER_EXCHANGE_YNA_NO, - -6, - 10, - 3, - expected_rewards)); - } - /* Test delete reserve private key */ - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == - plugin->delete_reserve (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub), - "Delete reserve private key failed\n"); - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == - plugin->delete_reserve (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub), - "Delete reserve private key failed\n"); - /* Test purging a reserve */ - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == - plugin->purge_reserve (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub), - "Purge reserve failed\n"); - TEST_COND_RET_ON_FAIL (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == - plugin->purge_reserve (plugin->cls, - cls->instance.instance.id, - &cls->reserve.reserve_pub), - "Purge reserve failed\n"); - - return 0; -} - - -/** - * Handles all logic for testing rewards in the database. - * - * @return 0 on success, 1 on failure. - */ -static int -test_rewards (void) -{ - struct TestRewards_Closure test_cls; - - pre_test_rewards (&test_cls); - int test_result = run_test_rewards (&test_cls); - post_test_rewards (&test_cls); - return test_result; -} - - /** * Closure for testing lookup_refunds. */ @@ -8366,7 +7059,6 @@ run_tests (void) TEST_RET_ON_FAIL (test_orders ()); TEST_RET_ON_FAIL (test_deposits ()); TEST_RET_ON_FAIL (test_transfers ()); - TEST_RET_ON_FAIL (test_rewards ()); TEST_RET_ON_FAIL (test_refunds ()); TEST_RET_ON_FAIL (test_lookup_orders_all_filters ()); TEST_RET_ON_FAIL (test_kyc ()); |