From 42decef957861689c41d16a0dcfa8af3d9052816 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 24 Jul 2021 09:00:35 +0200 Subject: fix #6939 in exchange --- src/json/json_helper.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- src/json/test_json.c | 4 +++- 2 files changed, 64 insertions(+), 2 deletions(-) (limited to 'src/json') diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 866c094d0..1f8d320bf 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -55,7 +55,7 @@ TALER_JSON_from_amount_nbo (const struct TALER_AmountNBO *amount) /** * Parse given JSON object to Amount * - * @param cls closure, NULL + * @param cls closure, expected currency, or NULL * @param root the json object representing data * @param[out] spec where to write the data * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error @@ -65,6 +65,7 @@ parse_amount (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) { + const char *currency = cls; struct TALER_Amount *r_amount = spec->ptr; (void) cls; @@ -80,13 +81,41 @@ parse_amount (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } + if ( (NULL != currency) && + (0 != + strcasecmp (currency, + r_amount->currency)) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } struct GNUNET_JSON_Specification TALER_JSON_spec_amount (const char *name, + const char *currency, struct TALER_Amount *r_amount) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_amount, + .cleaner = NULL, + .cls = (void *) currency, + .field = name, + .ptr = r_amount, + .ptr_size = 0, + .size_ptr = NULL + }; + + GNUNET_assert (NULL != currency); + return ret; +} + + +struct GNUNET_JSON_Specification +TALER_JSON_spec_amount_any (const char *name, + struct TALER_Amount *r_amount) { struct GNUNET_JSON_Specification ret = { .parser = &parse_amount, @@ -97,6 +126,7 @@ TALER_JSON_spec_amount (const char *name, .ptr_size = 0, .size_ptr = NULL }; + return ret; } @@ -114,6 +144,7 @@ parse_amount_nbo (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) { + const char *currency = cls; struct TALER_AmountNBO *r_amount = spec->ptr; const char *sv; @@ -134,13 +165,41 @@ parse_amount_nbo (void *cls, GNUNET_break_op (0); return GNUNET_SYSERR; } + if ( (NULL != currency) && + (0 != + strcasecmp (currency, + r_amount->currency)) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } struct GNUNET_JSON_Specification TALER_JSON_spec_amount_nbo (const char *name, + const char *currency, struct TALER_AmountNBO *r_amount) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_amount_nbo, + .cleaner = NULL, + .cls = (void *) currency, + .field = name, + .ptr = r_amount, + .ptr_size = 0, + .size_ptr = NULL + }; + + GNUNET_assert (NULL != currency); + return ret; +} + + +struct GNUNET_JSON_Specification +TALER_JSON_spec_amount_any_nbo (const char *name, + struct TALER_AmountNBO *r_amount) { struct GNUNET_JSON_Specification ret = { .parser = &parse_amount_nbo, @@ -151,6 +210,7 @@ TALER_JSON_spec_amount_nbo (const char *name, .ptr_size = 0, .size_ptr = NULL }; + return ret; } diff --git a/src/json/test_json.c b/src/json/test_json.c index bedea76ac..e312a4ed3 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c @@ -36,7 +36,9 @@ test_amount (void) struct TALER_Amount a1; struct TALER_Amount a2; struct GNUNET_JSON_Specification spec[] = { - TALER_JSON_spec_amount ("amount", &a2), + TALER_JSON_spec_amount ("amount", + "EUR", + &a2), GNUNET_JSON_spec_end () }; -- cgit v1.2.3