Skip to content

Commit b36992c

Browse files
authored
Merge pull request #35 from tmtm/tls_params_after_initialize
add Net::SMTP#tls_verify, Net::SMTP#tls_hostname, Net::SMTP#ssl_context_params
2 parents 78b087f + 3f19dfc commit b36992c

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

lib/net/smtp.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ def initialize(address, port = nil, tls: false, starttls: :auto, tls_verify: tru
251251
@ssl_context_params = ssl_context_params
252252
end
253253

254+
# If +true+, verify th server's certificate.
255+
attr_accessor :tls_verify
256+
257+
# The hostname for verifying hostname in the server certificatate.
258+
attr_accessor :tls_hostname
259+
260+
# Hash for additional SSLContext parameters.
261+
attr_accessor :ssl_context_params
262+
254263
# Provide human-readable stringification of class state.
255264
def inspect
256265
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"

test/net/smtp/test_sslcontext.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ def test_start_with_tls_verify_false
166166
assert_equal(true, smtp.rset.success?)
167167
end
168168

169+
def test_tls_verify_true_after_initialize
170+
smtp = MySMTP.new(start_smtpd_starttls, tls_verify: false)
171+
smtp.tls_verify = true
172+
assert_raise(OpenSSL::SSL::SSLError) { smtp.start }
173+
assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, smtp.__ssl_socket.verify_result)
174+
assert_equal(OpenSSL::SSL::VERIFY_PEER, smtp.__ssl_context.verify_mode)
175+
end
176+
177+
def test_tls_verify_false_after_initialize
178+
smtp = MySMTP.new(start_smtpd_starttls, tls_verify: true)
179+
smtp.tls_verify = false
180+
smtp.start
181+
assert_equal(OpenSSL::SSL::VERIFY_NONE, smtp.__ssl_context.verify_mode)
182+
assert_equal(true, smtp.rset.success?)
183+
end
184+
169185
def test_start_with_tls_hostname
170186
smtp = MySMTP.new(start_smtpd_starttls, tls_hostname: "unexpected.example.com")
171187
context = default_ssl_context
@@ -184,10 +200,27 @@ def test_start_without_tls_hostname
184200
assert_equal(true, smtp.rset.success?)
185201
end
186202

203+
def test_tls_hostname_after_initialize
204+
smtp = MySMTP.new(start_smtpd_starttls)
205+
smtp.tls_hostname = "unexpected.example.com"
206+
context = default_ssl_context
207+
smtp.enable_starttls(context)
208+
assert_raise(OpenSSL::SSL::SSLError) { smtp.start }
209+
# TODO: Not all OpenSSL versions have the same verify_result code
210+
assert_equal("unexpected.example.com", smtp.__ssl_socket.hostname)
211+
end
212+
187213
def test_start_with_ssl_context_params
188214
smtp = MySMTP.new(start_smtpd_starttls, ssl_context_params: {timeout: 123, verify_mode: OpenSSL::SSL::VERIFY_NONE})
189215
smtp.start
190216
assert_equal(123, smtp.__ssl_context.timeout)
191217
end
218+
219+
def test_ssl_context_params_after_initialize
220+
smtp = MySMTP.new(start_smtpd_starttls)
221+
smtp.ssl_context_params = {timeout: 123, verify_mode: OpenSSL::SSL::VERIFY_NONE}
222+
smtp.start
223+
assert_equal(123, smtp.__ssl_context.timeout)
224+
end
192225
end
193226
end

0 commit comments

Comments
 (0)