2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2009-11-25 10:08:06 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'digest'
|
2014-06-26 05:27:22 -04:00
|
|
|
require_relative '../lib/with_different_ofs.rb'
|
2009-11-25 10:08:06 -05:00
|
|
|
|
|
|
|
class TestDigestExtend < Test::Unit::TestCase
|
2010-12-25 21:15:55 -05:00
|
|
|
extend DifferentOFS
|
2010-12-25 08:08:07 -05:00
|
|
|
|
2009-11-25 10:08:06 -05:00
|
|
|
class MyDigest < Digest::Class
|
|
|
|
def initialize(*arg)
|
|
|
|
super
|
|
|
|
@buf = []
|
|
|
|
end
|
|
|
|
|
2009-12-01 04:03:29 -05:00
|
|
|
def initialize_copy(org)
|
|
|
|
@buf = org.buf.dup
|
|
|
|
end
|
|
|
|
|
2009-11-25 10:08:06 -05:00
|
|
|
def update(arg)
|
|
|
|
@buf << arg
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
alias << update
|
|
|
|
|
|
|
|
def finish
|
2010-12-25 08:08:07 -05:00
|
|
|
(@buf.join('').length % 256).chr
|
2009-11-25 10:08:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@buf.clear
|
|
|
|
self
|
|
|
|
end
|
2009-12-01 04:03:29 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def buf
|
|
|
|
@buf
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-25 08:32:48 -05:00
|
|
|
def setup
|
|
|
|
@MyDigest = Class.new(MyDigest)
|
|
|
|
end
|
|
|
|
|
2009-12-01 04:03:29 -05:00
|
|
|
def test_digest_s_hexencode
|
|
|
|
assert_equal('', Digest.hexencode(''))
|
|
|
|
assert_equal('0102', Digest.hexencode("\1\2"))
|
|
|
|
assert_equal(
|
2010-12-25 08:08:07 -05:00
|
|
|
(0..0xff).to_a.map { |c| sprintf("%02x", c ) }.join(''),
|
|
|
|
Digest.hexencode((0..0xff).to_a.map { |c| c.chr }.join(''))
|
2009-12-01 04:03:29 -05:00
|
|
|
)
|
2012-08-01 09:30:51 -04:00
|
|
|
assert_equal(Encoding::US_ASCII, Digest.hexencode("\1\2").encoding)
|
2009-12-01 04:03:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_reset
|
|
|
|
a = Digest::SHA1.new
|
|
|
|
base = a.to_s
|
|
|
|
assert_equal(base, a.reset.to_s)
|
|
|
|
b = a.new
|
|
|
|
assert_equal(base, b.to_s)
|
|
|
|
b.update('1')
|
|
|
|
assert_not_equal(base, b.to_s)
|
|
|
|
assert_equal(base, b.reset.to_s)
|
2009-11-25 10:08:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_digest
|
|
|
|
assert_equal("\3", MyDigest.digest("foo"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hexdigest
|
2010-12-25 08:32:48 -05:00
|
|
|
assert_equal("03", @MyDigest.hexdigest("foo"))
|
2009-11-25 10:08:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_context
|
2010-12-25 08:32:48 -05:00
|
|
|
digester = @MyDigest.new
|
2009-11-25 10:08:06 -05:00
|
|
|
digester.update("foo")
|
|
|
|
assert_equal("\3", digester.digest)
|
|
|
|
digester.update("foobar")
|
2009-12-01 04:03:29 -05:00
|
|
|
assert_equal("\t", digester.digest)
|
2009-11-25 10:08:06 -05:00
|
|
|
digester.update("foo")
|
2009-12-01 04:03:29 -05:00
|
|
|
assert_equal("\f", digester.digest)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new
|
|
|
|
a = Digest::SHA1.new
|
|
|
|
b = a.new
|
|
|
|
obj = a.to_s
|
|
|
|
assert_equal(obj, a.to_s)
|
|
|
|
assert_equal(obj, b.to_s)
|
|
|
|
a.update('1')
|
|
|
|
assert_not_equal(obj, a.to_s)
|
|
|
|
assert_equal(obj, b.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_digest_hexdigest
|
|
|
|
[:digest, :hexdigest].each do |m|
|
|
|
|
exp_1st = "\3"; exp_1st = Digest.hexencode(exp_1st) if m == :hexdigest
|
|
|
|
exp_2nd = "\6"; exp_2nd = Digest.hexencode(exp_2nd) if m == :hexdigest
|
2010-12-25 08:32:48 -05:00
|
|
|
digester = @MyDigest.new
|
2009-12-01 04:03:29 -05:00
|
|
|
digester.update("foo")
|
|
|
|
obj = digester.send(m)
|
|
|
|
# digest w/o param does not reset the org digester.
|
|
|
|
assert_equal(exp_1st, obj)
|
|
|
|
digester.update("bar")
|
|
|
|
obj = digester.send(m)
|
|
|
|
assert_equal(exp_2nd, obj)
|
|
|
|
obj = digester.send(m, "baz")
|
|
|
|
# digest with param resets the org digester.
|
|
|
|
assert_equal(exp_1st, obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_digest_hexdigest_bang
|
|
|
|
[:digest!, :hexdigest!].each do |m|
|
|
|
|
exp_1st = "\3"; exp_1st = Digest.hexencode(exp_1st) if m == :hexdigest!
|
2010-12-25 08:32:48 -05:00
|
|
|
digester = @MyDigest.new
|
2009-12-01 04:03:29 -05:00
|
|
|
digester.update("foo")
|
|
|
|
obj = digester.send(m) # digest! always resets the org digester.
|
|
|
|
assert_equal(exp_1st, obj)
|
|
|
|
digester.update("bar")
|
|
|
|
obj = digester.send(m)
|
|
|
|
assert_equal(exp_1st, obj)
|
|
|
|
end
|
2009-11-25 10:08:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
2010-12-25 08:32:48 -05:00
|
|
|
digester = @MyDigest.new
|
2009-11-25 10:08:06 -05:00
|
|
|
digester.update("foo")
|
|
|
|
assert_equal("03", digester.to_s)
|
|
|
|
end
|
|
|
|
|
2009-12-01 04:03:29 -05:00
|
|
|
def test_length
|
2010-12-25 08:32:48 -05:00
|
|
|
@MyDigest.class_eval do
|
|
|
|
def digest_length
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
digester = @MyDigest.new
|
2009-12-01 04:03:29 -05:00
|
|
|
assert_equal(2, digester.length)
|
|
|
|
assert_equal(2, digester.size)
|
|
|
|
end
|
|
|
|
|
2010-12-25 08:32:48 -05:00
|
|
|
def test_digest_length # breaks @MyDigest#digest_length
|
|
|
|
assert_equal(1, @MyDigest.new.digest_length)
|
|
|
|
@MyDigest.class_eval do
|
2009-11-25 10:08:06 -05:00
|
|
|
def digest_length
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
2010-12-25 08:32:48 -05:00
|
|
|
assert_equal(2, @MyDigest.new.digest_length)
|
2009-11-25 10:08:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_length
|
2015-06-01 22:18:44 -04:00
|
|
|
assert_raise(RuntimeError) do
|
2010-12-25 08:32:48 -05:00
|
|
|
@MyDigest.new.block_length
|
2009-11-25 10:08:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|