2016-08-16 20:58:20 +02:00
|
|
|
/* This file is part of libbrandt.
|
|
|
|
* Copyright (C) 2016 GNUnet e.V.
|
|
|
|
*
|
|
|
|
* libbrandt is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libbrandt is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* libbrandt. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file test_brandt.c
|
|
|
|
* @brief testing API functions.
|
|
|
|
* @author Markus Teich
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
#include <gnunet/gnunet_util_lib.h>
|
|
|
|
|
|
|
|
#include "brandt.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2016-10-14 05:50:27 +02:00
|
|
|
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
2016-08-16 20:58:20 +02:00
|
|
|
|
2016-09-21 13:50:20 +02:00
|
|
|
struct msg {
|
|
|
|
uint16_t sender;
|
|
|
|
uint16_t receiver;
|
|
|
|
void *buf;
|
|
|
|
size_t buf_len;
|
2016-08-31 14:37:22 +02:00
|
|
|
};
|
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
struct testcase {
|
|
|
|
uint16_t n;
|
|
|
|
uint16_t k;
|
|
|
|
uint16_t *bids;
|
|
|
|
uint16_t m;
|
|
|
|
uint16_t outcome_public;
|
|
|
|
uint16_t ret;
|
|
|
|
struct BRANDT_Auction **ad;
|
|
|
|
uint16_t *id;
|
|
|
|
uint16_t *result_called;
|
2016-09-23 17:35:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
static struct testcase tcase;
|
2016-09-23 17:35:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
static struct BRANDT_Result *
|
2016-10-14 04:55:53 +02:00
|
|
|
expected_outcome (uint16_t i)
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
2016-10-14 05:50:27 +02:00
|
|
|
struct BRANDT_Result *ret = NULL;
|
2016-09-23 17:35:31 +02:00
|
|
|
int32_t highest_bidder = -1;
|
|
|
|
int32_t highest_bid = -1;
|
2016-10-14 05:50:27 +02:00
|
|
|
int32_t mpf_highest_bidder;
|
|
|
|
int32_t mpf_highest_bid;
|
|
|
|
int32_t prev_mpf_highest_bidder = -1;
|
|
|
|
uint16_t winners = MIN (tcase.m, tcase.n);
|
|
|
|
uint16_t cur_winner = 0;
|
2016-09-23 17:35:31 +02:00
|
|
|
|
2016-10-14 05:50:27 +02:00
|
|
|
if (0 == tcase.m)
|
|
|
|
{
|
|
|
|
for (uint16_t h = 0; h < tcase.n; h++)
|
|
|
|
if (tcase.bids[h] > highest_bid)
|
|
|
|
highest_bid = tcase.bids[highest_bidder = h];
|
|
|
|
|
|
|
|
if (!tcase.outcome_public && !(i == highest_bidder || i == tcase.n))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = GNUNET_new (struct BRANDT_Result);
|
|
|
|
ret->bidder = highest_bidder;
|
|
|
|
ret->price = highest_bid;
|
|
|
|
ret->status = BRANDT_bidder_won;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find M+1st highest bidder to determine selling price */
|
|
|
|
for (uint16_t m = 0; m <= MIN (tcase.m, tcase.n - 1); m++)
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
2016-10-14 05:50:27 +02:00
|
|
|
for (uint16_t h = 0; h < tcase.n; h++)
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
2016-10-14 05:50:27 +02:00
|
|
|
mpf_highest_bidder = -1;
|
|
|
|
mpf_highest_bid = -1;
|
|
|
|
if (tcase.bids[h] > mpf_highest_bid &&
|
|
|
|
(-1 == prev_mpf_highest_bidder ||
|
|
|
|
tcase.bids[h] < tcase.bids[prev_mpf_highest_bidder] ||
|
|
|
|
h > prev_mpf_highest_bidder))
|
|
|
|
mpf_highest_bid = tcase.bids[mpf_highest_bidder = h];
|
2016-09-23 17:35:31 +02:00
|
|
|
}
|
2016-10-14 05:50:27 +02:00
|
|
|
prev_mpf_highest_bidder = mpf_highest_bidder;
|
2016-09-23 17:35:31 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 05:50:27 +02:00
|
|
|
/* for simplicity always locate the big block if we need to report at
|
|
|
|
* least one winner. with private outcome for losing bidders or winners
|
|
|
|
* only none or one element will be used respectively. */
|
|
|
|
if (tcase.outcome_public || i == tcase.n ||
|
|
|
|
tcase.bids[i] > mpf_highest_bid ||
|
|
|
|
(tcase.bids[i] == mpf_highest_bid && i < mpf_highest_bidder))
|
|
|
|
ret = GNUNET_new_array (winners, struct BRANDT_Result);
|
2016-09-23 17:35:31 +02:00
|
|
|
|
2016-10-14 05:50:27 +02:00
|
|
|
/* report winners */
|
|
|
|
for (uint16_t h = 0; h < tcase.n; h++)
|
|
|
|
{
|
|
|
|
if (((tcase.bids[h] == mpf_highest_bid && h < mpf_highest_bidder) ||
|
|
|
|
tcase.bids[h] > mpf_highest_bid) && /* h is a winner */
|
|
|
|
(tcase.outcome_public || i == h || i == tcase.n)) /* needs report */
|
|
|
|
{
|
|
|
|
if (cur_winner >= winners)
|
|
|
|
{
|
|
|
|
weprintf ("got too many winners");
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
ret[cur_winner].bidder = h;
|
|
|
|
ret[cur_winner].price = mpf_highest_bid;
|
|
|
|
ret[cur_winner].status = BRANDT_bidder_won;
|
|
|
|
cur_winner++;
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 17:35:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-08-31 14:37:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
bidder_start (void *arg)
|
|
|
|
{
|
|
|
|
uint16_t i = *(uint16_t *)arg;
|
|
|
|
|
2016-09-21 13:50:20 +02:00
|
|
|
weprintf ("starting bidder %d", i);
|
2016-10-14 04:55:53 +02:00
|
|
|
BRANDT_bidder_start (tcase.ad[i], i, tcase.n);
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
transfer_message (void *arg)
|
|
|
|
{
|
2016-09-21 13:50:20 +02:00
|
|
|
struct msg *m = (struct msg *)arg;
|
2016-08-31 14:37:22 +02:00
|
|
|
struct msg_head *h = (struct msg_head *)m->buf;
|
|
|
|
|
2016-09-21 13:50:20 +02:00
|
|
|
weprintf ("xfer msg %d %x from %d to %d", ntohl (
|
|
|
|
h->msg_type), arg, m->sender, m->receiver);
|
2016-10-14 04:55:53 +02:00
|
|
|
BRANDT_got_message (tcase.ad[m->receiver], m->sender, m->buf, m->buf_len);
|
|
|
|
GNUNET_free (m->buf);
|
|
|
|
GNUNET_free (m);
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint16_t
|
|
|
|
cb_start (void *auction_closure)
|
|
|
|
{
|
|
|
|
uint16_t *s = (uint16_t *)auction_closure;
|
2016-09-21 13:50:20 +02:00
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
if (tcase.n != *s)
|
2016-08-31 14:37:22 +02:00
|
|
|
{
|
|
|
|
weprintf ("start callback called from bidder");
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
for (uint16_t i = 0; i < tcase.n; i++)
|
|
|
|
GNUNET_SCHEDULER_add_now (&bidder_start, &tcase.id[i]);
|
2016-08-31 14:37:22 +02:00
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
return tcase.n;
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_broadcast (void *auction_closure,
|
|
|
|
const void *msg,
|
|
|
|
size_t msg_len)
|
|
|
|
{
|
2016-09-21 13:50:20 +02:00
|
|
|
uint16_t *s = (uint16_t *)auction_closure;
|
2016-08-31 14:37:22 +02:00
|
|
|
struct msg *m;
|
2016-09-08 19:17:15 +02:00
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
for (uint16_t i = 0; i <= tcase.n; i++)
|
2016-08-31 14:37:22 +02:00
|
|
|
{
|
|
|
|
if (i == *s)
|
|
|
|
continue;
|
|
|
|
m = GNUNET_new (struct msg);
|
|
|
|
m->sender = *s;
|
|
|
|
m->receiver = i;
|
|
|
|
m->buf = GNUNET_new_array (msg_len, unsigned char);
|
|
|
|
memcpy (m->buf, msg, msg_len);
|
|
|
|
m->buf_len = msg_len;
|
|
|
|
GNUNET_SCHEDULER_add_now (&transfer_message, m);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_unicast (void *auction_closure,
|
|
|
|
const void *msg,
|
|
|
|
size_t msg_len)
|
|
|
|
{
|
2016-09-21 13:50:20 +02:00
|
|
|
uint16_t *s = (uint16_t *)auction_closure;
|
2016-09-08 19:17:15 +02:00
|
|
|
struct msg *m;
|
|
|
|
|
|
|
|
m = GNUNET_new (struct msg);
|
|
|
|
m->sender = *s;
|
2016-10-14 04:55:53 +02:00
|
|
|
m->receiver = tcase.n; /* == seller */
|
2016-09-08 19:17:15 +02:00
|
|
|
m->buf = GNUNET_new_array (msg_len, unsigned char);
|
|
|
|
memcpy (m->buf, msg, msg_len);
|
|
|
|
m->buf_len = msg_len;
|
|
|
|
GNUNET_SCHEDULER_add_now (&transfer_message, m);
|
|
|
|
|
|
|
|
return 0;
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
cb_result (void *auction_closure,
|
|
|
|
struct BRANDT_Result results[],
|
|
|
|
uint16_t results_len)
|
|
|
|
{
|
2016-09-23 17:35:31 +02:00
|
|
|
uint16_t *s = (uint16_t *)auction_closure;
|
2016-10-14 04:55:53 +02:00
|
|
|
struct BRANDT_Result *must = expected_outcome (*s);
|
2016-09-08 20:09:22 +02:00
|
|
|
|
|
|
|
if (0 == results_len)
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
weprintf ("expected result is: %p", must);
|
|
|
|
weprintf ("computed result is: (nil) (from agent %d)", *s);
|
2016-09-23 17:35:31 +02:00
|
|
|
|
|
|
|
if (NULL != must)
|
2016-10-14 04:55:53 +02:00
|
|
|
tcase.ret = 1;
|
2016-09-23 17:35:31 +02:00
|
|
|
}
|
2016-09-08 20:09:22 +02:00
|
|
|
|
|
|
|
for (uint16_t i = 0; i < results_len; i++)
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
|
|
|
weprintf ("expected result is: bidder %d got status %d with price %d",
|
|
|
|
must[i].bidder,
|
|
|
|
must[i].status,
|
|
|
|
must[i].price);
|
2016-10-14 04:55:53 +02:00
|
|
|
weprintf (
|
|
|
|
"computed result is: bidder %d got status %d with price %d (from agent %d)",
|
|
|
|
results[i].bidder,
|
|
|
|
results[i].status,
|
|
|
|
results[i].price,
|
|
|
|
*s);
|
2016-09-23 17:35:31 +02:00
|
|
|
|
|
|
|
if (NULL == must ||
|
|
|
|
must[i].bidder != results[i].bidder ||
|
|
|
|
must[i].status != results[i].status ||
|
|
|
|
must[i].price != results[i].price)
|
2016-10-14 04:55:53 +02:00
|
|
|
tcase.ret = 1;
|
2016-09-23 17:35:31 +02:00
|
|
|
}
|
2016-10-14 04:55:53 +02:00
|
|
|
|
|
|
|
tcase.result_called[*s] = 1;
|
|
|
|
if (must)
|
|
|
|
GNUNET_free (must);
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-16 20:58:20 +02:00
|
|
|
static void
|
2016-09-23 17:35:31 +02:00
|
|
|
run_auction (void *arg)
|
2016-08-16 20:58:20 +02:00
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
const char description[] = "test description for test_auction";
|
|
|
|
void *desc;
|
|
|
|
size_t desc_len;
|
|
|
|
|
|
|
|
tcase.ad[tcase.n] = BRANDT_new (&cb_result,
|
|
|
|
&cb_broadcast,
|
|
|
|
&cb_start,
|
|
|
|
&tcase.id[tcase.n],
|
|
|
|
&desc,
|
|
|
|
&desc_len,
|
|
|
|
description,
|
|
|
|
sizeof (description),
|
|
|
|
GNUNET_TIME_absolute_get (),
|
|
|
|
GNUNET_TIME_UNIT_MINUTES,
|
|
|
|
tcase.k, /* number of prizes */
|
|
|
|
tcase.m, /* m */
|
|
|
|
tcase.outcome_public); /* outcome public */
|
|
|
|
if (!tcase.ad[tcase.n])
|
2016-08-16 20:58:20 +02:00
|
|
|
{
|
|
|
|
weprintf ("BRANDT_new() failed.");
|
2016-08-31 14:37:22 +02:00
|
|
|
_exit (1);
|
2016-08-16 20:58:20 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
for (uint16_t i = 0; i < tcase.n; i++)
|
2016-08-17 17:37:56 +02:00
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
tcase.ad[i] = BRANDT_join (&cb_result,
|
|
|
|
&cb_broadcast,
|
|
|
|
&cb_unicast,
|
|
|
|
&tcase.id[i],
|
|
|
|
desc,
|
|
|
|
desc_len,
|
|
|
|
description,
|
|
|
|
sizeof (description),
|
|
|
|
tcase.bids[i]); /* bid */
|
|
|
|
if (!tcase.ad[i])
|
2016-08-31 14:37:22 +02:00
|
|
|
{
|
|
|
|
weprintf ("BRANDT_join() failed.");
|
2016-10-14 04:55:53 +02:00
|
|
|
tcase.ret = 1;
|
|
|
|
return;
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
if (tcase.ad[tcase.n]->k != tcase.ad[i]->k ||
|
|
|
|
tcase.ad[tcase.n]->m != tcase.ad[i]->m ||
|
|
|
|
tcase.ad[tcase.n]->outcome_public != tcase.ad[i]->outcome_public ||
|
|
|
|
tcase.ad[tcase.n]->time_start.abs_value_us
|
|
|
|
!= tcase.ad[i]->time_start.abs_value_us ||
|
|
|
|
tcase.ad[tcase.n]->time_round.rel_value_us
|
|
|
|
!= tcase.ad[i]->time_round.rel_value_us ||
|
|
|
|
!tcase.ad[tcase.n]->seller_mode || /* todo: split out */
|
|
|
|
tcase.ad[i]->seller_mode)
|
2016-08-31 14:37:22 +02:00
|
|
|
{
|
|
|
|
weprintf ("error/mismatch in basic auction data");
|
2016-10-14 04:55:53 +02:00
|
|
|
tcase.ret = 1;
|
|
|
|
return;
|
2016-08-31 14:37:22 +02:00
|
|
|
}
|
2016-08-17 17:37:56 +02:00
|
|
|
}
|
2016-08-16 20:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2016-10-14 04:55:53 +02:00
|
|
|
test_auction (uint16_t n,
|
|
|
|
uint16_t k,
|
|
|
|
uint16_t *bids,
|
|
|
|
uint16_t m,
|
|
|
|
uint16_t outcome_public)
|
2016-08-16 20:58:20 +02:00
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
tcase.n = n;
|
|
|
|
tcase.k = k;
|
|
|
|
tcase.bids = bids;
|
|
|
|
tcase.m = m;
|
|
|
|
tcase.outcome_public = outcome_public;
|
|
|
|
tcase.ret = 0;
|
|
|
|
|
|
|
|
tcase.ad = GNUNET_new_array (tcase.n + 1, struct BRANDT_Auction *);
|
|
|
|
tcase.id = GNUNET_new_array (tcase.n + 1, uint16_t);
|
|
|
|
for (uint16_t i = 0; i <= tcase.n; i++)
|
|
|
|
tcase.id[i] = i;
|
|
|
|
tcase.result_called = GNUNET_new_array (tcase.n + 1, uint16_t);
|
|
|
|
|
|
|
|
GNUNET_SCHEDULER_run (&run_auction, NULL);
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i <= tcase.n; i++)
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
BRANDT_destroy (tcase.ad[i]);
|
|
|
|
if (!tcase.result_called[i])
|
2016-09-23 17:35:31 +02:00
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
weprintf ("result callback not called for bidder %d", i);
|
|
|
|
tcase.ret = 1;
|
2016-09-23 17:35:31 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-31 14:37:22 +02:00
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
GNUNET_free (tcase.ad);
|
|
|
|
GNUNET_free (tcase.id);
|
|
|
|
GNUNET_free (tcase.result_called);
|
|
|
|
|
|
|
|
return tcase.ret;
|
2016-08-16 20:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2016-10-14 04:55:53 +02:00
|
|
|
int ret = 0;
|
2016-08-16 20:58:20 +02:00
|
|
|
struct GNUNET_CRYPTO_EccDlogContext *edc;
|
|
|
|
|
|
|
|
edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
|
|
|
|
BRANDT_init (edc);
|
|
|
|
|
2016-10-14 04:55:53 +02:00
|
|
|
ret |= test_auction (4, 3, (uint16_t[]) { 1, 2, 0, 2 }, 0, 1) ||
|
|
|
|
test_auction (4, 3, (uint16_t[]) { 1, 2, 0, 2 }, 0, 0);
|
2016-08-16 20:58:20 +02:00
|
|
|
|
|
|
|
GNUNET_CRYPTO_ecc_dlog_release (edc);
|
|
|
|
return ret;
|
|
|
|
}
|