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 : : /*****************************************************/
23 : : /* File: Test_parser.c */
24 : : /* Description: Test sequences for these functions: */
25 : : /* asn1_parser_asn1, */
26 : : /*****************************************************/
27 : :
28 : : #include <stdio.h>
29 : : #include <string.h>
30 : : #include <stdlib.h>
31 : : #include "libtasn1.h"
32 : :
33 : : typedef struct
34 : : {
35 : : int lineNumber;
36 : : const char *line;
37 : : int errorNumber;
38 : : const char *errorDescription;
39 : : } test_type;
40 : :
41 : : const char *fileCorrectName;
42 : : char fileErroredName[] = "Test_parser_ERROR.asn";
43 : :
44 : : #define _FILE_ "Test_parser_ERROR.asn"
45 : :
46 : : test_type test_array[] = {
47 : : /* Test DEFINITIONS syntax */
48 : : {5,
49 : : "TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
50 : : ASN1_SYNTAX_ERROR, _FILE_ ":6: parse error near 'TEST_PARSER'"},
51 : : {6, "TEST_PARSER { }", ASN1_SUCCESS, ""},
52 : :
53 : : /* Test ASN1_MAX_NAME_SIZE (128) */
54 : : {12,
55 : : "a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 ::= INTEGER",
56 : : ASN1_SUCCESS, ""},
57 : : {12,
58 : : "a12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 ::= INTEGER",
59 : : ASN1_NAME_TOO_LONG,
60 : : _FILE_ ":12: name too long (more than 128 characters)"},
61 : :
62 : : /* Test 'check identifier' function */
63 : : {12, "ident1 ::= ident2 ident2 ::= INTEGER",
64 : : ASN1_SUCCESS, ""},
65 : : {12, "ident1 ::= ident2",
66 : : ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'ident2' not found"},
67 : : {12, "obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4} "
68 : : "pkix OBJECT IDENTIFIER ::= {1 2}",
69 : : ASN1_SUCCESS, ""},
70 : : {12, "obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4}",
71 : : ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'pkix' not found"},
72 : :
73 : : /* Test INTEGER */
74 : : {14, "int1 INTEGER OPTIONAL,", ASN1_SUCCESS, ""},
75 : : {14, "int1 INTEGER DEFAULT 1,", ASN1_SUCCESS, ""},
76 : : {14, "int1 INTEGER DEFAULT -1,", ASN1_SUCCESS, ""},
77 : : {14, "int1 INTEGER DEFAULT v1,", ASN1_SUCCESS, ""},
78 : : {14, "int1 [1] INTEGER,", ASN1_SUCCESS, ""},
79 : : {14, "int1 [1] EXPLICIT INTEGER,", ASN1_SUCCESS, ""},
80 : : {14, "int1 [1] IMPLICIT INTEGER,", ASN1_SUCCESS, ""},
81 : : {12, "Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}", ASN1_SUCCESS, ""},
82 : : {12, "Integer ::= INTEGER {v1(0), v2}",
83 : : ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near '}'"},
84 : : {12, "Integer ::= INTEGER {v1(0), 1}",
85 : : ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near '1'"},
86 : : {12, "const1 INTEGER ::= -1", ASN1_SUCCESS, ""},
87 : : {12, "const1 INTEGER ::= 1", ASN1_SUCCESS, ""},
88 : : {12, "const1 INTEGER ::= v1",
89 : : ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near 'v1'"},
90 : : {16, " generic generalstring",
91 : : ASN1_IDENTIFIER_NOT_FOUND,
92 : : _FILE_ ":: identifier 'generalstring' not found"},
93 : :
94 : : /* Test: OID */
95 : : {20, " oid1 OBJECT IDENTIFIER DEFAULT Oid-type",
96 : : ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'Oid-type' not found"},
97 : : {20, " oid1 OBJECT IDENTIFIER DEFAULT 1",
98 : : ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier '1' not found"},
99 : : {20, " oid1 OBJECT IDENTIFIER DEFAULT",
100 : : ASN1_SYNTAX_ERROR, _FILE_ ":21: parse error near '}'"},
101 : : {20, " oid1 OBJECT IDENTIFIER DEFAULT Oid-type1",
102 : : ASN1_SUCCESS, ""},
103 : :
104 : :
105 : : /* end */
106 : : {0}
107 : : };
108 : :
109 : : static int
110 : 1170 : readLine (FILE * file, char *line)
111 : : {
112 : : int c;
113 : :
114 [ + + ][ + + ]: 32838 : while (((c = fgetc (file)) != EOF) && (c != '\n'))
115 : : {
116 : 31668 : *line = c;
117 : 31668 : line++;
118 : : }
119 : :
120 : 1170 : *line = 0;
121 : :
122 : 1170 : return c;
123 : : }
124 : :
125 : : static void
126 : 26 : createFile (int lineNumber, const char *line)
127 : : {
128 : : FILE *fileIn, *fileOut;
129 : : char lineRead[1024];
130 : 26 : int fileInLineNumber = 0;
131 : :
132 : 26 : fileIn = fopen (fileCorrectName, "r");
133 : 26 : fileOut = fopen (fileErroredName, "w");
134 : :
135 [ + + ]: 1170 : while (readLine (fileIn, lineRead) != EOF)
136 : : {
137 : 1144 : fileInLineNumber++;
138 [ + + ]: 1144 : if (fileInLineNumber == lineNumber)
139 : 26 : fprintf (fileOut, "%s\n", line);
140 : : else
141 : 1118 : fprintf (fileOut, "%s\n", lineRead);
142 : : }
143 : :
144 : 26 : fclose (fileOut);
145 : 26 : fclose (fileIn);
146 : 26 : }
147 : :
148 : :
149 : : int
150 : 1 : main (int argc, char *argv[])
151 : : {
152 : : asn1_retCode result;
153 : 1 : ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
154 : : char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
155 : : test_type *test;
156 : 1 : int errorCounter = 0, testCounter = 0;
157 : :
158 : 1 : fileCorrectName = getenv ("ASN1PARSER");
159 [ - + ]: 1 : if (!fileCorrectName)
160 : 0 : fileCorrectName = "Test_parser.asn";
161 : :
162 : 1 : printf ("\n\n/****************************************/\n");
163 : 1 : printf ("/* Test sequence : Test_parser */\n");
164 : 1 : printf ("/****************************************/\n\n");
165 : 1 : printf ("ASN1PARSER: %s\n", fileCorrectName);
166 : :
167 : 1 : result = asn1_parser2tree (fileCorrectName, &definitions, errorDescription);
168 : :
169 [ - + ]: 1 : if (result != ASN1_SUCCESS)
170 : : {
171 : 0 : printf ("File '%s' not correct\n", fileCorrectName);
172 : 0 : asn1_perror (result);
173 : 0 : printf ("ErrorDescription = %s\n\n", errorDescription);
174 : 0 : exit (1);
175 : : }
176 : :
177 : : /* Only for Test */
178 : : /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
179 : :
180 : : /* Clear the definitions structures */
181 : 1 : asn1_delete_structure (&definitions);
182 : :
183 : :
184 : 1 : test = test_array;
185 : :
186 [ + + ]: 27 : while (test->lineNumber != 0)
187 : : {
188 : 26 : testCounter++;
189 : :
190 : 26 : createFile (test->lineNumber, test->line);
191 : :
192 : 26 : result =
193 : : asn1_parser2tree (fileErroredName, &definitions, errorDescription);
194 : 26 : asn1_delete_structure (&definitions);
195 : :
196 [ + - ][ - + ]: 26 : if ((result != test->errorNumber) ||
197 : 26 : (strcmp (errorDescription, test->errorDescription)))
198 : : {
199 : 0 : errorCounter++;
200 : 0 : printf ("ERROR N. %d:\n", errorCounter);
201 : 0 : printf (" Line %d - %s\n", test->lineNumber, test->line);
202 : 0 : printf (" Error expected: %s - %s\n",
203 : : asn1_strerror (test->errorNumber), test->errorDescription);
204 : 0 : printf (" Error detected: %s - %s\n\n", asn1_strerror (result),
205 : : errorDescription);
206 : : }
207 : :
208 : 26 : test++;
209 : : }
210 : :
211 : :
212 : 1 : printf ("Total tests : %d\n", testCounter);
213 : 1 : printf ("Total errors: %d\n", errorCounter);
214 : :
215 [ - + ]: 1 : if (errorCounter > 0)
216 : 0 : return 1;
217 : :
218 : 1 : exit (0);
219 : : }
|