asn1_read_value() and friends understand the ?CURRENT keyword.
[platform/upstream/libtasn1.git] / tests / Test_parser.c
1 /*
2  * Copyright (C) 2002-2014 Free Software Foundation, Inc.
3  *
4  * This file is part of LIBTASN1.
5  *
6  * This program 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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21
22 /*****************************************************/
23 /* File: Test_parser.c                               */
24 /* Description: Test sequences for these functions:  */
25 /*     asn1_parser_asn1,                             */
26 /*****************************************************/
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include "libtasn1.h"
32
33 typedef struct
34 {
35   int lineNumber;
36   const char *line;
37   int errorNumber;
38   const char *errorDescription;
39 } test_type;
40
41 const char *fileCorrectName;
42 char fileErroredName[] = "Test_parser_ERROR.asn";
43
44 #define _FILE_ "Test_parser_ERROR.asn"
45
46 test_type test_array[] = {
47   /* Test DEFINITIONS syntax */
48   {5,
49    "TEST_PARSER2 { } DEFINITIONS IMPLICIT TAGS ::= BEGIN int1 ::= INTEGER END",
50    ASN1_SYNTAX_ERROR,
51    _FILE_
52    ":6: Error: syntax error, unexpected IDENTIFIER, expecting $end near 'TEST_PARSER'"},
53   {6, "TEST_PARSER { }", ASN1_SUCCESS, ""},
54
55   /* Test ASN1_MAX_NAME_SIZE (128) */
56   {12,
57    "a123456789012345678901234567890123456789012345678901234567890123 ::= INTEGER",
58    ASN1_SUCCESS, ""},
59   {12,
60    "a1234567890123456789012345678901234567890123456789012345678901234 ::= INTEGER",
61    ASN1_NAME_TOO_LONG,
62    _FILE_ ":12: name too long (more than 64 characters)"},
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 (-5..5),", ASN1_SUCCESS, ""},
76   {14, "int1 INTEGER OPTIONAL,", ASN1_SUCCESS, ""},
77   {14, "int1 INTEGER DEFAULT 1,", ASN1_SUCCESS, ""},
78   {14, "int1 INTEGER DEFAULT -1,", ASN1_SUCCESS, ""},
79   {14, "int1 INTEGER DEFAULT v1,", ASN1_SUCCESS, ""},
80   {14, "int1 [1] INTEGER,", ASN1_SUCCESS, ""},
81   {14, "int1 [1] EXPLICIT INTEGER,", ASN1_SUCCESS, ""},
82   {14, "int1 [1] IMPLICIT INTEGER,", ASN1_SUCCESS, ""},
83   {12, "Integer ::= [1] EXPLICIT INTEGER {v1(-1), v2(1)}", ASN1_SUCCESS, ""},
84   {12, "Integer ::= INTEGER {v1(0), v2}", ASN1_SYNTAX_ERROR,
85    _FILE_ ":12: Error: syntax error, unexpected '}', expecting '(' near '}'"},
86   {12, "Integer ::= INTEGER {v1(0), 1}",
87    ASN1_SYNTAX_ERROR,
88    _FILE_
89    ":12: Error: syntax error, unexpected NUM, expecting IDENTIFIER or '(' near '1'"},
90   {12, "const1 INTEGER ::= -1", ASN1_SUCCESS, ""},
91   {12, "const1 INTEGER ::= 1", ASN1_SUCCESS, ""},
92   {12, "const1 INTEGER ::= v1",
93    ASN1_SYNTAX_ERROR,
94    _FILE_
95    ":12: Error: syntax error, unexpected IDENTIFIER, expecting NUM or '+' or '-' near 'v1'"},
96   {16, " generic generalstring",
97    ASN1_IDENTIFIER_NOT_FOUND,
98    _FILE_ ":: identifier 'generalstring' not found"},
99
100   /* Test: OID */
101   {20, "   oid1    OBJECT IDENTIFIER DEFAULT Oid-type",
102    ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier 'Oid-type' not found"},
103   {20, "   oid1    OBJECT IDENTIFIER DEFAULT 1",
104    ASN1_IDENTIFIER_NOT_FOUND, _FILE_ ":: identifier '1' not found"},
105   {20, "   oid1    OBJECT IDENTIFIER DEFAULT",
106    ASN1_SYNTAX_ERROR,
107    _FILE_ ":21: Error: syntax error, unexpected '}' near '}'"},
108   {20, "   oid1    OBJECT IDENTIFIER DEFAULT Oid-type1",
109    ASN1_SUCCESS, ""},
110
111   /* end */
112   {0}
113 };
114
115 static int
116 readLine (FILE * file, char *line)
117 {
118   int c;
119
120   while (((c = fgetc (file)) != EOF) && (c != '\n'))
121     {
122       *line = c;
123       line++;
124     }
125
126   *line = 0;
127
128   return c;
129 }
130
131 static void
132 createFile (int lineNumber, const char *line)
133 {
134   FILE *fileIn, *fileOut;
135   char lineRead[1024];
136   int fileInLineNumber = 0;
137
138   fileIn = fopen (fileCorrectName, "r");
139   fileOut = fopen (fileErroredName, "w");
140
141   while (readLine (fileIn, lineRead) != EOF)
142     {
143       fileInLineNumber++;
144       if (fileInLineNumber == lineNumber)
145         fprintf (fileOut, "%s\n", line);
146       else
147         fprintf (fileOut, "%s\n", lineRead);
148     }
149
150   fclose (fileOut);
151   fclose (fileIn);
152 }
153
154
155 int
156 main (int argc, char *argv[])
157 {
158   int result;
159   asn1_node definitions = NULL;
160   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
161   test_type *test;
162   int errorCounter = 0, testCounter = 0;
163   int verbose = 0;
164
165   if (argc > 1)
166     verbose = 1;
167
168   fileCorrectName = getenv ("ASN1PARSER");
169   if (!fileCorrectName)
170     fileCorrectName = "Test_parser.asn";
171
172   if (verbose != 0)
173     {
174       printf ("\n\n/****************************************/\n");
175       printf ("/*     Test sequence : Test_parser      */\n");
176       printf ("/****************************************/\n\n");
177       printf ("ASN1PARSER: %s\n", fileCorrectName);
178     }
179
180   result = asn1_parser2tree (fileCorrectName, &definitions, errorDescription);
181
182   if (result != ASN1_SUCCESS)
183     {
184       printf ("File '%s' not correct\n", fileCorrectName);
185       asn1_perror (result);
186       printf ("ErrorDescription = %s\n\n", errorDescription);
187       exit (1);
188     }
189
190   /* Only for Test */
191   /* asn1_visit_tree(stdout,definitions,"TEST_PARSER",ASN1_PRINT_ALL); */
192
193   /* Clear the definitions structures */
194   asn1_delete_structure (&definitions);
195
196
197   test = test_array;
198
199   while (test->lineNumber != 0)
200     {
201       testCounter++;
202
203       createFile (test->lineNumber, test->line);
204
205       result =
206         asn1_parser2tree (fileErroredName, &definitions, errorDescription);
207       asn1_delete_structure (&definitions);
208
209       if ((result != test->errorNumber) ||
210           (strcmp (errorDescription, test->errorDescription)))
211         {
212           errorCounter++;
213           printf ("ERROR N. %d:\n", errorCounter);
214           printf ("  Line %d - %s\n", test->lineNumber, test->line);
215           printf ("  Error expected: %s - %s\n",
216                   asn1_strerror (test->errorNumber), test->errorDescription);
217           printf ("  Error detected: %s - %s\n\n", asn1_strerror (result),
218                   errorDescription);
219           exit (1);
220         }
221
222       test++;
223     }
224
225
226   if (verbose != 0)
227     {
228       printf ("Total tests : %d\n", testCounter);
229       printf ("Total errors: %d\n", errorCounter);
230     }
231
232   if (errorCounter > 0)
233     return 1;
234
235   exit (0);
236 }