File: tc_hostbased.rb

package info (click to toggle)
libnet-ssh-ruby 1.1.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,472 kB
  • ctags: 2,465
  • sloc: ruby: 10,848; makefile: 17
file content (136 lines) | stat: -rw-r--r-- 4,206 bytes parent folder | download | duplicates (3)
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
#--
# =============================================================================
# Copyright (c) 2004,2005 Jamis Buck (jamis@37signals.com)
# All rights reserved.
#
# This source file is distributed as part of the Net::SSH Secure Shell Client
# library for Ruby. This file (and the library as a whole) may be used only as
# allowed by either the BSD license, or the Ruby license (or, by association
# with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
# distribution for the texts of these licenses.
# -----------------------------------------------------------------------------
# net-ssh website : http://net-ssh.rubyforge.org
# project website: http://rubyforge.org/projects/net-ssh
# =============================================================================
#++

$:.unshift "#{File.dirname(__FILE__)}/../../../lib"

require 'net/ssh/userauth/methods/hostbased'
require 'net/ssh/util/buffer'
require 'test/unit'
require 'ostruct'

class TC_Methods_HostBased < Test::Unit::TestCase

  class Buffers
    def writer
      Net::SSH::Util::WriterBuffer.new
    end
  end

  class Key < OpenStruct
    def initialize( e, n )
      super( :ssh_type => "ssh-rsa",
             :e => OpenStruct.new( :to_ssh => [ e ].pack("N") ),
             :n => OpenStruct.new( :to_ssh => [ n ].pack("N") ) )
    end
  end

  class Messenger
    attr_reader :data
    attr_reader :messages

    def initialize
      @data = []
      @messages = []
    end

    def send_message( msg )
      @messages << msg.to_s
    end

    def wait_for_message
      @data.shift
    end
  end

  class KeyManager
    attr_reader :host_identities
    attr_reader :state
    attr_reader :sigdata

    def initialize( *identities )
      @host_identities = identities.flatten
      @state = :open
    end

    def sign( identity, data )
      @sigdata = [ identity, data.to_s ]
      "<signature>"
    end

    def finish
      @state = :finished
    end
  end

  def setup
    ENV["USER"] = "test_client_user"
    buffers = Buffers.new
    @messenger = Messenger.new
    @method = Net::SSH::UserAuth::Methods::HostBased.new( buffers )
    @method.messenger = @messenger
    @method.session_id = "test"
    @method.hostname = "test.host"
  end

  def test_authenticate_no_key_manager
    assert !@method.authenticate( "test", "test_user" )
    assert @messenger.messages.empty?
  end

  def test_authenticate_no_identities
    manager = KeyManager.new
    assert !@method.authenticate( "test", "test_user", :key_manager => manager )
    assert_equal :finished, manager.state
  end

  def test_authenticate_success
    manager = KeyManager.new( Key.new( 0x01010101, 0x02020202 ) )
    @messenger.data.concat [ OpenStruct.new( :message_type => 52 ) ]
    assert @method.authenticate( "test", "test_user", :key_manager => manager )
    assert_equal :finished, manager.state
    assert_equal 1, @messenger.messages.length

    sig = "#{50.chr}\0\0\0\11test_user\0\0\0\4test\0\0\0\11hostbased\0\0\0\7ssh-rsa" +
          "\0\0\0\23\0\0\0\7ssh-rsa\1\1\1\1\2\2\2\2" +
          "\0\0\0\12test.host.\0\0\0\20test_client_user"
    packet1 = sig + "\0\0\0\13<signature>"
    assert_equal packet1, @messenger.messages[0]

    sigdata = manager.sigdata
    assert_equal "\0\0\0\4test" + sig, sigdata[1]
  end

  def test_authenticate_fail
    manager = KeyManager.new( Key.new( 0x01010101, 0x02020202 ),
                              Key.new( 0x03030303, 0x04040404 ) )
    @messenger.data.concat [ OpenStruct.new( :message_type => 51 ),
                             OpenStruct.new( :message_type => 51 ) ]
    assert !@method.authenticate( "test", "test_user", :key_manager => manager )
    assert_equal :finished, manager.state
    assert_equal 2, @messenger.messages.length
  end

  def test_authenticate_acceptible_identities_error
    manager = KeyManager.new( Key.new( 0x01010101, 0x02020202 ) )
    @messenger.data.concat [ OpenStruct.new( :message_type => 60 ),
                             OpenStruct.new( :message_type => 0 ) ]
    assert_raise( Net::SSH::Exception ) do
      @method.authenticate( "test", "test_user", :key_manager => manager )
    end
    assert_equal 1, @messenger.messages.length
  end

end