diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/test/net/smtp |
Fresh start
Diffstat (limited to 'jni/ruby/test/net/smtp')
-rw-r--r-- | jni/ruby/test/net/smtp/test_response.rb | 99 | ||||
-rw-r--r-- | jni/ruby/test/net/smtp/test_smtp.rb | 54 | ||||
-rw-r--r-- | jni/ruby/test/net/smtp/test_ssl_socket.rb | 91 |
3 files changed, 244 insertions, 0 deletions
diff --git a/jni/ruby/test/net/smtp/test_response.rb b/jni/ruby/test/net/smtp/test_response.rb new file mode 100644 index 0000000..e7011e0 --- /dev/null +++ b/jni/ruby/test/net/smtp/test_response.rb @@ -0,0 +1,99 @@ +require 'net/smtp' +require 'minitest/autorun' + +module Net + class SMTP + class TestResponse < MiniTest::Unit::TestCase + def test_capabilities + res = Response.parse("250-ubuntu-desktop\n250-PIPELINING\n250-SIZE 10240000\n250-VRFY\n250-ETRN\n250-STARTTLS\n250-ENHANCEDSTATUSCODES\n250 DSN\n") + + capabilities = res.capabilities + %w{ PIPELINING SIZE VRFY STARTTLS ENHANCEDSTATUSCODES DSN}.each do |str| + assert capabilities.key?(str), str + end + end + + def test_capabilities_default + res = Response.parse("250-ubuntu-desktop\n250-PIPELINING\n250 DSN\n") + assert_equal [], res.capabilities['PIPELINING'] + end + + def test_capabilities_value + res = Response.parse("250-ubuntu-desktop\n250-SIZE 1234\n250 DSN\n") + assert_equal ['1234'], res.capabilities['SIZE'] + end + + def test_capabilities_multi + res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") + assert_equal %w{1 2 3}, res.capabilities['SIZE'] + end + + def test_bad_string + res = Response.parse("badstring") + assert_equal({}, res.capabilities) + end + + def test_success? + res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") + assert res.success? + assert !res.continue? + end + + # RFC 2821, Section 4.2.1 + def test_continue? + res = Response.parse("3yz-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") + assert !res.success? + assert res.continue? + end + + def test_status_type_char + res = Response.parse("3yz-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") + assert_equal '3', res.status_type_char + + res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") + assert_equal '2', res.status_type_char + end + + def test_message + res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n") + assert_equal "250-ubuntu-desktop\n", res.message + end + + def test_server_busy_exception + res = Response.parse("400 omg busy") + assert_equal Net::SMTPServerBusy, res.exception_class + res = Response.parse("410 omg busy") + assert_equal Net::SMTPServerBusy, res.exception_class + end + + def test_syntax_error_exception + res = Response.parse("500 omg syntax error") + assert_equal Net::SMTPSyntaxError, res.exception_class + + res = Response.parse("501 omg syntax error") + assert_equal Net::SMTPSyntaxError, res.exception_class + end + + def test_authentication_exception + res = Response.parse("530 omg auth error") + assert_equal Net::SMTPAuthenticationError, res.exception_class + + res = Response.parse("531 omg auth error") + assert_equal Net::SMTPAuthenticationError, res.exception_class + end + + def test_fatal_error + res = Response.parse("510 omg fatal error") + assert_equal Net::SMTPFatalError, res.exception_class + + res = Response.parse("511 omg fatal error") + assert_equal Net::SMTPFatalError, res.exception_class + end + + def test_default_exception + res = Response.parse("250 omg fatal error") + assert_equal Net::SMTPUnknownError, res.exception_class + end + end + end +end diff --git a/jni/ruby/test/net/smtp/test_smtp.rb b/jni/ruby/test/net/smtp/test_smtp.rb new file mode 100644 index 0000000..748b20d --- /dev/null +++ b/jni/ruby/test/net/smtp/test_smtp.rb @@ -0,0 +1,54 @@ +require 'net/smtp' +require 'stringio' +require 'minitest/autorun' + +module Net + class TestSMTP < MiniTest::Unit::TestCase + class FakeSocket + def initialize out = "250 OK\n" + @write_io = StringIO.new + @read_io = StringIO.new out + end + + def writeline line + @write_io.write "#{line}\r\n" + end + + def readline + line = @read_io.gets + raise 'ran out of input' unless line + line.chop + end + end + + def test_critical + smtp = Net::SMTP.new 'localhost', 25 + + assert_raises RuntimeError do + smtp.send :critical do + raise 'fail on purpose' + end + end + + assert_kind_of Net::SMTP::Response, smtp.send(:critical), + '[Bug #9125]' + end + + def test_esmtp + smtp = Net::SMTP.new 'localhost', 25 + assert smtp.esmtp + assert smtp.esmtp? + + smtp.esmtp = 'omg' + assert_equal 'omg', smtp.esmtp + assert_equal 'omg', smtp.esmtp? + end + + def test_rset + smtp = Net::SMTP.new 'localhost', 25 + smtp.instance_variable_set :@socket, FakeSocket.new + + assert smtp.rset + end + end +end diff --git a/jni/ruby/test/net/smtp/test_ssl_socket.rb b/jni/ruby/test/net/smtp/test_ssl_socket.rb new file mode 100644 index 0000000..dc8b03e --- /dev/null +++ b/jni/ruby/test/net/smtp/test_ssl_socket.rb @@ -0,0 +1,91 @@ +require 'net/smtp' +require 'minitest/autorun' + +module Net + class TestSSLSocket < MiniTest::Unit::TestCase + class MySMTP < SMTP + attr_accessor :fake_tcp, :fake_ssl + + def tcp_socket address, port + fake_tcp + end + + def ssl_socket socket, context + fake_ssl + end + end + + require 'stringio' + class SSLSocket < StringIO + attr_accessor :sync_close, :connected, :closed + + def initialize(*args) + @connected = false + @closed = true + super + end + + def connect + self.connected = true + self.closed = false + end + + def close + self.closed = true + end + + def post_connection_check omg + end + end + + def test_ssl_socket_close_on_post_connection_check_fail + tcp_socket = StringIO.new success_response + + ssl_socket = SSLSocket.new.extend Module.new { + def post_connection_check omg + raise OpenSSL::SSL::SSLError, 'hostname was not match with the server certificate' + end + } + + connection = MySMTP.new('localhost', 25) + connection.enable_starttls_auto + connection.fake_tcp = tcp_socket + connection.fake_ssl = ssl_socket + + assert_raises(OpenSSL::SSL::SSLError) do + connection.start + end + assert_equal true, ssl_socket.closed + end + + def test_ssl_socket_open_on_post_connection_check_success + tcp_socket = StringIO.new success_response + + ssl_socket = SSLSocket.new success_response + + connection = MySMTP.new('localhost', 25) + connection.enable_starttls_auto + connection.fake_tcp = tcp_socket + connection.fake_ssl = ssl_socket + + connection.start + assert_equal false, ssl_socket.closed + end + + def success_response + [ + '220 smtp.example.com ESMTP Postfix', + "250-ubuntu-desktop", + "250-PIPELINING", + "250-SIZE 10240000", + "250-VRFY", + "250-ETRN", + "250-STARTTLS", + "250-ENHANCEDSTATUSCODES", + "250-8BITMIME", + "250 DSN", + "220 2.0.0 Ready to start TLS", + ].join("\r\n") + "\r\n" + end + end +end if defined?(OpenSSL) |