Skip to content

Commit 9a7858e

Browse files
committed
🔍 Simplify Net::IMAP#inspect with basic state
Most importantly, this no longer prints out *every* instance variable, some of which could contain sensitive data, and others are just _very_ verbose. Now `Net::IMAP#inspect` only prints the host:port, TLS state, and IMAP connection state. It will be extended to include more in the future.
1 parent 853fd13 commit 9a7858e

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

lib/net/imap.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,31 @@ def initialize(host, port: nil, ssl: nil, response_handlers: nil,
11201120
start_imap_connection
11211121
end
11221122

1123+
# Returns a string representation of +self+, showing basic client state
1124+
# information.
1125+
#
1126+
# imap = Net::IMAP.new(hostname, ssl: true)
1127+
# imap.inspect #=> "#<Net::IMAP imap.example.net:993 TLS not_authenticated>"
1128+
#
1129+
# imap.authenticate(:oauthbearer, "user", token)
1130+
# imap.inspect #=> "#<Net::IMAP imap.example.net:993 TLS authenticated>"
1131+
#
1132+
# imap.select("INBOX")
1133+
# imap.inspect #=> "#<Net::IMAP imap.example.net:993 TLS selected>"
1134+
#
1135+
# imap.logout
1136+
# imap.inspect #=> "#<Net::IMAP imap.example.net:993 TLS logout>"
1137+
#
1138+
def inspect
1139+
tls_state = tls_verified? ? "TLS" :
1140+
ssl_ctx ? "TLS (NOT VERIFIED)" :
1141+
"PLAINTEXT"
1142+
conn_state = disconnected? ? "disconnected" : connection_state.to_sym
1143+
"#<%s:0x%08x %s:%s %s %s>" % [
1144+
self.class.name, __id__, host, port, tls_state, conn_state
1145+
]
1146+
end
1147+
11231148
# Returns true after the TLS negotiation has completed and the remote
11241149
# hostname has been verified. Returns false when TLS has been established
11251150
# but peer verification was disabled.

test/net/imap/test_imap_inspect.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
require_relative "fake_server"
6+
7+
class IMAPInspectTest < Net::IMAP::TestCase
8+
include Net::IMAP::FakeServer::TestHelper
9+
10+
def format_inspect(client, details)
11+
"#<Net::IMAP:0x%s %s:%s %s>" % [
12+
"%08x" % client.__id__, # NOTE: this is different from `super`
13+
client.host,
14+
client.port,
15+
details,
16+
]
17+
end
18+
19+
test "#inspect for every connection state (plaintext)" do
20+
with_fake_server(preauth: false) do |server, imap|
21+
assert_equal(format_inspect(imap, "PLAINTEXT not_authenticated"),
22+
imap.inspect)
23+
# AUTHENTICATE, SELECT, CLOSE
24+
imap.authenticate :plain, "test_user", "test-password"
25+
assert_equal(format_inspect(imap, "PLAINTEXT authenticated"),
26+
imap.inspect)
27+
imap.select "INBOX"
28+
assert_equal(format_inspect(imap, "PLAINTEXT selected"),
29+
imap.inspect)
30+
imap.close
31+
assert_equal(format_inspect(imap, "PLAINTEXT authenticated"),
32+
imap.inspect)
33+
imap.logout
34+
assert_equal(format_inspect(imap, "PLAINTEXT logout"),
35+
imap.inspect)
36+
imap.disconnect
37+
assert_equal(format_inspect(imap, "PLAINTEXT disconnected"),
38+
imap.inspect)
39+
end
40+
end
41+
42+
test "#inspect for TLS verified" do
43+
with_fake_server(implicit_tls: true) do |server, imap|
44+
assert_equal(format_inspect(imap, "TLS authenticated"),
45+
imap.inspect)
46+
imap.logout
47+
assert_equal(format_inspect(imap, "TLS logout"),
48+
imap.inspect)
49+
imap.disconnect
50+
assert_equal(format_inspect(imap, "TLS disconnected"),
51+
imap.inspect)
52+
end
53+
end
54+
55+
test "#inspect for TLS unverified" do
56+
with_fake_server(preauth: false) do |server, imap|
57+
imap.starttls verify_mode: OpenSSL::SSL::VERIFY_NONE
58+
assert_equal(format_inspect(imap, "TLS (NOT VERIFIED) not_authenticated"),
59+
imap.inspect)
60+
imap.logout
61+
assert_equal(format_inspect(imap, "TLS (NOT VERIFIED) logout"),
62+
imap.inspect)
63+
imap.disconnect
64+
assert_equal(format_inspect(imap, "TLS (NOT VERIFIED) disconnected"),
65+
imap.inspect)
66+
end
67+
end
68+
69+
end

0 commit comments

Comments
 (0)