File: tc_buffer.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 (217 lines) | stat: -rw-r--r-- 6,108 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#--
# =============================================================================
# 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 'test/unit'
require 'net/ssh/util/buffer'
require 'net/ssh/util/openssl'

class BufferTester

  def initialize( test_case, buffer )
    @buffer = buffer
    @test_case = test_case
  end

  def run_tests
    methods.each do |meth|
      next unless meth =~ /^test_/
      @buffer.clear!
      send meth
    end
  end

end

class WriteBufferTester < BufferTester

  def test_write
    @buffer.write "hello", "\x1\x2\x3\x4"
    @test_case.assert_equal( "hello\x1\x2\x3\x4", @buffer.content )
  end

  def test_write_int64
    @buffer.write_int64 0x1234567898765432
    expect = "\x12\x34\x56\x78\x98\x76\x54\x32"
    @test_case.assert_equal( expect, @buffer.content )
    @buffer.write_int64 1, 2
    expect << "\x00\x00\x00\x00\x00\x00\x00\x01"
    expect << "\x00\x00\x00\x00\x00\x00\x00\x02"
    @test_case.assert_equal( expect, @buffer.content )
  end

  def test_write_long
    @buffer.write_long 0x12ABCDEF, 7
    @test_case.assert_equal( "\x12\xAB\xCD\xEF\x00\x00\x00\x07", @buffer.content )
  end

  def test_write_short
    @buffer.write_short 0x12AB, 7
    @test_case.assert_equal( "\x12\xAB\x00\x07", @buffer.content )
  end

  def test_write_byte
    @buffer.write_byte 0x12, 0xAB, 7
    @test_case.assert_equal( "\x12\xAB\x07", @buffer.content )
  end

  def test_write_string
    @buffer.write_string "hello", "\x01\x02\x03\x04\x05\x06"
    @test_case.assert_equal( "\x00\x00\x00\x05hello\x00\x00\x00\x06\x01\x02\x03\x04\x05\x06", @buffer.content )
  end

  def test_write_bool
    @buffer.write_bool true, false
    @test_case.assert_equal( "\x01\x00", @buffer.content )
  end

  def test_write_bignum
    bn = OpenSSL::BN.new( "1" )
    @buffer.write_bignum OpenSSL::BN.new( "1" ),
      OpenSSL::BN.new( "12345678", 16 ),
      OpenSSL::BN.new( "ABCDEF123456", 16 )
    expect = "\x00\x00\x00\x01\x01" +
             "\x00\x00\x00\x04\x12\x34\x56\x78" +
             "\x00\x00\x00\x07\x00\xAB\xCD\xEF\x12\x34\x56"
    @test_case.assert_equal( expect, @buffer.content )
  end

  def test_write_key
    key = OpenSSL::PKey::DSA.new
    key.p, key.q, key.g, key.pub_key = 1, 2, 3, 4
    @buffer.write_key key
    key = OpenSSL::PKey::RSA.new
    key.e, key.n = 1, 2
    @buffer.write_key key

    expect = "\x00\x00\x00\x07ssh-dss" +
             "\x00\x00\x00\x01\x01" +
             "\x00\x00\x00\x01\x02" +
             "\x00\x00\x00\x01\x03" +
             "\x00\x00\x00\x01\x04" +
             "\x00\x00\x00\x07ssh-rsa" +
             "\x00\x00\x00\x01\x01" +
             "\x00\x00\x00\x01\x02"
    @test_case.assert_equal( expect, @buffer.content )
  end

end

class ReadBufferTester < BufferTester

  def test_append
    @buffer.append "some text"
    @test_case.assert_equal( "some text", @buffer.content )
    @buffer.append "some more text"
    @test_case.assert_equal( "some textsome more text", @buffer.content )
  end

  def test_remainder_as_buffer
    @buffer.append "\x01\x02\x03\x04\x05\x06\x07"
    @buffer.read_byte
    b = @buffer.remainder_as_buffer
    @test_case.assert_equal( "\x02\x03\x04\x05\x06\x07", b.content )
  end

  def test_read
    @buffer.append "\x01\x02\x03\x04\x05\x06\x07"
    @test_case.assert_equal( "\x01", @buffer.read(1) )
    @test_case.assert_equal( "\x02\x03", @buffer.read(2) )
    @test_case.assert_equal( "\x04\x05\x06\x07", @buffer.read )
  end

  def test_read_int64
    @buffer.append "\x12\x34\x56\x78\x98\x76\x54\x32"
    i = @buffer.read_int64
    @test_case.assert_equal( 0x1234567898765432, i )
  end

  def test_read_long
    @buffer.append "\x12\x34\x56\x78\x00\x00\x00\x05"
    i = @buffer.read_long
    j = @buffer.read_long
    k = @buffer.read_long
    @test_case.assert_equal i, 0x12345678
    @test_case.assert_equal j, 5
    @test_case.assert_nil k
  end

  def test_read_short
    @buffer.append "\x12\x34\x00\x05"
    i = @buffer.read_short
    j = @buffer.read_short
    @test_case.assert_equal i, 0x1234
    @test_case.assert_equal j, 5
  end

  def test_read_string
    @buffer.append "\x00\x00\x00\x05hello, world"
    s = @buffer.read_string
    @test_case.assert_equal s, "hello"
  end

  def test_read_bool
    @buffer.append "\x01\x00"
    a = @buffer.read_bool
    b = @buffer.read_bool
    @test_case.assert a
    @test_case.assert !b
  end

end


class TC_WriterBuffer < Test::Unit::TestCase

  def test_buffer
    tester = WriteBufferTester.new( self, Net::SSH::Util::WriterBuffer.new )
    tester.run_tests
  end

end

class TC_ReaderBuffer < Test::Unit::TestCase

  def test_buffer
    tester = ReadBufferTester.new( self, Net::SSH::Util::ReaderBuffer.new( "" ) )
    tester.run_tests
  end

end

class TC_Buffer < Test::Unit::TestCase

  def test_write_buffer
    tester = WriteBufferTester.new( self, Net::SSH::Util::Buffer.new )
    tester.run_tests
  end

  def test_read_buffer
    tester = ReadBufferTester.new( self, Net::SSH::Util::Buffer.new )
    tester.run_tests
  end

end

if $0 == __FILE__
puts <<EOF
==========================================================================
NOTE: this file will run the tests for the BUFFER routines. If you want to
run a comprehensive test involving all of the suites written for Net::SSH,
run the 'tests.rb' file instead.
==========================================================================
EOF
end