Initialize Tizen 2.3
[external/libtasn1.git] / tests / Test_indefinite.c
1 /*
2  * Copyright (C) 2002, 2006, 2007, 2008, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * This file is part of LIBTASN1.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21
22
23 /*****************************************************/
24 /* File: Test_tree.c                                 */
25 /* Description: Test sequences for these functions:  */
26 /*     asn1_visit_tree,                              */
27 /*     asn1_create_element,                          */
28 /*     asn1_delete_structure,                        */
29 /*     asn1_write_value,                             */
30 /*     asn1_read_value,                              */
31 /*****************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "libtasn1.h"
37
38
39
40 int
41 main (int argc, char *argv[])
42 {
43   asn1_retCode result;
44   char buffer[10 * 1024];
45   ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
46   ASN1_TYPE asn1_element = ASN1_TYPE_EMPTY;
47   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
48   FILE *out, *fd;
49   ssize_t size;
50   const char *treefile = getenv ("ASN1PKIX");
51   const char *indeffile = getenv ("ASN1INDEF");
52
53   if (!treefile)
54     treefile = "pkix.asn";
55
56   if (!indeffile)
57     indeffile = "TestIndef.p12";
58
59   printf ("\n\n/****************************************/\n");
60   printf ("/*     Test sequence : Test_indefinite  */\n");
61   printf ("/****************************************/\n\n");
62   printf ("ASN1TREE: %s\n", treefile);
63
64   /* Check version */
65   if (asn1_check_version ("0.2.11") == NULL)
66     printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
67             asn1_check_version (NULL));
68
69   result = asn1_parser2tree (treefile, &definitions, errorDescription);
70   if (result != ASN1_SUCCESS)
71     {
72       asn1_perror (result);
73       printf ("ErrorDescription = %s\n\n", errorDescription);
74       exit (1);
75     }
76
77   out = stdout;
78
79   fd = fopen (indeffile, "rb");
80   if (fd == NULL)
81     {
82       printf ("Cannot read file %s\n", indeffile);
83       exit (1);
84     }
85   size = fread (buffer, 1, sizeof (buffer), fd);
86   if (size <= 0)
87     {
88       printf ("Cannot read from file %s\n", indeffile);
89       exit (1);
90     }
91
92   fclose (fd);
93
94   result =
95     asn1_create_element (definitions, "PKIX1.pkcs-12-PFX", &asn1_element);
96   if (result != ASN1_SUCCESS)
97     {
98       asn1_perror (result);
99       printf ("Cannot create PKCS12 element\n");
100       exit (1);
101     }
102
103   result = asn1_der_decoding (&asn1_element, buffer, size, errorDescription);
104   if (result != ASN1_SUCCESS)
105     {
106       asn1_perror (result);
107       printf ("Cannot decode BER data (size %d)\n", size);
108       exit (1);
109     }
110
111   /* Clear the definition structures */
112   asn1_delete_structure (&definitions);
113   asn1_delete_structure (&asn1_element);
114
115   if (out != stdout)
116     fclose (out);
117
118   exit (0);
119 }