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 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 *frags[NUM_MSGS];
63 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
66 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
72 GNUNET_DEFRAGMENT_context_destroy (defrag);
74 for (i = 0; i < NUM_MSGS; i++)
78 GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
85 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
87 static unsigned int total;
92 FPRINTF (stderr, "%s", "!"); /* message complete, good! */
94 buf = (const char *) hdr;
95 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
96 GNUNET_assert (buf[i] == (char) i);
99 if (0 == (total % (NUM_MSGS / 100)))
100 FPRINTF (stderr, "%s", ".");
102 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
103 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
105 if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
106 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
112 * Process ACK (by passing to fragmenter)
115 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
120 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
123 return; /* random drop */
125 for (i = 0; i < NUM_MSGS; i++)
127 if (frags[i] == NULL)
129 ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
130 if (ret == GNUNET_OK)
133 FPRINTF (stderr, "%s", "@"); /* good ACK */
135 GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
140 if (ret == GNUNET_NO)
143 FPRINTF (stderr, "%s", "@"); /* good ACK */
150 FPRINTF (stderr, "%s", "_"); /* BAD: ack that nobody feels responsible for... */
156 * Process fragment (by passing to defrag).
159 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
161 struct GNUNET_FRAGMENT_Context **fc = cls;
164 GNUNET_FRAGMENT_context_transmission_done (*fc);
165 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
168 return; /* random drop */
172 FPRINTF (stderr, "%s", "E"); /* Error: frag after shutdown!? */
175 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
176 if (ret == GNUNET_NO)
179 FPRINTF (stderr, "%s", "?"); /* duplicate fragment */
183 else if (ret == GNUNET_OK)
186 FPRINTF (stderr, "%s", "."); /* good fragment */
194 * Main function run with scheduler.
197 run (void *cls, char *const *args, const char *cfgfile,
198 const struct GNUNET_CONFIGURATION_Handle *cfg)
201 struct GNUNET_MessageHeader *msg;
202 char buf[MTU + 32 * 1024];
204 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
205 , NULL, &proc_msgs, &proc_acks);
206 for (i = 0; i < sizeof (buf); i++)
208 msg = (struct GNUNET_MessageHeader *) buf;
209 for (i = 0; i < NUM_MSGS; i++)
211 msg->type = htons ((uint16_t) i);
213 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
214 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
216 GNUNET_TIME_UNIT_MILLISECONDS,
217 GNUNET_TIME_UNIT_SECONDS,
219 &proc_frac, &frags[i]);
225 main (int argc, char *argv[])
227 struct GNUNET_GETOPT_CommandLineOption options[] = {
228 GNUNET_GETOPT_OPTION_END
230 char *const argv_prog[] = {
231 "test-fragmentation",
233 "test_fragmentation_data.conf",
240 GNUNET_log_setup ("test-fragmentation",
243 for (i = 0; i < NUM_MSGS; i++)
244 GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
245 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
247 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
250 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
251 fragc, dups, acks, ack_drops);