LCOV - code coverage report
Current view: top level - common - t-convert.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 106 155 68.4 %
Date: 2015-11-05 17:10:59 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* t-convert.c - Module test for convert.c
       2             :  *      Copyright (C) 2006, 2008 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       6             :  * GnuPG is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * GnuPG is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : #include <assert.h>
      24             : 
      25             : #include "util.h"
      26             : 
      27             : #define pass()  do { ; } while(0)
      28             : #define fail(a)  do { fprintf (stderr, "%s:%d: test %d failed\n",\
      29             :                                __FILE__,__LINE__, (a));          \
      30             :     /*exit (1)*/;                                                \
      31             :                    } while(0)
      32             : 
      33             : 
      34             : static void
      35           1 : test_hex2bin (void)
      36             : {
      37             :   static const char *valid[] = {
      38             :     "00112233445566778899aabbccddeeff11223344",
      39             :     "00112233445566778899AABBCCDDEEFF11223344",
      40             :     "00112233445566778899AABBCCDDEEFF11223344 blah",
      41             :     "00112233445566778899AABBCCDDEEFF11223344\tblah",
      42             :     "00112233445566778899AABBCCDDEEFF11223344\nblah",
      43             :     NULL
      44             :   };
      45             :   static const char *invalid[] = {
      46             :     "00112233445566778899aabbccddeeff1122334",
      47             :     "00112233445566778899AABBCCDDEEFF1122334",
      48             :     "00112233445566778899AABBCCDDEEFG11223344",
      49             :     "00 112233445566778899aabbccddeeff11223344",
      50             :     "00:112233445566778899aabbccddeeff11223344",
      51             :     ":00112233445566778899aabbccddeeff11223344",
      52             :     "0:0112233445566778899aabbccddeeff11223344",
      53             :     "00112233445566778899aabbccddeeff11223344:",
      54             :     "00112233445566778899aabbccddeeff112233445",
      55             :     "00112233445566778899aabbccddeeff1122334455",
      56             :     "00112233445566778899aabbccddeeff11223344blah",
      57             :     NULL
      58             :   };
      59             :   static const char *valid2[] = {
      60             :     "00",
      61             :     "00 x",
      62             :     NULL
      63             :   };
      64             :   static const char *invalid2[] = {
      65             :     "",
      66             :     "0",
      67             :     "00:",
      68             :     "00x",
      69             :     " 00",
      70             :     NULL
      71             :   };
      72             :   unsigned char buffer[20];
      73             :   int len;
      74             :   int i;
      75             : 
      76             : 
      77           6 :   for (i=0; valid[i]; i++)
      78             :     {
      79           5 :       len = hex2bin (valid[i], buffer, sizeof buffer);
      80           5 :       if (len < 0)
      81           0 :         fail (i);
      82           5 :       if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
      83             :                            "\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
      84           0 :           fail (i);
      85             :     }
      86           1 :   if (hex2bin (valid[0], buffer, sizeof buffer) != 40)
      87           0 :     fail (0);
      88           1 :   if (hex2bin (valid[2], buffer, sizeof buffer) != 41)
      89           0 :     fail (0);
      90             : 
      91          12 :   for (i=0; invalid[i]; i++)
      92             :     {
      93          11 :       len = hex2bin (invalid[i], buffer, sizeof buffer);
      94          11 :       if (!(len < 0))
      95           0 :         fail (i);
      96             :     }
      97             : 
      98           3 :   for (i=0; valid2[i]; i++)
      99             :     {
     100           2 :       len = hex2bin (valid2[i], buffer, 1);
     101           2 :       if (len < 0)
     102           0 :         fail (i);
     103           2 :       if (memcmp (buffer, "\x00", 1))
     104           0 :         fail (i);
     105             :     }
     106           1 :   if (hex2bin (valid2[0], buffer, 1) != 2)
     107           0 :     fail (0);
     108           1 :   if (hex2bin (valid2[1], buffer, 1) != 3)
     109           0 :     fail (0);
     110             : 
     111           6 :   for (i=0; invalid2[i]; i++)
     112             :     {
     113           5 :       len = hex2bin (invalid2[i], buffer, 1);
     114           5 :       if (!(len < 0))
     115           0 :         fail (i);
     116             :     }
     117           1 : }
     118             : 
     119             : 
     120             : 
     121             : static void
     122           1 : test_hexcolon2bin (void)
     123             : {
     124             :   static const char *valid[] = {
     125             :     "00112233445566778899aabbccddeeff11223344",
     126             :     "00112233445566778899AABBCCDDEEFF11223344",
     127             :     "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
     128             :     "00112233445566778899AABBCCDDEEFF11223344 blah",
     129             :     "00112233445566778899AABBCCDDEEFF11223344\tblah",
     130             :     "00112233445566778899AABBCCDDEEFF11223344\nblah",
     131             :     NULL
     132             :   };
     133             :   static const char *invalid[] = {
     134             :     "00112233445566778899aabbccddeeff1122334",
     135             :     "00112233445566778899AABBCCDDEEFF1122334",
     136             :     "00112233445566778899AABBCCDDEEFG11223344",
     137             :     ":00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
     138             :     "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44:",
     139             :     "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:3344",
     140             :     "00:1122:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
     141             :     "0011:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
     142             :     "00 11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
     143             :     "00:11 22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:11:22:33:44",
     144             :     "00112233445566778899aabbccddeeff112233445",
     145             :     "00112233445566778899aabbccddeeff1122334455",
     146             :     "00112233445566778899aabbccddeeff11223344blah",
     147             :     NULL
     148             :   };
     149             :   static const char *valid2[] = {
     150             :     "00",
     151             :     "00 x",
     152             :     NULL
     153             :   };
     154             :   static const char *invalid2[] = {
     155             :     "",
     156             :     "0",
     157             :     "00:",
     158             :     ":00",
     159             :     "0:0",
     160             :     "00x",
     161             :     " 00",
     162             :     NULL
     163             :   };
     164             :   unsigned char buffer[20];
     165             :   int len;
     166             :   int i;
     167             : 
     168             : 
     169           7 :   for (i=0; valid[i]; i++)
     170             :     {
     171           6 :       len = hexcolon2bin (valid[i], buffer, sizeof buffer);
     172           6 :       if (len < 0)
     173           0 :         fail (i);
     174           6 :       if (memcmp (buffer, ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
     175             :                            "\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44"), 20))
     176           0 :           fail (i);
     177             :     }
     178           1 :   if (hexcolon2bin (valid[0], buffer, sizeof buffer) != 40)
     179           0 :     fail (0);
     180           1 :   if (hexcolon2bin (valid[3], buffer, sizeof buffer) != 41)
     181           0 :     fail (0);
     182             : 
     183          14 :   for (i=0; invalid[i]; i++)
     184             :     {
     185          13 :       len = hexcolon2bin (invalid[i], buffer, sizeof buffer);
     186          13 :       if (!(len < 0))
     187           0 :         fail (i);
     188             :     }
     189             : 
     190           3 :   for (i=0; valid2[i]; i++)
     191             :     {
     192           2 :       len = hexcolon2bin (valid2[i], buffer, 1);
     193           2 :       if (len < 0)
     194           0 :         fail (i);
     195           2 :       if (memcmp (buffer, "\x00", 1))
     196           0 :         fail (i);
     197             :     }
     198           1 :   if (hexcolon2bin (valid2[0], buffer, 1) != 2)
     199           0 :     fail (0);
     200           1 :   if (hexcolon2bin (valid2[1], buffer, 1) != 3)
     201           0 :     fail (0);
     202             : 
     203           8 :   for (i=0; invalid2[i]; i++)
     204             :     {
     205           7 :       len = hexcolon2bin (invalid2[i], buffer, 1);
     206           7 :       if (!(len < 0))
     207           0 :         fail (i);
     208             :     }
     209             : 
     210             : 
     211           1 : }
     212             : 
     213             : 
     214             : 
     215             : static void
     216           1 : test_bin2hex (void)
     217             : {
     218           1 :   char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
     219             :                       "\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
     220           1 :   char hexstuff[] = "00112233445566778899AABBCCDDEEFF011002A3";
     221             :   char buffer[2*20+1];
     222             :   char *p;
     223             : 
     224           1 :   p = bin2hex (stuff, 20, buffer);
     225           1 :   if (!p)
     226           0 :     fail (0);
     227           1 :   if (p != buffer)
     228           0 :     fail (0);
     229           1 :   if (strcmp (buffer, hexstuff))
     230           0 :     fail (0);
     231             : 
     232           1 :   p = bin2hex (stuff, 20, NULL);
     233           1 :   if (!p)
     234           0 :     fail (0);
     235           1 :   if (strcmp (p, hexstuff))
     236           0 :     fail (0);
     237             : 
     238           1 :   p = bin2hex (stuff, (size_t)(-1), NULL);
     239           1 :   if (p)
     240           0 :     fail (0);
     241           1 :   if (errno != ENOMEM)
     242           0 :     fail (1);
     243           1 : }
     244             : 
     245             : 
     246             : static void
     247           1 : test_bin2hexcolon (void)
     248             : {
     249           1 :   char stuff[20+1] = ("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa"
     250             :                       "\xbb\xcc\xdd\xee\xff\x01\x10\x02\xa3");
     251           1 :   char hexstuff[] = ("00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF"
     252             :                      ":01:10:02:A3");
     253             :   char buffer[3*20+1];
     254             :   char *p;
     255             : 
     256           1 :   p = bin2hexcolon (stuff, 20, buffer);
     257           1 :   if (!p)
     258           0 :     fail (0);
     259           1 :   if (p != buffer)
     260           0 :     fail (0);
     261           1 :   if (strcmp (buffer, hexstuff))
     262           0 :     fail (0);
     263             : 
     264           1 :   p = bin2hexcolon (stuff, 20, NULL);
     265           1 :   if (!p)
     266           0 :     fail (0);
     267           1 :   if (strcmp (p, hexstuff))
     268           0 :     fail (0);
     269             : 
     270           1 :   p = bin2hexcolon (stuff, (size_t)(-1), NULL);
     271           1 :   if (p)
     272           0 :     fail (0);
     273           1 :   if (errno != ENOMEM)
     274           0 :     fail (1);
     275           1 : }
     276             : 
     277             : 
     278             : 
     279             : static void
     280           1 : test_hex2str (void)
     281             : {
     282             :   static struct {
     283             :     const char *hex;
     284             :     const char *str;
     285             :     int len; /* Length of STR.  This may included embedded nuls.  */
     286             :     int off;
     287             :     int no_alloc_test;
     288             :   } tests[] = {
     289             :     /* Simple tests.  */
     290             :     { "112233445566778899aabbccddeeff1122",
     291             :       "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
     292             :       17, 34 },
     293             :     { "112233445566778899aabbccddeeff1122 blah",
     294             :       "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
     295             :       17, 34 },
     296             :     { "112233445566778899aabbccddeeff1122\tblah",
     297             :       "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
     298             :       17, 34 },
     299             :     { "112233445566778899aabbccddeeff1122\nblah",
     300             :       "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x22",
     301             :       17, 34 },
     302             :     /* Valid tests yielding an empty string.  */
     303             :     { "00",
     304             :       "",
     305             :       1, 2 },
     306             :     { "00 x",
     307             :       "",
     308             :       1, 2 },
     309             :     { "",
     310             :       "",
     311             :       0, 0 },
     312             :     { " ",
     313             :       "",
     314             :       0, 0 },
     315             :     /* Test trailing Nul feature.  */
     316             :     { "112233445566778899aabbccddeeff1100",
     317             :       "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00",
     318             :       17, 34 },
     319             :     { "112233445566778899aabbccddeeff1100 ",
     320             :       "\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\x11\x00",
     321             :       17, 34 },
     322             :     /* Test buffer size. (buffer is of length 20)  */
     323             :     { "6162636465666768696A6b6c6D6e6f70717273",
     324             :       "abcdefghijklmnopqrs",
     325             :       19, 38 },
     326             :     { "6162636465666768696A6b6c6D6e6f7071727300",
     327             :       "abcdefghijklmnopqrs",
     328             :       20, 40 },
     329             :     { "6162636465666768696A6b6c6D6e6f7071727374",
     330             :       NULL,
     331             :       0, 0, 1 },
     332             :     { "6162636465666768696A6b6c6D6e6f707172737400",
     333             :       NULL,
     334             :       0, 0, 1 },
     335             :     { "6162636465666768696A6b6c6D6e6f707172737475",
     336             :       NULL,
     337             :       0, 0, 1 },
     338             : 
     339             :     /* Invalid tests. */
     340             :     { "112233445566778899aabbccddeeff1122334",      NULL, 0, 0 },
     341             :     { "112233445566778899AABBCCDDEEFF1122334",      NULL, 0, 0 },
     342             :     { "112233445566778899AABBCCDDEEFG11223344",     NULL, 0, 0 },
     343             :     { "0:0112233445566778899aabbccddeeff11223344",  NULL, 0, 0 },
     344             :     { "112233445566778899aabbccddeeff11223344:",    NULL, 0, 0 },
     345             :     { "112233445566778899aabbccddeeff112233445",    NULL, 0, 0 },
     346             :     { "112233445566778899aabbccddeeff1122334455",   NULL, 0, 0, 1 },
     347             :     { "112233445566778899aabbccddeeff11223344blah", NULL, 0, 0 },
     348             :     { "0",    NULL, 0, 0 },
     349             :     { "00:",  NULL, 0, 0 },
     350             :     { "00x",  NULL, 0, 0 },
     351             : 
     352             :     { NULL, NULL, 0, 0 }
     353             :   };
     354             : 
     355             :   int idx;
     356             :   char buffer[20];
     357             :   const char *tail;
     358             :   size_t count;
     359             :   char *result;
     360             : 
     361          27 :   for (idx=0; tests[idx].hex; idx++)
     362             :     {
     363          26 :       tail = hex2str (tests[idx].hex, buffer, sizeof buffer, &count);
     364          26 :       if (tests[idx].str)
     365             :         {
     366             :           /* Good case test.  */
     367          12 :           if (!tail)
     368           0 :             fail (idx);
     369          12 :           else if (strcmp (tests[idx].str, buffer))
     370           0 :             fail (idx);
     371          12 :           else if (tail - tests[idx].hex != tests[idx].off)
     372           0 :             fail (idx);
     373          12 :           else if (tests[idx].len != count)
     374           0 :             fail (idx);
     375             :         }
     376             :       else
     377             :         {
     378             :           /* Bad case test.  */
     379          14 :           if (tail)
     380           0 :             fail (idx);
     381             :         }
     382             :     }
     383             : 
     384             :   /* Same tests again using in-place conversion.  */
     385          27 :   for (idx=0; tests[idx].hex; idx++)
     386             :     {
     387             :       char tmpbuf[100];
     388             : 
     389          26 :       assert (strlen (tests[idx].hex)+1 < sizeof tmpbuf);
     390          26 :       strcpy (tmpbuf, tests[idx].hex);
     391             : 
     392             :       /* Note: we still need to use 20 as buffer length because our
     393             :          tests assume that. */
     394          26 :       tail = hex2str (tmpbuf, tmpbuf, 20, &count);
     395          26 :       if (tests[idx].str)
     396             :         {
     397             :           /* Good case test.  */
     398          12 :           if (!tail)
     399           0 :             fail (idx);
     400          12 :           else if (strcmp (tests[idx].str, tmpbuf))
     401           0 :             fail (idx);
     402          12 :           else if (tail - tmpbuf != tests[idx].off)
     403           0 :             fail (idx);
     404          12 :           else if (tests[idx].len != count)
     405           0 :             fail (idx);
     406             :         }
     407             :       else
     408             :         {
     409             :           /* Bad case test.  */
     410          14 :           if (tail)
     411           0 :             fail (idx);
     412          14 :           if (strcmp (tmpbuf, tests[idx].hex))
     413           0 :             fail (idx); /* Buffer was modified.  */
     414             :         }
     415             :     }
     416             : 
     417             :   /* Test the allocation variant.  */
     418          27 :   for (idx=0; tests[idx].hex; idx++)
     419             :     {
     420          26 :       if (tests[idx].no_alloc_test)
     421           4 :         continue;
     422             : 
     423          22 :       result = hex2str_alloc (tests[idx].hex, &count);
     424          22 :       if (tests[idx].str)
     425             :         {
     426             :           /* Good case test.  */
     427          12 :           if (!result)
     428           0 :             fail (idx);
     429          12 :           else if (strcmp (tests[idx].str, result))
     430           0 :             fail (idx);
     431          12 :           else if (count != tests[idx].off)
     432           0 :             fail (idx);
     433             :         }
     434             :       else
     435             :         {
     436             :           /* Bad case test.  */
     437          10 :           if (result)
     438           0 :             fail (idx);
     439             :         }
     440          22 :       xfree (result);
     441             :     }
     442           1 : }
     443             : 
     444             : 
     445             : 
     446             : 
     447             : 
     448             : int
     449           1 : main (int argc, char **argv)
     450             : {
     451             :   (void)argc;
     452             :   (void)argv;
     453             : 
     454           1 :   test_hex2bin ();
     455           1 :   test_hexcolon2bin ();
     456           1 :   test_bin2hex ();
     457           1 :   test_bin2hexcolon ();
     458           1 :   test_hex2str ();
     459             : 
     460           1 :   return 0;
     461             : }

Generated by: LCOV version 1.11