net-sftp 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,54 @@
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/operations/realpath'
21
+ require 'flexmock'
22
+
23
+ class TC_Operations_Realpath < Test::Unit::TestCase
24
+
25
+ def setup
26
+ @log = FlexMock.new
27
+ @session = FlexMock.new
28
+ @driver = FlexMock.new
29
+ @operation = Net::SFTP::Operations::Realpath.new( @log, @session, @driver )
30
+ end
31
+
32
+ def test_perform
33
+ id = path = nil
34
+ @driver.mock_handle( :realpath ) { |i,p| id, path = i, p; 10 }
35
+ assert_equal 10, @operation.perform( "foo" )
36
+ assert_nil id
37
+ assert_equal "foo", path
38
+ end
39
+
40
+ def test_do_name
41
+ @session.mock_handle( :loop )
42
+ @session.mock_handle( :status= )
43
+ @session.mock_handle( :register )
44
+ @driver.mock_handle( :realpath )
45
+ @log.mock_handle( :debug? )
46
+ @log.mock_handle( :debug )
47
+
48
+ called = false
49
+ @operation.execute( "foo" ) { |s,v| called = v }
50
+ @operation.do_name( [ :a, :b ] )
51
+ assert_equal :a, called
52
+ end
53
+
54
+ end
@@ -0,0 +1,40 @@
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/operations/remove'
21
+ require 'flexmock'
22
+
23
+ class TC_Operations_Remove < Test::Unit::TestCase
24
+
25
+ def setup
26
+ @log = FlexMock.new
27
+ @session = FlexMock.new
28
+ @driver = FlexMock.new
29
+ @operation = Net::SFTP::Operations::Remove.new( @log, @session, @driver )
30
+ end
31
+
32
+ def test_perform
33
+ id = filename = nil
34
+ @driver.mock_handle( :remove ) { |i,f,m| id, filename = i, f; 10 }
35
+ assert_equal 10, @operation.perform( "foo" )
36
+ assert_nil id
37
+ assert_equal "foo", filename
38
+ end
39
+
40
+ end
@@ -0,0 +1,40 @@
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/operations/rmdir'
21
+ require 'flexmock'
22
+
23
+ class TC_Operations_Rmdir < Test::Unit::TestCase
24
+
25
+ def setup
26
+ @log = FlexMock.new
27
+ @session = FlexMock.new
28
+ @driver = FlexMock.new
29
+ @operation = Net::SFTP::Operations::Rmdir.new( @log, @session, @driver )
30
+ end
31
+
32
+ def test_perform
33
+ id = path = nil
34
+ @driver.mock_handle( :rmdir ) { |i,p| id, path = i, p; 10 }
35
+ assert_equal 10, @operation.perform( "foo" )
36
+ assert_nil id
37
+ assert_equal "foo", path
38
+ end
39
+
40
+ end
@@ -0,0 +1,48 @@
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/operations/setstat'
21
+ require 'flexmock'
22
+
23
+ class TC_Operations_setstat < Test::Unit::TestCase
24
+
25
+ def setup
26
+ @log = FlexMock.new
27
+ @session = FlexMock.new
28
+ @attr_factory = FlexMock.new
29
+ @driver = FlexMock.new
30
+ @driver.mock_handle( :attr_factory ) { @attr_factory }
31
+ @operation = Net::SFTP::Operations::Setstat.new( @log, @session, @driver )
32
+ end
33
+
34
+ def test_perform
35
+ hash = nil
36
+ @attr_factory.mock_handle( :from_hash ) { |h| hash = h; :foo }
37
+
38
+ id = path = attrs = nil
39
+ @driver.mock_handle( :setstat ) { |i,p,a| id, path, attrs = i, p, a; 10 }
40
+
41
+ assert_equal 10, @operation.perform( "foo", "bar" )
42
+ assert_nil id
43
+ assert_equal "foo", path
44
+ assert_equal "bar", hash
45
+ assert_equal :foo, attrs
46
+ end
47
+
48
+ end
@@ -0,0 +1,40 @@
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/operations/stat'
21
+ require 'flexmock'
22
+
23
+ class TC_Operations_Stat < Test::Unit::TestCase
24
+
25
+ def setup
26
+ @log = FlexMock.new
27
+ @session = FlexMock.new
28
+ @driver = FlexMock.new
29
+ @operation = Net::SFTP::Operations::Stat.new( @log, @session, @driver )
30
+ end
31
+
32
+ def test_perform
33
+ id = path = nil
34
+ @driver.mock_handle( :stat ) { |i,p| id, path = i, p; 10 }
35
+ assert_equal 10, @operation.perform( "foo" )
36
+ assert_nil id
37
+ assert_equal "foo", path
38
+ end
39
+
40
+ end
@@ -0,0 +1,91 @@
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/operations/write'
21
+ require 'flexmock'
22
+
23
+ class TC_Operations_Write < Test::Unit::TestCase
24
+
25
+ def setup
26
+ @log = FlexMock.new
27
+ @log.mock_handle( :debug? )
28
+ @log.mock_handle( :debug )
29
+
30
+ @session = FlexMock.new
31
+ @driver = FlexMock.new
32
+ @operation = Net::SFTP::Operations::Write.new( @log, @session, @driver )
33
+ end
34
+
35
+ def test_perform_defaults
36
+ id = handle = offset = data = nil
37
+ @driver.mock_handle( :write ) { |i,h,o,d| id, handle, offset, data = i, h, o, d; 10 }
38
+ assert_equal 10, @operation.perform( "foo", "data" )
39
+ assert_nil id
40
+ assert_equal "foo", handle
41
+ assert_equal "data", data
42
+ assert_equal 0, offset
43
+ end
44
+
45
+ def test_perform_explicit
46
+ id = handle = offset = data = nil
47
+ @driver.mock_handle( :write ) { |i,h,o,d| id, handle, offset, data = i, h, o, d; 10 }
48
+ assert_equal 10, @operation.perform( "foo", "data", 15 )
49
+ assert_nil id
50
+ assert_equal "foo", handle
51
+ assert_equal "data", data
52
+ assert_equal 15, offset
53
+ end
54
+
55
+ def test_do_status_bad
56
+ assert_raise( Net::SFTP::Operations::StatusException ) do
57
+ @operation.do_status( 5, nil, nil )
58
+ end
59
+ end
60
+
61
+ def test_do_status_ok_more_chunks
62
+ @log.mock_handle :debug?
63
+ @log.mock_handle :debug
64
+ @session.mock_handle :register
65
+ @session.mock_handle :status=
66
+ @session.mock_handle :loop
67
+ @driver.mock_handle :write
68
+
69
+ @operation.execute( "foo", "1234567890" * 7000 )
70
+ @operation.do_status( 0, :a, :b )
71
+ assert_equal 2, @driver.mock_count( :write )
72
+ assert_equal 2, @session.mock_count( :register )
73
+ end
74
+
75
+ def test_do_status_ok_done
76
+ @log.mock_handle :debug?
77
+ @log.mock_handle :debug
78
+ @session.mock_handle :register
79
+ @session.mock_handle :status=
80
+ @session.mock_handle :loop
81
+ @driver.mock_handle :write
82
+
83
+ called = false
84
+ @operation.execute( "foo", "1234567890" ) { called = true }
85
+ @operation.do_status( 0, :a, :b )
86
+ assert_equal 1, @driver.mock_count( :write )
87
+ assert_equal 1, @session.mock_count( :register )
88
+ assert called
89
+ end
90
+
91
+ end
@@ -0,0 +1,138 @@
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/attributes'
21
+
22
+ require 'net/ssh'
23
+ require 'net/ssh/util/buffer'
24
+
25
+ class TC_01_Attributes < Test::Unit::TestCase
26
+
27
+ class Buffers
28
+ def writer
29
+ Net::SSH::Util::WriterBuffer.new
30
+ end
31
+ def reader( text )
32
+ Net::SSH::Util::ReaderBuffer.new( text )
33
+ end
34
+ end
35
+
36
+ def setup
37
+ @factory = Net::SFTP::Protocol::V_01::Attributes.init( Buffers.new )
38
+ end
39
+
40
+ def test_empty
41
+ empty = @factory.empty
42
+ assert_nil empty.size
43
+ assert_nil empty.uid
44
+ assert_nil empty.gid
45
+ assert_nil empty.permissions
46
+ assert_nil empty.atime
47
+ assert_nil empty.mtime
48
+ assert_nil empty.extended
49
+ assert_equal "\0\0\0\0", empty.to_s
50
+ end
51
+
52
+ def test_from_buffer_empty
53
+ buffer = @factory.buffers.reader( "\0\0\0\0" )
54
+ attrs = @factory.from_buffer( buffer )
55
+ assert_nil attrs.size
56
+ assert_nil attrs.uid
57
+ assert_nil attrs.gid
58
+ assert_nil attrs.permissions
59
+ assert_nil attrs.atime
60
+ assert_nil attrs.mtime
61
+ assert_nil attrs.extended
62
+ assert_equal "\0\0\0\0", attrs.to_s
63
+ end
64
+
65
+ ATTRIBUTES =
66
+ [ [ 1, "\1\2\3\4\5\6\7\10", :size, 0x0102030405060708 ],
67
+ [ 2, "\11\12\13\14\15\16\17\20", :uid, 0x090A0B0C ],
68
+ [ 2, "\11\12\13\14\15\16\17\20", :gid, 0x0D0E0F10 ],
69
+ [ 4, "\21\22\23\24", :permissions, 0x11121314 ],
70
+ [ 8, "\25\26\27\30\31\32\33\34", :atime, 0x15161718 ],
71
+ [ 8, "\25\26\27\30\31\32\33\34", :mtime, 0x191A1B1C ],
72
+ [ 0x80000000,
73
+ "\0\0\0\4" +
74
+ "\0\0\0\01a\0\0\0\01b" +
75
+ "\0\0\0\01c\0\0\0\01d" +
76
+ "\0\0\0\01e\0\0\0\01f" +
77
+ "\0\0\0\01g\0\0\0\01h",
78
+ :extended,
79
+ { "a" => "b", "c" => "d", "e" => "f", "g" => "h" } ] ]
80
+
81
+ # find all possible combinations of the elements of ATTRIBUTES
82
+ # (i.e., 'n' choose k, for k in [1..ATTRIBUTES.length]).
83
+ def self.choose( from, k )
84
+ result = []
85
+ ( from.length - k + 1 ).times do |i|
86
+ if i+1 < from.length && k > 1
87
+ choose( from[i+1..-1], k-1 ).each do |chosen|
88
+ result << [ from[i], *chosen ]
89
+ end
90
+ else
91
+ result << [ from[i] ]
92
+ end
93
+ end
94
+ result
95
+ end
96
+
97
+ method_id = 0
98
+ ATTRIBUTES.length.times do |k|
99
+ choose( ATTRIBUTES, k+1 ).each do |attrs|
100
+ define_method( "test_attributes_%03d" % method_id ) do
101
+ flags = 0
102
+ buffer = ""
103
+ attrs.each do |a|
104
+ if ( flags & a[0] ) == 0
105
+ flags |= a[0]
106
+ buffer += a[1]
107
+ end
108
+ end
109
+ buffer = [ flags, buffer ].pack( "NA*" )
110
+ input = @factory.buffers.reader( buffer )
111
+ obj = @factory.from_buffer( input )
112
+ attrs.each { |a| assert_equal a[3], obj.__send__( a[2] ) }
113
+ assert_equal buffer, obj.to_s
114
+ end
115
+ method_id += 1
116
+ end
117
+ end
118
+
119
+ require 'etc'
120
+ [
121
+ [ { :size => 1000 }, :size, 1000, :uid ],
122
+ [ { :uid => 1000 }, :uid, 1000 ],
123
+ [ { :gid => 1000 }, :gid, 1000 ],
124
+ [ { :permissions => 0600 }, :permissions, 0600 ],
125
+ [ { :atime => 123456 }, :atime, 123456 ],
126
+ [ { :mtime => 789012 }, :mtime, 789012 ],
127
+ [ { :extended => { "foo" => "bar" } }, :extended, { "foo" => "bar" } ],
128
+ [ { :owner => ENV['USER'] }, :uid, Etc.getpwnam(ENV['USER']).uid ],
129
+ [ { :group => 'root' }, :gid, Etc.getpwnam('root').uid ]
130
+ ].each do |fixture|
131
+ define_method( "test_from_hash_#{fixture[1]}" ) do
132
+ attrs = @factory.from_hash( fixture[0] )
133
+ assert_equal fixture[2], attrs.__send__( fixture[1] )
134
+ assert_nil attrs.__send__( fixture[3] || :size )
135
+ end
136
+ end
137
+
138
+ end