net-sftp 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/doc/LICENSE-BSD +27 -0
  2. data/doc/LICENSE-GPL +280 -0
  3. data/doc/LICENSE-RUBY +56 -0
  4. data/examples/asynchronous.rb +57 -0
  5. data/examples/ssh-service.rb +31 -0
  6. data/examples/synchronous.rb +120 -0
  7. data/lib/net/sftp.rb +39 -0
  8. data/lib/net/sftp/errors.rb +25 -0
  9. data/lib/net/sftp/operations/abstract.rb +103 -0
  10. data/lib/net/sftp/operations/close.rb +31 -0
  11. data/lib/net/sftp/operations/errors.rb +76 -0
  12. data/lib/net/sftp/operations/fsetstat.rb +36 -0
  13. data/lib/net/sftp/operations/fstat.rb +32 -0
  14. data/lib/net/sftp/operations/lstat.rb +31 -0
  15. data/lib/net/sftp/operations/mkdir.rb +33 -0
  16. data/lib/net/sftp/operations/open.rb +32 -0
  17. data/lib/net/sftp/operations/opendir.rb +32 -0
  18. data/lib/net/sftp/operations/read.rb +84 -0
  19. data/lib/net/sftp/operations/readdir.rb +55 -0
  20. data/lib/net/sftp/operations/realpath.rb +37 -0
  21. data/lib/net/sftp/operations/remove.rb +31 -0
  22. data/lib/net/sftp/operations/rename.rb +32 -0
  23. data/lib/net/sftp/operations/rmdir.rb +31 -0
  24. data/lib/net/sftp/operations/services.rb +42 -0
  25. data/lib/net/sftp/operations/setstat.rb +33 -0
  26. data/lib/net/sftp/operations/stat.rb +31 -0
  27. data/lib/net/sftp/operations/write.rb +63 -0
  28. data/lib/net/sftp/protocol/01/attributes.rb +146 -0
  29. data/lib/net/sftp/protocol/01/impl.rb +251 -0
  30. data/lib/net/sftp/protocol/01/packet-assistant.rb +82 -0
  31. data/lib/net/sftp/protocol/01/services.rb +47 -0
  32. data/lib/net/sftp/protocol/02/impl.rb +39 -0
  33. data/lib/net/sftp/protocol/02/packet-assistant.rb +32 -0
  34. data/lib/net/sftp/protocol/02/services.rb +44 -0
  35. data/lib/net/sftp/protocol/03/impl.rb +42 -0
  36. data/lib/net/sftp/protocol/03/packet-assistant.rb +35 -0
  37. data/lib/net/sftp/protocol/03/services.rb +44 -0
  38. data/lib/net/sftp/protocol/04/attributes.rb +227 -0
  39. data/lib/net/sftp/protocol/04/impl.rb +134 -0
  40. data/lib/net/sftp/protocol/04/packet-assistant.rb +51 -0
  41. data/lib/net/sftp/protocol/04/services.rb +44 -0
  42. data/lib/net/sftp/protocol/05/services.rb +44 -0
  43. data/lib/net/sftp/protocol/constants.rb +60 -0
  44. data/lib/net/sftp/protocol/driver.rb +232 -0
  45. data/lib/net/sftp/protocol/packet-assistant.rb +84 -0
  46. data/lib/net/sftp/protocol/services.rb +55 -0
  47. data/lib/net/sftp/session.rb +215 -0
  48. data/lib/net/sftp/version.rb +25 -0
  49. data/test/ALL-TESTS.rb +21 -0
  50. data/test/operations/tc_abstract.rb +124 -0
  51. data/test/operations/tc_close.rb +40 -0
  52. data/test/operations/tc_fsetstat.rb +48 -0
  53. data/test/operations/tc_fstat.rb +40 -0
  54. data/test/operations/tc_lstat.rb +40 -0
  55. data/test/operations/tc_mkdir.rb +48 -0
  56. data/test/operations/tc_open.rb +42 -0
  57. data/test/operations/tc_opendir.rb +40 -0
  58. data/test/operations/tc_read.rb +103 -0
  59. data/test/operations/tc_readdir.rb +88 -0
  60. data/test/operations/tc_realpath.rb +54 -0
  61. data/test/operations/tc_remove.rb +40 -0
  62. data/test/operations/tc_rmdir.rb +40 -0
  63. data/test/operations/tc_setstat.rb +48 -0
  64. data/test/operations/tc_stat.rb +40 -0
  65. data/test/operations/tc_write.rb +91 -0
  66. data/test/protocol/01/tc_attributes.rb +138 -0
  67. data/test/protocol/01/tc_impl.rb +294 -0
  68. data/test/protocol/01/tc_packet_assistant.rb +81 -0
  69. data/test/protocol/02/tc_impl.rb +41 -0
  70. data/test/protocol/02/tc_packet_assistant.rb +31 -0
  71. data/test/protocol/03/tc_impl.rb +48 -0
  72. data/test/protocol/03/tc_packet_assistant.rb +34 -0
  73. data/test/protocol/04/tc_attributes.rb +174 -0
  74. data/test/protocol/04/tc_impl.rb +102 -0
  75. data/test/protocol/04/tc_packet_assistant.rb +41 -0
  76. data/test/protocol/tc_driver.rb +219 -0
  77. metadata +137 -0
@@ -0,0 +1,294 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../../lib"
18
+
19
+ require 'test/unit'
20
+ require 'net/sftp/protocol/constants'
21
+ require 'net/sftp/protocol/01/impl'
22
+ require 'flexmock'
23
+ require 'net/ssh/util/buffer'
24
+
25
+ class TC_01_Impl < Test::Unit::TestCase
26
+
27
+ class Buffers
28
+ def reader( text )
29
+ Net::SSH::Util::ReaderBuffer.new( text )
30
+ end
31
+ end
32
+
33
+ def setup
34
+ @sent_data = []
35
+ @permissions = nil
36
+
37
+ @driver = FlexMock.new
38
+ @driver.mock_handle( :send_data ) { |*a| @sent_data << a }
39
+
40
+ @assistant = FlexMock.new
41
+
42
+ @attr_factory = FlexMock.new
43
+ @attr_factory.mock_handle( :empty ) do
44
+ o = FlexMock.new
45
+ o.mock_handle( :permissions= ) { |p| @permissions = p }
46
+ o
47
+ end
48
+ @attr_factory.mock_handle( :from_buffer ) do |b|
49
+ b.read_long
50
+ end
51
+
52
+ @impl = impl_class.new( Buffers.new, @driver, @assistant, @attr_factory )
53
+ end
54
+
55
+ def impl_class
56
+ Net::SFTP::Protocol::V_01::Impl
57
+ end
58
+
59
+ def self.operation( name, the_alias=name )
60
+ class_eval <<-EOF, __FILE__, __LINE__+1
61
+ def test_#{the_alias}
62
+ @assistant.mock_handle( :#{name} ) { |*a| [ a[0], a[1..-1] ] }
63
+ id = @impl.#{the_alias}( 14, :a, :b, :c )
64
+ assert_equal 14, id
65
+ assert_equal 1, @assistant.mock_count( :#{name} )
66
+ assert_equal 1, @driver.mock_count( :send_data )
67
+ assert_equal [
68
+ [ Net::SFTP::Protocol::Constants::FXP_#{name.to_s.upcase},
69
+ [ :a, :b, :c ]] ], @sent_data
70
+ end
71
+ EOF
72
+ end
73
+
74
+ operation :open, :open_raw
75
+ operation :close
76
+ operation :close, :close_handle
77
+ operation :read
78
+ operation :write
79
+ operation :opendir
80
+ operation :readdir
81
+ operation :remove
82
+ operation :stat, :stat_raw
83
+ operation :lstat, :lstat_raw
84
+ operation :fstat, :fstat_raw
85
+ operation :setstat
86
+ operation :fsetstat
87
+ operation :mkdir
88
+ operation :rmdir
89
+ operation :realpath
90
+
91
+ unless defined?( IO_FLAGS )
92
+ IO_FLAGS = [ IO::RDONLY, IO::WRONLY, IO::RDWR, IO::APPEND ]
93
+ OTHER_FLAGS = [ 0, IO::CREAT, IO::TRUNC, IO::EXCL ]
94
+ MAP = { IO::RDONLY => 1, IO::WRONLY => 2, IO::RDWR => 3, IO::APPEND => 4,
95
+ IO::CREAT => 8, IO::TRUNC => 0x10, IO::EXCL => 0x20 }
96
+
97
+ IO_FLAGS.each do |flag|
98
+ OTHER_FLAGS.each do |oflag|
99
+ [ nil, 0400 ].each do |mode|
100
+ define_method( "test_open_#{flag}_#{oflag}_#{mode||"nil"}" ) do
101
+ @assistant.mock_handle( :open ) { |*a| [ a[0], a[1..-1] ] }
102
+ args = [ 14, "a path", flag | oflag ]
103
+ args << mode if mode
104
+ assert_equal 14, @impl.open( *args )
105
+ assert_equal 1, @assistant.mock_count( :open )
106
+ assert_equal( ( mode || 0660 ), @permissions )
107
+ sftp_flag = MAP[flag] | ( oflag == 0 ? 0 : MAP[oflag] )
108
+ assert_equal Net::SFTP::Protocol::Constants::FXP_OPEN,
109
+ @sent_data.first[0]
110
+ assert_equal [ "a path", sftp_flag ], @sent_data.first[1][0,2]
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ def test_stat_flags
118
+ @assistant.mock_handle( :stat ) { |*a| [ a[0], a[1..-1] ] }
119
+ id = @impl.stat( 14, :a, :b )
120
+ assert_equal 14, id
121
+ assert_equal 1, @assistant.mock_count( :stat )
122
+ assert_equal 1, @driver.mock_count( :send_data )
123
+ assert_equal [
124
+ [ Net::SFTP::Protocol::Constants::FXP_STAT, [ :a ]] ], @sent_data
125
+ end
126
+
127
+ def test_lstat_flags
128
+ @assistant.mock_handle( :lstat ) { |*a| [ a[0], a[1..-1] ] }
129
+ id = @impl.lstat( 14, :a, :b )
130
+ assert_equal 14, id
131
+ assert_equal 1, @assistant.mock_count( :lstat )
132
+ assert_equal 1, @driver.mock_count( :send_data )
133
+ assert_equal [
134
+ [ Net::SFTP::Protocol::Constants::FXP_LSTAT, [ :a ]] ], @sent_data
135
+ end
136
+
137
+ def test_fstat_flags
138
+ @assistant.mock_handle( :fstat ) { |*a| [ a[0], a[1..-1] ] }
139
+ id = @impl.fstat( 14, :a, :b )
140
+ assert_equal 14, id
141
+ assert_equal 1, @assistant.mock_count( :fstat )
142
+ assert_equal 1, @driver.mock_count( :send_data )
143
+ assert_equal [
144
+ [ Net::SFTP::Protocol::Constants::FXP_FSTAT, [ :a ]] ], @sent_data
145
+ end
146
+
147
+ def test_dispatch_bad
148
+ assert_raise( Net::SFTP::Exception ) do
149
+ @impl.dispatch( nil, -5, "blah" )
150
+ end
151
+ end
152
+
153
+ def test_do_status_without_callback
154
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\2" )
155
+ @impl.do_status( nil, buffer )
156
+ assert_equal 0, buffer.position
157
+ end
158
+
159
+ def test_do_status_with_callback
160
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\2" )
161
+ called = false
162
+ @impl.on_status do |d,i,c,a,b|
163
+ called = true
164
+ assert_equal 1, i
165
+ assert_equal 2, c
166
+ assert_nil a
167
+ assert_nil b
168
+ end
169
+ @impl.do_status nil, buffer
170
+ assert called
171
+ end
172
+
173
+ def test_do_handle_without_callback
174
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\2ab" )
175
+ @impl.do_handle( nil, buffer )
176
+ assert_equal 0, buffer.position
177
+ end
178
+
179
+ def test_do_handle_with_callback
180
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\2ab" )
181
+ called = false
182
+ @impl.on_handle do |d,i,h|
183
+ called = true
184
+ assert_equal 1, i
185
+ assert_equal "ab", h
186
+ end
187
+ @impl.do_handle nil, buffer
188
+ assert called
189
+ end
190
+
191
+ def test_do_data_without_callback
192
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\7\0\0\0\3abc" )
193
+ @impl.do_data( nil, buffer )
194
+ assert_equal 0, buffer.position
195
+ end
196
+
197
+ def test_do_data_with_callback
198
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\7\0\0\0\3abc" )
199
+ called = false
200
+ @impl.on_data do |d,id,data|
201
+ called = true
202
+ assert_equal 1, id
203
+ assert_equal "\0\0\0\3abc", data
204
+ end
205
+ @impl.do_data nil, buffer
206
+ assert called
207
+ end
208
+
209
+ def test_do_name_without_callback
210
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\2" +
211
+ "\0\0\0\1a\0\0\0\1b\0\0\0\0" )
212
+ @impl.do_name( nil, buffer )
213
+ assert_equal 0, buffer.position
214
+ end
215
+
216
+ def test_do_name_with_callback
217
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\2" +
218
+ "\0\0\0\1a\0\0\0\1b\0\0\0\0" +
219
+ "\0\0\0\1c\0\0\0\1d\0\0\0\0" )
220
+ called = false
221
+ @impl.on_name do |d,i,names|
222
+ called = true
223
+ assert_equal 1, i
224
+ assert_equal 2, names.length
225
+ assert_equal "a", names.first.filename
226
+ assert_equal "b", names.first.longname
227
+ assert_equal 0, names.first.attributes
228
+ assert_equal "c", names.last.filename
229
+ assert_equal "d", names.last.longname
230
+ assert_equal 0, names.last.attributes
231
+ end
232
+ @impl.do_name nil, buffer
233
+ assert called
234
+ end
235
+
236
+ def test_do_attrs_without_callback
237
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\0" )
238
+ @impl.do_attrs( nil, buffer )
239
+ assert_equal 0, buffer.position
240
+ end
241
+
242
+ def test_do_attrs_with_callback
243
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\1\0\0\0\0" )
244
+ called = false
245
+ @impl.on_attrs do |d,i,a|
246
+ called = true
247
+ assert_equal 1, i
248
+ assert_equal 0, a
249
+ end
250
+ @impl.do_attrs nil, buffer
251
+ assert called
252
+ end
253
+
254
+ def test_dispatch_status
255
+ buffer = Net::SSH::Util::ReaderBuffer.new( "" )
256
+ called = false
257
+ @impl.on_status { called = true }
258
+ @impl.dispatch( nil, Net::SFTP::Protocol::Constants::FXP_STATUS, buffer )
259
+ assert called
260
+ end
261
+
262
+ def test_dispatch_handle
263
+ buffer = Net::SSH::Util::ReaderBuffer.new( "" )
264
+ called = false
265
+ @impl.on_handle { called = true }
266
+ @impl.dispatch( nil, Net::SFTP::Protocol::Constants::FXP_HANDLE, buffer )
267
+ assert called
268
+ end
269
+
270
+ def test_dispatch_data
271
+ buffer = Net::SSH::Util::ReaderBuffer.new( "" )
272
+ called = false
273
+ @impl.on_data { called = true }
274
+ @impl.dispatch( nil, Net::SFTP::Protocol::Constants::FXP_DATA, buffer )
275
+ assert called
276
+ end
277
+
278
+ def test_dispatch_name
279
+ buffer = Net::SSH::Util::ReaderBuffer.new( "\0\0\0\0\0\0\0\0" )
280
+ called = false
281
+ @impl.on_name { called = true }
282
+ @impl.dispatch( nil, Net::SFTP::Protocol::Constants::FXP_NAME, buffer )
283
+ assert called
284
+ end
285
+
286
+ def test_dispatch_attrs
287
+ buffer = Net::SSH::Util::ReaderBuffer.new( "" )
288
+ called = false
289
+ @impl.on_attrs { called = true }
290
+ @impl.dispatch( nil, Net::SFTP::Protocol::Constants::FXP_ATTRS, buffer )
291
+ assert called
292
+ end
293
+
294
+ end
@@ -0,0 +1,81 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../../lib"
18
+
19
+ require 'test/unit'
20
+ require 'net/sftp/protocol/01/packet-assistant'
21
+ require 'flexmock'
22
+ require 'net/ssh/util/buffer'
23
+
24
+ class TC_01_PacketAssistant < Test::Unit::TestCase
25
+
26
+ class Buffers
27
+ def writer
28
+ Net::SSH::Util::WriterBuffer.new
29
+ end
30
+ end
31
+
32
+ def setup
33
+ @driver = FlexMock.new
34
+ @driver.mock_handle( :next_request_id ) { 14 }
35
+ @packets = packet_assistant_class.new( Buffers.new, @driver )
36
+ end
37
+
38
+ def packet_assistant_class
39
+ Net::SFTP::Protocol::V_01::PacketAssistant
40
+ end
41
+
42
+ def self.packet( name, args, expect )
43
+ define_method( "test_#{name}" ) do
44
+ id, packet = @packets.__send__( name, *[ nil, *args ] )
45
+ assert_equal 14, id
46
+ assert_equal "\0\0\0\16" + expect, packet
47
+ end
48
+ end
49
+
50
+ packet :open, [ "a path", 1, "attrs" ], "\0\0\0\6a path\0\0\0\1attrs"
51
+
52
+ packet :close, [ "handle" ], "\0\0\0\6handle"
53
+
54
+ packet :read, [ "handle", 0, 16 ], "\0\0\0\6handle\0\0\0\0\0\0\0\0\0\0\0\20"
55
+
56
+ packet :write, [ "handle", 0, "data" ],
57
+ "\0\0\0\6handle\0\0\0\0\0\0\0\0\0\0\0\4data"
58
+
59
+ packet :opendir, [ "a path" ], "\0\0\0\6a path"
60
+
61
+ packet :readdir, [ "handle" ], "\0\0\0\6handle"
62
+
63
+ packet :remove, [ "a path" ], "\0\0\0\6a path"
64
+
65
+ packet :stat, [ "a path" ], "\0\0\0\6a path"
66
+
67
+ packet :lstat, [ "a path" ], "\0\0\0\6a path"
68
+
69
+ packet :fstat, [ "handle" ], "\0\0\0\6handle"
70
+
71
+ packet :setstat, [ "a path", "attrs" ], "\0\0\0\6a pathattrs"
72
+
73
+ packet :fsetstat, [ "handle", "attrs" ], "\0\0\0\6handleattrs"
74
+
75
+ packet :mkdir, [ "a path", "attrs" ], "\0\0\0\6a pathattrs"
76
+
77
+ packet :rmdir, [ "a path" ], "\0\0\0\6a path"
78
+
79
+ packet :realpath, [ "a path" ], "\0\0\0\6a path"
80
+
81
+ end
@@ -0,0 +1,41 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../../lib"
18
+ $:.unshift File.join( File.dirname( __FILE__ ), ".." )
19
+
20
+ require '01/tc_impl.rb'
21
+ require 'net/sftp/protocol/02/impl'
22
+
23
+ class TC_02_Impl < TC_01_Impl
24
+
25
+ def impl_class
26
+ Net::SFTP::Protocol::V_02::Impl
27
+ end
28
+
29
+ operation :rename, :rename_raw
30
+
31
+ def test_rename_flags
32
+ @assistant.mock_handle( :rename ) { |*a| [ a[0], a[1..-1] ] }
33
+ id = @impl.rename( 14, :a, :b, :c )
34
+ assert_equal 14, id
35
+ assert_equal 1, @assistant.mock_count( :rename )
36
+ assert_equal 1, @driver.mock_count( :send_data )
37
+ assert_equal [
38
+ [ Net::SFTP::Protocol::Constants::FXP_RENAME, [ :a, :b ]] ], @sent_data
39
+ end
40
+
41
+ end
@@ -0,0 +1,31 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ $:.unshift "../../../lib"
18
+ $:.unshift File.join( File.dirname( __FILE__ ), ".." )
19
+
20
+ require '01/tc_packet_assistant'
21
+ require 'net/sftp/protocol/02/packet-assistant'
22
+
23
+ class TC_02_PacketAssistant < TC_01_PacketAssistant
24
+
25
+ def packet_assistant_class
26
+ Net::SFTP::Protocol::V_02::PacketAssistant
27
+ end
28
+
29
+ packet :rename, [ "a path", "new path" ], "\0\0\0\6a path\0\0\0\10new path"
30
+
31
+ end