summaryrefslogtreecommitdiff
path: root/test/digest/test_digest_extend.rb
blob: 9b2e59cdf050b47b405f4a30371af9b1e471d71a (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
require 'test/unit'
require 'digest'

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

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

    alias << update

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

    def reset
      @buf.clear
      self
    end
  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("\6", digester.digest)
    digester.update("foo")
    assert_equal("\3", digester.digest)
  end

  def test_to_s
    digester = MyDigest.new
    digester.update("foo")
    assert_equal("03", digester.to_s)
  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_raises(RuntimeError) do
      MyDigest.new.block_length
    end
  end
end