Initialize Tizen 2.3
[external/libtasn1.git] / tests / Test_parser.c
1 /*
2  * Copyright (C) 2002, 2006, 2007, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * This file is part of LIBTASN1.
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22
23 /*****************************************************/
24 /* File: Test_parser.c                               */
25 /* Description: Test sequences for these functions:  */
26 /*     asn1_parser_asn1,                             */
27 /*****************************************************/
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include "libtasn1.h"
33
34 typedef struct
35 {
36   int lineNumber;
37   char *line;
38   int errorNumber;
39   char *errorDescription;
40 } test_type;
41
42 char *fileCorrectName;
43 char fileErroredName[] = "Test_parser_ERROR.asn";
44
45 #define _FILE_ "Test_parser_ERROR.asn"
46
47 test_type test_array[] = {
48   /* Test DEFINITIONS syntax */
49   {5,
50    "TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
51    ASN1_SYNTAX_ERROR, _FILE_ ":6: parse error near 'TEST_PARSER'"},
52   {6, "TEST_PARSER { }", ASN1_SUCCESS, ""},
53
54   /* Test ASN1_MAX_NAME_SIZE (128) */
55   {12,
56    "a1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 ::= INTEGER",
57    ASN1_SUCCESS, ""},
58   {12,
59    "a12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 ::= INTEGER",
60    ASN1_NAME_TOO_LONG,
61    _FILE_ ":12: name too long (more than 128 characters)"},
62
63   /* Test 'check identifier' function */
64   {12, "ident1 ::= ident2   ident2 ::= INTEGER",
65    ASN1_SUCCESS, ""},
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}",
70    ASN1_SUCCESS, ""},
71   {12, "obj1 OBJECT IDENTIFIER ::= {pkix 0 5 4}",
72    ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'pkix' not found"},
73
74   /* Test INTEGER */
75   {14, "int1 INTEGER OPTIONAL,", ASN1_SUCCESS, ""},
76   {14, "int1 INTEGER DEFAULT 1,", ASN1_SUCCESS, ""},
77   {14, "int1 INTEGER DEFAULT -1,", ASN1_SUCCESS, ""},
78   {14, "int1 INTEGER DEFAULT v1,", ASN1_SUCCESS, ""},
79   {14, "int1 [1] INTEGER,", ASN1_SUCCESS, ""},
80   {14, "int1 [1] EXPLICIT INTEGER,", ASN1_SUCCESS, ""},
81   {14, "int1 [1] IMPLICIT INTEGER,", ASN1_SUCCESS, ""},
82   {12, "Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}", ASN1_SUCCESS, ""},
83   {12, "Integer ::= INTEGER {v1(0), v2}",
84    ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near '}'"},
85   {12, "Integer ::= INTEGER {v1(0), 1}",
86    ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near '1'"},
87   {12, "const1 INTEGER ::= -1", ASN1_SUCCESS, ""},
88   {12, "const1 INTEGER ::= 1", ASN1_SUCCESS, ""},
89   {12, "const1 INTEGER ::= v1",
90    ASN1_SYNTAX_ERROR, _FILE_ ":12: parse error near 'v1'"},
91   {16, " generic generalstring",
92    ASN1_IDENTIFIER_NOT_FOUND,
93    _FILE_ ":: identifier 'generalstring' not found"},
94
95   /* Test: OID */
96   {20, "   oid1    OBJECT IDENTIFIER DEFAULT Oid-type",
97    ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'Oid-type' not found"},
98   {20, "   oid1    OBJECT IDENTIFIER DEFAULT 1",
99    ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier '1' not found"},
100   {20, "   oid1    OBJECT IDENTIFIER DEFAULT",
101    ASN1_SYNTAX_ERROR, _FILE_ ":21: parse error near '}'"},
102   {20, "   oid1    OBJECT IDENTIFIER DEFAULT Oid-type1",
103    ASN1_SUCCESS, ""},
104
105
106   /* end */
107   {0}
108 };
109
110 int
111 readLine (FILE * file, char *line)
112 {
113   int c;
114
115   while (((c = fgetc (file)) != EOF) && (c != '\n'))
116     {
117       *line = c;
118       line++;
119     }
120
121   *line = 0;
122
123   return c;
124 }
125
126
127 void
128 createFile (int lineNumber, char *line)
129 {
130   FILE *fileIn, *fileOut;
131   char lineRead[1024];
132   int fileInLineNumber = 0;
133
134   fileIn = fopen (fileCorrectName, "r");
135   fileOut = fopen (fileErroredName, "w");
136
137   while (readLine (fileIn, lineRead) != EOF)
138     {
139       fileInLineNumber++;
140       if (fileInLineNumber == lineNumber)
141         fprintf (fileOut, "%s\n", line);
142       else
143         fprintf (fileOut, "%s\n", lineRead);
144     }
145
146   fclose (fileOut);
147   fclose (fileIn);
148 }
149
150
151 int
152 main (int argc, char *argv[])
153 {
154   asn1_retCode result;
155   ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
156   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
157   test_type *test;
158   int errorCounter = 0, testCounter = 0;
159
160   fileCorrectName = getenv ("ASN1PARSER");
161   if (!fileCorrectName)
162     fileCorrectName = "Test_parser.asn";
163
164   printf ("\n\n/****************************************/\n");
165   printf ("/*     Test sequence : Test_parser      */\n");
166   printf ("/****************************************/\n\n");
167   printf ("ASN1PARSER: %s\n", fileCorrectName);
168
169   result = asn1_parser2tree (fileCorrectName, &definitions, errorDescription);
170
171   if (result != ASN1_SUCCESS)
172     {
173       printf ("File '%s' not correct\n", fileCorrectName);
174       asn1_perror (result);
175       printf ("ErrorDescription = %s\n\n", errorDescription);
176       exit (1);
177     }
178
179   /* Only for Test */
180   /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
181
182   /* Clear the definitions structures */
183   asn1_delete_structure (&definitions);
184
185
186   test = test_array;
187
188   while (test->lineNumber != 0)
189     {
190       testCounter++;
191
192       createFile (test->lineNumber, test->line);
193
194       result =
195         asn1_parser2tree (fileErroredName, &definitions, errorDescription);
196       asn1_delete_structure (&definitions);
197
198       if ((result != test->errorNumber) ||
199           (strcmp (errorDescription, test->errorDescription)))
200         {
201           errorCounter++;
202           printf ("ERROR N. %d:\n", errorCounter);
203           printf ("  Line %d - %s\n", test->lineNumber, test->line);
204           printf ("  Error expected: %s - %s\n",
205                   asn1_strerror (test->errorNumber), test->errorDescription);
206           printf ("  Error detected: %s - %s\n\n", asn1_strerror (result),
207                   errorDescription);
208         }
209
210       test++;
211     }
212
213
214   printf ("Total tests : %d\n", testCounter);
215   printf ("Total errors: %d\n", errorCounter);
216
217   if (errorCounter > 0)
218     return 1;
219
220   exit (0);
221 }