LCOV - code coverage report
Current view: top level - tests - t-oid.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 59 25.4 %
Date: 2016-09-12 12:20:35 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* t-oid.c - Test utility for the OID functions
       2             :  *      Copyright (C) 2009 g10 Code GmbH
       3             :  *
       4             :  * This file is part of KSBA.
       5             :  *
       6             :  * KSBA 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             :  * KSBA 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             : 
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : #include <string.h>
      24             : #include <assert.h>
      25             : #include <time.h>
      26             : #include <errno.h>
      27             : 
      28             : #include "../src/ksba.h"
      29             : 
      30             : #define PGM "t-oid"
      31             : #define BADOID "1.3.6.1.4.1.11591.2.12242973"
      32             : 
      33             : 
      34             : static void *
      35           0 : read_into_buffer (FILE *fp, size_t *r_length)
      36             : {
      37             :   char *buffer;
      38             :   size_t buflen;
      39           0 :   size_t nread, bufsize = 0;
      40             : 
      41           0 :   *r_length = 0;
      42             : #define NCHUNK 8192
      43             : #ifdef HAVE_W32_SYSTEM
      44             :   setmode (fileno(fp), O_BINARY);
      45             : #endif
      46           0 :   buffer = NULL;
      47           0 :   buflen = 0;
      48             :   do
      49             :     {
      50           0 :       bufsize += NCHUNK;
      51           0 :       buffer = realloc (buffer, bufsize);
      52           0 :       if (!buffer)
      53             :         {
      54           0 :           perror ("realloc failed");
      55           0 :           exit (1);
      56             :         }
      57             : 
      58           0 :       nread = fread (buffer + buflen, 1, NCHUNK, fp);
      59           0 :       if (nread < NCHUNK && ferror (fp))
      60             :         {
      61           0 :           perror ("fread failed");
      62           0 :           exit (1);
      63             :         }
      64           0 :       buflen += nread;
      65             :     }
      66           0 :   while (nread == NCHUNK);
      67             : #undef NCHUNK
      68             : 
      69           0 :   *r_length = buflen;
      70           0 :   return buffer;
      71             : }
      72             : 
      73             : 
      74             : static void
      75           1 : test_oid_to_str (void)
      76             : {
      77             :   struct {
      78             :     unsigned int binlen;
      79             :     unsigned char *bin;
      80             :     char *str;
      81           1 :   } tests[] = {
      82             : 
      83             :     {  7, "\x02\x82\x06\x01\x0A\x0C\x00",
      84             :        "0.2.262.1.10.12.0"
      85             :     },
      86             :     {  7, "\x02\x82\x06\x01\x0A\x0C\x01",
      87             :        "0.2.262.1.10.12.1"
      88             :     },
      89             :     {  7, "\x2A\x86\x48\xCE\x38\x04\x01",
      90             :        "1.2.840.10040.4.1"
      91             :     },
      92             :     {  7, "\x2A\x86\x48\xCE\x38\x04\x03",
      93             :        "1.2.840.10040.4.3"
      94             :     },
      95             :     { 10, "\x2B\x06\x01\x04\x01\xDA\x47\x02\x01\x01",
      96             :       "1.3.6.1.4.1.11591.2.1.1"
      97             :     },
      98             :     {  3, "\x55\x1D\x0E",
      99             :        "2.5.29.14"
     100             :     },
     101             :     {  9, "\x80\x02\x70\x50\x25\x46\xfd\x0c\xc0",
     102             :        BADOID
     103             :     },
     104             :     {  1, "\x80",
     105             :        BADOID
     106             :     },
     107             :     {  2, "\x81\x00",
     108             :        "2.48"
     109             :     },
     110             :     {  2, "\x81\x01",
     111             :        "2.49"
     112             :     },
     113             :     {  2, "\x81\x7f",
     114             :        "2.175"
     115             :     },
     116             :     {  2, "\x81\x80",  /* legal encoding? */
     117             :        "2.48"
     118             :     },
     119             :     {  2, "\x81\x81\x01",  /* legal encoding? */
     120             :        "2.49"
     121             :     },
     122             :     {  0, "",
     123             :        ""
     124             :     },
     125             : 
     126             :     { 0, NULL, NULL }
     127             :   };
     128             :   int tidx;
     129             :   char *str;
     130             : 
     131          15 :   for (tidx=0; tests[tidx].bin; tidx++)
     132             :     {
     133          14 :       str = ksba_oid_to_str (tests[tidx].bin, tests[tidx].binlen);
     134          14 :       if (!str)
     135             :         {
     136           0 :           perror ("ksba_oid_to_str failed");
     137           0 :           exit (1);
     138             :         }
     139          14 :       if (strcmp (tests[tidx].str, str))
     140             :         {
     141           0 :           fprintf (stderr, "ksba_oid_to_str test %d failed\n", tidx);
     142           0 :           fprintf (stderr, "  got=%s\n", str);
     143           0 :           fprintf (stderr, " want=%s\n", tests[tidx].str);
     144           0 :           exit (1);
     145             :         }
     146          14 :       ksba_free (str);
     147             :     }
     148           1 : }
     149             : 
     150             : 
     151             : int
     152           1 : main (int argc, char **argv)
     153             : {
     154             :   gpg_error_t err;
     155             : 
     156           1 :   if (argc)
     157             :     {
     158           1 :       argc--;
     159           1 :       argv++;
     160             :     }
     161             : 
     162             : 
     163           1 :   if (!argc)
     164             :     {
     165           1 :       test_oid_to_str ();
     166             :     }
     167           0 :   else if (!strcmp (*argv, "--from-str"))
     168             :     {
     169             :       unsigned char *buffer;
     170             :       size_t n, buflen;
     171             : 
     172           0 :       for (argv++,argc-- ; argc; argc--, argv++)
     173             :         {
     174           0 :           err = ksba_oid_from_str (*argv, &buffer, &buflen);
     175           0 :           if (err)
     176             :             {
     177           0 :               fprintf (stderr, "can't convert `%s': %s\n",
     178             :                        *argv, gpg_strerror (err));
     179           0 :               return 1;
     180             :             }
     181           0 :           printf ("%s ->", *argv);
     182           0 :           for (n=0; n < buflen; n++)
     183           0 :             printf (" %02X", buffer[n]);
     184           0 :           putchar ('\n');
     185           0 :           free (buffer);
     186             :         }
     187             :     }
     188           0 :   else if (!strcmp (*argv, "--to-str"))
     189             :     {
     190             :       char *buffer;
     191             :       size_t buflen;
     192             :       char *result;
     193             : 
     194           0 :       argv++;argc--;
     195             : 
     196           0 :       buffer = read_into_buffer (stdin, &buflen);
     197           0 :       result = ksba_oid_to_str (buffer, buflen);
     198           0 :       free (buffer);
     199           0 :       printf ("%s\n", result? result:"[malloc failed]");
     200           0 :       free (result);
     201             :     }
     202             :   else
     203             :     {
     204           0 :       fputs ("usage: "PGM" [--from-str|--to-str]\n", stderr);
     205           0 :       return 1;
     206             :     }
     207             : 
     208           1 :   return 0;
     209             : }

Generated by: LCOV version 1.11