aboutsummaryrefslogtreecommitdiff
path: root/src/fragmentation/test_fragmentation.c
blob: 1aec7bf3607ffa831a893c3420edf55dcd5942a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
     This file is part of GNUnet
     (C) 2004, 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file fragmentation/test_fragmentation.c
 * @brief test for fragmentation.c
 * @author Christian Grothoff
 */

/**
 * Testcase for defragmentation code.
 * We have testcases for:
 * - 2 fragments, aligned, [0,16),[16,32)
 * - n (50) fragments, [i*16,(i+1)*16)
 * - n (50) fragments, [0,i*16) + [50*16,51*16)
 * - n (100) fragments, inserted in interleaved order (holes in sequence)
 * - holes in sequence
 * - other overlaps
 * - timeouts
 * - multiple entries in GNUNET_hash-list
 * - id collisions in GNUNET_hash-list
 */

#include "platform.h"
#include "gnunet_fragmentation_lib.h"

#if 0

/* -- to speed up the testcases -- */
#define DEFRAGMENTATION_TIMEOUT (1 * GNUNET_CRON_SECONDS)


static GNUNET_PeerIdentity mySender;
static char *myMsg;
static unsigned short myMsgLen;

/* static buffers to avoid lots of malloc/free */
static char masterBuffer[65536];
static char resultBuffer[65536];

static void
handleHelper (const GNUNET_PeerIdentity * sender,
              const char *msg,
              const unsigned int len, int wasEncrypted, GNUNET_TSession * ts)
{
  GNUNET_GE_ASSERT (NULL,
                    0 == memcmp (sender, &mySender,
                                 sizeof (GNUNET_PeerIdentity)));
  myMsg = resultBuffer;
  memcpy (resultBuffer, msg, len);
  myMsgLen = len;
}

/**
 * Wait long enough to force all fragments to timeout.
 */
static void
makeTimeout ()
{
  GNUNET_thread_sleep (DEFRAGMENTATION_TIMEOUT * 2);
  defragmentationPurgeCron (NULL);
}

/**
 * Create a fragment. The data-portion will be filled
 * with a sequence of numbers from start+id to start+len-1+id.
 *
 * @param pep pointer to the ethernet frame/buffer
 * @param ip pointer to the ip-header
 * @param start starting-offset
 * @param length of the data portion
 * @param id the identity of the fragment
 */
static GNUNET_MessageHeader *
makeFragment (unsigned short start,
              unsigned short size, unsigned short tot, int id)
{
  P2P_fragmentation_MESSAGE *frag;
  int i;

  frag = (P2P_fragmentation_MESSAGE *) masterBuffer;
  frag->id = htonl (id);
  frag->off = htons (start);
  frag->len = htons (tot);
  frag->header.size = htons (sizeof (P2P_fragmentation_MESSAGE) + size);

  for (i = 0; i < size; i++)
    ((char *) &frag[1])[i] = (char) i + id + start;
  return &frag->header;
}

/**
 * Check that the packet received is what we expected to
 * get.
 * @param id the expected id
 * @param len the expected length
 */
static void
checkPacket (int id, unsigned int len)
{
  int i;

  GNUNET_GE_ASSERT (NULL, myMsg != NULL);
  GNUNET_GE_ASSERT (NULL, myMsgLen == len);
  for (i = 0; i < len; i++)
    GNUNET_GE_ASSERT (NULL, myMsg[i] == (char) (i + id));
  myMsgLen = 0;
  myMsg = NULL;
}


/* **************** actual testcases ***************** */

static void
testSimpleFragment ()
{
  GNUNET_MessageHeader *pep;

  pep = makeFragment (0, 16, 32, 42);
  processFragment (&mySender, pep);
  GNUNET_GE_ASSERT (NULL, myMsg == NULL);
  pep = makeFragment (16, 16, 32, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 32);
}

static void
testSimpleFragmentTimeout ()
{
  GNUNET_MessageHeader *pep;

  pep = makeFragment (0, 16, 32, 42);
  processFragment (&mySender, pep);
  GNUNET_GE_ASSERT (NULL, myMsg == NULL);
  makeTimeout ();
  pep = makeFragment (16, 16, 32, 42);
  processFragment (&mySender, pep);
  GNUNET_GE_ASSERT (NULL, myMsg == NULL);
  pep = makeFragment (0, 16, 32, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 32);
}

static void
testSimpleFragmentReverse ()
{
  GNUNET_MessageHeader *pep;

  pep = makeFragment (16, 16, 32, 42);
  processFragment (&mySender, pep);
  GNUNET_GE_ASSERT (NULL, myMsg == NULL);
  pep = makeFragment (0, 16, 32, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 32);
}

static void
testManyFragments ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (i * 16, 16, 51 * 16, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  pep = makeFragment (50 * 16, 16, 51 * 16, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 51 * 16);
}

static void
testManyFragmentsMegaLarge ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 4000; i++)
    {
      pep = makeFragment (i * 16, 16, 4001 * 16, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  pep = makeFragment (4000 * 16, 16, 4001 * 16, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 4001 * 16);
}

static void
testLastFragmentEarly ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 5; i++)
    {
      pep = makeFragment (i * 16, 8, 6 * 16 + 8, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  pep = makeFragment (5 * 16, 24, 6 * 16 + 8, 42);
  processFragment (&mySender, pep);
  for (i = 0; i < 5; i++)
    {
      pep = makeFragment (i * 16 + 8, 8, 6 * 16 + 8, 42);
      processFragment (&mySender, pep);
    }
  checkPacket (42, 6 * 16 + 8);
}

static void
testManyInterleavedFragments ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (i * 16, 8, 51 * 16 + 8, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (i * 16 + 8, 8, 51 * 16 + 8, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  pep = makeFragment (50 * 16, 24, 51 * 16 + 8, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 51 * 16 + 8);
}

static void
testManyInterleavedOverlappingFragments ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (i * 32, 16, 51 * 32, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (i * 32 + 8, 24, 51 * 32, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  pep = makeFragment (50 * 32, 32, 51 * 32, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 51 * 32);
}

static void
testManyOverlappingFragments ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (0, i * 16 + 16, 51 * 16, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  pep = makeFragment (50 * 16, 16, 51 * 16, 42);
  processFragment (&mySender, pep);
  checkPacket (42, 51 * 16);
}

static void
testManyOverlappingFragmentsTimeout ()
{
  GNUNET_MessageHeader *pep;
  int i;

  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (0, i * 16 + 16, 51 * 16 + 8, 42);
      processFragment (&mySender, pep);
      GNUNET_GE_ASSERT (NULL, myMsg == NULL);
    }
  makeTimeout ();
  pep = makeFragment (50 * 16, 24, 51 * 16 + 8, 42);
  processFragment (&mySender, pep);
  GNUNET_GE_ASSERT (NULL, myMsg == NULL);
  for (i = 0; i < 50; i++)
    {
      pep = makeFragment (0, i * 16 + 16, 51 * 16 + 8, 42);
      processFragment (&mySender, pep);
    }
  checkPacket (42, 51 * 16 + 8);
}

static void
testManyFragmentsMultiId ()
{
  GNUNET_MessageHeader *pep;
  int i;
  int id;

  for (i = 0; i < 50; i++)
    {
      for (id = 0; id < DEFRAG_BUCKET_COUNT; id++)
        {
          pep = makeFragment (i * 16, 16, 51 * 16, id + 5);
          mySender.hashPubKey.bits[0] = id;
          processFragment (&mySender, pep);
          GNUNET_GE_ASSERT (NULL, myMsg == NULL);
        }
    }
  for (id = 0; id < DEFRAG_BUCKET_COUNT; id++)
    {
      pep = makeFragment (50 * 16, 16, 51 * 16, id + 5);
      mySender.hashPubKey.bits[0] = id;
      processFragment (&mySender, pep);
      checkPacket (id + 5, 51 * 16);
    }
}

static void
testManyFragmentsMultiIdCollisions ()
{
  GNUNET_MessageHeader *pep;
  int i;
  int id;

  for (i = 0; i < 5; i++)
    {
      for (id = 0; id < DEFRAG_BUCKET_COUNT * 4; id++)
        {
          pep = makeFragment (i * 16, 16, 6 * 16, id + 5);
          mySender.hashPubKey.bits[0] = id;
          processFragment (&mySender, pep);
          GNUNET_GE_ASSERT (NULL, myMsg == NULL);
        }
    }
  for (id = 0; id < DEFRAG_BUCKET_COUNT * 4; id++)
    {
      pep = makeFragment (5 * 16, 16, 6 * 16, id + 5);
      mySender.hashPubKey.bits[0] = id;
      processFragment (&mySender, pep);
      checkPacket (id + 5, 6 * 16);
    }
}

/* ************* driver ****************** */

static int
p2p_register_handler (const unsigned short type,
                      GNUNET_P2PRequestHandler callback)
{
  return GNUNET_OK;
}

static int
p2p_unregister_handler (const unsigned short type,
                        GNUNET_P2PRequestHandler callback)
{
  return GNUNET_OK;
}


static void *
request_service (const char *name)
{
  return NULL;
}

#endif

int
main (int argc, char *argv[])
{
  fprintf (stderr, "WARNING: testcase not yet ported to new API.\n");
#if 0
  GNUNET_CoreAPIForPlugins capi;

  memset (&capi, 0, sizeof (GNUNET_CoreAPIForPlugins));
  capi.cron = GNUNET_cron_create (NULL);
  capi.loopback_send = &handleHelper;
  capi.service_request = &request_service;
  capi.p2p_ciphertext_handler_register = &p2p_register_handler;
  capi.p2p_ciphertext_handler_unregister = &p2p_unregister_handler;
  provide_module_fragmentation (&capi);

  fprintf (stderr, ".");
  testSimpleFragment ();
  fprintf (stderr, ".");
  testSimpleFragmentTimeout ();
  fprintf (stderr, ".");
  testSimpleFragmentReverse ();
  fprintf (stderr, ".");
  testManyFragments ();
  fprintf (stderr, ".");
  testManyFragmentsMegaLarge ();
  fprintf (stderr, ".");
  testManyFragmentsMultiId ();
  fprintf (stderr, ".");

  testManyInterleavedFragments ();
  fprintf (stderr, ".");
  testManyInterleavedOverlappingFragments ();
  fprintf (stderr, ".");
  testManyOverlappingFragments ();
  fprintf (stderr, ".");
  testManyOverlappingFragmentsTimeout ();
  fprintf (stderr, ".");
  testLastFragmentEarly ();
  fprintf (stderr, ".");
  testManyFragmentsMultiIdCollisions ();
  fprintf (stderr, ".");
  release_module_fragmentation ();
  fprintf (stderr, "\n");
  GNUNET_cron_destroy (capi.cron);
#endif
  return 0;                     /* testcase passed */
}