-ignore
[oweals/gnunet.git] / src / fragmentation / test_fragmentation.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file fragmentation/test_fragmentation.c
22  * @brief test for fragmentation.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fragmentation_lib.h"
27
28 #define VERBOSE GNUNET_NO
29
30 #define DETAILS GNUNET_NO
31
32 /**
33  * Number of messages to transmit (note: each uses ~32k memory!)
34  */
35 #define NUM_MSGS 500
36
37 /**
38  * MTU to force on fragmentation (must be > 1k + 12)
39  */
40 #define MTU 1111
41
42 /**
43  * Simulate dropping of 1 out of how many messages? (must be > 1)
44  */
45 #define DROPRATE 5
46
47 static int ret = 1;
48
49 static unsigned int dups;
50
51 static unsigned int fragc;
52
53 static unsigned int frag_drops;
54
55 static unsigned int acks;
56
57 static unsigned int ack_drops;
58
59 static struct GNUNET_DEFRAGMENT_Context *defrag;
60
61 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
62
63 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
64
65 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
66
67 static void
68 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   unsigned int i;
71
72   ret = 0;
73   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
74   GNUNET_DEFRAGMENT_context_destroy (defrag);
75   defrag = NULL;
76   for (i = 0; i < NUM_MSGS; i++)
77   {
78     if (frags[i] == NULL)
79       continue;
80     GNUNET_FRAGMENT_context_destroy (frags[i]);
81     frags[i] = NULL;
82   }
83 }
84
85
86 static void
87 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
88 {
89   static unsigned int total;
90   unsigned int i;
91   const char *buf;
92
93 #if DETAILS
94   FPRINTF (stderr, "%s",  "!");        /* message complete, good! */
95 #endif
96   buf = (const char *) hdr;
97   for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
98     GNUNET_assert (buf[i] == (char) i);
99   total++;
100 #if ! DETAILS
101   if (0 == (total % (NUM_MSGS / 100)))
102     FPRINTF (stderr, "%s",  ".");
103 #endif
104   /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
105   if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
106   {
107     if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
108       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
109   }
110 }
111
112
113 /**
114  * Process ACK (by passing to fragmenter)
115  */
116 static void
117 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
118 {
119   unsigned int i;
120   int ret;
121
122   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
123   {
124     ack_drops++;
125     return;                     /* random drop */
126   }
127   for (i = 0; i < NUM_MSGS; i++)
128   {
129     if (frags[i] == NULL)
130       continue;
131     ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
132     if (ret == GNUNET_OK)
133     {
134 #if DETAILS
135       FPRINTF (stderr, "%s",  "@");    /* good ACK */
136 #endif
137       GNUNET_FRAGMENT_context_destroy (frags[i]);
138       frags[i] = NULL;
139       acks++;
140       return;
141     }
142     if (ret == GNUNET_NO)
143     {
144 #if DETAILS
145       FPRINTF (stderr, "%s",  "@");    /* good ACK */
146 #endif
147       acks++;
148       return;
149     }
150   }
151 #if DETAILS
152   FPRINTF (stderr, "%s",  "_");        /* BAD: ack that nobody feels responsible for... */
153 #endif
154 }
155
156
157 /**
158  * Process fragment (by passing to defrag).
159  */
160 static void
161 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
162 {
163   struct GNUNET_FRAGMENT_Context **fc = cls;
164   int ret;
165
166   GNUNET_FRAGMENT_context_transmission_done (*fc);
167   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
168   {
169     frag_drops++;
170     return;                     /* random drop */
171   }
172   if (NULL == defrag)
173   {
174     FPRINTF (stderr, "%s",  "E");      /* Error: frag after shutdown!? */
175     return;
176   }
177   ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
178   if (ret == GNUNET_NO)
179   {
180 #if DETAILS
181     FPRINTF (stderr, "%s",  "?");      /* duplicate fragment */
182 #endif
183     dups++;
184   }
185   else if (ret == GNUNET_OK)
186   {
187 #if DETAILS
188     FPRINTF (stderr, "%s",  ".");      /* good fragment */
189 #endif
190     fragc++;
191   }
192 }
193
194
195 /**
196  * Main function run with scheduler.
197  */
198 static void
199 run (void *cls, char *const *args, const char *cfgfile,
200      const struct GNUNET_CONFIGURATION_Handle *cfg)
201 {
202   unsigned int i;
203   struct GNUNET_MessageHeader *msg;
204   char buf[MTU + 32 * 1024];
205
206   defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS        /* enough space for all */
207                                              , NULL, &proc_msgs, &proc_acks);
208   for (i = 0; i < sizeof (buf); i++)
209     buf[i] = (char) i;
210   msg = (struct GNUNET_MessageHeader *) buf;
211   for (i = 0; i < NUM_MSGS; i++)
212   {
213     msg->type = htons ((uint16_t) i);
214     msg->size =
215         htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
216     frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
217                                                MTU, &trackers[i],
218                                                GNUNET_TIME_UNIT_SECONDS, msg,
219                                                &proc_frac, &frags[i]);
220   }
221 }
222
223
224 int
225 main (int argc, char *argv[])
226 {
227   struct GNUNET_GETOPT_CommandLineOption options[] = {
228     GNUNET_GETOPT_OPTION_END
229   };
230   char *const argv_prog[] = {
231     "test-fragmentation",
232     "-c",
233     "test_fragmentation_data.conf",
234     "-L",
235 #if VERBOSE
236     "DEBUG",
237 #else
238     "WARNING",
239 #endif
240     NULL
241   };
242   unsigned int i;
243
244   GNUNET_log_setup ("test-fragmentation",
245 #if VERBOSE
246                     "DEBUG",
247 #else
248                     "WARNING",
249 #endif
250                     NULL);
251   for (i = 0; i < NUM_MSGS; i++)
252     GNUNET_BANDWIDTH_tracker_init (&trackers[i],
253                                    GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
254                                    100);
255   GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
256                       &run, NULL);
257   FPRINTF (stderr,
258            "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
259            fragc, dups, acks, ack_drops);
260   return ret;
261 }