LCOV - code coverage report
Current view: top level - libtasn1-2.x/tests - Test_overflow.c (source / functions) Hit Total Coverage
Test: GNU Libtasn1 Lines: 30 39 76.9 %
Date: 2012-09-24 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 5 10 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2012 Free Software Foundation, Inc.
       3                 :            :  *
       4                 :            :  * This file is part of LIBTASN1.
       5                 :            :  *
       6                 :            :  * This program 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                 :            :  * This program 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                 :            : /* Written by Simon Josefsson */
      22                 :            : 
      23                 :            : #include <stdio.h>
      24                 :            : #include <string.h>
      25                 :            : #include <stdlib.h>
      26                 :            : #include <limits.h>
      27                 :            : 
      28                 :            : #include "libtasn1.h"
      29                 :            : 
      30                 :            : int
      31                 :          1 : main (void)
      32                 :            : {
      33                 :            :   /* Test that values larger than long are rejected.  This has worked
      34                 :            :      fine with all versions of libtasn1. */
      35                 :            :   {
      36                 :          1 :     unsigned char der[] = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
      37                 :            :     long l;
      38                 :            :     int len;
      39                 :            : 
      40                 :          1 :     l = asn1_get_length_der (der, sizeof der, &len);
      41                 :            : 
      42         [ +  - ]:          1 :     if (l == -2L)
      43                 :          1 :       puts ("OK: asn1_get_length_der bignum");
      44                 :            :     else
      45                 :            :       {
      46                 :          0 :         printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len);
      47                 :          0 :         return 1;
      48                 :            :       }
      49                 :            :   }
      50                 :            : 
      51                 :            :   /* Test that values larger than int but smaller than long are
      52                 :            :      rejected.  This limitation was introduced with libtasn1 2.12. */
      53                 :            :   if (LONG_MAX > INT_MAX)
      54                 :            :     {
      55                 :          1 :       unsigned long num = ((long) UINT_MAX) << 2;
      56                 :            :       unsigned char der[20];
      57                 :            :       int der_len;
      58                 :            :       long l;
      59                 :            :       int len;
      60                 :            : 
      61                 :          1 :       asn1_length_der (num, der, &der_len);
      62                 :            : 
      63                 :          1 :       l = asn1_get_length_der (der, der_len, &len);
      64                 :            : 
      65         [ +  - ]:          1 :       if (l == -2L)
      66                 :          1 :         puts ("OK: asn1_get_length_der intnum");
      67                 :            :       else
      68                 :            :         {
      69                 :          0 :           printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l,
      70                 :            :                   len);
      71                 :          0 :           return 1;
      72                 :            :         }
      73                 :            :     }
      74                 :            : 
      75                 :            :   /* Test that values larger than would fit in the input string are
      76                 :            :      rejected.  This problem was fixed in libtasn1 2.12. */
      77                 :            :     {
      78                 :          1 :       unsigned long num = 64;
      79                 :            :       unsigned char der[20];
      80                 :            :       int der_len;
      81                 :            :       long l;
      82                 :            :       int len;
      83                 :            : 
      84                 :          1 :       asn1_length_der (num, der, &der_len);
      85                 :            : 
      86                 :          1 :       der_len = sizeof(der);
      87                 :          1 :       l = asn1_get_length_der (der, der_len, &len);
      88                 :            : 
      89         [ +  - ]:          1 :       if (l == -4L)
      90                 :          1 :         puts ("OK: asn1_get_length_der overflow-small");
      91                 :            :       else
      92                 :            :         {
      93                 :          0 :           printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n", l,
      94                 :            :                   len);
      95                 :          0 :           return 1;
      96                 :            :         }
      97                 :            :     }
      98                 :            : 
      99                 :            :   /* Test that values larger than would fit in the input string are
     100                 :            :      rejected.  This problem was fixed in libtasn1 2.12. */
     101                 :            :     {
     102                 :          1 :       unsigned long num = 1073741824;
     103                 :            :       unsigned char der[20];
     104                 :            :       int der_len;
     105                 :            :       long l;
     106                 :            :       int len;
     107                 :            : 
     108                 :          1 :       asn1_length_der (num, der, &der_len);
     109                 :            : 
     110                 :          1 :       der_len = sizeof(der);
     111                 :          1 :       l = asn1_get_length_der (der, der_len, &len);
     112                 :            : 
     113         [ +  - ]:          1 :       if (l == -4L)
     114                 :          1 :         puts ("OK: asn1_get_length_der overflow-large1");
     115                 :            :       else
     116                 :            :         {
     117                 :          0 :           printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n", l,
     118                 :            :                   len);
     119                 :          0 :           return 1;
     120                 :            :         }
     121                 :            :     }
     122                 :            : 
     123                 :            :   /* Test that values larger than would fit in the input string are
     124                 :            :      rejected.  This problem was fixed in libtasn1 2.12. */
     125                 :            :     {
     126                 :          1 :       unsigned long num = 2147483647;
     127                 :            :       unsigned char der[20];
     128                 :            :       int der_len;
     129                 :            :       long l;
     130                 :            :       int len;
     131                 :            : 
     132                 :          1 :       asn1_length_der (num, der, &der_len);
     133                 :            : 
     134                 :          1 :       der_len = sizeof(der);
     135                 :          1 :       l = asn1_get_length_der (der, der_len, &len);
     136                 :            : 
     137         [ +  - ]:          1 :       if (l == -2L)
     138                 :          1 :         puts ("OK: asn1_get_length_der overflow-large2");
     139                 :            :       else
     140                 :            :         {
     141                 :          0 :           printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n", l,
     142                 :            :                   len);
     143                 :          1 :           return 1;
     144                 :            :         }
     145                 :            :     }
     146                 :            : 
     147                 :          1 :   return 0;
     148                 :            : }

Generated by: LCOV version 1.9