Skip to content

Commit

Permalink
Added more SmtpStream unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Jan 15, 2024
1 parent 0bb3367 commit 14272a6
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
22 changes: 21 additions & 1 deletion UnitTests/Net/DummyNetworkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,37 @@
// THE SOFTWARE.
//


namespace UnitTests.Net {
public class DummyNetworkStream : MemoryStream
{
public DummyNetworkStream ()
readonly bool throwOnWrite;

public DummyNetworkStream (bool throwOnWrite = false)
{
this.throwOnWrite = throwOnWrite;
}

public override bool CanSeek => false;
public override bool CanTimeout => true;

public override int ReadTimeout { get; set; }
public override int WriteTimeout { get; set; }

public override void Write (byte[] buffer, int offset, int count)
{
if (throwOnWrite)
throw new IOException ();

base.Write (buffer, offset, count);
}

public override Task WriteAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (throwOnWrite)
throw new IOException ();

return base.WriteAsync (buffer, offset, count, cancellationToken);
}
}
}
105 changes: 105 additions & 0 deletions UnitTests/Net/Smtp/SmtpStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,21 @@ public void TestQueueReallyLongCommand ()
Assert.That (actual, Is.EqualTo (command));
}

[Test]
public async Task TestQueueReallyLongCommandAsync ()
{
using var stream = new SmtpStream (new DummyNetworkStream (), new NullProtocolLogger ());
var memory = (MemoryStream) stream.Stream;
var command = "AUTH GSSAPI YIIkMgYGK" + new string ('X', 4096) + "\r\n";

await stream.QueueCommandAsync (command, default);
stream.Flush ();

var actual = Encoding.ASCII.GetString (memory.GetBuffer (), 0, (int) memory.Length);

Assert.That (actual, Is.EqualTo (command));
}

[Test]
public void TestQueueReallyLongCommandAfterShortCommand ()
{
Expand All @@ -316,5 +331,95 @@ public void TestQueueReallyLongCommandAfterShortCommand ()

Assert.That (actual, Is.EqualTo (shortCommand + longCommand));
}

[Test]
public async Task TestQueueReallyLongCommandAfterShortCommandAsync ()
{
using var stream = new SmtpStream (new DummyNetworkStream (), new NullProtocolLogger ());
var memory = (MemoryStream) stream.Stream;

var shortCommand = "EHLO [192.168.1.1]\r\n";
var longCommand = "AUTH GSSAPI YIIkMgYGK" + new string ('X', 4096) + "\r\n";

await stream.QueueCommandAsync (shortCommand, default);
await stream.QueueCommandAsync (longCommand, default);
stream.Flush ();

var actual = Encoding.ASCII.GetString (memory.GetBuffer (), 0, (int) memory.Length);

Assert.That (actual, Is.EqualTo (shortCommand + longCommand));
}

[Test]
public void TestQueueOverflowRemainingOutputBufferCommand ()
{
using var stream = new SmtpStream (new DummyNetworkStream (), new NullProtocolLogger ());
var memory = (MemoryStream) stream.Stream;

var shortCommand = "EHLO [192.168.1.1]\r\n";
var longCommand = "AUTH GSSAPI YIIkMgYGK" + new string ('X', 4096 - shortCommand.Length - 22) + "\r\n";

stream.QueueCommand (shortCommand, default);
stream.QueueCommand (longCommand, default);
stream.Flush ();

var actual = Encoding.ASCII.GetString (memory.GetBuffer (), 0, (int) memory.Length);

Assert.That (actual, Is.EqualTo (shortCommand + longCommand));
}

[Test]
public async Task TestQueueOverflowRemainingOutputBufferCommandAsync ()
{
using var stream = new SmtpStream (new DummyNetworkStream (), new NullProtocolLogger ());
var memory = (MemoryStream) stream.Stream;

var shortCommand = "EHLO [192.168.1.1]\r\n";
var longCommand = "AUTH GSSAPI YIIkMgYGK" + new string ('X', 4096 - shortCommand.Length - 22) + "\r\n";

await stream.QueueCommandAsync (shortCommand, default);
await stream.QueueCommandAsync (longCommand, default);
stream.Flush ();

var actual = Encoding.ASCII.GetString (memory.GetBuffer (), 0, (int) memory.Length);

Assert.That (actual, Is.EqualTo (shortCommand + longCommand));
}

[Test]
public void TestDisconnectOnWriteException ()
{
using var stream = new SmtpStream (new DummyNetworkStream (throwOnWrite: true), new NullProtocolLogger ());
var memory = (MemoryStream) stream.Stream;

var command = new string ('a', 4094) + "\r\n";
var buffer = Encoding.ASCII.GetBytes (command);

try {
stream.Write (buffer, 0, buffer.Length, CancellationToken.None);
Assert.Fail ("Expected IOException to be thrown.");
} catch (IOException) {
}

Assert.That (stream.IsConnected, Is.False);
}

[Test]
public async Task TestDisconnectOnWriteExceptionAsync ()
{
using var stream = new SmtpStream (new DummyNetworkStream (throwOnWrite: true), new NullProtocolLogger ());
var memory = (MemoryStream) stream.Stream;

var command = new string ('a', 4094) + "\r\n";
var buffer = Encoding.ASCII.GetBytes (command);

try {
await stream.WriteAsync (buffer, 0, buffer.Length, CancellationToken.None);
Assert.Fail ("Expected IOException to be thrown.");
} catch (IOException) {
}

Assert.That (stream.IsConnected, Is.False);
}
}
}

0 comments on commit 14272a6

Please sign in to comment.