Skip to content
This repository
Newer
Older
100644 89 lines (75 sloc) 2.611 kb
aad24d7d »
2007-11-29 Adding openssl tests here where they can actually be run
1 begin
2 require "openssl"
3 rescue LoadError
4 end
5 require "digest/md5"
6 require "test/unit"
7
8 if defined?(OpenSSL)
9
10 class OpenSSL::TestDigest < Test::Unit::TestCase
11 def setup
12 @d1 = OpenSSL::Digest::Digest::new("MD5")
13 @d2 = OpenSSL::Digest::MD5.new
14 @md = Digest::MD5.new
15 @data = "DATA"
16 end
17
18 def teardown
19 @d1 = @d2 = @md = nil
20 end
21
22 def test_digest
23 assert_equal(@md.digest, @d1.digest)
24 assert_equal(@md.hexdigest, @d1.hexdigest)
25 @d1 << @data
26 @d2 << @data
27 @md << @data
28 assert_equal(@md.digest, @d1.digest)
29 assert_equal(@md.hexdigest, @d1.hexdigest)
30 assert_equal(@d1.digest, @d2.digest)
31 assert_equal(@d1.hexdigest, @d2.hexdigest)
32 assert_equal(@md.digest, OpenSSL::Digest::MD5.digest(@data))
33 assert_equal(@md.hexdigest, OpenSSL::Digest::MD5.hexdigest(@data))
34 end
35
36 def test_eql
37 assert(@d1 == @d2, "==")
38 d = @d1.clone
39 assert(d == @d1, "clone")
40 end
41
42 def test_info
43 assert_equal("MD5", @d1.name, "name")
44 assert_equal("MD5", @d2.name, "name")
45 assert_equal(16, @d1.size, "size")
46 end
47
48 def test_dup
49 @d1.update(@data)
50 assert_equal(@d1.name, @d1.dup.name, "dup")
51 assert_equal(@d1.name, @d1.clone.name, "clone")
52 assert_equal(@d1.digest, @d1.clone.digest, "clone .digest")
53 end
54
55 def test_reset
56 @d1.update(@data)
57 dig1 = @d1.digest
58 @d1.reset
59 @d1.update(@data)
60 dig2 = @d1.digest
61 assert_equal(dig1, dig2, "reset")
62 end
63
64 if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
65 def encode16(str)
66 str.unpack("H*").first
67 end
68
69 def test_098_features
70 sha224_a = "abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5"
71 sha256_a = "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
72 sha384_a = "54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31"
73 sha512_a = "1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75"
74
75 assert_equal(sha224_a, OpenSSL::Digest::SHA224.hexdigest("a"))
76 assert_equal(sha256_a, OpenSSL::Digest::SHA256.hexdigest("a"))
77 assert_equal(sha384_a, OpenSSL::Digest::SHA384.hexdigest("a"))
78 assert_equal(sha512_a, OpenSSL::Digest::SHA512.hexdigest("a"))
79
80 assert_equal(sha224_a, encode16(OpenSSL::Digest::SHA224.digest("a")))
81 assert_equal(sha256_a, encode16(OpenSSL::Digest::SHA256.digest("a")))
82 assert_equal(sha384_a, encode16(OpenSSL::Digest::SHA384.digest("a")))
83 assert_equal(sha512_a, encode16(OpenSSL::Digest::SHA512.digest("a")))
84 end
85 end
86 end
87
88 end
Something went wrong with that request. Please try again.