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