~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-security

« back to all changes in this revision

Viewing changes to common/t-convert.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* t-convert.c - Module test for convert.c
2
 
 *      Copyright (C) 2006 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2006, 2008 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
20
20
#include <config.h>
21
21
#include <stdio.h>
22
22
#include <stdlib.h>
 
23
#include <assert.h>
23
24
 
24
25
#include "util.h"
25
26
 
275
276
 
276
277
 
277
278
 
 
279
static void
 
280
test_hex2str (void)
 
281
{
 
282
  static struct {
 
283
    const char *hex;
 
284
    const char *str;
 
285
    int off;
 
286
    int no_alloc_test;
 
287
  } tests[] = {
 
288
    /* Simple tests.  */
 
289
    { "112233445566778899aabbccddeeff1122",
 
290
      "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
 
291
      34 },
 
292
    { "112233445566778899aabbccddeeff1122 blah",
 
293
      "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
 
294
      34 },
 
295
    { "112233445566778899aabbccddeeff1122\tblah",
 
296
      "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
 
297
      34 },
 
298
    { "112233445566778899aabbccddeeff1122\nblah",
 
299
      "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
 
300
      34 },
 
301
    /* Valid tests yielding an empty string.  */
 
302
    { "00",
 
303
      "",
 
304
      2 },
 
305
    { "00 x",
 
306
      "",
 
307
      2 },
 
308
    { "",
 
309
      "",
 
310
      0 },
 
311
    { " ",
 
312
      "",
 
313
      0 },
 
314
    /* Test trailing Nul feature.  */
 
315
    { "112233445566778899aabbccddeeff112200",
 
316
      "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
 
317
      36 },
 
318
    { "112233445566778899aabbccddeeff112200 ",
 
319
      "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
 
320
      36 },
 
321
    /* Test buffer size. (buffer is of length 20)  */
 
322
    { "6162636465666768696A6b6c6D6e6f70717273",
 
323
      "abcdefghijklmnopqrs",
 
324
      38 },
 
325
    { "6162636465666768696A6b6c6D6e6f7071727300",
 
326
      "abcdefghijklmnopqrs",
 
327
      40 },
 
328
    { "6162636465666768696A6b6c6D6e6f7071727374",
 
329
      NULL,
 
330
      0, 1 },
 
331
    { "6162636465666768696A6b6c6D6e6f707172737400",
 
332
      NULL,
 
333
      0, 1 },
 
334
    { "6162636465666768696A6b6c6D6e6f707172737475",
 
335
      NULL,
 
336
      0, 1 },
 
337
 
 
338
    /* Invalid tests. */
 
339
    { "112233445566778899aabbccddeeff1122334",      NULL, 0 },
 
340
    { "112233445566778899AABBCCDDEEFF1122334",      NULL, 0 },
 
341
    { "112233445566778899AABBCCDDEEFG11223344",     NULL, 0 },
 
342
    { "0:0112233445566778899aabbccddeeff11223344",  NULL, 0 },
 
343
    { "112233445566778899aabbccddeeff11223344:",    NULL, 0 },
 
344
    { "112233445566778899aabbccddeeff112233445",    NULL, 0 },
 
345
    { "112233445566778899aabbccddeeff1122334455",   NULL, 0, 1 },
 
346
    { "112233445566778899aabbccddeeff11223344blah", NULL, 0 },
 
347
    { "0",    NULL, 0 },
 
348
    { "00:",  NULL, 0 },
 
349
    { "00x",  NULL, 0 },
 
350
 
 
351
    { NULL, NULL, 0 }
 
352
  };
 
353
 
 
354
  int idx;
 
355
  char buffer[20];
 
356
  const char *tail;
 
357
  size_t count;
 
358
  char *result;
 
359
 
 
360
  for (idx=0; tests[idx].hex; idx++)
 
361
    {
 
362
      tail = hex2str (tests[idx].hex, buffer, sizeof buffer, &count);
 
363
      if (tests[idx].str)
 
364
        {
 
365
          /* Good case test.  */
 
366
          if (!tail)
 
367
            fail (idx);
 
368
          else if (strcmp (tests[idx].str, buffer))
 
369
            fail (idx);
 
370
          else if (tail - tests[idx].hex != tests[idx].off)
 
371
            fail (idx);
 
372
          else if (strlen (buffer) != count)
 
373
            fail (idx);
 
374
        }
 
375
      else
 
376
        {
 
377
          /* Bad case test.  */
 
378
          if (tail)
 
379
            fail (idx);
 
380
        }
 
381
    }
 
382
 
 
383
  /* Same tests again using in-place conversion.  */
 
384
  for (idx=0; tests[idx].hex; idx++)
 
385
    {
 
386
      char tmpbuf[100];
 
387
      
 
388
      assert (strlen (tests[idx].hex)+1 < sizeof tmpbuf);
 
389
      strcpy (tmpbuf, tests[idx].hex);
 
390
      
 
391
      /* Note: we still need to use 20 as buffer length because our
 
392
         tests assume that. */
 
393
      tail = hex2str (tmpbuf, tmpbuf, 20, &count);
 
394
      if (tests[idx].str)
 
395
        {
 
396
          /* Good case test.  */
 
397
          if (!tail)
 
398
            fail (idx);
 
399
          else if (strcmp (tests[idx].str, tmpbuf))
 
400
            fail (idx);
 
401
          else if (tail - tmpbuf != tests[idx].off)
 
402
            fail (idx);
 
403
          else if (strlen (tmpbuf) != count)
 
404
            fail (idx);
 
405
        }
 
406
      else
 
407
        {
 
408
          /* Bad case test.  */
 
409
          if (tail)
 
410
            fail (idx);
 
411
          if (strcmp (tmpbuf, tests[idx].hex))
 
412
            fail (idx); /* Buffer was modified.  */
 
413
        }
 
414
    }
 
415
 
 
416
  /* Test the allocation variant.  */
 
417
  for (idx=0; tests[idx].hex; idx++)
 
418
    {
 
419
      if (tests[idx].no_alloc_test)
 
420
        continue;
 
421
 
 
422
      result = hex2str_alloc (tests[idx].hex, &count);
 
423
      if (tests[idx].str)
 
424
        {
 
425
          /* Good case test.  */
 
426
          if (!result)
 
427
            fail (idx);
 
428
          else if (strcmp (tests[idx].str, result))
 
429
            fail (idx);
 
430
          else if (count != tests[idx].off)
 
431
            fail (idx);
 
432
        }
 
433
      else
 
434
        {
 
435
          /* Bad case test.  */
 
436
          if (result)
 
437
            fail (idx);
 
438
        }
 
439
      xfree (result);
 
440
    }
 
441
}
 
442
 
 
443
 
 
444
 
 
445
 
278
446
 
279
447
int
280
448
main (int argc, char **argv)
281
449
{
 
450
  (void)argc;
 
451
  (void)argv;
282
452
  
283
453
  test_hex2bin ();
284
454
  test_hexcolon2bin ();
285
455
  test_bin2hex ();
286
456
  test_bin2hexcolon ();
 
457
  test_hex2str ();
287
458
 
288
459
  return 0;
289
460
}