Skip to content

Commit 18e3900

Browse files
committed
βœ…πŸšš Organize most client tests into separate files
Many of the tests that remain in `test_imap` are connection related or edge cases, e.g: unexpected EOF, STARTTLS stripping, TLS with CA file, send_command argument translation. Those will probably stay in this file indefinitely. But some of them simply haven't been translated to `with_fake_server` yet and thus can't easily be moved, e.g: the tests for idle, disconnect, append, id. Those will be moved eventually.
1 parent 1a0c1a7 commit 18e3900

10 files changed

+850
-759
lines changed

β€Žtest/net/imap/test_imap.rb

Lines changed: 0 additions & 759 deletions
Large diffs are not rendered by default.
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
require_relative "fake_server"
6+
7+
class IMAPAuthenticateTest < Net::IMAP::TestCase
8+
include Net::IMAP::FakeServer::TestHelper
9+
10+
test("#authenticate sends an initial response " \
11+
"when supported by both the mechanism and the server") do
12+
with_fake_server(
13+
preauth: false, cleartext_auth: true, sasl_ir: true
14+
) do |server, imap|
15+
imap.authenticate("PLAIN", "test_user", "test-password")
16+
cmd = server.commands.pop
17+
assert_equal "AUTHENTICATE", cmd.name
18+
assert_equal(["PLAIN", ["\x00test_user\x00test-password"].pack("m0")],
19+
cmd.args)
20+
assert_empty server.commands
21+
end
22+
end
23+
24+
test("#authenticate sends '=' as the initial reponse " \
25+
"when the initial response is an empty string") do
26+
with_fake_server(
27+
preauth: false, cleartext_auth: true,
28+
sasl_ir: true, sasl_mechanisms: %i[EXTERNAL],
29+
) do |server, imap|
30+
server.on "AUTHENTICATE" do |cmd|
31+
server.state.authenticate(server.config.user)
32+
cmd.done_ok
33+
end
34+
imap.authenticate("EXTERNAL")
35+
cmd = server.commands.pop
36+
assert_equal "AUTHENTICATE", cmd.name
37+
assert_equal %w[EXTERNAL =], cmd.args
38+
assert_empty server.commands rescue pp server.commands.pop
39+
end
40+
end
41+
42+
test("#authenticate never sends an initial response " \
43+
"when the server doesn't explicitly support the mechanism") do
44+
with_fake_server(
45+
preauth: false, cleartext_auth: true,
46+
sasl_ir: true, sasl_mechanisms: %i[SCRAM-SHA-1 SCRAM-SHA-256],
47+
) do |server, imap|
48+
imap.authenticate("PLAIN", "test_user", "test-password")
49+
cmd, cont = 2.times.map { server.commands.pop }
50+
assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args]
51+
assert_equal(["\x00test_user\x00test-password"].pack("m0"),
52+
cont[:continuation].strip)
53+
assert_empty server.commands
54+
end
55+
end
56+
57+
test("#authenticate never sends an initial response " \
58+
"when the server isn't capable") do
59+
with_fake_server(
60+
preauth: false, cleartext_auth: true, sasl_ir: false
61+
) do |server, imap|
62+
imap.authenticate("PLAIN", "test_user", "test-password")
63+
cmd, cont = 2.times.map { server.commands.pop }
64+
assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args]
65+
assert_equal(["\x00test_user\x00test-password"].pack("m0"),
66+
cont[:continuation].strip)
67+
assert_empty server.commands
68+
end
69+
end
70+
71+
test("#authenticate never sends an initial response " \
72+
"when sasl_ir: false") do
73+
[true, false].each do |server_support|
74+
with_fake_server(
75+
preauth: false, cleartext_auth: true, sasl_ir: server_support
76+
) do |server, imap|
77+
imap.authenticate("PLAIN", "test_user", "test-password", sasl_ir: false)
78+
cmd, cont = 2.times.map { server.commands.pop }
79+
assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args]
80+
assert_equal(["\x00test_user\x00test-password"].pack("m0"),
81+
cont[:continuation].strip)
82+
assert_empty server.commands
83+
end
84+
end
85+
end
86+
87+
test("#authenticate never sends an initial response " \
88+
"when config.sasl_ir: false") do
89+
[true, false].each do |server_support|
90+
with_fake_server(
91+
preauth: false, cleartext_auth: true, sasl_ir: server_support
92+
) do |server, imap|
93+
imap.config.sasl_ir = false
94+
imap.authenticate("PLAIN", "test_user", "test-password")
95+
cmd, cont = 2.times.map { server.commands.pop }
96+
assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args]
97+
assert_equal(["\x00test_user\x00test-password"].pack("m0"),
98+
cont[:continuation].strip)
99+
assert_empty server.commands
100+
end
101+
end
102+
end
103+
104+
test("#authenticate never sends an initial response " \
105+
"when the mechanism does not support client-first") do
106+
with_fake_server(
107+
preauth: false, cleartext_auth: true,
108+
sasl_ir: true, sasl_mechanisms: %i[DIGEST-MD5]
109+
) do |server, imap|
110+
server.on "AUTHENTICATE" do |cmd|
111+
response_b64 = cmd.request_continuation(
112+
[
113+
%w[
114+
realm="somerealm"
115+
nonce="OA6MG9tEQGm2hh"
116+
qop="auth"
117+
charset=utf-8
118+
algorithm=md5-sess
119+
].join(",")
120+
].pack("m0")
121+
)
122+
state.commands << {continuation: response_b64}
123+
response_b64 = cmd.request_continuation(["rspauth="].pack("m0"))
124+
state.commands << {continuation: response_b64}
125+
server.state.authenticate(server.config.user)
126+
cmd.done_ok
127+
end
128+
imap.authenticate(:digest_md5, "test_user", "test-password",
129+
warn_deprecation: false)
130+
cmd, cont1, cont2 = 3.times.map { server.commands.pop }
131+
assert_equal %w[AUTHENTICATE DIGEST-MD5], [cmd.name, *cmd.args]
132+
assert_match(%r{\A[a-z0-9+/]+=*\z}i, cont1[:continuation].strip)
133+
assert_empty cont2[:continuation].strip
134+
assert_empty server.commands
135+
end
136+
end
137+
138+
test("#authenticate disconnects and raises SASL::AuthenticationFailed " \
139+
"when the server succeeds prematurely") do
140+
with_fake_server(
141+
preauth: false, cleartext_auth: true,
142+
sasl_ir: true, sasl_mechanisms: %i[DIGEST-MD5]
143+
) do |server, imap|
144+
server.on "AUTHENTICATE" do |cmd|
145+
response_b64 = cmd.request_continuation(
146+
[
147+
%w[
148+
realm="somerealm"
149+
nonce="OA6MG9tEQGm2hh"
150+
qop="auth"
151+
charset=utf-8
152+
algorithm=md5-sess
153+
].join(",")
154+
].pack("m0")
155+
)
156+
state.commands << {continuation: response_b64}
157+
server.state.authenticate(server.config.user)
158+
cmd.done_ok
159+
end
160+
assert_raise(Net::IMAP::SASL::AuthenticationIncomplete) do
161+
imap.authenticate("DIGEST-MD5", "test_user", "test-password",
162+
warn_deprecation: false)
163+
end
164+
assert imap.disconnected?
165+
end
166+
end
167+
168+
end

β€Žtest/net/imap/test_imap_close.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
require_relative "fake_server"
6+
7+
class IMAPSelectTest < Net::IMAP::TestCase
8+
include Net::IMAP::FakeServer::TestHelper
9+
10+
def test_close
11+
with_fake_server(select: "inbox") do |server, imap|
12+
resp = imap.close
13+
assert_equal("RUBY0002 CLOSE", server.commands.pop.raw.strip)
14+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0002", "OK"],
15+
[resp.class, resp.tag, resp.name])
16+
assert_empty server.commands
17+
end
18+
end
19+
20+
def test_unselect
21+
with_fake_server(select: "inbox") do |server, imap|
22+
resp = imap.unselect
23+
sent = server.commands.pop
24+
assert_equal(["UNSELECT", nil], [sent.name, sent.args])
25+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0002", "OK"],
26+
[resp.class, resp.tag, resp.name])
27+
assert_empty server.commands
28+
end
29+
end
30+
31+
end

β€Žtest/net/imap/test_imap_enable.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
require_relative "fake_server"
6+
7+
class IMAPEnableTest < Net::IMAP::TestCase
8+
include Net::IMAP::FakeServer::TestHelper
9+
10+
def test_enable
11+
with_fake_server(
12+
with_extensions: %i[ENABLE CONDSTORE UTF8=ACCEPT],
13+
capabilities_enablable: %w[CONDSTORE UTF8=ACCEPT]
14+
) do |server, imap|
15+
cmdq = server.commands
16+
17+
result1 = imap.enable(%w[CONDSTORE x-pig-latin])
18+
result2 = imap.enable(:utf8, "condstore QResync")
19+
result3 = imap.enable(:utf8, "UTF8=ACCEPT", "UTF8=ONLY")
20+
cmd1, cmd2, cmd3 = Array.new(3) { cmdq.pop.raw.strip }
21+
22+
assert_equal "RUBY0001 ENABLE CONDSTORE x-pig-latin", cmd1
23+
assert_equal "RUBY0002 ENABLE UTF8=ACCEPT condstore QResync", cmd2
24+
assert_equal "RUBY0003 ENABLE UTF8=ACCEPT", cmd3
25+
assert_empty cmdq
26+
27+
assert_equal %w[CONDSTORE], result1
28+
assert_equal %w[UTF8=ACCEPT], result2
29+
assert_equal [], result3
30+
end
31+
end
32+
33+
test("missing server ENABLED response") do
34+
with_fake_server do |server, imap|
35+
server.on "ENABLE", &:done_ok
36+
enabled = imap.enable "foo", "bar", "baz"
37+
assert_equal [], enabled
38+
end
39+
end
40+
41+
end

β€Žtest/net/imap/test_imap_expunge.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
require_relative "fake_server"
6+
7+
class IMAPExpungeTest < Net::IMAP::TestCase
8+
include Net::IMAP::FakeServer::TestHelper
9+
10+
test "#expunge with EXPUNGE responses" do
11+
with_fake_server(select: "INBOX") do |server, imap|
12+
server.on "EXPUNGE" do |resp|
13+
resp.untagged("1 EXPUNGE")
14+
resp.untagged("1 EXPUNGE")
15+
resp.untagged("99 EXPUNGE")
16+
resp.done_ok
17+
end
18+
response = imap.expunge
19+
cmd = server.commands.pop
20+
assert_equal ["EXPUNGE", nil], [cmd.name, cmd.args]
21+
assert_equal [1, 1, 99], response
22+
assert_equal [], imap.clear_responses("EXPUNGED")
23+
end
24+
end
25+
26+
test "#expunge with a VANISHED response" do
27+
with_fake_server(select: "INBOX") do |server, imap|
28+
server.on "EXPUNGE" do |resp|
29+
resp.untagged("VANISHED 15:456")
30+
resp.done_ok
31+
end
32+
response = imap.expunge
33+
cmd = server.commands.pop
34+
assert_equal ["EXPUNGE", nil], [cmd.name, cmd.args]
35+
assert_equal(
36+
Net::IMAP::VanishedData[uids: [15..456], earlier: false],
37+
response
38+
)
39+
assert_equal([], imap.clear_responses("VANISHED"))
40+
end
41+
end
42+
43+
test "#expunge with multiple VANISHED responses" do
44+
with_fake_server(select: "INBOX") do |server, imap|
45+
server.unsolicited("VANISHED 86")
46+
server.on "EXPUNGE" do |resp|
47+
resp.untagged("VANISHED (EARLIER) 1:5,99,123")
48+
resp.untagged("VANISHED 15,456")
49+
resp.untagged("VANISHED (EARLIER) 987,1001")
50+
resp.done_ok
51+
end
52+
response = imap.expunge
53+
cmd = server.commands.pop
54+
assert_equal ["EXPUNGE", nil], [cmd.name, cmd.args]
55+
assert_equal(
56+
Net::IMAP::VanishedData[uids: [15, 86, 456], earlier: false],
57+
response
58+
)
59+
assert_equal(
60+
[
61+
Net::IMAP::VanishedData[uids: [1..5, 99, 123], earlier: true],
62+
Net::IMAP::VanishedData[uids: [987, 1001], earlier: true],
63+
],
64+
imap.clear_responses("VANISHED")
65+
)
66+
end
67+
end
68+
69+
test "#uid_expunge with EXPUNGE responses" do
70+
with_fake_server(select: "INBOX",
71+
extensions: %i[UIDPLUS]) do |server, imap|
72+
server.on "UID EXPUNGE" do |resp|
73+
resp.untagged("1 EXPUNGE")
74+
resp.untagged("1 EXPUNGE")
75+
resp.untagged("1 EXPUNGE")
76+
resp.done_ok
77+
end
78+
response = imap.uid_expunge(1000..1003)
79+
cmd = server.commands.pop
80+
assert_equal ["UID EXPUNGE", "1000:1003"], [cmd.name, cmd.args]
81+
assert_equal(response, [1, 1, 1])
82+
end
83+
end
84+
85+
test "#uid_expunge with VANISHED response" do
86+
with_fake_server(select: "INBOX",
87+
extensions: %i[UIDPLUS]) do |server, imap|
88+
server.on "UID EXPUNGE" do |resp|
89+
resp.untagged("VANISHED 1001,1003")
90+
resp.done_ok
91+
end
92+
response = imap.uid_expunge(1000..1003)
93+
cmd = server.commands.pop
94+
assert_equal ["UID EXPUNGE", "1000:1003"], [cmd.name, cmd.args]
95+
assert_equal(
96+
Net::IMAP::VanishedData[uids: [1001, 1003], earlier: false],
97+
response
98+
)
99+
assert_equal([], imap.clear_responses("VANISHED"))
100+
end
101+
end
102+
103+
end

0 commit comments

Comments
Β (0)