LCOV - code coverage report
Current view: top level - tests - Test_encoding.c (source / functions) Hit Total Coverage
Test: GNU Libtasn1 Lines: 29 60 48.3 %
Date: 2013-03-24 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 13 26 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2002-2013 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                 :            : /******************************************************/
      22                 :            : /* File: Test_encoding.c                              */
      23                 :            : /* Description: Test writing values and DER encoding. */
      24                 :            : /******************************************************/
      25                 :            : 
      26                 :            : #include <stdio.h>
      27                 :            : #include <string.h>
      28                 :            : #include <stdlib.h>
      29                 :            : #include "libtasn1.h"
      30                 :            : 
      31                 :            : 
      32                 :            : unsigned char data[256];
      33                 :            : int data_size = sizeof (data);
      34                 :            : 
      35                 :            : 
      36                 :            : int
      37                 :          1 : main (int argc, char *argv[])
      38                 :            : {
      39                 :          1 :   int result, verbose = 0;
      40                 :          1 :   asn1_node definitions = NULL;
      41                 :          1 :   asn1_node asn1_element = NULL;
      42                 :            :   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
      43                 :          1 :   const char *treefile = getenv ("ASN1ENCODING");
      44                 :            : 
      45         [ -  + ]:          1 :   if (argc > 1)
      46                 :          0 :     verbose = 1;
      47                 :            : 
      48         [ -  + ]:          1 :   if (!treefile)
      49                 :          0 :     treefile = "Test_encoding.asn";
      50                 :            : 
      51         [ -  + ]:          1 :   if (verbose != 0)
      52                 :            :     {
      53                 :          0 :       printf ("\n\n/****************************************/\n");
      54                 :          0 :       printf ("/*     Test sequence : coding-decoding  */\n");
      55                 :          0 :       printf ("/****************************************/\n\n");
      56                 :            :     }
      57                 :            : 
      58                 :            :   /* Check version */
      59         [ -  + ]:          1 :   if (asn1_check_version ("0.3.3") == NULL)
      60                 :          0 :     printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
      61                 :            :             asn1_check_version (NULL));
      62                 :            : 
      63                 :          1 :   result = asn1_parser2tree (treefile, &definitions, errorDescription);
      64                 :            : 
      65         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
      66                 :            :     {
      67                 :          0 :       asn1_perror (result);
      68                 :          0 :       printf ("ErrorDescription = %s\n\n", errorDescription);
      69                 :          0 :       exit (1);
      70                 :            :     }
      71                 :            : 
      72                 :          1 :   result = asn1_create_element (definitions, "TEST_TREE.Koko", &asn1_element);
      73         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
      74                 :            :     {
      75                 :          0 :       fprintf (stderr, "asn1_create_element(): ");
      76                 :          0 :       asn1_perror (result);
      77                 :          0 :       exit (1);
      78                 :            :     }
      79                 :            : 
      80                 :          1 :   result = asn1_write_value (asn1_element, "seqint", "NEW", 1);
      81         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
      82                 :            :     {
      83                 :          0 :       fprintf (stderr, "asn1_write_value(): seqint ");
      84                 :          0 :       asn1_perror (result);
      85                 :          0 :       exit (1);
      86                 :            :     }
      87                 :            : 
      88                 :          1 :   result = asn1_write_value (asn1_element, "seqint.?LAST", "1234", 0);
      89         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
      90                 :            :     {
      91                 :          0 :       fprintf (stderr, "asn1_write_value(): seqint.?LAST ");
      92                 :          0 :       asn1_perror (result);
      93                 :          0 :       exit (1);
      94                 :            :     }
      95                 :            : 
      96                 :          1 :   result = asn1_write_value (asn1_element, "int", "\x0f\xff\x01", 3);
      97         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
      98                 :            :     {
      99                 :          0 :       fprintf (stderr, "asn1_write_value(): int ");
     100                 :          0 :       asn1_perror (result);
     101                 :          0 :       exit (1);
     102                 :            :     }
     103                 :            : 
     104                 :          1 :   result = asn1_write_value (asn1_element, "str", "string", 6);
     105         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
     106                 :            :     {
     107                 :          0 :       fprintf (stderr, "asn1_write_value(): str ");
     108                 :          0 :       asn1_perror (result);
     109                 :          0 :       exit (1);
     110                 :            :     }
     111                 :            : 
     112                 :            :   /* Clear the definition structures */
     113                 :          1 :   asn1_delete_structure (&definitions);
     114                 :            : 
     115                 :          1 :   result = asn1_der_coding (asn1_element, "", data, &data_size, NULL);
     116         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
     117                 :            :     {
     118                 :          0 :       fprintf (stderr, "Encoding error.\n");
     119                 :          0 :       asn1_perror (result);
     120                 :          0 :       exit (1);
     121                 :            :     }
     122                 :            : 
     123                 :          1 :   result = asn1_der_decoding (&asn1_element, data, data_size, NULL);
     124         [ -  + ]:          1 :   if (result != ASN1_SUCCESS)
     125                 :            :     {
     126                 :          0 :       fprintf (stderr, "Decoding error.\n");
     127                 :          0 :       asn1_perror (result);
     128                 :          0 :       exit (1);
     129                 :            :     }
     130                 :            : 
     131                 :          1 :   asn1_delete_structure (&asn1_element);
     132                 :            : 
     133         [ -  + ]:          1 :   if (verbose)
     134                 :          0 :     printf ("Success\n");
     135                 :          1 :   exit (0);
     136                 :            : }

Generated by: LCOV version 1.9