C0 code coverage information
Generated on Wed Jul 19 17:39:34 EDT 2006 with rcov 0.6.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Implements the randomness engine for RFuzz
2 require 'base64'
3 require 'fuzzrnd'
4
5 module RFuzz
6 class RandomGenerator
7
8 # Either initialized without a word dictionary or with one.
9 # If you initialize with a word dictionary then you can generate
10 # the random words, and is the expected behavior. You can use
11 # the "type" parameter to change the default type of generated
12 # content from words to any of the other types available
13 # [:base64,:uris,:byte_array,:ints,:floats].
14 #
15 # The dict should just be an array of words.
16 def initialize(dict=nil, type=:words)
17 # NOT meant to be secure so chill
18 @rnd = FuzzRnd.new("#{self.inspect}")
19 @dict = dict
20 @type = type
21 end
22
23 # Returns a random hash of type (default :words) where
24 # the key=>value is randomly generated. This is aliased
25 # for RandomGenerator.queries and RandomGenerator.headers
26 # so that it is more readable.
27 #
28 # [:words,:base64,:uris,:byte_array,:ints,:floats] are the available
29 # types of generated hash garbage you can use. These "types" just
30 # translate to function calls on self.send(type,length).
31 def hash_of(count,length=5,type=@type)
32 list = []
33 count.times do
34 list << Hash[*send(type,length*2)]
35 end
36
37 return list
38 end
39 alias :queries :hash_of
40 alias :headers :hash_of
41
42 # Generate an array of random length URIs based on words from the dict.
43 # The default max length=100 and is the number of words to chain together
44 # into a gigantor URI. The URI starts with /.
45 def uris(count,length=100)
46 ulist = []
47 count.times do
48 ulist << "/" + words(num(length)).join("/").tr("'","")
49 end
50 return ulist
51 end
52
53 # Generates an array with count number of randomly selected
54 # words from the dictionary.
55 def words(count=1)
56 raise "You need a dictionary." unless @dict
57 w = ints(count, @dict.length)
58 w.collect {|i| @dict[i]}
59 end
60
61
62 # Generates an array of base64 encoded chunks of garbage.
63 # The length=100 is the default and is a max, but the lengths
64 # are random.
65 def base64(count,length=100)
66 list = []
67 count.times {
68 list << Base64.encode64(@rnd.data(num(length)))
69 }
70 return list
71 end
72
73 # Generates an array of garbage byte strings, these are
74 # binary strings so very nasty. As usual, length=100
75 # is a max for the random lengths.
76 def byte_array(count,length=100)
77 list = []
78 count.times { list << @rnd.data(num(length)) }
79 return list
80 end
81
82 # Generate a single String with random binary garbage in it.
83 def bytes(count)
84 @rnd.data(count)
85 end
86
87 # A random number with a maximum of max.
88 def num(max)
89 ints(1, max)[0]
90 end
91
92 # An array of integers with a default max of max.
93 # The integers are 4 bytes and pulled from network
94 # encoding so they should be cross platform (meaning
95 # tests should run the same on all platforms).
96 def ints(count, max = nil)
97 i = @rnd.data(count * 4).unpack("N*")
98 if max
99 i = i.collect {|i| i % max }
100 end
101 return i
102 end
103
104 # An array for random floats.
105 def floats(count)
106 @rnd.data(count * 8).unpack("G*")
107 end
108 end
109
110 end
Generated using the rcov code coverage analysis tool for Ruby version 0.6.0.