summaryrefslogtreecommitdiff
path: root/spec/ruby/library/openssl/digest/shared/update.rb
blob: e5ff9dcb16f6e350bd6c50f62c409aa1c723ec82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require_relative '../../../../library/digest/sha1/shared/constants'
require_relative '../../../../library/digest/sha256/shared/constants'
require_relative '../../../../library/digest/sha384/shared/constants'
require_relative '../../../../library/digest/sha512/shared/constants'
require 'openssl'

describe :openssl_digest_update, shared: true do
  context "when given input as a single string" do
    it "returns a SHA1 digest" do
      digest = OpenSSL::Digest.new('sha1')
      digest.send(@method, SHA1Constants::Contents)
      digest.digest.should == SHA1Constants::Digest
    end

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

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

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

  context "when given input as multiple smaller substrings" do
    it "returns a SHA1 digest" do
      digest = OpenSSL::Digest.new('sha1')
      SHA1Constants::Contents.each_char { |b| digest.send(@method, b) }
      digest.digest.should == SHA1Constants::Digest
    end

    it "returns a SHA256 digest" do
      digest = OpenSSL::Digest.new('sha256')
      SHA256Constants::Contents.each_char { |b| digest.send(@method, b) }
      digest.digest.should == SHA256Constants::Digest
    end

    it "returns a SHA384 digest" do
      digest = OpenSSL::Digest.new('sha384')
      SHA384Constants::Contents.each_char { |b| digest.send(@method, b) }
      digest.digest.should == SHA384Constants::Digest
    end

    it "returns a SHA512 digest" do
      digest = OpenSSL::Digest.new('sha512')
      SHA512Constants::Contents.each_char { |b| digest.send(@method, b) }
      digest.digest.should == SHA512Constants::Digest
    end
  end

  context "when input is not a String and responds to #to_str" do
    it "returns a SHA1 digest" do
      str = mock('str')
      str.should_receive(:to_str).and_return(SHA1Constants::Contents)
      digest = OpenSSL::Digest.new('sha1')
      digest.send(@method, str)
      digest.digest.should == SHA1Constants::Digest
    end

    it "returns a SHA256 digest" do
      str = mock('str')
      str.should_receive(:to_str).and_return(SHA256Constants::Contents)
      digest = OpenSSL::Digest.new('sha256')
      digest.send(@method, str)
      digest.digest.should == SHA256Constants::Digest
    end

    it "returns a SHA384 digest" do
      str = mock('str')
      str.should_receive(:to_str).and_return(SHA384Constants::Contents)
      digest = OpenSSL::Digest.new('sha384')
      digest.send(@method, str)
      digest.digest.should == SHA384Constants::Digest
    end

    it "returns a SHA512 digest" do
      str = mock('str')
      str.should_receive(:to_str).and_return(SHA512Constants::Contents)
      digest = OpenSSL::Digest.new('sha512')
      digest.send(@method, str)
      digest.digest.should == SHA512Constants::Digest
    end
  end

  context "when input is not a String and does not respond to #to_str" do
    it "raises a TypeError with SHA1" do
      digest = OpenSSL::Digest.new('sha1')
      -> {
        digest.send(@method, Object.new)
      }.should raise_error(TypeError, 'no implicit conversion of Object into String')
    end

    it "raises a TypeError with SHA256" do
      digest = OpenSSL::Digest.new('sha256')
      -> {
        digest.send(@method, Object.new)
      }.should raise_error(TypeError, 'no implicit conversion of Object into String')
    end

    it "raises a TypeError with SHA384" do
      digest = OpenSSL::Digest.new('sha384')
      -> {
        digest.send(@method, Object.new)
      }.should raise_error(TypeError, 'no implicit conversion of Object into String')
    end

    it "raises a TypeError with SHA512" do
      digest = OpenSSL::Digest.new('sha512')
      -> {
        digest.send(@method, Object.new)
      }.should raise_error(TypeError, 'no implicit conversion of Object into String')
    end
  end
end