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 *frags[NUM_MSGS];
61 static struct GNUNET_SCHEDULER_Task *shutdown_task;
65 do_shutdown (void *cls)
71 GNUNET_DEFRAGMENT_context_destroy (defrag);
73 for (i = 0; i < NUM_MSGS; i++)
77 GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
84 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
86 static unsigned int total;
91 FPRINTF (stderr, "%s", "!"); /* message complete, good! */
93 buf = (const char *) hdr;
94 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
95 GNUNET_assert (buf[i] == (char) i);
98 if (0 == (total % (NUM_MSGS / 100)))
99 FPRINTF (stderr, "%s", ".");
101 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
102 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
104 if (NULL == shutdown_task)
105 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
111 * Process ACK (by passing to fragmenter)
114 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
119 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
122 return; /* random drop */
124 for (i = 0; i < NUM_MSGS; i++)
126 if (frags[i] == NULL)
128 ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
129 if (ret == GNUNET_OK)
132 FPRINTF (stderr, "%s", "@"); /* good ACK */
134 GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
139 if (ret == GNUNET_NO)
142 FPRINTF (stderr, "%s", "@"); /* good ACK */
149 FPRINTF (stderr, "%s", "_"); /* BAD: ack that nobody feels responsible for... */
155 * Process fragment (by passing to defrag).
158 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
160 struct GNUNET_FRAGMENT_Context **fc = cls;
163 GNUNET_FRAGMENT_context_transmission_done (*fc);
164 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
167 return; /* random drop */
171 FPRINTF (stderr, "%s", "E"); /* Error: frag after shutdown!? */
174 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
175 if (ret == GNUNET_NO)
178 FPRINTF (stderr, "%s", "?"); /* duplicate fragment */
182 else if (ret == GNUNET_OK)
185 FPRINTF (stderr, "%s", "."); /* good fragment */
193 * Main function run with scheduler.
196 run (void *cls, char *const *args, const char *cfgfile,
197 const struct GNUNET_CONFIGURATION_Handle *cfg)
200 struct GNUNET_MessageHeader *msg;
201 char buf[MTU + 32 * 1024];
203 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
204 , NULL, &proc_msgs, &proc_acks);
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);
212 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
213 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
215 GNUNET_TIME_UNIT_MILLISECONDS,
216 GNUNET_TIME_UNIT_SECONDS,
218 &proc_frac, &frags[i]);
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",
239 GNUNET_log_setup ("test-fragmentation",
242 for (i = 0; i < NUM_MSGS; i++)
243 GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
244 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
246 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
249 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
250 fragc, dups, acks, ack_drops);