Skip to content

Commit

Permalink
Update fuzzing script
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Oct 17, 2023
1 parent aa55bcf commit de7b118
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions test/fuzz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,57 @@
input = FuzzBert::Generators.random.call.chars.uniq
alphabet = ::EncodedId::Alphabet.new(input)
salt = FuzzBert::Generators.random.call
::EncodedId::ReversibleId.new(
split_with = FuzzBert::Generators.random(5).call
split_at = rand(-500..10_000)
length = rand(-500..10_000)
hex_digit_encoding_group_size = rand(-5..70)
max_length = rand(-500..10_000)
reversible_id = ::EncodedId::ReversibleId.new(
salt: salt,
alphabet: alphabet,
split_with: FuzzBert::Generators.random(5).call,
split_at: rand(-500..10_000),
length: rand(-500..10_000),
hex_digit_encoding_group_size: rand(-5..70),
max_length: rand(-500..10_000)
).decode(data)

# Test encode/decode pair
# TODO:
#
split_with: split_with,
split_at: split_at,
length: length,
hex_digit_encoding_group_size: hex_digit_encoding_group_size,
max_length: max_length
)

# # Test decode random fuzzed input
reversible_id.decode(data)

# Test encode/decode pair with inputs which will be converted using to_i
ids = FuzzBert::Generators.random.call
encoded = reversible_id.encode(ids.chars)
decoded = reversible_id.decode(encoded)

raise StandardError, "Decoded does not match input" unless decoded == ids.chars.map(&:to_i)

# Test encode/decode with integer inputs
encoded = reversible_id.encode(ids.codepoints)
decoded = reversible_id.decode(encoded)

raise StandardError, "Decoded does not match input" unless decoded == ids.codepoints
rescue EncodedId::EncodedIdFormatError,
EncodedId::InvalidInputError,
EncodedId::InvalidConfigurationError,
EncodedId::InvalidAlphabetError
EncodedId::InvalidAlphabetError,
EncodedId::EncodedIdLengthError
# fine, these are expected errors
rescue StandardError => e
puts "\n\nFailure\n--------------\n"
puts "Input: #{data}"
puts "Alphabet: #{alphabet}"
puts "Salt: #{salt}"
puts "Split with: #{split_with}"
puts "Split at: #{split_at}"
puts "Length: #{length}"
puts "Hex digit encoding group size: #{hex_digit_encoding_group_size}"
puts "Max length: #{max_length}"
puts "ReversibleId: #{reversible_id}"
puts "ids: #{ids}"
puts "encoded: #{encoded}"
puts "decoded: #{decoded}"
raise e
end

data "completely random" do
Expand Down

0 comments on commit de7b118

Please sign in to comment.