net-sftp 1.1.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,48 @@
1
+ require "common"
2
+
3
+ class FileFactoryTest < Net::SFTP::TestCase
4
+ def setup
5
+ @sftp = stub(:sftp)
6
+ @factory = Net::SFTP::Operations::FileFactory.new(@sftp)
7
+ end
8
+
9
+ def test_open_with_block_should_yield_and_close_handle
10
+ @sftp.expects(:open!).with("/path/to/remote", "r", :permissions => nil).returns("handle")
11
+ @sftp.expects(:close!).with("handle")
12
+
13
+ called = false
14
+ @factory.open("/path/to/remote") do |f|
15
+ called = true
16
+ assert_instance_of Net::SFTP::Operations::File, f
17
+ end
18
+
19
+ assert called
20
+ end
21
+
22
+ def test_open_with_block_should_close_file_even_if_exception_is_raised
23
+ @sftp.expects(:open!).with("/path/to/remote", "r", :permissions => nil).returns("handle")
24
+ @sftp.expects(:close!).with("handle")
25
+
26
+ assert_raises(RuntimeError) do
27
+ @factory.open("/path/to/remote") { |f| raise RuntimeError, "b00m" }
28
+ end
29
+ end
30
+
31
+ def test_open_without_block_should_return_new_file
32
+ @sftp.expects(:open!).with("/path/to/remote", "r", :permissions => nil).returns("handle")
33
+ @sftp.expects(:close!).never
34
+
35
+ f = @factory.open("/path/to/remote")
36
+ assert_instance_of Net::SFTP::Operations::File, f
37
+ end
38
+
39
+ def test_directory_should_be_true_for_directory
40
+ @sftp.expects(:lstat!).with("/path/to/dir").returns(mock('attrs', :directory? => true))
41
+ assert @factory.directory?("/path/to/dir")
42
+ end
43
+
44
+ def test_directory_should_be_false_for_non_directory
45
+ @sftp.expects(:lstat!).with("/path/to/file").returns(mock('attrs', :directory? => false))
46
+ assert !@factory.directory?("/path/to/file")
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ require 'common'
2
+
3
+ class PacketTest < Net::SFTP::TestCase
4
+ def test_packet_should_auto_read_type_byte
5
+ packet = Net::SFTP::Packet.new("\001rest-of-packet-here")
6
+ assert_equal 1, packet.type
7
+ assert_equal "rest-of-packet-here", packet.content[packet.position..-1]
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ require 'common'
2
+
3
+ class ProtocolTest < Net::SFTP::TestCase
4
+ 1.upto(6) do |version|
5
+ define_method("test_load_version_#{version}_should_return_v#{version}_driver") do
6
+ session = stub('session', :logger => nil)
7
+ driver = Net::SFTP::Protocol.load(session, version)
8
+ assert_instance_of Net::SFTP::Protocol.const_get("V%02d" % version)::Base, driver
9
+ end
10
+ end
11
+
12
+ def test_load_version_7_should_be_unsupported
13
+ assert_raises(NotImplementedError) do
14
+ Net::SFTP::Protocol.load(stub('session'), 7)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,71 @@
1
+ require 'common'
2
+
3
+ class RequestTest < Net::SFTP::TestCase
4
+ def test_property_setter_should_symbolize_key
5
+ request = Net::SFTP::Request.new(stub("session"), :open, 1)
6
+ request["key"] = :value
7
+ assert_equal :value, request['key']
8
+ assert_equal :value, request[:key]
9
+ assert_equal :value, request.properties[:key]
10
+ assert_nil request.properties['key']
11
+ end
12
+
13
+ def test_pending_should_query_pending_requests_of_session
14
+ session = stub("session", :pending_requests => {1 => true})
15
+ request = Net::SFTP::Request.new(session, :open, 1)
16
+ assert request.pending?
17
+ request = Net::SFTP::Request.new(session, :open, 2)
18
+ assert !request.pending?
19
+ end
20
+
21
+ def test_wait_should_run_loop_while_pending_and_return_self
22
+ session = MockSession.new
23
+ request = Net::SFTP::Request.new(session, :open, 1)
24
+ request.expects(:pending?).times(4).returns(true, true, true, false)
25
+ assert_equal 0, session.loops
26
+ assert_equal request, request.wait
27
+ assert_equal 4, session.loops
28
+ end
29
+
30
+ def test_respond_to_should_set_response_property
31
+ packet = stub("packet", :type => 1)
32
+ session = stub("session", :protocol => mock("protocol"))
33
+ session.protocol.expects(:parse).with(packet).returns({})
34
+ request = Net::SFTP::Request.new(session, :open, 1)
35
+ assert_nil request.response
36
+ request.respond_to(packet)
37
+ assert_instance_of Net::SFTP::Response, request.response
38
+ end
39
+
40
+ def test_respond_to_with_callback_should_invoke_callback
41
+ packet = stub("packet", :type => 1)
42
+ session = stub("session", :protocol => mock("protocol"))
43
+ session.protocol.expects(:parse).with(packet).returns({})
44
+
45
+ called = false
46
+ request = Net::SFTP::Request.new(session, :open, 1) do |response|
47
+ called = true
48
+ assert_equal request.response, response
49
+ end
50
+
51
+ request.respond_to(packet)
52
+ assert called
53
+ end
54
+
55
+ private
56
+
57
+ class MockSession
58
+ attr_reader :loops
59
+
60
+ def initialize
61
+ @loops = 0
62
+ end
63
+
64
+ def loop
65
+ while true
66
+ @loops += 1
67
+ break unless yield
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ require 'common'
2
+
3
+ class ResponseTest < Net::SFTP::TestCase
4
+ def test_code_should_default_to_FX_OK
5
+ response = Net::SFTP::Response.new(mock("response"))
6
+ assert_equal Net::SFTP::Response::FX_OK, response.code
7
+ end
8
+
9
+ def test_brackets_should_symbolize_key
10
+ response = Net::SFTP::Response.new(mock("response"), :handle => "foo")
11
+ assert_equal "foo", response['handle']
12
+ end
13
+
14
+ def test_to_s_with_nil_message_should_show_default_message
15
+ response = Net::SFTP::Response.new(mock("response"), :code => 14)
16
+ assert_equal "no space on filesystem (14)", response.to_s
17
+ end
18
+
19
+ def test_to_s_with_empty_message_should_show_default_message
20
+ response = Net::SFTP::Response.new(mock("response"), :code => 14, :message => "")
21
+ assert_equal "no space on filesystem (14)", response.to_s
22
+ end
23
+
24
+ def test_to_s_with_default_message_should_show_default_message
25
+ response = Net::SFTP::Response.new(mock("response"), :code => 14, :message => "no space on filesystem")
26
+ assert_equal "no space on filesystem (14)", response.to_s
27
+ end
28
+
29
+ def test_to_s_with_explicit_message_should_show_explicit_message
30
+ response = Net::SFTP::Response.new(mock("response"), :code => 14, :message => "out of space")
31
+ assert_equal "out of space (no space on filesystem, 14)", response.to_s
32
+ end
33
+
34
+ def test_ok_should_be_true_when_code_is_FX_OK
35
+ response = Net::SFTP::Response.new(mock("response"))
36
+ assert_equal true, response.ok?
37
+ end
38
+
39
+ def test_ok_should_be_false_when_code_is_not_FX_OK
40
+ response = Net::SFTP::Response.new(mock("response"), :code => 14)
41
+ assert_equal false, response.ok?
42
+ end
43
+
44
+ def test_eof_should_be_true_when_code_is_FX_EOF
45
+ response = Net::SFTP::Response.new(mock("response"), :code => 1)
46
+ assert_equal true, response.eof?
47
+ end
48
+
49
+ def test_eof_should_be_false_when_code_is_not_FX_EOF
50
+ response = Net::SFTP::Response.new(mock("response"), :code => 14)
51
+ assert_equal false, response.eof?
52
+ end
53
+ end
@@ -0,0 +1,741 @@
1
+ require "common"
2
+
3
+ class SessionTest < Net::SFTP::TestCase
4
+ include Net::SFTP::Constants
5
+ include Net::SFTP::Constants::OpenFlags
6
+ include Net::SFTP::Constants::PacketTypes
7
+
8
+ (1..6).each do |version|
9
+ define_method("test_server_reporting_version_#{version}_should_cause_version_#{version}_to_be_used") do
10
+ expect_sftp_session :server_version => version
11
+ assert_scripted { sftp.connect! }
12
+ assert_equal version, sftp.protocol.version
13
+ end
14
+ end
15
+
16
+ def test_v1_open_read_only_that_succeeds_should_invoke_callback
17
+ expect_open("/path/to/file", "r", nil, :server_version => 1)
18
+ assert_successful_open("/path/to/file")
19
+ end
20
+
21
+ def test_v1_open_read_only_that_fails_should_invoke_callback
22
+ expect_open("/path/to/file", "r", nil, :server_version => 1, :fail => 2)
23
+
24
+ assert_command_with_callback(:open, "/path/to/file") do |response|
25
+ assert !response.ok?
26
+ assert_equal 2, response.code
27
+ end
28
+ end
29
+
30
+ def test_v1_open_write_only_that_succeeds_should_invoke_callback
31
+ expect_open("/path/to/file", "w", nil, :server_version => 1)
32
+ assert_successful_open("/path/to/file", "w")
33
+ end
34
+
35
+ def test_v1_open_read_write_that_succeeds_should_invoke_callback
36
+ expect_open("/path/to/file", "rw", nil, :server_version => 1)
37
+ assert_successful_open("/path/to/file", "r+")
38
+ end
39
+
40
+ def test_v1_open_append_that_succeeds_should_invoke_callback
41
+ expect_open("/path/to/file", "a", nil, :server_version => 1)
42
+ assert_successful_open("/path/to/file", "a")
43
+ end
44
+
45
+ def test_v1_open_with_permissions_should_specify_permissions
46
+ expect_open("/path/to/file", "r", 0765, :server_version => 1)
47
+ assert_successful_open("/path/to/file", "r", :permissions => 0765)
48
+ end
49
+
50
+ def test_v4_open_with_permissions_should_specify_permissions
51
+ expect_open("/path/to/file", "r", 0765, :server_version => 4)
52
+ assert_successful_open("/path/to/file", "r", :permissions => 0765)
53
+ end
54
+
55
+ def test_v5_open_read_only_shuld_invoke_callback
56
+ expect_open("/path/to/file", "r", 0765, :server_version => 5)
57
+ assert_successful_open("/path/to/file", "r", :permissions => 0765)
58
+ end
59
+
60
+ def test_v6_open_with_permissions_should_specify_permissions
61
+ expect_open("/path/to/file", "r", 0765, :server_version => 6)
62
+ assert_successful_open("/path/to/file", "r", :permissions => 0765)
63
+ end
64
+
65
+ def test_open_bang_should_block_and_return_handle
66
+ expect_open("/path/to/file", "r", nil)
67
+ handle = assert_synchronous_command(:open!, "/path/to/file", "r")
68
+ assert_equal "handle", handle
69
+ end
70
+
71
+ def test_open_bang_should_block_and_raise_exception_on_error
72
+ expect_open("/path/to/file", "r", nil, :fail => 5)
73
+ assert_raises(Net::SFTP::StatusException) do
74
+ assert_synchronous_command(:open!, "/path/to/file", "r")
75
+ end
76
+ end
77
+
78
+ def test_close_should_send_close_request_and_invoke_callback
79
+ expect_sftp_session do |channel|
80
+ channel.sends_packet(FXP_CLOSE, :long, 0, :string, "handle")
81
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
82
+ end
83
+
84
+ assert_command_with_callback(:close, "handle") { |r| assert r.ok? }
85
+ end
86
+
87
+ def test_close_bang_should_block_and_return_response
88
+ expect_sftp_session do |channel|
89
+ channel.sends_packet(FXP_CLOSE, :long, 0, :string, "handle")
90
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
91
+ end
92
+
93
+ response = assert_synchronous_command(:close!, "handle")
94
+ assert response.ok?
95
+ end
96
+
97
+ def test_read_should_send_read_request_and_invoke_callback
98
+ expect_sftp_session do |channel|
99
+ channel.sends_packet(FXP_READ, :long, 0, :string, "handle", :int64, 512123, :long, 1024)
100
+ channel.gets_packet(FXP_DATA, :long, 0, :string, "this is some data!")
101
+ end
102
+
103
+ assert_command_with_callback(:read, "handle", 512123, 1024) do |response|
104
+ assert response.ok?
105
+ assert_equal "this is some data!", response[:data]
106
+ end
107
+ end
108
+
109
+ def test_read_bang_should_block_and_return_data
110
+ expect_sftp_session do |channel|
111
+ channel.sends_packet(FXP_READ, :long, 0, :string, "handle", :int64, 512123, :long, 1024)
112
+ channel.gets_packet(FXP_DATA, :long, 0, :string, "this is some data!")
113
+ end
114
+
115
+ data = assert_synchronous_command(:read!, "handle", 512123, 1024)
116
+ assert_equal "this is some data!", data
117
+ end
118
+
119
+ def test_read_bang_should_block_and_return_nil_on_eof
120
+ expect_sftp_session do |channel|
121
+ channel.sends_packet(FXP_READ, :long, 0, :string, "handle", :int64, 512123, :long, 1024)
122
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 1)
123
+ end
124
+
125
+ data = assert_synchronous_command(:read!, "handle", 512123, 1024)
126
+ assert_nil data
127
+ end
128
+
129
+ def test_write_should_send_write_request_and_invoke_callback
130
+ expect_sftp_session do |channel|
131
+ channel.sends_packet(FXP_WRITE, :long, 0, :string, "handle", :int64, 512123, :string, "this is some data!")
132
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
133
+ end
134
+
135
+ assert_command_with_callback(:write, "handle", 512123, "this is some data!") do |response|
136
+ assert response.ok?
137
+ end
138
+ end
139
+
140
+ def test_write_bang_should_block_and_return_response
141
+ expect_sftp_session do |channel|
142
+ channel.sends_packet(FXP_WRITE, :long, 0, :string, "handle", :int64, 512123, :string, "this is some data!")
143
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
144
+ end
145
+
146
+ response = assert_synchronous_command(:write!, "handle", 512123, "this is some data!")
147
+ assert response.ok?
148
+ end
149
+
150
+ def test_v1_lstat_should_send_lstat_request_and_invoke_callback
151
+ expect_sftp_session :server_version => 1 do |channel|
152
+ channel.sends_packet(FXP_LSTAT, :long, 0, :string, "/path/to/file")
153
+ channel.gets_packet(FXP_ATTRS, :long, 0, :long, 0xF, :int64, 123456, :long, 1, :long, 2, :long, 0765, :long, 123456789, :long, 234567890)
154
+ end
155
+
156
+ assert_command_with_callback(:lstat, "/path/to/file") do |response|
157
+ assert response.ok?
158
+ assert_equal 123456, response[:attrs].size
159
+ assert_equal 1, response[:attrs].uid
160
+ assert_equal 2, response[:attrs].gid
161
+ assert_equal 0765, response[:attrs].permissions
162
+ assert_equal 123456789, response[:attrs].atime
163
+ assert_equal 234567890, response[:attrs].mtime
164
+ end
165
+ end
166
+
167
+ def test_v4_lstat_should_send_default_flags_parameter
168
+ expect_sftp_session :server_version => 4 do |channel|
169
+ channel.sends_packet(FXP_LSTAT, :long, 0, :string, "/path/to/file", :long, 0x800001fd)
170
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
171
+ end
172
+
173
+ assert_command_with_callback(:lstat, "/path/to/file")
174
+ end
175
+
176
+ def test_v4_lstat_should_honor_flags_parameter
177
+ expect_sftp_session :server_version => 4 do |channel|
178
+ channel.sends_packet(FXP_LSTAT, :long, 0, :string, "/path/to/file", :long, 0x1)
179
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
180
+ end
181
+
182
+ assert_command_with_callback(:lstat, "/path/to/file", 0x1)
183
+ end
184
+
185
+ def test_lstat_bang_should_block_and_return_attrs
186
+ expect_sftp_session :server_version => 1 do |channel|
187
+ channel.sends_packet(FXP_LSTAT, :long, 0, :string, "/path/to/file")
188
+ channel.gets_packet(FXP_ATTRS, :long, 0, :long, 0xF, :int64, 123456, :long, 1, :long, 2, :long, 0765, :long, 123456789, :long, 234567890)
189
+ end
190
+
191
+ attrs = assert_synchronous_command(:lstat!, "/path/to/file")
192
+
193
+ assert_equal 123456, attrs.size
194
+ assert_equal 1, attrs.uid
195
+ assert_equal 2, attrs.gid
196
+ assert_equal 0765, attrs.permissions
197
+ assert_equal 123456789, attrs.atime
198
+ assert_equal 234567890, attrs.mtime
199
+ end
200
+
201
+ def test_v1_fstat_should_send_fstat_request_and_invoke_callback
202
+ expect_sftp_session :server_version => 1 do |channel|
203
+ channel.sends_packet(FXP_FSTAT, :long, 0, :string, "handle")
204
+ channel.gets_packet(FXP_ATTRS, :long, 0, :long, 0xF, :int64, 123456, :long, 1, :long, 2, :long, 0765, :long, 123456789, :long, 234567890)
205
+ end
206
+
207
+ assert_command_with_callback(:fstat, "handle") do |response|
208
+ assert response.ok?
209
+ assert_equal 123456, response[:attrs].size
210
+ assert_equal 1, response[:attrs].uid
211
+ assert_equal 2, response[:attrs].gid
212
+ assert_equal 0765, response[:attrs].permissions
213
+ assert_equal 123456789, response[:attrs].atime
214
+ assert_equal 234567890, response[:attrs].mtime
215
+ end
216
+ end
217
+
218
+ def test_v4_fstat_should_send_default_flags_parameter
219
+ expect_sftp_session :server_version => 4 do |channel|
220
+ channel.sends_packet(FXP_FSTAT, :long, 0, :string, "handle", :long, 0x800001fd)
221
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
222
+ end
223
+
224
+ assert_command_with_callback(:fstat, "handle")
225
+ end
226
+
227
+ def test_v4_fstat_should_honor_flags_parameter
228
+ expect_sftp_session :server_version => 4 do |channel|
229
+ channel.sends_packet(FXP_FSTAT, :long, 0, :string, "handle", :long, 0x1)
230
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
231
+ end
232
+
233
+ assert_command_with_callback(:fstat, "handle", 0x1)
234
+ end
235
+
236
+ def test_fstat_bang_should_block_and_return_attrs
237
+ expect_sftp_session :server_version => 1 do |channel|
238
+ channel.sends_packet(FXP_FSTAT, :long, 0, :string, "handle")
239
+ channel.gets_packet(FXP_ATTRS, :long, 0, :long, 0xF, :int64, 123456, :long, 1, :long, 2, :long, 0765, :long, 123456789, :long, 234567890)
240
+ end
241
+
242
+ attrs = assert_synchronous_command(:fstat!, "handle")
243
+
244
+ assert_equal 123456, attrs.size
245
+ assert_equal 1, attrs.uid
246
+ assert_equal 2, attrs.gid
247
+ assert_equal 0765, attrs.permissions
248
+ assert_equal 123456789, attrs.atime
249
+ assert_equal 234567890, attrs.mtime
250
+ end
251
+
252
+ def test_v1_setstat_should_send_v1_attributes
253
+ expect_sftp_session :server_version => 1 do |channel|
254
+ channel.sends_packet(FXP_SETSTAT, :long, 0, :string, "/path/to/file", :long, 0xc, :long, 0765, :long, 1234567890, :long, 2345678901)
255
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
256
+ end
257
+
258
+ assert_command_with_callback(:setstat, "/path/to/file", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901)
259
+ end
260
+
261
+ def test_v4_setstat_should_send_v4_attributes
262
+ expect_sftp_session :server_version => 4 do |channel|
263
+ channel.sends_packet(FXP_SETSTAT, :long, 0, :string, "/path/to/file", :long, 0x2c, :byte, 1, :long, 0765, :int64, 1234567890, :int64, 2345678901)
264
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
265
+ end
266
+
267
+ assert_command_with_callback(:setstat, "/path/to/file", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901)
268
+ end
269
+
270
+ def test_v6_setstat_should_send_v6_attributes
271
+ expect_sftp_session :server_version => 6 do |channel|
272
+ channel.sends_packet(FXP_SETSTAT, :long, 0, :string, "/path/to/file", :long, 0x102c, :byte, 1, :long, 0765, :int64, 1234567890, :int64, 2345678901, :string, "text/plain")
273
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
274
+ end
275
+
276
+ assert_command_with_callback(:setstat, "/path/to/file", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901, :mime_type => "text/plain")
277
+ end
278
+
279
+ def test_setstat_bang_should_block_and_return_response
280
+ expect_sftp_session :server_version => 1 do |channel|
281
+ channel.sends_packet(FXP_SETSTAT, :long, 0, :string, "/path/to/file", :long, 0xc, :long, 0765, :long, 1234567890, :long, 2345678901)
282
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
283
+ end
284
+
285
+ response = assert_synchronous_command(:setstat!, "/path/to/file", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901)
286
+ assert response.ok?
287
+ end
288
+
289
+ def test_v1_fsetstat_should_send_v1_attributes
290
+ expect_sftp_session :server_version => 1 do |channel|
291
+ channel.sends_packet(FXP_FSETSTAT, :long, 0, :string, "handle", :long, 0xc, :long, 0765, :long, 1234567890, :long, 2345678901)
292
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
293
+ end
294
+
295
+ assert_command_with_callback(:fsetstat, "handle", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901)
296
+ end
297
+
298
+ def test_v4_fsetstat_should_send_v4_attributes
299
+ expect_sftp_session :server_version => 4 do |channel|
300
+ channel.sends_packet(FXP_FSETSTAT, :long, 0, :string, "handle", :long, 0x2c, :byte, 1, :long, 0765, :int64, 1234567890, :int64, 2345678901)
301
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
302
+ end
303
+
304
+ assert_command_with_callback(:fsetstat, "handle", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901)
305
+ end
306
+
307
+ def test_v6_fsetstat_should_send_v6_attributes
308
+ expect_sftp_session :server_version => 6 do |channel|
309
+ channel.sends_packet(FXP_FSETSTAT, :long, 0, :string, "handle", :long, 0x102c, :byte, 1, :long, 0765, :int64, 1234567890, :int64, 2345678901, :string, "text/plain")
310
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
311
+ end
312
+
313
+ assert_command_with_callback(:fsetstat, "handle", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901, :mime_type => "text/plain")
314
+ end
315
+
316
+ def test_fsetstat_bang_should_block_and_return_response
317
+ expect_sftp_session :server_version => 1 do |channel|
318
+ channel.sends_packet(FXP_FSETSTAT, :long, 0, :string, "handle", :long, 0xc, :long, 0765, :long, 1234567890, :long, 2345678901)
319
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
320
+ end
321
+
322
+ response = assert_synchronous_command(:fsetstat!, "handle", :permissions => 0765, :atime => 1234567890, :mtime => 2345678901)
323
+ assert response.ok?
324
+ end
325
+
326
+ def test_opendir_should_send_opendir_request_and_invoke_callback
327
+ expect_sftp_session do |channel|
328
+ channel.sends_packet(FXP_OPENDIR, :long, 0, :string, "/path/to/dir")
329
+ channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
330
+ end
331
+
332
+ assert_command_with_callback(:opendir, "/path/to/dir")
333
+ end
334
+
335
+ def test_opendir_bang_should_block_and_return_handle
336
+ expect_sftp_session do |channel|
337
+ channel.sends_packet(FXP_OPENDIR, :long, 0, :string, "/path/to/dir")
338
+ channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
339
+ end
340
+
341
+ handle = assert_synchronous_command(:opendir!, "/path/to/dir")
342
+ assert_equal "handle", handle
343
+ end
344
+
345
+ def test_readdir_should_send_readdir_request_and_invoke_callback
346
+ expect_sftp_session do |channel|
347
+ channel.sends_packet(FXP_READDIR, :long, 0, :string, "handle")
348
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 1)
349
+ end
350
+
351
+ assert_command_with_callback(:readdir, "handle") { |r| assert r.eof? }
352
+ end
353
+
354
+ def test_readdir_bang_should_block_and_return_names_array
355
+ expect_sftp_session :server_version => 1 do |channel|
356
+ channel.sends_packet(FXP_READDIR, :long, 0, :string, "handle")
357
+ channel.gets_packet(FXP_NAME, :long, 0, :long, 2,
358
+ :string, "first", :string, "longfirst", :long, 0x0,
359
+ :string, "next", :string, "longnext", :long, 0x0)
360
+ end
361
+
362
+ names = assert_synchronous_command(:readdir!, "handle")
363
+ assert_equal 2, names.length
364
+ assert_equal %w(first next), names.map { |n| n.name }
365
+ end
366
+
367
+ def test_remove_should_send_remove_packet
368
+ expect_sftp_session do |channel|
369
+ channel.sends_packet(FXP_REMOVE, :long, 0, :string, "/path/to/file")
370
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
371
+ end
372
+
373
+ assert_command_with_callback(:remove, "/path/to/file")
374
+ end
375
+
376
+ def test_remove_bang_should_block_and_return_response
377
+ expect_sftp_session do |channel|
378
+ channel.sends_packet(FXP_REMOVE, :long, 0, :string, "/path/to/file")
379
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
380
+ end
381
+
382
+ response = assert_synchronous_command(:remove!, "/path/to/file")
383
+ assert response.ok?
384
+ end
385
+
386
+ def test_mkdir_should_send_mkdir_packet
387
+ expect_sftp_session do |channel|
388
+ channel.sends_packet(FXP_MKDIR, :long, 0, :string, "/path/to/dir", :long, 0x4, :byte, 1, :long, 0765)
389
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
390
+ end
391
+
392
+ assert_command_with_callback(:mkdir, "/path/to/dir", :permissions => 0765)
393
+ end
394
+
395
+ def test_mkdir_bang_should_block_and_return_response
396
+ expect_sftp_session do |channel|
397
+ channel.sends_packet(FXP_MKDIR, :long, 0, :string, "/path/to/dir", :long, 0x4, :byte, 1, :long, 0765)
398
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
399
+ end
400
+
401
+ response = assert_synchronous_command(:mkdir!, "/path/to/dir", :permissions => 0765)
402
+ assert response.ok?
403
+ end
404
+
405
+ def test_rmdir_should_send_rmdir_packet
406
+ expect_sftp_session do |channel|
407
+ channel.sends_packet(FXP_RMDIR, :long, 0, :string, "/path/to/dir")
408
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
409
+ end
410
+
411
+ assert_command_with_callback(:rmdir, "/path/to/dir")
412
+ end
413
+
414
+ def test_rmdir_bang_should_block_and_return_response
415
+ expect_sftp_session do |channel|
416
+ channel.sends_packet(FXP_RMDIR, :long, 0, :string, "/path/to/dir")
417
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
418
+ end
419
+
420
+ response = assert_synchronous_command(:rmdir!, "/path/to/dir")
421
+ assert response.ok?
422
+ end
423
+
424
+ def test_realpath_should_send_realpath_packet
425
+ expect_sftp_session do |channel|
426
+ channel.sends_packet(FXP_REALPATH, :long, 0, :string, "/path/to/dir")
427
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
428
+ end
429
+
430
+ assert_command_with_callback(:realpath, "/path/to/dir")
431
+ end
432
+
433
+ def test_realpath_bang_should_block_and_return_names_item
434
+ expect_sftp_session do |channel|
435
+ channel.sends_packet(FXP_REALPATH, :long, 0, :string, "/path/to/dir")
436
+ channel.gets_packet(FXP_NAME, :long, 0, :long, 1, :string, "dir", :long, 0x0, :long, 2)
437
+ end
438
+
439
+ name = assert_synchronous_command(:realpath!, "/path/to/dir")
440
+ assert_equal "dir", name.name
441
+ end
442
+
443
+ def test_v1_stat_should_send_stat_request_and_invoke_callback
444
+ expect_sftp_session :server_version => 1 do |channel|
445
+ channel.sends_packet(FXP_STAT, :long, 0, :string, "/path/to/file")
446
+ channel.gets_packet(FXP_ATTRS, :long, 0, :long, 0xF, :int64, 123456, :long, 1, :long, 2, :long, 0765, :long, 123456789, :long, 234567890)
447
+ end
448
+
449
+ assert_command_with_callback(:stat, "/path/to/file") do |response|
450
+ assert response.ok?
451
+ assert_equal 123456, response[:attrs].size
452
+ assert_equal 1, response[:attrs].uid
453
+ assert_equal 2, response[:attrs].gid
454
+ assert_equal 0765, response[:attrs].permissions
455
+ assert_equal 123456789, response[:attrs].atime
456
+ assert_equal 234567890, response[:attrs].mtime
457
+ end
458
+ end
459
+
460
+ def test_v4_stat_should_send_default_flags_parameter
461
+ expect_sftp_session :server_version => 4 do |channel|
462
+ channel.sends_packet(FXP_STAT, :long, 0, :string, "/path/to/file", :long, 0x800001fd)
463
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
464
+ end
465
+
466
+ assert_command_with_callback(:stat, "/path/to/file")
467
+ end
468
+
469
+ def test_v4_stat_should_honor_flags_parameter
470
+ expect_sftp_session :server_version => 4 do |channel|
471
+ channel.sends_packet(FXP_STAT, :long, 0, :string, "/path/to/file", :long, 0x1)
472
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
473
+ end
474
+
475
+ assert_command_with_callback(:stat, "/path/to/file", 0x1)
476
+ end
477
+
478
+ def test_stat_bang_should_block_and_return_attrs
479
+ expect_sftp_session :server_version => 1 do |channel|
480
+ channel.sends_packet(FXP_STAT, :long, 0, :string, "/path/to/file")
481
+ channel.gets_packet(FXP_ATTRS, :long, 0, :long, 0xF, :int64, 123456, :long, 1, :long, 2, :long, 0765, :long, 123456789, :long, 234567890)
482
+ end
483
+
484
+ attrs = assert_synchronous_command(:stat!, "/path/to/file")
485
+
486
+ assert_equal 123456, attrs.size
487
+ assert_equal 1, attrs.uid
488
+ assert_equal 2, attrs.gid
489
+ assert_equal 0765, attrs.permissions
490
+ assert_equal 123456789, attrs.atime
491
+ assert_equal 234567890, attrs.mtime
492
+ end
493
+
494
+ def test_v1_rename_should_be_unimplemented
495
+ assert_not_implemented 1, :rename, "from", "to"
496
+ end
497
+
498
+ def test_v2_rename_should_send_rename_packet
499
+ expect_sftp_session :server_version => 2 do |channel|
500
+ channel.sends_packet(FXP_RENAME, :long, 0, :string, "from", :string, "to")
501
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
502
+ end
503
+
504
+ assert_command_with_callback(:rename, "from", "to")
505
+ end
506
+
507
+ def test_v5_rename_should_send_rename_packet_and_default_flags
508
+ expect_sftp_session :server_version => 5 do |channel|
509
+ channel.sends_packet(FXP_RENAME, :long, 0, :string, "from", :string, "to", :long, 0)
510
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
511
+ end
512
+
513
+ assert_command_with_callback(:rename, "from", "to")
514
+ end
515
+
516
+ def test_v5_rename_should_send_rename_packet_and_honor_flags
517
+ expect_sftp_session :server_version => 5 do |channel|
518
+ channel.sends_packet(FXP_RENAME, :long, 0, :string, "from", :string, "to", :long, 1)
519
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
520
+ end
521
+
522
+ assert_command_with_callback(:rename, "from", "to", 1)
523
+ end
524
+
525
+ def test_rename_bang_should_block_and_return_response
526
+ expect_sftp_session :server_version => 2 do |channel|
527
+ channel.sends_packet(FXP_RENAME, :long, 0, :string, "from", :string, "to")
528
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
529
+ end
530
+
531
+ response = assert_synchronous_command(:rename!, "from", "to")
532
+ assert response.ok?
533
+ end
534
+
535
+ def test_v2_readlink_should_be_unimplemented
536
+ assert_not_implemented 2, :readlink, "/path/to/link"
537
+ end
538
+
539
+ def test_v3_readlink_should_send_readlink_packet
540
+ expect_sftp_session :server_version => 3 do |channel|
541
+ channel.sends_packet(FXP_READLINK, :long, 0, :string, "/path/to/link")
542
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 2)
543
+ end
544
+
545
+ assert_command_with_callback(:readlink, "/path/to/link")
546
+ end
547
+
548
+ def test_readlink_bang_should_block_and_return_name
549
+ expect_sftp_session :server_version => 3 do |channel|
550
+ channel.sends_packet(FXP_READLINK, :long, 0, :string, "/path/to/link")
551
+ channel.gets_packet(FXP_NAME, :long, 0, :long, 1, :string, "target", :string, "longtarget", :long, 0x0)
552
+ end
553
+
554
+ name = assert_synchronous_command(:readlink!, "/path/to/link")
555
+ assert_equal "target", name.name
556
+ end
557
+
558
+ def test_v2_symlink_should_be_unimplemented
559
+ assert_not_implemented 2, :symlink, "/path/to/source", "/path/to/link"
560
+ end
561
+
562
+ def test_v3_symlink_should_send_symlink_packet
563
+ expect_sftp_session :server_version => 3 do |channel|
564
+ channel.sends_packet(FXP_SYMLINK, :long, 0, :string, "/path/to/source", :string, "/path/to/link")
565
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
566
+ end
567
+
568
+ assert_command_with_callback(:symlink, "/path/to/source", "/path/to/link")
569
+ end
570
+
571
+ def test_v6_symlink_should_send_link_packet
572
+ expect_sftp_session :server_version => 6 do |channel|
573
+ channel.sends_packet(FXP_LINK, :long, 0, :string, "/path/to/link", :string, "/path/to/source", :bool, true)
574
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
575
+ end
576
+
577
+ assert_command_with_callback(:symlink, "/path/to/link", "/path/to/source")
578
+ end
579
+
580
+ def test_symlink_bang_should_block_and_return_response
581
+ expect_sftp_session :server_version => 3 do |channel|
582
+ channel.sends_packet(FXP_SYMLINK, :long, 0, :string, "/path/to/source", :string, "/path/to/link")
583
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
584
+ end
585
+
586
+ response = assert_synchronous_command(:symlink!, "/path/to/source", "/path/to/link")
587
+ assert response.ok?
588
+ end
589
+
590
+ def test_v5_link_should_be_unimplemented
591
+ assert_not_implemented 5, :link, "/path/to/source", "/path/to/link", true
592
+ end
593
+
594
+ def test_v6_link_should_send_link_packet
595
+ expect_sftp_session :server_version => 6 do |channel|
596
+ channel.sends_packet(FXP_LINK, :long, 0, :string, "/path/to/link", :string, "/path/to/source", :bool, true)
597
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
598
+ end
599
+
600
+ assert_command_with_callback(:link, "/path/to/link", "/path/to/source", true)
601
+ end
602
+
603
+ def test_link_bang_should_block_and_return_response
604
+ expect_sftp_session :server_version => 6 do |channel|
605
+ channel.sends_packet(FXP_LINK, :long, 0, :string, "/path/to/link", :string, "/path/to/source", :bool, true)
606
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
607
+ end
608
+
609
+ response = assert_synchronous_command(:link!, "/path/to/link", "/path/to/source", true)
610
+ assert response.ok?
611
+ end
612
+
613
+ def test_v5_block_should_be_unimplemented
614
+ assert_not_implemented 5, :block, "handle", 12345, 67890, 0xabcd
615
+ end
616
+
617
+ def test_v6_block_should_send_block_packet
618
+ expect_sftp_session :server_version => 6 do |channel|
619
+ channel.sends_packet(FXP_BLOCK, :long, 0, :string, "handle", :int64, 12345, :int64, 67890, :long, 0xabcd)
620
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
621
+ end
622
+
623
+ assert_command_with_callback(:block, "handle", 12345, 67890, 0xabcd)
624
+ end
625
+
626
+ def test_block_bang_should_block_and_return_response
627
+ expect_sftp_session :server_version => 6 do |channel|
628
+ channel.sends_packet(FXP_BLOCK, :long, 0, :string, "handle", :int64, 12345, :int64, 67890, :long, 0xabcd)
629
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
630
+ end
631
+
632
+ response = assert_synchronous_command(:block!, "handle", 12345, 67890, 0xabcd)
633
+ assert response.ok?
634
+ end
635
+
636
+ def test_v5_unblock_should_be_unimplemented
637
+ assert_not_implemented 5, :unblock, "handle", 12345, 67890
638
+ end
639
+
640
+ def test_v6_unblock_should_send_block_packet
641
+ expect_sftp_session :server_version => 6 do |channel|
642
+ channel.sends_packet(FXP_UNBLOCK, :long, 0, :string, "handle", :int64, 12345, :int64, 67890)
643
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
644
+ end
645
+
646
+ assert_command_with_callback(:unblock, "handle", 12345, 67890)
647
+ end
648
+
649
+ def test_unblock_bang_should_block_and_return_response
650
+ expect_sftp_session :server_version => 6 do |channel|
651
+ channel.sends_packet(FXP_UNBLOCK, :long, 0, :string, "handle", :int64, 12345, :int64, 67890)
652
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, 0)
653
+ end
654
+
655
+ response = assert_synchronous_command(:unblock!, "handle", 12345, 67890)
656
+ assert response.ok?
657
+ end
658
+
659
+ private
660
+
661
+ def assert_not_implemented(server_version, command, *args)
662
+ expect_sftp_session :server_version => 1
663
+ sftp.connect!
664
+ assert_raises(NotImplementedError) { sftp.send(command, *args) }
665
+ end
666
+
667
+ def assert_command_with_callback(command, *args)
668
+ called = false
669
+ assert_scripted_command do
670
+ sftp.send(command, *args) do |response|
671
+ called = true
672
+ yield response if block_given?
673
+ end
674
+ end
675
+ assert called, "expected callback to be invoked, but it wasn't"
676
+ end
677
+
678
+ def assert_synchronous_command(command, *args)
679
+ assert_scripted_command do
680
+ sequence = [:start]
681
+ result = sftp.send(command, *args) do |response|
682
+ sequence << :done
683
+ yield response if block_given?
684
+ end
685
+ sequence << :after
686
+ assert_equal [:start, :done, :after], sequence, "expected #{command} to be synchronous"
687
+ return result
688
+ end
689
+ end
690
+
691
+ def assert_successful_open(*args)
692
+ assert_command_with_callback(:open, *args) do |response|
693
+ assert response.ok?
694
+ assert_equal "handle", response[:handle]
695
+ end
696
+ end
697
+
698
+ def expect_open(path, mode, perms, options={})
699
+ version = options[:server_version] || 6
700
+
701
+ fail = options.delete(:fail)
702
+
703
+ attrs = [:long, perms ? 0x4 : 0]
704
+ attrs += [:byte, 1] if version >= 4
705
+ attrs += [:long, perms] if perms
706
+
707
+ expect_sftp_session(options) do |channel|
708
+ if version >= 5
709
+ flags, access = case mode
710
+ when "r" then
711
+ [FV5::OPEN_EXISTING, ACE::Mask::READ_DATA | ACE::Mask::READ_ATTRIBUTES]
712
+ when "w" then
713
+ [FV5::CREATE_TRUNCATE, ACE::Mask::WRITE_DATA | ACE::Mask::WRITE_ATTRIBUTES]
714
+ when "rw" then
715
+ [FV5::OPEN_OR_CREATE, ACE::Mask::READ_DATA | ACE::Mask::READ_ATTRIBUTES | ACE::Mask::WRITE_DATA | ACE::Mask::WRITE_ATTRIBUTES]
716
+ when "a" then
717
+ [FV5::OPEN_OR_CREATE | FV5::APPEND_DATA, ACE::Mask::WRITE_DATA | ACE::Mask::WRITE_ATTRIBUTES | ACE::Mask::APPEND_DATA]
718
+ else raise ArgumentError, "unsupported mode #{mode.inspect}"
719
+ end
720
+
721
+ channel.sends_packet(FXP_OPEN, :long, 0, :string, path, :long, access, :long, flags, *attrs)
722
+ else
723
+ flags = case mode
724
+ when "r" then FV1::READ
725
+ when "w" then FV1::WRITE | FV1::TRUNC | FV1::CREAT
726
+ when "rw" then FV1::WRITE | FV1::READ
727
+ when "a" then FV1::APPEND | FV1::WRITE | FV1::CREAT
728
+ else raise ArgumentError, "unsupported mode #{mode.inspect}"
729
+ end
730
+
731
+ channel.sends_packet(FXP_OPEN, :long, 0, :string, path, :long, flags, *attrs)
732
+ end
733
+
734
+ if fail
735
+ channel.gets_packet(FXP_STATUS, :long, 0, :long, fail)
736
+ else
737
+ channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
738
+ end
739
+ end
740
+ end
741
+ end