-
Notifications
You must be signed in to change notification settings - Fork 4
/
rand.py
executable file
·347 lines (327 loc) · 14.4 KB
/
rand.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
# 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 random
import hashlib
import logging
from Peach.Engine.engine import Engine
from Peach.mutatestrategies import *
from Peach.Engine.incoming import DataCracker
from Peach.Engine.common import *
class _RandomMutator(object):
name = "Random"
changedName = "N/A"
class RandomMutationStrategy(MutationStrategy):
"""
This mutation strategy will randomly select N fields from a data model to
fuzz on each test case.
Note: This strategy does not affect the state model. First test case will
not be modified.
"""
def __init__(self, node, parent):
MutationStrategy.__init__(self, node, parent)
if node is not None and node.get("seed") is not None:
Engine.context.SEED = RandomMutationStrategy.SEED = node.get("seed")
else:
RandomMutationStrategy.SEED = Engine.context.SEED
self.switchCount = 200
if node is not None and node.get("switchCount") is not None:
self.switchCount = int(node.get("switchCount"))
self.iterationCount = 0
self.multipleFiles = False
self.isFinite = False
self._n = 7
if node is not None and node.get("maxFieldsToMutate") is not None:
self._n = int(node.get("maxFieldsToMutate"))
self._dataModels = {}
self._fieldMutators = {}
self._isFirstTestCase = True
self._dataModelToChange = None
self._random = random.Random()
self._random.seed(hashlib.sha512(str(RandomMutationStrategy.SEED) +
str(self.iterationCount)).digest())
self._mutator = _RandomMutator()
def next(self):
self.iterationCount += 1
self._random.seed(hashlib.sha512(str(RandomMutationStrategy.SEED) +
str(self.iterationCount)).digest())
def getCount(self):
"""
Return the number of test cases.
"""
return None
def _getNodeCount(self, node):
"""
Return the number of DataElements that are children of node.
"""
return len(node.getAllChildDataElements())
def currentMutator(self):
"""
Return the current Mutator in use.
"""
return self._mutator
def onTestCaseStarting(self, test, count, stateEngine):
"""
Called as we start a test case
@type test: Test instance
@param test: Current test being run
@type count: int
@param count: Current test #
@type stateEngine: StateEngine instance
@param stateEngine: StateEngine instance in use
"""
if not self._isFirstTestCase:
## Select the data model to change
self._dataModelToChange = \
self._random.choice(self._dataModels.keys())
def onTestCaseFinished(self, test, count, stateEngine):
"""
Called as we exit a test case
@type test: Test instance
@param test: Current test being run
@type count: int
@param count: Current test #
@type stateEngine: StateEngine instance
@param stateEngine: StateEngine instance in use
"""
self._isFirstTestCase = False
self._dataModelToChange = None
def GetRef(self, str, parent=None, childAttr='templates'):
"""
Get the object indicated by ref. Currently the object must have been
defined prior to this point in the XML.
"""
#print "GetRef(%s) -- Starting" % str
origStr = str
baseObj = self.context
hasNamespace = False
isTopName = True
found = False
# Parse out a namespace
if str.find(":") > -1:
ns, tmp = str.split(':')
str = tmp
#print "GetRef(%s): Found namepsace: %s" % (str, ns)
# Check for namespace
if hasattr(self.context.namespaces, ns):
baseObj = getattr(self.context.namespaces, ns)
else:
#print self
raise PeachException("Unable to locate namespace: " + origStr)
hasNamespace = True
for name in str.split('.'):
#print "GetRef(%s): Looking for part %s" % (str, name)
found = False
if not hasNamespace and isTopName and parent is not None:
# Check parent, walk up from current parent to top level
# parent checking at each level.
while parent is not None and not found:
#print "GetRef(%s): Parent.name: %s" % (name, parent.name)
if hasattr(parent, 'name') and parent.name == name:
baseObj = parent
found = True
elif hasattr(parent, name):
baseObj = getattr(parent, name)
found = True
elif hasattr(parent.children, name):
baseObj = getattr(parent.children, name)
found = True
elif hasattr(parent, childAttr) and \
hasattr(getattr(parent, childAttr), name):
baseObj = getattr(getattr(parent, childAttr), name)
found = True
else:
parent = parent.parent
# Check base obj
elif hasattr(baseObj, name):
baseObj = getattr(baseObj, name)
found = True
# Check childAttr
elif hasattr(baseObj, childAttr):
obj = getattr(baseObj, childAttr)
if hasattr(obj, name):
baseObj = getattr(obj, name)
found = True
else:
raise PeachException("Could not resolve ref %s" % origStr)
# Check childAttr
if found is False and hasattr(baseObj, childAttr):
obj = getattr(baseObj, childAttr)
if hasattr(obj, name):
baseObj = getattr(obj, name)
found = True
# Check across namespaces if we can't find it in ours
if isTopName and found is False:
for child in baseObj:
if child.elementType != 'namespace':
continue
#print "GetRef(%s): CHecking namepsace: %s" % (str, child.name)
ret = self._SearchNamespaces(child, name, childAttr)
if ret:
#print "GetRef(%s) Found part %s in namespace" % (str, name)
baseObj = ret
found = True
isTopName = False
if not found:
raise PeachException("Unable to resolve reference: %s" % origStr)
return baseObj
def _SearchNamespaces(self, obj, name, attr):
"""
Used by GetRef to search across namespaces
"""
#print "_SearchNamespaces(%s, %s)" % (obj.name, name)
#print "dir(obj): ", dir(obj)
# Namespaces are stuffed under this variable if we have it we should
# be it.
if hasattr(obj, 'ns'):
obj = obj.ns
if hasattr(obj, name):
return getattr(obj, name)
elif hasattr(obj, attr) and hasattr(getattr(obj, attr), name):
return getattr(getattr(obj, attr), name)
for child in obj:
if child.elementType != 'namespace':
continue
ret = self._SearchNamespaces(child, name, attr)
if ret is not None:
return ret
return None
def onDataModelGetValue(self, action, dataModel):
"""
Called before getting a value from a data model
@type action: Action
@param action: Action we are starting
@type dataModel: Template
@param dataModel: Data model we are using
"""
if action.data is not None and action.data.multipleFiles \
and action.data.switchCount is not None:
self.switchCount = action.data.switchCount
if action.data is not None and action.data.multipleFiles \
and self.iterationCount % self.switchCount == 0:
self.context = action.getRoot()
# If a file fails to parse, don't exit the run, instead re-crack
# until we find a working file.
while True:
action.data.gotoRandomFile()
# Locate fresh copy of template with no data
obj = self.GetRef(action.template.ref)
cracker = DataCracker(obj.getRoot())
cracker.optmizeModelForCracking(obj)
template = obj.copy(action)
template.ref = action.template.ref
template.parent = action
template.name = action.template.name
# Switch any references to old name
oldName = template.ref
for relation in template._genRelationsInDataModelFromHere():
if relation.of == oldName:
relation.of = template.name
elif relation.From == oldName:
relation.From = template.name
# Crack file
try:
template.setDefaults(action.data, False, True)
break
except Exception as e:
logging.warning(e)
# Cache default values
action.template = template
template.getValue()
# Re-create state engine copy. We do this to avoid have
# optmizeModelForCracking called over and over.
if hasattr(action, "origionalTemplate"):
#delattr(action, "origionalTemplate")
action.origionalTemplate = action.template
action.origionalTemplate.BuildRelationCache()
action.origionalTemplate.resetDataModel()
action.origionalTemplate.getValue()
action.template = action.template.copy(action)
# Regenerate mutator state
self._isFirstTestCase = True
self._dataModels = {}
self._fieldMutators = {}
if self._isFirstTestCase:
fullName = dataModel.getFullname()
if fullName not in self._dataModels:
self._dataModels[fullName] = self._getNodeCount(dataModel)
nodes = dataModel.getAllChildDataElements()
nodes.append(dataModel)
nonMutableNodes = []
for node in nodes:
if not node.isMutable:
nonMutableNodes.append(node)
mutators = []
self._fieldMutators[node.getFullname()] = mutators
for m in Engine.context.mutators:
if m.supportedDataElement(node):
# Need to create new instance from class
for i in range(m.weight ** 4):
mutators.append(m(Engine.context, node))
for node in nonMutableNodes:
nodes.remove(node)
nonMutableNodes = None
return
else:
# Is this data model we are changing?
if dataModel.getFullname() != self._dataModelToChange:
return
# Select fields to modify
nodes = dataModel.getAllChildDataElements()
nodes.append(dataModel)
nodesToRemove = []
# Remove non-mutable fields
for node in nodes:
if not node.isMutable:
nodesToRemove.append(node)
for node in nodesToRemove:
nodes.remove(node)
#for node in nodes:
# if not self._fieldMutators.has_key(node.getFullname()) or len(self._fieldMutators[node.getFullname()]) == 0:
# raise Exception("Found element with no mutations!")
#logging.info("Preparing testcase for file {}".format(action.data.fileName))
# Select nodes we will modify
logging.info("Performing mutation.")
if len(nodes) <= self._n:
fields = nodes
maxN = self._n - len(fields)
if maxN <= 0:
maxN = self._n / 2
for _ in range(self._random.randint(1, maxN)):
# Now perform mutations on fields
if len(fields) < 3:
sampleset = fields
else:
sampleset = self._random.sample(
fields, self._random.randint(1, len(fields)))
for node in sampleset:
try:
mutator = self._random.choice(self._fieldMutators[node.getFullname()])
fullName = node.getFullnameInDataModel()[len(dataModel.name) + 1:]
logging.debug("%s => %s" % (mutator.name, fullName or "N/A"))
# Since we are applying multiple mutations sometimes a mutation will fail.
# We should ignore those failures.
try:
mutator.randomMutation(node, self._random)
except:
pass
except:
pass
else:
fields = self._random.sample(nodes, self._random.randint(1, self._n))
# Now perform mutations on fields
for node in fields:
try:
mutator = self._random.choice(self._fieldMutators[node.getFullname()])
fullName = node.getFullnameInDataModel()[len(dataModel.name) + 1:]
logging.debug("%s => %s" % (mutator.name, fullName or "N/A"))
# Since we are applying multiple mutations sometimes a
# mutation will fail. We should ignore those failures.
try:
mutator.randomMutation(node, self._random)
except:
pass
except:
pass
logging.info("Mutation finished.")
#MutationStrategy.DefaultStrategy = RandomMutationStrategy