gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] 151/277: backenddb tests for lookup_transfers, lookup_t


From: gnunet
Subject: [taler-merchant] 151/277: backenddb tests for lookup_transfers, lookup_transfer_summary, lookup_transfer_details
Date: Sun, 05 Jul 2020 20:51:04 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository merchant.

commit f1e1ff81885ed8605006a351cbde888456bddc09
Author: Jonathan Buchanan <jonathan.russ.buchanan@gmail.com>
AuthorDate: Fri May 29 14:46:42 2020 -0400

    backenddb tests for lookup_transfers, lookup_transfer_summary, 
lookup_transfer_details
---
 src/backenddb/drop0001.sql                 |   2 +
 src/backenddb/plugin_merchantdb_postgres.c |  24 +-
 src/backenddb/test_merchantdb.c            | 548 +++++++++++++++++++++++------
 3 files changed, 453 insertions(+), 121 deletions(-)

diff --git a/src/backenddb/drop0001.sql b/src/backenddb/drop0001.sql
index 5295feb..aec588d 100644
--- a/src/backenddb/drop0001.sql
+++ b/src/backenddb/drop0001.sql
@@ -43,6 +43,8 @@ DROP TABLE IF EXISTS merchant_credits CASCADE;
 DROP TABLE IF EXISTS merchant_transfers CASCADE;
 DROP TABLE IF EXISTS merchant_transfer_signatures CASCADE;
 DROP TABLE IF EXISTS merchant_transfer_by_coin CASCADE;
+DROP TABLE IF EXISTS merchant_transfer_to_coin CASCADE;
+DROP TABLE IF EXISTS merchant_deposit_to_transfer CASCADE;
 DROP TABLE IF EXISTS merchant_tip_reserves CASCADE;
 DROP TABLE IF EXISTS merchant_tip_reserve_keys CASCADE;
 DROP TABLE IF EXISTS merchant_tips CASCADE;
diff --git a/src/backenddb/plugin_merchantdb_postgres.c 
b/src/backenddb/plugin_merchantdb_postgres.c
index ae4fc35..c58a947 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -2874,10 +2874,10 @@ RETRY:
   for (unsigned int i = 0; i<td->details_length; i++)
   {
     const struct TALER_TrackTransferDetails *d = &td->details[i];
-    uint32_t i32 = (uint32_t) i;
+    uint64_t i64 = (uint64_t) i;
     struct GNUNET_PQ_QueryParam params[] = {
       GNUNET_PQ_query_param_uint64 (&credit_serial),
-      GNUNET_PQ_query_param_uint32 (&i32),
+      GNUNET_PQ_query_param_uint64 (&i64),
       TALER_PQ_query_param_amount (&d->coin_value),
       TALER_PQ_query_param_amount (&d->coin_fee), /* deposit fee */
       GNUNET_PQ_query_param_auto_from_type (&d->coin_pub),
@@ -3381,10 +3381,10 @@ lookup_transfer_details_cb (void *cls,
 
   for (unsigned int i = 0; i<num_results; i++)
   {
-    uint32_t current_offset;
+    uint64_t current_offset;
     struct TALER_TrackTransferDetails ttd;
     struct GNUNET_PQ_ResultSpec rs[] = {
-      GNUNET_PQ_result_spec_uint32 ("offset_in_exchange_list",
+      GNUNET_PQ_result_spec_uint64 ("offset_in_exchange_list",
                                     &current_offset),
       GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
                                             &ttd.h_contract_terms),
@@ -3882,12 +3882,12 @@ lookup_reserves_cb (void *cls,
     struct TALER_Amount exchange_initial_balance;
     struct TALER_Amount pickup_amount;
     struct TALER_Amount committed_amount;
-    uint8_t active;
+    uint8_t active = 0;
     struct GNUNET_PQ_ResultSpec rs[] = {
       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
                                             &reserve_pub),
-      GNUNET_PQ_result_spec_absolute_time ("creation_time",
-                                           &creation_time),
+      /*GNUNET_PQ_result_spec_absolute_time ("creation_time",
+                                           &creation_time),*/
       GNUNET_PQ_result_spec_absolute_time ("expiration",
                                            &expiration_time),
       TALER_PQ_RESULT_SPEC_AMOUNT ("merchant_initial_balance",
@@ -3898,8 +3898,8 @@ lookup_reserves_cb (void *cls,
                                    &committed_amount),
       TALER_PQ_RESULT_SPEC_AMOUNT ("tips_picked_up",
                                    &pickup_amount),
-      GNUNET_PQ_result_spec_auto_from_type ("active",
-                                            &active),
+      /*GNUNET_PQ_result_spec_auto_from_type ("active",
+                                            &active),*/
       GNUNET_PQ_result_spec_end
     };
 
@@ -4079,7 +4079,7 @@ postgres_lookup_reserve (void *cls,
   struct TALER_Amount exchange_initial_balance;
   struct TALER_Amount pickup_amount;
   struct TALER_Amount committed_amount;
-  uint8_t active;
+  /*uint8_t active;*/
   struct GNUNET_PQ_ResultSpec rs[] = {
     GNUNET_PQ_result_spec_absolute_time ("creation_time",
                                          &creation_time),
@@ -4093,8 +4093,8 @@ postgres_lookup_reserve (void *cls,
                                  &pickup_amount),
     TALER_PQ_RESULT_SPEC_AMOUNT ("tips_committed",
                                  &committed_amount),
-    GNUNET_PQ_result_spec_auto_from_type ("active",
-                                          &active),
+    /*GNUNET_PQ_result_spec_auto_from_type ("active",
+                                          &active),*/
     GNUNET_PQ_result_spec_end
   };
   enum GNUNET_DB_QueryStatus qs;
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index 5012e95..70e20a6 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -2005,6 +2005,13 @@ test_deposits (void *cls)
 }
 
 
+struct TransferWithDetails
+{
+  struct TALER_WireTransferIdentifierRawP wtid;
+  struct TALER_EXCHANGE_TransferData details;
+};
+
+
 static int
 test_lookup_transfer (const char *exchange_url,
                       const struct TALER_WireTransferIdentifierRawP *wtid,
@@ -2076,9 +2083,9 @@ lookup_transfer_summary_cb (void *cls,
                                                deposit_fee)) &&
       (0 == TALER_amount_cmp (cmp->deposit_fee,
                               deposit_fee)))
-    cmp->result = 1;
-  else
     cmp->result = 0;
+  else
+    cmp->result = 1;
 }
 
 
@@ -2093,7 +2100,8 @@ test_lookup_transfer_summary (const char *exchange_url,
   struct TestLookupTransferSummary_Closure cmp = {
     .order_id = expected_order_id,
     .deposit_value = expected_deposit_value,
-    .deposit_fee = expected_deposit_fee
+    .deposit_fee = expected_deposit_fee,
+    .result = 0
   };
   if (1 != plugin->lookup_transfer_summary (plugin->cls,
                                             exchange_url,
@@ -2105,7 +2113,7 @@ test_lookup_transfer_summary (const char *exchange_url,
                 "Lookup transfer summary failed\n");
     return 1;
   }
-  if (1 != cmp.result)
+  if (0 != cmp.result)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup transfer summary failed: mismatched data\n");
@@ -2129,9 +2137,29 @@ lookup_transfer_details_cb (void *cls,
                             unsigned int current_offset,
                             const struct TALER_TrackTransferDetails *details)
 {
-  if (NULL == cls)
-    return;
   struct TestLookupTransferDetails_Closure *cmp = cls;
+  if (NULL == cmp)
+    return;
+  for (unsigned int i = 0; cmp->details_to_cmp_length > i; ++i)
+  {
+    if ((0 == GNUNET_memcmp (&cmp->details_to_cmp[i].h_contract_terms,
+                             &details->h_contract_terms)) &&
+        (0 == GNUNET_memcmp (&cmp->details_to_cmp[i].coin_pub,
+                             &details->coin_pub)) &&
+        (GNUNET_OK == TALER_amount_cmp_currency (
+           &cmp->details_to_cmp[i].coin_value,
+           &details->coin_value)) &&
+        (0 == TALER_amount_cmp (&cmp->details_to_cmp[i].coin_value,
+                                &details->coin_value)) &&
+        (GNUNET_OK == TALER_amount_cmp_currency (
+           &cmp->details_to_cmp[i].coin_fee,
+           &details->coin_fee)) &&
+        (0 == TALER_amount_cmp (&cmp->details_to_cmp[i].coin_fee,
+                                &details->coin_fee)))
+    {
+      cmp->results_matching[i] += 1;
+    }
+  }
   cmp->results_length += 1;
 }
 
@@ -2144,13 +2172,13 @@ test_lookup_transfer_details (const char *exchange_url,
                               const struct TALER_TrackTransferDetails *details)
 {
   unsigned int results_matching[details_length];
-  memset (results_matching, 0, sizeof (unsigned int) * details_length);
   struct TestLookupTransferDetails_Closure cmp = {
     .details_to_cmp_length = details_length,
     .details_to_cmp = details,
     .results_matching = results_matching,
     .results_length = 0
   };
+  memset (results_matching, 0, sizeof (unsigned int) * details_length);
   if (1 != plugin->lookup_transfer_details (plugin->cls,
                                             exchange_url,
                                             wtid,
@@ -2231,6 +2259,105 @@ test_lookup_wire_fee (const struct 
TALER_MasterPublicKeyP *master_pub,
 }
 
 
+struct TestLookupTransfers_Closure
+{
+  unsigned int transfers_to_cmp_length;
+
+  const struct TransferWithDetails *transfers_to_cmp;
+
+  unsigned int *results_matching;
+
+  unsigned int results_length;
+};
+
+
+static void
+lookup_transfers_cb (void *cls,
+                     const struct TALER_Amount *credit_amount,
+                     const struct TALER_WireTransferIdentifierRawP *wtid,
+                     const char *payto_uri,
+                     const char *exchange_url,
+                     uint64_t transfer_serial_id,
+                     struct GNUNET_TIME_Absolute execution_time,
+                     bool verified,
+                     bool confirmed)
+{
+  struct TestLookupTransfers_Closure *cmp = cls;
+  if (NULL == cmp)
+    return;
+  for (unsigned int i = 0; cmp->transfers_to_cmp_length > i; ++i)
+  {
+    if ((GNUNET_OK ==
+         TALER_amount_cmp_currency (
+           &cmp->transfers_to_cmp[i].details.total_amount,
+           credit_amount)) &&
+        (0 == TALER_amount_cmp (&cmp->transfers_to_cmp[i].details.total_amount,
+                                credit_amount)) &&
+        (cmp->transfers_to_cmp[i].details.execution_time.abs_value_us ==
+         execution_time.abs_value_us))
+    {
+      cmp->results_matching[i] += 1;
+    }
+  }
+  cmp->results_length += 1;
+}
+
+
+static int
+test_lookup_transfers (const char *instance_id,
+                       const char *payto_uri,
+                       struct GNUNET_TIME_Absolute before,
+                       struct GNUNET_TIME_Absolute after,
+                       int64_t limit,
+                       uint64_t offset,
+                       enum TALER_MERCHANTDB_YesNoAll filter_verified,
+                       unsigned int transfers_length,
+                       const struct TransferWithDetails *transfers)
+{
+  unsigned int results_matching[transfers_length];
+  struct TestLookupTransfers_Closure cmp = {
+    .transfers_to_cmp_length = transfers_length,
+    .transfers_to_cmp = transfers,
+    .results_matching = results_matching,
+    .results_length = 0
+  };
+  memset (results_matching,
+          0,
+          sizeof (unsigned int) * transfers_length);
+  if (1 != plugin->lookup_transfers (plugin->cls,
+                                     instance_id,
+                                     payto_uri,
+                                     before,
+                                     after,
+                                     limit,
+                                     offset,
+                                     filter_verified,
+                                     &lookup_transfers_cb,
+                                     &cmp))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup transfers failed\n");
+    return 1;
+  }
+  if (transfers_length != cmp.results_length)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup transfers failed: incorrect number of results\n");
+    return 1;
+  }
+  for (unsigned int i = 0; transfers_length > i; ++i)
+  {
+    if (1 != cmp.results_matching[i])
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Lookup transfers failed: mismatched data\n");
+      return 1;
+    }
+  }
+  return 0;
+}
+
+
 struct TestTransfers_Closure
 {
   /**
@@ -2297,6 +2424,16 @@ struct TestTransfers_Closure
    * The exchange signkey end date
    */
   struct GNUNET_TIME_Absolute signkey_end;
+
+  /**
+   * The order data
+   */
+  struct OrderData order;
+
+  /**
+   * The deposit data
+   */
+  struct DepositData deposit;
 };
 
 
@@ -2310,6 +2447,17 @@ pre_test_transfers (struct TestTransfers_Closure *cls)
     }
 
 
+  };
+  struct GNUNET_TIME_Absolute pay_deadline;
+  struct GNUNET_TIME_Absolute refund_deadline;
+  struct TALER_CoinSpendPrivateKeyP coin_priv;
+  struct TALER_DepositRequestPS deposit_sign = {
+    .purpose = {
+      .size = htonl (sizeof (struct TALER_DepositRequestPS)),
+      .purpose = htonl (TALER_SIGNATURE_WALLET_COIN_DEPOSIT)
+    }
+
+
   };
 
   /* Instance */
@@ -2344,6 +2492,64 @@ pre_test_transfers (struct TestTransfers_Closure *cls)
   cls->account.payto_uri = "payto://x-taler-bank/bank.demo.taler.net/4";
   cls->account.active = true;
 
+  /* Order */
+  pay_deadline = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
+                                           GNUNET_TIME_UNIT_DAYS);
+  refund_deadline = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
+                                              GNUNET_TIME_UNIT_WEEKS);
+  GNUNET_TIME_round_abs (&pay_deadline);
+  GNUNET_TIME_round_abs (&refund_deadline);
+  cls->order.id = "test_orders_od_1";
+  cls->order.pay_deadline = pay_deadline;
+  cls->order.contract = json_object ();
+  json_object_set (cls->order.contract,
+                   "fulfillment_url",
+                   json_string ("a"));
+  json_object_set (cls->order.contract,
+                   "pay_deadline",
+                   GNUNET_JSON_from_time_abs (pay_deadline));
+  json_object_set (cls->order.contract,
+                   "refund_deadline",
+                   GNUNET_JSON_from_time_abs (refund_deadline));
+
+  /* Deposit */
+  cls->deposit.timestamp = GNUNET_TIME_absolute_get ();
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_JSON_hash (cls->order.contract,
+                                  &cls->deposit.h_contract_terms));
+  GNUNET_CRYPTO_eddsa_key_create (&coin_priv.eddsa_priv);
+  GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
+                                      &cls->deposit.coin_pub.eddsa_pub);
+  cls->deposit.exchange_url = "test-exchange";
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:50.00",
+                                         &cls->deposit.amount_with_fee));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:1.00",
+                                         &cls->deposit.deposit_fee));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:1.50",
+                                         &cls->deposit.refund_fee));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:2.00",
+                                         &cls->deposit.wire_fee));
+  cls->deposit.h_wire = cls->account.h_wire;
+  deposit_sign.h_contract_terms = cls->deposit.h_contract_terms;
+  deposit_sign.h_wire = cls->deposit.h_wire;
+  deposit_sign.wallet_timestamp = GNUNET_TIME_absolute_hton (
+    GNUNET_TIME_absolute_get ());
+  deposit_sign.refund_deadline = GNUNET_TIME_absolute_hton (
+    GNUNET_TIME_absolute_get ());
+  TALER_amount_hton (&deposit_sign.amount_with_fee,
+                     &cls->deposit.amount_with_fee);
+  TALER_amount_hton (&deposit_sign.deposit_fee,
+                     &cls->deposit.deposit_fee);
+  deposit_sign.merchant = cls->merchant_pub;
+  deposit_sign.coin_pub = cls->deposit.coin_pub;
+  GNUNET_CRYPTO_eddsa_sign (&cls->exchange_priv.eddsa_priv,
+                            &deposit_sign,
+                            &cls->deposit.exchange_sig.eddsa_signature);
+
   /* Signing key */
   GNUNET_CRYPTO_eddsa_key_create (&cls->exchange_priv.eddsa_priv);
   GNUNET_CRYPTO_eddsa_key_get_public (&cls->exchange_priv.eddsa_priv,
@@ -2365,6 +2571,8 @@ post_test_transfers (struct TestTransfers_Closure *cls)
 {
   json_decref (cls->is.address);
   json_decref (cls->is.jurisdiction);
+
+  json_decref (cls->order.contract);
 }
 
 
@@ -2381,6 +2589,15 @@ run_test_transfers (struct TestTransfers_Closure *cls)
     .exchange_pub = cls->exchange_pub,
     .execution_time = GNUNET_TIME_absolute_get (),
   };
+  struct TALER_TrackTransferDetails transfer_detail = {
+    .h_contract_terms = cls->deposit.h_contract_terms,
+    .coin_pub = cls->deposit.coin_pub,
+    .coin_value = cls->deposit.amount_with_fee,
+    .coin_fee = cls->deposit.deposit_fee
+  };
+  struct TransferWithDetails full_transfer_data = {
+    .wtid = wtid
+  };
 
   /* Insert the instance */
   TEST_RET_ON_FAIL (test_insert_instance (&cls->merchant_pub,
@@ -2391,9 +2608,35 @@ run_test_transfers (struct TestTransfers_Closure *cls)
   TEST_RET_ON_FAIL (test_insert_account (cls->is.id,
                                          &cls->account));
 
+  /* Insert a signing key */
+  TEST_RET_ON_FAIL (test_insert_exchange_signkey (&cls->master_pub,
+                                                  &cls->exchange_pub,
+                                                  cls->signkey_start,
+                                                  cls->signkey_expire,
+                                                  cls->signkey_end,
+                                                  &cls->master_sig));
+
+  /* Insert an order */
+  TEST_RET_ON_FAIL (test_insert_order (cls->is.id,
+                                       cls->order.id,
+                                       cls->order.pay_deadline,
+                                       cls->order.contract));
+
+  /* Insert contract terms */
+  TEST_RET_ON_FAIL (test_insert_contract_terms (cls->is.id,
+                                                cls->order.id,
+                                                cls->order.contract));
+
+  /* Insert the deposit */
+  TEST_RET_ON_FAIL (test_insert_deposit (cls->is.id,
+                                         &cls->exchange_pub,
+                                         &cls->deposit));
+  transfer_data.details_length = 1;
+  transfer_data.details = &transfer_detail;
+
   /* Insert the transfer */
   GNUNET_assert (GNUNET_OK ==
-                 TALER_string_to_amount ("EUR:13",
+                 TALER_string_to_amount ("EUR:50.00",
                                          &amount));
   if (1 != plugin->insert_transfer (plugin->cls,
                                     cls->is.id,
@@ -2408,17 +2651,9 @@ run_test_transfers (struct TestTransfers_Closure *cls)
     return 1;
   }
 
-  /* Insert a signing key */
-  TEST_RET_ON_FAIL (test_insert_exchange_signkey (&cls->master_pub,
-                                                  &cls->exchange_pub,
-                                                  cls->signkey_start,
-                                                  cls->signkey_expire,
-                                                  cls->signkey_end,
-                                                  &cls->master_sig));
-
   /* Test transfer details */
   GNUNET_assert (GNUNET_OK ==
-                 TALER_string_to_amount ("EUR:13",
+                 TALER_string_to_amount ("EUR:50.00",
                                          &transfer_data.total_amount));
   GNUNET_assert (GNUNET_OK ==
                  TALER_string_to_amount ("EUR:0.49",
@@ -2466,21 +2701,29 @@ run_test_transfers (struct TestTransfers_Closure *cls)
                                           true));
 
   /* Test lookup transfer summary */
-  /*
   TEST_RET_ON_FAIL (test_lookup_transfer_summary (exchange_url,
                                                   &wtid,
-                                                  "",
-                                                  &amount,
-                                                  &transfer_data.wire_fee));
-  */
+                                                  cls->order.id,
+                                                  
&cls->deposit.amount_with_fee,
+                                                  &cls->deposit.deposit_fee));
 
   /* Test lookup transfer details */
-  /*
   TEST_RET_ON_FAIL (test_lookup_transfer_details (exchange_url,
                                                   &wtid,
-                                                  0,
-                                                  NULL));
-  */
+                                                  1,
+                                                  &transfer_detail));
+
+  /* Test lookup transfers */
+  full_transfer_data.details = transfer_data;
+  TEST_RET_ON_FAIL (test_lookup_transfers (cls->is.id,
+                                           cls->account.payto_uri,
+                                           GNUNET_TIME_UNIT_FOREVER_ABS,
+                                           GNUNET_TIME_UNIT_ZERO_ABS,
+                                           1,
+                                           0,
+                                           TALER_MERCHANTDB_YNA_ALL,
+                                           1,
+                                           &full_transfer_data));
 
   return 0;
 }
@@ -2497,19 +2740,38 @@ test_transfers (void *cls)
 }
 
 
-struct TestLookupReserve_Callback
+struct ReserveData
 {
-  const struct GNUNET_TIME_Absolute *creation_time;
+  /**
+   * The reserve public key
+   */
+  struct TALER_ReservePublicKeyP reserve_pub;
 
-  const struct GNUNET_TIME_Absolute *expiration_time;
+  /**
+   * The reserve private key
+   */
+  struct TALER_ReservePrivateKeyP reserve_priv;
 
-  const struct TALER_Amount *merchant_initial_amount;
+  /**
+   * The reserve initial amount
+   */
+  struct TALER_Amount initial_amount;
 
-  const struct TALER_Amount *exchange_initial_amount;
+  /**
+   * The exchange url
+   */
+  const char *exchange_url;
 
-  const struct TALER_Amount *picked_up_amount;
+  /**
+   * The expiration date
+   */
+  struct GNUNET_TIME_Absolute expiration;
+};
 
-  const struct TALER_Amount *committed_amount;
+
+struct TestLookupReserve_Closure
+{
+  const struct ReserveData *reserve_to_cmp;
 
   unsigned int tips_length;
 
@@ -2530,35 +2792,45 @@ lookup_reserve_cb (void *cls,
                    unsigned int tips_length,
                    const struct TALER_MERCHANTDB_TipDetails *tips)
 {
-  struct TestLookupReserve_Callback *cmp = cls;
+  struct TestLookupReserve_Closure *cmp = cls;
+  unsigned int tip_cmp_results[tips_length];
   if (NULL == cmp)
     return;
-  if ((cmp->creation_time->abs_value_us != creation_time.abs_value_us) ||
-      (cmp->expiration_time->abs_value_us != expiration_time.abs_value_us) ||
-      (GNUNET_OK != TALER_amount_cmp_currency (cmp->merchant_initial_amount,
-                                               merchant_initial_amount)) ||
-      (0 != TALER_amount_cmp (cmp->merchant_initial_amount,
+  if ((cmp->reserve_to_cmp->expiration.abs_value_us !=
+       expiration_time.abs_value_us) ||
+      (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)) ||
-      (GNUNET_OK != TALER_amount_cmp_currency (cmp->exchange_initial_amount,
-                                               exchange_initial_amount)) ||
-      (0 != TALER_amount_cmp (cmp->exchange_initial_amount,
-                              exchange_initial_amount)) ||
-      (GNUNET_OK != TALER_amount_cmp_currency (cmp->picked_up_amount,
-                                               picked_up_amount)) ||
-      (0 != TALER_amount_cmp (cmp->picked_up_amount,
-                              picked_up_amount)) ||
-      (GNUNET_OK != TALER_amount_cmp_currency (cmp->committed_amount,
-                                               committed_amount)) ||
-      (0 != TALER_amount_cmp (cmp->committed_amount,
-                              committed_amount)) ||
       (cmp->tips_length != tips_length))
   {
     cmp->result_matches = 1;
     return;
   }
+  memset (tip_cmp_results, 0, sizeof (unsigned int) * tips_length);
+  for (unsigned int i = 0; tips_length > i; ++i)
+  {
+    for (unsigned int j = 0; tips_length > j; ++j)
+    {
+      if ((GNUNET_OK == TALER_amount_cmp_currency (&cmp->tips[i].total_amount,
+                                                   &tips[j].total_amount)) &&
+          (0 == TALER_amount_cmp (&cmp->tips[i].total_amount,
+                                  &tips[j].total_amount)) &&
+          (0 == strcmp (cmp->tips[i].reason,
+                        tips[j].reason)))
+      {
+        tip_cmp_results[i] += 1;
+      }
+    }
+  }
   for (unsigned int i = 0; tips_length > i; ++i)
   {
-    /* TODO: compare tips */
+    if (1 != tip_cmp_results[i])
+    {
+      cmp->result_matches = 1;
+      return;
+    }
   }
   cmp->result_matches = 0;
 }
@@ -2567,25 +2839,13 @@ lookup_reserve_cb (void *cls,
 static int
 test_lookup_reserve (const char *instance_id,
                      const struct TALER_ReservePublicKeyP *reserve_pub,
-                     const struct GNUNET_TIME_Absolute *expected_creation_time,
-                     const struct
-                     GNUNET_TIME_Absolute *expected_expiration_time,
-                     const struct TALER_Amount *expected_merchant_amount,
-                     const struct TALER_Amount *expected_exchange_amount,
-                     const struct TALER_Amount *expected_picked_up_amount,
-                     const struct TALER_Amount *expected_committed_amount,
-                     unsigned int expected_tips_length,
-                     const struct TALER_MERCHANTDB_TipDetails *expected_tips)
-{
-  struct TestLookupReserve_Callback cmp = {
-    .creation_time = expected_creation_time,
-    .expiration_time = expected_expiration_time,
-    .merchant_initial_amount = expected_merchant_amount,
-    .exchange_initial_amount = expected_exchange_amount,
-    .picked_up_amount = expected_picked_up_amount,
-    .committed_amount = expected_committed_amount,
-    .tips_length = expected_tips_length,
-    .tips = expected_tips
+                     const struct ReserveData *reserve)
+{
+  struct TestLookupReserve_Closure cmp = {
+    .reserve_to_cmp = reserve,
+    .tips_length = 0,
+    .tips = NULL,
+    .result_matches = 0
   };
   if (1 != plugin->lookup_reserve (plugin->cls,
                                    instance_id,
@@ -2608,35 +2868,94 @@ test_lookup_reserve (const char *instance_id,
 }
 
 
-struct ReserveData
+struct TestLookupReserves_Closure
 {
-  /**
-   * The reserve public key
-   */
-  struct TALER_ReservePublicKeyP reserve_pub;
-
-  /**
-   * The reserve private key
-   */
-  struct TALER_ReservePrivateKeyP reserve_priv;
+  unsigned int reserves_to_cmp_length;
 
-  /**
-   * The reserve initial amount
-   */
-  struct TALER_Amount initial_amount;
+  const struct ReserveData *reserves_to_cmp;
 
-  /**
-   * The exchange url
-   */
-  const char *exchange_url;
+  unsigned int *results_matching;
 
-  /**
-   * The expiration date
-   */
-  struct GNUNET_TIME_Absolute expiration;
+  unsigned int results_length;
 };
 
 
+static void
+lookup_reserves_cb (void *cls,
+                    const struct TALER_ReservePublicKeyP *reserve_pub,
+                    struct GNUNET_TIME_Absolute creation_time,
+                    struct GNUNET_TIME_Absolute 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 ((cmp->reserves_to_cmp[i].expiration.abs_value_us ==
+         expiration_time.abs_value_us) ||
+        (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;
+}
+
+
+static int
+test_lookup_reserves (const char *instance_id,
+                      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_id,
+                                    GNUNET_TIME_absolute_get_zero_ (),
+                                    TALER_MERCHANTDB_YNA_ALL,
+                                    TALER_MERCHANTDB_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;
+}
+
+
 struct TestTips_Closure
 {
   /**
@@ -2733,23 +3052,9 @@ run_test_tips (struct TestTips_Closure *cls)
   }
 
   /* Test lookup reserve */
-  /*
-  struct GNUNET_TIME_Absolute creation = GNUNET_TIME_absolute_get ();
-  struct TALER_Amount zero_amount;
-  GNUNET_assert (GNUNET_OK ==
-                 TALER_amount_get_zero ("EUR",
-                                        &zero_amount));
   TEST_RET_ON_FAIL (test_lookup_reserve (cls->is.id,
-                                         &cls->reserve_pub,
-                                         &creation,
-                                         &expiration,
-                                         &initial_amount,
-                                         &zero_amount,
-                                         &zero_amount,
-                                         &zero_amount,
-                                         0,
-                                         NULL));
-  */
+                                         &cls->reserve.reserve_pub,
+                                         &cls->reserve));
 
   /* Test inserting a tip */
   /*
@@ -2775,6 +3080,31 @@ run_test_tips (struct TestTips_Closure *cls)
   }
   */
 
+  /* Test lookup reserves */
+  TEST_RET_ON_FAIL (test_lookup_reserves (cls->is.id,
+                                          1,
+                                          &cls->reserve));
+
+  /* Test delete reserve private key */
+  if (1 != plugin->delete_reserve (plugin->cls,
+                                   cls->is.id,
+                                   &cls->reserve.reserve_pub))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Delete reserve private key failed\n");
+    return 1;
+  }
+
+  /* Test purging a reserve */
+  if (1 != plugin->purge_reserve (plugin->cls,
+                                  cls->is.id,
+                                  &cls->reserve.reserve_pub))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Purge reserve failed\n");
+    return 1;
+  }
+
   return 0;
 }
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]