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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 struct GNUNET_SCHEDULER_Task *shutdown_task;
67 do_shutdown (void *cls)
73 GNUNET_DEFRAGMENT_context_destroy (defrag);
75 for (i = 0; i < NUM_MSGS; i++)
79 GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
86 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
88 static unsigned int total;
93 FPRINTF (stderr, "%s", "!"); /* message complete, good! */
95 buf = (const char *) hdr;
96 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
97 GNUNET_assert (buf[i] == (char) i);
100 if (0 == (total % (NUM_MSGS / 100)))
101 FPRINTF (stderr, "%s", ".");
103 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
104 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
106 if (NULL == shutdown_task)
107 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
113 * Process ACK (by passing to fragmenter)
116 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
121 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
124 return; /* random drop */
126 for (i = 0; i < NUM_MSGS; i++)
128 if (frags[i] == NULL)
130 ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
131 if (ret == GNUNET_OK)
134 FPRINTF (stderr, "%s", "@"); /* good ACK */
136 GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
141 if (ret == GNUNET_NO)
144 FPRINTF (stderr, "%s", "@"); /* good ACK */
151 FPRINTF (stderr, "%s", "_"); /* BAD: ack that nobody feels responsible for... */
157 * Process fragment (by passing to defrag).
160 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
162 struct GNUNET_FRAGMENT_Context **fc = cls;
165 GNUNET_FRAGMENT_context_transmission_done (*fc);
166 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
169 return; /* random drop */
173 FPRINTF (stderr, "%s", "E"); /* Error: frag after shutdown!? */
176 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
177 if (ret == GNUNET_NO)
180 FPRINTF (stderr, "%s", "?"); /* duplicate fragment */
184 else if (ret == GNUNET_OK)
187 FPRINTF (stderr, "%s", "."); /* good fragment */
195 * Main function run with scheduler.
198 run (void *cls, char *const *args, const char *cfgfile,
199 const struct GNUNET_CONFIGURATION_Handle *cfg)
202 struct GNUNET_MessageHeader *msg;
203 char buf[MTU + 32 * 1024];
205 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
206 , NULL, &proc_msgs, &proc_acks);
207 for (i = 0; i < sizeof (buf); i++)
209 msg = (struct GNUNET_MessageHeader *) buf;
210 for (i = 0; i < NUM_MSGS; i++)
212 msg->type = htons ((uint16_t) i);
214 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
215 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
217 GNUNET_TIME_UNIT_MILLISECONDS,
218 GNUNET_TIME_UNIT_SECONDS,
220 &proc_frac, &frags[i]);
226 main (int argc, char *argv[])
228 struct GNUNET_GETOPT_CommandLineOption options[] = {
229 GNUNET_GETOPT_OPTION_END
231 char *const argv_prog[] = {
232 "test-fragmentation",
234 "test_fragmentation_data.conf",
241 GNUNET_log_setup ("test-fragmentation",
244 for (i = 0; i < NUM_MSGS; i++)
245 GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
246 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
248 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
251 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
252 fragc, dups, acks, ack_drops);