gnunet-svn
[Top][All Lists]
Advanced

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

[taler-exchange] branch master updated: fix off-by-one in coarse date ca


From: gnunet
Subject: [taler-exchange] branch master updated: fix off-by-one in coarse date calculation
Date: Tue, 05 Sep 2023 18:44:37 +0200

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 f4148925 fix off-by-one in coarse date calculation
     new 97309075 Merge branch 'master' of ssh://git.taler.net/exchange
f4148925 is described below

commit f4148925f1cb880f97af20b3af7ef10fa9d81d6c
Author: Özgür Kesim <oec-taler@kesim.org>
AuthorDate: Tue Sep 5 18:44:16 2023 +0200

    fix off-by-one in coarse date calculation
---
 src/util/age_restriction.c      | 16 ++++++----
 src/util/test_age_restriction.c | 68 ++++++++++++++++++++++++++++-------------
 2 files changed, 56 insertions(+), 28 deletions(-)

diff --git a/src/util/age_restriction.c b/src/util/age_restriction.c
index d8c6e4da..73c8255f 100644
--- a/src/util/age_restriction.c
+++ b/src/util/age_restriction.c
@@ -392,6 +392,7 @@ TALER_age_commitment_attest (
           &at,
           &attest->signature);
   }
+#undef sign
 
   return GNUNET_OK;
 }
@@ -441,6 +442,7 @@ TALER_age_commitment_verify (
                    &attest->signature,
                    &comm->keys[group - 1].pub);
   }
+#undef verify
 }
 
 
@@ -776,24 +778,26 @@ TALER_parse_coarse_date (
   GNUNET_assert (NULL !=mask);
   GNUNET_assert (NULL !=out);
 
-  if (NULL == strptime (in, "%Y-%0m-%0d", &date))
+  if (NULL == strptime (in, "%Y-%m-%d", &date))
   {
-    if (NULL == strptime (in, "%Y-%0m-00", &date))
+    if (NULL == strptime (in, "%Y-%m-00", &date))
       if (NULL == strptime (in, "%Y-00-00", &date))
         return GNUNET_SYSERR;
-
     /* turns out that the day is off by one in the last two cases */
     date.tm_mday += 1;
   }
 
-  seconds = mktime (&date);
+  seconds = timegm (&date);
   if (-1 == seconds)
     return GNUNET_SYSERR;
 
   /* calculate the limit date for the largest age group */
-  localtime_r (&(time_t){time (NULL)}, &limit);
+  {
+    time_t l = time (NULL);
+    localtime_r (&l, &limit);
+  }
   limit.tm_year -= TALER_adult_age (mask);
-  GNUNET_assert (-1 != mktime (&limit));
+  GNUNET_assert (-1 != timegm (&limit));
 
   if ((limit.tm_year < date.tm_year)
       || ((limit.tm_year == date.tm_year)
diff --git a/src/util/test_age_restriction.c b/src/util/test_age_restriction.c
index 53cacc6d..406d16ef 100644
--- a/src/util/test_age_restriction.c
+++ b/src/util/test_age_restriction.c
@@ -80,24 +80,24 @@ test_groups (void)
       .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 }
+      .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}
+      .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}
 
 
     }
@@ -135,7 +135,6 @@ test_dates (void)
   struct TALER_AgeMask mask = {
     .bits = 1 | 1 << 5 | 1 << 9 | 1 << 13 | 1 << 17 | 1 << 21
   };
-
   struct
   {
     char *date;
@@ -156,26 +155,51 @@ test_dates (void)
 
     /* These dates should be far enough for the near future so that
      * the expected values are correct. Will need adjustment in 2044 :) */
-    {.date = "2023-06-26", .expected = 19533, .ret = GNUNET_OK },
-    {.date = "2023-06-01", .expected = 19508, .ret = GNUNET_OK },
-    {.date = "2023-06-00", .expected = 19508, .ret = GNUNET_OK },
-    {.date = "2023-01-01", .expected = 19357, .ret = GNUNET_OK },
-    {.date = "2023-00-00", .expected = 19357, .ret = GNUNET_OK },
+    {.date = "2022-11-26", .expected = 19322, .ret = GNUNET_OK },
+    {.date = "2022-11-27", .expected = 19323, .ret = GNUNET_OK },
+    {.date = "2023-06-26", .expected = 19534, .ret = GNUNET_OK },
+    {.date = "2023-06-01", .expected = 19509, .ret = GNUNET_OK },
+    {.date = "2023-06-00", .expected = 19509, .ret = GNUNET_OK },
+    {.date = "2023-01-01", .expected = 19358, .ret = GNUNET_OK },
+    {.date = "2023-00-00", .expected = 19358, .ret = GNUNET_OK },
+
+    /* Special case: .date == NULL meands birthday == current date, which
+     * should be 21 years in the future.  We will set these values below in the
+     * loop */
+    {.date = NULL, .expected = 0, .ret = GNUNET_OK },
   };
+  char buf[256]={0};
 
   for (uint8_t t = 0; t < sizeof(test) / sizeof(test[0]); t++)
   {
     uint32_t d;
     enum GNUNET_GenericReturnValue ret;
+    char *date = test[t].date;
+
+    if (NULL == test[t].date)
+    {
+      /* Special case:  We set .date to the current date. */
+      time_t tn;
+      struct tm now;
+
+      time (&tn);
+      localtime_r (&tn, &now);
+      strftime (buf, sizeof(buf), "%Y-%m-%d", &now);
+      date = &buf[0];
+
+      /* The expected value is the number of days since 1970-01-01,
+       * counted simplistically */
+      test[t].expected = timegm (&now) / 60 / 60 / 24;
+    }
 
-    ret = TALER_parse_coarse_date (test[t].date,
+    ret = TALER_parse_coarse_date (date,
                                    &mask,
                                    &d);
     if (ret != test[t].ret)
     {
       printf (
         "dates[%d] for date `%s` expected parser to return: %d, got: %d\n",
-        t, test[t].date, test[t].ret, ret);
+        t, date, test[t].ret, ret);
       return GNUNET_SYSERR;
     }
 
@@ -186,12 +210,12 @@ test_dates (void)
     {
       printf (
         "dates[%d] for date `%s` expected value %d, but got %d\n",
-        t, test[t].date, test[t].expected, d);
+        t, date, test[t].expected, d);
       return GNUNET_SYSERR;
     }
 
     printf ("dates[%d] for date `%s` got expected value %d\n",
-            t, test[t].date, d);
+            t, date, d);
   }
 
   printf ("done with dates\n");

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