ext/digest/test.rb


DEFINITIONS

This source file includes following functions.


   1  #!/usr/bin/env ruby
   2  #
   3  # $RoughId: test.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
   4  # $Id: test.rb,v 1.1 2001/07/13 20:06:13 knu Exp $
   5  
   6  require 'runit/testcase'
   7  require 'runit/cui/testrunner'
   8  
   9  require 'digest/md5'
  10  require 'digest/rmd160'
  11  require 'digest/sha1'
  12  require 'digest/sha2'
  13  include Digest
  14  
  15  class TestDigest < RUNIT::TestCase
  16    ALGOS = [
  17      MD5,
  18      SHA1,
  19      SHA256,
  20      SHA384,
  21      SHA512,
  22      RMD160
  23    ]
  24  
  25    DATA = {
  26      "abc" => {
  27        MD5 => "900150983cd24fb0d6963f7d28e17f72",
  28        SHA1 => "a9993e364706816aba3e25717850c26c9cd0d89d",
  29        SHA256 => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
  30        SHA384 => "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
  31        SHA512 => "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
  32        RMD160 => "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"
  33      },
  34  
  35      "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" => {
  36        MD5 => "8215ef0796a20bcaaae116d3876c664a",
  37        SHA1 => "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  38        SHA256 => "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
  39        SHA384 => "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b",
  40        SHA512 => "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445",
  41        RMD160 => "12a053384a9c0c88e405a06c27dcf49ada62eb2b"
  42      }
  43    }
  44  
  45    def test_s_hexdigest
  46      ALGOS.each do |algo|
  47        DATA.each do |str, table|
  48          assert_equal(table[algo], algo.hexdigest(str))
  49        end
  50      end
  51    end
  52  
  53    def test_s_digest
  54      ALGOS.each do |algo|
  55        DATA.each do |str, table|
  56          assert_equal([table[algo]].pack("H*"), algo.digest(str))
  57        end
  58      end
  59    end
  60  
  61    def test_update
  62      # This test is also for digest() and hexdigest()
  63  
  64      str = "ABC"
  65  
  66      ALGOS.each do |algo|
  67        md = algo.new
  68        md.update str
  69        assert_equal(algo.hexdigest(str), md.hexdigest)
  70        assert_equal(algo.digest(str), md.digest)
  71      end
  72    end
  73  
  74    def test_eq
  75      # This test is also for clone()
  76  
  77      ALGOS.each do |algo|
  78        md1 = algo.new("ABC")
  79  
  80        assert_equal(md1, md1.clone)
  81  
  82        md2 = algo.new
  83        md2 << "A"
  84  
  85        assert(md1 != md2)
  86  
  87        md2 << "BC"
  88  
  89        assert_equal(md1, md2)
  90      end
  91    end
  92  end
  93  
  94  if $0 == __FILE__
  95    suite = RUNIT::TestSuite.new
  96  
  97    suite.add_test(TestDigest.suite)
  98  
  99    RUNIT::CUI::TestRunner.run(suite)
 100  end