-
Notifications
You must be signed in to change notification settings - Fork 4
/
xmlstuff.py
executable file
·362 lines (299 loc) · 11.3 KB
/
xmlstuff.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import re
import sys
import struct
from Peach.generator import Generator
from Peach.Generators.dictionary import *
from Peach.Generators.static import *
import Peach.Generators.static
class XmlCreateElements(Generator):
"""
This generator create XML elements N deep
"""
_startingDepth = 1
_increment = 1
_nodePrefix = Static('PeachFuzz')
_nodePostfix = None
_elementAttributs = None
_currentDepth = 1
_maxDepth = 1000
def __init__(self, group, startingDepth=None, increment=None,
maxDepth=None, nodePrefix=None, nodePostfix=None,
elementAttributes=None):
"""
@type group: Group
@param group: Group to use
@type startingDepth: integer
@param startingDepth: How many deep to start at, default 1
@type increment: integer
@param increment: Incrementor, default 1
@type maxDepth: integer
@param maxDepth: Max depth, default 1000
@type nodePrefix: Generator
@param nodePrefix: Node prefix, default is Static('PeachFuzz')
@type nodePostfix: Generator
@param nodePostfix: Node postfix, default is None
@type elementAttributes: Generator
@param elementAttributes: Element attributes, default is None
"""
self.setGroup(group)
if startingDepth is not None:
self._startingDepth = startingDepth
if increment is not None:
self._increment = increment
if nodePrefix is not None:
self._nodePrefix = nodePrefix
if nodePostfix is not None:
self._nodePostfix = nodePostfix
if elementAttributes is not None:
self._elementAttributes = elementAttributes
if maxDepth is not None:
self._maxDepth = maxDepth
def next(self):
self._currentDepth += self._increment
if self._currentDepth > self._maxDepth:
raise generator.GeneratorCompleted("XmlCreateNodes")
def getRawValue(self):
ret = ''
postFixs = []
for i in range(self._currentDepth):
if self._nodePostfix is not None:
postFixs[i] = self._nodePostfix.getValue()
if self._elementAttributes is not None:
ret += "<%s%s %s>" % (self._nodePrefix.getValue(), postFixs[i],
self._elementAttributes.getValue())
else:
ret += "<%s%s>" % (self._nodePrefix.getValue(), postFixs[i])
else:
if self._elementAttributes is not None:
ret += "<%s %s>" % (self._nodePrefix.getValue(),
self._elementAttributes.getValue())
else:
ret += "<%s>" % self._nodePrefix.getValue()
for j in range(self._currentDepth):
if self._nodePostfix is not None:
ret += "</%s%s>" % (self._nodePrefix.getValue(), postFixs[i - j])
else:
ret += "</%s>" % self._nodePrefix.getValue()
return ret
def reset(self):
self._currentDepth = 1
@staticmethod
def unittest():
expected = '<PeachFuzz1><PeachFuzz2><PeachFuzz3></PeachFuzz3></PeachFuzz2></PeachFuzz1>'
g = XmlCreateNodes(1, 1)
g.next()
g.next()
g.next()
if g.getRawValue() != expected:
print("FAILURE!!! XmlCreateNodes")
class XmlCreateNodes(Generator):
"""
This generator create XML nodes N deep
"""
_startingDepth = 1
_increment = 1
_nodePrefix = Static('PeachFuzz')
_currentDepth = 1
_maxDepth = 1000
def __init__(self, group, startingDepth, increment, maxDepth, nodePrefix):
"""
@type group: Group
@param group: Group to use
@type startingDepth: integer
@param startingDepth: How many deep to start at, default 1
@type increment: integer
@param increment: Incrementor, default 1
@type maxDepth: integer
@param maxDepth: Max depth, default 1000
@type nodePrefix: Generator
@param nodePrefix: Node prefix, default is Static('PeachFuzz')
"""
self.setGroup(group)
if startingDepth is not None:
self._startingDepth = startingDepth
if increment is not None:
self._increment = increment
if nodePrefix is not None:
self._nodePrefix = nodePrefix
if maxDepth is not None:
self._maxDepth = maxDepth
def next(self):
self._currentDepth += self._increment
if self._currentDepth > self._maxDepth:
raise generator.GeneratorCompleted("XmlCreateNodes")
def getRawValue(self):
ret = ''
for i in range(self._currentDepth):
ret += "<%s%d>" % (self._nodePrefix.getValue(), i)
for j in range(self._currentDepth):
ret += "</%s%d>" % (self._nodePrefix.getValue(), i - j)
return ret
def reset(self):
self._currentDepth = 1
@staticmethod
def unittest():
expected = '<PeachFuzz1><PeachFuzz2><PeachFuzz3></PeachFuzz3></PeachFuzz2></PeachFuzz1>'
g = XmlCreateNodes(1, 1)
g.next()
g.next()
g.next()
if g.getRawValue() != expected:
print("FAILURE!!! XmlCreateNodes")
class XmlParserTests(Generator):
"""
W3C XML Validation Tests. This includes
all sets of tests, invalid, non-well formed, valid and error.
NOTE: Test files are in samples/xmltests.zip these are the
latest test cases from W3C as of 02/23/06 for XML.
"""
def __init__(self, group, testFiles=None):
"""
@type group: Group
@param group: Group this Generator belongs to
@type testFiles: string
@param testFiles: Location of test files
"""
Generator.__init__(self)
p = None
if not (hasattr(sys, "frozen") and sys.frozen == "console_exe"):
p = Peach.Generators.static.__file__[:-10]
else:
p = os.path.dirname(os.path.abspath(sys.executable))
testFiles = os.path.join(p, "xmltests")
self._generatorList = GeneratorList(group,
[XmlParserTestsInvalid(None, testFiles),
XmlParserTestsNotWellFormed(None, testFiles),
XmlParserTestsValid(None, testFiles)])
def getRawValue(self):
return self._generatorList.getRawValue()
def next(self):
self._generatorList.next()
class XmlParserTestsGeneric(Generator):
"""
Base class
"""
def __init__(self, group, testsFolder, testsFile):
"""
@type group: Group
@param group: Group this Generator belongs to
@type testsFolder: string
@param testsFolder: Location of test files
@type testsFile: string
@param testsFile: File with listing of test files
"""
Generator.__init__(self)
self._testsFolder = 'xmltests'
self._testsFile = 'invalid.txt'
self._currentValue = None
self._currentTestNum = 1
self._currentFilename = None
self._fdTests = None
self._fd = None
self.setGroup(group)
if testsFile is not None:
self._testsFile = testsFile
if testsFolder is not None:
self._testsFolder = testsFolder
def next(self):
if self._fdTests is None:
fileName = os.path.join(self._testsFolder, self._testsFile)
self._fdTests = open(fileName, 'rb')
self._currentFilename = os.path.join(self._testsFolder,
self._fdTests.readline())
self._currentFilename = self._currentFilename.strip("\r\n")
if len(self._currentFilename) <= len(self._testsFolder) + 2:
raise generator.GeneratorCompleted(
"Peach.Generators.xml.XmlParserTestsInvalid")
if self._fd is None:
self._fd = open(self._currentFilename, 'rb')
if self._fd is None:
raise Exception('Unable to open', self._currentFilename)
self._currentValue = self._fd.read()
self._fd = None
def getRawValue(self):
if self._currentValue is None:
self.next()
return self._currentValue
def reset(self):
self._fd = None
self._fdTests = None
self._currentValue = None
@staticmethod
def unittest():
pass
class XmlParserTestsInvalid(XmlParserTestsGeneric):
"""
W3C XML Validation Tests, invalid set only.
NOTE: Test files are in samples/xmltests.zip these are the
latest test cases from W3C as of 02/23/06 for XML.
"""
def __init__(self, group, testsFolder):
"""
@type group: Group
@param group: Group this Generator belongs to
@type testsFolder: string
@param testsFolder: Location of test files
"""
XmlParserTestsGeneric.__init__(self, group, testsFolder, None)
self.setGroup(group)
self._testsFile = 'invalid.txt'
if testsFolder is not None:
self._testsFolder = testsFolder
class XmlParserTestsValid(XmlParserTestsGeneric):
"""
W3C XML Validation Tests, valid set only.
NOTE: Test files are in samples/xmltests.zip these are the
latest test cases from W3C as of 02/23/06 for XML.
"""
def __init__(self, group, testsFolder):
"""
@type group: Group
@param group: Group this Generator belongs to
@type testsFolder: string
@param testsFolder: Location of test files
"""
XmlParserTestsGeneric.__init__(self, group, testsFolder, None)
self.setGroup(group)
self._testsFile = 'valid.txt'
if testsFolder is not None:
self._testsFolder = testsFolder
class XmlParserTestsError(XmlParserTestsGeneric):
"""
W3C XML Validation Tests, error set only.
NOTE: Test files are in samples/xmltests.zip these are the
latest test cases from W3C as of 02/23/06 for XML.
"""
def __init__(self, group, testsFolder):
"""
@type group: Group
@param group: Group this Generator belongs to
@type testsFolder: string
@param testsFolder: Location of test files
"""
XmlParserTestsGeneric.__init__(self, group, testsFolder, None)
self.setGroup(group)
self._testsFile = 'error.txt'
if testsFolder is not None:
self._testsFolder = testsFolder
class XmlParserTestsNotWellFormed(XmlParserTestsGeneric):
"""
W3C XML Validation Tests, Invalid set only.
NOTE: Test files are in samples/xmltests.zip these are the
latest test cases from W3C as of 02/23/06 for XML.
"""
def __init__(self, group, testsFolder):
"""
@type group: Group
@param group: Group this Generator belongs to
@type testsFolder: string
@param testsFolder: Location of test files
"""
XmlParserTestsGeneric.__init__(self, group, testsFolder, None)
self.setGroup(group)
self._testsFile = 'nonwf.txt'
if testsFolder is not None:
self._testsFolder = testsFolder