LCOV - code coverage report
Current view: top level - tests - t-dnparser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 56 64.3 %
Date: 2016-09-12 12:51:24 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* t-dnparser.c - basic test for the DN parser
       2             :  *      Copyright (C) 2002, 2006 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             : #include <stdio.h>
      21             : #include <stdlib.h>
      22             : #include <string.h>
      23             : #include <assert.h>
      24             : #include <time.h>
      25             : #include <errno.h>
      26             : 
      27             : #include "../src/ksba.h"
      28             : #include "t-common.h"
      29             : 
      30             : 
      31             : static void
      32           1 : test_0 (void)
      33             : {
      34             :   static char *good_strings[] = {
      35             :     "C=de,O=g10 Code,OU=qa,CN=Pépé le Moko",
      36             :     "C= de,   O=g10 Code  ,  OU=qa ,CN=Pépé le Moko",
      37             :     "CN=www.gnupg.org",
      38             :     "   CN=www.gnupg.org  ",
      39             :     "C=fr,L=Paris,CN=Julien Duvivier,EMAIL=julien@example.org",
      40             :     NULL
      41             :   };
      42             :   gpg_error_t err;
      43             :   int i;
      44             :   unsigned char *buf;
      45             :   size_t off, len;
      46             : 
      47           6 :   for (i=0; good_strings[i]; i++)
      48             :     {
      49           5 :       err = ksba_dn_str2der (good_strings[i], &buf, &len);
      50           5 :       if (err)
      51             :         {
      52           0 :           fprintf (stderr, "%s:%d: ksba_dn_str2der failed for `%s': %s\n",
      53             :                    __FILE__,__LINE__, good_strings[i], gpg_strerror (err));
      54           0 :           exit (1);
      55             :         }
      56           5 :       err = ksba_dn_teststr (good_strings[i], 0, &off, &len);
      57           5 :       if (err)
      58             :         {
      59           0 :           fprintf (stderr, "%s:%d: ksba_dn_teststr failed for `%s': %s\n",
      60             :                    __FILE__,__LINE__, good_strings[i], gpg_strerror (err));
      61           0 :           exit (1);
      62             :         }
      63           5 :       xfree (buf);
      64             :     }
      65           1 : }
      66             : 
      67             : 
      68             : static void
      69           1 : test_1 (void)
      70             : {
      71             :   static char *empty_elements[] = {
      72             :     "C=de,O=foo,OU=,CN=joe",
      73             :     "C=de,O=foo,OU= ,CN=joe",
      74             :     "C=de,O=foo,OU=\"\" ,CN=joe",
      75             :     "C=de,O=foo,OU=",
      76             :     "C=de,O=foo,OU= ",
      77             :     "C=,O=foo,OU=bar ",
      78             :     "C = ,O=foo,OU=bar ",
      79             :     "C=",
      80             :     NULL
      81             :   };
      82             :   gpg_error_t err;
      83             :   int i;
      84             :   unsigned char *buf;
      85             :   size_t off, len;
      86             : 
      87           9 :   for (i=0; empty_elements[i]; i++)
      88             :     {
      89           8 :       err = ksba_dn_str2der (empty_elements[i], &buf, &len);
      90           8 :       if (gpg_err_code (err) != GPG_ERR_SYNTAX)
      91           0 :         fail ("empty element not detected");
      92           8 :       err = ksba_dn_teststr (empty_elements[i], 0, &off, &len);
      93           8 :       if (!err)
      94           0 :         fail ("ksba_dn_teststr returned no error");
      95           8 :       printf ("string ->%s<-  error at %lu.%lu (%.*s)\n",
      96             :               empty_elements[i], (unsigned long)off, (unsigned long)len,
      97           8 :               (int)len, empty_elements[i]+off);
      98           8 :       xfree (buf);
      99             :     }
     100           1 : }
     101             : 
     102             : static void
     103           1 : test_2 (void)
     104             : {
     105             :   static char *invalid_labels[] = {
     106             :     "C=de,FOO=something,O=bar",
     107             :     "Y=foo, C=baz",
     108             :     NULL
     109             :   };
     110             :   gpg_error_t err;
     111             :   int i;
     112             :   unsigned char *buf;
     113             :   size_t off, len;
     114             : 
     115           3 :   for (i=0; invalid_labels[i]; i++)
     116             :     {
     117           2 :       err = ksba_dn_str2der (invalid_labels[i], &buf, &len);
     118           2 :       if (gpg_err_code (err) != GPG_ERR_UNKNOWN_NAME)
     119           0 :         fail ("invalid label not detected");
     120           2 :       err = ksba_dn_teststr (invalid_labels[i], 0, &off, &len);
     121           2 :       if (!err)
     122           0 :         fail ("ksba_dn_test_str returned no error");
     123           2 :       printf ("string ->%s<-  error at %lu.%lu (%.*s)\n",
     124             :               invalid_labels[i], (unsigned long)off, (unsigned long)len,
     125           2 :               (int)len, invalid_labels[i]+off);
     126           2 :       xfree (buf);
     127             :     }
     128           1 : }
     129             : 
     130             : 
     131             : 
     132             : int
     133           1 : main (int argc, char **argv)
     134             : {
     135             :   char inputbuf[4096];
     136             :   unsigned char *buf;
     137             :   size_t len;
     138             :   gpg_error_t err;
     139             : 
     140           1 :   if (argc == 2 && !strcmp (argv[1], "--to-str") )
     141             :     { /* Read the DER encoded DN from stdin write the string to stdout */
     142           0 :       fread (inputbuf, 1, sizeof inputbuf, stdin);
     143           0 :       if (!feof (stdin))
     144           0 :         fail ("read error or input too large");
     145             : 
     146           0 :       fail ("not yet implemented");
     147             : 
     148             :     }
     149           1 :   else if (argc == 2 && !strcmp (argv[1], "--to-der") )
     150             :     { /* Read the String from stdin write the DER encoding to stdout */
     151           0 :       fread (inputbuf, 1, sizeof inputbuf, stdin);
     152           0 :       if (!feof (stdin))
     153           0 :         fail ("read error or input too large");
     154             : 
     155           0 :       err = ksba_dn_str2der (inputbuf, &buf, &len);
     156           0 :       fail_if_err (err);
     157           0 :       fwrite (buf, len, 1, stdout);
     158             :     }
     159           1 :   else if (argc == 1)
     160             :     {
     161           1 :       test_0 ();
     162           1 :       test_1 ();
     163           1 :       test_2 ();
     164             :     }
     165             :   else
     166             :     {
     167           0 :       fprintf (stderr, "usage: t-dnparser [--to-str|--to-der]\n");
     168           0 :       return 1;
     169             :     }
     170             : 
     171           1 :   return 0;
     172             : }

Generated by: LCOV version 1.11