Skip to content

Commit

Permalink
SmtpClientSimulator should throw on error rather than returning true/…
Browse files Browse the repository at this point in the history
…false
  • Loading branch information
martinknafve committed Oct 30, 2014
1 parent f44689d commit e372130
Show file tree
Hide file tree
Showing 35 changed files with 336 additions and 301 deletions.
4 changes: 2 additions & 2 deletions hmailserver/test/RegressionTests/API/Basics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ public void TestReinitialize()
"WhatTest\r\n";

Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");
Assert.IsTrue(SMTPClientSimulator.StaticSend(account.Address, account.Address, "First message",
"Test message"));
SMTPClientSimulator.StaticSend(account.Address, account.Address, "First message",
"Test message");
POP3ClientSimulator.AssertMessageCount(account.Address, "test", 1);

// Create another message on disk and import it.
Expand Down
47 changes: 22 additions & 25 deletions hmailserver/test/RegressionTests/AntiSpam/Basics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void TestHeloSpamTest()
// Send a messages to this account.
var oSMTP = new SMTPClientSimulator();

Assert.IsFalse(oSMTP.Send("[email protected]", "[email protected]", "SURBL-Match", "Test"));
CustomAsserts.Throws<DeliveryFailedException>(() => oSMTP.Send("[email protected]", "[email protected]", "SURBL-Match", "Test"));
}

[Test]
Expand All @@ -104,25 +104,23 @@ public void TestIncorrectLineEndings()
// Send a messages to this account.

var oSMTP = new SMTPClientSimulator();
if (
!oSMTP.Send("[email protected]", "[email protected]", "INBOX",
"This is a test message\r\n consisting of correct lines"))
throw new Exception("ERROR - Was not able to send correct email.");
oSMTP.Send("[email protected]", "[email protected]", "INBOX",
"This is a test message\r\n consisting of correct lines");

CustomAsserts.Throws<DeliveryFailedException>(() => oSMTP.Send("[email protected]",
"[email protected]", "INBOX",
"This is a test message\r consisting of incorrect lines"));

if (oSMTP.Send("[email protected]", "[email protected]", "INBOX",
"This is a test message\r consisting of incorrect lines"))
throw new Exception("ERROR - Was able to send incorrect email.");


if (oSMTP.Send("[email protected]", "[email protected]", "INBOX",
"This is a test message\n consisting of incorrect lines"))
throw new Exception("ERROR - Was able to send incorrect email.");
CustomAsserts.Throws<DeliveryFailedException>(
() => oSMTP.Send("[email protected]", "SpamProtectionLineEndings@test.com", "INBOX",
"This is a test message\n consisting of incorrect lines"));


if (oSMTP.Send("[email protected]", "[email protected]", "INBOX",
"This is a test message\n\r consisting of incorrect lines"))
throw new Exception("ERROR - Was able to send incorrect email.");
CustomAsserts.Throws<DeliveryFailedException>(() => oSMTP.Send("[email protected]",
"SpamProtectionLineEndings@test.com", "INBOX",
"This is a test message\n\r consisting of incorrect lines"));

POP3ClientSimulator.AssertMessageCount(oAccount1.Address, "test", 1);
}
Expand Down Expand Up @@ -161,9 +159,9 @@ public void TestMaxSizeLimit()
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n");
}

Assert.IsTrue(oSMTP.Send("[email protected]", "[email protected]", "SURBL-No-Match",
oSMTP.Send("[email protected]", "[email protected]", "SURBL-No-Match",
"This is a test message with a SURBL url: -> http://surbl-org-permanent-test-point.com/ <-\r\n" +
sb));
sb);

string sMessageContents = POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");
if (sMessageContents.Contains("X-hMailServer-Spam") ||
Expand Down Expand Up @@ -210,9 +208,9 @@ public void TestMaxSizeNoLimit()
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n");
}

Assert.IsTrue(oSMTP.Send("[email protected]", "[email protected]", "SURBL-No-Match",
oSMTP.Send("[email protected]", "[email protected]", "SURBL-No-Match",
"This is a test message with a SURBL url: -> http://surbl-org-permanent-test-point.com/ <-\r\n" +
sb));
sb);

string sMessageContents = POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");
if (!sMessageContents.Contains("X-hMailServer-Spam") ||
Expand Down Expand Up @@ -244,12 +242,11 @@ public void TestMissingMXRecord()
// Send a messages to this account.
var oSMTP = new SMTPClientSimulator();

if (!oSMTP.Send("[email protected]", "[email protected]", "INBOX", "This is a test message."))
throw new Exception("ERROR - Was not able to send correct email.");
oSMTP.Send("[email protected]", "[email protected]", "INBOX", "This is a test message.");

if (oSMTP.Send("test@domain_without_mx_records421dfsam430sasd.com", oAccount1.Address, "INBOX",
"This is a test message."))
throw new Exception("ERROR - Was not able to send incorrect email.");
CustomAsserts.Throws<DeliveryFailedException>(
() => oSMTP.Send("test@domain_without_mx_records421dfsam430sasd.com", oAccount1.Address, "INBOX",
"This is a test message."));

_antiSpam.UseMXChecks = false;

Expand Down Expand Up @@ -499,8 +496,8 @@ public void TestSURBLCorrectNegative()
oSURBLServer.Save();

// Send a messages to this account.
Assert.IsTrue(SMTPClientSimulator.StaticSend("[email protected]", "[email protected]", "SURBL-Match",
"This is a test message without a SURBL url: -> http://www.youtube.com/ <-"));
SMTPClientSimulator.StaticSend("[email protected]", "[email protected]", "SURBL-Match",
"This is a test message without a SURBL url: -> http://www.youtube.com/ <-");

string sMessageContents = POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");
if (sMessageContents.Contains("X-hMailServer-Spam"))
Expand Down
11 changes: 6 additions & 5 deletions hmailserver/test/RegressionTests/AntiSpam/Combinations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

using System;
using NUnit.Framework;
using RegressionTests.Infrastructure;
using RegressionTests.Shared;
Expand Down Expand Up @@ -54,8 +55,8 @@ public void TestDeleteThresholdLowerThanMarkThreshold()

// Should not be possible to send this email since it's results in a spam
// score over the delete threshold.
Assert.IsTrue(oSMTP.Send("[email protected]", oAccount1.Address, "INBOX",
"Test http://surbl-org-permanent-test-point.com/ Test 2"));
oSMTP.Send("[email protected]", oAccount1.Address, "INBOX",
"Test http://surbl-org-permanent-test-point.com/ Test 2");

string message = POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");

Expand Down Expand Up @@ -92,8 +93,8 @@ public void TestOneFailOnePass()

// Should not be possible to send this email since it's results in a spam
// score over the delete threshold.
Assert.IsTrue(oSMTP.Send("test@domain_without_mx_records421dfsam430sasd.com", oAccount1.Address, "INBOX",
"This is a test message."));
oSMTP.Send("test@domain_without_mx_records421dfsam430sasd.com", oAccount1.Address, "INBOX",
"This is a test message.");

string message = POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");

Expand Down Expand Up @@ -144,7 +145,7 @@ public void TestSpamMultipleHits()

// Should not be possible to send this email since it's results in a spam
// score over the delete threshold.
Assert.IsFalse(oSMTP.Send("test@domain_without_mx_records421dfsam430sasd.com", oAccount1.Address, "INBOX",
CustomAsserts.Throws<DeliveryFailedException>(() => oSMTP.Send("test@domain_without_mx_records421dfsam430sasd.com", oAccount1.Address, "INBOX",
"This is a test message. It contains incorrect MX records and a SURBL string: http://surbl-org-permanent-test-point.com/ SpamAssassinString: XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"));

liveLog = _settings.Logging.LiveLog;
Expand Down
2 changes: 1 addition & 1 deletion hmailserver/test/RegressionTests/AntiSpam/DKIM/Signing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private string SendMessage(string body)
var smtp = new SMTPClientSimulator();
var recipients = new List<string>();
recipients.Add("[email protected]");
Assert.IsTrue(smtp.Send("[email protected]", recipients, "Test", body));
smtp.Send("[email protected]", recipients, "Test", body);

// Wait for the client to disconnect.
server.WaitForCompletion();
Expand Down
12 changes: 7 additions & 5 deletions hmailserver/test/RegressionTests/AntiSpam/DKIM/Verification.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

using System;
using NUnit.Framework;
using RegressionTests.Infrastructure;
using RegressionTests.Shared;
using hMailServer;

Expand Down Expand Up @@ -59,7 +61,7 @@ public void TestInvalidBodyHash()
"" + "\r\n";

Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");
Assert.IsFalse(SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
CustomAsserts.Throws<DeliveryFailedException>(() => SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
}

[Test]
Expand Down Expand Up @@ -99,7 +101,7 @@ public void TestInvalidBodyHashMark()
"" + "\r\n";

Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");
Assert.IsTrue(SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText);
string text = POP3ClientSimulator.AssertGetFirstMessageText(account1.Address, "test");

Assert.IsTrue(text.Contains("Rejected by DKIM. - (Score: 6)"));
Expand Down Expand Up @@ -140,7 +142,7 @@ public void TestInvalidSignature()
"" + "\r\n";

Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");
Assert.IsFalse(SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
CustomAsserts.Throws<DeliveryFailedException>(() => SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
}

[Test]
Expand Down Expand Up @@ -178,7 +180,7 @@ public void TestValidSignatureSHA1()
"" + "\r\n";

Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");
Assert.IsTrue(SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText);
string text = POP3ClientSimulator.AssertGetFirstMessageText(account1.Address, "test");
}

Expand Down Expand Up @@ -217,7 +219,7 @@ public void TestValidSignatureSHA256()
"The quick brown fox jumped over the lazy dog." + "\r\n";

Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");
Assert.IsTrue(SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText));
SMTPClientSimulator.StaticSendRaw(account1.Address, account1.Address, messageText);
string text = POP3ClientSimulator.AssertGetFirstMessageText(account1.Address, "test");
}
}
Expand Down
45 changes: 23 additions & 22 deletions hmailserver/test/RegressionTests/AntiSpam/GreyListing.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

using System;
using System.Collections.Generic;
using NUnit.Framework;
using RegressionTests.Infrastructure;
using RegressionTests.Shared;
using hMailServer;

Expand Down Expand Up @@ -31,13 +33,14 @@ public void ItShouldBePossibleToBypassGreylistingOnMessagesArrivingFromMXorA()

_antiSpam.GreyListingEnabled = true;

Assert.IsFalse(SMTPClientSimulator.StaticSend("[email protected]", oAccount1.Address, "Test",
"Body"));
CustomAsserts.Throws<DeliveryFailedException>(
() => SMTPClientSimulator.StaticSend("[email protected]", oAccount1.Address, "Test",
"Body"));

_antiSpam.BypassGreylistingOnMailFromMX = true;

Assert.IsTrue(SMTPClientSimulator.StaticSend("[email protected]", oAccount1.Address, "Test",
"Body"));
SMTPClientSimulator.StaticSend("[email protected]", oAccount1.Address, "Test",
"Body");

POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");
}
Expand All @@ -52,19 +55,19 @@ public void TestGreyListing()
var smtp = new SMTPClientSimulator();
var recipients = new List<string>();
recipients.Add(oAccount1.Address);
bool result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsTrue(result);
smtp.Send("[email protected]", recipients, "Test", "Body");

POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");

_antiSpam.GreyListingEnabled = true;

result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsFalse(result);
CustomAsserts.Throws<DeliveryFailedException>(() => smtp.Send("[email protected]", recipients, "Test", "Body"));


_antiSpam.GreyListingEnabled = false;

result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsTrue(result);
smtp.Send("[email protected]", recipients, "Test", "Body");

POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");
}

Expand All @@ -80,12 +83,12 @@ public void TestGreyListingWhiteListWildcard()
whiteAddress.IPAddress = "127.0.0.5";
whiteAddress.Save();

Assert.IsFalse(SMTPClientSimulator.StaticSend("[email protected]", account.Address, "Test", "Test"));
CustomAsserts.Throws<DeliveryFailedException>(() => SMTPClientSimulator.StaticSend("[email protected]", account.Address, "Test", "Test"));

whiteAddress.IPAddress = "*";
whiteAddress.Save();

Assert.IsTrue(SMTPClientSimulator.StaticSend("[email protected]", account.Address, "Test", "Test"));
SMTPClientSimulator.StaticSend("[email protected]", account.Address, "Test", "Test");

POP3ClientSimulator.AssertGetFirstMessageText(account.Address, "test");
}
Expand All @@ -100,20 +103,20 @@ public void TestGreyListingWithDomainAliases()
var smtp = new SMTPClientSimulator();
var recipients = new List<string>();
recipients.Add(oAccount1.Address);
bool result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsTrue(result);
smtp.Send("[email protected]", recipients, "Test", "Body");

POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");

_antiSpam.GreyListingEnabled = true;

result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsFalse(result);
CustomAsserts.Throws<DeliveryFailedException>(() => smtp.Send("[email protected]", recipients, "Test", "Body"));


_domain.AntiSpamEnableGreylisting = false;
_domain.Save();

result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsTrue(result);
smtp.Send("[email protected]", recipients, "Test", "Body");

POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");

DomainAlias da = _domain.DomainAliases.Add();
Expand All @@ -123,15 +126,13 @@ public void TestGreyListingWithDomainAliases()
recipients = new List<string>();
recipients.Add("[email protected]");

result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsTrue(result);
smtp.Send("[email protected]", recipients, "Test", "Body");
POP3ClientSimulator.AssertGetFirstMessageText(oAccount1.Address, "test");

_domain.AntiSpamEnableGreylisting = true;
_domain.Save();

result = smtp.Send("[email protected]", recipients, "Test", "Body");
Assert.IsFalse(result);
CustomAsserts.Throws<DeliveryFailedException>(() => smtp.Send("[email protected]", recipients, "Test", "Body"));
}
}
}
4 changes: 2 additions & 2 deletions hmailserver/test/RegressionTests/AntiSpam/SpamAssassin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void TestBasic()
// Send a messages to this account.
var oSMTP = new SMTPClientSimulator();

Assert.IsTrue(oSMTP.Send(account.Address, account.Address, "SA test", "This is a test message."));
oSMTP.Send(account.Address, account.Address, "SA test", "This is a test message.");
string sMessageContents = POP3ClientSimulator.AssertGetFirstMessageText(account.Address, "test");
if (!sMessageContents.Contains("X-Spam-Status"))
{
Expand Down Expand Up @@ -209,7 +209,7 @@ public void TestSANotRunning()
// Send a messages to this account.
var oSMTP = new SMTPClientSimulator();

Assert.IsTrue(oSMTP.Send(account.Address, account.Address, "SA test", "This is a test message."));
oSMTP.Send(account.Address, account.Address, "SA test", "This is a test message.");
string sMessageContents = POP3ClientSimulator.AssertGetFirstMessageText(account.Address, "test");

Assert.IsFalse(sMessageContents.Contains("X-Spam-Status"));
Expand Down
Loading

0 comments on commit e372130

Please sign in to comment.