From cafbbb5994d333323e79c02ecd4700d82ff4242c Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Thu, 12 Jan 2017 19:23:12 +0100 Subject: [PATCH] auction: add pricemap validation --- src/auction/gnunet-auction-create.c | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/auction/gnunet-auction-create.c b/src/auction/gnunet-auction-create.c index c21e93f26..dc43f635e 100644 --- a/src/auction/gnunet-auction-create.c +++ b/src/auction/gnunet-auction-create.c @@ -24,6 +24,9 @@ * @author Markus Teich */ #include "platform.h" + +#include + #include "gnunet_util_lib.h" #include "gnunet_json_lib.h" /* #include "gnunet_auction_service.h" */ @@ -56,6 +59,13 @@ run (void *cls, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { + unsigned int i; + double cur, prev = DBL_MAX; + json_t *pmap; + json_t *parray; + json_t *pnode; + json_error_t jerr; + /* cmdline parsing */ if (GNUNET_TIME_UNIT_ZERO.rel_value_us == dstart.rel_value_us) { @@ -82,6 +92,48 @@ run (void *cls, goto fail; } + /* parse and check pricemap validity */ + if (!(pmap = json_load_file (fnprices, JSON_DECODE_INT_AS_REAL, &jerr))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "parsing pricemap json at %d:%d: %s\n", + jerr.line, jerr.column, jerr.text); + goto fail; + } + if (-1 == json_unpack_ex (pmap, &jerr, JSON_VALIDATE_ONLY, + "{s:s, s:[]}", "currency", "prices")) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "validating pricemap: %s\n", jerr.text); + goto fail; + } + if (!(parray = json_object_get (pmap, "prices")) || !json_is_array (parray)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "could not get `prices` array node from pricemap\n"); + goto fail; + } + json_array_foreach (parray, i, pnode) + { + if (-1 == json_unpack_ex (pnode, &jerr, 0, "F", &cur)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "validating pricearray index %d: %s\n", i, jerr.text); + goto fail; + } + if (prev <= cur) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "validating pricearray index %d: " + "prices must be strictly monotonically decreasing\n", + i); + goto fail; + } + prev = cur; + } + + return; + fail: ret = 1; return; -- 2.25.1