Skip to content

Commit

Permalink
Added unit tests for STARTTLS not being supported by the server
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Sep 13, 2023
1 parent e165696 commit 1a92b17
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
19 changes: 17 additions & 2 deletions UnitTests/Net/Imap/ImapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static Socket Connect (string host, int port)
[Test]
public void TestSslHandshakeExceptions ()
{
using (var client = new ImapClient () { TagPrefix = 'A' }) {
using (var client = new ImapClient ()) {
Socket socket;

// 1. Test connecting to a non-SSL port fails with an SslHandshakeException.
Expand Down Expand Up @@ -376,7 +376,7 @@ public void TestSslHandshakeExceptions ()
[Test]
public async Task TestSslHandshakeExceptionsAsync ()
{
using (var client = new ImapClient () { TagPrefix = 'A' }) {
using (var client = new ImapClient ()) {
Socket socket;

// 1. Test connecting to a non-SSL port fails with an SslHandshakeException.
Expand Down Expand Up @@ -417,6 +417,21 @@ public async Task TestSslHandshakeExceptionsAsync ()
}
}

[Test]
public void TestStartTlsNotSupported ()
{
var commands = new List<ImapReplayCommand> {
new ImapReplayCommand ("", "common.basic-greeting.txt"),
new ImapReplayCommand ("A00000000 CAPABILITY\r\n", "common.capability.txt"),
};

using (var client = new ImapClient () { TagPrefix = 'A' })
Assert.Throws<NotSupportedException> (() => client.Connect (new ImapReplayStream (commands, false), "localhost", 143, SecureSocketOptions.StartTls), "STARTTLS");

using (var client = new ImapClient () { TagPrefix = 'A' })
Assert.ThrowsAsync<NotSupportedException> (() => client.ConnectAsync (new ImapReplayStream (commands, true), "localhost", 143, SecureSocketOptions.StartTls), "STARTTLS Async");
}

[Test]
public void TestProtocolLoggerExceptions ()
{
Expand Down
1 change: 1 addition & 0 deletions UnitTests/Net/Imap/Resources/common/basic-greeting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* OK IMAP4 server ready.
17 changes: 17 additions & 0 deletions UnitTests/Net/Pop3/Pop3ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,21 @@ public async Task TestInvalidStateExceptionsAsync ()
}
}

[Test]
public void TestStartTlsNotSupported ()
{
var commands = new List<Pop3ReplayCommand> {
new Pop3ReplayCommand ("", "comcast.greeting.txt"),
new Pop3ReplayCommand ("CAPA\r\n", "comcast.err.txt")
};

using (var client = new Pop3Client ())
Assert.Throws<NotSupportedException> (() => client.Connect (new Pop3ReplayStream (commands, false), "localhost", 110, SecureSocketOptions.StartTls), "STLS");

using (var client = new Pop3Client ())
Assert.ThrowsAsync<NotSupportedException> (() => client.ConnectAsync (new Pop3ReplayStream (commands, true), "localhost", 110, SecureSocketOptions.StartTls), "STLS Async");
}

[Test]
public void TestProtocolLoggerExceptions ()
{
Expand Down Expand Up @@ -1671,6 +1686,7 @@ public void TestProbedUidlSupportError ()
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.Throws<NotSupportedException> (() => client.GetMessageUids ());
Assert.Throws<NotSupportedException> (() => client.GetMessageUid (0));

try {
client.Disconnect (true);
Expand Down Expand Up @@ -1723,6 +1739,7 @@ public async Task TestProbedUidlSupportErrorAsync ()
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.ThrowsAsync<NotSupportedException> (() => client.GetMessageUidsAsync ());
Assert.ThrowsAsync<NotSupportedException> (() => client.GetMessageUidAsync (0));

try {
await client.DisconnectAsync (true);
Expand Down
16 changes: 16 additions & 0 deletions UnitTests/Net/Smtp/SmtpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,22 @@ public async Task TestInvalidStateExceptionsAsync ()
}
}

[Test]
public void TestStartTlsNotSupported ()
{
var commands = new List<SmtpReplayCommand> {
new SmtpReplayCommand ("", "comcast-greeting.txt"),
new SmtpReplayCommand ("EHLO unit-tests.mimekit.org\r\n", "ehlo-failed.txt"),
new SmtpReplayCommand ("HELO unit-tests.mimekit.org\r\n", "helo.txt"),
};

using (var client = new SmtpClient () { LocalDomain = "unit-tests.mimekit.org" })
Assert.Throws<NotSupportedException> (() => client.Connect (new SmtpReplayStream (commands, false), "localhost", 25, SecureSocketOptions.StartTls), "STARTTLS");

using (var client = new SmtpClient () { LocalDomain = "unit-tests.mimekit.org" })
Assert.ThrowsAsync<NotSupportedException> (() => client.ConnectAsync (new SmtpReplayStream (commands, true), "localhost", 25, SecureSocketOptions.StartTls), "STARTTLS Async");
}

[Test]
public void TestServiceNotReady ()
{
Expand Down
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<EmbeddedResource Include="Net\Imap\Resources\acl\getacl.txt" />
<EmbeddedResource Include="Net\Imap\Resources\acl\listrights.txt" />
<EmbeddedResource Include="Net\Imap\Resources\acl\myrights.txt" />
<EmbeddedResource Include="Net\Imap\Resources\common\basic-greeting.txt" />
<EmbeddedResource Include="Net\Imap\Resources\common\capability.txt" />
<EmbeddedResource Include="Net\Imap\Resources\common\capability-greeting.txt" />
<EmbeddedResource Include="Net\Imap\Resources\common\fetch-annotations.txt" />
Expand Down

0 comments on commit 1a92b17

Please sign in to comment.