2 * Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 * This file is part of LIBTASN1.
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.
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.
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/>.
22 /*****************************************************/
23 /* File: Test_parser.c */
24 /* Description: Test sequences for these functions: */
25 /* asn1_parser_asn1, */
26 /*****************************************************/
38 const char *errorDescription;
41 const char *fileCorrectName;
42 char fileErroredName[] = "Test_parser_ERROR.asn";
44 #define _FILE_ "Test_parser_ERROR.asn"
46 test_type test_array[] = {
47 /* Test DEFINITIONS syntax */
49 "TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
52 ":6: Error: syntax error, unexpected IDENTIFIER, expecting $end near 'TEST_PARSER'"},
53 {6, "TEST_PARSER { }", ASN1_SUCCESS, ""},
55 /* Test ASN1_MAX_NAME_SIZE (128) */
57 "a123456789012345678901234567890123456789012345678901234567890123 ::= INTEGER",
60 "a1234567890123456789012345678901234567890123456789012345678901234 ::= INTEGER",
62 _FILE_ ":12: name too long (more than 64 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 (-5..5),", ASN1_SUCCESS, ""},
76 {14, "int1 INTEGER OPTIONAL,", ASN1_SUCCESS, ""},
77 {14, "int1 INTEGER DEFAULT 1,", ASN1_SUCCESS, ""},
78 {14, "int1 INTEGER DEFAULT -1,", ASN1_SUCCESS, ""},
79 {14, "int1 INTEGER DEFAULT v1,", ASN1_SUCCESS, ""},
80 {14, "int1 [1] INTEGER,", ASN1_SUCCESS, ""},
81 {14, "int1 [1] EXPLICIT INTEGER,", ASN1_SUCCESS, ""},
82 {14, "int1 [1] IMPLICIT INTEGER,", ASN1_SUCCESS, ""},
83 {12, "Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}", ASN1_SUCCESS, ""},
84 {12, "Integer ::= INTEGER {v1(0), v2}", ASN1_SYNTAX_ERROR,
85 _FILE_ ":12: Error: syntax error, unexpected '}', expecting '(' near '}'"},
86 {12, "Integer ::= INTEGER {v1(0), 1}",
89 ":12: Error: syntax error, unexpected NUM, expecting IDENTIFIER or '(' near '1'"},
90 {12, "const1 INTEGER ::= -1", ASN1_SUCCESS, ""},
91 {12, "const1 INTEGER ::= 1", ASN1_SUCCESS, ""},
92 {12, "const1 INTEGER ::= v1",
95 ":12: Error: syntax error, unexpected IDENTIFIER, expecting NUM or '+' or '-' near 'v1'"},
96 {16, " generic generalstring",
97 ASN1_IDENTIFIER_NOT_FOUND,
98 _FILE_ ":: identifier 'generalstring' not found"},
101 {20, " oid1 OBJECT IDENTIFIER DEFAULT Oid-type",
102 ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'Oid-type' not found"},
103 {20, " oid1 OBJECT IDENTIFIER DEFAULT 1",
104 ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier '1' not found"},
105 {20, " oid1 OBJECT IDENTIFIER DEFAULT",
107 _FILE_ ":21: Error: syntax error, unexpected '}' near '}'"},
108 {20, " oid1 OBJECT IDENTIFIER DEFAULT Oid-type1",
116 readLine (FILE * file, char *line)
120 while (((c = fgetc (file)) != EOF) && (c != '\n'))
132 createFile (int lineNumber, const char *line)
134 FILE *fileIn, *fileOut;
136 int fileInLineNumber = 0;
138 fileIn = fopen (fileCorrectName, "r");
139 fileOut = fopen (fileErroredName, "w");
141 while (readLine (fileIn, lineRead) != EOF)
144 if (fileInLineNumber == lineNumber)
145 fprintf (fileOut, "%s\n", line);
147 fprintf (fileOut, "%s\n", lineRead);
156 main (int argc, char *argv[])
159 asn1_node definitions = NULL;
160 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
162 int errorCounter = 0, testCounter = 0;
168 fileCorrectName = getenv ("ASN1PARSER");
169 if (!fileCorrectName)
170 fileCorrectName = "Test_parser.asn";
174 printf ("\n\n/****************************************/\n");
175 printf ("/* Test sequence : Test_parser */\n");
176 printf ("/****************************************/\n\n");
177 printf ("ASN1PARSER: %s\n", fileCorrectName);
180 result = asn1_parser2tree (fileCorrectName, &definitions, errorDescription);
182 if (result != ASN1_SUCCESS)
184 printf ("File '%s' not correct\n", fileCorrectName);
185 asn1_perror (result);
186 printf ("ErrorDescription = %s\n\n", errorDescription);
191 /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
193 /* Clear the definitions structures */
194 asn1_delete_structure (&definitions);
199 while (test->lineNumber != 0)
203 createFile (test->lineNumber, test->line);
206 asn1_parser2tree (fileErroredName, &definitions, errorDescription);
207 asn1_delete_structure (&definitions);
209 if ((result != test->errorNumber) ||
210 (strcmp (errorDescription, test->errorDescription)))
213 printf ("ERROR N. %d:\n", errorCounter);
214 printf (" Line %d - %s\n", test->lineNumber, test->line);
215 printf (" Error expected: %s - %s\n",
216 asn1_strerror (test->errorNumber), test->errorDescription);
217 printf (" Error detected: %s - %s\n\n", asn1_strerror (result),
228 printf ("Total tests : %d\n", testCounter);
229 printf ("Total errors: %d\n", errorCounter);
232 if (errorCounter > 0)