summaryrefslogtreecommitdiff
path: root/test/digest
diff options
context:
space:
mode:
authorAnas Nashif <anas.nashif@intel.com>2012-11-06 11:57:02 -0800
committerAnas Nashif <anas.nashif@intel.com>2012-11-06 11:57:02 -0800
commit3b35de2a90e26b99e2a6d4f61dc56d6ce7ded748 (patch)
treef66334a0ad8cf59590dd682d95d6244e8b454853 /test/digest
downloadruby-3b35de2a90e26b99e2a6d4f61dc56d6ce7ded748.tar.gz
ruby-3b35de2a90e26b99e2a6d4f61dc56d6ce7ded748.tar.bz2
ruby-3b35de2a90e26b99e2a6d4f61dc56d6ce7ded748.zip
Imported Upstream version 1.9.3.p194upstream/1.9.3.p194
Diffstat (limited to 'test/digest')
-rwxr-xr-xtest/digest/test_digest.rb150
-rw-r--r--test/digest/test_digest_extend.rb157
-rw-r--r--test/digest/test_digest_hmac.rb2
3 files changed, 309 insertions, 0 deletions
diff --git a/test/digest/test_digest.rb b/test/digest/test_digest.rb
new file mode 100755
index 0000000..f41e537
--- /dev/null
+++ b/test/digest/test_digest.rb
@@ -0,0 +1,150 @@
+#!/usr/bin/env ruby
+#
+# $RoughId: test.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
+# $Id: test_digest.rb 32547 2011-07-15 03:05:23Z nahi $
+
+require 'test/unit'
+
+require 'digest'
+%w[digest/md5 digest/rmd160 digest/sha1 digest/sha2].each do |lib|
+ begin
+ require lib
+ rescue LoadError
+ end
+end
+
+module TestDigest
+ Data1 = "abc"
+ Data2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+
+ def test_s_new
+ self.class::DATA.each do |str, hexdigest|
+ assert_raise(ArgumentError) { self.class::ALGO.new("") }
+ end
+ end
+
+ def test_s_hexdigest
+ self.class::DATA.each do |str, hexdigest|
+ assert_equal(hexdigest, self.class::ALGO.hexdigest(str))
+ end
+ end
+
+ def test_s_base64digest
+ self.class::DATA.each do |str, hexdigest|
+ digest = [hexdigest].pack("H*")
+ assert_equal([digest].pack("m0"), self.class::ALGO.base64digest(str))
+ end
+ end
+
+ def test_s_digest
+ self.class::DATA.each do |str, hexdigest|
+ digest = [hexdigest].pack("H*")
+ assert_equal(digest, self.class::ALGO.digest(str))
+ end
+ end
+
+ def test_update
+ # This test is also for digest() and hexdigest()
+
+ str = "ABC"
+
+ 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)
+ end
+
+ def test_eq
+ # This test is also for clone()
+
+ md1 = self.class::ALGO.new
+ md1 << "ABC"
+
+ assert_equal(md1, md1.clone, self.class::ALGO)
+
+ md2 = self.class::ALGO.new
+ md2 << "A"
+
+ assert_not_equal(md1, md2, self.class::ALGO)
+
+ md2 << "BC"
+
+ assert_equal(md1, md2, self.class::ALGO)
+ end
+
+ def test_instance_eval
+ assert_nothing_raised {
+ self.class::ALGO.new.instance_eval { update "a" }
+ }
+ end
+
+ def test_alignment
+ md = self.class::ALGO.new
+ assert_nothing_raised('#4320') {
+ md.update('a' * 97)
+ md.update('a' * 97)
+ md.hexdigest
+ }
+ end
+
+ class TestMD5 < Test::Unit::TestCase
+ include TestDigest
+ ALGO = Digest::MD5
+ DATA = {
+ Data1 => "900150983cd24fb0d6963f7d28e17f72",
+ Data2 => "8215ef0796a20bcaaae116d3876c664a",
+ }
+ end if defined?(Digest::MD5)
+
+ class TestSHA1 < Test::Unit::TestCase
+ include TestDigest
+ ALGO = Digest::SHA1
+ DATA = {
+ Data1 => "a9993e364706816aba3e25717850c26c9cd0d89d",
+ Data2 => "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
+ }
+ end if defined?(Digest::SHA1)
+
+ class TestSHA256 < Test::Unit::TestCase
+ include TestDigest
+ ALGO = Digest::SHA256
+ DATA = {
+ Data1 => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
+ Data2 => "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
+ }
+ end if defined?(Digest::SHA256)
+
+ class TestSHA384 < Test::Unit::TestCase
+ include TestDigest
+ ALGO = Digest::SHA384
+ DATA = {
+ Data1 => "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
+ Data2 => "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b",
+ }
+ end if defined?(Digest::SHA384)
+
+ class TestSHA512 < Test::Unit::TestCase
+ include TestDigest
+ ALGO = Digest::SHA512
+ DATA = {
+ Data1 => "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
+ Data2 => "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445",
+ }
+ end if defined?(Digest::SHA512)
+
+ class TestRMD160 < Test::Unit::TestCase
+ include TestDigest
+ ALGO = Digest::RMD160
+ DATA = {
+ Data1 => "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
+ Data2 => "12a053384a9c0c88e405a06c27dcf49ada62eb2b",
+ }
+ end if defined?(Digest::RMD160)
+
+ class TestBase < Test::Unit::TestCase
+ def test_base
+ bug3810 = '[ruby-core:32231]'
+ assert_raise(NotImplementedError, bug3810) {Digest::Base.new}
+ end
+ end
+end
diff --git a/test/digest/test_digest_extend.rb b/test/digest/test_digest_extend.rb
new file mode 100644
index 0000000..577fd7a
--- /dev/null
+++ b/test/digest/test_digest_extend.rb
@@ -0,0 +1,157 @@
+require 'test/unit'
+require 'digest'
+require_relative '../with_different_ofs.rb'
+
+class TestDigestExtend < Test::Unit::TestCase
+ extend DifferentOFS
+
+ class MyDigest < Digest::Class
+ def initialize(*arg)
+ super
+ @buf = []
+ end
+
+ def initialize_copy(org)
+ @buf = org.buf.dup
+ end
+
+ def update(arg)
+ @buf << arg
+ self
+ end
+
+ alias << update
+
+ def finish
+ (@buf.join('').length % 256).chr
+ end
+
+ def reset
+ @buf.clear
+ self
+ end
+
+ protected
+
+ def buf
+ @buf
+ end
+ end
+
+ def setup
+ @MyDigest = Class.new(MyDigest)
+ end
+
+ def test_digest_s_hexencode
+ assert_equal('', Digest.hexencode(''))
+ assert_equal('0102', Digest.hexencode("\1\2"))
+ assert_equal(
+ (0..0xff).to_a.map { |c| sprintf("%02x", c ) }.join(''),
+ Digest.hexencode((0..0xff).to_a.map { |c| c.chr }.join(''))
+ )
+ 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)
+ end
+
+ def test_digest
+ assert_equal("\3", MyDigest.digest("foo"))
+ end
+
+ def test_hexdigest
+ assert_equal("03", @MyDigest.hexdigest("foo"))
+ end
+
+ def test_context
+ digester = @MyDigest.new
+ digester.update("foo")
+ assert_equal("\3", digester.digest)
+ digester.update("foobar")
+ assert_equal("\t", digester.digest)
+ digester.update("foo")
+ 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
+ digester = @MyDigest.new
+ 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!
+ digester = @MyDigest.new
+ 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
+ end
+
+ def test_to_s
+ digester = @MyDigest.new
+ digester.update("foo")
+ assert_equal("03", digester.to_s)
+ end
+
+ def test_length
+ @MyDigest.class_eval do
+ def digest_length
+ 2
+ end
+ end
+ digester = @MyDigest.new
+ assert_equal(2, digester.length)
+ assert_equal(2, digester.size)
+ end
+
+ def test_digest_length # breaks @MyDigest#digest_length
+ assert_equal(1, @MyDigest.new.digest_length)
+ @MyDigest.class_eval do
+ def digest_length
+ 2
+ end
+ end
+ assert_equal(2, @MyDigest.new.digest_length)
+ end
+
+ def test_block_length
+ assert_raises(RuntimeError) do
+ @MyDigest.new.block_length
+ end
+ end
+end
diff --git a/test/digest/test_digest_hmac.rb b/test/digest/test_digest_hmac.rb
new file mode 100644
index 0000000..3eb1567
--- /dev/null
+++ b/test/digest/test_digest_hmac.rb
@@ -0,0 +1,2 @@
+require_relative '../inlinetest.rb'
+InlineTest.loadtest__END__part('digest/hmac.rb')