digest-keccak/test/generate_tests.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

2012-10-05 14:40:31 -04:00
#!/usr/bin/env ruby
2019-06-08 08:00:43 -04:00
# frozen_string_literal: true
2012-10-05 14:40:31 -04:00
# This will generate a test suite.
# Based on python-sha3's test suite.
FILES = [
['test/data/ShortMsgKAT_224.txt', 224],
['test/data/ShortMsgKAT_256.txt', 256],
['test/data/ShortMsgKAT_384.txt', 384],
['test/data/ShortMsgKAT_512.txt', 512],
['test/data/LongMsgKAT_224.txt', 224],
]
def generate
puts %Q{
# encoding: binary
# This file generated by generate_tests.rb
require 'test/unit'
2019-06-08 07:53:04 -04:00
class KeccakTests < Test::Unit::TestCase
2012-10-05 14:40:31 -04:00
}
FILES.each do |path, hashlen|
contents = File.read(path).split('Len = ')
contents.each do |test|
lines = test.split("\n")
if !lines.empty? && lines[0] !~ /^#/
length = lines[0].to_i
if length % 8 == 0 && length != 0
msg_raw = [lines[1].split(' = ').last].pack("H*")
md = lines[2].split(' = ').last.downcase
name = File.basename(path).split('.')[0]
puts %Q{
def test_#{name}_#{length}
2019-06-08 07:53:04 -04:00
inst = Digest::Keccak.new(#{hashlen})
2012-10-05 14:40:31 -04:00
inst.update(#{msg_raw.inspect})
assert_equal #{md.inspect}, inst.hexdigest
end
}
end
end
end
end
puts "end"
end
generate