Fix memory leaks, tiny patch from Christian Grothoff <christian@grothoff.org>.
[libtasn1.git] / tests / Test_parser.c
blobe8321809c0df3bd587a906c7a3a8038ba2d34bd2
1 /*
2 * Copyright (C) 2006, 2007 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
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 /*****************************************************/
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include "libtasn1.h"
34 typedef struct{
35 int lineNumber;
36 char *line;
37 int errorNumber;
38 char *errorDescription;
39 } test_type;
41 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 */
48 {5,"TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
49 ASN1_SYNTAX_ERROR,_FILE_":6: parse error near 'TEST_PARSER'"},
50 {6,"TEST_PARSER { }",ASN1_SUCCESS,""},
52 /* Test MAX_NAME_SIZE (128) */
53 {12,"a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 ::= INTEGER",
54 ASN1_SUCCESS,""},
55 {12,"a12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 ::= INTEGER",
56 ASN1_NAME_TOO_LONG,_FILE_":12: name too long (more than 128 characters)"},
58 /* Test 'check identifier' function */
59 {12,"ident1 ::= ident2 ident2 ::= INTEGER",
60 ASN1_SUCCESS,""},
61 {12,"ident1 ::= ident2",
62 ASN1_IDENTIFIER_NOT_FOUND,_FILE_":: identifier 'ident2' not found"},
63 {12,"obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4} "
64 "pkix OBJECT IDENTIFIER ::= {1 2}",
65 ASN1_SUCCESS,""},
66 {12,"obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4}",
67 ASN1_IDENTIFIER_NOT_FOUND,_FILE_":: identifier 'pkix' not found"},
69 /* Test INTEGER */
70 {14,"int1 INTEGER OPTIONAL,",ASN1_SUCCESS,""},
71 {14,"int1 INTEGER DEFAULT 1,",ASN1_SUCCESS,""},
72 {14,"int1 INTEGER DEFAULT -1,",ASN1_SUCCESS,""},
73 {14,"int1 INTEGER DEFAULT v1,",ASN1_SUCCESS,""},
74 {14,"int1 [1] INTEGER,",ASN1_SUCCESS,""},
75 {14,"int1 [1] EXPLICIT INTEGER,",ASN1_SUCCESS,""},
76 {14,"int1 [1] IMPLICIT INTEGER,",ASN1_SUCCESS,""},
77 {12,"Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}",ASN1_SUCCESS,""},
78 {12,"Integer ::= INTEGER {v1(0), v2}",
79 ASN1_SYNTAX_ERROR,_FILE_":12: parse error near '}'"},
80 {12,"Integer ::= INTEGER {v1(0), 1}",
81 ASN1_SYNTAX_ERROR,_FILE_":12: parse error near '1'"},
82 {12,"const1 INTEGER ::= -1",ASN1_SUCCESS,""},
83 {12,"const1 INTEGER ::= 1",ASN1_SUCCESS,""},
84 {12,"const1 INTEGER ::= v1",
85 ASN1_SYNTAX_ERROR,_FILE_":12: parse error near 'v1'"},
86 {16," generic generalstring",
87 ASN1_IDENTIFIER_NOT_FOUND,_FILE_":: identifier 'generalstring' not found"},
89 /* Test: OID */
90 {20," oid1 OBJECT IDENTIFIER DEFAULT Oid-type",
91 ASN1_IDENTIFIER_NOT_FOUND,_FILE_":: identifier 'Oid-type' not found"},
92 {20," oid1 OBJECT IDENTIFIER DEFAULT 1",
93 ASN1_IDENTIFIER_NOT_FOUND,_FILE_":: identifier '1' not found"},
94 {20," oid1 OBJECT IDENTIFIER DEFAULT",
95 ASN1_SYNTAX_ERROR,_FILE_":21: parse error near '}'"},
96 {20," oid1 OBJECT IDENTIFIER DEFAULT Oid-type1",
97 ASN1_SUCCESS,""},
100 /* end */
105 readLine(FILE *file,char *line)
107 int c;
109 while(((c=fgetc(file))!=EOF) && (c!='\n')){
110 *line=c;
111 line++;
114 *line=0;
116 return c;
120 void
121 createFile(int lineNumber,char *line)
123 FILE *fileIn,*fileOut;
124 char lineRead[1024];
125 int fileInLineNumber=0;
127 fileIn=fopen(fileCorrectName,"r");
128 fileOut=fopen(fileErroredName,"w");
130 while(readLine(fileIn,lineRead) != EOF){
131 fileInLineNumber++;
132 if(fileInLineNumber==lineNumber)
133 fprintf(fileOut,"%s\n",line);
134 else
135 fprintf(fileOut,"%s\n",lineRead);
138 fclose(fileOut);
139 fclose(fileIn);
143 int
144 main(int argc,char *argv[])
146 asn1_retCode result;
147 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
148 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
149 test_type *test;
150 int errorCounter=0,testCounter=0;
152 fileCorrectName = getenv ("ASN1PARSER");
153 if (!fileCorrectName)
154 fileCorrectName="Test_parser.asn";
156 printf("\n\n/****************************************/\n");
157 printf( "/* Test sequence : Test_parser */\n");
158 printf( "/****************************************/\n\n");
159 printf("ASN1PARSER: %s\n", fileCorrectName);
161 result=asn1_parser2tree(fileCorrectName,&definitions,errorDescription);
163 if(result!=ASN1_SUCCESS){
164 printf("File '%s' not correct\n",fileCorrectName);
165 libtasn1_perror(result);
166 printf("ErrorDescription = %s\n\n",errorDescription);
167 exit(1);
170 /* Only for Test */
171 /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
173 /* Clear the definitions structures */
174 asn1_delete_structure(&definitions);
177 test=test_array;
179 while(test->lineNumber != 0){
180 testCounter++;
182 createFile(test->lineNumber,test->line);
184 result=asn1_parser2tree(fileErroredName,&definitions,errorDescription);
185 asn1_delete_structure(&definitions);
187 if((result != test->errorNumber) ||
188 (strcmp(errorDescription,test->errorDescription))){
189 errorCounter++;
190 printf("ERROR N. %d:\n",errorCounter);
191 printf(" Line %d - %s\n",test->lineNumber,test->line);
192 printf(" Error expected: %s - %s\n",libtasn1_strerror(test->errorNumber),
193 test->errorDescription);
194 printf(" Error detected: %s - %s\n\n",libtasn1_strerror(result),
195 errorDescription);
198 test++;
202 printf("Total tests : %d\n",testCounter);
203 printf("Total errors: %d\n",errorCounter);
205 if(errorCounter > 0)
206 return 1;
208 exit(0);