added TALER_adult_age(struct TALER_AgeMask *mask)

This commit is contained in:
Özgür Kesim 2023-06-27 18:57:05 +02:00
parent 537206e49f
commit 9c3ddcbc18
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7
3 changed files with 43 additions and 2 deletions

View File

@ -622,6 +622,14 @@ TALER_get_lowest_age (
const struct TALER_AgeMask *mask, const struct TALER_AgeMask *mask,
uint8_t age); 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 * Handle to an external process that will assist
* with some JSON-to-JSON conversion. * with some JSON-to-JSON conversion.

View File

@ -23,6 +23,7 @@
#include "taler_signatures.h" #include "taler_signatures.h"
#include <gnunet/gnunet_json_lib.h> #include <gnunet/gnunet_json_lib.h>
#include <gcrypt.h> #include <gcrypt.h>
#include <stdint.h>
struct struct
#ifndef AGE_RESTRICTION_WITH_ECDSA #ifndef AGE_RESTRICTION_WITH_ECDSA
@ -746,7 +747,7 @@ TALER_parse_coarse_date (
/* calculate the limit date for the largest age group */ /* calculate the limit date for the largest age group */
localtime_r (&(time_t){time (NULL)}, &limit); 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)); GNUNET_assert (-1 != mktime (&limit));
if ((limit.tm_year < date.tm_year) if ((limit.tm_year < date.tm_year)

View File

@ -233,7 +233,7 @@ test_lowest (void)
{.age = 22, .expected = 21 }, {.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); uint8_t l = TALER_get_lowest_age (&mask, test[n].age);
printf ("lowest[%d] for age %d, expected lowest: %d, got: %d\n", 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 = { static struct TALER_AgeMask age_mask = {
.bits = 1 | 1 << 8 | 1 << 10 | 1 << 12 | 1 << 14 | 1 << 16 | 1 << 18 | 1 << 21 .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 ()) if (GNUNET_OK != test_dates ())
return 4; return 4;
if (GNUNET_OK != test_adult ())
return 5;
return 0; return 0;
} }