aboutsummaryrefslogtreecommitdiff
path: root/test_brandt.c
blob: 077f6f14158ab3a553b1bf1b71eca4689bc2989e (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
/* 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 "test.h"
#include "util.h"


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


static uint16_t              *id;
static struct BRANDT_Auction **ad;
static uint16_t              bidders = 3;
static uint16_t              prizes = 8;


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

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


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 (ad[m->receiver], m->sender, m->buf, m->buf_len);
	free (arg);
}


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

	if (!s || bidders != *s)
	{
		weprintf ("start callback called from bidder");
		_exit (1);
	}

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

	return bidders;
}


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 <= bidders; 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 = bidders; /* == 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;

	if (0 == results_len)
		weprintf ("result determined by agent %d: none", *s);

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


static void
run_new_join (void *arg)
{
	int        *ret = arg;
	const char description[] = "test description for test_new_join";
	void       *desc;
	size_t     desc_len;

	ad = GNUNET_new_array (bidders + 1, struct BRANDT_Auction *);

	ad[bidders] = BRANDT_new (&cb_result,
	                          &cb_broadcast,
	                          &cb_start,
	                          &id[bidders],
	                          &desc,
	                          &desc_len,
	                          description,
	                          sizeof (description),
	                          GNUNET_TIME_absolute_get (),
	                          GNUNET_TIME_UNIT_MINUTES,
	                          prizes, /* amount of possible prizes */
	                          0,      /* m */
	                          1);     /* outcome public */
	if (!ad[bidders])
	{
		weprintf ("BRANDT_new() failed.");
		_exit (1);
	}

	for (uint16_t i = 0; i < bidders; i++)
	{
		ad[i] = BRANDT_join (&cb_result,
		                     &cb_broadcast,
		                     &cb_unicast,
		                     &id[i],
		                     desc,
		                     desc_len,
		                     description,
		                     sizeof (description),
		                     3);      /* bid */
		if (!ad[i])
		{
			weprintf ("BRANDT_join() failed.");
			_exit (1);
		}

		if (ad[bidders]->k != ad[i]->k ||
		    ad[bidders]->m != ad[i]->m ||
		    ad[bidders]->outcome_public != ad[i]->outcome_public ||
		    ad[bidders]->time_start.abs_value_us
		    != ad[i]->time_start.abs_value_us ||
		    ad[bidders]->time_round.rel_value_us
		    != ad[i]->time_round.rel_value_us ||
		    !ad[bidders]->seller_mode || /* todo: split out */
		    ad[i]->seller_mode)
		{
			weprintf ("error/mismatch in basic auction data");
			_exit (1);
		}
	}

	*ret = 1;
}


static int
test_new_join ()
{
	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 (uint16_t i = 0; i <= bidders; i++)
		BRANDT_destroy (ad[i]);

	return ret;
}


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

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

	RUN (test_new_join);

	GNUNET_CRYPTO_ecc_dlog_release (edc);
	return ret;
}