From f9394ba74208eacf5e4d7a0ca354394d4a34c982 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Fri, 23 Sep 2016 17:35:31 +0200 Subject: [PATCH] test_brandt: actually check the outcome correctnes --- test_brandt.c | 129 +++++++++++++++++++++++++++++++++++++++++--------- test_crypto.c | 4 +- 2 files changed, 108 insertions(+), 25 deletions(-) diff --git a/test_brandt.c b/test_brandt.c index e56f076..da74af2 100644 --- a/test_brandt.c +++ b/test_brandt.c @@ -38,10 +38,49 @@ struct msg { }; +struct test { + uint16_t ret; + uint16_t m; + uint16_t outcome_public; +}; + + static uint16_t *id; +static uint16_t *result_called; static struct BRANDT_Auction **ad; -static uint16_t bidders = 3; -static uint16_t prizes = 8; +static uint16_t unexpected_result; + +/* TEST CONFIGURATION */ +static const uint16_t bidders = 4; +static const uint16_t prizes = 3; +static const uint16_t bids[] = { 1, 2, 0, 2 }; + + +static struct BRANDT_Result * +expected_outcome (uint16_t m, uint16_t outcome_public, uint16_t i) +{ + int32_t highest_bidder = -1; + int32_t highest_bid = -1; + struct BRANDT_Result *ret; + + for (uint16_t i = 0; i < bidders; i++) + { + if (bids[i] > highest_bid) + { + highest_bid = bids[i]; + highest_bidder = i; + } + } + + if (!outcome_public && !(i == highest_bidder || i == bidders)) + return NULL; + + ret = GNUNET_new (struct BRANDT_Result); + ret->bidder = highest_bidder; + ret->price = highest_bid; + ret->status = BRANDT_bidder_won; + return ret; +} static void @@ -134,27 +173,49 @@ cb_result (void *auction_closure, struct BRANDT_Result results[], uint16_t results_len) { - uint16_t *s = (uint16_t *)auction_closure; + uint16_t *s = (uint16_t *)auction_closure; + struct BRANDT_Result *must = expected_outcome (ad[*s]->m, + ad[*s]->outcome_public, + *s); if (0 == results_len) - weprintf ("result from agent %d: none", *s); + { + weprintf ("result from agent %d: %p", *s, must); + weprintf ("expected result is: (nil)"); + + if (NULL != must) + unexpected_result = 1; + } for (uint16_t i = 0; i < results_len; i++) + { weprintf ("result from agent %d: bidder %d got status %d with price %d", *s, results[i].bidder, results[i].status, results[i].price); + weprintf ("expected result is: bidder %d got status %d with price %d", + must[i].bidder, + must[i].status, + must[i].price); + + if (NULL == must || + must[i].bidder != results[i].bidder || + must[i].status != results[i].status || + must[i].price != results[i].price) + unexpected_result = 1; + } + result_called[*s] = 1; } static void -run_new_join (void *arg) +run_auction (void *arg) { - int *ret = arg; - const char description[] = "test description for test_new_join"; - void *desc; - size_t desc_len; + struct test *asetup = arg; + const char description[] = "test description for test_auctions"; + void *desc; + size_t desc_len; ad = GNUNET_new_array (bidders + 1, struct BRANDT_Auction *); @@ -168,9 +229,9 @@ run_new_join (void *arg) sizeof (description), GNUNET_TIME_absolute_get (), GNUNET_TIME_UNIT_MINUTES, - prizes, /* amount of possible prizes */ - 0, /* m */ - 1); /* outcome public */ + prizes, /* number of prizes */ + asetup->m, /* m */ + asetup->outcome_public); /* outcome public */ if (!ad[bidders]) { weprintf ("BRANDT_new() failed."); @@ -187,7 +248,7 @@ run_new_join (void *arg) desc_len, description, sizeof (description), - 3); /* bid */ + bids[i]); /* bid */ if (!ad[i]) { weprintf ("BRANDT_join() failed."); @@ -208,26 +269,48 @@ run_new_join (void *arg) _exit (1); } } - - *ret = 1; } static int -test_new_join () +test_auctions () { - int ret = 0; - id = GNUNET_new_array (bidders + 1, uint16_t); for (uint16_t i = 0; i <= bidders; i++) id[i] = i; - GNUNET_SCHEDULER_run (&run_new_join, &ret); + for (size_t atype = 0; atype < auction_last; atype++) + { + if (auction_firstPrice != atype) /* others not yet implemented */ + continue; - for (uint16_t i = 0; i <= bidders; i++) - BRANDT_destroy (ad[i]); + for (size_t oc = 0; oc < outcome_last; oc++) + { + struct test asetup; - return ret; + result_called = GNUNET_new_array (bidders + 1, uint16_t); + unexpected_result = 0; + asetup.ret = 0; + asetup.m = atype; + asetup.outcome_public = oc; + GNUNET_SCHEDULER_run (&run_auction, &asetup); + + for (uint16_t i = 0; i <= bidders; i++) + { + BRANDT_destroy (ad[i]); + if (!result_called[i]) + { + weprintf ("result callback not called for bidder %d", i); + unexpected_result = 1; + } + } + + if (unexpected_result) + return 0; + } + } + + return 1; } @@ -239,7 +322,7 @@ main (int argc, char *argv[]) edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16); BRANDT_init (edc); - RUN (test_new_join); + RUN (test_auctions); GNUNET_CRYPTO_ecc_dlog_release (edc); return ret; diff --git a/test_crypto.c b/test_crypto.c index cfc6160..c2f5231 100644 --- a/test_crypto.c +++ b/test_crypto.c @@ -191,7 +191,7 @@ test_setup_auction_data () ad[i].n = bidders; ad[i].i = i; ad[i].k = prizes; - ad[i].b = 2 * i; + ad[i].b = i; } return 1; } @@ -330,7 +330,7 @@ main (int argc, char *argv[]) struct GNUNET_CRYPTO_EccDlogContext *edc; bidders = 3; - prizes = 6; + prizes = 3; edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16); BRANDT_init (edc);