Skip to content

Commit f7e7443

Browse files
committed
Adjust OpenSSL specs for digest algorithm lookup
ruby/openssl#958 changed the common logic for digest algorithm lookup: - If the argument is neither an OpenSSL::Digest instance nor a String, it is now implicitly converted to String with #to_str. This is consistent with algorithm name lookup logic in ruby/openssl for pkeys and ciphers. - If the name is not recognized, OpenSSL::Digest::DigestError is raised instead of RuntimeError. Update the specs accordingly: - Remove specs that expect #to_str not to be called. - Relax regexps matching TypeError messages. - Expect OpenSSL::Digest::DigestError instead of RuntimeError for ruby/openssl 4.0.0 and later.
1 parent 87ae631 commit f7e7443

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

spec/ruby/library/openssl/digest/initialize_spec.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@
2323
OpenSSL::Digest.new("sha512").name.should == "SHA512"
2424
end
2525

26-
it "throws an error when called with an unknown digest" do
27-
-> { OpenSSL::Digest.new("wd40") }.should raise_error(RuntimeError, /Unsupported digest algorithm \(wd40\)/)
26+
version_is OpenSSL::VERSION, "4.0.0" do
27+
it "throws an error when called with an unknown digest" do
28+
-> { OpenSSL::Digest.new("wd40") }.should raise_error(OpenSSL::Digest::DigestError, /wd40/)
29+
end
2830
end
2931

3032
it "cannot be called with a symbol" do
31-
-> { OpenSSL::Digest.new(:SHA1) }.should raise_error(TypeError, /wrong argument type Symbol/)
32-
end
33-
34-
it "does not call #to_str on the argument" do
35-
name = mock("digest name")
36-
name.should_not_receive(:to_str)
37-
-> { OpenSSL::Digest.new(name) }.should raise_error(TypeError, /wrong argument type/)
33+
-> { OpenSSL::Digest.new(:SHA1) }.should raise_error(TypeError)
3834
end
3935
end
4036

@@ -62,7 +58,7 @@
6258
end
6359

6460
it "cannot be called with a digest class" do
65-
-> { OpenSSL::Digest.new(OpenSSL::Digest::SHA1) }.should raise_error(TypeError, /wrong argument type Class/)
61+
-> { OpenSSL::Digest.new(OpenSSL::Digest::SHA1) }.should raise_error(TypeError)
6662
end
6763

6864
context "when called without an initial String argument" do

spec/ruby/library/openssl/kdf/pbkdf2_hmac_spec.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,15 @@
107107
it "raises a TypeError when hash is neither a String nor an OpenSSL::Digest" do
108108
-> {
109109
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: Object.new)
110-
}.should raise_error(TypeError, "wrong argument type Object (expected OpenSSL/Digest)")
110+
}.should raise_error(TypeError)
111111
end
112112

113-
it "raises a TypeError when hash is neither a String nor an OpenSSL::Digest, it does not try to call #to_str" do
114-
hash = mock("hash")
115-
hash.should_not_receive(:to_str)
116-
-> {
117-
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: hash)
118-
}.should raise_error(TypeError, "wrong argument type MockObject (expected OpenSSL/Digest)")
119-
end
120-
121-
it "raises a RuntimeError for unknown digest algorithms" do
122-
-> {
123-
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: "wd40")
124-
}.should raise_error(RuntimeError, /Unsupported digest algorithm \(wd40\)/)
113+
version_is OpenSSL::VERSION, "4.0.0" do
114+
it "raises a OpenSSL::Digest::DigestError for unknown digest algorithms" do
115+
-> {
116+
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: "wd40")
117+
}.should raise_error(OpenSSL::Digest::DigestError, /wd40/)
118+
end
125119
end
126120

127121
it "treats salt as a required keyword" do

0 commit comments

Comments
 (0)