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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
50 '''
51 Generate resource id (#....)
52 '''
53
55 SimpleGenerator.__init__(self, group)
56
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
75 Repeater(None, Block2([
76 Static('#'),
77 Repeater(None, Static("A"), 1, 1000)
78 ]), 1, 1000)
79 ])
80
81
83 '''
84 Generate resource id (#....)
85 '''
86
88 SimpleGenerator.__init__(self, group)
89 self._generator = Block2([
90 Static("#"),
91 GeneratorList(None, [
92 Static("Peach"),
93 _UriFragment(),
94 _UriFragment().setTransformer(UrlEncode()),
95 _UriFragment().setTransformer(UrlEncodePlus()),
96 _UriFragment().setTransformer(Utf16()),
97 _UriFragment().setTransformer(Utf16().setAnotherTransformer(UrlEncode())),
98 _UriFragment().setTransformer(UrlEncode().setAnotherTransformer(Utf16())),
99 Static("Peach")
100 ])
101 ])
102
104 '''
105 Known valid scheme/protocols.
106 '''
107
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
129 '''
130 Generate variouse uri scheme's.
131 '''
132
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
147 '''
148 Generate variouse location portions of URI's.
149 '''
150
151
152
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
240
241
243 '''
244 Generate variouse resource portions of URI's. This does
245 not include querystrings or id's (#xxx)
246 '''
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
300 '''
301 Helper class to create names for key/value pairs
302 '''
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
325 '''
326 Helper class to create names for key/value pairs.
327 '''
335
336
338 '''
339 Generate querystring's "?k=v&k=v&k=v"
340 '''
341
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
398
399
400
401
402
403
404
405
406
407
408
409
410
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
435
436 Block3(groupForeach, [
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
488
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
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
568