-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_basic_parse.py
More file actions
330 lines (278 loc) · 8.62 KB
/
Copy pathtest_basic_parse.py
File metadata and controls
330 lines (278 loc) · 8.62 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
import os
import sys
import unittest
sys.path.insert(0, "..")
from py010parser import parse_file, parse_string, c_ast
def template_path(template_name):
return os.path.join(os.path.dirname(__file__), "templates", template_name)
class TestBasicParse(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_sizeof_unary(self):
res = parse_string("""
sizeof(this);
""")
def test_exists_unary(self):
res = parse_string("""
exists(this);
""")
def test_parentof_unary(self):
res = parse_string("""
parentof(this);
""")
def test_function_exists_unary(self):
res = parse_string("""
function_exists(this);
""")
def test_startof_unary(self):
res = parse_string("""
startof(this);
""")
def test_bitfield_in_if(self):
res = parse_string("""
struct {
if(1) {
int field1:16;
//int field1;
}
} blah;
""", optimize=True, predefine_types=False)
bitfield_decl = res.children()[0][1].type.type.children()[0][1].iftrue.children()[0][1]
self.assertNotEqual(type(bitfield_decl), dict)
def test_bitfield_outside_of_struct(self):
res = parse_string("""
uint blah1:4;
uint blah2:8;
uint blah3:4;
""", optimize=True, predefine_types=True)
def test_basic(self):
res = parse_string("""
struct NAME {
int stringLength;
char name[stringLength];
} name;
""", optimize=True)
def test_declaration_in_switch(self):
res = parse_string("""
int c;
switch(c) {
case 1:
c++;
case 2:
int c;
}
""", optimize=True)
def test_declaration_in_if(self):
res = parse_string("""
if(1) {
int c;
} else {
int b;
}
""", optimize=True)
# http://www.sweetscape.com/010editor/manual/TemplateVariables.htm
def test_local_keyword(self):
res = parse_string("""
local int a;
local int b;
""", optimize=True)
def test_metadata(self):
res = parse_string("""
local int a <hidden=true>;
""", optimize=True)
def test_metadata_with_quoted_strings(self):
res = parse_string("""
local int asdfsdf <comment="Hello > there">;
""", optimize=True, predefine_types=False)
node = res.children()[0][1]
self.assertEqual(len(node.metadata.values), 1)
self.assertEqual(node.metadata.values[0][0], "comment")
self.assertEqual(node.metadata.values[0][1], '"Hello > there"')
def test_typedef(self):
res = parse_string("""
typedef unsigned int UINT2;
UINT2 blah;
""", optimize=True)
def test_value_types(self):
res = parse_string("""
time_t var1;
OLETIME var2;
FILETIME var3;
DOSTIME var4;
DOSDATE var5;
HFLOAT var6;
hfloat var7;
DOUBLE var8;
double var9;
FLOAT var10;
float var11;
__uint64 var12;
QWORD var13;
UINT64 var14;
UQUAD var15;
uquad var16;
uint64 var17;
__int64 var18;
INT64 var19;
QUAD var20;
quad var21;
int64 var22;
DWORD var23;
ULONG var24;
UINT32 var25;
UINT var26;
ulong var27;
uint32 var28;
uint var29;
LONG var30;
INT32 var31;
INT var32;
long var33;
int32 var34;
int var35;
WORD var36;
UINT16 var37;
USHORT var38;
uint16 var39;
ushort var40;
INT16 var41;
SHORT var42;
int16 var43;
short var44;
UBYTE var45;
UCHAR var46;
ubyte var47;
uchar var48;
BYTE var49;
CHAR var50;
byte var51;
char var52;
string var53;
wstring var54;
wchar_t var55;
""", optimize=True)
def test_block_item_at_root(self):
# had to get rid of the default int ret val on functions
# from pycparser
res = parse_string("""
int a = 10;
void some_function(int num) {
some_function();
}
a++;
some_function();
""", optimize=True)
def test_pass_by_reference(self):
res = parse_string("""
void some_function(int &num, int &num2) {
}
void some_function(int &num2) {
}
""", optimize=True)
def test_single_decl_in_for_loop(self):
res = parse_string("""
for(j = 0; j < 10; j++)
ushort blah;
""", optimize=True)
def test_single_decl_in_while_loop(self):
res = parse_string("""
while(1)
ushort blah;
""", optimize=True)
def test_single_decl_in_do_while_loop(self):
res = parse_string("""
while(1)
ushort blah;
""", optimize=True)
def test_single_decl_in_do_while_loop(self):
res = parse_string("""
do
ushort blah;
while(1);
""", optimize=True)
def test_single_decl_in_if(self):
res = parse_string("""
if(1)
ushort blah;
""", optimize=True)
def test_single_decl_in_if_else(self):
res = parse_string("""
if(1)
ushort blah;
else
ushort blah;
if(1) {
ushort blah;
} else
ushort blah;
if(1)
ushort blah;
else {
ushort blah;
}
if(1) {
ushort blah;
} else {
ushort blah;
}
""", optimize=True)
# I think we'll make a break from 010 syntax here...
# it's too ridiculous to me to allow types that have
# not yet been defined
def test_runtime_declared_type(self):
res = parse_string("""
void ReadAscString1(StrAscii1 &s) {
;
}
""", optimize=True, predefine_types=False)
def test_comment_single_line(self):
res = parse_string("""
// this is a comment
int blah;
""", optimize=True, predefine_types=False)
self.assertEqual(len(res.children()), 1, "should only have one node in the AST")
node = res.children()[0][1]
self.assertEqual(node.name, "blah")
self.assertEqual(node.type.declname, "blah")
self.assertEqual(node.type.type.names, ["int"])
def test_comment_multi_line(self):
res = parse_string("""
/*
this is a comment
*/
int blah;
""", optimize=True, predefine_types=False)
self.assertEqual(len(res.children()), 1, "should only have one node in the AST")
node = res.children()[0][1]
self.assertEqual(node.name, "blah")
self.assertEqual(node.type.declname, "blah")
self.assertEqual(node.type.type.names, ["int"])
def test_metadata_with_string_value(self):
res = parse_string("""
int a <comment="this is a comment", key=val>;
int a <comment="this is a comment">;
""", optimize=True)
def test_large_template(self):
res = parse_file(template_path("JPGTemplate.bt"))
def test_png_template(self):
res = parse_file(template_path("PNGTemplate.bt"))
def test_exe_template(self):
res = parse_file(template_path("EXETemplate.bt"))
def test_preprocessor_with_string(self):
res = parse_string("""
//this shouldn't cause any problems
int a;
""", optimize=True)
def test_metadata_with_space1(self):
res = parse_string("""
int a < key1 = value1 >;
""", optimize=True)
def test_metadata_with_space2(self):
res = parse_string("""
int a < key1 = value1 , key2 = value2 >;
""", optimize=True)
if __name__ == "__main__":
unittest.main()