investtools-ftpd 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +5 -0
  3. data/.yardopts +7 -0
  4. data/Changelog.md +310 -0
  5. data/Gemfile +15 -0
  6. data/Gemfile.lock +93 -0
  7. data/LICENSE.md +9 -0
  8. data/README.md +371 -0
  9. data/Rakefile +14 -0
  10. data/VERSION +1 -0
  11. data/doc/benchmarks.md +82 -0
  12. data/doc/references.md +66 -0
  13. data/doc/rfc-compliance.md +292 -0
  14. data/examples/example.rb +275 -0
  15. data/examples/example_spec.rb +93 -0
  16. data/examples/hello_world.rb +32 -0
  17. data/features/example/eplf.feature +14 -0
  18. data/features/example/example.feature +18 -0
  19. data/features/example/read_only.feature +63 -0
  20. data/features/example/step_definitions/example_server.rb +11 -0
  21. data/features/ftp_server/abort.feature +13 -0
  22. data/features/ftp_server/allo.feature +33 -0
  23. data/features/ftp_server/append.feature +94 -0
  24. data/features/ftp_server/cdup.feature +36 -0
  25. data/features/ftp_server/command_errors.feature +13 -0
  26. data/features/ftp_server/concurrent_sessions.feature +14 -0
  27. data/features/ftp_server/delay_after_failed_login.feature +23 -0
  28. data/features/ftp_server/delete.feature +60 -0
  29. data/features/ftp_server/directory_navigation.feature +59 -0
  30. data/features/ftp_server/disconnect_after_failed_logins.feature +25 -0
  31. data/features/ftp_server/eprt.feature +55 -0
  32. data/features/ftp_server/epsv.feature +36 -0
  33. data/features/ftp_server/features.feature +38 -0
  34. data/features/ftp_server/file_structure.feature +43 -0
  35. data/features/ftp_server/get.feature +80 -0
  36. data/features/ftp_server/get_ipv6.feature +43 -0
  37. data/features/ftp_server/get_tls.feature +23 -0
  38. data/features/ftp_server/help.feature +21 -0
  39. data/features/ftp_server/implicit_tls.feature +23 -0
  40. data/features/ftp_server/invertability.feature +15 -0
  41. data/features/ftp_server/list.feature +94 -0
  42. data/features/ftp_server/list_tls.feature +29 -0
  43. data/features/ftp_server/logging.feature +11 -0
  44. data/features/ftp_server/login_auth_level_account.feature +51 -0
  45. data/features/ftp_server/login_auth_level_password.feature +59 -0
  46. data/features/ftp_server/login_auth_level_user.feature +31 -0
  47. data/features/ftp_server/max_connections.feature +39 -0
  48. data/features/ftp_server/mdtm.feature +53 -0
  49. data/features/ftp_server/mkdir.feature +70 -0
  50. data/features/ftp_server/mode.feature +43 -0
  51. data/features/ftp_server/name_list.feature +77 -0
  52. data/features/ftp_server/name_list_tls.feature +30 -0
  53. data/features/ftp_server/noop.feature +17 -0
  54. data/features/ftp_server/options.feature +17 -0
  55. data/features/ftp_server/pasv.feature +23 -0
  56. data/features/ftp_server/port.feature +49 -0
  57. data/features/ftp_server/put.feature +79 -0
  58. data/features/ftp_server/put_tls.feature +23 -0
  59. data/features/ftp_server/put_unique.feature +56 -0
  60. data/features/ftp_server/quit.feature +23 -0
  61. data/features/ftp_server/reinitialize.feature +13 -0
  62. data/features/ftp_server/rename.feature +97 -0
  63. data/features/ftp_server/rmdir.feature +71 -0
  64. data/features/ftp_server/site.feature +13 -0
  65. data/features/ftp_server/size.feature +69 -0
  66. data/features/ftp_server/status.feature +18 -0
  67. data/features/ftp_server/step_definitions/logging.rb +8 -0
  68. data/features/ftp_server/step_definitions/test_server.rb +65 -0
  69. data/features/ftp_server/structure_mount.feature +13 -0
  70. data/features/ftp_server/syntax_errors.feature +18 -0
  71. data/features/ftp_server/syst.feature +18 -0
  72. data/features/ftp_server/timeout.feature +26 -0
  73. data/features/ftp_server/type.feature +59 -0
  74. data/features/step_definitions/append.rb +15 -0
  75. data/features/step_definitions/client.rb +24 -0
  76. data/features/step_definitions/client_and_server_files.rb +24 -0
  77. data/features/step_definitions/client_files.rb +14 -0
  78. data/features/step_definitions/command.rb +5 -0
  79. data/features/step_definitions/connect.rb +37 -0
  80. data/features/step_definitions/delete.rb +15 -0
  81. data/features/step_definitions/directory_navigation.rb +26 -0
  82. data/features/step_definitions/error_replies.rb +115 -0
  83. data/features/step_definitions/features.rb +21 -0
  84. data/features/step_definitions/file_structure.rb +16 -0
  85. data/features/step_definitions/generic_send.rb +9 -0
  86. data/features/step_definitions/get.rb +16 -0
  87. data/features/step_definitions/help.rb +18 -0
  88. data/features/step_definitions/invalid_commands.rb +11 -0
  89. data/features/step_definitions/line_endings.rb +7 -0
  90. data/features/step_definitions/list.rb +73 -0
  91. data/features/step_definitions/login.rb +82 -0
  92. data/features/step_definitions/mkdir.rb +9 -0
  93. data/features/step_definitions/mode.rb +15 -0
  94. data/features/step_definitions/mtime.rb +23 -0
  95. data/features/step_definitions/noop.rb +15 -0
  96. data/features/step_definitions/options.rb +9 -0
  97. data/features/step_definitions/passive.rb +3 -0
  98. data/features/step_definitions/pending.rb +3 -0
  99. data/features/step_definitions/port.rb +5 -0
  100. data/features/step_definitions/put.rb +29 -0
  101. data/features/step_definitions/quit.rb +15 -0
  102. data/features/step_definitions/rename.rb +11 -0
  103. data/features/step_definitions/rmdir.rb +9 -0
  104. data/features/step_definitions/server_files.rb +61 -0
  105. data/features/step_definitions/server_title.rb +12 -0
  106. data/features/step_definitions/size.rb +20 -0
  107. data/features/step_definitions/status.rb +9 -0
  108. data/features/step_definitions/success_replies.rb +7 -0
  109. data/features/step_definitions/system.rb +7 -0
  110. data/features/step_definitions/timing.rb +19 -0
  111. data/features/step_definitions/type.rb +15 -0
  112. data/features/support/env.rb +4 -0
  113. data/features/support/example_server.rb +67 -0
  114. data/features/support/file_templates/ascii_unix +4 -0
  115. data/features/support/file_templates/ascii_windows +4 -0
  116. data/features/support/file_templates/binary +0 -0
  117. data/features/support/test_client.rb +250 -0
  118. data/features/support/test_file_templates.rb +33 -0
  119. data/features/support/test_server.rb +293 -0
  120. data/features/support/test_server_files.rb +57 -0
  121. data/ftpd.gemspec +283 -0
  122. data/insecure-test-cert.pem +29 -0
  123. data/investtools-ftpd.gemspec +284 -0
  124. data/lib/ftpd.rb +86 -0
  125. data/lib/ftpd/auth_levels.rb +9 -0
  126. data/lib/ftpd/cmd_abor.rb +13 -0
  127. data/lib/ftpd/cmd_allo.rb +20 -0
  128. data/lib/ftpd/cmd_appe.rb +24 -0
  129. data/lib/ftpd/cmd_auth.rb +21 -0
  130. data/lib/ftpd/cmd_cdup.rb +16 -0
  131. data/lib/ftpd/cmd_cwd.rb +20 -0
  132. data/lib/ftpd/cmd_dele.rb +21 -0
  133. data/lib/ftpd/cmd_eprt.rb +23 -0
  134. data/lib/ftpd/cmd_epsv.rb +30 -0
  135. data/lib/ftpd/cmd_feat.rb +44 -0
  136. data/lib/ftpd/cmd_help.rb +29 -0
  137. data/lib/ftpd/cmd_list.rb +33 -0
  138. data/lib/ftpd/cmd_login.rb +60 -0
  139. data/lib/ftpd/cmd_mdtm.rb +27 -0
  140. data/lib/ftpd/cmd_mkd.rb +23 -0
  141. data/lib/ftpd/cmd_mode.rb +27 -0
  142. data/lib/ftpd/cmd_nlst.rb +27 -0
  143. data/lib/ftpd/cmd_noop.rb +14 -0
  144. data/lib/ftpd/cmd_opts.rb +14 -0
  145. data/lib/ftpd/cmd_pasv.rb +28 -0
  146. data/lib/ftpd/cmd_pbsz.rb +23 -0
  147. data/lib/ftpd/cmd_port.rb +28 -0
  148. data/lib/ftpd/cmd_prot.rb +34 -0
  149. data/lib/ftpd/cmd_pwd.rb +15 -0
  150. data/lib/ftpd/cmd_quit.rb +18 -0
  151. data/lib/ftpd/cmd_rein.rb +13 -0
  152. data/lib/ftpd/cmd_rename.rb +32 -0
  153. data/lib/ftpd/cmd_rest.rb +13 -0
  154. data/lib/ftpd/cmd_retr.rb +24 -0
  155. data/lib/ftpd/cmd_rmd.rb +22 -0
  156. data/lib/ftpd/cmd_site.rb +13 -0
  157. data/lib/ftpd/cmd_size.rb +29 -0
  158. data/lib/ftpd/cmd_smnt.rb +13 -0
  159. data/lib/ftpd/cmd_stat.rb +15 -0
  160. data/lib/ftpd/cmd_stor.rb +25 -0
  161. data/lib/ftpd/cmd_stou.rb +25 -0
  162. data/lib/ftpd/cmd_stru.rb +27 -0
  163. data/lib/ftpd/cmd_syst.rb +16 -0
  164. data/lib/ftpd/cmd_type.rb +28 -0
  165. data/lib/ftpd/command_handler.rb +90 -0
  166. data/lib/ftpd/command_handler_factory.rb +51 -0
  167. data/lib/ftpd/command_handlers.rb +60 -0
  168. data/lib/ftpd/command_loop.rb +80 -0
  169. data/lib/ftpd/command_sequence_checker.rb +58 -0
  170. data/lib/ftpd/config.rb +13 -0
  171. data/lib/ftpd/connection_throttle.rb +56 -0
  172. data/lib/ftpd/connection_tracker.rb +82 -0
  173. data/lib/ftpd/data_connection_helper.rb +123 -0
  174. data/lib/ftpd/disk_file_system.rb +434 -0
  175. data/lib/ftpd/error.rb +21 -0
  176. data/lib/ftpd/exception_translator.rb +32 -0
  177. data/lib/ftpd/exceptions.rb +62 -0
  178. data/lib/ftpd/file_info.rb +115 -0
  179. data/lib/ftpd/file_system_helper.rb +67 -0
  180. data/lib/ftpd/ftp_server.rb +214 -0
  181. data/lib/ftpd/gets_peer_address.rb +41 -0
  182. data/lib/ftpd/insecure_certificate.rb +16 -0
  183. data/lib/ftpd/list_format/eplf.rb +74 -0
  184. data/lib/ftpd/list_format/ls.rb +154 -0
  185. data/lib/ftpd/list_path.rb +28 -0
  186. data/lib/ftpd/null_logger.rb +22 -0
  187. data/lib/ftpd/protocols.rb +60 -0
  188. data/lib/ftpd/read_only_disk_file_system.rb +22 -0
  189. data/lib/ftpd/server.rb +139 -0
  190. data/lib/ftpd/session.rb +220 -0
  191. data/lib/ftpd/session_config.rb +111 -0
  192. data/lib/ftpd/stream.rb +80 -0
  193. data/lib/ftpd/telnet.rb +114 -0
  194. data/lib/ftpd/temp_dir.rb +22 -0
  195. data/lib/ftpd/tls_server.rb +111 -0
  196. data/lib/ftpd/translate_exceptions.rb +68 -0
  197. data/rake_tasks/cucumber.rake +9 -0
  198. data/rake_tasks/default.rake +1 -0
  199. data/rake_tasks/jeweler.rake +52 -0
  200. data/rake_tasks/spec.rake +3 -0
  201. data/rake_tasks/test.rake +2 -0
  202. data/rake_tasks/yard.rake +3 -0
  203. data/spec/command_sequence_checker_spec.rb +83 -0
  204. data/spec/connection_throttle_spec.rb +99 -0
  205. data/spec/connection_tracker_spec.rb +97 -0
  206. data/spec/disk_file_system_spec.rb +320 -0
  207. data/spec/exception_translator_spec.rb +36 -0
  208. data/spec/file_info_spec.rb +59 -0
  209. data/spec/ftp_server_error_spec.rb +13 -0
  210. data/spec/list_format/eplf_spec.rb +61 -0
  211. data/spec/list_format/ls_spec.rb +270 -0
  212. data/spec/list_path_spec.rb +21 -0
  213. data/spec/null_logger_spec.rb +24 -0
  214. data/spec/protocols_spec.rb +139 -0
  215. data/spec/server_spec.rb +81 -0
  216. data/spec/spec_helper.rb +15 -0
  217. data/spec/telnet_spec.rb +75 -0
  218. data/spec/translate_exceptions_spec.rb +40 -0
  219. metadata +404 -0
@@ -0,0 +1,7 @@
1
+ When /^the client successfully queries system ID$/ do
2
+ @reply = client.system
3
+ end
4
+
5
+ Then /^the server returns a system ID reply$/ do
6
+ step 'the server returns a "UNIX Type: L8" reply'
7
+ end
@@ -0,0 +1,19 @@
1
+ Before do
2
+ @start_time = Time.now
3
+ end
4
+
5
+ When /^the client is idle for (\S+) seconds$/ do |seconds|
6
+ sleep seconds.to_f
7
+ end
8
+
9
+ Then /^it should take at least (\S+) seconds$/ do |s|
10
+ min_elapsed_time = s.to_f
11
+ elapsed_time = Time.now - @start_time
12
+ expect(elapsed_time).to be >= min_elapsed_time
13
+ end
14
+
15
+ Then /^it should take less than (\S+) seconds$/ do |s|
16
+ max_elapsed_time = s.to_f
17
+ elapsed_time = Time.now - @start_time
18
+ expect(elapsed_time).to be < max_elapsed_time
19
+ end
@@ -0,0 +1,15 @@
1
+ When /^the client successfully sets type "(.*?)"$/ do |type|
2
+ client.raw 'TYPE', type
3
+ end
4
+
5
+ When /^the client sets type "(.*?)"$/ do |type|
6
+ capture_error do
7
+ step %Q'the client successfully sets type "#{type}"'
8
+ end
9
+ end
10
+
11
+ When /^the client sets type with no parameter$/ do
12
+ capture_error do
13
+ client.raw 'TYPE'
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', File.dirname(__FILE__))
2
+
3
+ require 'ftpd'
4
+ require 'stringio'
@@ -0,0 +1,67 @@
1
+ require 'fileutils'
2
+ require 'forwardable'
3
+ require File.expand_path('test_server_files',
4
+ File.dirname(__FILE__))
5
+
6
+ class ExampleServer
7
+
8
+ extend Forwardable
9
+ include FileUtils
10
+ include TestServerFiles
11
+
12
+ def initialize(args = nil)
13
+ command = [
14
+ File.expand_path('../../examples/example.rb',
15
+ File.dirname(__FILE__)),
16
+ args,
17
+ ].join(' ')
18
+ @io = IO.popen(command, 'r+')
19
+ @output = read_output
20
+ end
21
+
22
+ def stop
23
+ @io.close
24
+ end
25
+
26
+ def host
27
+ @output[/Host: (.*)$/, 1]
28
+ end
29
+
30
+ def port
31
+ @output[/Port: (.*)$/, 1].to_i
32
+ end
33
+
34
+ def user
35
+ @output[/User: "(.*)"$/, 1]
36
+ end
37
+
38
+ def password
39
+ @output[/Pass: "(.*)"$/, 1]
40
+ end
41
+
42
+ def account
43
+ @output[/Account: "(.*)"$/, 1]
44
+ end
45
+
46
+ def auth_level
47
+ Ftpd::AUTH_PASSWORD
48
+ end
49
+
50
+ private
51
+
52
+ def read_output
53
+ output = ''
54
+ loop do
55
+ line = @io.gets
56
+ break if line.nil?
57
+ output << line
58
+ break if line =~ /FTP server started/
59
+ end
60
+ output
61
+ end
62
+
63
+ def temp_dir
64
+ @output[/Directory: (.*)$/, 1]
65
+ end
66
+
67
+ end
@@ -0,0 +1,4 @@
1
+ Billy was a chemist
2
+ but Billy is no more
3
+ For what he thought was H2O
4
+ Was H2SO4
@@ -0,0 +1,4 @@
1
+ Billy was a chemist
2
+ but Billy is no more
3
+ For what he thought was H2O
4
+ Was H2SO4
@@ -0,0 +1,250 @@
1
+ require 'double_bag_ftps'
2
+ require 'net/ftp'
3
+
4
+ class TestClient
5
+
6
+ extend Forwardable
7
+ include FileUtils
8
+
9
+ attr_accessor :tls_mode
10
+
11
+ def initialize
12
+ @tls_mode = :off
13
+ @temp_dir = Ftpd::TempDir.make
14
+ @templates = TestFileTemplates.new
15
+ end
16
+
17
+ def start
18
+ @ftp = make_ftp
19
+ end
20
+
21
+ def close
22
+ return unless @ftp
23
+ @ftp.close
24
+ end
25
+
26
+ def_delegators :@ftp,
27
+ :chdir,
28
+ :connect,
29
+ :delete,
30
+ :getbinaryfile,
31
+ :gettextfile,
32
+ :help,
33
+ :login,
34
+ :ls,
35
+ :mkdir,
36
+ :nlst,
37
+ :noop,
38
+ :passive=,
39
+ :pwd,
40
+ :quit,
41
+ :rename,
42
+ :rmdir,
43
+ :status,
44
+ :system
45
+
46
+ # Make a connection from a specific IP. Net::FTP doesn't have a way
47
+ # to force the local IP, so fake it here.
48
+
49
+ def connect_from(source_ip, host, port)
50
+ in_addr = Socket.pack_sockaddr_in(0, source_ip)
51
+ out_addr = Socket.pack_sockaddr_in(port, host)
52
+ socket = Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0)
53
+ socket.bind(in_addr)
54
+ socket.connect(out_addr)
55
+ decorate_socket socket
56
+ @ftp = make_ftp
57
+ @ftp.set_socket(socket)
58
+ end
59
+
60
+ def raw(*command)
61
+ @ftp.sendcmd command.compact.join(' ')
62
+ end
63
+
64
+ def get(mode, remote_path)
65
+ method = "get#{mode}file"
66
+ @ftp.send method, remote_path, local_path(remote_path)
67
+ end
68
+
69
+ def put(mode, remote_path)
70
+ method = "put#{mode}file"
71
+ @ftp.send method, local_path(remote_path), remote_path
72
+ end
73
+
74
+ def get_size(mode, remote_path)
75
+ raise unless ['binary', 'text'].include?(mode)
76
+ @ftp.binary = mode == 'binary'
77
+ override_with_binary do
78
+ @ftp.size(remote_path)
79
+ end
80
+ end
81
+
82
+ def get_mtime(remote_path)
83
+ @ftp.mtime(remote_path)
84
+ end
85
+
86
+ def add_file(path)
87
+ full_path = temp_path(path)
88
+ mkdir_p File.dirname(full_path)
89
+ File.open(full_path, 'wb') do |file|
90
+ file.write @templates[File.basename(full_path)]
91
+ end
92
+ end
93
+
94
+ def template(path)
95
+ @templates[File.basename(path)]
96
+ end
97
+
98
+ def file_contents(path)
99
+ File.open(temp_path(path), 'rb', &:read)
100
+ end
101
+
102
+ def xpwd
103
+ response = @ftp.sendcmd('XPWD')
104
+ response[/"(.+)"/, 1]
105
+ end
106
+
107
+ def store_unique(local_path, remote_path)
108
+ command = ['STOU', remote_path].compact.join(' ')
109
+ File.open(temp_path(local_path), 'rb') do |file|
110
+ @ftp.storbinary command, file, Net::FTP::DEFAULT_BLOCKSIZE
111
+ end
112
+ end
113
+
114
+ def append_binary(local_path, remote_path)
115
+ command = ['APPE', remote_path].compact.join(' ')
116
+ File.open(temp_path(local_path), 'rb') do |file|
117
+ @ftp.storbinary command, file, Net::FTP::DEFAULT_BLOCKSIZE
118
+ end
119
+ end
120
+
121
+ def append_text(local_path, remote_path)
122
+ command = ['APPE', remote_path].compact.join(' ')
123
+ File.open(temp_path(local_path), 'rb') do |file|
124
+ @ftp.storlines command, file
125
+ end
126
+ end
127
+
128
+ def connected?
129
+ begin
130
+ @ftp.noop
131
+ true
132
+ rescue Net::FTPTempError => e
133
+ !!e.to_s =~ /^421/
134
+ rescue EOFError
135
+ false
136
+ end
137
+ end
138
+
139
+ def set_option(option)
140
+ @ftp.sendcmd "OPTS #{option}"
141
+ end
142
+
143
+ private
144
+
145
+ RAW_METHOD_REGEX = /^send_(.*)$/
146
+
147
+ def local_path(remote_path)
148
+ temp_path(File.basename(remote_path))
149
+ end
150
+
151
+ def temp_path(path)
152
+ File.expand_path(path, @temp_dir)
153
+ end
154
+
155
+ def make_ftp
156
+ ftp =
157
+ case @tls_mode
158
+ when :off
159
+ make_non_tls_ftp
160
+ when :implicit
161
+ make_tls_ftp(:implicit)
162
+ when :explicit
163
+ make_tls_ftp(:explicit)
164
+ else
165
+ raise "Unknown TLS mode: #{@tls_mode}"
166
+ end
167
+ allow_size_in_ascii_mode ftp
168
+ ftp
169
+ end
170
+
171
+ def make_tls_ftp(ftps_mode)
172
+ ftp = DoubleBagFTPS.new
173
+ context_opts = {
174
+ :verify_mode => OpenSSL::SSL::VERIFY_NONE
175
+ }
176
+ ftp.ssl_context = DoubleBagFTPS.create_ssl_context(context_opts)
177
+ ftp.ftps_mode = ftps_mode
178
+ ftp
179
+ end
180
+
181
+ def make_non_tls_ftp
182
+ Net::FTP.new
183
+ end
184
+
185
+ # Ruby FTP client forces binary mode when doing a SIZE command. Our
186
+ # tests want to check that the server's SIZE command works correctly
187
+ # in ASCII mode as well, so we'll monkey-patch the FTP client.
188
+
189
+ def allow_size_in_ascii_mode(ftp)
190
+
191
+ class << ftp
192
+
193
+ attr_accessor :override_with_binary
194
+
195
+ alias :orig_with_binary :with_binary
196
+
197
+ def with_binary(*args, &block)
198
+ if @override_with_binary
199
+ block.call
200
+ else
201
+ return orig_with_binary(*args, &block)
202
+ end
203
+ end
204
+
205
+ end
206
+
207
+ end
208
+
209
+ def override_with_binary
210
+ orig = @ftp.override_with_binary
211
+ begin
212
+ @ftp.override_with_binary = true
213
+ yield
214
+ ensure
215
+ @ftp.override_with_binary = orig
216
+ end
217
+ end
218
+
219
+ # Ruby 2.0's Ftp class is expecting a TCPSocket, not a Socket. The
220
+ # trouble comes with Ftp#close, which closes sockets by first doing
221
+ # a shutdown, setting the read timeout, and doing a read. Plain
222
+ # Socket doesn't have those methods, so fake it.
223
+ #
224
+ # Plain socket _does_ have #close, but we short-circuit it, too,
225
+ # because it takes a few seconds. We're in a hurry when running
226
+ # tests, and can afford to be a little sloppy when cleaning up.
227
+
228
+ def decorate_socket(sock)
229
+
230
+ def sock.shutdown(how)
231
+ @shutdown = true
232
+ end
233
+
234
+ def sock.read_timeout=(seconds)
235
+ end
236
+
237
+ # Skip read after shutdown. Prevents 2.0 from hanging in
238
+ # Ftp#close
239
+
240
+ def sock.read(*args)
241
+ return if @shutdown
242
+ super(*args)
243
+ end
244
+
245
+ def close
246
+ end
247
+
248
+ end
249
+
250
+ end
@@ -0,0 +1,33 @@
1
+ class TestFileTemplates
2
+
3
+ def [](filename)
4
+ if have_template?(filename)
5
+ read_template filename
6
+ else
7
+ default_template filename
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def have_template?(filename)
14
+ File.exists?(template_path(filename))
15
+ end
16
+
17
+ def read_template(filename)
18
+ File.open(template_path(filename), 'rb', &:read)
19
+ end
20
+
21
+ def template_path(filename)
22
+ File.expand_path(filename, templates_path)
23
+ end
24
+
25
+ def templates_path
26
+ File.expand_path('file_templates', File.dirname(__FILE__))
27
+ end
28
+
29
+ def default_template(filename)
30
+ "Contents of #{filename}"
31
+ end
32
+
33
+ end
@@ -0,0 +1,293 @@
1
+ require 'fileutils'
2
+ require 'forwardable'
3
+ require 'stringio'
4
+ require 'tempfile'
5
+
6
+ require File.expand_path('test_server_files',
7
+ File.dirname(__FILE__))
8
+
9
+ class TestServer
10
+ class TestServerDriver
11
+
12
+ extend Forwardable
13
+
14
+ USER = 'user'
15
+ PASSWORD = 'password'
16
+ ACCOUNT = 'account'
17
+
18
+ attr_accessor :append
19
+ attr_accessor :delete
20
+ attr_accessor :list
21
+ attr_accessor :mkdir
22
+ attr_accessor :read
23
+ attr_accessor :rename
24
+ attr_accessor :rmdir
25
+ attr_accessor :write
26
+
27
+ def initialize(temp_dir)
28
+ @temp_dir = temp_dir
29
+ @append = true
30
+ @delete = true
31
+ @list = true
32
+ @mkdir = true
33
+ @read = true
34
+ @rename = true
35
+ @rmdir = true
36
+ @write = true
37
+ end
38
+
39
+ def authenticate(user, password, account)
40
+ user == USER &&
41
+ (password.nil? || password == PASSWORD) &&
42
+ (account.nil? || account == ACCOUNT)
43
+ end
44
+
45
+ def file_system(user)
46
+ TestServerFileSystem.new(@temp_dir,
47
+ :append => @append,
48
+ :delete => @delete,
49
+ :list => @list,
50
+ :mkdir => @mkdir,
51
+ :read => @read,
52
+ :rename => @rename,
53
+ :rmdir => @rmdir,
54
+ :write => @write)
55
+ end
56
+
57
+ end
58
+ end
59
+
60
+ class TestServer
61
+
62
+ module ForcesAccessDenied
63
+
64
+ def self.included(includer)
65
+ includer.extend ClassMethods
66
+ end
67
+
68
+ module ClassMethods
69
+
70
+ def return_false_on_force_access_denied(method_name)
71
+ original_method = instance_method(method_name)
72
+ define_method method_name do |*args|
73
+ ftp_path = args.first
74
+ return false if force_access_denied?(ftp_path)
75
+ original_method.bind(self).call *args
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ def force_access_denied?(ftp_path)
82
+ ftp_path =~ /forbidden/
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ class TestServer
90
+
91
+ module ForcesFileSystemError
92
+
93
+ def self.included(includer)
94
+ includer.extend ClassMethods
95
+ end
96
+
97
+ module ClassMethods
98
+
99
+ def raise_on_file_system_error(method_name)
100
+ original_method = instance_method(method_name)
101
+ define_method method_name do |*args, &block|
102
+ ftp_path = args.first
103
+ if force_file_system_error?(ftp_path)
104
+ raise Ftpd::PermanentFileSystemError, 'Unable to do it'
105
+ end
106
+ original_method.bind(self).call *args, &block
107
+ end
108
+ end
109
+
110
+ def return_true_on_file_system_error(method_name)
111
+ original_method = instance_method(method_name)
112
+ define_method method_name do |*args, &block|
113
+ ftp_path = args.first
114
+ return true if force_file_system_error?(ftp_path)
115
+ original_method.bind(self).call *args, &block
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ def force_file_system_error?(ftp_path)
122
+ ftp_path =~ /unable/
123
+ end
124
+
125
+ end
126
+ end
127
+
128
+ class TestServer
129
+ class TestServerFileSystem
130
+
131
+ # In order to test ftpd's ability to adapt itself to the driver's
132
+ # signature, we create a new, anonymous instance of the file
133
+ # system class for each test. The option flags determine whether
134
+ # or not to mix in certain behavior such as writing files, reading
135
+ # files, etc.
136
+
137
+ def self.new(data_dir, opts)
138
+ Class.new do
139
+
140
+ include ForcesAccessDenied
141
+ include ForcesFileSystemError
142
+
143
+ include Ftpd::DiskFileSystem::Base
144
+
145
+ if opts[:append]
146
+ include Ftpd::DiskFileSystem::Append
147
+ raise_on_file_system_error :append
148
+ end
149
+
150
+ if opts[:delete]
151
+ include Ftpd::DiskFileSystem::Delete
152
+ raise_on_file_system_error :delete
153
+ end
154
+
155
+ if opts[:list]
156
+ include Ftpd::DiskFileSystem::List
157
+ end
158
+
159
+ if opts[:mkdir]
160
+ include Ftpd::DiskFileSystem::Mkdir
161
+ end
162
+
163
+ if opts[:read]
164
+ include Ftpd::DiskFileSystem::Read
165
+ raise_on_file_system_error :read
166
+ end
167
+
168
+ if opts[:rename]
169
+ include Ftpd::DiskFileSystem::Rename
170
+ raise_on_file_system_error :rename
171
+ end
172
+
173
+ if opts[:rmdir]
174
+ include Ftpd::DiskFileSystem::Rmdir
175
+ end
176
+
177
+ if opts[:write]
178
+ include Ftpd::DiskFileSystem::Write
179
+ raise_on_file_system_error :write
180
+ end
181
+
182
+ def initialize(data_dir)
183
+ set_data_dir data_dir
184
+ translate_exception SystemCallError
185
+ end
186
+
187
+ return_false_on_force_access_denied :accessible?
188
+
189
+ return_true_on_file_system_error :accessible?
190
+ return_true_on_file_system_error :exists?
191
+ return_true_on_file_system_error :directory?
192
+
193
+ end.new(data_dir)
194
+ end
195
+
196
+ end
197
+ end
198
+
199
+ class TestServer
200
+
201
+ extend Forwardable
202
+ include FileUtils
203
+ include Ftpd::InsecureCertificate
204
+ include TestServerFiles
205
+
206
+ attr_writer :logging
207
+
208
+ def initialize
209
+ @temp_dir = Ftpd::TempDir.make
210
+ @log_device = StringIO.new
211
+ @driver = TestServerDriver.new(@temp_dir)
212
+ @server = Ftpd::FtpServer.new(@driver)
213
+ @server.certfile_path = insecure_certfile_path
214
+ @templates = TestFileTemplates.new
215
+ self.tls = :off
216
+ end
217
+
218
+ def_delegator :@server, :'allow_low_data_ports='
219
+ def_delegator :@server, :'auth_level'
220
+ def_delegator :@server, :'auth_level='
221
+ def_delegator :@server, :'failed_login_delay='
222
+ def_delegator :@server, :'interface='
223
+ def_delegator :@server, :'max_connections='
224
+ def_delegator :@server, :'max_connections_per_ip='
225
+ def_delegator :@server, :'max_failed_logins='
226
+ def_delegator :@server, :'server_name'
227
+ def_delegator :@server, :'server_name='
228
+ def_delegator :@server, :'session_timeout='
229
+ def_delegator :@server, :'tls='
230
+
231
+ def_delegator :@driver, :'append='
232
+ def_delegator :@driver, :'delete='
233
+ def_delegator :@driver, :'list='
234
+ def_delegator :@driver, :'mkdir='
235
+ def_delegator :@driver, :'rmdir='
236
+ def_delegator :@driver, :'read='
237
+ def_delegator :@driver, :'rename='
238
+ def_delegator :@driver, :'write='
239
+
240
+ def log_output
241
+ @log_device.string
242
+ end
243
+
244
+ def start
245
+ @server.log = make_log
246
+ @server.start
247
+ end
248
+
249
+ def stop
250
+ @server.stop
251
+ end
252
+
253
+ def host
254
+ @server.interface
255
+ end
256
+
257
+ def user
258
+ TestServerDriver::USER
259
+ end
260
+
261
+ def password
262
+ TestServerDriver::PASSWORD
263
+ end
264
+
265
+ def account
266
+ TestServerDriver::ACCOUNT
267
+ end
268
+
269
+ def port
270
+ @server.bound_port
271
+ end
272
+
273
+ def template(path)
274
+ @templates[File.basename(path)]
275
+ end
276
+
277
+ private
278
+
279
+ def temp_dir
280
+ @temp_dir
281
+ end
282
+
283
+ def make_log
284
+ if @logging
285
+ Logger.new(@log_device)
286
+ elsif ENV['FTPD_DEBUG'].to_i != 0
287
+ Logger.new($stdout)
288
+ else
289
+ nil
290
+ end
291
+ end
292
+
293
+ end