LCOV - code coverage report
Current view: top level - common - t-sexputil.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 40 72.5 %
Date: 2016-09-12 13:01:59 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* t-sexputil.c - Module test for sexputil.c
       2             :  *      Copyright (C) 2007 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             : 
      24             : #include "util.h"
      25             : 
      26             : #define pass()  do { ; } while(0)
      27             : #define fail(a)  do { fprintf (stderr, "%s:%d: test %d failed\n",\
      28             :                                __FILE__,__LINE__, (a));          \
      29             :                      exit (1);                                   \
      30             :                    } while(0)
      31             : 
      32             : 
      33             : static void
      34           1 : test_hash_algo_from_sigval (void)
      35             : {
      36             :   int algo;
      37             :   /* A real world example.  */
      38           1 :   unsigned char example1_rsa_sha1[] =
      39             :     ("\x28\x37\x3A\x73\x69\x67\x2D\x76\x61\x6C\x28\x33\x3A\x72\x73\x61"
      40             :      "\x28\x31\x3A\x73\x31\x32\x38\x3A\x17\xD2\xE9\x5F\xB4\x24\xD4\x1E"
      41             :      "\x8C\xEE\x94\xDA\x41\x42\x1F\x26\x5E\xF4\x6D\xEC\x5B\xBD\x5B\x89"
      42             :      "\x7A\x69\x11\x43\xE9\xD2\x23\x21\x25\x64\xA6\xB0\x56\xEF\xB4\xE9"
      43             :      "\x06\xB2\x44\xF6\x80\x1E\xFF\x41\x23\xEB\xC9\xFA\xFD\x09\xBF\x9C"
      44             :      "\x8E\xCF\x7F\xC3\x7F\x3A\x40\x48\x89\xDC\xBA\xB7\xDB\x9E\xF1\xBA"
      45             :      "\x7C\x08\xEA\x74\x1D\x49\xE7\x65\xEF\x67\x79\xBC\x23\xD9\x49\xCD"
      46             :      "\x05\x99\xD3\xD8\xB7\x7B\xC7\x0E\xF2\xB3\x01\x48\x0F\xC8\xEB\x05"
      47             :      "\x7B\xFB\x61\xCC\x41\x04\x74\x6D\x33\x84\xB1\xE6\x6A\xD8\x0F\xBC"
      48             :      "\x27\xAC\x43\x45\xFA\x04\xD1\x22\x29\x29\x28\x34\x3A\x68\x61\x73"
      49             :      "\x68\x34\x3A\x73\x68\x61\x31\x29\x29");
      50             :   /* The same but without the hash algo. */
      51           1 :   unsigned char example1_rsa[] =
      52             :     ("\x28\x37\x3A\x73\x69\x67\x2D\x76\x61\x6C\x28\x33\x3A\x72\x73\x61"
      53             :      "\x28\x31\x3A\x73\x31\x32\x38\x3A\x17\xD2\xE9\x5F\xB4\x24\xD4\x1E"
      54             :      "\x8C\xEE\x94\xDA\x41\x42\x1F\x26\x5E\xF4\x6D\xEC\x5B\xBD\x5B\x89"
      55             :      "\x7A\x69\x11\x43\xE9\xD2\x23\x21\x25\x64\xA6\xB0\x56\xEF\xB4\xE9"
      56             :      "\x06\xB2\x44\xF6\x80\x1E\xFF\x41\x23\xEB\xC9\xFA\xFD\x09\xBF\x9C"
      57             :      "\x8E\xCF\x7F\xC3\x7F\x3A\x40\x48\x89\xDC\xBA\xB7\xDB\x9E\xF1\xBA"
      58             :      "\x7C\x08\xEA\x74\x1D\x49\xE7\x65\xEF\x67\x79\xBC\x23\xD9\x49\xCD"
      59             :      "\x05\x99\xD3\xD8\xB7\x7B\xC7\x0E\xF2\xB3\x01\x48\x0F\xC8\xEB\x05"
      60             :      "\x7B\xFB\x61\xCC\x41\x04\x74\x6D\x33\x84\xB1\xE6\x6A\xD8\x0F\xBC"
      61             :      "\x27\xAC\x43\x45\xFA\x04\xD1\x22\x29\x29\x29");
      62             : 
      63           1 :   algo = hash_algo_from_sigval (example1_rsa_sha1);
      64           1 :   if (algo != GCRY_MD_SHA1)
      65           0 :     fail (0);
      66           1 :   algo = hash_algo_from_sigval (example1_rsa);
      67           1 :   if (algo)
      68           0 :     fail (0);
      69           1 : }
      70             : 
      71             : 
      72             : static void
      73           1 : test_make_canon_sexp_from_rsa_pk (void)
      74             : {
      75             :   struct {
      76             :     unsigned char *m;
      77             :     size_t mlen;
      78             :     unsigned char *e;
      79             :     size_t elen;
      80             :     unsigned char *result;
      81             :     size_t resultlen;
      82             :     gpg_err_code_t reverr;  /* Expected error from the reverse function.  */
      83           1 :   } tests[] = {
      84             :     {
      85             :       "\x82\xB4\x12\x48\x08\x48\xC0\x76\xAA\x8E\xF1\xF8\x7F\x5E\x9B\x89"
      86             :       "\xA9\x62\x92\xA2\x16\x1B\xF5\x9F\xE1\x41\xF3\xF0\x42\xB5\x5C\x46"
      87             :       "\xB8\x83\x9F\x39\x97\x73\xFF\xC5\xB2\xF4\x59\x5F\xBA\xC7\x0E\x03"
      88             :       "\x9D\x27\xC0\x86\x37\x31\x46\xE0\xA1\xFE\xA1\x41\xD4\xE3\xE9\xB3"
      89             :       "\x9B\xD5\x84\x65\xA5\x37\x35\x34\x07\x58\xB6\xBA\x21\xCA\x21\x72"
      90             :       "\x4C\xF3\xFC\x91\x47\xD1\x3C\x1D\xA5\x9C\x38\x4D\x58\x39\x92\x16"
      91             :       "\xB1\xE5\x43\xFE\xB5\x46\x4B\x43\xD1\x47\xB0\xE8\x2A\xDB\xF8\x34"
      92             :       "\xB0\x5A\x22\x3D\x14\xBB\xEA\x63\x65\xA7\xF1\xF2\xF8\x97\x74\xA7",
      93             :       128,
      94             :       "\x40\x00\x00\x81",
      95             :       4,
      96             :       "\x28\x31\x30\x3a\x70\x75\x62\x6c\x69\x63\x2d\x6b\x65\x79\x28\x33"
      97             :       "\x3a\x72\x73\x61\x28\x31\x3a\x6e\x31\x32\x39\x3a\x00\x82\xb4\x12"
      98             :       "\x48\x08\x48\xc0\x76\xaa\x8e\xf1\xf8\x7f\x5e\x9b\x89\xa9\x62\x92"
      99             :       "\xa2\x16\x1b\xf5\x9f\xe1\x41\xf3\xf0\x42\xb5\x5c\x46\xb8\x83\x9f"
     100             :       "\x39\x97\x73\xff\xc5\xb2\xf4\x59\x5f\xba\xc7\x0e\x03\x9d\x27\xc0"
     101             :       "\x86\x37\x31\x46\xe0\xa1\xfe\xa1\x41\xd4\xe3\xe9\xb3\x9b\xd5\x84"
     102             :       "\x65\xa5\x37\x35\x34\x07\x58\xb6\xba\x21\xca\x21\x72\x4c\xf3\xfc"
     103             :       "\x91\x47\xd1\x3c\x1d\xa5\x9c\x38\x4d\x58\x39\x92\x16\xb1\xe5\x43"
     104             :       "\xfe\xb5\x46\x4b\x43\xd1\x47\xb0\xe8\x2a\xdb\xf8\x34\xb0\x5a\x22"
     105             :       "\x3d\x14\xbb\xea\x63\x65\xa7\xf1\xf2\xf8\x97\x74\xa7\x29\x28\x31"
     106             :       "\x3a\x65\x34\x3a\x40\x00\x00\x81\x29\x29\x29",
     107             :       171
     108             :     },
     109             :     {
     110             :       "\x63\xB4\x12\x48\x08\x48\xC0\x76\xAA\x8E\xF1\xF8\x7F\x5E\x9B\x89",
     111             :       16,
     112             :       "\x03",
     113             :       1,
     114             :       "\x28\x31\x30\x3a\x70\x75\x62\x6c\x69\x63\x2d\x6b\x65\x79\x28\x33"
     115             :       "\x3a\x72\x73\x61\x28\x31\x3a\x6e\x31\x36\x3a\x63\xb4\x12\x48\x08"
     116             :       "\x48\xc0\x76\xaa\x8e\xf1\xf8\x7f\x5e\x9b\x89\x29\x28\x31\x3a\x65"
     117             :       "\x31\x3a\x03\x29\x29\x29",
     118             :       54,
     119             :     },
     120             :     {
     121             :       "",
     122             :       0,
     123             :       "",
     124             :       0,
     125             :       "\x28\x31\x30\x3a\x70\x75\x62\x6c\x69\x63\x2d\x6b\x65\x79\x28\x33"
     126             :       "\x3a\x72\x73\x61\x28\x31\x3a\x6e\x31\x3a\x00\x29\x28\x31\x3a\x65"
     127             :       "\x31\x3a\x00\x29\x29\x29",
     128             :       38,
     129             :       GPG_ERR_BAD_PUBKEY
     130             :     },
     131             :     {
     132             :       NULL
     133             :     }
     134             :   };
     135             :   int idx;
     136             :   gpg_error_t err;
     137             :   unsigned char *sexp;
     138             :   size_t length;
     139             :   const unsigned char *rsa_n, *rsa_e;
     140             :   size_t rsa_n_len, rsa_e_len;
     141             : 
     142           4 :   for (idx=0; tests[idx].m; idx++)
     143             :     {
     144           6 :       sexp = make_canon_sexp_from_rsa_pk (tests[idx].m, tests[idx].mlen,
     145           3 :                                           tests[idx].e, tests[idx].elen,
     146             :                                           &length);
     147           3 :       if (!sexp)
     148             :         {
     149           0 :           fprintf (stderr, "%s:%d: out of core\n", __FILE__, __LINE__);
     150           0 :           exit (1);
     151             :         }
     152             : 
     153           3 :       if (length != tests[idx].resultlen)
     154           0 :         fail (idx);
     155           3 :       if (memcmp (sexp, tests[idx].result, tests[idx].resultlen))
     156           0 :         fail (idx);
     157             : 
     158             :       /* Test the reverse function.  */
     159           3 :       err = get_rsa_pk_from_canon_sexp (sexp, length,
     160             :                                         &rsa_n, &rsa_n_len,
     161             :                                         &rsa_e, &rsa_e_len);
     162           3 :       if (gpg_err_code (err) != tests[idx].reverr)
     163           0 :         fail (idx);
     164           3 :       if (!err)
     165             :         {
     166           2 :           if (tests[idx].mlen != rsa_n_len)
     167           0 :             fail (idx);
     168           2 :           if (memcmp (tests[idx].m, rsa_n, rsa_n_len))
     169           0 :             fail (idx);
     170           2 :           if (tests[idx].elen != rsa_e_len)
     171           0 :             fail (idx);
     172           2 :           if (memcmp (tests[idx].e, rsa_e, rsa_e_len))
     173           0 :             fail (idx);
     174             :         }
     175             : 
     176           3 :       xfree (sexp);
     177             :     }
     178           1 : }
     179             : 
     180             : 
     181             : int
     182           1 : main (int argc, char **argv)
     183             : {
     184             :   (void)argc;
     185             :   (void)argv;
     186             : 
     187           1 :   test_hash_algo_from_sigval ();
     188           1 :   test_make_canon_sexp_from_rsa_pk ();
     189             : 
     190           1 :   return 0;
     191             : }

Generated by: LCOV version 1.11