From 9c3ddcbc183a9a96f10cdb14f28258ea61f5f7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Tue, 27 Jun 2023 18:57:05 +0200 Subject: added TALER_adult_age(struct TALER_AgeMask *mask) --- src/include/taler_util.h | 8 ++++++++ src/util/age_restriction.c | 3 ++- src/util/test_age_restriction.c | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/include/taler_util.h b/src/include/taler_util.h index dc30fca31..e0473bff7 100644 --- a/src/include/taler_util.h +++ b/src/include/taler_util.h @@ -622,6 +622,14 @@ TALER_get_lowest_age ( const struct TALER_AgeMask *mask, uint8_t age); +/* @brief Get the lowest age for the largest age group + * + * @param mask the age mask + * @return lowest age for the largest age group + */ +#define TALER_adult_age(mask) \ + sizeof((mask)->bits) * 8 - __builtin_clz ((mask)->bits) - 1 + /** * Handle to an external process that will assist * with some JSON-to-JSON conversion. diff --git a/src/util/age_restriction.c b/src/util/age_restriction.c index 839ed7cd5..6f0702298 100644 --- a/src/util/age_restriction.c +++ b/src/util/age_restriction.c @@ -23,6 +23,7 @@ #include "taler_signatures.h" #include #include +#include struct #ifndef AGE_RESTRICTION_WITH_ECDSA @@ -746,7 +747,7 @@ TALER_parse_coarse_date ( /* 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); + limit.tm_year -= TALER_adult_age (mask); GNUNET_assert (-1 != mktime (&limit)); if ((limit.tm_year < date.tm_year) diff --git a/src/util/test_age_restriction.c b/src/util/test_age_restriction.c index 29e722acc..53cacc6d5 100644 --- a/src/util/test_age_restriction.c +++ b/src/util/test_age_restriction.c @@ -233,7 +233,7 @@ test_lowest (void) {.age = 22, .expected = 21 }, }; - for (uint8_t n = 0; n < 21; n++) + for (uint8_t n = 0; n < sizeof(test) / sizeof(test[0]); n++) { uint8_t l = TALER_get_lowest_age (&mask, test[n].age); printf ("lowest[%d] for age %d, expected lowest: %d, got: %d\n", @@ -246,6 +246,36 @@ test_lowest (void) } +enum GNUNET_GenericReturnValue +test_adult (void) +{ + struct { struct TALER_AgeMask mask; uint8_t expected; } + test[] = { + { .mask = {.bits = 1 | 1 << 2}, + .expected = 2 }, + { .mask = {.bits = 1 | 1 << 2 | 1 << 3}, + .expected = 3 }, + { .mask = {.bits = 1 | 1 << 3}, + .expected = 3 }, + { .mask = {.bits = 1 | 1 << 22}, + .expected = 22 }, + { .mask = {.bits = 1 | 1 << 10 | 1 << 16 | 1 << 22}, + .expected = 22 }, + }; + for (uint8_t n = 0; n < sizeof(test) / sizeof(test[0]); n++) + { + uint8_t l = TALER_adult_age (&test[n].mask); + printf ("adult[%d] for mask %s, expected: %d, got: %d\n", + n, TALER_age_mask_to_string (&test[n].mask), test[n].expected, l); + if (test[n].expected != l) + return GNUNET_SYSERR; + } + printf ("done with adult\n"); + + 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 }; @@ -381,6 +411,8 @@ main (int argc, } if (GNUNET_OK != test_dates ()) return 4; + if (GNUNET_OK != test_adult ()) + return 5; return 0; } -- cgit v1.2.3