2 This file is part of GNUnet
3 (C) 2004, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file fragmentation/test_fragmentation.c
22 * @brief test for fragmentation.c
23 * @author Christian Grothoff
26 #include "gnunet_fragmentation_lib.h"
28 #define VERBOSE GNUNET_EXTRA_LOGGING
30 #define DETAILS GNUNET_NO
33 * Number of messages to transmit (note: each uses ~32k memory!)
38 * MTU to force on fragmentation (must be > 1k + 12)
43 * Simulate dropping of 1 out of how many messages? (must be > 1)
49 static unsigned int dups;
51 static unsigned int fragc;
53 static unsigned int frag_drops;
55 static unsigned int acks;
57 static unsigned int ack_drops;
59 static struct GNUNET_DEFRAGMENT_Context *defrag;
61 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
63 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
66 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
68 static unsigned int total;
73 fprintf (stderr, "!"); /* message complete, good! */
75 buf = (const char *) hdr;
76 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
77 GNUNET_assert (buf[i] == (char) i);
80 if (0 == (total % (NUM_MSGS / 100)))
81 fprintf (stderr, ".");
83 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
84 if (total >= NUM_MSGS - (NUM_MSGS / 10))
87 GNUNET_DEFRAGMENT_context_destroy (defrag);
89 for (i = 0; i < NUM_MSGS; i++)
93 GNUNET_FRAGMENT_context_destroy (frags[i]);
101 * Process ACK (by passing to fragmenter)
104 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
109 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
112 return; /* random drop */
114 for (i = 0; i < NUM_MSGS; i++)
116 if (frags[i] == NULL)
118 ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
119 if (ret == GNUNET_OK)
122 fprintf (stderr, "@"); /* good ACK */
124 GNUNET_FRAGMENT_context_destroy (frags[i]);
129 if (ret == GNUNET_NO)
132 fprintf (stderr, "@"); /* good ACK */
139 fprintf (stderr, "_"); /* BAD: ack that nobody feels responsible for... */
145 * Process fragment (by passing to defrag).
148 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
150 struct GNUNET_FRAGMENT_Context **fc = cls;
153 GNUNET_FRAGMENT_context_transmission_done (*fc);
154 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
157 return; /* random drop */
161 fprintf (stderr, "E"); /* Error: frag after shutdown!? */
164 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
165 if (ret == GNUNET_NO)
168 fprintf (stderr, "?"); /* duplicate fragment */
172 else if (ret == GNUNET_OK)
175 fprintf (stderr, "."); /* good fragment */
183 * Main function run with scheduler.
186 run (void *cls, char *const *args, const char *cfgfile,
187 const struct GNUNET_CONFIGURATION_Handle *cfg)
190 struct GNUNET_MessageHeader *msg;
191 char buf[MTU + 32 * 1024];
193 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
194 , NULL, &proc_msgs, &proc_acks);
195 for (i = 0; i < sizeof (buf); i++)
197 msg = (struct GNUNET_MessageHeader *) buf;
198 for (i = 0; i < NUM_MSGS; i++)
200 msg->type = htons ((uint16_t) i);
202 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
203 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
205 GNUNET_TIME_UNIT_SECONDS, msg,
206 &proc_frac, &frags[i]);
212 main (int argc, char *argv[])
214 struct GNUNET_GETOPT_CommandLineOption options[] = {
215 GNUNET_GETOPT_OPTION_END
217 char *const argv_prog[] = {
218 "test-fragmentation",
220 "test_fragmentation_data.conf",
231 GNUNET_log_setup ("test-fragmentation",
238 for (i = 0; i < NUM_MSGS; i++)
239 GNUNET_BANDWIDTH_tracker_init (&trackers[i],
240 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
242 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
245 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
246 fragc, dups, acks, ack_drops);