net-sftp 1.1.1 → 2.0.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 (138) hide show
  1. data/CHANGELOG.rdoc +23 -0
  2. data/Manifest +55 -0
  3. data/README.rdoc +96 -0
  4. data/Rakefile +30 -0
  5. data/lib/net/sftp.rb +53 -38
  6. data/lib/net/sftp/constants.rb +187 -0
  7. data/lib/net/sftp/errors.rb +34 -20
  8. data/lib/net/sftp/operations/dir.rb +93 -0
  9. data/lib/net/sftp/operations/download.rb +364 -0
  10. data/lib/net/sftp/operations/file.rb +176 -0
  11. data/lib/net/sftp/operations/file_factory.rb +60 -0
  12. data/lib/net/sftp/operations/upload.rb +387 -0
  13. data/lib/net/sftp/packet.rb +21 -0
  14. data/lib/net/sftp/protocol.rb +32 -0
  15. data/lib/net/sftp/protocol/01/attributes.rb +265 -96
  16. data/lib/net/sftp/protocol/01/base.rb +268 -0
  17. data/lib/net/sftp/protocol/01/name.rb +43 -0
  18. data/lib/net/sftp/protocol/02/base.rb +31 -0
  19. data/lib/net/sftp/protocol/03/base.rb +35 -0
  20. data/lib/net/sftp/protocol/04/attributes.rb +120 -195
  21. data/lib/net/sftp/protocol/04/base.rb +94 -0
  22. data/lib/net/sftp/protocol/04/name.rb +67 -0
  23. data/lib/net/sftp/protocol/05/base.rb +66 -0
  24. data/lib/net/sftp/protocol/06/attributes.rb +107 -0
  25. data/lib/net/sftp/protocol/06/base.rb +63 -0
  26. data/lib/net/sftp/protocol/base.rb +50 -0
  27. data/lib/net/sftp/request.rb +91 -0
  28. data/lib/net/sftp/response.rb +76 -0
  29. data/lib/net/sftp/session.rb +914 -238
  30. data/lib/net/sftp/version.rb +14 -21
  31. data/net-sftp.gemspec +60 -0
  32. data/setup.rb +1331 -0
  33. data/test/common.rb +173 -0
  34. data/test/protocol/01/test_attributes.rb +97 -0
  35. data/test/protocol/01/test_base.rb +210 -0
  36. data/test/protocol/01/test_name.rb +27 -0
  37. data/test/protocol/02/test_base.rb +26 -0
  38. data/test/protocol/03/test_base.rb +27 -0
  39. data/test/protocol/04/test_attributes.rb +148 -0
  40. data/test/protocol/04/test_base.rb +74 -0
  41. data/test/protocol/04/test_name.rb +49 -0
  42. data/test/protocol/05/test_base.rb +62 -0
  43. data/test/protocol/06/test_attributes.rb +124 -0
  44. data/test/protocol/06/test_base.rb +51 -0
  45. data/test/protocol/test_base.rb +42 -0
  46. data/test/test_all.rb +3 -0
  47. data/test/test_dir.rb +47 -0
  48. data/test/test_download.rb +252 -0
  49. data/test/test_file.rb +159 -0
  50. data/test/test_file_factory.rb +48 -0
  51. data/test/test_packet.rb +9 -0
  52. data/test/test_protocol.rb +17 -0
  53. data/test/test_request.rb +71 -0
  54. data/test/test_response.rb +53 -0
  55. data/test/test_session.rb +741 -0
  56. data/test/test_upload.rb +219 -0
  57. metadata +59 -111
  58. data/doc/LICENSE-BSD +0 -27
  59. data/doc/LICENSE-GPL +0 -280
  60. data/doc/LICENSE-RUBY +0 -56
  61. data/doc/faq/faq.html +0 -298
  62. data/doc/faq/faq.rb +0 -154
  63. data/doc/faq/faq.yml +0 -183
  64. data/examples/asynchronous.rb +0 -57
  65. data/examples/get-put.rb +0 -45
  66. data/examples/sftp-open-uri.rb +0 -30
  67. data/examples/ssh-service.rb +0 -30
  68. data/examples/synchronous.rb +0 -131
  69. data/lib/net/sftp/operations/abstract.rb +0 -108
  70. data/lib/net/sftp/operations/close.rb +0 -31
  71. data/lib/net/sftp/operations/errors.rb +0 -76
  72. data/lib/net/sftp/operations/fsetstat.rb +0 -36
  73. data/lib/net/sftp/operations/fstat.rb +0 -32
  74. data/lib/net/sftp/operations/lstat.rb +0 -31
  75. data/lib/net/sftp/operations/mkdir.rb +0 -33
  76. data/lib/net/sftp/operations/open.rb +0 -32
  77. data/lib/net/sftp/operations/opendir.rb +0 -32
  78. data/lib/net/sftp/operations/read.rb +0 -88
  79. data/lib/net/sftp/operations/readdir.rb +0 -55
  80. data/lib/net/sftp/operations/realpath.rb +0 -37
  81. data/lib/net/sftp/operations/remove.rb +0 -31
  82. data/lib/net/sftp/operations/rename.rb +0 -32
  83. data/lib/net/sftp/operations/rmdir.rb +0 -31
  84. data/lib/net/sftp/operations/services.rb +0 -42
  85. data/lib/net/sftp/operations/setstat.rb +0 -33
  86. data/lib/net/sftp/operations/stat.rb +0 -31
  87. data/lib/net/sftp/operations/write.rb +0 -63
  88. data/lib/net/sftp/protocol/01/impl.rb +0 -251
  89. data/lib/net/sftp/protocol/01/packet-assistant.rb +0 -82
  90. data/lib/net/sftp/protocol/01/services.rb +0 -47
  91. data/lib/net/sftp/protocol/02/impl.rb +0 -39
  92. data/lib/net/sftp/protocol/02/packet-assistant.rb +0 -32
  93. data/lib/net/sftp/protocol/02/services.rb +0 -44
  94. data/lib/net/sftp/protocol/03/impl.rb +0 -42
  95. data/lib/net/sftp/protocol/03/packet-assistant.rb +0 -35
  96. data/lib/net/sftp/protocol/03/services.rb +0 -44
  97. data/lib/net/sftp/protocol/04/impl.rb +0 -86
  98. data/lib/net/sftp/protocol/04/packet-assistant.rb +0 -45
  99. data/lib/net/sftp/protocol/04/services.rb +0 -44
  100. data/lib/net/sftp/protocol/05/impl.rb +0 -90
  101. data/lib/net/sftp/protocol/05/packet-assistant.rb +0 -34
  102. data/lib/net/sftp/protocol/05/services.rb +0 -44
  103. data/lib/net/sftp/protocol/constants.rb +0 -60
  104. data/lib/net/sftp/protocol/driver.rb +0 -235
  105. data/lib/net/sftp/protocol/packet-assistant.rb +0 -84
  106. data/lib/net/sftp/protocol/services.rb +0 -55
  107. data/lib/uri/open-sftp.rb +0 -54
  108. data/lib/uri/sftp.rb +0 -42
  109. data/test/ALL-TESTS.rb +0 -23
  110. data/test/operations/tc_abstract.rb +0 -124
  111. data/test/operations/tc_close.rb +0 -40
  112. data/test/operations/tc_fsetstat.rb +0 -48
  113. data/test/operations/tc_fstat.rb +0 -40
  114. data/test/operations/tc_lstat.rb +0 -40
  115. data/test/operations/tc_mkdir.rb +0 -48
  116. data/test/operations/tc_open.rb +0 -42
  117. data/test/operations/tc_opendir.rb +0 -40
  118. data/test/operations/tc_read.rb +0 -103
  119. data/test/operations/tc_readdir.rb +0 -88
  120. data/test/operations/tc_realpath.rb +0 -54
  121. data/test/operations/tc_remove.rb +0 -40
  122. data/test/operations/tc_rmdir.rb +0 -40
  123. data/test/operations/tc_setstat.rb +0 -48
  124. data/test/operations/tc_stat.rb +0 -40
  125. data/test/operations/tc_write.rb +0 -91
  126. data/test/protocol/01/tc_attributes.rb +0 -138
  127. data/test/protocol/01/tc_impl.rb +0 -294
  128. data/test/protocol/01/tc_packet_assistant.rb +0 -81
  129. data/test/protocol/02/tc_impl.rb +0 -41
  130. data/test/protocol/02/tc_packet_assistant.rb +0 -31
  131. data/test/protocol/03/tc_impl.rb +0 -48
  132. data/test/protocol/03/tc_packet_assistant.rb +0 -34
  133. data/test/protocol/04/tc_attributes.rb +0 -174
  134. data/test/protocol/04/tc_impl.rb +0 -91
  135. data/test/protocol/04/tc_packet_assistant.rb +0 -38
  136. data/test/protocol/05/tc_impl.rb +0 -61
  137. data/test/protocol/05/tc_packet_assistant.rb +0 -32
  138. data/test/protocol/tc_driver.rb +0 -219
@@ -1,40 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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
@@ -1,40 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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
@@ -1,48 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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
@@ -1,40 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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
@@ -1,91 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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
@@ -1,138 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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 => 'wheel' }, :gid, Etc.getgrnam('wheel').gid ]
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
@@ -1,294 +0,0 @@
1
- #--
2
- # =============================================================================
3
- # Copyright (c) 2004, Jamis Buck (jamis@37signals.com)
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