Skip to content

Commit f7d9f1b

Browse files
committed
Add specs for OpenSSL::Digest#initialize
1 parent edb3da0 commit f7d9f1b

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
require_relative '../../../spec_helper'
2+
require_relative '../../../library/digest/sha1/shared/constants'
3+
require_relative '../../../library/digest/sha256/shared/constants'
4+
require_relative '../../../library/digest/sha384/shared/constants'
5+
require_relative '../../../library/digest/sha512/shared/constants'
6+
require 'openssl'
7+
8+
describe "OpenSSL::Digest initialization" do
9+
describe "can be called with a digest name" do
10+
it "returns a SHA1 object" do
11+
OpenSSL::Digest.new("sha1").name.should == "SHA1"
12+
end
13+
14+
it "returns a SHA256 object" do
15+
OpenSSL::Digest.new("sha256").name.should == "SHA256"
16+
end
17+
18+
it "returns a SHA384 object" do
19+
OpenSSL::Digest.new("sha384").name.should == "SHA384"
20+
end
21+
22+
it "returns a SHA512 object" do
23+
OpenSSL::Digest.new("sha512").name.should == "SHA512"
24+
end
25+
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\)/)
28+
end
29+
30+
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 "doest 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/)
38+
end
39+
end
40+
41+
describe "can be called with a digest object" do
42+
it "returns a SHA1 object" do
43+
OpenSSL::Digest.new(OpenSSL::Digest::SHA1.new).name.should == "SHA1"
44+
end
45+
46+
it "returns a SHA256 object" do
47+
OpenSSL::Digest.new(OpenSSL::Digest::SHA256.new).name.should == "SHA256"
48+
end
49+
50+
it "returns a SHA384 object" do
51+
OpenSSL::Digest.new(OpenSSL::Digest::SHA384.new).name.should == "SHA384"
52+
end
53+
54+
it "returns a SHA512 object" do
55+
OpenSSL::Digest.new(OpenSSL::Digest::SHA512.new).name.should == "SHA512"
56+
end
57+
58+
it "cannot be called with a digest class" do
59+
-> { OpenSSL::Digest.new(OpenSSL::Digest::SHA1) }.should raise_error(TypeError, /wrong argument type Class/)
60+
end
61+
62+
it "ignores the state of the name object" do
63+
sha1 = OpenSSL::Digest.new('sha1', SHA1Constants::Contents)
64+
OpenSSL::Digest.new(sha1).digest.should == SHA1Constants::BlankDigest
65+
end
66+
end
67+
68+
describe "ititialization with an empty string" do
69+
it "returns a SHA1 digest" do
70+
OpenSSL::Digest.new("sha1").digest.should == SHA1Constants::BlankDigest
71+
end
72+
73+
it "returns a SHA256 digest" do
74+
OpenSSL::Digest.new("sha256").digest.should == SHA256Constants::BlankDigest
75+
end
76+
77+
it "returns a SHA384 digest" do
78+
OpenSSL::Digest.new("sha384").digest.should == SHA384Constants::BlankDigest
79+
end
80+
81+
it "returns a SHA512 digest" do
82+
OpenSSL::Digest.new("sha512").digest.should == SHA512Constants::BlankDigest
83+
end
84+
end
85+
86+
describe "can be called with a digest name and data" do
87+
it "returns a SHA1 digest" do
88+
OpenSSL::Digest.new("sha1", SHA1Constants::Contents).digest.should == SHA1Constants::Digest
89+
end
90+
91+
it "returns a SHA256 digest" do
92+
OpenSSL::Digest.new("sha256", SHA256Constants::Contents).digest.should == SHA256Constants::Digest
93+
end
94+
95+
it "returns a SHA384 digest" do
96+
OpenSSL::Digest.new("sha384", SHA384Constants::Contents).digest.should == SHA384Constants::Digest
97+
end
98+
99+
it "returns a SHA512 digest" do
100+
OpenSSL::Digest.new("sha512", SHA512Constants::Contents).digest.should == SHA512Constants::Digest
101+
end
102+
end
103+
104+
context "can be called on subclasses" do
105+
describe "can be called without data on subclasses" do
106+
it "returns a SHA1 digest" do
107+
OpenSSL::Digest::SHA1.new.digest.should == SHA1Constants::BlankDigest
108+
end
109+
110+
it "returns a SHA256 digest" do
111+
OpenSSL::Digest::SHA256.new.digest.should == SHA256Constants::BlankDigest
112+
end
113+
114+
it "returns a SHA384 digest" do
115+
OpenSSL::Digest::SHA384.new.digest.should == SHA384Constants::BlankDigest
116+
end
117+
118+
it "returns a SHA512 digest" do
119+
OpenSSL::Digest::SHA512.new.digest.should == SHA512Constants::BlankDigest
120+
end
121+
end
122+
123+
describe "can be called with data on subclasses" do
124+
it "returns a SHA1 digest" do
125+
OpenSSL::Digest::SHA1.new(SHA1Constants::Contents).digest.should == SHA1Constants::Digest
126+
end
127+
128+
it "returns a SHA256 digest" do
129+
OpenSSL::Digest::SHA256.new(SHA256Constants::Contents).digest.should == SHA256Constants::Digest
130+
end
131+
132+
it "returns a SHA384 digest" do
133+
OpenSSL::Digest::SHA384.new(SHA384Constants::Contents).digest.should == SHA384Constants::Digest
134+
end
135+
136+
it "returns a SHA512 digest" do
137+
OpenSSL::Digest::SHA512.new(SHA512Constants::Contents).digest.should == SHA512Constants::Digest
138+
end
139+
end
140+
end
141+
end

0 commit comments

Comments
 (0)