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_NO
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];
67 const struct GNUNET_MessageHeader *hdr)
69 static unsigned int total;
74 fprintf (stderr, "!"); /* message complete, good! */
76 buf = (const char*) hdr;
77 for (i=sizeof (struct GNUNET_MessageHeader);i<ntohs(hdr->size);i++)
78 GNUNET_assert (buf[i] == (char) i);
81 if (0 == (total % (NUM_MSGS / 100)))
82 fprintf (stderr, ".");
84 if (total == NUM_MSGS)
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,
105 const struct GNUNET_MessageHeader *hdr)
110 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
113 return; /* random drop */
115 for (i=0;i<NUM_MSGS;i++)
117 if (frags[i] == NULL)
119 ret = GNUNET_FRAGMENT_process_ack (frags[i],
121 if (ret == GNUNET_OK)
124 fprintf (stderr, "@"); /* good ACK */
126 GNUNET_FRAGMENT_context_destroy (frags[i]);
131 if (ret == GNUNET_NO)
134 fprintf (stderr, "@"); /* good ACK */
141 fprintf (stderr, "_"); /* BAD: ack that nobody feels responsible for... */
147 * Process fragment (by passing to defrag).
150 proc_frac (void *cls,
151 const struct GNUNET_MessageHeader *hdr)
155 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
158 return; /* random drop */
162 fprintf (stderr, "E"); /* Error: frag after shutdown!? */
165 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
166 if (ret == GNUNET_NO)
169 fprintf (stderr, "?"); /* duplicate fragment */
173 else if (ret == GNUNET_OK)
176 fprintf (stderr, "."); /* good fragment */
184 * Main function run with scheduler.
189 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
192 struct GNUNET_MessageHeader *msg;
193 char buf[MTU + 32 * 1024];
195 defrag = GNUNET_DEFRAGMENT_context_create (NULL,
197 NUM_MSGS /* enough space for all */,
201 for (i=0;i<sizeof(buf);i++)
203 msg = (struct GNUNET_MessageHeader* ) buf;
204 for (i=0;i<NUM_MSGS;i++)
206 msg->type = htons ((uint16_t) i);
207 msg->size = htons (MTU + 1 + (17 * i) % (32 * 1024));
208 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */,
211 GNUNET_TIME_UNIT_SECONDS,
220 main (int argc, char *argv[])
222 struct GNUNET_GETOPT_CommandLineOption options[] = {
223 GNUNET_GETOPT_OPTION_END
225 char *const argv_prog[] = {
226 "test-fragmentation",
228 "test_fragmentation_data.conf",
239 GNUNET_log_setup ("test-fragmentation",
246 for (i=0;i<NUM_MSGS;i++)
247 GNUNET_BANDWIDTH_tracker_init (&trackers[i],
248 GNUNET_BANDWIDTH_value_init ((i+1) * 1024),
250 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options, &run, NULL);
252 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",