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