gnunet-svn
[Top][All Lists]
Advanced

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

[taler-merchant] 130/277: added more instance tests and fixed mem leaks/


From: gnunet
Subject: [taler-merchant] 130/277: added more instance tests and fixed mem leaks/code style in tests
Date: Sun, 05 Jul 2020 20:50:43 +0200

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

grothoff pushed a commit to branch master
in repository merchant.

commit 709b002de430440833bd504b445345f0ad49d775
Author: Jonathan Buchanan <jonathan.russ.buchanan@gmail.com>
AuthorDate: Thu May 21 15:34:47 2020 -0400

    added more instance tests and fixed mem leaks/code style in tests
---
 src/backenddb/test_merchantdb.c | 313 +++++++++++++++++++++++++++++++++-------
 1 file changed, 264 insertions(+), 49 deletions(-)

diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index 30a0d88..4d2b66b 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -41,7 +41,40 @@ static struct TALER_MERCHANTDB_Plugin *plugin;
  */
 static int instance_count;
 
-void
+/**
+ * An array of instance settings found from the lookup method.
+ */
+static struct TALER_MERCHANTDB_InstanceSettings instances_found[8];
+
+static void
+copy_instance (struct TALER_MERCHANTDB_InstanceSettings *dest,
+               const struct TALER_MERCHANTDB_InstanceSettings *src)
+{
+  dest->id = GNUNET_malloc (sizeof(char) * (strlen (src->id) + 1));
+  strcpy (dest->id, src->id);
+  dest->name = GNUNET_malloc (sizeof(char) * (strlen (src->name) + 1));
+  strcpy (dest->name, src->name);
+  dest->address = json_copy (src->address);
+  dest->jurisdiction = json_copy (src->jurisdiction);
+  dest->default_max_deposit_fee = src->default_max_deposit_fee;
+  dest->default_max_wire_fee = src->default_max_wire_fee;
+  dest->default_wire_fee_amortization = src->default_wire_fee_amortization;
+  dest->default_wire_transfer_delay = src->default_wire_transfer_delay;
+  dest->default_pay_delay = src->default_pay_delay;
+}
+
+
+static void
+free_instance (struct TALER_MERCHANTDB_InstanceSettings *is)
+{
+  GNUNET_free (is->id);
+  GNUNET_free (is->name);
+  json_decref (is->address);
+  json_decref (is->jurisdiction);
+}
+
+
+static void
 lookup_instances_cb (void *cls,
                      const struct TALER_MerchantPublicKeyP *merchant_pub,
                      const struct TALER_MerchantPrivateKeyP *merchant_priv,
@@ -50,12 +83,42 @@ lookup_instances_cb (void *cls,
                      const struct TALER_MERCHANTDB_AccountDetails accounts[])
 {
   instance_count += 1;
+  if (8 > instance_count)
+  {
+    /* Duplicate the instance settings */
+    copy_instance (&instances_found[instance_count - 1], is);
+  }
 }
 
 
-int
-check_products_equal (struct TALER_MERCHANTDB_ProductDetails *a,
-                      struct TALER_MERCHANTDB_ProductDetails *b)
+static int
+check_instances_equal (const struct TALER_MERCHANTDB_InstanceSettings *a,
+                       const struct TALER_MERCHANTDB_InstanceSettings *b)
+{
+  if ((0 != strcmp (a->id, b->id)) ||
+      (0 != strcmp (a->name, b->name)) ||
+      (1 != json_equal (a->address, b->address)) ||
+      (1 != json_equal (a->jurisdiction, b->jurisdiction)) ||
+      (GNUNET_OK != TALER_amount_cmp_currency (&a->default_max_deposit_fee,
+                                               &b->default_max_deposit_fee)) ||
+      (0 != TALER_amount_cmp (&a->default_max_deposit_fee,
+                              &b->default_max_deposit_fee)) ||
+      (GNUNET_OK != TALER_amount_cmp_currency (&a->default_max_wire_fee,
+                                               &b->default_max_wire_fee)) ||
+      (0 != TALER_amount_cmp (&a->default_max_wire_fee,
+                              &b->default_max_wire_fee)) ||
+      (a->default_wire_fee_amortization != b->default_wire_fee_amortization) ||
+      (a->default_wire_transfer_delay.rel_value_us !=
+       b->default_wire_transfer_delay.rel_value_us) ||
+      (a->default_pay_delay.rel_value_us != b->default_pay_delay.rel_value_us))
+    return 1;
+  return 0;
+}
+
+
+static int
+check_products_equal (const struct TALER_MERCHANTDB_ProductDetails *a,
+                      const struct TALER_MERCHANTDB_ProductDetails *b)
 {
   if ((0 != strcmp (a->description, b->description)) ||
       (1 != json_equal (a->description_i18n, b->description_i18n)) ||
@@ -75,44 +138,29 @@ check_products_equal (struct 
TALER_MERCHANTDB_ProductDetails *a,
 
 
 /**
- * Main function that will be run by the scheduler.
+ * Function that tests instances.
  *
  * @param cls closure with config
  */
-static void
-run (void *cls)
+static int
+test_instances (void *cls)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
-  /* Data for 'store_payment()' */
-
-  /* Load the plugin */
-  if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
-  {
-    result = 77;
-    return;
-  }
-
-  /* Run the preflight */
-  plugin->preflight (plugin->cls);
-
   /* Test lookup instances- there should be nothing here */
   instance_count = 0;
   if (0 > plugin->lookup_instances (plugin->cls, false,
-                                    lookup_instances_cb, cls))
+                                    &lookup_instances_cb, cls))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup instances failed\n");
-    result = 1;
-    plugin->drop_tables (plugin->cls); /* Try to drop tables if possible to 
clean up for the next test */
-    return;
+    plugin->drop_tables (plugin->cls);   /* Try to drop tables if possible to 
clean up for the next test */
+    return 1;
   }
   if (instance_count != 0)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Non-zero number of instances found after initialization\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
 
   /* Test making an instance */
@@ -122,14 +170,14 @@ run (void *cls)
   GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
                                       &merchant_pub.eddsa_pub);
   struct TALER_MERCHANTDB_InstanceSettings is;
-  is.id = "test_instance_0";
+  is.id = "test_inst";
   is.name = "Test";
   is.address = json_array ();
   json_array_append (is.address, json_string ("123 Example St"));
   is.jurisdiction = json_array ();
   json_array_append (is.jurisdiction, json_string ("Ohio"));
-  TALER_string_to_amount ("USD:1200.40", &is.default_max_deposit_fee);
-  TALER_string_to_amount ("USD:1200.40", &is.default_max_wire_fee);
+  TALER_string_to_amount ("EUR:1200.40", &is.default_max_deposit_fee);
+  TALER_string_to_amount ("EUR:1200.40", &is.default_max_wire_fee);
   is.default_wire_fee_amortization = 1;
   is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
   is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
@@ -138,29 +186,35 @@ run (void *cls)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Instance insertion failed\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
 
   /* Test lookup instances- is our new instance there? */
   instance_count = 0;
   if (0 > plugin->lookup_instances (plugin->cls, false,
-                                    lookup_instances_cb, cls))
+                                    &lookup_instances_cb, cls))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup instances failed\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
   if (instance_count != 1)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Instance count doesn't match number of instances inserted\n");
-    result = 1;
-    return;
+    plugin->drop_tables (plugin->cls);
+    return 1;
   }
+  if (0 != check_instances_equal (&is, &instances_found[0]))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup instance failed: incorrect instance returned\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  free_instance (&instances_found[0]);
 
   /* Test update instance */
   is.name = "Test - updated";
@@ -168,9 +222,129 @@ run (void *cls)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Update instance failed\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  instance_count = 0;
+  if (0 > plugin->lookup_instances (plugin->cls, false,
+                                    &lookup_instances_cb, cls))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup instances failed\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  if (instance_count != 1)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Update instance failed: Instance count doesn't match number 
of instances inserted/updated\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  if (0 != check_instances_equal (&is, &instances_found[0]))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Update instance failed: result from lookup doesn't match\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  free_instance (&instances_found[0]);
+
+  /* Test instance private key deletion */
+  if (0 > plugin->delete_instance_private_key (plugin->cls, "test_inst"))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Delete instance private key failed\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  instance_count = 0;
+  if (0 > plugin->lookup_instances (plugin->cls, true,
+                                    &lookup_instances_cb, cls))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup instances failed\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  if (0 != instance_count)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup instances failed: instance without private key not 
excluded when only counting active instances\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  instance_count = 0;
+  if (0 > plugin->lookup_instances (plugin->cls, false,
+                                    &lookup_instances_cb, cls))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup instances failed\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  if (1 != instance_count)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup instances failed: instance without private key not 
included when counting all instances\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
+  }
+  /* Delete the instance we just copied */
+  free_instance (&instances_found[0]);
+
+  /* Test instance deletion */
+  /* This test currently FAILS */
+  /*instance_count = 0;
+  if (0 > plugin->purge_instance (plugin->cls, "test_inst"))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Purge instance failed\n");
     result = 1;
     plugin->drop_tables (plugin->cls);
     return;
+  }*/
+
+  json_decref (is.address);
+  json_decref (is.jurisdiction);
+
+  return 0;
+}
+
+
+/**
+ * Function that tests products.
+ *
+ * @param cls closure with config
+ */
+static int
+test_products (void *cls)
+{
+  /* Test making an instance */
+  struct TALER_MerchantPublicKeyP merchant_pub;
+  struct TALER_MerchantPrivateKeyP merchant_priv;
+  GNUNET_CRYPTO_eddsa_key_create (&merchant_priv.eddsa_priv);
+  GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
+                                      &merchant_pub.eddsa_pub);
+  struct TALER_MERCHANTDB_InstanceSettings is;
+  is.id = "test_instance_0";
+  is.name = "Test";
+  is.address = json_array ();
+  json_array_append (is.address, json_string ("123 Example St"));
+  is.jurisdiction = json_array ();
+  json_array_append (is.jurisdiction, json_string ("Ohio"));
+  TALER_string_to_amount ("USD:1200.40", &is.default_max_deposit_fee);
+  TALER_string_to_amount ("USD:1200.40", &is.default_max_wire_fee);
+  is.default_wire_fee_amortization = 1;
+  is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
+  is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
+  if (0 > plugin->insert_instance (plugin->cls, &merchant_pub, &merchant_priv,
+                                   &is))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Instance insertion failed\n");
+    plugin->drop_tables (plugin->cls);
+    return 1;
   }
 
   /* Test creating a product */
@@ -186,26 +360,24 @@ run (void *cls)
   pd.image = json_array ();
   pd.address = json_array ();
   pd.next_restock = GNUNET_TIME_absolute_get_zero_ ();
-  if (0 > plugin->insert_product (plugin->cls, "test_instance_0", "is_0_pd_0",
+  if (0 > plugin->insert_product (plugin->cls, "test_instance_1", "is_0_pd_0",
                                   &pd))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Insert product failed\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
 
   /* Test lookup of individual products */
   struct TALER_MERCHANTDB_ProductDetails lookup_pd;
-  if (0 > plugin->lookup_product (plugin->cls, "test_instance_0", "is_0_pd_0",
+  if (0 > plugin->lookup_product (plugin->cls, "test_instance_1", "is_0_pd_0",
                                   &lookup_pd))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup product failed\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
   /* This test currently FAILS */
   /*if (0 != check_products_equal(&pd, &lookup_pd)) {
@@ -217,27 +389,30 @@ run (void *cls)
   }*/
 
   /* Make sure it fails correctly for products that don't exist */
-  if (0 != plugin->lookup_product (plugin->cls, "test_instance_0",
+  if (0 != plugin->lookup_product (plugin->cls, "test_instance_1",
                                    "fictional_product", &lookup_pd))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup product failed: product returned where there was 
none\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
 
   /* Test product deletion */
-  if (0 > plugin->delete_product (plugin->cls, "test_instance_0", "is_0_pd_0"))
+  if (0 > plugin->delete_product (plugin->cls, "test_instance_1", "is_0_pd_0"))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Delete product failed\n");
-    result = 1;
     plugin->drop_tables (plugin->cls);
-    return;
+    return 1;
   }
 
-  /* Test instance deletion */
+  json_decref (pd.description_i18n);
+  json_decref (pd.taxes);
+  json_decref (pd.image);
+  json_decref (pd.address);
+
+  /* Clean up: delete the instance */
   /* This test currently FAILS */
   /*instance_count = 0;
   if (0 > plugin->purge_instance(plugin->cls, "t")) {
@@ -248,6 +423,44 @@ run (void *cls)
     return;
   }*/
 
+  json_decref (is.address);
+  json_decref (is.jurisdiction);
+
+  return 0;
+}
+
+
+/**
+ * Main function that will be run by the scheduler.
+ *
+ * @param cls closure with config
+ */
+static void
+run (void *cls)
+{
+  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  /* Data for 'store_payment()' */
+
+  /* Load the plugin */
+  if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
+  {
+    result = 77;
+    return;
+  }
+
+  /* Run the preflight */
+  plugin->preflight (plugin->cls);
+
+  /* Test instances */
+  result = test_instances (cls);
+  if (0 != result)
+    return;
+
+  /* Test products */
+  result = test_products (cls);
+  if (0 != result)
+    return;
+
   /* Test dropping tables */
   if (GNUNET_OK != plugin->drop_tables (plugin->cls))
   {
@@ -261,6 +474,8 @@ run (void *cls)
   TALER_MERCHANTDB_plugin_unload (plugin);
   if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Plugin unload failed\n");
     result = 77;
     return;
   }

-- 
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]