aboutsummaryrefslogtreecommitdiff
path: root/test_brandt.c
blob: 46ec90dfed762438c96396d6718df1cf6e7bdcc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* 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"

#define MIN(A, B) ((A) < (B) ? (A) : (B))

struct msg {
	uint16_t sender;
	uint16_t receiver;
	void     *buf;
	size_t   buf_len;
};

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;
};


static struct testcase tcase;


static struct BRANDT_Result *
expected_outcome (uint16_t i)
{
	struct BRANDT_Result *ret = NULL;
	int32_t              highest_bidder = -1;
	int32_t              highest_bid = -1;
	int32_t              mpf_highest_bidder;
	int32_t              mpf_highest_bid = -1;
	int32_t              prev_mpf_highest_bidder = -1;
	uint16_t             winners = MIN (tcase.m, tcase.n);
	uint16_t             cur_winner = 0;

	if (0 == tcase.n)
		return NULL;

	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;
	}

	/* fewer bidders than needed -> everyone wins with lowest price */
	if (tcase.n <= tcase.m)
	{
		if (tcase.outcome_public || i == tcase.n)
		{
			ret = GNUNET_new_array (tcase.n, struct BRANDT_Result);
			for (uint16_t h = 0; h < tcase.n; h++)
			{
				ret[h].bidder = h;
				ret[h].price = 0;
				ret[h].status = BRANDT_bidder_won;
			}
		}
		else
		{
			ret = GNUNET_new (struct BRANDT_Result);
			ret->bidder = i;
			ret->price = 0;
			ret->status = BRANDT_bidder_won;
		}
		return ret;
	}

	/* find M+1st highest bidder to determine selling price */
	for (uint16_t h = 0; h < tcase.n; h++)
		if (tcase.bids[h] > mpf_highest_bid)
			mpf_highest_bid = tcase.bids[prev_mpf_highest_bidder = h];
	for (uint16_t m = 0; m < MIN (tcase.m, tcase.n - 1); m++)
	{
		mpf_highest_bidder = -1;
		mpf_highest_bid = -1;
		for (uint16_t h = 0; h < tcase.n; h++)
		{
			if (tcase.bids[h] > mpf_highest_bid &&
			    (tcase.bids[h] < tcase.bids[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];
			}
		}
		prev_mpf_highest_bidder = mpf_highest_bidder;
	}

	/* 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);

	/* 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++;
		}
	}
	return ret;
}


static void
bidder_start (void *arg)
{
	uint16_t i = *(uint16_t *)arg;

	weprintf ("starting bidder %d", i);
	BRANDT_bidder_start (tcase.ad[i], i, tcase.n);
}


static void
transfer_message (void *arg)
{
	struct msg      *m = (struct msg *)arg;
	struct msg_head *h = (struct msg_head *)m->buf;

	weprintf ("xfer msg %d %x from %d to %d", ntohl (
	              h->msg_type), arg, m->sender, m->receiver);
	BRANDT_got_message (tcase.ad[m->receiver], m->sender, m->buf, m->buf_len);
	GNUNET_free (m->buf);
	GNUNET_free (m);
}


static uint16_t
cb_start (void *auction_closure)
{
	uint16_t *s = (uint16_t *)auction_closure;

	if (tcase.n != *s)
	{
		weprintf ("start callback called from bidder");
		_exit (1);
	}

	for (uint16_t i = 0; i < tcase.n; i++)
		GNUNET_SCHEDULER_add_now (&bidder_start, &tcase.id[i]);

	return tcase.n;
}


static int
cb_broadcast (void       *auction_closure,
              const void *msg,
              size_t     msg_len)
{
	uint16_t   *s = (uint16_t *)auction_closure;
	struct msg *m;

	for (uint16_t i = 0; i <= tcase.n; i++)
	{
		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)
{
	uint16_t   *s = (uint16_t *)auction_closure;
	struct msg *m;

	m = GNUNET_new (struct msg);
	m->sender = *s;
	m->receiver = tcase.n; /* == seller */
	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 void
cb_result (void                 *auction_closure,
           struct BRANDT_Result results[],
           uint16_t             results_len)
{
	uint16_t             *s = (uint16_t *)auction_closure;
	struct BRANDT_Result *must = expected_outcome (*s);

	if (0 == results_len)
	{
		weprintf ("expected result is: %p", must);
		weprintf ("computed result is: (nil) (by agent %d)", *s);

		if (NULL != must)
			tcase.ret = 1;
	}

	for (uint16_t i = 0; i < results_len; i++)
	{
		weprintf ("expected result is: bidder %d got status %d with price %d",
		          must[i].bidder,
		          must[i].status,
		          must[i].price);
		weprintf (
		    "computed result is: bidder %d got status %d with price %d (by agent %d)",
		    results[i].bidder,
		    results[i].status,
		    results[i].price,
		    *s);

		if (NULL == must ||
		    must[i].bidder != results[i].bidder ||
		    must[i].status != results[i].status ||
		    must[i].price != results[i].price)
			tcase.ret = 1;
	}

	tcase.result_called[*s] = 1;
	if (must)
		GNUNET_free (must);
}


static void
run_auction (void *arg)
{
	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])
	{
		weprintf ("BRANDT_new() failed.");
		_exit (1);
	}

	for (uint16_t i = 0; i < tcase.n; i++)
	{
		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])
		{
			weprintf ("BRANDT_join() failed.");
			tcase.ret = 1;
			return;
		}

		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)
		{
			weprintf ("error/mismatch in basic auction data");
			tcase.ret = 1;
			return;
		}
	}
}


static int
test_auction (uint16_t n,
              uint16_t k,
              uint16_t *bids,
              uint16_t m,
              uint16_t outcome_public)
{
	tcase.n = n;
	tcase.k = k;
	tcase.bids = bids;
	tcase.m = m;
	tcase.outcome_public = outcome_public;
	tcase.ret = 0;

	weprintf ("######################################");
	weprintf ("testing %s auction with m = %d and %s outcome",
	          tcase.m > 0 ? "M+1ST PRICE" : "FIRST PRICE",
	          tcase.m,
	          tcase.outcome_public ? "PUBLIC" : "PRIVATE");
	/** \todo: output bids when migrating to GNUNET_log */
	weprintf ("######################################");
	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++)
	{
		BRANDT_destroy (tcase.ad[i]);
		if (!tcase.result_called[i])
		{
			weprintf ("result callback not called for bidder %d", i);
			tcase.ret = 1;
		}
	}

	GNUNET_free (tcase.ad);
	GNUNET_free (tcase.id);
	GNUNET_free (tcase.result_called);

	return tcase.ret;
}


int
main (int argc, char *argv[])
{
	int                                 ret = 0;
	struct GNUNET_CRYPTO_EccDlogContext *edc;

	edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
	BRANDT_init (edc);

	ret |= 0 ||
	       test_auction (0, 2, NULL, 0, 0) ||
	       test_auction (0, 2, NULL, 0, 1) ||
	       test_auction (0, 2, NULL, 1, 0) ||
	       test_auction (0, 2, NULL, 2, 0) ||
	       test_auction (1, 2, (uint16_t[]) { 1 }, 1, 0) ||
	       test_auction (1, 2, (uint16_t[]) { 0 }, 2, 0) ||
	       test_auction (2, 2, (uint16_t[]) { 1, 0 }, 2, 0) ||
	       test_auction (2, 2, (uint16_t[]) { 1, 0 }, 1, 0) ||
	       test_auction (3, 2, (uint16_t[]) { 0, 0, 1 }, 2, 0) ||
	       test_auction (3, 2, (uint16_t[]) { 0, 1, 1 }, 0, 0) ||
	       test_auction (3, 2, (uint16_t[]) { 0, 1, 1 }, 0, 1) ||
	       0;

	GNUNET_CRYPTO_ecc_dlog_release (edc);
	return ret;
}