LCOV - code coverage report
Current view: top level - shishi/tests - safe.c (source / functions) Hit Total Coverage
Test: GNU Shishi Lines: 61 85 71.8 %
Date: 2010-05-20 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 28 58 48.3 %

           Branch data     Line data    Source code
       1                 :            : /* safe.c --- Shishi SAFE self tests.
       2                 :            :  * Copyright (C) 2002, 2003, 2007, 2008  Simon Josefsson
       3                 :            :  *
       4                 :            :  * This file is part of Shishi.
       5                 :            :  *
       6                 :            :  * Shishi 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                 :            :  * Shishi 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 Shishi; if not, see http://www.gnu.org/licenses or write
      18                 :            :  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
      19                 :            :  * Floor, Boston, MA 02110-1301, USA
      20                 :            :  *
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "utils.c"
      24                 :            : 
      25                 :            : void
      26                 :          1 : test (Shishi * handle)
      27                 :            : {
      28                 :            :   Shishi_safe *safe;
      29                 :            :   Shishi_key *key;
      30                 :            :   Shishi_asn1 asn1safe;
      31                 :            :   char *p, *q;
      32                 :            :   size_t l, m;
      33                 :            :   int32_t t;
      34                 :            :   int res;
      35                 :            : 
      36                 :            :   /* shishi_safe() */
      37                 :          1 :   res = shishi_safe (handle, &safe);
      38         [ -  + ]:          1 :   if (debug)
      39                 :          0 :     printf ("shishi_safe () => `%p'.\n", safe);
      40         [ +  - ]:          1 :   if (res == SHISHI_OK)
      41                 :          1 :     success ("shishi_safe() OK\n");
      42                 :            :   else
      43                 :          0 :     fail ("shishi_safe() failed\n");
      44                 :            : 
      45                 :            :   /* shishi_safe_key */
      46                 :          1 :   key = shishi_safe_key (safe);
      47         [ +  - ]:          1 :   if (key)
      48                 :          1 :     success ("shishi_safe_key() OK\n");
      49                 :            :   else
      50                 :          0 :     fail ("shishi_safe_key() failed\n");
      51                 :            : 
      52                 :            :   /* shishi_safe_safe */
      53                 :          1 :   asn1safe = shishi_safe_safe (safe);
      54         [ +  - ]:          1 :   if (asn1safe)
      55                 :          1 :     success ("shishi_safe_safe() OK\n");
      56                 :            :   else
      57                 :          0 :     fail ("shishi_safe_safe() failed\n");
      58                 :            : 
      59                 :            :   /* shishi_safe_set_user_data */
      60                 :          1 :   res = shishi_safe_set_user_data (handle, asn1safe, "foo", 3);
      61         [ +  - ]:          1 :   if (res == SHISHI_OK)
      62                 :          1 :     success ("shishi_safe_set_user_data() OK\n");
      63                 :            :   else
      64                 :          0 :     fail ("shishi_safe_set_user_data() failed (%d)\n", res);
      65                 :            : 
      66                 :            :   /* shishi_safe_user_data */
      67                 :          1 :   res = shishi_safe_user_data (handle, asn1safe, &p, &l);
      68         [ -  + ]:          1 :   if (debug)
      69                 :          0 :     escapeprint (p, l);
      70 [ +  - ][ +  - ]:          2 :   if (res == SHISHI_OK && l == 3 && memcmp (p, "foo", 3) == 0)
                 [ +  - ]
      71                 :          1 :     success ("shishi_safe_user_data() OK\n");
      72                 :            :   else
      73                 :          0 :     fail ("shishi_safe_user_data() failed (%d)\n", res);
      74                 :          1 :   free (p);
      75                 :            : 
      76                 :            :   /* shishi_safe_set_cksum */
      77                 :          1 :   res = shishi_safe_set_cksum (handle, asn1safe, 42, "bar", 3);
      78         [ +  - ]:          1 :   if (res == SHISHI_OK)
      79                 :          1 :     success ("shishi_safe_set_cksum() OK\n");
      80                 :            :   else
      81                 :          0 :     fail ("shishi_safe_set_cksum() failed (%d)\n", res);
      82                 :            : 
      83                 :            :   /* shishi_safe_cksum */
      84                 :          1 :   res = shishi_safe_cksum (handle, asn1safe, &t, &q, &m);
      85         [ -  + ]:          1 :   if (debug)
      86                 :            :     {
      87                 :          0 :       printf ("type=%d\n", t);
      88                 :          0 :       escapeprint (q, m);
      89                 :            :     }
      90 [ +  - ][ +  - ]:          2 :   if (res == SHISHI_OK && t == 42 && m == 3 && memcmp (q, "bar", 3) == 0)
         [ +  - ][ +  - ]
      91                 :          1 :     success ("shishi_safe_cksum() OK\n");
      92                 :            :   else
      93                 :          0 :     fail ("shishi_safe_cksum() failed (%d)\n", res);
      94                 :          1 :   free (q);
      95                 :            : 
      96                 :            :   /* shishi_safe_verify */
      97                 :          1 :   res = shishi_safe_verify (safe, key);
      98         [ +  - ]:          1 :   if (res == SHISHI_CRYPTO_ERROR)       /* t==42 unsupported cksumtype */
      99                 :          1 :     success ("shishi_safe_verify() OK\n");
     100                 :            :   else
     101                 :          0 :     fail ("shishi_safe_verify() failed (%d)\n", res);
     102                 :            : 
     103                 :            : #if WITH_DES
     104                 :            :   /* shishi_safe_set_cksum */
     105                 :          1 :   res = shishi_safe_set_cksum (handle, asn1safe, SHISHI_RSA_MD5_DES,
     106                 :            :                                "bar", 3);
     107         [ +  - ]:          1 :   if (res == SHISHI_OK)
     108                 :          1 :     success ("shishi_safe_set_cksum() OK\n");
     109                 :            :   else
     110                 :          0 :     fail ("shishi_safe_set_cksum() failed (%d)\n", res);
     111                 :            : 
     112                 :            :   /* shishi_safe_verify */
     113                 :          1 :   res = shishi_safe_verify (safe, key);
     114         [ +  - ]:          1 :   if (res == SHISHI_VERIFY_FAILED)
     115                 :          1 :     success ("shishi_safe_verify() OK\n");
     116                 :            :   else
     117                 :          0 :     fail ("shishi_safe_verify() failed (%d)\n", res);
     118                 :            : #endif
     119                 :            : 
     120                 :            :   /* shishi_safe_safe_der() */
     121                 :          1 :   res = shishi_safe_safe_der (safe, &p, &l);
     122         [ +  - ]:          1 :   if (res == SHISHI_OK)
     123                 :          1 :     success ("shishi_safe_safe_der() OK\n");
     124                 :            :   else
     125                 :          0 :     fail ("shishi_safe_safe_der() failed\n");
     126                 :            : 
     127                 :            :   /* shishi_safe_to_file() */
     128                 :          1 :   res = shishi_safe_to_file (handle, asn1safe, SHISHI_FILETYPE_TEXT,
     129                 :            :                              "safe.tmp");
     130         [ +  - ]:          1 :   if (res == SHISHI_OK)
     131                 :          1 :     success ("shishi_safe_to_file() OK\n");
     132                 :            :   else
     133                 :          0 :     fail ("shishi_safe_to_file() failed\n");
     134                 :            : 
     135                 :            :   /* shishi_safe_done() */
     136                 :          1 :   shishi_safe_done (safe);
     137                 :          1 :   success ("shishi_safe_done() OK\n");
     138                 :            : 
     139                 :            :   /* shishi_authenticator_from_file() */
     140                 :          1 :   asn1safe = NULL;
     141                 :          1 :   res = shishi_safe_from_file (handle, &asn1safe, SHISHI_FILETYPE_TEXT,
     142                 :            :                                "safe.tmp");
     143         [ +  - ]:          1 :   if (res == SHISHI_OK)
     144                 :          1 :     success ("shishi_safe_from_file() OK\n");
     145                 :            :   else
     146                 :          0 :     fail ("shishi_safe_from_file() failed\n");
     147                 :            : 
     148         [ -  + ]:          1 :   if (debug)
     149                 :            :     {
     150                 :            :       /* shishi_safe_print() */
     151                 :          0 :       res = shishi_safe_print (handle, stdout, asn1safe);
     152         [ #  # ]:          0 :       if (res == SHISHI_OK)
     153                 :          0 :         success ("shishi_safe_print() OK\n");
     154                 :            :       else
     155                 :          0 :         fail ("shishi_safe_print() failed\n");
     156                 :            :     }
     157                 :            : 
     158                 :            :   /* shishi_asn1_to_der() */
     159                 :          1 :   res = shishi_asn1_to_der (handle, asn1safe, &q, &m);
     160         [ +  - ]:          1 :   if (res == SHISHI_OK)
     161                 :          1 :     success ("shishi_asn1_to_der() OK\n");
     162                 :            :   else
     163                 :          0 :     fail ("shishi_asn1_to_der() failed\n");
     164                 :            : 
     165                 :            :   /* Compare DER encodings of authenticators */
     166 [ +  - ][ +  - ]:          2 :   if (l > 0 && m > 0 && l == m && memcmp (p, q, l) == 0)
         [ +  - ][ +  - ]
     167                 :          1 :     success ("DER comparison OK\n");
     168                 :            :   else
     169                 :          0 :     fail ("DER comparison failed\n");
     170                 :            : 
     171                 :          1 :   shishi_asn1_done (handle, asn1safe);
     172                 :          1 :   free (q);
     173                 :          1 :   free (p);
     174                 :            : 
     175                 :            :   /* unlink() */
     176                 :          1 :   res = unlink ("safe.tmp");
     177         [ +  - ]:          1 :   if (res == 0)
     178                 :          1 :     success ("unlink() OK\n");
     179                 :            :   else
     180                 :          0 :     fail ("unlink() failed\n");
     181                 :          1 : }

Generated by: LCOV version 1.8