digest-keccak/test/generate_tests.rb

51 lines
1.2 KiB
Ruby

#!/usr/bin/env ruby
# frozen_string_literal: true
# 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'
class KeccakTests < Test::Unit::TestCase
}
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}
inst = Digest::Keccak.new(#{hashlen})
inst.update(#{msg_raw.inspect})
assert_equal #{md.inspect}, inst.hexdigest
end
}
end
end
end
end
puts "end"
end
generate