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

Source Code for Module Peach.Generators.xmlstuff

  1   
  2  ''' 
  3  Currently contains W3C XML test suite 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: Peach.Generators.xmlstuff-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2006-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: Peach.Generators.xmlstuff-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 35   
 36  import re, struct, os 
 37  from Peach.generator import Generator 
 38  from Peach.Generators.dictionary  import * 
 39  from Peach.Generators.static  import * 
 40  import Peach.Generators.static 
 41   
42 -class XmlCreateElements(Generator):
43 ''' 44 This generator create XML elements N deep 45 ''' 46 47 _startingDepth = 1 48 _increment = 1 49 _nodePrefix = Static('PeachFuzz') 50 _nodePostfix = None 51 _elementAttributs = None 52 _currentDepth = 1 53 _maxDepth = 1000 54
55 - def __init__(self, group, startingDepth = None, increment = None, 56 maxDepth = None, nodePrefix = None, nodePostfix = None, 57 elementAttributes = None):
58 ''' 59 @type group: Group 60 @param group: Group to use 61 @type startingDepth: integer 62 @param startingDepth: How many deep to start at, default 1 63 @type increment: integer 64 @param increment: Incrementor, default 1 65 @type maxDepth: integer 66 @param maxDepth: Max depth, default 1000 67 @type nodePrefix: Generator 68 @param nodePrefix: Node prefix, default is Static('PeachFuzz') 69 @type nodePostfix: Generator 70 @param nodePostfix: Node postfix, default is None 71 @type elementAttributes: Generator 72 @param elementAttributes: Element attributes, default is None 73 ''' 74 self.setGroup(group) 75 if startingDepth != None: 76 self._startingDepth = startingDepth 77 if increment != None: 78 self._increment = increment 79 if nodePrefix != None: 80 self._nodePrefix = nodePrefix 81 if nodePostfix != None: 82 self._nodePostfix = nodePostfix 83 if elementAttributes != None: 84 self._elementAttributes = elementAttributes 85 if maxDepth != None: 86 self._maxDepth = maxDepth
87
88 - def next(self):
89 self._currentDepth += self._increment 90 if self._currentDepth > self._maxDepth: 91 raise generator.GeneratorCompleted("XmlCreateNodes")
92
93 - def getRawValue(self):
94 ret = '' 95 96 postFixs = [] 97 for i in range(self._currentDepth): 98 if self._nodePostfix != None: 99 postFixs[i] = self._nodePostfix.getValue() 100 if self._elementAttributes != None: 101 ret += "<%s%s %s>" % (self._nodePrefix.getValue(), postFixs[i], 102 self._elementAttributes.getValue()) 103 else: 104 ret += "<%s%s>" % (self._nodePrefix.getValue(), postFixs[i]) 105 else: 106 if self._elementAttributes != None: 107 ret += "<%s %s>" % (self._nodePrefix.getValue(), 108 self._elementAttributes.getValue()) 109 else: 110 ret += "<%s>" % self._nodePrefix.getValue() 111 112 for j in range(self._currentDepth): 113 if self._nodePostfix != None: 114 ret += "</%s%s>" % (self._nodePrefix.getValue(), postFixs[i-j]) 115 else: 116 ret += "</%s>" % self._nodePrefix.getValue() 117 118 return ret
119
120 - def reset(self):
121 self._currentDepth = 1
122
123 - def unittest():
124 expected = '<PeachFuzz1><PeachFuzz2><PeachFuzz3></PeachFuzz3></PeachFuzz2></PeachFuzz1>' 125 g = XmlCreateNodes(1, 1) 126 g.next() 127 g.next() 128 g.next() 129 if g.getRawValue() != expected: 130 print "FAILURE!!! XmlCreateNodes"
131 132 unittest = staticmethod(unittest)
133
134 -class XmlCreateNodes(Generator):
135 ''' 136 This generator create XML nodes N deep 137 ''' 138 139 _startingDepth = 1 140 _increment = 1 141 _nodePrefix = Static('PeachFuzz') 142 _currentDepth = 1 143 _maxDepth = 1000 144
145 - def __init__(self, group, startingDepth, increment, maxDepth, nodePrefix):
146 ''' 147 @type group: Group 148 @param group: Group to use 149 @type startingDepth: integer 150 @param startingDepth: How many deep to start at, default 1 151 @type increment: integer 152 @param increment: Incrementor, default 1 153 @type maxDepth: integer 154 @param maxDepth: Max depth, default 1000 155 @type nodePrefix: Generator 156 @param nodePrefix: Node prefix, default is Static('PeachFuzz') 157 ''' 158 self.setGroup(group) 159 if startingDepth != None: 160 self._startingDepth = startingDepth 161 if increment != None: 162 self._increment = increment 163 if nodePrefix != None: 164 self._nodePrefix = nodePrefix 165 if maxDepth != None: 166 self._maxDepth = maxDepth
167
168 - def next(self):
169 self._currentDepth += self._increment 170 if self._currentDepth > self._maxDepth: 171 raise generator.GeneratorCompleted("XmlCreateNodes")
172
173 - def getRawValue(self):
174 ret = '' 175 176 for i in range(self._currentDepth): 177 ret += "<%s%d>" % (self._nodePrefix.getValue(), i) 178 179 for j in range(self._currentDepth): 180 ret += "</%s%d>" % (self._nodePrefix.getValue(), i-j) 181 182 return ret
183
184 - def reset(self):
185 self._currentDepth = 1
186
187 - def unittest():
188 expected = '<PeachFuzz1><PeachFuzz2><PeachFuzz3></PeachFuzz3></PeachFuzz2></PeachFuzz1>' 189 g = XmlCreateNodes(1, 1) 190 g.next() 191 g.next() 192 g.next() 193 if g.getRawValue() != expected: 194 print "FAILURE!!! XmlCreateNodes"
195 196 unittest = staticmethod(unittest)
197
198 -class XmlParserTests(Generator):
199 ''' 200 W3C XML Validation Tests. This includes 201 all sets of tests, invalid, non-well formed, valid and error. 202 203 NOTE: Test files are in samples/xmltests.zip these are the 204 latest test cases from W3C as of 02/23/06 for XML. 205 ''' 206
207 - def __init__(self, group, testFiles = None):
208 ''' 209 @type group: Group 210 @param group: Group this Generator belongs to 211 @type testFiles: string 212 @param testFiles: Location of test files 213 ''' 214 Generator.__init__(self) 215 216 testFiles = os.path.join(Peach.Generators.static.__file__[:-11], "xmltests") 217 218 self._generatorList = GeneratorList(group, 219 [XmlParserTestsInvalid(None, testFiles), 220 XmlParserTestsNotWellFormed(None, testFiles), 221 XmlParserTestsValid(None, testFiles)])
222
223 - def getRawValue(self):
224 return self._generatorList.getRawValue()
225
226 - def next(self):
227 self._generatorList.next()
228
229 - def unittest():
230 pass
231 unittest = staticmethod(unittest)
232
233 -class XmlParserTestsGeneric(Generator):
234 ''' 235 Base class 236 ''' 237
238 - def __init__(self, group, testsFolder, testsFile):
239 ''' 240 @type group: Group 241 @param group: Group this Generator belongs to 242 @type testsFolder: string 243 @param testsFolder: Location of test files 244 @type testsFile: string 245 @param testsFile: File with listing of test files 246 ''' 247 Generator.__init__(self) 248 249 self._testsFolder = 'xmltests' 250 self._testsFile = 'invalid.txt' 251 self._currentValue = None 252 self._currentTestNum = 1 253 self._currentFilename = None 254 self._fdTests = None 255 self._fd = None 256 257 258 self.setGroup(group) 259 if testsFile != None: 260 self._testsFile = testsFile 261 if testsFolder != None: 262 self._testsFolder = testsFolder
263 264
265 - def next(self):
266 if self._fdTests == None: 267 self._fdTests = open(os.path.join(self._testsFolder, self._testsFile), 268 'rb') 269 270 self._currentFilename = os.path.join(self._testsFolder, 271 self._fdTests.readline()) 272 self._currentFilename = self._currentFilename.strip("\r\n") 273 if len(self._currentFilename) <= len(self._testsFolder)+2: 274 raise generator.GeneratorCompleted( 275 "Peach.Generators.xml.XmlParserTestsInvalid") 276 277 if self._fd == None: 278 self._fd = open(self._currentFilename, 'rb') 279 if self._fd == None: 280 raise Exception('Unable to open', self._currentFilename) 281 282 self._currentValue = self._fd.read() 283 self._fd = None
284
285 - def getRawValue(self):
286 if self._currentValue == None: 287 self.next() 288 289 return self._currentValue
290
291 - def reset(self):
292 self._fd = None 293 self._fdTests = None 294 self._currentValue = None
295
296 - def unittest():
297 pass
298 unittest = staticmethod(unittest)
299
300 -class XmlParserTestsInvalid(XmlParserTestsGeneric):
301 ''' 302 W3C XML Validation Tests, invalid set only. 303 304 NOTE: Test files are in samples/xmltests.zip these are the 305 latest test cases from W3C as of 02/23/06 for XML. 306 ''' 307
308 - def __init__(self, group, testsFolder):
309 ''' 310 @type group: Group 311 @param group: Group this Generator belongs to 312 @type testsFolder: string 313 @param testsFolder: Location of test files 314 ''' 315 XmlParserTestsGeneric.__init__(self, group, testsFolder, None) 316 self.setGroup(group) 317 self._testsFile = 'valid.txt' 318 if testsFolder != None: 319 self._testsFolder = testsFolder
320
321 -class XmlParserTestsValid(XmlParserTestsGeneric):
322 ''' 323 W3C XML Validation Tests, valid set only. 324 325 NOTE: Test files are in samples/xmltests.zip these are the 326 latest test cases from W3C as of 02/23/06 for XML. 327 ''' 328
329 - def __init__(self, group, testsFolder):
330 ''' 331 @type group: Group 332 @param group: Group this Generator belongs to 333 @type testsFolder: string 334 @param testsFolder: Location of test files 335 ''' 336 XmlParserTestsGeneric.__init__(self, group, testsFolder, None) 337 self.setGroup(group) 338 self._testsFile = 'valid.txt' 339 if testsFolder != None: 340 self._testsFolder = testsFolder
341
342 -class XmlParserTestsError(XmlParserTestsGeneric):
343 ''' 344 W3C XML Validation Tests, error set only. 345 346 NOTE: Test files are in samples/xmltests.zip these are the 347 latest test cases from W3C as of 02/23/06 for XML. 348 ''' 349
350 - def __init__(self, group, testsFolder):
351 ''' 352 @type group: Group 353 @param group: Group this Generator belongs to 354 @type testsFolder: string 355 @param testsFolder: Location of test files 356 ''' 357 XmlParserTestsGeneric.__init__(self, group, testsFolder, None) 358 self.setGroup(group) 359 self._testsFile = 'error.txt' 360 if testsFolder != None: 361 self._testsFolder = testsFolder
362
363 -class XmlParserTestsNotWellFormed(XmlParserTestsGeneric):
364 ''' 365 W3C XML Validation Tests, Invalid set only. 366 367 NOTE: Test files are in samples/xmltests.zip these are the 368 latest test cases from W3C as of 02/23/06 for XML. 369 ''' 370
371 - def __init__(self, group, testsFolder):
372 ''' 373 @type group: Group 374 @param group: Group this Generator belongs to 375 @type testsFolder: string 376 @param testsFolder: Location of test files 377 ''' 378 XmlParserTestsGeneric.__init__(self, group, testsFolder, None) 379 self.setGroup(group) 380 self._testsFile = 'nonwf.txt' 381 if testsFolder != None: 382 self._testsFolder = testsFolder
383 384 # end 385