asn1_read_value() and friends understand the ?CURRENT keyword.
[platform/upstream/libtasn1.git] / tests / Test_indefinite.c
1 /*
2  * Copyright (C) 2002-2014 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 /*****************************************************/
23 /* File: Test_tree.c                                 */
24 /* Description: Test sequences for these functions:  */
25 /*     asn1_visit_tree,                              */
26 /*     asn1_create_element,                          */
27 /*     asn1_delete_structure,                        */
28 /*     asn1_write_value,                             */
29 /*     asn1_read_value,                              */
30 /*****************************************************/
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "libtasn1.h"
36
37
38
39 int
40 main (int argc, char *argv[])
41 {
42   int result;
43   char buffer[10 * 1024];
44   asn1_node definitions = NULL;
45   asn1_node asn1_element = NULL;
46   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
47   FILE *out, *fd;
48   ssize_t size;
49   const char *treefile = getenv ("ASN1PKIX");
50   const char *indeffile = getenv ("ASN1INDEF");
51   int verbose = 0;
52
53   if (argc > 1)
54     verbose = 1;
55
56   if (!treefile)
57     treefile = "pkix.asn";
58
59   if (!indeffile)
60     indeffile = "TestIndef.p12";
61
62   if (verbose)
63     {
64       printf ("\n\n/****************************************/\n");
65       printf ("/*     Test sequence : Test_indefinite  */\n");
66       printf ("/****************************************/\n\n");
67       printf ("ASN1TREE: %s\n", treefile);
68     }
69
70   /* Check version */
71   if (asn1_check_version ("0.2.11") == NULL)
72     printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
73             asn1_check_version (NULL));
74
75   result = asn1_parser2tree (treefile, &definitions, errorDescription);
76   if (result != ASN1_SUCCESS)
77     {
78       asn1_perror (result);
79       printf ("ErrorDescription = %s\n\n", errorDescription);
80       exit (1);
81     }
82
83   out = stdout;
84
85   fd = fopen (indeffile, "rb");
86   if (fd == NULL)
87     {
88       printf ("Cannot read file %s\n", indeffile);
89       exit (1);
90     }
91   size = fread (buffer, 1, sizeof (buffer), fd);
92   if (size <= 0)
93     {
94       printf ("Cannot read from file %s\n", indeffile);
95       exit (1);
96     }
97
98   fclose (fd);
99
100   result =
101     asn1_create_element (definitions, "PKIX1.pkcs-12-PFX", &asn1_element);
102   if (result != ASN1_SUCCESS)
103     {
104       asn1_perror (result);
105       printf ("Cannot create PKCS12 element\n");
106       exit (1);
107     }
108
109   result = asn1_der_decoding (&asn1_element, buffer, size, errorDescription);
110   if (result != ASN1_SUCCESS)
111     {
112       asn1_perror (result);
113       printf ("Cannot decode BER data (size %ld)\n", (long) size);
114       exit (1);
115     }
116
117   /* Clear the definition structures */
118   asn1_delete_structure (&definitions);
119   asn1_delete_structure (&asn1_element);
120
121   if (out != stdout)
122     fclose (out);
123
124   exit (0);
125 }