From 922323b4104abfaef2a11fed4e5e8292344a3bcd Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 5 Jul 2015 16:55:01 +0200 Subject: misc bugfixes from tests --- src/util/json.c | 25 ++++++++----------------- src/util/test_json.c | 4 ++-- src/util/util.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/util/wireformats.c | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 87 insertions(+), 21 deletions(-) (limited to 'src/util') diff --git a/src/util/json.c b/src/util/json.c index 1c7cb7520..42f146463 100644 --- a/src/util/json.c +++ b/src/util/json.c @@ -89,11 +89,13 @@ TALER_json_from_abs (struct GNUNET_TIME_Absolute stamp) char *mystr; int ret; + GNUNET_assert (GNUNET_OK == + TALER_round_abs_time (&stamp)); if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) return json_string ("/never/"); ret = GNUNET_asprintf (&mystr, - "/%llu/", - (long long) (stamp.abs_value_us / (1000LL * 1000LL))); + "/Date(%llu)/", + (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL))); GNUNET_assert (ret > 0); j = json_string (mystr); GNUNET_free (mystr); @@ -320,7 +322,7 @@ TALER_json_to_amount (json_t *json, /** - * Parse given JSON object to Amount + * Parse given JSON object to absolute time. * * @param json the json object representing Amount * @param[out] abs where the amount has to be written @@ -331,9 +333,7 @@ TALER_json_to_abs (json_t *json, struct GNUNET_TIME_Absolute *abs) { const char *val; - size_t slen; unsigned long long int tval; - char *endp; val = json_string_value (json); if (NULL == val) @@ -341,14 +341,6 @@ TALER_json_to_abs (json_t *json, GNUNET_break_op (0); return GNUNET_SYSERR; } - slen = strlen (val); - if ( (slen <= 2) || - ('/' != val[0]) || - ('/' != val[slen - 1]) ) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } if ( (0 == strcasecmp (val, "/forever/")) || (0 == strcasecmp (val, @@ -357,10 +349,9 @@ TALER_json_to_abs (json_t *json, *abs = GNUNET_TIME_UNIT_FOREVER_ABS; return GNUNET_OK; } - tval = strtoull (&val[1], - &endp, - 10); - if (&val[slen - 1] != endp) + if (1 != sscanf (val, + "/Date(%llu)/", + &tval)) { GNUNET_break_op (0); return GNUNET_SYSERR; diff --git a/src/util/test_json.c b/src/util/test_json.c index 6d887f489..c48fe68bd 100644 --- a/src/util/test_json.c +++ b/src/util/test_json.c @@ -65,7 +65,7 @@ test_time () struct GNUNET_TIME_Absolute a2; a1 = GNUNET_TIME_absolute_get (); - a1.abs_value_us -= a1.abs_value_us % 1000000; /* round! */ + TALER_round_abs_time (&a1); j = TALER_json_from_abs (a1); GNUNET_assert (NULL != j); GNUNET_assert (GNUNET_OK == @@ -110,7 +110,7 @@ test_raw () TALER_json_to_data (j, blob2, i)); - GNUNET_assert (0 == + GNUNET_assert (0 == memcmp (blob, blob2, i)); diff --git a/src/util/util.c b/src/util/util.c index 6da2f3f78..1d8b09c4d 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -63,6 +63,46 @@ TALER_config_get_denom (struct GNUNET_CONFIGURATION_Handle *cfg, } +/** + * Round a time value so that it is suitable for transmission + * via JSON encodings. + * + * @param at time to round + * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if + * it was just now rounded + */ +int +TALER_round_abs_time (struct GNUNET_TIME_Absolute *at) +{ + if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) + return GNUNET_OK; + if (0 == at->abs_value_us % 1000000) + return GNUNET_OK; + at->abs_value_us -= at->abs_value_us % 1000000; + return GNUNET_NO; +} + + +/** + * Round a time value so that it is suitable for transmission + * via JSON encodings. + * + * @param rt time to round + * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if + * it was just now rounded + */ +int +TALER_round_rel_time (struct GNUNET_TIME_Relative *rt) +{ + if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) + return GNUNET_OK; + if (0 == rt->rel_value_us % 1000000) + return GNUNET_OK; + rt->rel_value_us -= rt->rel_value_us % 1000000; + return GNUNET_NO; +} + + /** * Load configuration by parsing all configuration * files in the given directory. diff --git a/src/util/wireformats.c b/src/util/wireformats.c index 7ea4fd067..88af4a438 100644 --- a/src/util/wireformats.c +++ b/src/util/wireformats.c @@ -330,7 +330,7 @@ validate_sepa (const json_t *wire) ((json_t *) wire, &error, JSON_STRICT, "{" - "s:s " /* type: "SEPA" */ + "s:s " /* TYPE: sepa */ "s:s " /* IBAN: iban */ "s:s " /* name: beneficiary name */ "s:s " /* BIC: beneficiary bank's BIC */ @@ -345,7 +345,6 @@ validate_sepa (const json_t *wire) "edate", &edate, "r", &r, "address", &address)); - EXITIF (0 != strcmp (type, "SEPA")); if (1 != validate_iban (iban)) { GNUNET_log (GNUNET_ERROR_TYPE_INFO, @@ -359,6 +358,20 @@ validate_sepa (const json_t *wire) } +/** + * Validate TEST account details. The "test" format is used + * for testing, so this validator does nothing. + * + * @param wire JSON with the TEST details + * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not + */ +static int +validate_test (const json_t *wire) +{ + return GNUNET_YES; +} + + /** * Handler for a wire format. */ @@ -392,9 +405,29 @@ TALER_json_validate_wireformat (const char *type, { static const struct FormatHandler format_handlers[] = { { "SEPA", &validate_sepa }, + { "TEST", &validate_test }, { NULL, NULL} }; unsigned int i; + char *stype; + json_error_t error; + + UNPACK_EXITIF (0 != json_unpack_ex + ((json_t *) wire, + &error, 0, + "{" + "s:s " /* TYPE: type */ + "}", + "type", &stype)); + if (0 != strcasecmp (type, + stype)) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Wireformat `%s' does not match mint's expected `%s' format\n", + stype, + type); + return GNUNET_NO; + } for (i=0;NULL != format_handlers[i].type;i++) if (0 == strcasecmp (format_handlers[i].type, @@ -404,6 +437,8 @@ TALER_json_validate_wireformat (const char *type, "Wireformat `%s' not supported\n", type); return GNUNET_NO; + EXITIF_exit: + return GNUNET_NO; } /* end of wireformats.c */ -- cgit v1.2.3