2 This file is part of GNUnet
3 Copyright (C) 2004, 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 DETAILS GNUNET_NO
31 * Number of messages to transmit (note: each uses ~32k memory!)
36 * MTU to force on fragmentation (must be > 1k + 12)
41 * Simulate dropping of 1 out of how many messages? (must be > 1)
47 static unsigned int dups;
49 static unsigned int fragc;
51 static unsigned int frag_drops;
53 static unsigned int acks;
55 static unsigned int ack_drops;
57 static struct GNUNET_DEFRAGMENT_Context *defrag;
59 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
61 static struct GNUNET_FRAGMENT_Context *frag;
63 static struct GNUNET_SCHEDULER_Task * shutdown_task;
65 static struct GNUNET_TIME_Relative msg_delay;
67 static struct GNUNET_TIME_Relative ack_delay;
71 do_shutdown (void *cls)
75 GNUNET_DEFRAGMENT_context_destroy (defrag);
79 GNUNET_FRAGMENT_context_destroy (frag, &msg_delay, &ack_delay);
83 "\nFinal message-delay: %s\n",
84 GNUNET_STRINGS_relative_time_to_string (msg_delay,
87 "Final ack-delay: %s\n",
88 GNUNET_STRINGS_relative_time_to_string (ack_delay,
94 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
96 static unsigned int total;
101 FPRINTF (stderr, "%s", "M! "); /* message complete, good! */
103 buf = (const char *) hdr;
104 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
105 GNUNET_assert (buf[i] == (char) i);
108 if (0 == (total % (NUM_MSGS / 100)))
109 FPRINTF (stderr, "%s", ".");
111 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
112 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
114 if (NULL == shutdown_task)
115 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
121 * Process fragment (by passing to defrag).
124 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
126 struct GNUNET_FRAGMENT_Context **fc = cls;
129 GNUNET_FRAGMENT_context_transmission_done (*fc);
130 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
134 FPRINTF (stderr, "%s", "DF "); /* dropped Frag */
136 return; /* random drop */
140 FPRINTF (stderr, "%s", "?E "); /* Error: frag after shutdown!? */
143 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
144 if (ret == GNUNET_NO)
147 FPRINTF (stderr, "%s", "FF "); /* duplicate fragment */
151 else if (ret == GNUNET_OK)
154 FPRINTF (stderr, "%s", "F! "); /* good fragment */
164 static unsigned int i;
165 struct GNUNET_MessageHeader *msg;
166 static char buf[MTU + 32 * 1024];
171 for (j = 0; j < sizeof (buf); j++)
176 GNUNET_FRAGMENT_context_destroy (frag,
184 FPRINTF (stderr, "%s", "T! "); /* sending message */
186 msg = (struct GNUNET_MessageHeader *) buf;
187 msg->type = htons ((uint16_t) i);
189 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
190 frag = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
201 * Process ACK (by passing to fragmenter)
204 proc_acks (void *cls,
206 const struct GNUNET_MessageHeader *hdr)
211 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
215 FPRINTF (stderr, "%s", "DA "); /* dropped ACK */
217 return; /* random drop */
219 for (i = 0; i < NUM_MSGS; i++)
223 ret = GNUNET_FRAGMENT_process_ack (frag, hdr);
224 if (ret == GNUNET_OK)
227 FPRINTF (stderr, "%s", "GA "); /* good ACK */
229 next_transmission ();
233 if (ret == GNUNET_NO)
236 FPRINTF (stderr, "%s", "AA "); /* duplciate ACK */
243 FPRINTF (stderr, "%s", "?A "); /* BAD: ack that nobody feels responsible for... */
249 * Main function run with scheduler.
255 const struct GNUNET_CONFIGURATION_Handle *cfg)
257 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU,
262 next_transmission ();
267 main (int argc, char *argv[])
269 struct GNUNET_GETOPT_CommandLineOption options[] = {
270 GNUNET_GETOPT_OPTION_END
272 char *const argv_prog[] = {
273 "test-fragmentation",
275 "test_fragmentation_data.conf",
282 msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
283 ack_delay = GNUNET_TIME_UNIT_SECONDS;
284 GNUNET_log_setup ("test-fragmentation",
287 for (i = 0; i < NUM_MSGS; i++)
288 GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
289 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
291 GNUNET_PROGRAM_run (5,
293 "test-fragmentation", "nohelp",
297 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",