Class: RFuzz::RandomGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rfuzz/random.rb

Instance Method Summary collapse

Constructor Details

#initialize(dict = nil, type = :words) ⇒ RandomGenerator

Either initialized without a word dictionary or with one. If you initialize with a word dictionary then you can generate the random words, and is the expected behavior. You can use the “type” parameter to change the default type of generated content from words to any of the other types available [:base64,:uris,:byte_array,:ints,:floats].

The dict should just be an array of words.



16
17
18
19
20
21
# File 'lib/rfuzz/random.rb', line 16

def initialize(dict=nil, type=:words)
  # NOT meant to be secure so chill
  @rnd = FuzzRnd.new("#{self.inspect}")
  @dict = dict
  @type = type
end

Instance Method Details

#base64(count, length = 100) ⇒ Object

Generates an array of base64 encoded chunks of garbage. The length=100 is the default and is a max, but the lengths are random.



65
66
67
68
69
70
71
# File 'lib/rfuzz/random.rb', line 65

def base64(count,length=100)
  list = []
  count.times { 
    list << Base64.encode64(@rnd.data(num(length)))
  }
  return list
end

#byte_array(count, length = 100) ⇒ Object

Generates an array of garbage byte strings, these are binary strings so very nasty. As usual, length=100 is a max for the random lengths.



76
77
78
79
80
# File 'lib/rfuzz/random.rb', line 76

def byte_array(count,length=100)
  list = []
  count.times { list << @rnd.data(num(length)) }
  return list
end

#bytes(count) ⇒ Object

Generate a single String with random binary garbage in it.



83
84
85
# File 'lib/rfuzz/random.rb', line 83

def bytes(count)
  @rnd.data(count)
end

#floats(count) ⇒ Object

An array for random floats.



105
106
107
# File 'lib/rfuzz/random.rb', line 105

def floats(count)
  @rnd.data(count * 8).unpack("G*")
end

#hash_of(count, length = 5, type = @type) ⇒ Object Also known as: queries, headers

Returns a random hash of type (default :words) where the key=>value is randomly generated. This is aliased for RandomGenerator.queries and RandomGenerator.headers so that it is more readable.

:words,:base64,:uris,:byte_array,:ints,:floats

are the available

types of generated hash garbage you can use. These “types” just translate to function calls on self.send(type,length).



31
32
33
34
35
36
37
38
# File 'lib/rfuzz/random.rb', line 31

def hash_of(count,length=5,type=@type)
  list = []
  count.times do
    list << Hash[*send(type,length*2)]
  end

  return list
end

#ints(count, max = nil) ⇒ Object

An array of integers with a default max of max. The integers are 4 bytes and pulled from network encoding so they should be cross platform (meaning tests should run the same on all platforms).



96
97
98
99
100
101
102
# File 'lib/rfuzz/random.rb', line 96

def ints(count, max = nil)
  i = @rnd.data(count * 4).unpack("N*")
  if max
    i = i.collect {|i| i % max }
  end
  return i
end

#num(max) ⇒ Object

A random number with a maximum of max.



88
89
90
# File 'lib/rfuzz/random.rb', line 88

def num(max)
  ints(1, max)[0]
end

#uris(count, length = 100) ⇒ Object

Generate an array of random length URIs based on words from the dict. The default max length=100 and is the number of words to chain together into a gigantor URI. The URI starts with /.



45
46
47
48
49
50
51
# File 'lib/rfuzz/random.rb', line 45

def uris(count,length=100)
  ulist = []
  count.times do
    ulist << "/" + words(num(length)).join("/").tr("'","")
  end
  return ulist
end

#words(count = 1) ⇒ Object

Generates an array with count number of randomly selected words from the dictionary.



55
56
57
58
59
# File 'lib/rfuzz/random.rb', line 55

def words(count=1)
  raise "You need a dictionary." unless @dict
  w = ints(count, @dict.length)
  w.collect {|i| @dict[i]}
end