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

Source Code for Module Peach.Mutators.string

  1   
  2  ''' 
  3  Mutators that operate on string types. 
  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.data import * 
 46  from Peach.Generators.static import _StaticFromTemplate 
 47  from Peach.Generators.xmlstuff import * 
 48  from Peach.mutator import * 
 49  from Peach.group import * 
 50  from Peach.Engine.common import * 
 51   
52 -class StringTokenMutator(Mutator):
53 ''' 54 Apply StringTokenFuzzer to each string node in DDT 55 one Node at a time. 56 ''' 57
58 - def __init__(self, peach):
59 Mutator.__init__(self) 60 61 self.name = "StringTokenMutator" 62 self._peach = peach 63 64 self._stateMasterCount = -1 65 self._masterGroup = GroupSequence() 66 self._masterCount = 0 67 self._countThread = None 68 self._countGroup = GroupSequence() 69 self._actions = [] 70 71 # All active groups 72 self._activeGroups = [] 73 74 # Hashtable, key is element, value is [group, generator] 75 self._generatorMap = {} 76 self._countGeneratorMap = {}
77
78 - def isFinite(self):
79 ''' 80 Some mutators could contine forever, this 81 should indicate. 82 ''' 83 return True
84
85 - def reset(self):
86 ''' 87 Reset mutator 88 ''' 89 90 self._masterGroup = GroupSequence() 91 self._activeGroups = [] 92 self._generatorMap = {} 93 self._masterCount = 0 94 self._actions = []
95
96 - def next(self):
97 ''' 98 Goto next mutation. When this is called 99 the state machine is updated as needed. 100 ''' 101 102 try: 103 # Check if we set our state and need 104 # to skip ahead. We need todo this in 105 # next() to assure we have all our action 106 # templates added into our masterGroup 107 if self._stateMasterCount > -1: 108 print "string: skipping from %d to %d" % (self._masterCount, self._stateMasterCount) 109 for cnt in xrange(self._masterCount, self._stateMasterCount): 110 self._masterGroup.next() 111 self._masterCount += 1 112 113 self._stateMasterCount = -1 114 115 else: 116 self._masterGroup.next() 117 self._masterCount += 1 118 119 except GroupCompleted: 120 raise MutatorCompleted()
121
122 - def getState(self):
123 ''' 124 Return a binary string that contains 125 any information about current state of 126 Mutator. This state information should be 127 enough to let the same mutator "restart" 128 and continue when setState() is called. 129 ''' 130 131 # Ensure a minor overlap of testing 132 return str(self._masterCount - 2)
133
134 - def setState(self, state):
135 ''' 136 Set the state of this object. Should put us 137 back in the same place as when we said 138 "getState()". 139 ''' 140 141 self._stateMasterCount = int(state) 142 try: 143 self.next() 144 except: 145 pass
146
147 - def getCount(self):
148 if self._countThread != None and self._countThread.hasCountEvent.isSet(): 149 self._count = self._countThread.count 150 self._countThread = None 151 self._countGroup = None 152 self._countGeneratorMap = None 153 154 return self._count
155
156 - def calculateCount(self):
157 158 count = 0 159 try: 160 while True: 161 count += 1 162 self._countGroup.next() 163 164 except GroupCompleted: 165 pass 166 167 return count
168
169 - def _getStringElements(self, node):
170 171 elements = [] 172 173 for e in node._children: 174 if e.elementType == 'string' and not e.isStatic: 175 elements.append(e) 176 177 if e.hasChildren: 178 for ee in self._getStringElements(e): 179 elements.append(ee) 180 181 return elements
182 183 ##################################################### 184 # Callbacks when Action needs a value 185
186 - def getActionValue(self, action):
187 188 if action not in self._actions: 189 190 # Walk data tree and locate each string type. 191 stringElements = self._getStringElements(action.template) 192 self._generatorMap[action] = {} 193 self._countGeneratorMap[action] = {} 194 195 for e in stringElements: 196 group = Group() 197 gen = StringTokenFuzzer(None, e.getValue()) 198 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 199 self._masterGroup.append(group) 200 self._generatorMap[action][e.getFullnameInDataModel()] = gen 201 202 group = Group() 203 gen = StringTokenFuzzer(None, e.getValue()) 204 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 205 self._countGroup.append(group) 206 self._countGeneratorMap[action][e.getFullnameInDataModel()] = gen 207 208 self._actions.append(action) 209 210 # Set values 211 for key in self._generatorMap[action].keys(): 212 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 213 214 return action.template.getValue()
215
216 - def getActionParamValue(self, action):
217 return self.getActionValue(action)
218
219 - def getActionChangeStateValue(self, action, value):
220 return value
221 222 223 ##################################################### 224 # Event callbacks for state machine 225
226 - def onStateStart(self, state):
227 pass
228
229 - def onStateComplete(self, state):
230 pass
231
232 - def onActionStart(self, action):
233 pass
234
235 - def onActionComplete(self, action):
236 pass
237
238 - def onStateMachineStart(self, stateMachine):
239 pass
240
241 - def onStateMachineComplete(self, stateMachine):
242 243 # Lets calc our count if we haven't already 244 if self._count == -1 and self._countThread == None: 245 self._countThread = MutatorCountCalculator(self) 246 self._countThread.start() 247 248 elif self._countThread != None: 249 if self._countThread.hasCountEvent.isSet(): 250 self._count = self._countThread.count 251 self._countThread = None 252 self._countGroup = None 253 self._countGeneratorMap = None
254 255
256 -class _SimpleHintMutator(Mutator):
257 ''' 258 Base class for simple hint mutators 259 ''' 260
261 - def __init__(self, peach):
262 Mutator.__init__(self) 263 264 self.name = "_SimpleHintMutator" 265 self._peach = peach 266 267 self._stateMasterCount = -1 268 self._masterGroup = GroupSequence() 269 self._masterCount = 0 270 self._countThread = None 271 self._countGroup = GroupSequence() 272 self._actions = [] 273 274 # All active groups 275 self._activeGroups = [] 276 277 # Hashtable, key is element, value is [group, generator] 278 self._generatorMap = {} 279 self._countGeneratorMap = {}
280
281 - def isFinite(self):
282 ''' 283 Some mutators could contine forever, this 284 should indicate. 285 ''' 286 return True
287
288 - def reset(self):
289 ''' 290 Reset mutator 291 ''' 292 293 self._masterGroup = GroupSequence() 294 self._activeGroups = [] 295 self._generatorMap = {} 296 self._masterCount = 0 297 self._actions = [] 298 self._countGeneratorMap = {}
299
300 - def next(self):
301 ''' 302 Goto next mutation. When this is called 303 the state machine is updated as needed. 304 ''' 305 306 try: 307 # Check if we set our state and need 308 # to skip ahead. We need todo this in 309 # next() to assure we have all our action 310 # templates added into our masterGroup 311 if self._stateMasterCount > -1: 312 for cnt in xrange(self._stateMasterCount): 313 self._masterGroup.next() 314 self._masterCount += 1 315 self._stateMasterCount = -1 316 else: 317 self._masterGroup.next() 318 self._masterCount += 1 319 320 except GroupCompleted: 321 raise MutatorCompleted()
322
323 - def getState(self):
324 ''' 325 Return a binary string that contains 326 any information about current state of 327 Mutator. This state information should be 328 enough to let the same mutator "restart" 329 and continue when setState() is called. 330 ''' 331 332 # Ensure a minor overlap of testing 333 return str(self._masterCount - 2)
334
335 - def setState(self, state):
336 ''' 337 Set the state of this object. Should put us 338 back in the same place as when we said 339 "getState()". 340 ''' 341 self.reset() 342 self._stateMasterCount = int(state)
343
344 - def calculateCount(self):
345 346 count = 0 347 try: 348 while True: 349 count += 1 350 self._countGroup.next() 351 352 except GroupCompleted: 353 pass 354 355 return count
356
357 - def _getStringElements(self, node):
358 359 # Only return strings with hings type=xml 360 361 elements = [] 362 363 for e in node._children: 364 if e.elementType == 'string' and not e.isStatic: 365 for child in e: 366 if isinstance(child, Hint) and child.name == 'type' and child.value == 'xml': 367 elements.append(e) 368 369 if e.hasChildren: 370 for ee in self._getStringElements(e): 371 elements.append(ee) 372 373 return elements
374 375 ##################################################### 376 # Callbacks when Action needs a value 377
378 - def getActionValue(self, action):
379 380 if action not in self._actions: 381 382 # Walk data tree and locate each string type. 383 stringElements = self._getStringElements(action.template) 384 self._generatorMap[action] = {} 385 self._countGeneratorMap[action] = {} 386 387 for e in stringElements: 388 group = Group() 389 gen = XmlParserTests() 390 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 391 self._masterGroup.append(group) 392 self._generatorMap[action][e.name] = gen 393 394 group = Group() 395 gen = XmlParserTests() 396 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 397 self._countGroup.append(group) 398 self._countGeneratorMap[action][e.name] = gen 399 400 self._actions.append(action) 401 402 # Set values 403 for key in self._generatorMap[action].keys(): 404 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 405 406 return action.template.getValue()
407
408 - def getActionParamValue(self, action):
409 return self.getActionValue(action)
410
411 - def getActionChangeStateValue(self, action, value):
412 return value
413 414 415 ##################################################### 416 # Event callbacks for state machine 417
418 - def onStateStart(self, state):
419 pass
420
421 - def onStateComplete(self, state):
422 pass
423
424 - def onActionStart(self, action):
425 pass
426
427 - def onActionComplete(self, action):
428 pass
429
430 - def onStateMachineStart(self, stateMachine):
431 pass
432
433 - def getCount(self):
434 if self._countThread != None and self._countThread.hasCountEvent.isSet(): 435 self._count = self._countThread.count 436 self._countThread = None 437 self._countGroup = None 438 self._countGeneratorMap = None 439 440 return self._count
441
442 - def onStateMachineComplete(self, stateMachine):
443 444 # Lets calc our count if we haven't already 445 if self._count == -1 and self._countThread == None: 446 self._countThread = MutatorCountCalculator(self) 447 self._countThread.start() 448 449 elif self._countThread != None: 450 if self._countThread.hasCountEvent.isSet(): 451 self._count = self._countThread.count 452 self._countThread = None 453 self._countGroup = None 454 self._countGeneratorMap = None
455 456
457 -class XmlW3CMutator(_SimpleHintMutator):
458 ''' 459 Performs the W3C parser tests. Only works on 460 <String> elements with a <Hint name="type" value="xml"> 461 ''' 462
463 - def __init__(self, peach):
464 _SimpleHintMutator.__init__(self, peach) 465 self.name = "XmlW3CMutator"
466
467 - def _getStringElements(self, node):
468 469 # Only return strings with hings type=xml 470 471 elements = [] 472 473 for e in node._children: 474 if e.elementType == 'string' and not e.isStatic: 475 for child in e: 476 if isinstance(child, Hint) and child.name == 'type' and child.value == 'xml': 477 elements.append(e) 478 479 if e.hasChildren: 480 for ee in self._getStringElements(e): 481 elements.append(ee) 482 483 return elements
484 485 ##################################################### 486 # Callbacks when Action needs a value 487
488 - def getActionValue(self, action):
489 490 if action not in self._actions: 491 492 # Walk data tree and locate each string type. 493 stringElements = self._getStringElements(action.template) 494 self._generatorMap[action] = {} 495 self._countGeneratorMap[action] = {} 496 497 for e in stringElements: 498 group = Group() 499 gen = XmlParserTests(None) 500 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 501 self._masterGroup.append(group) 502 self._generatorMap[action][e.name] = gen 503 504 group = Group() 505 gen = XmlParserTests(None) 506 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 507 self._countGroup.append(group) 508 self._countGeneratorMap[action][e.name] = gen 509 510 self._actions.append(action) 511 512 # Set values 513 for key in self._generatorMap[action].keys(): 514 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 515 516 return action.template.getValue()
517 518
519 -class PathMutator(_SimpleHintMutator):
520 ''' 521 Perform path mutatons. Only works on 522 <String> elements with a <Hint name="type" value="path"> 523 ''' 524
525 - def __init__(self, peach):
526 _SimpleHintMutator.__init__(self, peach) 527 self.name = "PathMutator"
528
529 - def _getStringElements(self, node):
530 531 # Only return strings with hings type=xml 532 533 elements = [] 534 535 for e in node._children: 536 if e.elementType == 'string' and not e.isStatic: 537 for child in e: 538 if isinstance(child, Hint) and child.name == 'type' and child.value == 'path': 539 elements.append(e) 540 541 if e.hasChildren: 542 for ee in self._getStringElements(e): 543 elements.append(ee) 544 545 return elements
546
547 - def getActionValue(self, action):
548 549 if action not in self._actions: 550 551 # Walk data tree and locate each string type. 552 stringElements = self._getStringElements(action.template) 553 self._generatorMap[action] = {} 554 self._countGeneratorMap[action] = {} 555 556 for e in stringElements: 557 group = Group() 558 gen = BadPath() 559 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 560 self._masterGroup.append(group) 561 self._generatorMap[action][e.name] = gen 562 563 group = Group() 564 gen = BadPath() 565 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 566 self._countGroup.append(group) 567 self._countGeneratorMap[action][e.name] = gen 568 569 self._actions.append(action) 570 571 # Set values 572 for key in self._generatorMap[action].keys(): 573 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 574 575 return action.template.getValue()
576 577
578 -class HostnameMutator(_SimpleHintMutator):
579 ''' 580 Perform hostname mutators. Only works on 581 <String> elements with a <Hint name="type" value="hostname"> 582 ''' 583
584 - def __init__(self, peach):
585 _SimpleHintMutator.__init__(self, peach) 586 self.name = "HostnameMutator" 587 self._peach = peach
588
589 - def _getStringElements(self, node):
590 591 # Only return strings with hings type=xml 592 593 elements = [] 594 595 for e in node._children: 596 if e.elementType == 'string' and not e.isStatic: 597 for child in e: 598 if isinstance(child, Hint) and child.name == 'type' and child.value == 'hostname': 599 elements.append(e) 600 601 if e.hasChildren: 602 for ee in self._getStringElements(e): 603 elements.append(ee) 604 605 return elements
606
607 - def getActionValue(self, action):
608 609 if action not in self._actions: 610 611 # Walk data tree and locate each string type. 612 stringElements = self._getStringElements(action.template) 613 self._generatorMap[action] = {} 614 self._countGeneratorMap[action] = {} 615 616 for e in stringElements: 617 group = Group() 618 gen = BadHostname() 619 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 620 self._masterGroup.append(group) 621 self._generatorMap[action][e.name] = gen 622 623 group = Group() 624 gen = BadHostname() 625 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 626 self._countGroup.append(group) 627 self._countGeneratorMap[action][e.name] = gen 628 629 self._actions.append(action) 630 631 # Set values 632 for key in self._generatorMap[action].keys(): 633 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 634 635 return action.template.getValue()
636 637
638 -class FilenameMutator(_SimpleHintMutator):
639 ''' 640 Perform hostname mutators. Only works on 641 <String> elements with a <Hint name="type" value="filename"> 642 ''' 643
644 - def __init__(self, peach):
645 _SimpleHintMutator.__init__(self, peach) 646 self.name = "FilenameMutator" 647 self._peach = peach
648
649 - def _getStringElements(self, node):
650 651 # Only return strings with hings type=xml 652 653 elements = [] 654 655 for e in node._children: 656 if e.elementType == 'string' and not e.isStatic: 657 for child in e: 658 if isinstance(child, Hint) and child.name == 'type' and child.value == 'filename': 659 elements.append(e) 660 661 if e.hasChildren: 662 for ee in self._getStringElements(e): 663 elements.append(ee) 664 665 return elements
666
667 - def getActionValue(self, action):
668 669 if action not in self._actions: 670 671 # Walk data tree and locate each string type. 672 stringElements = self._getStringElements(action.template) 673 self._generatorMap[action] = {} 674 self._countGeneratorMap[action] = {} 675 676 for e in stringElements: 677 group = Group() 678 gen = BadFilename() 679 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 680 self._masterGroup.append(group) 681 self._generatorMap[action][e.name] = gen 682 683 group = Group() 684 gen = BadFilename() 685 gen = WithDefault(group, _StaticFromTemplate(action, e), gen) 686 self._countGroup.append(group) 687 self._countGeneratorMap[action][e.name] = gen 688 689 self._actions.append(action) 690 691 # Set values 692 for key in self._generatorMap[action].keys(): 693 self._getElementByName(action.template, key).setValue(self._generatorMap[action][key].getValue()) 694 695 return action.template.getValue()
696 697 # end 698