-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
309 lines (304 loc) · 7.33 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#! /usr/bin/env python
## RFC5246 Data definition language parser
## Rich Salz, [email protected], June 2013.
## Copyright 2013-2015, Rich Salz
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
import os, StringIO, subprocess, sys
tests = (
(
"uint8 f; uint8 f;\n",
"INFILE:1: error near ``;'': Duplicate symbol ``f'' found\n"
),
(
"",
"INFILE:1: error near ``'': syntax error, unexpected $end\n"
),
(
"uint16 f<10..1>;\n",
"INFILE:1: error near ``;'': ``f'' range 10 less than 1\n"
),
(
"uint16 f[0];\n",
"INFILE:1: error near ``;'': ``f'' size (0) is not positive\n"
),
(
"uint16 f[2^33-1];\n",
"INFILE:1: error near ``2^33-1'': Exponent out of range\n"
"INFILE:1: error near ``;'': ``f'' size (0) is not positive\n"
),
(
"uint16 f[2^3-1];\n",
"INFILE:1: error near ``2^3-1'': Bad exponent\n"
"INFILE:1: error near ``;'': ``f'' size (0) is not positive\n"
),
(
"uint16 f[2^3-2];\n",
"INFILE:1: error near ``2^3-2'': Bad exponent\n"
"INFILE:1: error near ``;'': ``f'' size (0) is not positive\n"
),
(
"enum { a, a } b;",
"INFILE:1: error near ``a'': Duplicate ``a'' in enum\n"
),
(
"enum { c(1), a(1..3) } b;",
"INFILE:1: error near ``)'': Enum range for ``a'' for non-reserved name\n"
),
(
"enum { c(1), reserved(1..3) } b;",
""
),
(
"enum { a(1), b } c;",
"INFILE:1: error near ``}'': syntax error, unexpected '}', expecting '('\n"
),
(
"enum { a(1), b, } c;",
"INFILE:1: error near ``,'': syntax error, unexpected ',', expecting '('\n"
),
(
"enum { a(20), b(20), (12) } d; ",
"INFILE:1: error near ``;'': Value for ``a'' is too big (20 > 12)\n"
"INFILE:1: error near ``;'': Enum ``b'' duplicates value 20\n"
"INFILE:1: error near ``;'': Value for ``b'' is too big (20 > 12)\n"
),
(
'''extern f;
uint32 g[f];
uint32 gg[ff];''',
"INFILE:3: error near ``;'': Unknown size reference ``ff''\n"
),
(
"select (f) { case a: ; } f;",
"INFILE:1: error near ``{'': Unknown variant selector ``f''\n"
),
(
'''extern f;
select (f) { case a: ; } g;''',
""
),
(
'''enum { true, false } bool;
select (bool) {
case a: case a: ;
} g;''',
"INFILE:3: error near ``:'': Duplicate case ``a''\n"
),
(
'''extern f; extern extensions;
select (f) {
case a: case b: case c: digitally-signed extensions;
case d: ;
} s;''',
""
),
(
'''extern f; extern extensions;
select (f) {
case a: case b: case c: digitally-signed extensions;
case a: ;
} s;''',
"INFILE:5: error near ``;'': Duplicate case ``a'' in ``s''\n"
),
(
'''extern f; extern extensions;
select (f) {
case a: case b: case b: digitally-signed extensions;
} s;''',
"INFILE:3: error near ``:'': Duplicate case ``b''\n"
),
(
'''extern f; extern extensions;
select (f) {
case a: case b: case c: digitally-signed extensions ; uint8 spacer;
} s;''',
"INFILE:3: error near ``uint8'': syntax error, unexpected tUINT8, expecting tCASE or '}'\n"
),
(
'''extern f; extern extensions; extern d;
select (f) {
case a: case b: case c:
digitally-signed extensions;
d e;
} s;''',
""
),
(
'''extern f; extern extensions;
select (f) {
case a: digitally-signed extensions ;
case b: f;
} s;''',
""
),
(
'''extern f; extern extensions;
select (f) {
case a: digitally-signed extensions ;
case b: f field;
} s;''',
""
),
(
'''extern f; extern extensions;
select (f) {
case c: f2 field;
} s;''',
"INFILE:4: error near ``}'': Unknown member type ``f2''\n"
),
(
"uint8 f; extern f;",
""
),
(
"extern f; uint8 f;",
""
),
(
"struct { uint8 f; uint8 f; } s;",
"INFILE:1: error near ``;'': Duplicate item ``f'' in ``s''\n"
),
(
'''extern ZZ;
struct { uint8 f; ZZ g; } s;''',
""
),
(
"struct { uint8 f; mytype g; } s;",
"INFILE:1: error near ``;'': Unknown member type ``mytype''\n"
),
(
'''struct {
uint8 size;
opaque g[s.size];
} s;''',
"INFILE:3: error near ``;'': Unknown size reference ``s.size''\n"
"INFILE:3: error near ``;'': Note: cannot resolve dotted items.\n"
),
(
'''struct {
uint8 size;
} s;
opaque g[s.size];
opaque h[s.size];''',
"INFILE:4: error near ``;'': Unknown size reference ``s.size''\n"
"INFILE:4: error near ``;'': Note: cannot resolve dotted items.\n"
"INFILE:5: error near ``;'': Unknown size reference ``s.size''\n"
),
(
"struct { } empty;",
""
),
(
'''// comment
uint8 foo;
struct {foo bar;} baz;''',
""
),
(
"uint8 f; /* comment",
"INFILE:1: error near ``'': EOF in comment\n"
),
(
"uint8 f; // /* comment",
""
),
(
'''extern Extension; extern extensions_present;
uint8 ProtocolVersion; opaque Random[12]; opaque SessionID[2^8-1];
struct {
ProtocolVersion client_version;
Random random;
SessionID session_id;
select (extensions_present) {
case false:
;
case true:
Extension extensions<0..2^16-1>;
};
} ClientHello;''',
""
),
(
'''uint8 f; struct {} empty;
select (f) {
case false:
select (f) {
case false: ;
}
}''',
"INFILE:4: error near ``select'': syntax error, unexpected tSELECT\n"
),
(
'''struct {
struct {
uint16 x[2];
} n;
uint32 x;
} b;''',
""
),
(
'''struct { uint8 f[12]; } x;
struct {
struct {
x nested[2];
} n;
uint32 x;
} b;''',
""
),
(
'''struct {
struct {
foo nested[2];
} n;
uint32 x;
} b;''',
"INFILE:4: error near ``;'': Unknown member type ``foo''\n"
),
(
'''struct {
select (b.x) {
case foo: ;
} n;
uint32 x;
} b;''',
"INFILE:2: error near ``{'': Unknown variant selector ``b.x''\n"
"INFILE:2: error near ``{'': Note: cannot resolve dotted items.\n"
),
)
infile = "test-in"
outfile = "test-out"
passed = 0
failed = 0
argv = ( "./parser", infile )
for test,expected in tests:
open(infile, "w").write(test)
subprocess.Popen(argv, stdout=open(outfile, "w")).wait()
results = open(outfile).read()
expected = expected.replace("INFILE", infile)
if results == expected:
passed += 1
else:
failed += 1
print "---\nFail"
print " - Test", passed + failed + 1, "\n", test, "\n"
print " - Expected\n", expected, "\n"
print " - Got\n", results, "\n"
print "---"
os.unlink(infile)
os.unlink(outfile)
print "passed", passed, "failed", failed, "total", passed + failed
sys.exit(failed)