Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Net::SMTP#tls_verify, Net::SMTP#tls_hostname, Net::SMTP#ssl_context_params #35

Merged
merged 1 commit into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ def initialize(address, port = nil, tls: false, starttls: :auto, tls_verify: tru
@ssl_context_params = ssl_context_params
end

# If +true+, verify th server's certificate.
attr_accessor :tls_verify

# The hostname for verifying hostname in the server certificatate.
attr_accessor :tls_hostname

# Hash for additional SSLContext parameters.
attr_accessor :ssl_context_params

# Provide human-readable stringification of class state.
def inspect
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
Expand Down
33 changes: 33 additions & 0 deletions test/net/smtp/test_sslcontext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ def test_start_with_tls_verify_false
assert_equal(true, smtp.rset.success?)
end

def test_tls_verify_true_after_initialize
smtp = MySMTP.new(start_smtpd_starttls, tls_verify: false)
smtp.tls_verify = true
assert_raise(OpenSSL::SSL::SSLError) { smtp.start }
assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, smtp.__ssl_socket.verify_result)
assert_equal(OpenSSL::SSL::VERIFY_PEER, smtp.__ssl_context.verify_mode)
end

def test_tls_verify_false_after_initialize
smtp = MySMTP.new(start_smtpd_starttls, tls_verify: true)
smtp.tls_verify = false
smtp.start
assert_equal(OpenSSL::SSL::VERIFY_NONE, smtp.__ssl_context.verify_mode)
assert_equal(true, smtp.rset.success?)
end

def test_start_with_tls_hostname
smtp = MySMTP.new(start_smtpd_starttls, tls_hostname: "unexpected.example.com")
context = default_ssl_context
Expand All @@ -184,10 +200,27 @@ def test_start_without_tls_hostname
assert_equal(true, smtp.rset.success?)
end

def test_tls_hostname_after_initialize
smtp = MySMTP.new(start_smtpd_starttls)
smtp.tls_hostname = "unexpected.example.com"
context = default_ssl_context
smtp.enable_starttls(context)
assert_raise(OpenSSL::SSL::SSLError) { smtp.start }
# TODO: Not all OpenSSL versions have the same verify_result code
assert_equal("unexpected.example.com", smtp.__ssl_socket.hostname)
end

def test_start_with_ssl_context_params
smtp = MySMTP.new(start_smtpd_starttls, ssl_context_params: {timeout: 123, verify_mode: OpenSSL::SSL::VERIFY_NONE})
smtp.start
assert_equal(123, smtp.__ssl_context.timeout)
end

def test_ssl_context_params_after_initialize
smtp = MySMTP.new(start_smtpd_starttls)
smtp.ssl_context_params = {timeout: 123, verify_mode: OpenSSL::SSL::VERIFY_NONE}
smtp.start
assert_equal(123, smtp.__ssl_context.timeout)
end
end
end