Bump version.
[libtasn1.git] / tests / Test_encoding.c
blobbc619004ff815a62c5454233cc2c9e5397c9257e
1 /*
2 * Copyright (C) 2006, 2007 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
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.
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.
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/>.
22 /******************************************************/
23 /* File: Test_encoding.c */
24 /* Description: Test writing values and DER encoding. */
25 /******************************************************/
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include "libtasn1.h"
33 unsigned char data[256];
34 int data_size = sizeof (data);
37 int
38 main (int argc, char *argv[])
40 asn1_retCode result;
41 ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
42 ASN1_TYPE asn1_element = ASN1_TYPE_EMPTY;
43 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
44 const char *treefile = getenv ("ASN1ENCODING");
46 if (!treefile)
47 treefile = "Test_encoding.asn";
49 printf ("\n\n/****************************************/\n");
50 printf ("/* Test sequence : coding-decoding */\n");
51 printf ("/****************************************/\n\n");
53 /* Check version */
54 if (asn1_check_version ("0.3.3") == NULL)
55 printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
56 asn1_check_version (NULL));
58 result = asn1_parser2tree (treefile, &definitions, errorDescription);
60 if (result != ASN1_SUCCESS)
62 libtasn1_perror (result);
63 printf ("ErrorDescription = %s\n\n", errorDescription);
64 exit (1);
67 result = asn1_create_element (definitions, "TEST_TREE.Koko", &asn1_element);
68 if (result != ASN1_SUCCESS)
70 fprintf (stderr, "asn1_create_element(): ");
71 libtasn1_perror (result);
72 exit (1);
75 result = asn1_write_value (asn1_element, "seqint", "NEW", 1);
76 if (result != ASN1_SUCCESS)
78 fprintf (stderr, "asn1_write_value(): seqint ");
79 libtasn1_perror (result);
80 exit (1);
83 result = asn1_write_value (asn1_element, "seqint.?LAST", "1234", 0);
84 if (result != ASN1_SUCCESS)
86 fprintf (stderr, "asn1_write_value(): seqint.?LAST ");
87 libtasn1_perror (result);
88 exit (1);
91 result = asn1_write_value (asn1_element, "int", "\x0f\xff\x01", 3);
92 if (result != ASN1_SUCCESS)
94 fprintf (stderr, "asn1_write_value(): int ");
95 libtasn1_perror (result);
96 exit (1);
99 result = asn1_write_value (asn1_element, "str", "string", 6);
100 if (result != ASN1_SUCCESS)
102 fprintf (stderr, "asn1_write_value(): str ");
103 libtasn1_perror (result);
104 exit (1);
107 /* Clear the definition structures */
108 asn1_delete_structure (&definitions);
110 result = asn1_der_coding (asn1_element, "", data, &data_size, NULL);
111 if (result != ASN1_SUCCESS)
113 fprintf (stderr, "Encoding error.\n");
114 libtasn1_perror (result);
115 exit (1);
118 result = asn1_der_decoding (&asn1_element, data, data_size, NULL);
119 if (result != ASN1_SUCCESS)
121 fprintf (stderr, "Decoding error.\n");
122 libtasn1_perror (result);
123 exit (1);
127 printf ("Success\n");
128 exit (0);