-
Notifications
You must be signed in to change notification settings - Fork 4
/
group.py
executable file
·509 lines (387 loc) · 13.5 KB
/
group.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# 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 traceback
from Peach import generator
#__all__ = ["Group", "GroupFixed", "GroupSequence", "GroupForeachDo"]
class Group(object):
"""
Groups allow for performing a C{next()} call on a specific set of
Generators allowing for more complex Fuzzing setups. This default group
object will iterate an infinite amount of times.
Group objects implement the iterator protocol.
"""
_name = None
_generators = []
_identity = ""
def __init__(self, name=None):
"""
Create a new Group object.
@type name: string
@param name: Name of Group object. Not currently used.
"""
self._name = name
self._generators = []
# For debugging. This is slow (0.02 sec), if things are slow
# you can comment this line out safely.
self._identity = traceback.format_stack()
def getName(self):
"""
Get current name of Group. Not currently used.
@rtype: string
@return: name of Group
"""
return self._name
def setName(self, name):
"""
Set name of Group. Not currently used.
@type name: string
@param name: Name of Group
"""
self._name = name
def addGenerator(self, gen):
"""
Add Generator to Group. This should almost never be called
directly. Generators will call this when you set there Group.
However, you can do some crazy stuff by adding a Generator into
multiple Groups so they iterate themselves in strange ways.
@type gen: Generator
@param gen: Generator to add
"""
self._generators.append(gen)
def addGenerators(self, gens):
"""
Add Generators to Group. This should almost never be called
directly. Generators will call this when you set there Group.
However, you can do some crazy stuff by adding a Generator into
multiple Groups so they iterate themselves in strange ways.
@type gens: Array of Generators
@param gens: Generatorsto add
"""
for g in gens:
self._generators.append(g)
def removeGenerator(self, gen):
"""
Remove Generator from Group.
@type gen: Generator
@param gen: Generator to remove
"""
self._generators.remove(gen)
def getAllGenerators(self):
"""
Returns list of all generators in Group. This is a reference
to our internal list so any changes will also affect the Group.
@rtype: Array
@return: Returns Array of strings
"""
return self._generators
def __iter__(self):
return self
def next(self):
"""
Iterate all Generators to next value.
From Python docs on next():
I{The intention of the protocol is that once an iterator's next() method
raises StopIteration, it will continue to do so on subsequent calls.
Implementations that do not obey this property are deemed broken. (This
constraint was added in Python 2.3; in Python 2.2, various iterators are
broken according to this rule.)}
For Groups, please use the GroupCompleted exception instead of
StopIteration (its a subclass).
"""
# We will continue until all of our generators are
# returning GeneratorCompleted exceptions
if len(self._generators) < 1:
print("Identity of Group: %s" % self._identity)
raise Exception("Error: Group does not contain any generators. This is probably not a good thing.")
done = 1
for i in range(len(self._generators)):
try:
self._generators[i].next()
done = 0
except generator.GeneratorCompleted:
pass
if done == 1:
raise GroupCompleted()
def reset(self):
"""
Resets all Generators to there initial state.
"""
for i in self._generators:
i.reset()
class GroupCompleted(StopIteration):
"""
Raised when group has completed all iterations. This exception is a
sub class of StopIteration.
"""
pass
class GroupSequence(Group):
"""
A sequence of groups. Each group will be iterated until they are
completed in sequence.
This is also a container type and can be used as such to gain
access to the contained groups.
HINT: If groups param is an integer it will create an array of
Group() objects of that length that can be accessed using
the array specifier groupSequence[x].
"""
def __init__(self, groups=None, name=None):
"""
Create a GroupSequence object.
@type groups: list
@param groups: Optional list of Groups to use
"""
self._slackerCount = 0
if name is None:
self._name = ""
else:
self._name = name
self._generators = []
# Hack allert!
if str(type(groups)) == "<type 'int'>":
self._groups = []
for i in range(groups):
self._groups.append(Group())
elif groups is not None:
self._groups = groups
else:
self._groups = []
self._position = 0
self._count = 1
def getNextGroup(self):
"""
This is a function for slackers that allows access to the next group
without having to specify an index.
@rtype: Group
@return: Returns the next Group in the list
"""
if self._slackerCount >= len(self._groups):
raise Exception("GroupSequence: getNextGroup() ran past end of array.")
self._slackerCount += 1
return self._groups[self._slackerCount - 1]
def addNewGroup(self, newGroup=None):
"""
Will add a new Group to sequence of groups and then return that group.
@type newGroup: Group
@param newGroup: [optional] Group to append, or if not given add Group()
@rtype: Group
@return: Returns appended Group
"""
if newGroup is None:
newGroup = Group()
self._groups.append(newGroup)
return newGroup
def append(self, group=None):
"""
Append a Group.
@type group: Group
@param group: Group to append
@rtype: Group
@return: Returns appended Group
"""
return self.addNewGroup(group)
def remove(self, group):
"""
Remove a Group.
@type group: Group
@param group: Group to remove
"""
self._groups.remove(group)
def next(self):
if self._position < len(self._groups):
try:
self._groups[self._position].next()
self._count += 1
except GroupCompleted:
#sys.stderr.write('%s: GroupSequence.next(): GroupCompleted [%d]\n' % (self._name, self._count))
self._count = 1
self._groups[self._position].reset()
self._position += 1
if self._position >= len(self._groups):
raise GroupCompleted()
else:
raise GroupCompleted()
def reset(self):
for group in self._groups:
group.reset()
self._position = 0
# Container emulation methods ############################
def __len__(self):
return self._groups.__len__()
def __getitem__(self, key):
return self._groups.__getitem__(key)
def __setitem__(self, key, value):
return self._groups.__setitem(key, value)
def __delitem__(self, key):
return self._groups.__delitem__(key)
def __iter__(self):
return self._groups.__iter__()
def __contains__(self, item):
return self._groups.__contains__(item)
import inspect, pyclbr, random
class GroupForever(Group):
"""
This group will take a GroupSequence and perform random mutations
on how generators are incremented. This group understands that a
GroupSequence can have other GroupSequences in it.
"""
def __init__(self, groupSequence):
self.groupSequence = groupSequence
self.groups = self._findAllGroups(groupSequence)
self.count = len(self.groups)
self._resetAll()
self._pickStuff()
def addGroup(self, group):
self.groups.append(group)
for g in self._findAllGroups(group):
self.groups.append(g)
def _resetAll(self):
print("len: %d" % len(self.groups))
for group in self.groups:
group.reset()
def _pickStuff(self):
"""Pick some groups to play with
"""
# Types of mutations
#
# 1. Bunch at once
# 2. A for each
type = random.randint(0, 1)
if type == 0:
# bunch at once
groupsDo = []
picks = random.sample(range(self.count), random.randint(0, self.count - 1))
for pick in picks:
groupsDo.append(self.groups[pick])
self.currentGroup = GroupSequence(groupsDo, "GroupForever")
elif type == 1:
# a for each
groupEach = groupFor = self.groups[random.randint(0, self.count - 1)]
groupsDo = []
picks = random.sample(range(self.count), random.randint(0, self.count - 1))
for pick in picks:
groupsDo.append(self.groups[pick])
try:
groupsDo.remove(groupEach)
except:
pass
groupDo = GroupSequence(groupsDo, "GroupForever")
self.currentGroup = GroupForeachDo(groupEach, groupDo)
else:
raise Exception("GroupForever._pickStuff(): Should not be here!!")
self.isCompleted = False
def _findAllGroups(self, groupSequence):
if hasattr(groupSequence, 'next') and hasattr(groupSequence, '__iter__'):
groups = []
for group in groupSequence:
if group is None:
continue
groups.append(group)
if hasattr(group, 'next') and hasattr(group, '__iter__'):
for g in self._findAllGroups(group):
if g is None:
continue
groups.append(g)
return groups
if hasattr(groupSequence, "getForeachGroup"):
return [group.getForeachGroup(), group.getDoGroup()]
return []
def next(self):
try:
self.currentGroup.next()
except GroupCompleted:
self._resetAll()
self._pickStuff()
def reset(self):
self._resetAll()
self._pickStuff()
class GroupFixed(Group):
"""
Group object with a fixed number of iterations.
"""
_max = 0
_current = 0
def __init__(self, maxIterations=0):
"""
Create GroupFixed object.
@type maxIterations: number
@param maxIterations: Maximum number of iterations.
"""
self._max = maxIterations
Group.__init__(self)
def getMaxIterations(self):
"""
Get the maximum iterations to perform.
@rtype: number
@return the maximum iterations
"""
return self._max
def setMaxIterations(self, maxIterations):
"""
Set the maximum iterations to perform.
@type maxIterations: number
@param maxIterations: Maximum number of iterations.
"""
self._max = maxIterations
def next(self):
if self._current < self._max:
self._current += 1
try:
Group.next(self)
except generator.GeneratorCompleted:
raise GroupCompleted("Peach.group.GroupFixed")
else:
raise GroupCompleted("Peach.group.GroupFixed")
class GroupForeachDo(Group):
"""
For each iteration of group A do group B
"""
#_groupA = None
#_groupB = None
def __init__(self, groupA, groupB, verbose=True, name=""):
"""
For each iteration of group A do group B
@type groupA: Group
@param groupA: The for each of group
@type groupB: Group
@param groupB: The Do group
@type verbose: Boolean
@param verbose: [optional] Control printing of group completed message, enabled by default.
"""
self._generators = []
self._groupA = groupA
self._groupB = groupB
self._count = 1
self._name = name
self._verbose = verbose
self._isCompleted = False
def next(self):
if self._isCompleted:
raise GroupCompleted("We are done")
try:
self._groupB.next()
self._count += 1
except GroupCompleted:
if self._verbose:
print("%s: GroupForeachDo.GroupCompleted -- [%d]" % (self._name, self._count))
self._count = 1
self._groupB.reset()
try:
self._groupA.next()
except GroupCompleted:
self._isCompleted = True
def reset(self):
self._groupA.reset()
self._groupB.reset()
self._count = 1
self._isCompleted = False
def getForeachGroup(self):
"""
Returns the For each group
"""
return self._groupA
def getDoGroup(self):
"""
Returns the Do group
"""
return self._groupB