Skip to content

Commit

Permalink
optimize validate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jan 14, 2025
1 parent 9fd3b95 commit d712b46
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public sealed class BasicTest
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Expand All @@ -16,6 +17,7 @@ public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Expand All @@ -25,6 +27,7 @@ public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
public void TryParse_CorruptDmarcString1_ReturnsTrueAndParseErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("verification=123456789", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Expand All @@ -35,6 +38,7 @@ public void TryParse_CorruptDmarcString1_ReturnsTrueAndParseErrors()
public void TryParse_CorruptDmarcString2_ReturnsFalse()
{
var isSuccessful = DmarcRecordParser.TryParse(" ", out var dmarcDataFragment, out var parseErrors);

Assert.IsFalse(isSuccessful);
Assert.IsNull(dmarcDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public sealed class ComplexTest
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=100; adkim=s; aspf=s", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,38 @@ public sealed class FailureReportingOptionsTest
[DataRow("1")]
[DataRow("d")]
[DataRow("s")]
[DataRow("s:d")]
[DataRow("s:d:1")]
[DataTestMethod]
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord(string failureReportingOptions)
{
var isSuccessful = DmarcRecordParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual(failureReportingOptions, dmarcDataFragment.FailureReportingOptions);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}

[DataRow("a")]
[DataRow("b")]
[DataRow("8")]
[DataRow("9")]
[DataRow("a", 2)]
[DataRow("b", 2)]
[DataRow("8", 2)]
[DataRow("9", 2)]
[DataRow("wrong", 2)]
[DataRow("s:x:dd", 3)]
[DataTestMethod]
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors(string failureReportingOptions)
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors(string failureReportingOptions, int parseErrorCount)
{
var isSuccessful = DmarcRecordParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual(failureReportingOptions, dmarcDataFragment.FailureReportingOptions);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
Assert.IsTrue(parseErrors.Length == parseErrorCount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public sealed class PolicyPercentageTest
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; pct=60;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Expand All @@ -18,22 +19,26 @@ public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
public void TryParse_InvalidDmarcString1_ReturnsTrueWithErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; pct=;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("", dmarcDataFragment.PolicyPercentage);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}

[TestMethod]
public void TryParse_InvalidDmarcString2_ReturnsTrueWithErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; pct=200;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("200", dmarcDataFragment.PolicyPercentage);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public sealed class PolicyTest
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=Test", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("Test", dmarcDataFragment.DomainPolicy);
Expand All @@ -18,6 +19,7 @@ public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=Test;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("Test", dmarcDataFragment.DomainPolicy);
Expand All @@ -29,6 +31,7 @@ public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ public sealed class ReportFormatTest
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; rf=afrf", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("afrf", dmarcDataFragment.ReportFormat);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}

[TestMethod]
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; rf=afrf1", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Expand All @@ -30,6 +33,7 @@ public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithP
public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; rf=", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace Nager.EmailAuthentication.UnitTest.DmarcRecordParserTests
{
[TestClass]
public sealed class ReportingIntervalTest
{
[TestMethod]
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; ri=86400;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("86400", dmarcDataFragment.ReportingInterval);
Assert.IsNull(parseErrors, "ParseErrors is not null");
}

[TestMethod]
public void TryParse_InvalidDmarcString1_ReturnsTrueWithErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; ri=1000;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("1000", dmarcDataFragment.ReportingInterval);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}

[TestMethod]
public void TryParse_InvalidDmarcString2_ReturnsTrueWithErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; ri=1000000;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("1000000", dmarcDataFragment.ReportingInterval);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}

[TestMethod]
public void TryParse_InvalidDmarcString3_ReturnsTrueWithErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; ri=-1000000;", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("-1000000", dmarcDataFragment.ReportingInterval);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
}
}
}
Loading

0 comments on commit d712b46

Please sign in to comment.