aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-10-14 04:55:53 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-10-14 04:55:53 +0200
commite333dd01839038942ae724305316f8df1196103d (patch)
treea2e29177ad9d1aad7a040154f78c671a71ff4625
parent45b930823c25c0c13a86febf5f6c234f229d7498 (diff)
downloadlibbrandt-e333dd01839038942ae724305316f8df1196103d.tar.gz
libbrandt-e333dd01839038942ae724305316f8df1196103d.zip
rework test_brandt
-rw-r--r--test_brandt.c237
1 files changed, 117 insertions, 120 deletions
diff --git a/test_brandt.c b/test_brandt.c
index a386124..ee53a0d 100644
--- a/test_brandt.c
+++ b/test_brandt.c
@@ -36,42 +36,39 @@ struct msg {
36 size_t buf_len; 36 size_t buf_len;
37}; 37};
38 38
39 39struct testcase {
40struct test { 40 uint16_t n;
41 uint16_t ret; 41 uint16_t k;
42 uint16_t m; 42 uint16_t *bids;
43 uint16_t outcome_public; 43 uint16_t m;
44 uint16_t outcome_public;
45 uint16_t ret;
46 struct BRANDT_Auction **ad;
47 uint16_t *id;
48 uint16_t *result_called;
44}; 49};
45 50
46 51
47static uint16_t *id; 52static struct testcase tcase;
48static uint16_t *result_called;
49static struct BRANDT_Auction **ad;
50static uint16_t unexpected_result;
51
52/* TEST CONFIGURATION */
53static const uint16_t bidders = 4;
54static const uint16_t prizes = 3;
55static const uint16_t bids[] = { 1, 2, 0, 2 };
56 53
57 54
58static struct BRANDT_Result * 55static struct BRANDT_Result *
59expected_outcome (uint16_t m, uint16_t outcome_public, uint16_t i) 56expected_outcome (uint16_t i)
60{ 57{
61 int32_t highest_bidder = -1; 58 int32_t highest_bidder = -1;
62 int32_t highest_bid = -1; 59 int32_t highest_bid = -1;
63 struct BRANDT_Result *ret; 60 struct BRANDT_Result *ret;
64 61
65 for (uint16_t i = 0; i < bidders; i++) 62 for (uint16_t h = 0; h < tcase.n; h++)
66 { 63 {
67 if (bids[i] > highest_bid) 64 if (tcase.bids[h] > highest_bid)
68 { 65 {
69 highest_bid = bids[i]; 66 highest_bid = tcase.bids[h];
70 highest_bidder = i; 67 highest_bidder = h;
71 } 68 }
72 } 69 }
73 70
74 if (!outcome_public && !(i == highest_bidder || i == bidders)) 71 if (!tcase.outcome_public && !(i == highest_bidder || i == tcase.n))
75 return NULL; 72 return NULL;
76 73
77 ret = GNUNET_new (struct BRANDT_Result); 74 ret = GNUNET_new (struct BRANDT_Result);
@@ -88,7 +85,7 @@ bidder_start (void *arg)
88 uint16_t i = *(uint16_t *)arg; 85 uint16_t i = *(uint16_t *)arg;
89 86
90 weprintf ("starting bidder %d", i); 87 weprintf ("starting bidder %d", i);
91 BRANDT_bidder_start (ad[i], i, bidders); 88 BRANDT_bidder_start (tcase.ad[i], i, tcase.n);
92} 89}
93 90
94 91
@@ -100,8 +97,9 @@ transfer_message (void *arg)
100 97
101 weprintf ("xfer msg %d %x from %d to %d", ntohl ( 98 weprintf ("xfer msg %d %x from %d to %d", ntohl (
102 h->msg_type), arg, m->sender, m->receiver); 99 h->msg_type), arg, m->sender, m->receiver);
103 BRANDT_got_message (ad[m->receiver], m->sender, m->buf, m->buf_len); 100 BRANDT_got_message (tcase.ad[m->receiver], m->sender, m->buf, m->buf_len);
104 free (arg); 101 GNUNET_free (m->buf);
102 GNUNET_free (m);
105} 103}
106 104
107 105
@@ -110,16 +108,16 @@ cb_start (void *auction_closure)
110{ 108{
111 uint16_t *s = (uint16_t *)auction_closure; 109 uint16_t *s = (uint16_t *)auction_closure;
112 110
113 if (!s || bidders != *s) 111 if (tcase.n != *s)
114 { 112 {
115 weprintf ("start callback called from bidder"); 113 weprintf ("start callback called from bidder");
116 _exit (1); 114 _exit (1);
117 } 115 }
118 116
119 for (uint16_t i = 0; i < bidders; i++) 117 for (uint16_t i = 0; i < tcase.n; i++)
120 GNUNET_SCHEDULER_add_now (&bidder_start, &id[i]); 118 GNUNET_SCHEDULER_add_now (&bidder_start, &tcase.id[i]);
121 119
122 return bidders; 120 return tcase.n;
123} 121}
124 122
125 123
@@ -131,7 +129,7 @@ cb_broadcast (void *auction_closure,
131 uint16_t *s = (uint16_t *)auction_closure; 129 uint16_t *s = (uint16_t *)auction_closure;
132 struct msg *m; 130 struct msg *m;
133 131
134 for (uint16_t i = 0; i <= bidders; i++) 132 for (uint16_t i = 0; i <= tcase.n; i++)
135 { 133 {
136 if (i == *s) 134 if (i == *s)
137 continue; 135 continue;
@@ -157,7 +155,7 @@ cb_unicast (void *auction_closure,
157 155
158 m = GNUNET_new (struct msg); 156 m = GNUNET_new (struct msg);
159 m->sender = *s; 157 m->sender = *s;
160 m->receiver = bidders; /* == seller */ 158 m->receiver = tcase.n; /* == seller */
161 m->buf = GNUNET_new_array (msg_len, unsigned char); 159 m->buf = GNUNET_new_array (msg_len, unsigned char);
162 memcpy (m->buf, msg, msg_len); 160 memcpy (m->buf, msg, msg_len);
163 m->buf_len = msg_len; 161 m->buf_len = msg_len;
@@ -173,157 +171,156 @@ cb_result (void *auction_closure,
173 uint16_t results_len) 171 uint16_t results_len)
174{ 172{
175 uint16_t *s = (uint16_t *)auction_closure; 173 uint16_t *s = (uint16_t *)auction_closure;
176 struct BRANDT_Result *must = expected_outcome (ad[*s]->m, 174 struct BRANDT_Result *must = expected_outcome (*s);
177 ad[*s]->outcome_public,
178 *s);
179 175
180 if (0 == results_len) 176 if (0 == results_len)
181 { 177 {
182 weprintf ("result from agent %d: %p", *s, must); 178 weprintf ("expected result is: %p", must);
183 weprintf ("expected result is: (nil)"); 179 weprintf ("computed result is: (nil) (from agent %d)", *s);
184 180
185 if (NULL != must) 181 if (NULL != must)
186 unexpected_result = 1; 182 tcase.ret = 1;
187 } 183 }
188 184
189 for (uint16_t i = 0; i < results_len; i++) 185 for (uint16_t i = 0; i < results_len; i++)
190 { 186 {
191 weprintf ("result from agent %d: bidder %d got status %d with price %d",
192 *s,
193 results[i].bidder,
194 results[i].status,
195 results[i].price);
196 weprintf ("expected result is: bidder %d got status %d with price %d", 187 weprintf ("expected result is: bidder %d got status %d with price %d",
197 must[i].bidder, 188 must[i].bidder,
198 must[i].status, 189 must[i].status,
199 must[i].price); 190 must[i].price);
191 weprintf (
192 "computed result is: bidder %d got status %d with price %d (from agent %d)",
193 results[i].bidder,
194 results[i].status,
195 results[i].price,
196 *s);
200 197
201 if (NULL == must || 198 if (NULL == must ||
202 must[i].bidder != results[i].bidder || 199 must[i].bidder != results[i].bidder ||
203 must[i].status != results[i].status || 200 must[i].status != results[i].status ||
204 must[i].price != results[i].price) 201 must[i].price != results[i].price)
205 unexpected_result = 1; 202 tcase.ret = 1;
206 } 203 }
207 result_called[*s] = 1; 204
205 tcase.result_called[*s] = 1;
206 if (must)
207 GNUNET_free (must);
208} 208}
209 209
210 210
211static void 211static void
212run_auction (void *arg) 212run_auction (void *arg)
213{ 213{
214 struct test *asetup = arg; 214 const char description[] = "test description for test_auction";
215 const char description[] = "test description for test_auctions"; 215 void *desc;
216 void *desc; 216 size_t desc_len;
217 size_t desc_len; 217
218 218 tcase.ad[tcase.n] = BRANDT_new (&cb_result,
219 ad = GNUNET_new_array (bidders + 1, struct BRANDT_Auction *); 219 &cb_broadcast,
220 220 &cb_start,
221 ad[bidders] = BRANDT_new (&cb_result, 221 &tcase.id[tcase.n],
222 &cb_broadcast, 222 &desc,
223 &cb_start, 223 &desc_len,
224 &id[bidders], 224 description,
225 &desc, 225 sizeof (description),
226 &desc_len, 226 GNUNET_TIME_absolute_get (),
227 description, 227 GNUNET_TIME_UNIT_MINUTES,
228 sizeof (description), 228 tcase.k, /* number of prizes */
229 GNUNET_TIME_absolute_get (), 229 tcase.m, /* m */
230 GNUNET_TIME_UNIT_MINUTES, 230 tcase.outcome_public); /* outcome public */
231 prizes, /* number of prizes */ 231 if (!tcase.ad[tcase.n])
232 asetup->m, /* m */
233 asetup->outcome_public); /* outcome public */
234 if (!ad[bidders])
235 { 232 {
236 weprintf ("BRANDT_new() failed."); 233 weprintf ("BRANDT_new() failed.");
237 _exit (1); 234 _exit (1);
238 } 235 }
239 236
240 for (uint16_t i = 0; i < bidders; i++) 237 for (uint16_t i = 0; i < tcase.n; i++)
241 { 238 {
242 ad[i] = BRANDT_join (&cb_result, 239 tcase.ad[i] = BRANDT_join (&cb_result,
243 &cb_broadcast, 240 &cb_broadcast,
244 &cb_unicast, 241 &cb_unicast,
245 &id[i], 242 &tcase.id[i],
246 desc, 243 desc,
247 desc_len, 244 desc_len,
248 description, 245 description,
249 sizeof (description), 246 sizeof (description),
250 bids[i]); /* bid */ 247 tcase.bids[i]); /* bid */
251 if (!ad[i]) 248 if (!tcase.ad[i])
252 { 249 {
253 weprintf ("BRANDT_join() failed."); 250 weprintf ("BRANDT_join() failed.");
254 _exit (1); 251 tcase.ret = 1;
252 return;
255 } 253 }
256 254
257 if (ad[bidders]->k != ad[i]->k || 255 if (tcase.ad[tcase.n]->k != tcase.ad[i]->k ||
258 ad[bidders]->m != ad[i]->m || 256 tcase.ad[tcase.n]->m != tcase.ad[i]->m ||
259 ad[bidders]->outcome_public != ad[i]->outcome_public || 257 tcase.ad[tcase.n]->outcome_public != tcase.ad[i]->outcome_public ||
260 ad[bidders]->time_start.abs_value_us 258 tcase.ad[tcase.n]->time_start.abs_value_us
261 != ad[i]->time_start.abs_value_us || 259 != tcase.ad[i]->time_start.abs_value_us ||
262 ad[bidders]->time_round.rel_value_us 260 tcase.ad[tcase.n]->time_round.rel_value_us
263 != ad[i]->time_round.rel_value_us || 261 != tcase.ad[i]->time_round.rel_value_us ||
264 !ad[bidders]->seller_mode || /* todo: split out */ 262 !tcase.ad[tcase.n]->seller_mode || /* todo: split out */
265 ad[i]->seller_mode) 263 tcase.ad[i]->seller_mode)
266 { 264 {
267 weprintf ("error/mismatch in basic auction data"); 265 weprintf ("error/mismatch in basic auction data");
268 _exit (1); 266 tcase.ret = 1;
267 return;
269 } 268 }
270 } 269 }
271} 270}
272 271
273 272
274static int 273static int
275test_auctions () 274test_auction (uint16_t n,
275 uint16_t k,
276 uint16_t *bids,
277 uint16_t m,
278 uint16_t outcome_public)
276{ 279{
277 id = GNUNET_new_array (bidders + 1, uint16_t); 280 tcase.n = n;
278 for (uint16_t i = 0; i <= bidders; i++) 281 tcase.k = k;
279 id[i] = i; 282 tcase.bids = bids;
280 283 tcase.m = m;
281 for (size_t atype = 0; atype < auction_last; atype++) 284 tcase.outcome_public = outcome_public;
285 tcase.ret = 0;
286
287 tcase.ad = GNUNET_new_array (tcase.n + 1, struct BRANDT_Auction *);
288 tcase.id = GNUNET_new_array (tcase.n + 1, uint16_t);
289 for (uint16_t i = 0; i <= tcase.n; i++)
290 tcase.id[i] = i;
291 tcase.result_called = GNUNET_new_array (tcase.n + 1, uint16_t);
292
293 GNUNET_SCHEDULER_run (&run_auction, NULL);
294
295 for (uint16_t i = 0; i <= tcase.n; i++)
282 { 296 {
283 if (auction_firstPrice != atype) /* others not yet implemented */ 297 BRANDT_destroy (tcase.ad[i]);
284 continue; 298 if (!tcase.result_called[i])
285
286 for (size_t oc = 0; oc < outcome_last; oc++)
287 { 299 {
288 struct test asetup; 300 weprintf ("result callback not called for bidder %d", i);
289 301 tcase.ret = 1;
290 result_called = GNUNET_new_array (bidders + 1, uint16_t);
291 unexpected_result = 0;
292 asetup.ret = 0;
293 asetup.m = atype;
294 asetup.outcome_public = oc;
295 GNUNET_SCHEDULER_run (&run_auction, &asetup);
296
297 for (uint16_t i = 0; i <= bidders; i++)
298 {
299 BRANDT_destroy (ad[i]);
300 if (!result_called[i])
301 {
302 weprintf ("result callback not called for bidder %d", i);
303 unexpected_result = 1;
304 }
305 }
306
307 if (unexpected_result)
308 return 0;
309 } 302 }
310 } 303 }
311 304
312 return 1; 305 GNUNET_free (tcase.ad);
306 GNUNET_free (tcase.id);
307 GNUNET_free (tcase.result_called);
308
309 return tcase.ret;
313} 310}
314 311
315 312
316int 313int
317main (int argc, char *argv[]) 314main (int argc, char *argv[])
318{ 315{
319 int ret = 0; 316 int ret = 0;
320 struct GNUNET_CRYPTO_EccDlogContext *edc; 317 struct GNUNET_CRYPTO_EccDlogContext *edc;
321 318
322 edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16); 319 edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
323 BRANDT_init (edc); 320 BRANDT_init (edc);
324 321
325 if (!test_auctions()) 322 ret |= test_auction (4, 3, (uint16_t[]) { 1, 2, 0, 2 }, 0, 1) ||
326 ret = 1; 323 test_auction (4, 3, (uint16_t[]) { 1, 2, 0, 2 }, 0, 0);
327 324
328 GNUNET_CRYPTO_ecc_dlog_release (edc); 325 GNUNET_CRYPTO_ecc_dlog_release (edc);
329 return ret; 326 return ret;