Package Peach :: Package Generators :: Module incrementor
[hide private]

Source Code for Module Peach.Generators.incrementor

  1   
  2  ''' 
  3  Incrementing generators (numerical, etc) 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: Peach.Generators.incrementor-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2005-2007 Michael Eddington 
 11  # Copyright (c) 2004-2005 IOActive Inc. 
 12  # 
 13  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 14  # of this software and associated documentation files (the "Software"), to deal 
 15  # in the Software without restriction, including without limitation the rights  
 16  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 17  # copies of the Software, and to permit persons to whom the Software is  
 18  # furnished to do so, subject to the following conditions: 
 19  # 
 20  # The above copyright notice and this permission notice shall be included in     
 21  # all copies or substantial portions of the Software. 
 22  # 
 23  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 24  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 25  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 26  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 27  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 28  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 29  # SOFTWARE. 
 30  # 
 31   
 32  # Authors: 
 33  #   Michael Eddington (mike@phed.org) 
 34   
 35  # $Id: Peach.Generators.incrementor-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 36   
 37  import struct 
 38  from types import * 
 39  from Peach import generator, group 
 40  from Peach.generator import * 
 41   
 42  #__all__ = ['Incrementor', 'PerCallIncrementor', 'PerRoundIncrementor'] 
 43   
44 -class Incrementor(generator.Generator):
45 ''' 46 Increment a value by another value each round. For example, 47 one could set 1 as an initial value with an incrementor of 1. 48 ''' 49 50 _roundCount = 0 51 _value = None 52 _incrementor = None 53 _currentValue = None 54 _formatString = None 55 _maxValue = None 56 _maxIterations = None 57 _packString = None 58
59 - def __init__(self, group = None, value = 1, incrementor = 1, formatString = None, 60 maxValue = None, maxIterations = None, packString = None):
61 ''' 62 @type group: Group 63 @param group: Group this generator works with 64 @type value: number 65 @param value: Number to increment 66 @type incrementor: number 67 @param incrementor: Increment amount (can be negative), default is 1 68 @type formatString: string 69 @param formatString: Format string for value (optional) 70 @type maxValue: number 71 @param maxValue: Maximum value (optional, default None) 72 @type maxIterations: number 73 @param maxIterations: Maximum number of times to increment 74 value (optional, default None) 75 @type packString: string 76 @param packString: Pack format string. Note that use of this 77 option will override formatString. (optional, default None) 78 ''' 79 Generator.__init__(self) 80 self._value = value 81 self._incrementor = incrementor 82 self._formatString = formatString 83 self._maxValue = maxValue 84 self._maxIterations = maxIterations 85 self._packString = packString 86 self.setGroup(group)
87
88 - def next(self):
89 self._roundCount += 1 90 91 if self._currentValue == None: 92 self._currentValue = self._value 93 else: 94 self._currentValue += self._incrementor 95 96 if self._maxValue: 97 if self._currentValue > self._maxValue: 98 raise generator.GeneratorCompleted('Generators.Incrementor: maxValue') 99 if self._maxIterations: 100 if self._roundCount > self._maxIterations: 101 raise generator.GeneratorCompleted('Generators.Incrementor: maxIterations')
102
103 - def reset(self):
104 self._roundCount = 0 105 self._currentValue = None
106
107 - def getRawValue(self):
108 if self._currentValue == None: 109 self._currentValue = self._value 110 111 ret = None 112 113 if self._packString != None: 114 ret = struct.pack(self._packString, self._currentValue) 115 else: 116 if self._formatString == None: 117 retType = type(self._currentValue) 118 if retType is IntType: 119 ret = "%d" % self._currentValue 120 elif retType is FloatType: 121 ret = "%f" % self._currentValue 122 elif retType is LongType: 123 ret = "%d" % self._currentValue 124 else: 125 ret = self._formatString % self._currentValue 126 127 return ret
128
129 - def setValue(self, value):
130 ''' 131 Set value to increment. 132 133 @type value: number 134 @param value: Number to increment 135 ''' 136 self._value = value
137
138 - def unittest():
139 g = group.GroupFixed(5) 140 inc = Incrementor(g, 1, 1) 141 142 try: 143 while g.next(): 144 print inc.getValue() 145 except group.GroupCompleted: 146 pass 147 148 g = group.GroupFixed(5) 149 inc = Incrementor(g, 1, 10, "<<%d>>") 150 151 try: 152 while g.next(): 153 print inc.getValue() 154 except group.GroupCompleted: 155 pass 156 157 g = group.GroupFixed(5) 158 inc = Incrementor(g, 1, 0.212673, "[[%0.2f]]") 159 160 try: 161 while g.next(): 162 print inc.getValue() 163 except group.GroupCompleted: 164 pass
165 unittest = staticmethod(unittest)
166 167
168 -class PerCallIncrementor(generator.Generator):
169 ''' 170 Each call to getValue will increment. Usefull to make a string 171 unique accross fuzz. 172 ''' 173 174 _incrementor = None 175
176 - def __init__(self, group = None, value = 1, incrementor = 1, formatString = None):
177 ''' 178 @type group: Group 179 @param group: Group this generator works with 180 @type value: number 181 @param value: Number to increment 182 @type incrementor: number 183 @param incrementor: Amount to increment 184 @type formatString: string 185 @param formatString: Format string for value (optional) 186 ''' 187 generator.Generator.__init__(self) 188 self._incrementor = Incrementor(group, value, incrementor, formatString)
189
190 - def next(self):
191 raise generator.GeneratorCompleted('PerCallIncrementor')
192
193 - def reset(self):
194 self._incrementor.reset()
195
196 - def getRawValue(self):
197 ret = self._incrementor.getRawValue() 198 self._incrementor.next() 199 return ret
200
201 - def setValue(self, value):
202 ''' 203 Set value to increment. 204 205 @type value: number 206 @param value: Number to increment 207 ''' 208 self._incrementor.setValue(value)
209
210 - def unittest():
211 g = group.GroupFixed(5) 212 inc = PerCallIncrementor(g, 1, 0.212673, "[[%0.2f]]") 213 214 try: 215 while g.next(): 216 print inc.getValue() 217 print inc.getValue() 218 print inc.getValue() 219 except group.GroupCompleted: 220 pass
221 unittest = staticmethod(unittest)
222
223 -class PerRoundIncrementor(generator.Generator):
224 ''' 225 Each round we increment. Has it's uses :) 226 ''' 227 228 _incrementor = None 229
230 - def __init__(self, value = 1, incrementor = 1, formatString = None):
231 ''' 232 @type value: number 233 @param value: Number to increment 234 @type incrementor: number 235 @param incrementor: Amount to increment 236 @type formatString: string 237 @param formatString: Format string for value (optional) 238 ''' 239 240 self._incrementor = Incrementor(None, value, incrementor, formatString)
241
242 - def next(self):
243 self._incrementor.next() 244 raise generator.GeneratorCompleted('PerCallIncrementor')
245
246 - def reset(self):
247 self._incrementor.reset()
248
249 - def getRawValue(self):
250 return self._incrementor.getRawValue()
251
252 - def setValue(self, value):
253 ''' 254 Set value to increment. 255 256 @type value: number 257 @param value: Number to increment 258 ''' 259 self._incrementor.setValue(value)
260
261 - def unittest():
262 g = group.GroupFixed(5) 263 inc = PerCallIncrementor(g, 1, 0.212673, "[[%0.2f]]") 264 265 try: 266 while g.next(): 267 print inc.getValue() 268 print inc.getValue() 269 print inc.getValue() 270 except group.GroupCompleted: 271 pass
272 unittest = staticmethod(unittest)
273 274 275 276 # end 277