Skip to content
Open
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
21 changes: 17 additions & 4 deletions lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1160,16 +1160,29 @@ def initialize(host, port: nil, ssl: nil, response_handlers: nil,
# imap.logout
# imap.inspect #=> "#<Net::IMAP imap.example.net:993 TLS logout>"
#
# imap = Net::IMAP.new(hostname, ssl: false)
# imap.inspect #=> "#<Net::IMAP imap.example.net:143 PLAINTEXT not_authenticated>"
#
# imap.starttls verify_mode: OpenSSL::SSL::VERIFY_NONE
# imap.inspect #=> "#<Net::IMAP imap.example.net:993 TLS (NOT VERIFIED) not_authenticated>"
#
def inspect
tls_state = tls_verified? ? "TLS" :
ssl_ctx ? "TLS (NOT VERIFIED)" :
"PLAINTEXT"
conn_state = disconnected? ? "disconnected" : connection_state.to_sym
"#<%s:0x%08x %s:%s %s %s>" % [
self.class.name, __id__, host, port, tls_state, conn_state
self.class.name, __id__, host, port, inspect_tls_state, conn_state
]
end

private def inspect_tls_state
if tls_verified?
"TLS"
elsif ssl_ctx && @sock.kind_of?(OpenSSL::SSL::SSLSocket)
"TLS (#{@sock.session ? "NOT VERIFIED" : "NOT ESTABLISHED"})"
else
"PLAINTEXT#{" (TLS NOT STARTED)" if ssl_ctx}"
end
end

# Returns true after the TLS negotiation has completed and the remote
# hostname has been verified. Returns false when TLS has been established
# but peer verification was disabled.
Expand Down
5 changes: 4 additions & 1 deletion test/net/imap/test_imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def test_starttls
assert_equal false, initial_verified
assert_equal false, initial_params
assert_equal nil, initial_ctx
assert_equal true, imap.tls_verified?
assert_equal true, imap.tls_verified?
assert_include imap.inspect, " TLS disconnected"
assert_equal({ca_file: CA_FILE}, imap.ssl_ctx_params)
rescue SystemCallError
skip $!
Expand Down Expand Up @@ -164,6 +165,7 @@ def test_starttls_stripping_not_ok
end

assert_equal false, imap.tls_verified?
assert_include imap.inspect, " PLAINTEXT (TLS NOT STARTED) "
assert_equal({ca_file: CA_FILE}, imap.ssl_ctx_params)
assert_equal(CA_FILE, imap.ssl_ctx.ca_file)
assert_equal(OpenSSL::SSL::VERIFY_PEER, imap.ssl_ctx.verify_mode)
Expand Down Expand Up @@ -201,6 +203,7 @@ def test_starttls_stripping_ok_sent_before_response
imap.disconnect if imap && !imap.disconnected?
end
assert_equal false, imap.tls_verified?
assert_include imap.inspect, " PLAINTEXT (TLS NOT STARTED) "
assert_equal({ca_file: CA_FILE}, imap.ssl_ctx_params)
assert_equal(CA_FILE, imap.ssl_ctx.ca_file)
assert_equal(OpenSSL::SSL::VERIFY_PEER, imap.ssl_ctx.verify_mode)
Expand Down
Loading