Waste less stack.
[shishi.git] / asn1 / tests / Test_parser.c
blob5d6b6381c310273f9459445f61fdcc733815fa76
1 /*
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 /*****************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include "libtasn1.h"
32 typedef struct{
33 int lineNumber;
34 char *line;
35 int errorNumber;
36 char *errorDescription;
37 } test_type;
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",
52 ASN1_SUCCESS,""},
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",
58 ASN1_SUCCESS,""},
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}",
63 ASN1_SUCCESS,""},
64 {12,"obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4}",
65 ASN1_IDENTIFIER_NOT_FOUND,_FILE_":: identifier 'pkix' not found"},
67 /* Test INTEGER */
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"},
87 /* Test: OID */
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",
95 ASN1_SUCCESS,""},
98 /* end */
99 {0}
103 readLine(FILE *file,char *line)
105 int c;
107 while(((c=fgetc(file))!=EOF) && (c!='\n')){
108 *line=c;
109 line++;
112 *line=0;
114 return c;
118 void
119 createFile(int lineNumber,char *line)
121 FILE *fileIn,*fileOut;
122 char lineRead[1024];
123 int fileInLineNumber=0;
125 fileIn=fopen(fileCorrectName,"r");
126 fileOut=fopen(fileErroredName,"w");
128 while(readLine(fileIn,lineRead) != EOF){
129 fileInLineNumber++;
130 if(fileInLineNumber==lineNumber)
131 fprintf(fileOut,"%s\n",line);
132 else
133 fprintf(fileOut,"%s\n",lineRead);
136 fclose(fileOut);
137 fclose(fileIn);
141 int
142 main(int argc,char *argv[])
144 asn1_retCode result;
145 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
146 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
147 test_type *test;
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);
161 exit(1);
164 /* Only for Test */
165 /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
167 /* Clear the definitions structures */
168 asn1_delete_structure(&definitions);
171 test=test_array;
173 while(test->lineNumber != 0){
174 testCounter++;
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))){
183 errorCounter++;
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),
189 errorDescription);
192 test++;
196 printf("Total tests : %d\n",testCounter);
197 printf("Total errors: %d\n",errorCounter);
199 exit(0);