Add, to workaround gtk-dock bug.
[libtasn1.git] / tests / Test_parser.c
blob12f6cb56521cf4b9d672726f1308234cbafd26e5
1 /*
2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBTASN1.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA
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[]="Test_parser.asn";
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 printf("\n\n/****************************************/\n");
153 printf( "/* Test sequence : Test_parser */\n");
154 printf( "/****************************************/\n\n");
157 result=asn1_parser2tree(fileCorrectName,&definitions,errorDescription);
159 if(result!=ASN1_SUCCESS){
160 printf("File '%s' not correct\n",fileCorrectName);
161 libtasn1_perror(result);
162 printf("ErrorDescription = %s\n\n",errorDescription);
163 exit(1);
166 /* Only for Test */
167 /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
169 /* Clear the definitions structures */
170 asn1_delete_structure(&definitions);
173 test=test_array;
175 while(test->lineNumber != 0){
176 testCounter++;
178 createFile(test->lineNumber,test->line);
180 result=asn1_parser2tree(fileErroredName,&definitions,errorDescription);
181 asn1_delete_structure(&definitions);
183 if((result != test->errorNumber) ||
184 (strcmp(errorDescription,test->errorDescription))){
185 errorCounter++;
186 printf("ERROR N. %d:\n",errorCounter);
187 printf(" Line %d - %s\n",test->lineNumber,test->line);
188 printf(" Error expected: %s - %s\n",libtasn1_strerror(test->errorNumber),
189 test->errorDescription);
190 printf(" Error detected: %s - %s\n\n",libtasn1_strerror(result),
191 errorDescription);
194 test++;
198 printf("Total tests : %d\n",testCounter);
199 printf("Total errors: %d\n",errorCounter);
201 if(errorCounter > 0)
202 return 1;
204 exit(0);