\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /proc/self/root/opt/chef/embedded/lib/ruby/gems/2.7.0/gems/json-2.3.1/tools/

Viewing File: fuzz.rb

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