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