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

Source Code for Module Peach.Generators.uri

  1   
  2  ''' 
  3  [BETA] Generators for parts of URLs.  These are BETA quality. 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: Peach.Generators.uri-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2006-2007 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.uri-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 35   
 36  import sys, static 
 37   
 38  from Peach.generator import Generator 
 39  from Peach.group import * 
 40  from Peach.Transformers import * 
 41  from Peach.Transformers import * 
 42  from Peach.Generators.dictionary import * 
 43  from Peach.Generators.data import * 
 44  from Peach.Transformers.encode import * 
 45  from Peach.Generators.incrementor import * 
 46  from Peach.Generators.unicode import * 
 47   
 48   
49 -class _UriFragment(SimpleGenerator):
50 ''' 51 Generate resource id (#....) 52 ''' 53
54 - def __init__(self, group = None):
55 SimpleGenerator.__init__(self, group) 56 # fragment = *( pchar / "/" / "?" ) 57 self._generator = GeneratorList(None, [ 58 BadString(), 59 GoodUnicode(None, BadString()), 60 BadUnicode(None, BadString()), 61 62 BadIpAddress(), 63 BadHostname(), 64 BadNumbers(), 65 66 BadPath(), 67 GoodUnicode(None, BadPath()), 68 BadUnicode(None, BadPath()), 69 70 BadFilename(), 71 GoodUnicode(None, BadFilename()), 72 BadUnicode(None, BadFilename()), 73 74 #Repeater(None, Static('A'), 1, 100000), 75 Repeater(None, Block2([ 76 Static('#'), 77 Repeater(None, Static("A"), 1, 1000) 78 ]), 1, 1000) 79 ])
80 81
82 -class UriFragment(SimpleGenerator):
83 ''' 84 Generate resource id (#....) 85 ''' 86
87 - def __init__(self, group = None):
102
103 -class UriSchemeKnown(SimpleGenerator):
104 ''' 105 Known valid scheme/protocols. 106 ''' 107
108 - def __init__(self, group = None):
109 SimpleGenerator.__init__(self, group) 110 self._generator = List(None, [ 111 'about', 'chrome', 112 'data', 'default', 113 'default-blocked', 114 'feed', 'file', 115 'ftp', 'gopher', 116 'http', 'https', 117 'jar', 'keyword', 118 'moz-icon', 'pcast', 119 'resource', 'view-source', 120 'wyciwyg', 'mailto', 121 'telnet', 'ldap', 122 'disk', 'disks', 123 'news', 'urn', 124 'tel', 'javascript', 125 'jscript', 'vbscript' 126 ])
127
128 -class UriScheme(SimpleGenerator):
129 ''' 130 Generate variouse uri scheme's. 131 ''' 132
133 - def __init__(self, group = None):
134 SimpleGenerator.__init__(self, group) 135 self._generator = GeneratorList(None, [ 136 Static('http'), 137 UriSchemeKnown(), 138 BadString(), 139 GoodUnicode(None, BadString()), 140 BadUnicode(None, BadString()), 141 RepeaterGI(None, Static('A'), BadUnsignedNumbers16()), 142 Static('http') 143 ] )
144 145
146 -class UriAuthority(SimpleGenerator):
147 ''' 148 Generate variouse location portions of URI's. 149 ''' 150 151 # authority = [ userinfo "@" ] host [ ":" port ] 152
153 - def __init__(self, group = None):
154 SimpleGenerator.__init__(self, group) 155 self._generator = GeneratorList(None, [ 156 Static('localhost'), 157 UriHost(), 158 Block2([ 159 UriUserinfo(), 160 Static('@localhost') 161 ]), 162 Block2([ 163 Static('localhost:'), 164 UriPort() 165 ]), 166 RepeaterGI(None, Static('A'), BadUnsignedNumbers16()), 167 Static('localhost'), 168 ] )
169 170
171 -class UriUserinfo(SimpleGenerator):
172 - def __init__(self, group = None):
173 SimpleGenerator.__init__(self, group) 174 self._generator = GeneratorList(None, [ 175 Static('Peach:Ing'), 176 BadString(), 177 Block2([ 178 BadString(), 179 Static(':'), 180 BadString() 181 ]), 182 RepeaterGI(None, Static('A'), BadUnsignedNumbers16()), 183 Static('Peach:Ing'), 184 ])
185 186
187 -class UriHost(SimpleGenerator):
188 # host = IP-literal / IPv4address / reg-name 189 # IP-literal = "[" ( IPv6address / IPvFuture ) "]" 190 # IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) 191 # 192 # IPv6address = 6( h16 ":" ) ls32 193 # / "::" 5( h16 ":" ) ls32 194 # / [ h16 ] "::" 4( h16 ":" ) ls32 195 # / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 196 # / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 197 # / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32 198 # / [ *4( h16 ":" ) h16 ] "::" ls32 199 # / [ *5( h16 ":" ) h16 ] "::" h16 200 # / [ *6( h16 ":" ) h16 ] "::" 201 # 202 # ls32 = ( h16 ":" h16 ) / IPv4address 203 # ; least-significant 32 bits of address 204 # 205 # h16 = 1*4HEXDIG 206 # ; 16 bits of address represented in hexadecimal 207 # IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet 208 # 209 # dec-octet = DIGIT ; 0-9 210 # / %x31-39 DIGIT ; 10-99 211 # / "1" 2DIGIT ; 100-199 212 # / "2" %x30-34 DIGIT ; 200-249 213 # / "25" %x30-35 ; 250-255 214 # reg-name = *( unreserved / pct-encoded / sub-delims )
215 - def __init__(self, group = None):
216 SimpleGenerator.__init__(self, group) 217 self._generator = GeneratorList(None, [ 218 Static('localhost'), 219 BadString(), 220 BadIpAddress(), 221 BadHostname(), 222 GoodUnicode(None, BadIpAddress()), 223 BadUnicode(None, BadHostname()), 224 RepeaterGI(None, Static('A'), BadUnsignedNumbers16()), 225 Static('localhost') 226 ])
227 228
229 -class UriPort(SimpleGenerator):
230 # port = *DIGIT
231 - def __init__(self, group = None):
232 SimpleGenerator.__init__(self, group) 233 self._generator = GeneratorList(None, [ 234 Static('81'), 235 BadString(), 236 RepeaterGI(None, Static('A'), BadUnsignedNumbers16()), 237 BadNumbers(), 238 Static('81') 239 ])
240 241
242 -class UriPath(SimpleGenerator):
243 ''' 244 Generate variouse resource portions of URI's. This does 245 not include querystrings or id's (#xxx) 246 ''' 247 248 #path = path-abempty ; begins with "/" or is empty 249 # / path-absolute ; begins with "/" but not "//" 250 # / path-noscheme ; begins with a non-colon segment 251 # / path-rootless ; begins with a segment 252 # / path-empty ; zero characters 253 # 254 #path-abempty = *( "/" segment ) 255 #path-absolute = "/" [ segment-nz *( "/" segment ) ] 256 #path-noscheme = segment-nz-nc *( "/" segment ) 257 #path-rootless = segment-nz *( "/" segment ) 258 #path-empty = 0<pchar> 259 # 260 #segment = *pchar 261 #segment-nz = 1*pchar 262 #segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) 263 # ; non-zero-length segment without any colon ":" 264 # 265 #pchar = unreserved / pct-encoded / sub-delims / ":" / "@" 266
267 - def __init__(self, group = None):
268 SimpleGenerator.__init__(self, group) 269 self._generator = Block2([ 270 Static('/'), 271 GeneratorList(None, [ 272 Static('AAAAA'), 273 BadString(), 274 GoodUnicode(None, BadString()), 275 BadUnicode(None, BadString()), 276 277 BadIpAddress(), 278 BadHostname(), 279 BadNumbers(), 280 281 BadPath(), 282 GoodUnicode(None, BadPath()), 283 BadUnicode(None, BadPath()), 284 285 BadFilename(), 286 GoodUnicode(None, BadFilename()), 287 BadUnicode(None, BadFilename()), 288 289 Repeater(None, Static('AAAA/'), 10, 100), 290 Repeater(None, Static('AAAA/'), 100, 100), 291 Repeater(None, Static('AAAA/../'), 20, 100), 292 Repeater(None, Static('../AAAA/'), 20, 100), 293 RepeaterGI(None, Static('A/'), BadUnsignedNumbers16()), 294 Static('AAAAA') 295 ] ) 296 ])
297 298
299 -class _UriQuery_GenName(SimpleGenerator):
300 ''' 301 Helper class to create names for key/value pairs 302 '''
303 - def __init__(self, group = None):
304 SimpleGenerator.__init__(self, group) 305 self._generator = GeneratorList(None, [ 306 BadString(), 307 GoodUnicode(None, BadString()), 308 BadUnicode(None, BadString()), 309 310 BadIpAddress(), 311 BadHostname(), 312 BadNumbers(), 313 314 BadPath(), 315 GoodUnicode(None, BadPath()), 316 BadUnicode(None, BadPath()), 317 318 BadFilename(), 319 GoodUnicode(None, BadFilename()), 320 BadUnicode(None, BadFilename()) 321 ], "_UriQuery_GenName")
322 323
324 -class _UriQuery_GenNameAll(SimpleGenerator):
325 ''' 326 Helper class to create names for key/value pairs. 327 '''
328 - def __init__(self, group = None):
329 SimpleGenerator.__init__(self, group) 330 self._generator = GeneratorList(None, [ 331 _UriQuery_GenName(), 332 _UriQuery_GenName().setTransformer( UrlEncode() ), 333 _UriQuery_GenName().setTransformer( BadUrlEncode() ) 334 ], "_UriQuery_GenNameAll")
335 336
337 -class UriQuery(SimpleGenerator):
338 ''' 339 Generate querystring's "?k=v&k=v&k=v" 340 ''' 341
342 - def __init__(self, group = None):
343 SimpleGenerator.__init__(self, group) 344 self._generator = Block2([ 345 GeneratorList(None, [ 346 Static("?"), 347 GoodUnicode(None, Static("?")), 348 BadUnicode(None, Static("?")), 349 Static("") 350 ], "_generator_question-mark"), 351 352 GeneratorList(None, [ 353 _UriQuery_GenNameAll(), 354 355 GeneratorList(None, [ 356 Block2([ 357 _UriQuery_GenNameAll(), 358 Static('=') 359 ]), 360 Block2([ 361 Static('='), 362 _UriQuery_GenNameAll() 363 ]), 364 Block2([ 365 _UriQuery_GenNameAll(), 366 Static('='), 367 _UriQuery_GenNameAll() 368 ]) 369 ],"_generator_1"), 370 371 Repeater(None, Block2([ 372 Static('KEY'), 373 PerCallIncrementor(None, 1, 1), 374 Static('='), 375 Static('VALUE'), 376 PerCallIncrementor(None, 1, 1), 377 Static('&') 378 ]), 10, 1000 ), 379 380 Repeater(None, Block2([ 381 Repeater(None, Static('KKKKKKKKK'), 10, 1000), 382 PerCallIncrementor(None, 1, 1), 383 Static('='), 384 Repeater(None, Static('VVVVVVVVV'), 10, 1000), 385 PerCallIncrementor(None, 1, 1), 386 Static('&') 387 ]), 10, 1000 ) 388 ], "_generator_2") 389 ])
390 391
392 -class Uri(SimpleGenerator):
393 ''' 394 Generate a gazillion URI's!! 395 ''' 396 397 # foo://example.com:8042/over/there?name=ferret#nose 398 # \_/ \______________/\_________/ \_________/ \__/ 399 # | | | | | 400 # scheme authority path query fragment 401 402 # formats 403 # prot://location/resource/ 404 # prot://user:pass@location/resource 405 # prot://location/reqource#stuff 406 # prot://location/resource?key=value&key=value 407 # mailto:dd@phed.org 408 # tel:xxxx 409 # urn:oasis:names:blah:blah: 410
411 - def __init__(self, group = None):
412 SimpleGenerator.__init__(self, group) 413 414 groupAA = Group() 415 groupBB = GroupSequence([Group(), Group()], "UriGroupBB") 416 groupCC = GroupSequence([Group(), Group(), Group()], "UriGroupCC") 417 groupDD = GroupSequence([Group(), Group(), Group(), Group()], "UriGroupDD") 418 groupEE = GroupSequence([Group(), Group(), Group()], "UriGroupED") 419 groupFF = GroupSequence([Group(), Group()], "UriGroupFF") 420 421 groupA = GroupSequence([Group(), Group()], "UriGroupA") 422 groupB = GroupSequence([Group(), Group(), Group()], "UriGroupB") 423 groupC = GroupSequence([Group(), Group(), Group(), Group()], "UriGroupC") 424 groupD = GroupSequence([Group(), Group(), Group(), Group(), Group()], "UriGroupD") 425 groupE = GroupSequence([Group(), Group(), Group(), Group()], "UriGroupE") 426 groupF = GroupSequence([Group(), Group(), Group()], "UriGroupF") 427 428 groupEach = Group() 429 groupDo = Group() 430 groupForeach = GroupForeachDo(groupEach, groupDo) 431 432 self._generator = GeneratorList(None, [ 433 434 # For each known Scheme do some basic fuzzing of each 435 # uri component 436 Block3(groupForeach, [ # Note, groupForeach is incremented by Block3 437 UriSchemeKnown(groupEach), 438 GeneratorList2(groupDo, [ 439 groupAA, 440 groupBB, 441 groupCC, 442 groupDD, 443 groupEE, 444 groupFF 445 ], [ 446 Block([ 447 Static('://'), 448 UriAuthority(groupAA), 449 ]), 450 Block([ 451 Static('://'), 452 UriAuthority(groupBB[0]), 453 UriPath(groupBB[1]), 454 ]), 455 Block([ 456 Static('://'), 457 UriAuthority(groupCC[0]), 458 UriPath(groupCC[1]), 459 UriQuery(groupCC[2]), 460 ]), 461 Block([ 462 Static('://'), 463 UriAuthority(groupDD[0]), 464 UriPath(groupDD[1]), 465 UriQuery(groupDD[2]), 466 UriFragment(groupDD[3]) 467 ]), 468 Block([ 469 Static('://'), 470 UriAuthority(groupEE[0]), 471 UriPath(groupEE[1]), 472 UriFragment(groupEE[2]) 473 ]), 474 Block([ 475 Static('://'), 476 UriAuthority(groupFF[0]), 477 UriFragment(groupFF[1]) 478 ]) 479 ]) 480 ]), 481 482 Block2([ 483 UriScheme(), 484 Static('://localhost') 485 ]), 486 487 # Do some scheme fuzzing with other portions fuzzing 488 # not sure how usefull this is? 489 GeneratorList2(None, [ 490 groupA, 491 groupB, 492 groupC, 493 groupD, 494 groupE, 495 groupF 496 ], [ 497 Block([ 498 UriScheme(groupA[0]), 499 Static('://'), 500 UriAuthority(groupA[1]), 501 ]), 502 Block([ 503 UriScheme(groupB[0]), 504 Static('://'), 505 UriAuthority(groupB[1]), 506 UriPath(groupB[2]), 507 ]), 508 Block([ 509 UriScheme(groupC[0]), 510 Static('://'), 511 UriAuthority(groupC[1]), 512 UriPath(groupC[2]), 513 UriQuery(groupC[3]), 514 ]), 515 Block([ 516 UriScheme(groupD[0]), 517 Static('://'), 518 UriAuthority(groupD[1]), 519 UriPath(groupD[2]), 520 UriQuery(groupD[3]), 521 UriFragment(groupD[4]) 522 ]), 523 Block([ 524 UriScheme(groupE[0]), 525 Static('://'), 526 UriAuthority(groupE[1]), 527 UriPath(groupE[2]), 528 UriFragment(groupE[3]) 529 ]), 530 Block([ 531 UriScheme(groupF[0]), 532 Static('://'), 533 UriAuthority(groupF[1]), 534 UriFragment(groupF[2]) 535 ]) 536 ]) 537 ])
538 539 540 # ############################################################################ 541 542 import inspect, pyclbr 543
544 -def RunUnit(obj, clsName):
545 print "Unittests for: %s..." % clsName, 546 cnt = 0 547 try: 548 while True: 549 s = obj.getValue() 550 obj.next() 551 cnt+=1 552 553 except GeneratorCompleted: 554 print "%d tests found." % cnt
555 556 if __name__ == "__main__": 557 print "\n -- Running A Quick Unittest for %s --\n" % __file__ 558 mod = inspect.getmodulename(__file__) 559 for clsName in pyclbr.readmodule(mod): 560 cls = globals()[clsName] 561 if str(cls).find('__main__') > -1 and hasattr(cls, 'next') and hasattr(cls, 'getValue'): 562 try: 563 RunUnit(cls(), clsName) 564 except TypeError: 565 pass 566 567 # end 568