Adjust OpenSSL specs for digest algorithm lookup

https://github.com/ruby/openssl/pull/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.
This commit is contained in:
Kazuki Yamaguchi 2025-11-07 00:45:10 +09:00 committed by Kazuki Yamaguchi
parent 87ae631b40
commit f7e7443aaa
Notes: git 2025-11-06 16:19:59 +00:00
2 changed files with 13 additions and 23 deletions

View File

@ -23,18 +23,14 @@ describe "OpenSSL::Digest#initialize" do
OpenSSL::Digest.new("sha512").name.should == "SHA512"
end
it "throws an error when called with an unknown digest" do
-> { OpenSSL::Digest.new("wd40") }.should raise_error(RuntimeError, /Unsupported digest algorithm \(wd40\)/)
version_is OpenSSL::VERSION, "4.0.0" do
it "throws an error when called with an unknown digest" do
-> { OpenSSL::Digest.new("wd40") }.should raise_error(OpenSSL::Digest::DigestError, /wd40/)
end
end
it "cannot be called with a symbol" do
-> { OpenSSL::Digest.new(:SHA1) }.should raise_error(TypeError, /wrong argument type Symbol/)
end
it "does not call #to_str on the argument" do
name = mock("digest name")
name.should_not_receive(:to_str)
-> { OpenSSL::Digest.new(name) }.should raise_error(TypeError, /wrong argument type/)
-> { OpenSSL::Digest.new(:SHA1) }.should raise_error(TypeError)
end
end
@ -62,7 +58,7 @@ describe "OpenSSL::Digest#initialize" do
end
it "cannot be called with a digest class" do
-> { OpenSSL::Digest.new(OpenSSL::Digest::SHA1) }.should raise_error(TypeError, /wrong argument type Class/)
-> { OpenSSL::Digest.new(OpenSSL::Digest::SHA1) }.should raise_error(TypeError)
end
context "when called without an initial String argument" do

View File

@ -107,21 +107,15 @@ describe "OpenSSL::KDF.pbkdf2_hmac" do
it "raises a TypeError when hash is neither a String nor an OpenSSL::Digest" do
-> {
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: Object.new)
}.should raise_error(TypeError, "wrong argument type Object (expected OpenSSL/Digest)")
}.should raise_error(TypeError)
end
it "raises a TypeError when hash is neither a String nor an OpenSSL::Digest, it does not try to call #to_str" do
hash = mock("hash")
hash.should_not_receive(:to_str)
-> {
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: hash)
}.should raise_error(TypeError, "wrong argument type MockObject (expected OpenSSL/Digest)")
end
it "raises a RuntimeError for unknown digest algorithms" do
-> {
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: "wd40")
}.should raise_error(RuntimeError, /Unsupported digest algorithm \(wd40\)/)
version_is OpenSSL::VERSION, "4.0.0" do
it "raises a OpenSSL::Digest::DigestError for unknown digest algorithms" do
-> {
OpenSSL::KDF.pbkdf2_hmac("secret", **@defaults, hash: "wd40")
}.should raise_error(OpenSSL::Digest::DigestError, /wd40/)
end
end
it "treats salt as a required keyword" do