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,22 @@
1
+ module Ftpd
2
+
3
+ # Create temporary directories that will be removed when the program
4
+ # exits.
5
+
6
+ module TempDir
7
+
8
+ # Create a temporary directory, returning its path. When the
9
+ # program exists, the directory (and its contents) are removed.
10
+
11
+ def make
12
+ Dir.mktmpdir.tap do |path|
13
+ at_exit do
14
+ FileUtils.rm_rf path
15
+ Dir.rmdir path if File.exists?(path)
16
+ end
17
+ end
18
+ end
19
+ module_function :make
20
+
21
+ end
22
+ end
@@ -0,0 +1,111 @@
1
+ require_relative 'server'
2
+
3
+ module Ftpd
4
+ class TlsServer < Server
5
+
6
+ # Whether or not to do TLS, and which flavor.
7
+ #
8
+ # One of:
9
+ # * :off
10
+ # * :explicit
11
+ # * :implicit
12
+ #
13
+ # Notes:
14
+ # * Defaults to :off
15
+ # * Set this before calling #start.
16
+ # * If other than :off, then #certfile_path must be set.
17
+ #
18
+ # @return [Symbol]
19
+
20
+ attr_accessor :tls
21
+
22
+ # The path of the SSL certificate to use for TLS. Defaults to nil
23
+ # (no SSL certificate).
24
+ #
25
+ # Set this before calling #start.
26
+ #
27
+ # @return [String]
28
+
29
+ attr_accessor :certfile_path
30
+
31
+ # Create a new TLS server.
32
+
33
+ def initialize
34
+ super
35
+ @tls = :off
36
+ end
37
+
38
+ private
39
+
40
+ def make_server_socket
41
+ socket = super
42
+ if tls_enabled?
43
+ socket = OpenSSL::SSL::SSLServer.new(socket, ssl_context);
44
+ socket.start_immediately = false
45
+ end
46
+ socket
47
+ end
48
+
49
+ def accept
50
+ socket = @server_socket.accept
51
+ if tls_enabled?
52
+ add_missing_methods_to_socket socket
53
+ add_tls_methods_to_socket socket
54
+ end
55
+ socket
56
+ end
57
+
58
+ def ssl_context
59
+ unless @certfile_path
60
+ raise ArgumentError, ":certfile required if tls enabled"
61
+ end
62
+ context = OpenSSL::SSL::SSLContext.new
63
+ File.open(@certfile_path) do |certfile|
64
+ context.cert = OpenSSL::X509::Certificate.new(certfile)
65
+ certfile.rewind
66
+ context.key = OpenSSL::PKey::RSA.new(certfile)
67
+ end
68
+ context
69
+ end
70
+ memoize :ssl_context
71
+
72
+ # Add to the TLS socket some methods that regular socket has, but TLS socket is missing
73
+
74
+ def add_missing_methods_to_socket(socket)
75
+
76
+ def socket.local_address
77
+ @io.local_address
78
+ end
79
+
80
+ end
81
+
82
+ # Add to the TLS sockets some methods of our own invention that
83
+ # make life a little easier.
84
+
85
+ def add_tls_methods_to_socket(socket)
86
+ context = @ssl_context
87
+ class << socket
88
+
89
+ def ssl_context
90
+ context
91
+ end
92
+
93
+ def encrypted?
94
+ !!cipher
95
+ end
96
+
97
+ def encrypt
98
+ accept
99
+ end
100
+
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def tls_enabled?
107
+ @tls != :off
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,68 @@
1
+ module Ftpd
2
+
3
+ # This module translates exceptions to FileSystemError exceptions.
4
+ #
5
+ # A disk file system (such as Ftpd::DiskFileSystem) is expected to
6
+ # raise only FileSystemError exceptions, but many common operations
7
+ # result in other exceptions such as SystemCallError. This module
8
+ # aids a disk driver in translating exceptions to FileSystemError
9
+ # exceptions.
10
+ #
11
+ # In your file system, driver, include this module:
12
+ #
13
+ # module MyDiskDriver
14
+ # include Ftpd::TranslateExceptions
15
+ #
16
+ # in your constructor, register the exceptions that should be translated:
17
+ #
18
+ # def initialize
19
+ # translate_exception SystemCallError
20
+ # end
21
+ #
22
+ # And register methods for translation:
23
+ #
24
+ # def read(ftp_path)
25
+ # ...
26
+ # end
27
+ # translate_exceptions :read
28
+
29
+ module TranslateExceptions
30
+
31
+ include Memoizer
32
+
33
+ def self.included(includer)
34
+ includer.extend ClassMethods
35
+ end
36
+
37
+ module ClassMethods
38
+
39
+ # Cause the named method to translate exceptions.
40
+
41
+ def translate_exceptions(method_name)
42
+ original_method = instance_method(method_name)
43
+ define_method method_name do |*args, &block|
44
+ exception_translator.translate_exceptions do
45
+ original_method.bind(self).call *args, &block
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ # Add exception class e to the list of exceptions to be
53
+ # translated.
54
+
55
+ def translate_exception(e)
56
+ exception_translator.register_exception e
57
+ end
58
+
59
+ private
60
+
61
+ def exception_translator
62
+ ExceptionTranslator.new
63
+ end
64
+ memoize :exception_translator
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,9 @@
1
+ require 'cucumber/rake/task'
2
+
3
+ Cucumber::Rake::Task.new 'test:features' do |t|
4
+ t.fork = true
5
+ t.cucumber_opts = '--format progress'
6
+ end
7
+
8
+ task 'test:cucumber' => ['test:features']
9
+ task 'cucumber' => ['test:features']
@@ -0,0 +1 @@
1
+ task :default => [:test]
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require 'jeweler'
4
+
5
+ README_PATH = File.expand_path('../README.md', File.dirname(__FILE__))
6
+
7
+ def remove_markdown_link(description)
8
+ regex = %r{
9
+ \[
10
+ ([^\]]+)
11
+ \]
12
+ (
13
+ \[\d+\] |
14
+ \([^)]+\)
15
+ )
16
+ }x
17
+ description = description.gsub(regex, '\1')
18
+ end
19
+
20
+ def remove_badges(description)
21
+ description.gsub(/^\[!.*\n/, '')
22
+ end
23
+
24
+ def join_lines(description)
25
+ description.gsub(/\n/, ' ').strip
26
+ end
27
+
28
+ def extract_description_from_readme
29
+ readme = File.open(README_PATH, 'r', &:read)
30
+ description = readme[/^# FTPD.*\n+((?:.*\n)+?)\n*##/i, 1]
31
+ unless description
32
+ raise 'Unable to extract description from readme'
33
+ end
34
+ description = remove_badges(description)
35
+ description = remove_markdown_link(description)
36
+ description = join_lines(description)
37
+ description
38
+ end
39
+
40
+ Jeweler::Tasks.new do |gem|
41
+ # gem is a Gem::Specification... see
42
+ # http://docs.rubygems.org/read/chapter/20 for more options
43
+ gem.name = 'investtools-ftpd'
44
+ gem.homepage = 'http://github.com/wconrad/ftpd'
45
+ gem.license = 'MIT'
46
+ gem.summary = %Q{Pure Ruby FTP server library}
47
+ gem.description = extract_description_from_readme
48
+ gem.email = 'wconrad@yagni.com'
49
+ gem.authors = ['Wayne Conrad']
50
+ # dependencies defined in Gemfile
51
+ end
52
+ Jeweler::RubygemsDotOrgTasks.new
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new 'test:spec'
3
+ task :spec => ['test:spec']
@@ -0,0 +1,2 @@
1
+ desc 'Run all tests'
2
+ task :test => ['test:spec', 'test:cucumber']
@@ -0,0 +1,3 @@
1
+ require 'yard'
2
+ YARD::Rake::YardocTask.new do |t|
3
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Ftpd
4
+ describe CommandSequenceChecker do
5
+
6
+ let(:sequence_error_verification) do
7
+ lambda {|e| e.code == 503 && e.message == "Bad sequence of commands"}
8
+ end
9
+ subject(:checker) {CommandSequenceChecker.new}
10
+
11
+ context 'initial' do
12
+
13
+ it 'accepts any command' do
14
+ checker.check 'NOOP'
15
+ end
16
+
17
+ end
18
+
19
+ context 'when a specific command is expected' do
20
+
21
+ before(:each) {checker.expect 'PASS'}
22
+
23
+ it 'accepts that command' do
24
+ checker.check 'PASS'
25
+ end
26
+
27
+ it 'rejects any other command' do
28
+ expect {
29
+ checker.check 'NOOP'
30
+ }.to raise_error(FtpServerError, &sequence_error_verification)
31
+ end
32
+
33
+ end
34
+
35
+ context 'after the expected command has arrived' do
36
+
37
+ before(:each) do
38
+ checker.expect 'PASS'
39
+ checker.check 'PASS'
40
+ end
41
+
42
+ it 'accepts any other command' do
43
+ checker.check 'NOOP'
44
+ end
45
+
46
+ end
47
+
48
+ context 'after a command is rejected' do
49
+
50
+ before(:each) do
51
+ checker.expect 'PASS'
52
+ expect {
53
+ checker.check 'NOOP'
54
+ }.to raise_error(FtpServerError, &sequence_error_verification)
55
+ end
56
+
57
+ it 'accepts any other command' do
58
+ checker.check 'NOOP'
59
+ end
60
+
61
+ end
62
+
63
+ context 'when a command must be expected' do
64
+
65
+ before(:each) do
66
+ checker.must_expect 'PASS'
67
+ end
68
+
69
+ it 'rejects that command if not expected' do
70
+ expect {
71
+ checker.check 'PASS'
72
+ }.to raise_error(FtpServerError, &sequence_error_verification)
73
+ end
74
+
75
+ it 'accepts that command when it is accepted' do
76
+ checker.expect 'PASS'
77
+ checker.check 'PASS'
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,99 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Ftpd
4
+
5
+ describe ConnectionThrottle do
6
+
7
+ let(:socket) {double TCPSocket}
8
+ let(:connections) {0}
9
+ let(:connections_for_socket) {0}
10
+ let(:connection_tracker) do
11
+ double ConnectionTracker, :connections => connections
12
+ end
13
+ subject(:connection_throttle) do
14
+ ConnectionThrottle.new(connection_tracker)
15
+ end
16
+
17
+ before(:each) do
18
+ allow(connection_tracker).to receive(:connections)
19
+ .and_return(connections)
20
+ allow(connection_tracker).to receive(:connections_for)
21
+ .with(socket)
22
+ .and_return(connections_for_socket)
23
+ end
24
+
25
+ it 'should have defaults' do
26
+ expect(connection_throttle.max_connections).to be_nil
27
+ expect(connection_throttle.max_connections_per_ip).to be_nil
28
+ end
29
+
30
+ describe '#allow?' do
31
+
32
+ context '(total connections)' do
33
+
34
+ let(:max_connections) {50}
35
+
36
+ before(:each) do
37
+ connection_throttle.max_connections = max_connections
38
+ connection_throttle.max_connections_per_ip = 2 * max_connections
39
+ end
40
+
41
+ context 'almost at maximum connections' do
42
+ let(:connections) {max_connections - 1}
43
+ specify {expect(connection_throttle.allow?(socket)).to be_truthy}
44
+ end
45
+
46
+ context 'at maximum connections' do
47
+ let(:connections) {max_connections}
48
+ specify {expect(connection_throttle.allow?(socket)).to be_falsey}
49
+ end
50
+
51
+ context 'above maximum connections' do
52
+ let(:connections) {max_connections + 1}
53
+ specify {expect(connection_throttle.allow?(socket)).to be_falsey}
54
+ end
55
+
56
+ end
57
+
58
+ context '(per ip)' do
59
+
60
+ let(:max_connections_per_ip) {5}
61
+
62
+ before(:each) do
63
+ connection_throttle.max_connections = 2 * max_connections_per_ip
64
+ connection_throttle.max_connections_per_ip = max_connections_per_ip
65
+ end
66
+
67
+ context 'almost at maximum connections for ip' do
68
+ let(:connections_for_socket) {max_connections_per_ip - 1}
69
+ specify {expect(connection_throttle.allow?(socket)).to be_truthy}
70
+ end
71
+
72
+ context 'at maximum connections for ip' do
73
+ let(:connections_for_socket) {max_connections_per_ip}
74
+ specify {expect(connection_throttle.allow?(socket)).to be_falsey}
75
+ end
76
+
77
+ context 'above maximum connections for ip' do
78
+ let(:connections_for_socket) {max_connections_per_ip + 1}
79
+ specify {expect(connection_throttle.allow?(socket)).to be_falsey}
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ describe '#deny' do
87
+
88
+ let(:socket) {StringIO.new}
89
+
90
+ it 'should send a "too many connections" message' do
91
+ connection_throttle.deny socket
92
+ expect(socket.string).to eq "421 Too many connections\r\n"
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end