summaryrefslogtreecommitdiff
path: root/test/transport/test_hmac.rb
blob: 55c46554244e3001cf654aa188a51c962c4fb6e5 (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
require 'common'
require 'net/ssh/transport/hmac'

module Transport

  class TestHMAC < NetSSHTest
    Net::SSH::Transport::HMAC::MAP.each do |name, value|
      method = name.tr("-", "_")
      define_method("test_get_with_#{method}_returns_new_hmac_instance") do
        key = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!&$%"[0,Net::SSH::Transport::HMAC::MAP[name].key_length]
        hmac = Net::SSH::Transport::HMAC.get(name, key, { :shared => "123", :hash => "^&*", :digester => OpenSSL::Digest::SHA1 })
        assert_instance_of Net::SSH::Transport::HMAC::MAP[name], hmac
        assert_equal key, hmac.key
      end

      define_method("test_key_length_with_#{method}_returns_correct_key_length") do
        assert_equal Net::SSH::Transport::HMAC::MAP[name].key_length, Net::SSH::Transport::HMAC.key_length(name)
      end
    end

    def test_get_with_unrecognized_hmac_raises_argument_error
      assert_raises(ArgumentError) do
        Net::SSH::Transport::HMAC.get("bogus")
      end
    end

    def test_key_length_with_unrecognized_hmac_raises_argument_error
      assert_raises(ArgumentError) do
        Net::SSH::Transport::HMAC.get("bogus")
      end
    end
  end

end