2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2001-07-13 16:06:14 -04:00
|
|
|
# $RoughId: test.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
|
|
|
|
# $Id$
|
|
|
|
|
2002-11-27 02:28:45 -05:00
|
|
|
require 'test/unit'
|
2013-05-21 08:41:34 -04:00
|
|
|
require 'tempfile'
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
require 'digest'
|
2013-11-20 20:31:51 -05:00
|
|
|
%w[digest/md5 digest/rmd160 digest/sha1 digest/sha2 digest/bubblebabble].each do |lib|
|
2005-02-17 06:16:57 -05:00
|
|
|
begin
|
|
|
|
require lib
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
module TestDigest
|
|
|
|
Data1 = "abc"
|
|
|
|
Data2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2010-10-18 00:26:06 -04:00
|
|
|
def test_s_new
|
|
|
|
self.class::DATA.each do |str, hexdigest|
|
|
|
|
assert_raise(ArgumentError) { self.class::ALGO.new("") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
def test_s_hexdigest
|
2010-01-17 14:12:10 -05:00
|
|
|
self.class::DATA.each do |str, hexdigest|
|
2012-08-01 09:30:51 -04:00
|
|
|
actual = self.class::ALGO.hexdigest(str)
|
|
|
|
assert_equal(hexdigest, actual)
|
|
|
|
assert_equal(Encoding::US_ASCII, actual.encoding)
|
2010-01-17 14:12:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_base64digest
|
|
|
|
self.class::DATA.each do |str, hexdigest|
|
|
|
|
digest = [hexdigest].pack("H*")
|
2012-08-01 09:30:51 -04:00
|
|
|
actual = self.class::ALGO.base64digest(str)
|
|
|
|
assert_equal([digest].pack("m0"), actual)
|
|
|
|
assert_equal(Encoding::US_ASCII, actual.encoding)
|
2001-07-13 16:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_digest
|
2010-01-17 14:12:10 -05:00
|
|
|
self.class::DATA.each do |str, hexdigest|
|
|
|
|
digest = [hexdigest].pack("H*")
|
2012-08-01 09:30:51 -04:00
|
|
|
actual = self.class::ALGO.digest(str)
|
|
|
|
assert_equal(digest, actual)
|
|
|
|
assert_equal(Encoding::BINARY, actual.encoding)
|
2001-07-13 16:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
|
|
|
# This test is also for digest() and hexdigest()
|
|
|
|
|
|
|
|
str = "ABC"
|
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
md = self.class::ALGO.new
|
|
|
|
md.update str
|
|
|
|
assert_equal(self.class::ALGO.hexdigest(str), md.hexdigest)
|
|
|
|
assert_equal(self.class::ALGO.digest(str), md.digest)
|
2001-07-13 16:06:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_eq
|
|
|
|
# This test is also for clone()
|
|
|
|
|
2006-10-13 07:58:05 -04:00
|
|
|
md1 = self.class::ALGO.new
|
|
|
|
md1 << "ABC"
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
assert_equal(md1, md1.clone, self.class::ALGO)
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2014-06-06 17:05:48 -04:00
|
|
|
bug9913 = '[ruby-core:62967] [Bug #9913]'
|
|
|
|
assert_not_equal(md1, nil, bug9913)
|
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
md2 = self.class::ALGO.new
|
|
|
|
md2 << "A"
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2010-05-02 01:56:35 -04:00
|
|
|
assert_not_equal(md1, md2, self.class::ALGO)
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
md2 << "BC"
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
assert_equal(md1, md2, self.class::ALGO)
|
2001-07-13 16:06:14 -04:00
|
|
|
end
|
2004-09-07 01:32:26 -04:00
|
|
|
|
2013-05-21 08:41:34 -04:00
|
|
|
def test_s_file
|
2013-05-22 06:36:28 -04:00
|
|
|
Tempfile.create("test_digest_file", mode: File::BINARY) { |tmpfile|
|
2013-05-21 08:41:34 -04:00
|
|
|
str = "hello, world.\r\n"
|
|
|
|
tmpfile.print str
|
|
|
|
tmpfile.close
|
|
|
|
|
|
|
|
assert_equal self.class::ALGO.new.update(str), self.class::ALGO.file(tmpfile.path)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
def test_instance_eval
|
2004-09-07 01:32:26 -04:00
|
|
|
assert_nothing_raised {
|
2005-02-17 06:16:57 -05:00
|
|
|
self.class::ALGO.new.instance_eval { update "a" }
|
2004-09-07 01:32:26 -04:00
|
|
|
}
|
|
|
|
end
|
2005-02-17 06:16:57 -05:00
|
|
|
|
2011-07-14 23:03:31 -04:00
|
|
|
def test_alignment
|
|
|
|
md = self.class::ALGO.new
|
|
|
|
assert_nothing_raised('#4320') {
|
|
|
|
md.update('a' * 97)
|
|
|
|
md.update('a' * 97)
|
|
|
|
md.hexdigest
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-20 20:31:51 -05:00
|
|
|
def test_bubblebabble
|
2013-11-21 02:51:29 -05:00
|
|
|
expected = "xirek-hasol-fumik-lanax"
|
2013-11-21 19:43:08 -05:00
|
|
|
assert_equal expected, Digest.bubblebabble('message')
|
2013-11-21 02:51:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bubblebabble_class
|
2013-11-20 20:31:51 -05:00
|
|
|
expected = "xopoh-fedac-fenyh-nehon-mopel-nivor-lumiz-rypon-gyfot-cosyz-rimez-lolyv-pekyz-rosud-ricob-surac-toxox"
|
2013-11-21 19:43:08 -05:00
|
|
|
assert_equal expected, Digest::SHA256.bubblebabble('message')
|
2013-11-20 20:31:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-21 02:51:29 -05:00
|
|
|
def test_bubblebabble_instance
|
|
|
|
expected = "xumor-boceg-dakuz-sulic-gukoz-rutas-mekek-zovud-gunap-vabov-genin-rygyg-sanun-hykac-ruvah-dovah-huxex"
|
|
|
|
|
|
|
|
hash = Digest::SHA256.new
|
2013-11-21 19:43:08 -05:00
|
|
|
assert_equal expected, hash.bubblebabble
|
2013-11-21 02:51:29 -05:00
|
|
|
end
|
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
class TestMD5 < Test::Unit::TestCase
|
|
|
|
include TestDigest
|
2006-02-17 03:40:52 -05:00
|
|
|
ALGO = Digest::MD5
|
2005-02-17 06:16:57 -05:00
|
|
|
DATA = {
|
|
|
|
Data1 => "900150983cd24fb0d6963f7d28e17f72",
|
|
|
|
Data2 => "8215ef0796a20bcaaae116d3876c664a",
|
|
|
|
}
|
2006-02-17 03:40:52 -05:00
|
|
|
end if defined?(Digest::MD5)
|
2005-02-17 06:16:57 -05:00
|
|
|
|
|
|
|
class TestSHA1 < Test::Unit::TestCase
|
|
|
|
include TestDigest
|
2006-02-17 03:40:52 -05:00
|
|
|
ALGO = Digest::SHA1
|
2005-02-17 06:16:57 -05:00
|
|
|
DATA = {
|
|
|
|
Data1 => "a9993e364706816aba3e25717850c26c9cd0d89d",
|
|
|
|
Data2 => "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
|
|
|
|
}
|
2006-02-17 03:40:52 -05:00
|
|
|
end if defined?(Digest::SHA1)
|
2005-02-17 06:16:57 -05:00
|
|
|
|
|
|
|
class TestSHA256 < Test::Unit::TestCase
|
|
|
|
include TestDigest
|
2006-02-17 03:40:52 -05:00
|
|
|
ALGO = Digest::SHA256
|
2005-02-17 06:16:57 -05:00
|
|
|
DATA = {
|
|
|
|
Data1 => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
|
|
|
Data2 => "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
|
|
|
|
}
|
2006-02-17 03:40:52 -05:00
|
|
|
end if defined?(Digest::SHA256)
|
2005-02-17 06:16:57 -05:00
|
|
|
|
|
|
|
class TestSHA384 < Test::Unit::TestCase
|
|
|
|
include TestDigest
|
2006-02-17 03:40:52 -05:00
|
|
|
ALGO = Digest::SHA384
|
2005-02-17 06:16:57 -05:00
|
|
|
DATA = {
|
2016-10-27 02:26:09 -04:00
|
|
|
Data1 => "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"\
|
|
|
|
"8086072ba1e7cc2358baeca134c825a7",
|
|
|
|
Data2 => "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6"\
|
|
|
|
"b0455a8520bc4e6f5fe95b1fe3c8452b",
|
2005-02-17 06:16:57 -05:00
|
|
|
}
|
2006-02-17 03:40:52 -05:00
|
|
|
end if defined?(Digest::SHA384)
|
2005-02-17 06:16:57 -05:00
|
|
|
|
|
|
|
class TestSHA512 < Test::Unit::TestCase
|
|
|
|
include TestDigest
|
2006-02-17 03:40:52 -05:00
|
|
|
ALGO = Digest::SHA512
|
2005-02-17 06:16:57 -05:00
|
|
|
DATA = {
|
2016-10-27 02:26:09 -04:00
|
|
|
Data1 => "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"\
|
|
|
|
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
|
|
|
|
Data2 => "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c335"\
|
|
|
|
"96fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445",
|
2005-02-17 06:16:57 -05:00
|
|
|
}
|
2006-02-17 03:40:52 -05:00
|
|
|
end if defined?(Digest::SHA512)
|
2005-02-17 06:16:57 -05:00
|
|
|
|
2013-05-21 08:41:34 -04:00
|
|
|
class TestSHA2 < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_s_file
|
|
|
|
Tempfile.create("test_digest_file") { |tmpfile|
|
|
|
|
str = Data1
|
|
|
|
tmpfile.print str
|
|
|
|
tmpfile.close
|
|
|
|
|
2016-10-27 02:26:09 -04:00
|
|
|
assert_equal "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"\
|
|
|
|
"8086072ba1e7cc2358baeca134c825a7",
|
|
|
|
Digest::SHA2.file(tmpfile.path, 384).hexdigest
|
2013-05-21 08:41:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end if defined?(Digest::SHA2)
|
|
|
|
|
2005-02-17 06:16:57 -05:00
|
|
|
class TestRMD160 < Test::Unit::TestCase
|
|
|
|
include TestDigest
|
2006-02-17 03:40:52 -05:00
|
|
|
ALGO = Digest::RMD160
|
2005-02-17 06:16:57 -05:00
|
|
|
DATA = {
|
|
|
|
Data1 => "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
|
|
|
|
Data2 => "12a053384a9c0c88e405a06c27dcf49ada62eb2b",
|
|
|
|
}
|
2006-02-17 03:40:52 -05:00
|
|
|
end if defined?(Digest::RMD160)
|
2010-09-09 10:25:56 -04:00
|
|
|
|
|
|
|
class TestBase < Test::Unit::TestCase
|
|
|
|
def test_base
|
|
|
|
bug3810 = '[ruby-core:32231]'
|
|
|
|
assert_raise(NotImplementedError, bug3810) {Digest::Base.new}
|
|
|
|
end
|
|
|
|
end
|
2014-09-26 21:30:16 -04:00
|
|
|
|
|
|
|
class TestInitCopy < Test::Unit::TestCase
|
|
|
|
if defined?(Digest::MD5) and defined?(Digest::RMD160)
|
|
|
|
def test_initialize_copy_md5_rmd160
|
|
|
|
assert_separately(%w[-rdigest], <<-'end;')
|
|
|
|
md5 = Digest::MD5.allocate
|
|
|
|
rmd160 = Digest::RMD160.allocate
|
|
|
|
assert_raise(TypeError) {md5.__send__(:initialize_copy, rmd160)}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-31 09:21:51 -04:00
|
|
|
|
|
|
|
class TestDigestParen < Test::Unit::TestCase
|
|
|
|
def test_sha2
|
|
|
|
assert_separately(%w[-rdigest], <<-'end;')
|
|
|
|
assert_nothing_raised {
|
|
|
|
Digest(:SHA256).new
|
|
|
|
Digest(:SHA384).new
|
|
|
|
Digest(:SHA512).new
|
|
|
|
}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_lib
|
|
|
|
assert_separately(%w[-rdigest], <<-'end;')
|
|
|
|
class Digest::Nolib < Digest::Class
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised {
|
|
|
|
Digest(:Nolib).new
|
|
|
|
}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_lib_no_def
|
|
|
|
assert_separately(%w[-rdigest], <<-'end;')
|
|
|
|
assert_raise(LoadError) {
|
|
|
|
Digest(:Nodef).new
|
|
|
|
}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_race
|
2020-03-27 02:31:14 -04:00
|
|
|
assert_separately(['-rdigest', "-I#{File.dirname(__FILE__)}"], <<-"end;")
|
2014-10-31 09:21:51 -04:00
|
|
|
assert_nothing_raised {
|
|
|
|
t = Thread.start {
|
2020-03-27 02:31:14 -04:00
|
|
|
sleep #{ EnvUtil.apply_timeout_scale(0.1) }
|
2014-10-31 09:21:51 -04:00
|
|
|
Digest(:Foo).new
|
|
|
|
}
|
|
|
|
Digest(:Foo).new
|
|
|
|
t.join
|
|
|
|
}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_race_mixed
|
2020-03-27 02:31:14 -04:00
|
|
|
assert_separately(['-rdigest', "-I#{File.dirname(__FILE__)}"], <<-"end;")
|
2014-10-31 09:21:51 -04:00
|
|
|
assert_nothing_raised {
|
|
|
|
t = Thread.start {
|
2020-03-27 02:31:14 -04:00
|
|
|
sleep #{ EnvUtil.apply_timeout_scale(0.1) }
|
2017-12-12 13:44:21 -05:00
|
|
|
Thread.current.report_on_exception = false
|
2014-10-31 09:21:51 -04:00
|
|
|
Digest::Foo.new
|
|
|
|
}
|
|
|
|
Digest(:Foo).new
|
|
|
|
begin
|
|
|
|
t.join
|
|
|
|
rescue NoMethodError, NameError
|
|
|
|
# NoMethodError is highly likely; NameError is listed just in case
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
2001-07-13 16:06:14 -04:00
|
|
|
end
|