Skip to content

Commit

Permalink
Added unit tests to test ProtocolLogger exceptions in Connect/Connect…
Browse files Browse the repository at this point in the history
…Async
  • Loading branch information
jstedfast committed Sep 13, 2023
1 parent b30318e commit c0b3ec5
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 12 deletions.
6 changes: 6 additions & 0 deletions MailKit/Net/Imap/ImapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public ImapClient (IProtocolLogger protocolLogger) : base (protocolLogger)
engine.Alert += OnEngineAlert;
}

// Note: This is only needed for UnitTests.
internal char TagPrefix {
get { return engine.TagPrefix; }
set { engine.TagPrefix = value; }
}

/// <summary>
/// Gets an object that can be used to synchronize access to the IMAP server.
/// </summary>
Expand Down
13 changes: 2 additions & 11 deletions MailKit/Net/Imap/ImapEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public ImapEngine (CreateImapFolderDelegate createImapFolderDelegate)
Capabilities = ImapCapabilities.None;
QuirksMode = ImapQuirksMode.None;
queue = new List<ImapCommand> ();

TagPrefix = (char) ('A' + (TagPrefixIndex++ % 26));
}

/// <summary>
Expand Down Expand Up @@ -603,7 +605,6 @@ internal void SetStream (ImapStream stream)

void Initialize (ImapStream stream)
{
TagPrefix = (char) ('A' + (TagPrefixIndex++ % 26));
ProtocolVersion = ImapProtocolVersion.Unknown;
Capabilities = ImapCapabilities.None;
AuthenticationMechanisms.Clear ();
Expand Down Expand Up @@ -1267,16 +1268,6 @@ async Task SkipLineAsync (CancellationToken cancellationToken)
} while (token.Type != ImapTokenType.Eoln);
}

Task SkipLineAsync (bool doAsync, CancellationToken cancellationToken)
{
if (doAsync)
return SkipLineAsync (cancellationToken);

SkipLine (cancellationToken);

return Task.CompletedTask;
}

static bool TryParseUInt32 (string text, int startIndex, out uint value)
{
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
Expand Down
46 changes: 46 additions & 0 deletions UnitTests/ExceptionalProtocolLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;

using MailKit;

namespace UnitTests {
enum ExceptionalProtocolLoggerMode
{
ThrowOnLogConnect,
ThrowOnLogClient,
ThrowOnLogServer,
}

class ExceptionalProtocolLogger : IProtocolLogger
{
readonly ExceptionalProtocolLoggerMode mode;

public IAuthenticationSecretDetector AuthenticationSecretDetector { get; set; }

public ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode mode)
{
this.mode = mode;
}

public void LogConnect (Uri uri)
{
if (mode == ExceptionalProtocolLoggerMode.ThrowOnLogConnect)
throw new NotImplementedException ();
}

public void LogClient (byte[] buffer, int offset, int count)
{
if (mode == ExceptionalProtocolLoggerMode.ThrowOnLogClient)
throw new NotImplementedException ();
}

public void LogServer (byte[] buffer, int offset, int count)
{
if (mode == ExceptionalProtocolLoggerMode.ThrowOnLogServer)
throw new NotImplementedException ();
}

public void Dispose ()
{
}
}
}
27 changes: 27 additions & 0 deletions UnitTests/Net/Imap/ImapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,33 @@ public async Task TestSslHandshakeExceptionsAsync ()
}
}

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

using (var client = new ImapClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogConnect)) { TagPrefix = 'A' })
Assert.Throws<NotImplementedException> (() => client.Connect (Stream.Null, "imap.gmail.com", 143, SecureSocketOptions.None), "LogConnect");

using (var client = new ImapClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogConnect)) { TagPrefix = 'A' })
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (Stream.Null, "imap.gmail.com", 143, SecureSocketOptions.None), "LogConnect Async");

using (var client = new ImapClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogServer)) { TagPrefix = 'A' })
Assert.Throws<NotImplementedException> (() => client.Connect (new ImapReplayStream (commands, false), "imap.gmail.com", 143, SecureSocketOptions.None), "LogServer");

using (var client = new ImapClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogServer)) { TagPrefix = 'A' })
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (new ImapReplayStream (commands, true), "imap.gmail.com", 143, SecureSocketOptions.None), "LogServer Async");

using (var client = new ImapClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogClient)) { TagPrefix = 'A' })
Assert.Throws<NotImplementedException> (() => client.Connect (new ImapReplayStream (commands, false), "imap.gmail.com", 143, SecureSocketOptions.None), "LogClient");

using (var client = new ImapClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogClient)) { TagPrefix = 'A' })
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (new ImapReplayStream (commands, true), "imap.gmail.com", 143, SecureSocketOptions.None), "LogClient Async");
}

static void AssertGMailIsConnected (IMailService client)
{
Assert.IsTrue (client.IsConnected, "Expected the client to be connected");
Expand Down
27 changes: 27 additions & 0 deletions UnitTests/Net/Pop3/Pop3ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,33 @@ public async Task TestInvalidStateExceptionsAsync ()
}
}

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

using (var client = new Pop3Client (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogConnect)))
Assert.Throws<NotImplementedException> (() => client.Connect (Stream.Null, "pop.gmail.com", 110, SecureSocketOptions.None), "LogConnect");

using (var client = new Pop3Client (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogConnect)))
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (Stream.Null, "pop.gmail.com", 110, SecureSocketOptions.None), "LogConnect Async");

using (var client = new Pop3Client (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogServer)))
Assert.Throws<NotImplementedException> (() => client.Connect (new Pop3ReplayStream (commands, false), "pop.gmail.com", 110, SecureSocketOptions.None), "LogServer");

using (var client = new Pop3Client (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogServer)))
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (new Pop3ReplayStream (commands, true), "pop.gmail.com", 110, SecureSocketOptions.None), "LogServer Async");

using (var client = new Pop3Client (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogClient)))
Assert.Throws<NotImplementedException> (() => client.Connect (new Pop3ReplayStream (commands, false), "pop.gmail.com", 110, SecureSocketOptions.None), "LogClient");

using (var client = new Pop3Client (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogClient)))
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (new Pop3ReplayStream (commands, true), "pop.gmail.com", 110, SecureSocketOptions.None), "LogClient Async");
}

void AssertGMailIsConnected (IMailService client)
{
Assert.IsTrue (client.IsConnected, "Expected the client to be connected");
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/Net/Pop3/Pop3ReplayStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Pop3ReplayStream : Stream
bool isAsync;
int index;

public Pop3ReplayStream (IList<Pop3ReplayCommand> commands, bool asyncIO, bool testUnixFormat)
public Pop3ReplayStream (IList<Pop3ReplayCommand> commands, bool asyncIO, bool testUnixFormat = false)
{
stream = GetResourceStream (commands[0].Resource);
state = Pop3ReplayState.SendResponse;
Expand Down
27 changes: 27 additions & 0 deletions UnitTests/Net/Smtp/SmtpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,33 @@ public async Task TestServiceNotReadyAsync ()
}
}

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

using (var client = new SmtpClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogConnect)))
Assert.Throws<NotImplementedException> (() => client.Connect (Stream.Null, "smtp.gmail.com", 587, SecureSocketOptions.None), "LogConnect");

using (var client = new SmtpClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogConnect)))
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (Stream.Null, "smtp.gmail.com", 587, SecureSocketOptions.None), "LogConnect Async");

using (var client = new SmtpClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogServer)) { LocalDomain = "unit-tests.mimekit.org" })
Assert.Throws<NotImplementedException> (() => client.Connect (new SmtpReplayStream (commands, false), "smtp.gmail.com", 587, SecureSocketOptions.None), "LogServer");

using (var client = new SmtpClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogServer)) { LocalDomain = "unit-tests.mimekit.org" })
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (new SmtpReplayStream (commands, true), "smtp.gmail.com", 587, SecureSocketOptions.None), "LogServer Async");

using (var client = new SmtpClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogClient)) { LocalDomain = "unit-tests.mimekit.org" })
Assert.Throws<NotImplementedException> (() => client.Connect (new SmtpReplayStream (commands, false), "smtp.gmail.com", 587, SecureSocketOptions.None), "LogClient");

using (var client = new SmtpClient (new ExceptionalProtocolLogger (ExceptionalProtocolLoggerMode.ThrowOnLogClient)) { LocalDomain = "unit-tests.mimekit.org" })
Assert.ThrowsAsync<NotImplementedException> (() => client.ConnectAsync (new SmtpReplayStream (commands, true), "smtp.gmail.com", 587, SecureSocketOptions.None), "LogClient Async");
}

static void AssertGMailIsConnected (IMailService client)
{
Assert.IsTrue (client.IsConnected, "Expected the client to be connected");
Expand Down
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="DuplexStreamTests.cs" />
<Compile Include="EnvelopeTests.cs" />
<Compile Include="EventArgsTests.cs" />
<Compile Include="ExceptionalProtocolLogger.cs" />
<Compile Include="ExceptionTests.cs" />
<Compile Include="FolderNamespaceTests.cs" />
<Compile Include="HeaderSetTests.cs" />
Expand Down

0 comments on commit c0b3ec5

Please sign in to comment.