8f0be87a5d955ec5d041b248b5ff5f4880f4eb8f
[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_EXTRA_LOGGING
29
30 #define DETAILS GNUNET_NO
31
32 /**
33  * Number of messages to transmit (note: each uses ~32k memory!)
34  */
35 #define NUM_MSGS 5000
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 10
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,
69              const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   unsigned int i;
72
73   ret = 0;
74   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
75   GNUNET_DEFRAGMENT_context_destroy (defrag);
76   defrag = NULL;
77   for (i = 0; i < NUM_MSGS; i++)
78   {
79     if (frags[i] == NULL)
80       continue;
81     GNUNET_FRAGMENT_context_destroy (frags[i]);
82     frags[i] = NULL;
83   }  
84 }
85
86
87 static void
88 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
89 {
90   static unsigned int total;
91   unsigned int i;
92   const char *buf;
93
94 #if DETAILS
95   fprintf (stderr, "!");        /* message complete, good! */
96 #endif
97   buf = (const char *) hdr;
98   for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
99     GNUNET_assert (buf[i] == (char) i);
100   total++;
101 #if ! DETAILS
102   if (0 == (total % (NUM_MSGS / 100)))
103     fprintf (stderr, ".");
104 #endif
105   /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
106   if ( (total >= NUM_MSGS - (NUM_MSGS / 10)) &&
107        (ret != 0) )
108   {
109     if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
110       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
111   }
112 }
113
114
115 /**
116  * Process ACK (by passing to fragmenter)
117  */
118 static void
119 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
120 {
121   unsigned int i;
122   int ret;
123
124   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
125   {
126     ack_drops++;
127     return;                     /* random drop */
128   }
129   for (i = 0; i < NUM_MSGS; i++)
130   {
131     if (frags[i] == NULL)
132       continue;
133     ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
134     if (ret == GNUNET_OK)
135     {
136 #if DETAILS
137       fprintf (stderr, "@");    /* good ACK */
138 #endif
139       GNUNET_FRAGMENT_context_destroy (frags[i]);
140       frags[i] = NULL;
141       acks++;
142       return;
143     }
144     if (ret == GNUNET_NO)
145     {
146 #if DETAILS
147       fprintf (stderr, "@");    /* good ACK */
148 #endif
149       acks++;
150       return;
151     }
152   }
153 #if DETAILS
154   fprintf (stderr, "_");        /* BAD: ack that nobody feels responsible for... */
155 #endif
156 }
157
158
159 /**
160  * Process fragment (by passing to defrag).
161  */
162 static void
163 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
164 {
165   struct GNUNET_FRAGMENT_Context **fc = cls;
166   int ret;
167
168   GNUNET_FRAGMENT_context_transmission_done (*fc);
169   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
170   {
171     frag_drops++;
172     return;                     /* random drop */
173   }
174   if (NULL == defrag)
175   {
176     fprintf (stderr, "E");      /* Error: frag after shutdown!? */
177     return;
178   }
179   ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
180   if (ret == GNUNET_NO)
181   {
182 #if DETAILS
183     fprintf (stderr, "?");      /* duplicate fragment */
184 #endif
185     dups++;
186   }
187   else if (ret == GNUNET_OK)
188   {
189 #if DETAILS
190     fprintf (stderr, ".");      /* good fragment */
191 #endif
192     fragc++;
193   }
194 }
195
196
197 /**
198  * Main function run with scheduler.
199  */
200 static void
201 run (void *cls, char *const *args, const char *cfgfile,
202      const struct GNUNET_CONFIGURATION_Handle *cfg)
203 {
204   unsigned int i;
205   struct GNUNET_MessageHeader *msg;
206   char buf[MTU + 32 * 1024];
207
208   defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS        /* enough space for all */
209                                              , NULL, &proc_msgs, &proc_acks);
210   for (i = 0; i < sizeof (buf); i++)
211     buf[i] = (char) i;
212   msg = (struct GNUNET_MessageHeader *) buf;
213   for (i = 0; i < NUM_MSGS; i++)
214   {
215     msg->type = htons ((uint16_t) i);
216     msg->size =
217         htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
218     frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
219                                                MTU, &trackers[i],
220                                                GNUNET_TIME_UNIT_SECONDS, msg,
221                                                &proc_frac, &frags[i]);
222   }
223 }
224
225
226 int
227 main (int argc, char *argv[])
228 {
229   struct GNUNET_GETOPT_CommandLineOption options[] = {
230     GNUNET_GETOPT_OPTION_END
231   };
232   char *const argv_prog[] = {
233     "test-fragmentation",
234     "-c",
235     "test_fragmentation_data.conf",
236     "-L",
237 #if VERBOSE
238     "DEBUG",
239 #else
240     "WARNING",
241 #endif
242     NULL
243   };
244   unsigned int i;
245
246   GNUNET_log_setup ("test-fragmentation",
247 #if VERBOSE
248                     "DEBUG",
249 #else
250                     "WARNING",
251 #endif
252                     NULL);
253   for (i = 0; i < NUM_MSGS; i++)
254     GNUNET_BANDWIDTH_tracker_init (&trackers[i],
255                                    GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
256                                    100);
257   GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
258                       &run, NULL);
259   fprintf (stderr,
260            "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
261            fragc, dups, acks, ack_drops);
262   return ret;
263 }