summaryrefslogtreecommitdiff
path: root/test/digest/test_digest_extend.rb
blob: bd599f2d2042244ea58a446aa8b09dd2ce8b2460 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: false
require 'test/unit'
require 'digest'
require_relative '../lib/with_different_ofs'

class TestDigestExtend < Test::Unit::TestCase
  extend DifferentOFS

  class MyDigest < Digest::Class
    def initialize(*arg)
      super
      @buf = []
    end

    def initialize_copy(org)
      @buf = org.buf.dup
    end

    def update(arg)
      @buf << arg
      self
    end

    alias << update

    def finish
      (@buf.join('').length % 256).chr
    end

    def reset
      @buf.clear
      self
    end

  protected

    def buf
      @buf
    end
  end

  def setup
    @MyDigest = Class.new(MyDigest)
  end

  def test_digest_s_hexencode
    assert_equal('', Digest.hexencode(''))
    assert_equal('0102', Digest.hexencode("\1\2"))
    assert_equal(
      (0..0xff).to_a.map { |c| sprintf("%02x", c ) }.join(''),
      Digest.hexencode((0..0xff).to_a.map { |c| c.chr }.join(''))
    )
    assert_equal(Encoding::US_ASCII, Digest.hexencode("\1\2").encoding)
  end

  def test_class_reset
    a = Digest::SHA1.new
    base = a.to_s
    assert_equal(base, a.reset.to_s)
    b = a.new
    assert_equal(base, b.to_s)
    b.update('1')
    assert_not_equal(base, b.to_s)
    assert_equal(base, b.reset.to_s)
  end

  def test_digest
    assert_equal("\3", MyDigest.digest("foo"))
  end

  def test_hexdigest
    assert_equal("03", @MyDigest.hexdigest("foo"))
  end

  def test_context
    digester = @MyDigest.new
    digester.update("foo")
    assert_equal("\3", digester.digest)
    digester.update("foobar")
    assert_equal("\t", digester.digest)
    digester.update("foo")
    assert_equal("\f", digester.digest)
  end

  def test_new
    a = Digest::SHA1.new
    b = a.new
    obj = a.to_s
    assert_equal(obj, a.to_s)
    assert_equal(obj, b.to_s)
    a.update('1')
    assert_not_equal(obj, a.to_s)
    assert_equal(obj, b.to_s)
  end

  def test_digest_hexdigest
    [:digest, :hexdigest].each do |m|
      exp_1st = "\3"; exp_1st = Digest.hexencode(exp_1st) if m == :hexdigest
      exp_2nd = "\6"; exp_2nd = Digest.hexencode(exp_2nd) if m == :hexdigest
      digester = @MyDigest.new
      digester.update("foo")
      obj = digester.send(m)
      # digest w/o param does not reset the org digester.
      assert_equal(exp_1st, obj)
      digester.update("bar")
      obj = digester.send(m)
      assert_equal(exp_2nd, obj)
      obj = digester.send(m, "baz")
      # digest with param resets the org digester.
      assert_equal(exp_1st, obj)
    end
  end

  def test_digest_hexdigest_bang
    [:digest!, :hexdigest!].each do |m|
      exp_1st = "\3"; exp_1st = Digest.hexencode(exp_1st) if m == :hexdigest!
      digester = @MyDigest.new
      digester.update("foo")
      obj = digester.send(m) # digest! always resets the org digester.
      assert_equal(exp_1st, obj)
      digester.update("bar")
      obj = digester.send(m)
      assert_equal(exp_1st, obj)
    end
  end

  def test_to_s
    digester = @MyDigest.new
    digester.update("foo")
    assert_equal("03", digester.to_s)
  end

  def test_length
    @MyDigest.class_eval do
      def digest_length
        2
      end
    end
    digester = @MyDigest.new
    assert_equal(2, digester.length)
    assert_equal(2, digester.size)
  end

  def test_digest_length # breaks @MyDigest#digest_length
    assert_equal(1, @MyDigest.new.digest_length)
    @MyDigest.class_eval do
      def digest_length
        2
      end
    end
    assert_equal(2, @MyDigest.new.digest_length)
  end

  def test_block_length
    assert_raise(RuntimeError) do
      @MyDigest.new.block_length
    end
  end
end