Skip to content

Commit

Permalink
Import OpenSSL digest specs
Browse files Browse the repository at this point in the history
Imported from ruby/spec#1054 and subject to
change.
They have been imported into the natalie folder to break a dependeny on
the change request of ruby-spec.
  • Loading branch information
herwinw committed Aug 29, 2023
1 parent eb1e592 commit bfb23d0
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/natalie/openssl_digest/append_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_relative '../../../spec/spec_helper'
require_relative 'shared/update'

describe "OpenSSL::Digest#<<" do
it_behaves_like :openssl_digest_update, :<<
end
44 changes: 44 additions & 0 deletions test/natalie/openssl_digest/block_length_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative '../../../spec/spec_helper'
require_relative '../../../spec/library/digest/sha1/shared/constants'
require_relative '../../../spec/library/digest/sha256/shared/constants'
require_relative '../../../spec/library/digest/sha384/shared/constants'
require_relative '../../../spec/library/digest/sha512/shared/constants'
require 'openssl'

describe "OpenSSL::Digest#block_length" do
describe "block_length of OpenSSL::Digest.new(name)" do
it "returns a SHA1 block length" do
OpenSSL::Digest.new('sha1').block_length.should == SHA1Constants::BlockLength
end

it "returns a SHA256 block length" do
OpenSSL::Digest.new('sha256').block_length.should == SHA256Constants::BlockLength
end

it "returns a SHA384 block_length" do
OpenSSL::Digest.new('sha384').block_length.should == SHA384Constants::BlockLength
end

it "returns a SHA512 block_length" do
OpenSSL::Digest.new('sha512').block_length.should == SHA512Constants::BlockLength
end
end

describe "block_length of subclasses" do
it "returns a SHA1 block length" do
OpenSSL::Digest::SHA1.new.block_length.should == SHA1Constants::BlockLength
end

it "returns a SHA256 block length" do
OpenSSL::Digest::SHA256.new.block_length.should == SHA256Constants::BlockLength
end

it "returns a SHA384 block_length" do
OpenSSL::Digest::SHA384.new.block_length.should == SHA384Constants::BlockLength
end

it "returns a SHA512 block_length" do
OpenSSL::Digest::SHA512.new.block_length.should == SHA512Constants::BlockLength
end
end
end
44 changes: 44 additions & 0 deletions test/natalie/openssl_digest/digest_length_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative '../../../spec/spec_helper'
require_relative '../../../spec/library/digest/sha1/shared/constants'
require_relative '../../../spec/library/digest/sha256/shared/constants'
require_relative '../../../spec/library/digest/sha384/shared/constants'
require_relative '../../../spec/library/digest/sha512/shared/constants'
require 'openssl'

describe "OpenSSL::Digest#digest_length" do
describe "digest_length of OpenSSL::Digest.new(name)" do
it "returns a SHA1 block length" do
OpenSSL::Digest.new('sha1').digest_length.should == SHA1Constants::DigestLength
end

it "returns a SHA256 block length" do
OpenSSL::Digest.new('sha256').digest_length.should == SHA256Constants::DigestLength
end

it "returns a SHA384 digest_length" do
OpenSSL::Digest.new('sha384').digest_length.should == SHA384Constants::DigestLength
end

it "returns a SHA512 digest_length" do
OpenSSL::Digest.new('sha512').digest_length.should == SHA512Constants::DigestLength
end
end

describe "digest_length of subclasses" do
it "returns a SHA1 block length" do
OpenSSL::Digest::SHA1.new.digest_length.should == SHA1Constants::DigestLength
end

it "returns a SHA256 block length" do
OpenSSL::Digest::SHA256.new.digest_length.should == SHA256Constants::DigestLength
end

it "returns a SHA384 digest_length" do
OpenSSL::Digest::SHA384.new.digest_length.should == SHA384Constants::DigestLength
end

it "returns a SHA512 digest_length" do
OpenSSL::Digest::SHA512.new.digest_length.should == SHA512Constants::DigestLength
end
end
end
62 changes: 62 additions & 0 deletions test/natalie/openssl_digest/digest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require_relative '../../../spec/spec_helper'
require_relative '../../../spec/library/digest/sha1/shared/constants'
require_relative '../../../spec/library/digest/sha256/shared/constants'
require_relative '../../../spec/library/digest/sha384/shared/constants'
require_relative '../../../spec/library/digest/sha512/shared/constants'
require 'openssl'

describe "OpenSSL::Digest class methods" do
describe ".digest" do
it "returns a SHA1 digest" do
OpenSSL::Digest.digest('sha1', SHA1Constants::Contents).should == SHA1Constants::Digest
end

it "returns a SHA256 digest" do
OpenSSL::Digest.digest('sha256', SHA256Constants::Contents).should == SHA256Constants::Digest
end

it "returns a SHA384 digest" do
OpenSSL::Digest.digest('sha384', SHA384Constants::Contents).should == SHA384Constants::Digest
end

it "returns a SHA512 digest" do
OpenSSL::Digest.digest('sha512', SHA512Constants::Contents).should == SHA512Constants::Digest
end
end

describe ".hexdigest" do
it "returns a SHA1 hexdigest" do
OpenSSL::Digest.hexdigest('sha1', SHA1Constants::Contents).should == SHA1Constants::Hexdigest
end

it "returns a SHA256 hexdigest" do
OpenSSL::Digest.hexdigest('sha256', SHA256Constants::Contents).should == SHA256Constants::Hexdigest
end

it "returns a SHA384 hexdigest" do
OpenSSL::Digest.hexdigest('sha384', SHA384Constants::Contents).should == SHA384Constants::Hexdigest
end

it "returns a SHA512 hexdigest" do
OpenSSL::Digest.hexdigest('sha512', SHA512Constants::Contents).should == SHA512Constants::Hexdigest
end
end

describe ".base64digest" do
it "returns a SHA1 base64digest" do
OpenSSL::Digest.base64digest('sha1', SHA1Constants::Contents).should == SHA1Constants::Base64digest
end

it "returns a SHA256 base64digest" do
OpenSSL::Digest.base64digest('sha256', SHA256Constants::Contents).should == SHA256Constants::Base64digest
end

it "returns a SHA384 base64digest" do
OpenSSL::Digest.base64digest('sha384', SHA384Constants::Contents).should == SHA384Constants::Base64digest
end

it "returns a SHA512 base64digest" do
OpenSSL::Digest.base64digest('sha512', SHA512Constants::Contents).should == SHA512Constants::Base64digest
end
end
end
141 changes: 141 additions & 0 deletions test/natalie/openssl_digest/initialize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
require_relative '../../../spec/spec_helper'
require_relative '../../../spec/library/digest/sha1/shared/constants'
require_relative '../../../spec/library/digest/sha256/shared/constants'
require_relative '../../../spec/library/digest/sha384/shared/constants'
require_relative '../../../spec/library/digest/sha512/shared/constants'
require 'openssl'

describe "OpenSSL::Digest initialization" do
describe "can be called with a digest name" do
it "returns a SHA1 object" do
OpenSSL::Digest.new("sha1").name.should == "SHA1"
end

it "returns a SHA256 object" do
OpenSSL::Digest.new("sha256").name.should == "SHA256"
end

it "returns a SHA384 object" do
OpenSSL::Digest.new("sha384").name.should == "SHA384"
end

it "returns a SHA512 object" 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\)/)
end

it "cannot be called with a symbol" do
-> { OpenSSL::Digest.new(:SHA1) }.should raise_error(TypeError, /wrong argument type Symbol/)
end

it "doest 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/)
end
end

describe "can be called with a digest object" do
it "returns a SHA1 object" do
OpenSSL::Digest.new(OpenSSL::Digest::SHA1.new).name.should == "SHA1"
end

it "returns a SHA256 object" do
OpenSSL::Digest.new(OpenSSL::Digest::SHA256.new).name.should == "SHA256"
end

it "returns a SHA384 object" do
OpenSSL::Digest.new(OpenSSL::Digest::SHA384.new).name.should == "SHA384"
end

it "returns a SHA512 object" do
OpenSSL::Digest.new(OpenSSL::Digest::SHA512.new).name.should == "SHA512"
end

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

it "ignores the state of the name object" do
sha1 = OpenSSL::Digest.new('sha1', SHA1Constants::Contents)
OpenSSL::Digest.new(sha1).digest.should == SHA1Constants::BlankDigest
end
end

describe "ititialization with an empty string" do
it "returns a SHA1 digest" do
OpenSSL::Digest.new("sha1").digest.should == SHA1Constants::BlankDigest
end

it "returns a SHA256 digest" do
OpenSSL::Digest.new("sha256").digest.should == SHA256Constants::BlankDigest
end

it "returns a SHA384 digest" do
OpenSSL::Digest.new("sha384").digest.should == SHA384Constants::BlankDigest
end

it "returns a SHA512 digest" do
OpenSSL::Digest.new("sha512").digest.should == SHA512Constants::BlankDigest
end
end

describe "can be called with a digest name and data" do
it "returns a SHA1 digest" do
OpenSSL::Digest.new("sha1", SHA1Constants::Contents).digest.should == SHA1Constants::Digest
end

it "returns a SHA256 digest" do
OpenSSL::Digest.new("sha256", SHA256Constants::Contents).digest.should == SHA256Constants::Digest
end

it "returns a SHA384 digest" do
OpenSSL::Digest.new("sha384", SHA384Constants::Contents).digest.should == SHA384Constants::Digest
end

it "returns a SHA512 digest" do
OpenSSL::Digest.new("sha512", SHA512Constants::Contents).digest.should == SHA512Constants::Digest
end
end

context "can be called on subclasses" do
describe "can be called without data on subclasses" do
it "returns a SHA1 digest" do
OpenSSL::Digest::SHA1.new.digest.should == SHA1Constants::BlankDigest
end

it "returns a SHA256 digest" do
OpenSSL::Digest::SHA256.new.digest.should == SHA256Constants::BlankDigest
end

it "returns a SHA384 digest" do
OpenSSL::Digest::SHA384.new.digest.should == SHA384Constants::BlankDigest
end

it "returns a SHA512 digest" do
OpenSSL::Digest::SHA512.new.digest.should == SHA512Constants::BlankDigest
end
end

describe "can be called with data on subclasses" do
it "returns a SHA1 digest" do
OpenSSL::Digest::SHA1.new(SHA1Constants::Contents).digest.should == SHA1Constants::Digest
end

it "returns a SHA256 digest" do
OpenSSL::Digest::SHA256.new(SHA256Constants::Contents).digest.should == SHA256Constants::Digest
end

it "returns a SHA384 digest" do
OpenSSL::Digest::SHA384.new(SHA384Constants::Contents).digest.should == SHA384Constants::Digest
end

it "returns a SHA512 digest" do
OpenSSL::Digest::SHA512.new(SHA512Constants::Contents).digest.should == SHA512Constants::Digest
end
end
end
end
16 changes: 16 additions & 0 deletions test/natalie/openssl_digest/name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_relative '../../../spec/spec_helper'
require 'openssl'

describe "OpenSSL::Digest#name" do
it "returns the name of digest when called on an instance" do
OpenSSL::Digest.new('SHA1').name.should == 'SHA1'
end

it "converts the name to the internal representation of OpenSSL" do
OpenSSL::Digest.new('sha1').name.should == 'SHA1'
end

it "works on subclasses too" do
OpenSSL::Digest::SHA1.new.name.should == 'SHA1'
end
end
38 changes: 38 additions & 0 deletions test/natalie/openssl_digest/reset_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative '../../../spec/spec_helper'
require_relative '../../../spec/library/digest/sha1/shared/constants'
require_relative '../../../spec/library/digest/sha256/shared/constants'
require_relative '../../../spec/library/digest/sha384/shared/constants'
require_relative '../../../spec/library/digest/sha512/shared/constants'
require 'openssl'

describe "OpenSSL::Digest#reset" do
describe "resets the state of the digest" do
it "works for a SHA1 digest" do
digest = OpenSSL::Digest.new('sha1', SHA1Constants::Contents)
digest.reset
digest.update(SHA1Constants::Contents)
digest.digest.should == SHA1Constants::Digest
end

it "works for a SHA256 digest" do
digest = OpenSSL::Digest.new('sha256', SHA256Constants::Contents)
digest.reset
digest.update(SHA256Constants::Contents)
digest.digest.should == SHA256Constants::Digest
end

it "works for a SHA384 digest" do
digest = OpenSSL::Digest.new('sha384', SHA384Constants::Contents)
digest.reset
digest.update(SHA384Constants::Contents)
digest.digest.should == SHA384Constants::Digest
end

it "works for a SHA512 digest" do
digest = OpenSSL::Digest.new('sha512', SHA512Constants::Contents)
digest.reset
digest.update(SHA512Constants::Contents)
digest.digest.should == SHA512Constants::Digest
end
end
end
Loading

0 comments on commit bfb23d0

Please sign in to comment.