From e13b13b3041e905feb0e22a3488457c280dbeed2 Mon Sep 17 00:00:00 2001 From: Jonathan Buchanan Date: Sun, 12 Jul 2020 04:37:35 -0400 Subject: finish documenting test_merchantdb.c --- src/backenddb/test_merchantdb.c | 454 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 421 insertions(+), 33 deletions(-) (limited to 'src/backenddb') diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index 5f3def2c..152cb567 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -4535,7 +4535,7 @@ lookup_reserves_cb (void *cls, * Test looking up reserves for an instance. * @param instance the instance to get the reserves from. * - * @returns 0 on success, 1 otherwise. + * @return 0 on success, 1 otherwise. */ static int test_lookup_reserves (const struct InstanceData *instance, @@ -4582,6 +4582,14 @@ test_lookup_reserves (const struct InstanceData *instance, } +/** + * 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, @@ -4611,6 +4619,13 @@ lookup_pending_reserves_cb (void *cls, } +/** + * 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) @@ -4651,14 +4666,32 @@ test_lookup_pending_reserves (unsigned int reserves_length, } +/** + * Container for all tip data relevant to the database. + */ struct TipData { + /** + * The details of the tip. + */ struct TALER_MERCHANTDB_TipDetails details; + + /** + * Where the user should be redirected. + */ const char *next_url; + + /** + * When the tip expires. + */ struct GNUNET_TIME_Absolute expiration; }; +/** + * Creates a tip for testing. + * @param tip the tip to fill with data. + */ static void make_tip (struct TipData *tip) { @@ -4670,6 +4703,14 @@ make_tip (struct TipData *tip) } +/** + * Tests authorizing a tip. + * @param instance the instance authorizing the tip. + * @param reserve where the tip is coming from. + * @param tip the tip to authorize. + * + * @return 0 on success, 1 otherwise. + */ static int test_authorize_tip (const struct InstanceData *instance, const struct ReserveData *reserve, @@ -4689,6 +4730,16 @@ test_authorize_tip (const struct InstanceData *instance, } +/** + * Tests looking up a tip from the database. + * @param instance the instance to look up tips from. + * @param reserve the reserve to look up tips from. + * @param tip the tip we expect to find (uses @e tip_id to perform lookup). + * @param expected_total_picked_up how much of the tip should have been + * picked up. + * + * @return 0 on success, 1 otherwise. + */ static int test_lookup_tip (const struct InstanceData *instance, const struct ReserveData *reserve, @@ -4741,6 +4792,19 @@ test_lookup_tip (const struct InstanceData *instance, } +/** + * Tests looking up the details of a tip from the database. + * @param instance the instance the tip is in. + * @param reserve the reserve the tip was authorized from. + * @param tip the tip we expect to find (uses @e tip_id to perform lookup). + * @param expected_total_picked_up how much of the tip 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 tip. + * + * @return 0 on success, 1 otherwise. + */ static int test_lookup_tip_details (const struct InstanceData *instance, const struct ReserveData *reserve, @@ -4839,6 +4903,11 @@ test_lookup_tip_details (const struct InstanceData *instance, } +/** + * 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_rsa_signature_array (unsigned int sigs_length, struct GNUNET_CRYPTO_RsaSignature **sigs) @@ -4851,9 +4920,21 @@ free_rsa_signature_array (unsigned int sigs_length, } +/** + * Tests looking up a tip pickup. + * @param instance the instance to look up from. + * @param tip the tip 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 char *instance_id, - const struct GNUNET_HashCode *tip_id, +test_lookup_pickup (const struct InstanceData *instance, + const struct TipData *tip, const struct GNUNET_HashCode *pickup_id, const char *expected_exchange_url, const struct @@ -4869,8 +4950,8 @@ test_lookup_pickup (const char *instance_id, 0, sizeof (struct GNUNET_CRYPTO_RsaSignature *) * expected_sigs_length); if (0 > plugin->lookup_pickup (plugin->cls, - instance_id, - tip_id, + instance->instance.id, + &tip->details.tip_id, pickup_id, &exchange_url, &reserve_priv, @@ -4930,15 +5011,40 @@ test_lookup_pickup (const char *instance_id, } +/** + * Closure for testing lookup_tips. + */ struct TestLookupTips_Closure { + /** + * The length of @e tips_to_cmp. + */ unsigned int tips_to_cmp_length; + + /** + * The tips that we are expecting to find. + */ const struct TipData *tips_to_cmp; + + /** + * The number of results found from the lookup. + */ unsigned int results_length; + + /** + * Whether each result matches with the corresponding tip in @tips_to_cmp. + */ bool *results_match; }; +/** + * Called after test_lookup_tips. + * @param cls pointer to a TestLookupTips_Closure. + * @param row_id the row id of the tip. + * @param tip_id the id of the tip. + * @param amount the amount of the tip. + */ static void lookup_tips_cb (void *cls, uint64_t row_id, @@ -4966,6 +5072,16 @@ lookup_tips_cb (void *cls, } +/** + * Tests looking up the tips from the database. + * @param instance the instance to look up tips from. + * @param expired how to filter expired tips. + * @param offset where to start retrieving tips. + * @param tips_length length of @e tips. + * @param tips the tips that we expect to find. + * + * @return 0 on success, 1 otherwise. + */ static int test_lookup_tips (const struct InstanceData *instance, enum TALER_EXCHANGE_YesNoAll expired, @@ -5019,6 +5135,8 @@ test_lookup_tips (const struct InstanceData *instance, /** * Convenience function for testing lookup tips with filters + * @param tips_length length of @e tips. + * @param tips the array of tips to reverse. */ static void reverse_tip_data_array (unsigned int tips_length, @@ -5032,27 +5150,62 @@ reverse_tip_data_array (unsigned int tips_length, } +/** + * Container for data for testing tips. + */ struct TestTips_Closure { + /** + * The instance. + */ struct InstanceData instance; /** - * The tip reserve data + * The tip reserve data. */ struct ReserveData reserve; + + /** + * Reserve data that is expired. + */ struct ReserveData expired_reserve; - /* Tip and pickup data */ + /** + * A normal tip. + */ struct TipData tip; + + /** + * A tip that is too large to authorize. + */ struct TipData bigtip; + + /** + * Array of tips for testing lookups. + */ struct TipData tips[5]; + /** + * Id of a pickup. + */ struct GNUNET_HashCode pickup_id; + + /** + * Private key of the pickup. + */ struct GNUNET_CRYPTO_RsaPrivateKey *pickup_priv; + + /** + * Signature for the pickup. + */ struct GNUNET_CRYPTO_RsaSignature *pickup_sig; }; +/** + * Prepares for testing tips functionality. + * @param cls the data to prepare. + */ static void pre_test_tips (struct TestTips_Closure *cls) { @@ -5109,6 +5262,10 @@ pre_test_tips (struct TestTips_Closure *cls) } +/** + * Cleans up after testing tips. + * @param cls the data to clean up. + */ static void post_test_tips (struct TestTips_Closure *cls) { @@ -5119,6 +5276,12 @@ post_test_tips (struct TestTips_Closure *cls) } +/** + * Runs tests for tips. + * @param cls container of test data. + * + * @return 0 on success, 1 on failure. + */ static int run_test_tips (struct TestTips_Closure *cls) { @@ -5173,8 +5336,8 @@ run_test_tips (struct TestTips_Closure *cls) &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.details.tip_id, + TEST_RET_ON_FAIL (test_lookup_pickup (&cls->instance, + &cls->tip, &cls->pickup_id, cls->reserve.exchange_url, &cls->reserve.reserve_priv, @@ -5325,8 +5488,13 @@ run_test_tips (struct TestTips_Closure *cls) } +/** + * Handles all logic for testing tips in the database. + * + * @return 0 on success, 1 on failure. + */ static int -test_tips (void *cls) +test_tips (void) { struct TestTips_Closure test_cls; pre_test_tips (&test_cls); @@ -5336,20 +5504,44 @@ test_tips (void *cls) } +/** + * Closure for testing lookup_refunds. + */ struct TestLookupRefunds_Closure { + /** + * Length of @e coin_pub_to_cmp and @e refund_amount_to_cmp. + */ unsigned int refunds_to_cmp_length; + /** + * Public keys of the refunded coins. + */ const struct TALER_CoinSpendPublicKeyP *coin_pub_to_cmp; + /** + * Amount of each refund. + */ const struct TALER_Amount *refund_amount_to_cmp; + /** + * Number of results matching each refund provided. + */ unsigned int *results_matching; + /** + * Total number of results returned; + */ unsigned int results_length; }; +/** + * Called after test_lookup_refunds. + * @param cls pointer to a TestLookupRefunds_Closure. + * @param coin_pub the public key of the coin for the refund found. + * @param refund_amount the amount of the refund found. + */ static void lookup_refunds_cb (void *cls, const struct TALER_CoinSpendPublicKeyP *coin_pub, @@ -5374,8 +5566,18 @@ lookup_refunds_cb (void *cls, } +/** + * Tests looking up refunds from the database. + * @param instance the instance to look up refunds for. + * @param h_contract_terms hash of the contract terms the refunds are for. + * @param refunds_length length of @e coin_pubs and @e refund_amounts. + * @param coin_pubs the public keys of the coins that were refunded. + * @param refund_amounts the amounts of the coins that were refunded. + * + * @return 0 on success, 1 otherwise. + */ static int -test_lookup_refunds (const char *instance_id, +test_lookup_refunds (const struct InstanceData *instance, const struct GNUNET_HashCode *h_contract_terms, unsigned int refunds_length, const struct TALER_CoinSpendPublicKeyP *coin_pubs, @@ -5391,7 +5593,7 @@ test_lookup_refunds (const char *instance_id, }; memset (results_matching, 0, sizeof (unsigned int) * refunds_length); if (1 != plugin->lookup_refunds (plugin->cls, - instance_id, + instance->instance.id, h_contract_terms, &lookup_refunds_cb, &cmp)) @@ -5419,16 +5621,43 @@ test_lookup_refunds (const char *instance_id, } +/** + * Container for refund data. + */ struct RefundData { + /** + * When the refund occured. + */ struct GNUNET_TIME_Absolute timestamp; + + /** + * Reason for the refund. + */ const char *reason; + + /** + * Amount of the refund. + */ struct TALER_Amount refund_amount; + + /** + * Public key of the coin that was refunded. + */ const struct TALER_CoinSpendPublicKeyP *coin_pub; + + /** + * URL to the exchange that did the refund. + */ const char *exchange_url; }; +/** + * Creates a refund for testing with. + * @param deposit the deposit being refunded. + * @param refund the data to set. + */ static void make_refund (const struct DepositData *deposit, struct RefundData *refund) @@ -5441,25 +5670,61 @@ make_refund (const struct DepositData *deposit, } +/** + * Container for proof of a refund. + */ struct RefundProofData { + /** + * Fee charged for the refund. + */ struct TALER_Amount refund_fee; + + /** + * The exchange's signature on the refund. + */ struct TALER_ExchangeSignatureP exchange_sig; }; +/** + * Closure for testing lookup_refunds_detailed. + */ struct TestLookupRefundsDetailed_Closure { + /** + * Length of @e refunds_to_cmp. + */ unsigned int refunds_to_cmp_length; + /** + * The refunds we expect to find. + */ const struct RefundData *refunds_to_cmp; + /** + * The number of results matching each refund. + */ unsigned int *results_matching; + /** + * The total number of results from the db. + */ unsigned int results_length; }; +/** + * Called after test_lookup_refunds_detailed. + * @param cls pointer to a TestLookupRefundsDetailed_Closure. + * @param refund_serial unique serial number of the refund + * @param timestamp time of the refund (for grouping of refunds in the wallet UI) + * @param coin_pub public coin from which the refund comes from + * @param exchange_url URL of the exchange that issued @a coin_pub + * @param rtransaction_id identificator of the refund + * @param reason human-readable explanation of the refund + * @param refund_amount refund amount which is being taken from @a coin_pub + */ static void lookup_refunds_detailed_cb (void *cls, uint64_t refund_serial, @@ -5496,8 +5761,17 @@ lookup_refunds_detailed_cb (void *cls, } +/** + * Tests looking up refunds with details from the database. + * @param instance the instance to lookup from. + * @param h_contract_terms the contract terms the refunds were made for. + * @param refunds_length length of @e refunds. + * @param refunds the refunds we expect to be returned. + * + * @return 0 on success, 1 otherwise. + */ static int -test_lookup_refunds_detailed (const char *instance_id, +test_lookup_refunds_detailed (const struct InstanceData *instance, const struct GNUNET_HashCode *h_contract_terms, unsigned int refunds_length, const struct RefundData *refunds) @@ -5511,7 +5785,7 @@ test_lookup_refunds_detailed (const char *instance_id, }; memset (results_matching, 0, sizeof (unsigned int) * refunds_length); if (1 != plugin->lookup_refunds_detailed (plugin->cls, - instance_id, + instance->instance.id, h_contract_terms, &lookup_refunds_detailed_cb, &cmp)) @@ -5539,14 +5813,34 @@ test_lookup_refunds_detailed (const char *instance_id, } +/** + * Closure for get_refund_serial. + */ struct LookupRefundSerial_Closure { + /** + * The refund we are looking up the id for. + */ const struct RefundData *refund; + /** + * The row number found. + */ uint64_t serial; }; +/** + * Called after get_refund_serial. + * @param cls pointer to a LookupRefundSerial_Closure. + * @param refund_serial unique serial number of the refund + * @param timestamp time of the refund (for grouping of refunds in the wallet UI) + * @param coin_pub public coin from which the refund comes from + * @param exchange_url URL of the exchange that issued @a coin_pub + * @param rtransaction_id identificator of the refund + * @param reason human-readable explanation of the refund + * @param refund_amount refund amount which is being taken from @a coin_pub + */ static void get_refund_serial_cb (void *cls, uint64_t refund_serial, @@ -5577,6 +5871,14 @@ get_refund_serial_cb (void *cls, } +/** + * Utility function for getting the database row number of a refund. + * @param instance the instance associated with the refund. + * @param h_contract_terms the contract terms the refund was made with. + * @param refund the refund we are querying the row number of. + * + * @return the row number of the refund. + */ static uint64_t get_refund_serial (const struct InstanceData *instance, const struct GNUNET_HashCode *h_contract_terms, @@ -5598,6 +5900,14 @@ get_refund_serial (const struct InstanceData *instance, } +/** + * Tests looking up proof of a refund. + * @param refund_serial the row number of the refund. + * @param expected_exchange_sig the exchange signature we are expecting. + * @param expected_exchange_pub the exchange public key we are expecting. + * + * @return 0 on success, 1 otherwise. + */ static int test_lookup_refund_proof (uint64_t refund_serial, const struct @@ -5629,42 +5939,52 @@ test_lookup_refund_proof (uint64_t refund_serial, } +/** + * Closure for testing refund functionality. + */ struct TestRefunds_Closure { + /** + * The instance. + */ struct InstanceData instance; /** - * The merchant account + * The merchant account. */ struct TALER_MERCHANTDB_AccountDetails account; /** - * The exchange signing key + * The exchange signing key. */ struct ExchangeSignkeyData signkey; /** - * The order data + * The order data. */ struct OrderData order; /** - * The deposit data + * The deposit data. */ struct DepositData deposits[2]; /** - * The refund data + * The refund data. */ struct RefundData refund; /** - * The refund proof data + * The refund proof data. */ struct RefundProofData refund_proof; }; +/** + * Prepares for testing refunds. + * @param cls the closure to initialize. + */ static void pre_test_refunds (struct TestRefunds_Closure *cls) { @@ -5717,6 +6037,10 @@ pre_test_refunds (struct TestRefunds_Closure *cls) } +/** + * Cleans up after testing refunds. + * @param cls the closure. + */ static void post_test_refunds (struct TestRefunds_Closure *cls) { @@ -5725,6 +6049,12 @@ post_test_refunds (struct TestRefunds_Closure *cls) } +/** + * Runs the refund tests. + * @param cls the closure. + * + * @return 0 on success, 1 otherwise. + */ static int run_test_refunds (struct TestRefunds_Closure *cls) { @@ -5784,13 +6114,13 @@ run_test_refunds (struct TestRefunds_Closure *cls) cls->refund.reason), "Refund coin failed\n"); /* Test lookup refunds */ - TEST_RET_ON_FAIL (test_lookup_refunds (cls->instance.instance.id, + TEST_RET_ON_FAIL (test_lookup_refunds (&cls->instance, &cls->deposits[0].h_contract_terms, 1, cls->refund.coin_pub, &cls->refund.refund_amount)); /* Test lookup refunds detailed */ - TEST_RET_ON_FAIL (test_lookup_refunds_detailed (cls->instance.instance.id, + TEST_RET_ON_FAIL (test_lookup_refunds_detailed (&cls->instance, &cls->deposits[0]. h_contract_terms, 1, @@ -5845,8 +6175,13 @@ run_test_refunds (struct TestRefunds_Closure *cls) } +/** + * All logic for testing refunds. + * + * @return 0 on success, 1 otherwise. + */ static int -test_refunds (void *cls) +test_refunds (void) { struct TestRefunds_Closure test_cls; pre_test_refunds (&test_cls); @@ -5857,7 +6192,9 @@ test_refunds (void *cls) /** - * Convenience function for testing lookup orders with filters + * Convenience function that reverses an array of orders. + * @param orders_length length of @e orders. + * @param orders the array to reverse. */ static void reverse_order_data_array (unsigned int orders_length, @@ -5871,19 +6208,52 @@ reverse_order_data_array (unsigned int orders_length, } +/** + * Closure for testing all the filters for looking up orders. + */ struct TestLookupOrdersAllFilters_Closure { + /** + * The instance. + */ struct InstanceData instance; + + /** + * The merchant account. + */ struct TALER_MERCHANTDB_AccountDetails account; + + /** + * The exchange signing key. + */ struct ExchangeSignkeyData signkey; + /** + * The array of order ids. + */ char *order_ids[64]; + + /** + * The array of orders. + */ struct OrderData orders[64]; + + /** + * The array of deposits. + */ struct DepositData deposits[64]; + + /** + * The array of refunds. + */ struct RefundData refunds[64]; }; +/** + * Sets up for testing lookup order filters. + * @param cls the closure. + */ static void pre_test_lookup_orders_all_filters (struct TestLookupOrdersAllFilters_Closure *cls) @@ -5913,6 +6283,10 @@ pre_test_lookup_orders_all_filters (struct } +/** + * Cleans up after testing lookup order filters. + * @param cls the closure. + */ static void post_test_lookup_orders_all_filters (struct TestLookupOrdersAllFilters_Closure *cls) @@ -5926,6 +6300,12 @@ post_test_lookup_orders_all_filters (struct } +/** + * Runs the tests for lookup order filters. + * @param cls the closure. + * + * @return 0 on success, 1 otherwise. + */ static int run_test_lookup_orders_all_filters (struct TestLookupOrdersAllFilters_Closure *cls) @@ -6067,10 +6447,13 @@ run_test_lookup_orders_all_filters (struct } -/* This has to happen after everything else because it - depends on deposits, etc. */ +/** + * Handles all logic for testing lookup order filters. + * + * @return 0 on success, 1 otherwise. + */ static int -test_lookup_orders_all_filters (void *cls) +test_lookup_orders_all_filters (void) { struct TestLookupOrdersAllFilters_Closure test_cls; pre_test_lookup_orders_all_filters (&test_cls); @@ -6081,19 +6464,21 @@ test_lookup_orders_all_filters (void *cls) /** - * Function that runs all tests and returns 1 upon error, 0 on success. + * Function that runs all tests. + * + * @return 0 on success, 1 otherwise. */ static int -run_tests (void *cls) +run_tests (void) { TEST_RET_ON_FAIL (test_instances ()); TEST_RET_ON_FAIL (test_products ()); TEST_RET_ON_FAIL (test_orders ()); TEST_RET_ON_FAIL (test_deposits ()); TEST_RET_ON_FAIL (test_transfers ()); - TEST_RET_ON_FAIL (test_tips (cls)); - TEST_RET_ON_FAIL (test_refunds (cls)); - TEST_RET_ON_FAIL (test_lookup_orders_all_filters (cls)); + TEST_RET_ON_FAIL (test_tips ()); + TEST_RET_ON_FAIL (test_refunds ()); + TEST_RET_ON_FAIL (test_lookup_orders_all_filters ()); return 0; } @@ -6131,7 +6516,7 @@ run (void *cls) /* Run the preflight */ plugin->preflight (plugin->cls); - result = run_tests (cls); + result = run_tests (); /* Test dropping tables */ if (GNUNET_OK != plugin->drop_tables (plugin->cls)) @@ -6159,6 +6544,9 @@ run (void *cls) } +/** + * Entry point for the tests. + */ int main (int argc, char *const argv[]) -- cgit v1.2.3