summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-07-05 16:55:01 +0200
committerChristian Grothoff <christian@grothoff.org>2015-07-05 16:55:01 +0200
commit922323b4104abfaef2a11fed4e5e8292344a3bcd (patch)
tree8ed3781141741e743d120180b7e785f6a38eca3c /src/util
parentf623214c1292b93fa8672361dfa978a506f4bef3 (diff)
downloadexchange-922323b4104abfaef2a11fed4e5e8292344a3bcd.tar.gz
exchange-922323b4104abfaef2a11fed4e5e8292344a3bcd.tar.bz2
exchange-922323b4104abfaef2a11fed4e5e8292344a3bcd.zip
misc bugfixes from tests
Diffstat (limited to 'src/util')
-rw-r--r--src/util/json.c25
-rw-r--r--src/util/test_json.c4
-rw-r--r--src/util/util.c40
-rw-r--r--src/util/wireformats.c39
4 files changed, 87 insertions, 21 deletions
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
@@ -64,6 +64,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,
@@ -360,6 +359,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.
*/
struct FormatHandler
@@ -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 */