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 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
85 if (total >= NUM_MSGS - (NUM_MSGS / 10))
88 GNUNET_DEFRAGMENT_context_destroy (defrag);
90 for (i=0;i<NUM_MSGS;i++)
94 GNUNET_FRAGMENT_context_destroy (frags[i]);
102 * Process ACK (by passing to fragmenter)
105 proc_acks (void *cls,
107 const struct GNUNET_MessageHeader *hdr)
112 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
115 return; /* random drop */
117 for (i=0;i<NUM_MSGS;i++)
119 if (frags[i] == NULL)
121 ret = GNUNET_FRAGMENT_process_ack (frags[i],
123 if (ret == GNUNET_OK)
126 fprintf (stderr, "@"); /* good ACK */
128 GNUNET_FRAGMENT_context_destroy (frags[i]);
133 if (ret == GNUNET_NO)
136 fprintf (stderr, "@"); /* good ACK */
143 fprintf (stderr, "_"); /* BAD: ack that nobody feels responsible for... */
149 * Process fragment (by passing to defrag).
152 proc_frac (void *cls,
153 const struct GNUNET_MessageHeader *hdr)
155 struct GNUNET_FRAGMENT_Context **fc = cls;
158 GNUNET_FRAGMENT_context_transmission_done (*fc);
159 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
162 return; /* random drop */
166 fprintf (stderr, "E"); /* Error: frag after shutdown!? */
169 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
170 if (ret == GNUNET_NO)
173 fprintf (stderr, "?"); /* duplicate fragment */
177 else if (ret == GNUNET_OK)
180 fprintf (stderr, "."); /* good fragment */
188 * Main function run with scheduler.
193 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
196 struct GNUNET_MessageHeader *msg;
197 char buf[MTU + 32 * 1024];
199 defrag = GNUNET_DEFRAGMENT_context_create (NULL,
201 NUM_MSGS /* enough space for all */,
205 for (i=0;i<sizeof(buf);i++)
207 msg = (struct GNUNET_MessageHeader* ) buf;
208 for (i=0;i<NUM_MSGS;i++)
210 msg->type = htons ((uint16_t) i);
211 msg->size = htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
212 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */,
215 GNUNET_TIME_UNIT_SECONDS,
224 main (int argc, char *argv[])
226 struct GNUNET_GETOPT_CommandLineOption options[] = {
227 GNUNET_GETOPT_OPTION_END
229 char *const argv_prog[] = {
230 "test-fragmentation",
232 "test_fragmentation_data.conf",
243 GNUNET_log_setup ("test-fragmentation",
250 for (i=0;i<NUM_MSGS;i++)
251 GNUNET_BANDWIDTH_tracker_init (&trackers[i],
252 GNUNET_BANDWIDTH_value_init ((i+1) * 1024),
254 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options, &run, NULL);
256 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",