net-sftp 2.1.2 → 2.1.3.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,233 +0,0 @@
1
- require "common"
2
-
3
- class UploadTest < Net::SFTP::TestCase
4
- def setup
5
- prepare_progress!
6
- end
7
-
8
- def test_upload_file_should_send_file_contents
9
- expect_file_transfer("/path/to/local", "/path/to/remote", "here are the contents")
10
- assert_scripted_command { sftp.upload("/path/to/local", "/path/to/remote") }
11
- end
12
-
13
- def test_upload_file_without_remote_uses_filename_of_local_file
14
- expect_file_transfer("/path/to/local", "local", "here are the contents")
15
-
16
- assert_scripted_command do
17
- sftp.upload("/path/to/local") { |*args| record_progress(args) }
18
- end
19
-
20
- assert_progress_reported_open(:remote => "local")
21
- assert_progress_reported_put(0, "here are the contents", :remote => "local")
22
- assert_progress_reported_close(:remote => "local")
23
- assert_progress_reported_finish
24
- assert_no_more_reported_events
25
- end
26
-
27
- def test_upload_file_with_progress_should_report_progress
28
- expect_file_transfer("/path/to/local", "/path/to/remote", "here are the contents")
29
-
30
- assert_scripted_command do
31
- sftp.upload("/path/to/local", "/path/to/remote") { |*args| record_progress(args) }
32
- end
33
-
34
- assert_progress_reported_open(:remote => "/path/to/remote")
35
- assert_progress_reported_put(0, "here are the contents", :remote => "/path/to/remote")
36
- assert_progress_reported_close(:remote => "/path/to/remote")
37
- assert_progress_reported_finish
38
- assert_no_more_reported_events
39
- end
40
-
41
- def test_upload_file_with_progress_handler_should_report_progress
42
- expect_file_transfer("/path/to/local", "/path/to/remote", "here are the contents")
43
-
44
- assert_scripted_command do
45
- sftp.upload("/path/to/local", "/path/to/remote", :progress => ProgressHandler.new(@progress))
46
- end
47
-
48
- assert_progress_reported_open(:remote => "/path/to/remote")
49
- assert_progress_reported_put(0, "here are the contents", :remote => "/path/to/remote")
50
- assert_progress_reported_close(:remote => "/path/to/remote")
51
- assert_progress_reported_finish
52
- assert_no_more_reported_events
53
- end
54
-
55
- def test_upload_file_should_read_chunks_of_size(requested_size=nil)
56
- size = requested_size || Net::SFTP::Operations::Upload::DEFAULT_READ_SIZE
57
- expect_sftp_session :server_version => 3 do |channel|
58
- channel.sends_packet(FXP_OPEN, :long, 0, :string, "/path/to/remote", :long, 0x1A, :long, 0)
59
- channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
60
- channel.sends_packet(FXP_WRITE, :long, 1, :string, "handle", :int64, 0, :string, "a" * size)
61
- channel.sends_packet(FXP_WRITE, :long, 2, :string, "handle", :int64, size, :string, "b" * size)
62
- channel.sends_packet(FXP_WRITE, :long, 3, :string, "handle", :int64, size*2, :string, "c" * size)
63
- channel.gets_packet(FXP_STATUS, :long, 1, :long, 0)
64
- channel.sends_packet(FXP_WRITE, :long, 4, :string, "handle", :int64, size*3, :string, "d" * size)
65
- channel.gets_packet(FXP_STATUS, :long, 2, :long, 0)
66
- channel.sends_packet(FXP_CLOSE, :long, 5, :string, "handle")
67
- channel.gets_packet(FXP_STATUS, :long, 3, :long, 0)
68
- channel.gets_packet(FXP_STATUS, :long, 4, :long, 0)
69
- channel.gets_packet(FXP_STATUS, :long, 5, :long, 0)
70
- end
71
-
72
- expect_file("/path/to/local", "a" * size + "b" * size + "c" * size + "d" * size)
73
-
74
- assert_scripted_command do
75
- opts = {}
76
- opts[:read_size] = size if requested_size
77
- sftp.upload("/path/to/local", "/path/to/remote", opts)
78
- end
79
- end
80
-
81
- def test_upload_file_with_custom_read_size_should_read_chunks_of_that_size
82
- test_upload_file_should_read_chunks_of_size(100)
83
- end
84
-
85
- def test_upload_file_with_custom_requests_should_start_that_many_writes
86
- size = Net::SFTP::Operations::Upload::DEFAULT_READ_SIZE
87
- expect_sftp_session :server_version => 3 do |channel|
88
- channel.sends_packet(FXP_OPEN, :long, 0, :string, "/path/to/remote", :long, 0x1A, :long, 0)
89
- channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
90
- channel.sends_packet(FXP_WRITE, :long, 1, :string, "handle", :int64, 0, :string, "a" * size)
91
- channel.sends_packet(FXP_WRITE, :long, 2, :string, "handle", :int64, size, :string, "b" * size)
92
- channel.sends_packet(FXP_WRITE, :long, 3, :string, "handle", :int64, size*2, :string, "c" * size)
93
- channel.sends_packet(FXP_WRITE, :long, 4, :string, "handle", :int64, size*3, :string, "d" * size)
94
- channel.gets_packet(FXP_STATUS, :long, 1, :long, 0)
95
- channel.sends_packet(FXP_CLOSE, :long, 5, :string, "handle")
96
- channel.gets_packet(FXP_STATUS, :long, 2, :long, 0)
97
- channel.gets_packet(FXP_STATUS, :long, 3, :long, 0)
98
- channel.gets_packet(FXP_STATUS, :long, 4, :long, 0)
99
- channel.gets_packet(FXP_STATUS, :long, 5, :long, 0)
100
- end
101
-
102
- expect_file("/path/to/local", "a" * size + "b" * size + "c" * size + "d" * size)
103
-
104
- assert_scripted_command do
105
- sftp.upload("/path/to/local", "/path/to/remote", :requests => 3)
106
- end
107
- end
108
-
109
- def test_upload_directory_should_mirror_directory_structure_remotely
110
- prepare_directory
111
-
112
- assert_scripted_command do
113
- sftp.upload("/path/to/local", "/path/to/remote", :mkdir => true)
114
- end
115
- end
116
-
117
- def test_upload_directory_with_handler_should_report_progress
118
- prepare_directory
119
-
120
- assert_scripted_command do
121
- sftp.upload("/path/to/local", "/path/to/remote", :mkdir => true) { |*args| record_progress(args) }
122
- end
123
-
124
- assert_progress_reported_open(:remote => "/path/to/remote/file1")
125
- assert_progress_reported_open(:remote => "/path/to/remote/file2")
126
- assert_progress_reported_open(:remote => "/path/to/remote/file3")
127
- assert_progress_reported_mkdir("/path/to/remote/subdir")
128
- assert_progress_reported_open(:remote => "/path/to/remote/subdir/other1")
129
- assert_progress_reported_open(:remote => "/path/to/remote/subdir/other2")
130
- assert_progress_reported_put(0, "contents of file1", :remote => "/path/to/remote/file1")
131
- assert_progress_reported_put(0, "contents of file2", :remote => "/path/to/remote/file2")
132
- assert_progress_reported_put(0, "contents of file3", :remote => "/path/to/remote/file3")
133
- assert_progress_reported_close(:remote => "/path/to/remote/file1")
134
- assert_progress_reported_put(0, "contents of other1", :remote => "/path/to/remote/subdir/other1")
135
- assert_progress_reported_put(0, "contents of other2", :remote => "/path/to/remote/subdir/other2")
136
- assert_progress_reported_close(:remote => "/path/to/remote/file2")
137
- assert_progress_reported_close(:remote => "/path/to/remote/file3")
138
- assert_progress_reported_close(:remote => "/path/to/remote/subdir/other1")
139
- assert_progress_reported_close(:remote => "/path/to/remote/subdir/other2")
140
- assert_progress_reported_finish
141
- assert_no_more_reported_events
142
- end
143
-
144
- def test_upload_io_should_send_io_as_file
145
- expect_sftp_session :server_version => 3 do |channel|
146
- channel.sends_packet(FXP_OPEN, :long, 0, :string, "/path/to/remote", :long, 0x1A, :long, 0)
147
- channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
148
- channel.sends_packet(FXP_WRITE, :long, 1, :string, "handle", :int64, 0, :string, "this is some text")
149
- channel.sends_packet(FXP_CLOSE, :long, 2, :string, "handle")
150
- channel.gets_packet(FXP_STATUS, :long, 1, :long, 0)
151
- channel.gets_packet(FXP_STATUS, :long, 2, :long, 0)
152
- end
153
-
154
- assert_scripted_command do
155
- sftp.upload(StringIO.new("this is some text"), "/path/to/remote")
156
- end
157
- end
158
-
159
- private
160
-
161
- def prepare_directory
162
- expect_directory("/path/to/local", %w(. .. file1 file2 file3 subdir))
163
- expect_directory("/path/to/local/subdir", %w(. .. other1 other2))
164
- expect_file("/path/to/local/file1", "contents of file1")
165
- expect_file("/path/to/local/file2", "contents of file2")
166
- expect_file("/path/to/local/file3", "contents of file3")
167
- expect_file("/path/to/local/subdir/other1", "contents of other1")
168
- expect_file("/path/to/local/subdir/other2", "contents of other2")
169
-
170
- expect_sftp_session :server_version => 3 do |ch|
171
- ch.sends_packet(FXP_MKDIR, :long, 0, :string, "/path/to/remote", :long, 0)
172
- ch.gets_packet(FXP_STATUS, :long, 0, :long, 0)
173
- ch.sends_packet(FXP_OPEN, :long, 1, :string, "/path/to/remote/file1", :long, 0x1A, :long, 0)
174
- ch.sends_packet(FXP_OPEN, :long, 2, :string, "/path/to/remote/file2", :long, 0x1A, :long, 0)
175
- ch.sends_packet(FXP_OPEN, :long, 3, :string, "/path/to/remote/file3", :long, 0x1A, :long, 0)
176
- ch.sends_packet(FXP_MKDIR, :long, 4, :string, "/path/to/remote/subdir", :long, 0)
177
- ch.sends_packet(FXP_OPEN, :long, 5, :string, "/path/to/remote/subdir/other1", :long, 0x1A, :long, 0)
178
- ch.sends_packet(FXP_OPEN, :long, 6, :string, "/path/to/remote/subdir/other2", :long, 0x1A, :long, 0)
179
- ch.gets_packet(FXP_HANDLE, :long, 1, :string, "hfile1")
180
- ch.sends_packet(FXP_WRITE, :long, 7, :string, "hfile1", :int64, 0, :string, "contents of file1")
181
- ch.gets_packet(FXP_HANDLE, :long, 2, :string, "hfile2")
182
- ch.sends_packet(FXP_WRITE, :long, 8, :string, "hfile2", :int64, 0, :string, "contents of file2")
183
- ch.gets_packet(FXP_HANDLE, :long, 3, :string, "hfile3")
184
- ch.sends_packet(FXP_WRITE, :long, 9, :string, "hfile3", :int64, 0, :string, "contents of file3")
185
- ch.gets_packet(FXP_STATUS, :long, 4, :long, 0)
186
- ch.gets_packet(FXP_HANDLE, :long, 5, :string, "hother1")
187
- ch.sends_packet(FXP_CLOSE, :long, 10, :string, "hfile1")
188
- ch.sends_packet(FXP_WRITE, :long, 11, :string, "hother1", :int64, 0, :string, "contents of other1")
189
- ch.gets_packet(FXP_HANDLE, :long, 6, :string, "hother2")
190
- ch.sends_packet(FXP_WRITE, :long, 12, :string, "hother2", :int64, 0, :string, "contents of other2")
191
- ch.gets_packet(FXP_STATUS, :long, 7, :long, 0)
192
- ch.sends_packet(FXP_CLOSE, :long, 13, :string, "hfile2")
193
- ch.gets_packet(FXP_STATUS, :long, 8, :long, 0)
194
- ch.sends_packet(FXP_CLOSE, :long, 14, :string, "hfile3")
195
- ch.gets_packet(FXP_STATUS, :long, 9, :long, 0)
196
- ch.sends_packet(FXP_CLOSE, :long, 15, :string, "hother1")
197
- ch.gets_packet(FXP_STATUS, :long, 10, :long, 0)
198
- ch.sends_packet(FXP_CLOSE, :long, 16, :string, "hother2")
199
- ch.gets_packet(FXP_STATUS, :long, 11, :long, 0)
200
- ch.gets_packet(FXP_STATUS, :long, 12, :long, 0)
201
- ch.gets_packet(FXP_STATUS, :long, 13, :long, 0)
202
- ch.gets_packet(FXP_STATUS, :long, 14, :long, 0)
203
- ch.gets_packet(FXP_STATUS, :long, 15, :long, 0)
204
- ch.gets_packet(FXP_STATUS, :long, 16, :long, 0)
205
- end
206
- end
207
-
208
- def expect_file(path, data)
209
- File.stubs(:directory?).with(path).returns(false)
210
- File.stubs(:exists?).with(path).returns(true)
211
- file = StringIO.new(data)
212
- file.stubs(:stat).returns(stub("stat", :size => data.length))
213
- File.stubs(:open).with(path, "rb").returns(file)
214
- end
215
-
216
- def expect_directory(path, entries)
217
- Dir.stubs(:entries).with(path).returns(entries)
218
- File.stubs(:directory?).with(path).returns(true)
219
- end
220
-
221
- def expect_file_transfer(local, remote, data)
222
- expect_sftp_session :server_version => 3 do |channel|
223
- channel.sends_packet(FXP_OPEN, :long, 0, :string, remote, :long, 0x1A, :long, 0)
224
- channel.gets_packet(FXP_HANDLE, :long, 0, :string, "handle")
225
- channel.sends_packet(FXP_WRITE, :long, 1, :string, "handle", :int64, 0, :string, data)
226
- channel.sends_packet(FXP_CLOSE, :long, 2, :string, "handle")
227
- channel.gets_packet(FXP_STATUS, :long, 1, :long, 0)
228
- channel.gets_packet(FXP_STATUS, :long, 2, :long, 0)
229
- end
230
-
231
- expect_file(local, data)
232
- end
233
- end