~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/openssl/test_digest.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
begin
2
 
  require "openssl"
3
 
rescue LoadError
4
 
end
5
 
require "digest/md5"
6
 
require "test/unit"
 
1
require_relative 'utils'
7
2
 
8
3
if defined?(OpenSSL)
9
4
 
61
56
    assert_equal(dig1, dig2, "reset")
62
57
  end
63
58
 
 
59
  def test_digest_constants
 
60
    algs = %w(DSS1 MD4 MD5 RIPEMD160 SHA SHA1)
 
61
    if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
 
62
      algs += %w(SHA224 SHA256 SHA384 SHA512)
 
63
    end
 
64
    algs.each do |alg|
 
65
      assert_not_nil(OpenSSL::Digest.new(alg))
 
66
      klass = OpenSSL::Digest.const_get(alg)
 
67
      assert_not_nil(klass.new)
 
68
    end
 
69
  end
 
70
 
 
71
  def test_digest_by_oid_and_name
 
72
    check_digest(OpenSSL::ASN1::ObjectId.new("MD5"))
 
73
    check_digest(OpenSSL::ASN1::ObjectId.new("SHA1"))
 
74
  end
 
75
 
64
76
  if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
65
77
    def encode16(str)
66
78
      str.unpack("H*").first
82
94
      assert_equal(sha384_a, encode16(OpenSSL::Digest::SHA384.digest("a")))
83
95
      assert_equal(sha512_a, encode16(OpenSSL::Digest::SHA512.digest("a")))
84
96
    end
 
97
 
 
98
    def test_digest_by_oid_and_name_sha2
 
99
      check_digest(OpenSSL::ASN1::ObjectId.new("SHA224"))
 
100
      check_digest(OpenSSL::ASN1::ObjectId.new("SHA256"))
 
101
      check_digest(OpenSSL::ASN1::ObjectId.new("SHA384"))
 
102
      check_digest(OpenSSL::ASN1::ObjectId.new("SHA512"))
 
103
    end
 
104
  end
 
105
 
 
106
  private
 
107
 
 
108
  def check_digest(oid)
 
109
    d = OpenSSL::Digest.new(oid.sn)
 
110
    assert_not_nil(d)
 
111
    d = OpenSSL::Digest.new(oid.ln)
 
112
    assert_not_nil(d)
 
113
    d = OpenSSL::Digest.new(oid.oid)
 
114
    assert_not_nil(d)
85
115
  end
86
116
end
87
117