summaryrefslogtreecommitdiff
path: root/tools/fuzz.rb
blob: 82619301915b63d14c98ddf1f9df0d27c40f2187 (plain)
1
2
3
4
5
6
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'json'

class Fuzzer
  def initialize(n, freqs = {})
    sum = freqs.inject(0.0) { |s, x| s + x.last }
    freqs.each_key { |x| freqs[x] /= sum }
    s = 0.0
    freqs.each_key do |x|
      freqs[x] = s .. (s + t = freqs[x])
      s += t
    end
    @freqs = freqs
    @n = n
    @alpha = (0..0xff).to_a
  end

  def random_string
    s = ''
    30.times { s << @alpha[rand(@alpha.size)] }
    s
  end

  def pick
    r = rand
    found = @freqs.find { |k, f| f.include? rand }
    found && found.first
  end

  def make_pick
    k = pick
    case
    when k == Hash, k == Array
      k.new
    when k == true, k == false, k == nil
      k
    when k == String
      random_string
    when k == Fixnum
      rand(2 ** 30) - 2 ** 29
    when k == Bignum
      rand(2 ** 70) - 2 ** 69
    end
  end

  def fuzz(current = nil)
    if @n > 0
      case current
      when nil
        @n -= 1
        current = fuzz [ Hash, Array ][rand(2)].new
      when Array
        while @n > 0
          @n -= 1
          current << case p = make_pick
          when Array, Hash
            fuzz(p)
          else
            p
          end
        end
      when Hash
        while @n > 0
          @n -= 1
          current[random_string] = case p = make_pick
          when Array, Hash
            fuzz(p)
          else
            p
          end
        end
      end
    end
    current
  end
end

class MyState < JSON.state
  WS = " \r\t\n"

  def initialize
    super(
          :indent       => make_spaces,
          :space        => make_spaces,
          :space_before => make_spaces,
          :object_nl    => make_spaces,
          :array_nl     => make_spaces,
          :max_nesting  => false
         )
  end

  def make_spaces
    s = ''
    rand(1).times { s << WS[rand(WS.size)] }
    s
  end
end

n = (ARGV.shift || 500).to_i
loop do
  fuzzer = Fuzzer.new(n,
                      Hash => 25,
                      Array => 25,
                      String => 10,
                      Fixnum => 10,
                      Bignum => 10,
                      nil => 5,
                      true => 5,
                      false => 5
                     )
  o1 = fuzzer.fuzz
  json = JSON.generate o1, MyState.new
  if $DEBUG
    puts "-" * 80
    puts json, json.size
  else
    puts json.size
  end
  begin
    o2 = JSON.parse(json, :max_nesting => false)
  rescue JSON::ParserError => e
    puts "Caught #{e.class}: #{e.message}\n#{e.backtrace * "\n"}"
    puts "o1 = #{o1.inspect}", "json = #{json}", "json_str = #{json.inspect}"
    puts "locals = #{local_variables.inspect}"
    exit
  end
  if o1 != o2
    puts "mismatch", "o1 = #{o1.inspect}", "o2 = #{o2.inspect}",
      "json = #{json}", "json_str = #{json.inspect}"
    puts "locals = #{local_variables.inspect}"
  end
end