From cdc0f43f359821b12faa74f9823ba091c2897ee3 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 17 May 2015 17:57:31 +0200 Subject: slightly expand wireformat test to cover error cases --- src/util/test_wireformats.c | 55 ++++++++++++++++++++++++++++++++++++++------- src/util/wireformats.c | 19 +++++++++++----- 2 files changed, 60 insertions(+), 14 deletions(-) (limited to 'src/util') diff --git a/src/util/test_wireformats.c b/src/util/test_wireformats.c index 26ec4b79f..ebb96604c 100644 --- a/src/util/test_wireformats.c +++ b/src/util/test_wireformats.c @@ -24,7 +24,8 @@ #include "taler_util.h" #include "taler_json_lib.h" -static const char * const json_wire_str = +/* Valid SEPA data */ +static const char * const valid_wire_str = "{ \"type\":\"SEPA\", \ \"IBAN\":\"DE67830654080004822650\", \ \"name\":\"GNUnet e.V.\", \ @@ -33,7 +34,40 @@ static const char * const json_wire_str = \"r\":123456789, \ \"address\": \"foobar\"}"; -int main(int argc, const char *const argv[]) +/* IBAN has wrong country code */ +static const char * const invalid_wire_str = + "{ \"type\":\"SEPA\", \ +\"IBAN\":\"XX67830654080004822650\", \ +\"name\":\"GNUnet e.V.\", \ +\"bic\":\"GENODEF1SLR\", \ +\"edate\":\"1449930207000\", \ +\"r\":123456789, \ +\"address\": \"foobar\"}"; + +/* IBAN has wrong checksum */ +static const char * const invalid_wire_str2 = + "{ \"type\":\"SEPA\", \ +\"IBAN\":\"DE67830654080004822651\", \ +\"name\":\"GNUnet e.V.\", \ +\"bic\":\"GENODEF1SLR\", \ +\"edate\":\"1449930207000\", \ +\"r\":123456789, \ +\"address\": \"foobar\"}"; + +/* Unsupported wireformat type */ +static const char * const unsupported_wire_str = + "{ \"type\":\"unsupported\", \ +\"IBAN\":\"DE67830654080004822650\", \ +\"name\":\"GNUnet e.V.\", \ +\"bic\":\"GENODEF1SLR\", \ +\"edate\":\"1449930207000\", \ +\"r\":123456789, \ +\"address\": \"foobar\"}"; + + +int +main(int argc, + const char *const argv[]) { json_t *wire; json_error_t error; @@ -41,13 +75,18 @@ int main(int argc, const char *const argv[]) GNUNET_log_setup ("test-json-validations", "WARNING", NULL); (void) memset(&error, 0, sizeof(error)); - wire = json_loads (json_wire_str, 0, &error); - if (NULL == wire) - { - TALER_json_warn (error); - return 2; - } + GNUNET_assert (NULL != (wire = json_loads (unsupported_wire_str, 0, NULL))); + GNUNET_assert (1 != TALER_json_validate_wireformat ("unsupported", wire)); + json_decref (wire); + GNUNET_assert (NULL != (wire = json_loads (invalid_wire_str, 0, NULL))); + GNUNET_assert (1 != TALER_json_validate_wireformat ("SEPA", wire)); + json_decref (wire); + GNUNET_assert (NULL != (wire = json_loads (invalid_wire_str2, 0, NULL))); + GNUNET_assert (1 != TALER_json_validate_wireformat ("SEPA", wire)); + json_decref (wire); + GNUNET_assert (NULL != (wire = json_loads (valid_wire_str, 0, &error))); ret = TALER_json_validate_wireformat ("SEPA", wire); + json_decref (wire); if (1 == ret) return 0; return 1; diff --git a/src/util/wireformats.c b/src/util/wireformats.c index e86044d7e..7ea4fd067 100644 --- a/src/util/wireformats.c +++ b/src/util/wireformats.c @@ -297,10 +297,11 @@ validate_iban (const char *iban) dividend += remainder * (pow (10, nread)); remainder = dividend % 97; } - EXITIF (1 != remainder); - GNUNET_free (nbuf); - return GNUNET_YES; - + if (1 == remainder) + { + GNUNET_free (nbuf); + return GNUNET_YES; + } EXITIF_exit: GNUNET_free (nbuf); return GNUNET_NO; @@ -345,7 +346,13 @@ validate_sepa (const json_t *wire) "r", &r, "address", &address)); EXITIF (0 != strcmp (type, "SEPA")); - EXITIF (1 != validate_iban (iban)); + if (1 != validate_iban (iban)) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "IBAN `%s' invalid\n", + iban); + return GNUNET_NO; + } return GNUNET_YES; EXITIF_exit: return GNUNET_NO; @@ -393,7 +400,7 @@ TALER_json_validate_wireformat (const char *type, if (0 == strcasecmp (format_handlers[i].type, type)) return format_handlers[i].handler (wire); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Wireformat `%s' not supported\n", type); return GNUNET_NO; -- cgit v1.2.3