2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBASN1.
6 * LIBASN1 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 2 of the License, or
9 * (at your option) any later version.
11 * LIBASN1 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /*****************************************************/
23 /* File: Test_parser.c */
24 /* Description: Test sequences for these functions: */
25 /* asn1_parser_asn1, */
26 /*****************************************************/
36 char *errorDescription
;
39 char fileCorrectName
[]="Test_parser.asn";
40 char fileErroredName
[]="Test_parser_ERROR.asn";
42 #define _FILE_ "Test_parser_ERROR.asn"
44 test_type test_array
[]={
45 /* Test DEFINITIONS syntax */
46 {5,"TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
47 ASN1_SYNTAX_ERROR
,_FILE_
":6: parse error near 'TEST_PARSER'"},
48 {6,"TEST_PARSER { }",ASN1_SUCCESS
,""},
50 /* Test MAX_NAME_SIZE (128) */
51 {12,"a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 ::= INTEGER",
53 {12,"a12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 ::= INTEGER",
54 ASN1_NAME_TOO_LONG
,_FILE_
":12: name too long (more than 128 characters)"},
56 /* Test 'check identifier' function */
57 {12,"ident1 ::= ident2 ident2 ::= INTEGER",
59 {12,"ident1 ::= ident2",
60 ASN1_IDENTIFIER_NOT_FOUND
,_FILE_
":: identifier 'ident2' not found"},
61 {12,"obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4} "
62 "pkix OBJECT IDENTIFIER ::= {1 2}",
64 {12,"obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4}",
65 ASN1_IDENTIFIER_NOT_FOUND
,_FILE_
":: identifier 'pkix' not found"},
68 {14,"int1 INTEGER OPTIONAL,",ASN1_SUCCESS
,""},
69 {14,"int1 INTEGER DEFAULT 1,",ASN1_SUCCESS
,""},
70 {14,"int1 INTEGER DEFAULT -1,",ASN1_SUCCESS
,""},
71 {14,"int1 INTEGER DEFAULT v1,",ASN1_SUCCESS
,""},
72 {14,"int1 [1] INTEGER,",ASN1_SUCCESS
,""},
73 {14,"int1 [1] EXPLICIT INTEGER,",ASN1_SUCCESS
,""},
74 {14,"int1 [1] IMPLICIT INTEGER,",ASN1_SUCCESS
,""},
75 {12,"Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}",ASN1_SUCCESS
,""},
76 {12,"Integer ::= INTEGER {v1(0), v2}",
77 ASN1_SYNTAX_ERROR
,_FILE_
":12: parse error near '}'"},
78 {12,"Integer ::= INTEGER {v1(0), 1}",
79 ASN1_SYNTAX_ERROR
,_FILE_
":12: parse error near '1'"},
80 {12,"const1 INTEGER ::= -1",ASN1_SUCCESS
,""},
81 {12,"const1 INTEGER ::= 1",ASN1_SUCCESS
,""},
82 {12,"const1 INTEGER ::= v1",
83 ASN1_SYNTAX_ERROR
,_FILE_
":12: parse error near 'v1'"},
84 {16," generic generalstring",
85 ASN1_IDENTIFIER_NOT_FOUND
,_FILE_
":: identifier 'generalstring' not found"},
88 {20," oid1 OBJECT IDENTIFIER DEFAULT Oid-type",
89 ASN1_IDENTIFIER_NOT_FOUND
,_FILE_
":: identifier 'Oid-type' not found"},
90 {20," oid1 OBJECT IDENTIFIER DEFAULT 1",
91 ASN1_IDENTIFIER_NOT_FOUND
,_FILE_
":: identifier '1' not found"},
92 {20," oid1 OBJECT IDENTIFIER DEFAULT",
93 ASN1_SYNTAX_ERROR
,_FILE_
":21: parse error near '}'"},
94 {20," oid1 OBJECT IDENTIFIER DEFAULT Oid-type1",
103 readLine(FILE *file
,char *line
)
107 while(((c
=fgetc(file
))!=EOF
) && (c
!='\n')){
119 createFile(int lineNumber
,char *line
)
121 FILE *fileIn
,*fileOut
;
123 int fileInLineNumber
=0;
125 fileIn
=fopen(fileCorrectName
,"r");
126 fileOut
=fopen(fileErroredName
,"w");
128 while(readLine(fileIn
,lineRead
) != EOF
){
130 if(fileInLineNumber
==lineNumber
)
131 fprintf(fileOut
,"%s\n",line
);
133 fprintf(fileOut
,"%s\n",lineRead
);
142 main(int argc
,char *argv
[])
145 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
146 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
148 int errorCounter
=0,testCounter
=0;
150 printf("\n\n/****************************************/\n");
151 printf( "/* Test sequence : Test_parser */\n");
152 printf( "/****************************************/\n\n");
155 result
=asn1_parser2tree(fileCorrectName
,&definitions
,errorDescription
);
157 if(result
!=ASN1_SUCCESS
){
158 printf("File '%s' not correct\n",fileCorrectName
);
159 libtasn1_perror(result
);
160 printf("ErrorDescription = %s\n\n",errorDescription
);
165 /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
167 /* Clear the definitions structures */
168 asn1_delete_structure(&definitions
);
173 while(test
->lineNumber
!= 0){
176 createFile(test
->lineNumber
,test
->line
);
178 result
=asn1_parser2tree(fileErroredName
,&definitions
,errorDescription
);
179 asn1_delete_structure(&definitions
);
181 if((result
!= test
->errorNumber
) ||
182 (strcmp(errorDescription
,test
->errorDescription
))){
184 printf("ERROR N. %d:\n",errorCounter
);
185 printf(" Line %d - %s\n",test
->lineNumber
,test
->line
);
186 printf(" Error expected: %s - %s\n",libtasn1_strerror(test
->errorNumber
),
187 test
->errorDescription
);
188 printf(" Error detected: %s - %s\n\n",libtasn1_strerror(result
),
196 printf("Total tests : %d\n",testCounter
);
197 printf("Total errors: %d\n",errorCounter
);