diff options
author | Özgür Kesim <oec-taler@kesim.org> | 2023-06-27 09:45:50 +0200 |
---|---|---|
committer | Özgür Kesim <oec-taler@kesim.org> | 2023-06-27 09:45:50 +0200 |
commit | 145310e20e1a80d7c3a4a76a27f1e415bea7b3ce (patch) | |
tree | 0ab96ce9160504ce2df2171f3a4cb9a2de068339 /src/util | |
parent | ddd0e0af136f694db6013abd0344f20c1931bad9 (diff) | |
download | exchange-145310e20e1a80d7c3a4a76a27f1e415bea7b3ce.tar.gz exchange-145310e20e1a80d7c3a4a76a27f1e415bea7b3ce.tar.bz2 exchange-145310e20e1a80d7c3a4a76a27f1e415bea7b3ce.zip |
added birthdate parser
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/age_restriction.c | 53 | ||||
-rw-r--r-- | src/util/test_age_restriction.c | 73 |
2 files changed, 126 insertions, 0 deletions
diff --git a/src/util/age_restriction.c b/src/util/age_restriction.c index b667fa3a9..839ed7cd5 100644 --- a/src/util/age_restriction.c +++ b/src/util/age_restriction.c @@ -710,4 +710,57 @@ TALER_age_restriction_from_secret ( } +enum GNUNET_GenericReturnValue +TALER_parse_coarse_date ( + const char *in, + const struct TALER_AgeMask *mask, + uint32_t *out) +{ + struct tm date = {0}; + struct tm limit = {0}; + time_t seconds; + + if (NULL == in) + { + /* FIXME[oec]: correct behaviour? */ + *out = 0; + return GNUNET_OK; + } + + GNUNET_assert (NULL !=mask); + GNUNET_assert (NULL !=out); + + if (NULL == strptime (in, "%Y-%0m-%0d", &date)) + { + if (NULL == strptime (in, "%Y-%0m-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); + if (-1 == seconds) + return GNUNET_SYSERR; + + /* calculate the limit date for the largest age group */ + localtime_r (&(time_t){time (NULL)}, &limit); + limit.tm_year -= TALER_get_lowest_age (mask, 255); + GNUNET_assert (-1 != mktime (&limit)); + + if ((limit.tm_year < date.tm_year) + || ((limit.tm_year == date.tm_year) + && (limit.tm_mon < date.tm_mon)) + || ((limit.tm_year == date.tm_year) + && (limit.tm_mon == date.tm_mon) + && (limit.tm_mday < date.tm_mday))) + *out = seconds / 60 / 60 / 24; + else + *out = 0; + + return GNUNET_OK; +} + + /* end util/age_restriction.c */ diff --git a/src/util/test_age_restriction.c b/src/util/test_age_restriction.c index e1979314e..29e722acc 100644 --- a/src/util/test_age_restriction.c +++ b/src/util/test_age_restriction.c @@ -130,6 +130,77 @@ test_groups (void) enum GNUNET_GenericReturnValue +test_dates (void) +{ + struct TALER_AgeMask mask = { + .bits = 1 | 1 << 5 | 1 << 9 | 1 << 13 | 1 << 17 | 1 << 21 + }; + + struct + { + char *date; + uint32_t expected; + enum GNUNET_GenericReturnValue ret; + } + test [] = { + {.date = "abcd-00-00", .expected = 0, .ret = GNUNET_SYSERR}, + {.date = "1900-00-01", .expected = 0, .ret = GNUNET_SYSERR}, + {.date = "19000001", .expected = 0, .ret = GNUNET_SYSERR}, + {.date = "2001-33-05", .expected = 0, .ret = GNUNET_SYSERR}, + {.date = "2001-33-35", .expected = 0, .ret = GNUNET_SYSERR}, + + {.date = "1900-00-00", .expected = 0, .ret = GNUNET_OK}, + {.date = "2001-00-00", .expected = 0, .ret = GNUNET_OK}, + {.date = "2001-03-00", .expected = 0, .ret = GNUNET_OK}, + {.date = "2001-03-05", .expected = 0, .ret = GNUNET_OK}, + + /* 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 }, + }; + + for (uint8_t t = 0; t < sizeof(test) / sizeof(test[0]); t++) + { + uint32_t d; + enum GNUNET_GenericReturnValue ret; + + ret = TALER_parse_coarse_date (test[t].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); + return GNUNET_SYSERR; + } + + if (ret == GNUNET_SYSERR) + continue; + + if (d != test[t].expected) + { + printf ( + "dates[%d] for date `%s` expected value %d, but got %d\n", + t, test[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); + } + + printf ("done with dates\n"); + + return GNUNET_OK; +} + + +enum GNUNET_GenericReturnValue test_lowest (void) { struct TALER_AgeMask mask = { @@ -308,6 +379,8 @@ main (int argc, GNUNET_break (0); return 3; } + if (GNUNET_OK != test_dates ()) + return 4; return 0; } |