summaryrefslogtreecommitdiff
path: root/test/transport/test_state.rb
blob: bc9b00d694be13b184fa0fe30116a384daf63a84 (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
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
# encoding: ASCII-8BIT

require 'common'
require 'net/ssh/transport/state'

module Transport
  class TestState < NetSSHTest
    def setup
      @socket = @state = @deflater = @inflater = nil
    end

    def teardown
      if @deflater
        @deflater.finish if !@deflater.finished?
        @deflater.close
      end

      if @inflater
        @inflater.finish if !@inflater.finished?
        @inflater.close
      end

      state.cleanup
    end

    def test_constructor_should_initialize_all_values
      assert_equal 0, state.sequence_number
      assert_equal 0, state.packets
      assert_equal 0, state.blocks

      assert_nil state.compression
      assert_nil state.compression_level
      assert_nil state.max_packets
      assert_nil state.max_blocks
      assert_nil state.rekey_limit

      assert_equal "identity", state.cipher.name
      assert_instance_of Net::SSH::Transport::HMAC::None, state.hmac
    end

    def test_increment_should_increment_counters
      state.increment(24)
      assert_equal 1, state.sequence_number
      assert_equal 1, state.packets
      assert_equal 3, state.blocks
    end

    def test_reset_should_reset_counters_and_fix_defaults_for_maximums
      state.increment(24)
      state.reset!
      assert_equal 1, state.sequence_number
      assert_equal 0, state.packets
      assert_equal 0, state.blocks
      assert_equal 2147483648, state.max_packets
      assert_equal 134217728, state.max_blocks
    end

    def test_set_should_set_variables_and_reset_counters
      state.expects(:reset!)
      state.set cipher: :a, hmac: :b, compression: :c,
                compression_level: :d, max_packets: 500, max_blocks: 1000,
                rekey_limit: 1500
      assert_equal :a, state.cipher
      assert_equal :b, state.hmac
      assert_equal :c, state.compression
      assert_equal :d, state.compression_level
      assert_equal 500, state.max_packets
      assert_equal 1000, state.max_blocks
      assert_equal 1500, state.rekey_limit
    end

    def test_set_with_max_packets_should_respect_max_packets_setting
      state.set max_packets: 500
      assert_equal 500, state.max_packets
    end

    def test_set_with_max_blocks_should_respect_max_blocks_setting
      state.set max_blocks: 1000
      assert_equal 1000, state.max_blocks
    end

    def test_set_with_rekey_limit_should_include_rekey_limit_in_computation_of_max_blocks
      state.set rekey_limit: 4000
      assert_equal 500, state.max_blocks
    end

    def test_compressor_defaults_to_default_zlib_compression
      expect = deflater.deflate("hello world")
      assert_equal expect, state.compressor.deflate("hello world")
    end

    def test_compressor_uses_compression_level_when_given
      state.set compression_level: 1
      expect = deflater(1).deflate("hello world")
      assert_equal expect, state.compressor.deflate("hello world")
    end

    def test_compress_when_no_compression_is_active_returns_text
      assert_equal "hello everybody", state.compress("hello everybody")
    end

    def test_decompress_when_no_compression_is_active_returns_text
      assert_equal "hello everybody", state.decompress("hello everybody")
    end

    def test_compress_when_compression_is_delayed_and_no_auth_hint_is_set_should_return_text
      state.set compression: :delayed
      assert_equal "hello everybody", state.compress("hello everybody")
    end

    def test_decompress_when_compression_is_delayed_and_no_auth_hint_is_set_should_return_text
      state.set compression: :delayed
      assert_equal "hello everybody", state.decompress("hello everybody")
    end

    def test_compress_when_compression_is_enabled_should_return_compressed_text
      state.set compression: :standard
      # JRuby Zlib implementation (1.4 & 1.5) does not have byte-to-byte compatibility with MRI's.
      # skip this test under JRuby.
      return if defined?(JRUBY_VERSION)

      assert_equal "x\234\312H\315\311\311WH-K-\252L\312O\251\004\000\000\000\377\377", state.compress("hello everybody")
    end

    def test_decompress_when_compression_is_enabled_should_return_decompressed_text
      state.set compression: :standard
      # JRuby Zlib implementation (1.4 & 1.5) does not have byte-to-byte compatibility with MRI's.
      # skip this test under JRuby.
      return if defined?(JRUBY_VERSION)

      assert_equal "hello everybody", state.decompress("x\234\312H\315\311\311WH-K-\252L\312O\251\004\000\000\000\377\377")
    end

    def test_compress_when_compression_is_delayed_and_auth_hint_is_set_should_return_compressed_text
      socket.hints[:authenticated] = true
      state.set compression: :delayed
      assert_equal "x\234\312H\315\311\311WH-K-\252L\312O\251\004\000\000\000\377\377", state.compress("hello everybody")
    end

    def test_decompress_when_compression_is_delayed_and_auth_hint_is_set_should_return_decompressed_text
      socket.hints[:authenticated] = true
      state.set compression: :delayed
      assert_equal "hello everybody", state.decompress("x\234\312H\315\311\311WH-K-\252L\312O\251\004\000\000\000\377\377")
    end

    def test_needs_rekey_should_be_true_if_packets_exceeds_max_packets
      state.set max_packets: 2
      state.increment(8)
      state.increment(8)
      assert !state.needs_rekey?
      state.increment(8)
      assert state.needs_rekey?
    end

    def test_needs_rekey_should_be_true_if_blocks_exceeds_max_blocks
      state.set max_blocks: 10
      assert !state.needs_rekey?
      state.increment(88)
      assert state.needs_rekey?
    end

    private

    def deflater(level = Zlib::DEFAULT_COMPRESSION)
      @deflater ||= Zlib::Deflate.new(level)
    end

    def inflater
      @inflater ||= Zlib::Inflate.new(nil)
    end

    def socket
      @socket ||= stub("socket", hints: {})
    end

    def state
      @state ||= Net::SSH::Transport::State.new(socket, :test)
    end
  end
end