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