gnunet-svn
[Top][All Lists]
Advanced

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

[taler-exchange] branch master updated: [age restriction] added unit tes


From: gnunet
Subject: [taler-exchange] branch master updated: [age restriction] added unit test for get_age_group
Date: Fri, 04 Mar 2022 17:00:36 +0100

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

oec pushed a commit to branch master
in repository exchange.

The following commit(s) were added to refs/heads/master by this push:
     new d0b27833 [age restriction] added unit test for get_age_group
d0b27833 is described below

commit d0b27833b2f59b4238d67bc4c058244daae85daf
Author: Özgür Kesim <oec-taler@kesim.org>
AuthorDate: Fri Mar 4 17:00:28 2022 +0100

    [age restriction] added unit test for get_age_group
---
 src/util/test_age_restriction.c | 122 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 115 insertions(+), 7 deletions(-)

diff --git a/src/util/test_age_restriction.c b/src/util/test_age_restriction.c
index a047714f..0b182bd4 100644
--- a/src/util/test_age_restriction.c
+++ b/src/util/test_age_restriction.c
@@ -23,20 +23,126 @@
 #include "taler_util.h"
 #include "taler_crypto_lib.h"
 
-static struct TALER_AgeMask age_mask = {
-  .bits = 1 | 1 << 8 | 1 << 10 | 1 << 12 | 1 << 14 | 1 << 16 | 1 << 18 | 1 << 
21
-};
-
 extern uint8_t
 get_age_group (
   const struct TALER_AgeMask *mask,
   uint8_t age);
 
+/**
+ * Encodes the age mask into a string, like "8:10:12:14:16:18:21"
+ *
+ * @param mask Age mask
+ * @return String representation of the age mask, allocated by GNUNET_malloc.
+ *         Can be used as value in the TALER config.
+ */
+char *
+age_mask_to_string (
+  const struct TALER_AgeMask *m)
+{
+  uint32_t bits = m->bits;
+  unsigned int n = 0;
+  char *buf = GNUNET_malloc (32 * 3); // max characters possible
+  char *pos = buf;
+
+  if (NULL == buf)
+  {
+    return buf;
+  }
+
+  while (bits != 0)
+  {
+    bits >>= 1;
+    n++;
+    if (0 == (bits & 1))
+    {
+      continue;
+    }
+
+    if (n > 9)
+    {
+      *(pos++) = '0' + n / 10;
+    }
+    *(pos++) = '0' + n % 10;
+
+    if (0 != (bits >> 1))
+    {
+      *(pos++) = ':';
+    }
+  }
+  return buf;
+}
+
+
+enum GNUNET_GenericReturnValue
+test_groups (void)
+{
+  struct
+  {
+    uint32_t bits;
+    uint8_t group[33];
+  } test[] = {
+    {
+      .bits =
+        1 | 1 << 5 | 1 << 13 | 1 << 23,
+
+        .group = { 0, 0, 0, 0, 0,
+                   1, 1, 1, 1, 1, 1, 1, 1,
+                   2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }
+
+
+    },
+    {
+      .bits =
+        1 | 1 << 8 | 1 << 10 | 1 << 12 | 1 << 14 | 1 << 16 | 1 << 18 | 1 << 21,
+        .group = { 0, 0, 0, 0, 0, 0, 0, 0,
+                   1, 1,
+                   2, 2,
+                   3, 3,
+                   4, 4,
+                   5, 5,
+                   6, 6, 6,
+                   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}
+
+
+    }
+  };
+
+  for (uint8_t t = 0; t < sizeof(test) / sizeof(test[0]); t++)
+  {
+    struct TALER_AgeMask mask = {.bits = test[t].bits};
+
+    for (uint8_t i = 0; i < 32; i++)
+    {
+      uint8_t r = get_age_group (&mask, i);
+      char *m = age_mask_to_string (&mask);
+
+      printf ("get_age_group(%s, %2d) = %d vs %d (exp)\n",
+              m,
+              i,
+              r,
+              test[t].group[i]);
+
+      if (test[t].group[i] != r)
+        return GNUNET_SYSERR;
+
+      GNUNET_free (m);
+    }
+  }
+
+  return GNUNET_OK;
+}
+
+
+static struct TALER_AgeMask age_mask = {
+  .bits = 1 | 1 << 8 | 1 << 10 | 1 << 12 | 1 << 14 | 1 << 16 | 1 << 18 | 1 << 
21
+};
+
 enum GNUNET_GenericReturnValue
 test_attestation (void)
 {
   uint8_t age;
-  for (age = 0; age < 35; age++)
+  for (age = 0; age < 33; age++)
   {
     enum GNUNET_GenericReturnValue ret;
     struct TALER_AgeCommitmentProof acp[3] = {0};
@@ -57,9 +163,9 @@ test_attestation (void)
       acp[0].proof.num,
       age_group);
 
+    /* Also derive two more commitments right away */
     for (uint8_t i = 0; i<2; i++)
     {
-      /* Also derive another commitment right away */
       salt = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
                                        UINT64_MAX);
       GNUNET_assert (GNUNET_OK ==
@@ -126,8 +232,10 @@ main (int argc,
 {
   (void) argc;
   (void) argv;
-  if (GNUNET_OK != test_attestation ())
+  if (GNUNET_OK != test_groups ())
     return 1;
+  if (GNUNET_OK != test_attestation ())
+    return 2;
   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]