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 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.
16 * @file fragmentation/test_fragmentation.c
17 * @brief test for fragmentation.c
18 * @author Christian Grothoff
21 #include "gnunet_fragmentation_lib.h"
23 #define DETAILS GNUNET_NO
26 * Number of messages to transmit (note: each uses ~32k memory!)
31 * MTU to force on fragmentation (must be > 1k + 12)
36 * Simulate dropping of 1 out of how many messages? (must be > 1)
42 static unsigned int dups;
44 static unsigned int fragc;
46 static unsigned int frag_drops;
48 static unsigned int acks;
50 static unsigned int ack_drops;
52 static struct GNUNET_DEFRAGMENT_Context *defrag;
54 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
56 static struct GNUNET_FRAGMENT_Context *frag;
58 static struct GNUNET_SCHEDULER_Task * shutdown_task;
60 static struct GNUNET_TIME_Relative msg_delay;
62 static struct GNUNET_TIME_Relative ack_delay;
66 do_shutdown (void *cls)
70 GNUNET_DEFRAGMENT_context_destroy (defrag);
74 GNUNET_FRAGMENT_context_destroy (frag, &msg_delay, &ack_delay);
78 "\nFinal message-delay: %s\n",
79 GNUNET_STRINGS_relative_time_to_string (msg_delay,
82 "Final ack-delay: %s\n",
83 GNUNET_STRINGS_relative_time_to_string (ack_delay,
89 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
91 static unsigned int total;
96 FPRINTF (stderr, "%s", "M! "); /* message complete, good! */
98 buf = (const char *) hdr;
99 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
100 GNUNET_assert (buf[i] == (char) i);
103 if (0 == (total % (NUM_MSGS / 100)))
104 FPRINTF (stderr, "%s", ".");
106 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
107 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
109 if (NULL == shutdown_task)
110 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
116 * Process fragment (by passing to defrag).
119 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
121 struct GNUNET_FRAGMENT_Context **fc = cls;
124 GNUNET_FRAGMENT_context_transmission_done (*fc);
125 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
129 FPRINTF (stderr, "%s", "DF "); /* dropped Frag */
131 return; /* random drop */
135 FPRINTF (stderr, "%s", "?E "); /* Error: frag after shutdown!? */
138 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
139 if (ret == GNUNET_NO)
142 FPRINTF (stderr, "%s", "FF "); /* duplicate fragment */
146 else if (ret == GNUNET_OK)
149 FPRINTF (stderr, "%s", "F! "); /* good fragment */
159 static unsigned int i;
160 struct GNUNET_MessageHeader *msg;
161 static char buf[MTU + 32 * 1024];
166 for (j = 0; j < sizeof (buf); j++)
171 GNUNET_FRAGMENT_context_destroy (frag,
179 FPRINTF (stderr, "%s", "T! "); /* sending message */
181 msg = (struct GNUNET_MessageHeader *) buf;
182 msg->type = htons ((uint16_t) i);
184 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
185 frag = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
196 * Process ACK (by passing to fragmenter)
199 proc_acks (void *cls,
201 const struct GNUNET_MessageHeader *hdr)
206 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
210 FPRINTF (stderr, "%s", "DA "); /* dropped ACK */
212 return; /* random drop */
214 for (i = 0; i < NUM_MSGS; i++)
218 ret = GNUNET_FRAGMENT_process_ack (frag, hdr);
219 if (ret == GNUNET_OK)
222 FPRINTF (stderr, "%s", "GA "); /* good ACK */
224 next_transmission ();
228 if (ret == GNUNET_NO)
231 FPRINTF (stderr, "%s", "AA "); /* duplciate ACK */
238 FPRINTF (stderr, "%s", "?A "); /* BAD: ack that nobody feels responsible for... */
244 * Main function run with scheduler.
250 const struct GNUNET_CONFIGURATION_Handle *cfg)
252 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU,
257 next_transmission ();
262 main (int argc, char *argv[])
264 struct GNUNET_GETOPT_CommandLineOption options[] = {
265 GNUNET_GETOPT_OPTION_END
267 char *const argv_prog[] = {
268 "test-fragmentation",
270 "test_fragmentation_data.conf",
277 msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
278 ack_delay = GNUNET_TIME_UNIT_SECONDS;
279 GNUNET_log_setup ("test-fragmentation",
282 for (i = 0; i < NUM_MSGS; i++)
283 GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
284 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
286 GNUNET_PROGRAM_run (5,
288 "test-fragmentation", "nohelp",
292 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",