fuzz.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. require 'json'
  2. require 'iconv'
  3. ISO_8859_1_TO_UTF8 = Iconv.new('utf-8', 'iso-8859-15')
  4. class ::String
  5. def to_utf8
  6. ISO_8859_1_TO_UTF8.iconv self
  7. end
  8. end
  9. class Fuzzer
  10. def initialize(n, freqs = {})
  11. sum = freqs.inject(0.0) { |s, x| s + x.last }
  12. freqs.each_key { |x| freqs[x] /= sum }
  13. s = 0.0
  14. freqs.each_key do |x|
  15. freqs[x] = s .. (s + t = freqs[x])
  16. s += t
  17. end
  18. @freqs = freqs
  19. @n = n
  20. @alpha = (0..0xff).to_a
  21. end
  22. def random_string
  23. s = ''
  24. 30.times { s << @alpha[rand(@alpha.size)] }
  25. s.to_utf8
  26. end
  27. def pick
  28. r = rand
  29. found = @freqs.find { |k, f| f.include? rand }
  30. found && found.first
  31. end
  32. def make_pick
  33. k = pick
  34. case
  35. when k == Hash, k == Array
  36. k.new
  37. when k == true, k == false, k == nil
  38. k
  39. when k == String
  40. random_string
  41. when k == Fixnum
  42. rand(2 ** 30) - 2 ** 29
  43. when k == Bignum
  44. rand(2 ** 70) - 2 ** 69
  45. end
  46. end
  47. def fuzz(current = nil)
  48. if @n > 0
  49. case current
  50. when nil
  51. @n -= 1
  52. current = fuzz [ Hash, Array ][rand(2)].new
  53. when Array
  54. while @n > 0
  55. @n -= 1
  56. current << case p = make_pick
  57. when Array, Hash
  58. fuzz(p)
  59. else
  60. p
  61. end
  62. end
  63. when Hash
  64. while @n > 0
  65. @n -= 1
  66. current[random_string] = case p = make_pick
  67. when Array, Hash
  68. fuzz(p)
  69. else
  70. p
  71. end
  72. end
  73. end
  74. end
  75. current
  76. end
  77. end
  78. class MyState < JSON.state
  79. WS = " \r\t\n"
  80. def initialize
  81. super(
  82. :indent => make_spaces,
  83. :space => make_spaces,
  84. :space_before => make_spaces,
  85. :object_nl => make_spaces,
  86. :array_nl => make_spaces,
  87. :max_nesting => false
  88. )
  89. end
  90. def make_spaces
  91. s = ''
  92. rand(1).times { s << WS[rand(WS.size)] }
  93. s
  94. end
  95. end
  96. n = (ARGV.shift || 500).to_i
  97. loop do
  98. fuzzer = Fuzzer.new(n,
  99. Hash => 25,
  100. Array => 25,
  101. String => 10,
  102. Fixnum => 10,
  103. Bignum => 10,
  104. nil => 5,
  105. true => 5,
  106. false => 5
  107. )
  108. o1 = fuzzer.fuzz
  109. json = JSON.generate o1, MyState.new
  110. if $DEBUG
  111. puts "-" * 80
  112. puts json, json.size
  113. else
  114. puts json.size
  115. end
  116. begin
  117. o2 = JSON.parse(json, :max_nesting => false)
  118. rescue JSON::ParserError => e
  119. puts "Caught #{e.class}: #{e.message}\n#{e.backtrace * "\n"}"
  120. puts "o1 = #{o1.inspect}", "json = #{json}", "json_str = #{json.inspect}"
  121. puts "locals = #{local_variables.inspect}"
  122. exit
  123. end
  124. if o1 != o2
  125. puts "mismatch", "o1 = #{o1.inspect}", "o2 = #{o2.inspect}",
  126. "json = #{json}", "json_str = #{json.inspect}"
  127. puts "locals = #{local_variables.inspect}"
  128. end
  129. end