Package Peach :: Package Mutators :: Module size
[hide private]

Source Code for Module Peach.Mutators.size

  1   
  2  ''' 
  3  Mutators that operate on the size relation. 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id$ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2008 Michael Eddington 
 11  # 
 12  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 13  # of this software and associated documentation files (the "Software"), to deal 
 14  # in the Software without restriction, including without limitation the rights  
 15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 16  # copies of the Software, and to permit persons to whom the Software is  
 17  # furnished to do so, subject to the following conditions: 
 18  # 
 19  # The above copyright notice and this permission notice shall be included in     
 20  # all copies or substantial portions of the Software. 
 21  # 
 22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 28  # SOFTWARE. 
 29  # 
 30   
 31  # Authors: 
 32  #   Michael Eddington (mike@phed.org) 
 33   
 34  # $Id$ 
 35   
 36  import sys, os, time 
 37  #from parser import * 
 38  from Peach.Generators.block import * 
 39  from Peach.Generators.data import * 
 40  from Peach.Generators.dictionary import * 
 41  from Peach.Generators.flipper import * 
 42  from Peach.Generators.static import Static, _StaticFromTemplate, _StaticCurrentValueFromDom 
 43  from Peach.Transformers.encode import WideChar 
 44  from Peach import Transformers 
 45  from Peach.Generators.block import * 
 46  from Peach.Generators.data import * 
 47  from Peach.Generators.dictionary import * 
 48  from Peach.Generators.flipper import * 
 49  from Peach.Generators.static import Static, _StaticFromTemplate 
 50  from Peach.Transformers.encode import WideChar 
 51  from Peach.mutator import * 
 52  from Peach.group import * 
 53  from Peach.Engine.common import * 
 54   
55 -class OfEdgeCasesMutator(Mutator):
56 ''' 57 Keep the size field same, but change "of" data length. 58 ''' 59
60 - def __init__(self, peach):
61 Mutator.__init__(self) 62 63 self.name = "OfEdgeCasesMutator" 64 self._peach = peach 65 66 self._stateMasterCount = -1 67 self._masterGroup = GroupSequence() 68 self._masterCount = 0 69 self._countThread = None 70 self._countGroup = GroupSequence() 71 self._actions = [] 72 73 # All active groups 74 self._activeGroups = [] 75 76 # Hashtable, key is element, value is [group, generator] 77 self._generatorMap = {} 78 self._countGeneratorMap = {}
79
80 - def isFinite(self):
81 ''' 82 Some mutators could contine forever, this 83 should indicate. 84 ''' 85 return True
86
87 - def reset(self):
88 ''' 89 Reset mutator 90 ''' 91 92 self._masterGroup = GroupSequence() 93 self._activeGroups = [] 94 self._generatorMap = {} 95 self._masterCount = 0 96 self._actions = []
97
98 - def next(self):
99 ''' 100 Goto next mutation. When this is called 101 the state machine is updated as needed. 102 ''' 103 104 try: 105 # Check if we set our state and need 106 # to skip ahead. We need todo this in 107 # next() to assure we have all our action 108 # templates added into our masterGroup 109 if self._stateMasterCount > -1: 110 for cnt in xrange(self._masterCount, self._stateMasterCount): 111 self._masterGroup.next() 112 self._masterCount += 1 113 self._stateMasterCount = -1 114 else: 115 self._masterGroup.next() 116 self._masterCount += 1 117 118 except GroupCompleted: 119 raise MutatorCompleted()
120
121 - def getState(self):
122 ''' 123 Return a binary string that contains 124 any information about current state of 125 Mutator. This state information should be 126 enough to let the same mutator "restart" 127 and continue when setState() is called. 128 ''' 129 130 # Ensure a minor overlap of testing 131 return str(self._masterCount - 2)
132
133 - def setState(self, state):
134 ''' 135 Set the state of this object. Should put us 136 back in the same place as when we said 137 "getState()". 138 ''' 139 self._stateMasterCount = int(state) 140 try: 141 self.next() 142 except: 143 pass
144
145 - def getCount(self):
146 if self._countThread != None and self._countThread.hasCountEvent.isSet(): 147 self._count = self._countThread.count 148 self._countThread = None 149 self._countGroup = None 150 self._countGeneratorMap = None 151 152 return self._count
153
154 - def calculateCount(self):
155 156 count = 0 157 try: 158 while True: 159 count += 1 160 self._countGroup.next() 161 162 except GroupCompleted: 163 pass 164 165 return count
166
167 - def _getStringElements(self, node):
168 169 elements = [] 170 171 for e in node._children: 172 if e.elementType == 'string' and not e.isStatic: 173 elements.append(e) 174 175 if e.hasChildren: 176 for ee in self._getStringElements(e): 177 elements.append(ee) 178 179 return elements
180 181 ##################################################### 182 # Callbacks when Action needs a value 183
184 - def getActionValue(self, action):
185 186 if action not in self._actions: 187 188 # Walk data tree and locate each string type. 189 stringElements = self._getStringElements(action.template) 190 self._generatorMap[action] = {} 191 self._countGeneratorMap[action] = {} 192 193 for e in stringElements: 194 group = Group() 195 gen = StringTokenFuzzer(group, e.getValue()) 196 self._masterGroup.append(group) 197 self._generatorMap[action][e.getFullnameInDataModel()] = gen 198 199 group = Group() 200 gen = StringTokenFuzzer(group, e.getValue()) 201 self._countGroup.append(group) 202 self._countGeneratorMap[action][e.getFullnameInDataModel()] = gen 203 204 self._actions.append(action) 205 206 # Set values 207 for key in self._generatorMap[action].keys(): 208 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 209 210 return action.template.getValue()
211
212 - def getActionParamValue(self, action):
213 return self.getActionValue(action)
214
215 - def getActionChangeStateValue(self, action, value):
216 return value
217 218 219 ##################################################### 220 # Event callbacks for state machine 221
222 - def onStateStart(self, state):
223 pass
224
225 - def onStateComplete(self, state):
226 pass
227
228 - def onActionStart(self, action):
229 pass
230
231 - def onActionComplete(self, action):
232 pass
233
234 - def onStateMachineStart(self, stateMachine):
235 pass
236
237 - def onStateMachineComplete(self, stateMachine):
238 239 # Lets calc our count if we haven't already 240 if self._count == -1 and self._countThread == None: 241 self._countThread = MutatorCountCalculator(self) 242 self._countThread.start() 243 244 elif self._countThread != None: 245 if self._countThread.hasCountEvent.isSet(): 246 self._count = self._countThread.count 247 self._countThread = None 248 self._countGroup = None 249 self._countGeneratorMap = None
250 251 252 # end 253