2 * Copyright (C) 2002, 2006, 2007, 2009, 2010 Free Software
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/>.
23 /*****************************************************/
24 /* File: Test_parser.c */
25 /* Description: Test sequences for these functions: */
26 /* asn1_parser_asn1, */
27 /*****************************************************/
39 char *errorDescription;
42 char *fileCorrectName;
43 char fileErroredName[] = "Test_parser_ERROR.asn";
45 #define _FILE_ "Test_parser_ERROR.asn"
47 test_type test_array[] = {
48 /* Test DEFINITIONS syntax */
50 "TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
51 ASN1_SYNTAX_ERROR, _FILE_ ":6: parse error near 'TEST_PARSER'"},
52 {6, "TEST_PARSER { }", ASN1_SUCCESS, ""},
54 /* Test ASN1_MAX_NAME_SIZE (128) */
56 "a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 ::= INTEGER",
59 "a12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 ::= INTEGER",
61 _FILE_ ":12: name too long (more than 128 characters)"},
63 /* Test 'check identifier' function */
64 {12, "ident1 ::= ident2 ident2 ::= INTEGER",
66 {12, "ident1 ::= ident2",
67 ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'ident2' not found"},
68 {12, "obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4} "
69 "pkix OBJECT IDENTIFIER ::= {1 2}",
71 {12, "obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4}",
72 ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'pkix' not found"},
75 {14, "int1 INTEGER OPTIONAL,", ASN1_SUCCESS, ""},
76 {14, "int1 INTEGER DEFAULT 1,", ASN1_SUCCESS, ""},
77 {14, "int1 INTEGER DEFAULT -1,", ASN1_SUCCESS, ""},
78 {14, "int1 INTEGER DEFAULT v1,", ASN1_SUCCESS, ""},
79 {14, "int1 [1] INTEGER,", ASN1_SUCCESS, ""},
80 {14, "int1 [1] EXPLICIT INTEGER,", ASN1_SUCCESS, ""},
81 {14, "int1 [1] IMPLICIT INTEGER,", ASN1_SUCCESS, ""},
82 {12, "Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}", ASN1_SUCCESS, ""},
83 {12, "Integer ::= INTEGER {v1(0), v2}",
84 ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near '}'"},
85 {12, "Integer ::= INTEGER {v1(0), 1}",
86 ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near '1'"},
87 {12, "const1 INTEGER ::= -1", ASN1_SUCCESS, ""},
88 {12, "const1 INTEGER ::= 1", ASN1_SUCCESS, ""},
89 {12, "const1 INTEGER ::= v1",
90 ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near 'v1'"},
91 {16, " generic generalstring",
92 ASN1_IDENTIFIER_NOT_FOUND,
93 _FILE_ ":: identifier 'generalstring' not found"},
96 {20, " oid1 OBJECT IDENTIFIER DEFAULT Oid-type",
97 ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'Oid-type' not found"},
98 {20, " oid1 OBJECT IDENTIFIER DEFAULT 1",
99 ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier '1' not found"},
100 {20, " oid1 OBJECT IDENTIFIER DEFAULT",
101 ASN1_SYNTAX_ERROR, _FILE_ ":21: parse error near '}'"},
102 {20, " oid1 OBJECT IDENTIFIER DEFAULT Oid-type1",
111 readLine (FILE * file, char *line)
115 while (((c = fgetc (file)) != EOF) && (c != '\n'))
128 createFile (int lineNumber, char *line)
130 FILE *fileIn, *fileOut;
132 int fileInLineNumber = 0;
134 fileIn = fopen (fileCorrectName, "r");
135 fileOut = fopen (fileErroredName, "w");
137 while (readLine (fileIn, lineRead) != EOF)
140 if (fileInLineNumber == lineNumber)
141 fprintf (fileOut, "%s\n", line);
143 fprintf (fileOut, "%s\n", lineRead);
152 main (int argc, char *argv[])
155 ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
156 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
158 int errorCounter = 0, testCounter = 0;
160 fileCorrectName = getenv ("ASN1PARSER");
161 if (!fileCorrectName)
162 fileCorrectName = "Test_parser.asn";
164 printf ("\n\n/****************************************/\n");
165 printf ("/* Test sequence : Test_parser */\n");
166 printf ("/****************************************/\n\n");
167 printf ("ASN1PARSER: %s\n", fileCorrectName);
169 result = asn1_parser2tree (fileCorrectName, &definitions, errorDescription);
171 if (result != ASN1_SUCCESS)
173 printf ("File '%s' not correct\n", fileCorrectName);
174 asn1_perror (result);
175 printf ("ErrorDescription = %s\n\n", errorDescription);
180 /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
182 /* Clear the definitions structures */
183 asn1_delete_structure (&definitions);
188 while (test->lineNumber != 0)
192 createFile (test->lineNumber, test->line);
195 asn1_parser2tree (fileErroredName, &definitions, errorDescription);
196 asn1_delete_structure (&definitions);
198 if ((result != test->errorNumber) ||
199 (strcmp (errorDescription, test->errorDescription)))
202 printf ("ERROR N. %d:\n", errorCounter);
203 printf (" Line %d - %s\n", test->lineNumber, test->line);
204 printf (" Error expected: %s - %s\n",
205 asn1_strerror (test->errorNumber), test->errorDescription);
206 printf (" Error detected: %s - %s\n\n", asn1_strerror (result),
214 printf ("Total tests : %d\n", testCounter);
215 printf ("Total errors: %d\n", errorCounter);
217 if (errorCounter > 0)