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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file fragmentation/test_fragmentation.c
20 * @brief test for fragmentation.c
21 * @author Christian Grothoff
24 #include "gnunet_fragmentation_lib.h"
26 #define DETAILS GNUNET_NO
29 * Number of messages to transmit (note: each uses ~32k memory!)
34 * MTU to force on fragmentation (must be > 1k + 12)
39 * Simulate dropping of 1 out of how many messages? (must be > 1)
45 static unsigned int dups;
47 static unsigned int fragc;
49 static unsigned int frag_drops;
51 static unsigned int acks;
53 static unsigned int ack_drops;
55 static struct GNUNET_DEFRAGMENT_Context *defrag;
57 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
59 static struct GNUNET_FRAGMENT_Context *frag;
61 static struct GNUNET_SCHEDULER_Task * shutdown_task;
63 static struct GNUNET_TIME_Relative msg_delay;
65 static struct GNUNET_TIME_Relative ack_delay;
69 do_shutdown (void *cls)
73 GNUNET_DEFRAGMENT_context_destroy (defrag);
77 GNUNET_FRAGMENT_context_destroy (frag, &msg_delay, &ack_delay);
81 "\nFinal message-delay: %s\n",
82 GNUNET_STRINGS_relative_time_to_string (msg_delay,
85 "Final ack-delay: %s\n",
86 GNUNET_STRINGS_relative_time_to_string (ack_delay,
92 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
94 static unsigned int total;
99 FPRINTF (stderr, "%s", "M! "); /* message complete, good! */
101 buf = (const char *) hdr;
102 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
103 GNUNET_assert (buf[i] == (char) i);
106 if (0 == (total % (NUM_MSGS / 100)))
107 FPRINTF (stderr, "%s", ".");
109 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
110 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
112 if (NULL == shutdown_task)
113 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
119 * Process fragment (by passing to defrag).
122 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
124 struct GNUNET_FRAGMENT_Context **fc = cls;
127 GNUNET_FRAGMENT_context_transmission_done (*fc);
128 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
132 FPRINTF (stderr, "%s", "DF "); /* dropped Frag */
134 return; /* random drop */
138 FPRINTF (stderr, "%s", "?E "); /* Error: frag after shutdown!? */
141 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
142 if (ret == GNUNET_NO)
145 FPRINTF (stderr, "%s", "FF "); /* duplicate fragment */
149 else if (ret == GNUNET_OK)
152 FPRINTF (stderr, "%s", "F! "); /* good fragment */
162 static unsigned int i;
163 struct GNUNET_MessageHeader *msg;
164 static char buf[MTU + 32 * 1024];
169 for (j = 0; j < sizeof (buf); j++)
174 GNUNET_FRAGMENT_context_destroy (frag,
182 FPRINTF (stderr, "%s", "T! "); /* sending message */
184 msg = (struct GNUNET_MessageHeader *) buf;
185 msg->type = htons ((uint16_t) i);
187 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
188 frag = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
199 * Process ACK (by passing to fragmenter)
202 proc_acks (void *cls,
204 const struct GNUNET_MessageHeader *hdr)
209 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
213 FPRINTF (stderr, "%s", "DA "); /* dropped ACK */
215 return; /* random drop */
217 for (i = 0; i < NUM_MSGS; i++)
221 ret = GNUNET_FRAGMENT_process_ack (frag, hdr);
222 if (ret == GNUNET_OK)
225 FPRINTF (stderr, "%s", "GA "); /* good ACK */
227 next_transmission ();
231 if (ret == GNUNET_NO)
234 FPRINTF (stderr, "%s", "AA "); /* duplciate ACK */
241 FPRINTF (stderr, "%s", "?A "); /* BAD: ack that nobody feels responsible for... */
247 * Main function run with scheduler.
253 const struct GNUNET_CONFIGURATION_Handle *cfg)
255 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU,
260 next_transmission ();
265 main (int argc, char *argv[])
267 struct GNUNET_GETOPT_CommandLineOption options[] = {
268 GNUNET_GETOPT_OPTION_END
270 char *const argv_prog[] = {
271 "test-fragmentation",
273 "test_fragmentation_data.conf",
280 msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
281 ack_delay = GNUNET_TIME_UNIT_SECONDS;
282 GNUNET_log_setup ("test-fragmentation",
285 for (i = 0; i < NUM_MSGS; i++)
286 GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
287 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
289 GNUNET_PROGRAM_run (5,
291 "test-fragmentation", "nohelp",
295 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",