Skip to content

Commit

Permalink
optimize dkim validator, optimize object naming
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jan 22, 2025
1 parent 98de5ef commit c0276a2
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public void TryParse_ValidDkimHeaderString1_ReturnsTrueAndPopulatesDataFragment(
{
var dkimHeader = "v=1; a=rsa-sha256; c=relaxed/simple; q=dns/txt; d=domain.com; [email protected]; s=mailjet; x=1737017824; h=message-id:from:from:reply-to:to:to:subject:subject:date:date:list-unsubscribe-post:list-unsubscribe:feedback-id:x-csa-complaints:x-mj-mid:x-report-abuse-to:mime-version:content-type; bh=TyN/x6t3AOfI298rgJAgZHgdWcq/XLISGen5nN3NLAc=; b=HLCLiikV92Ku/k9mGlZM0bmqPjKggGnMI0igqhXmPRzPJUC+5SUWRS6/FLUpxbX6AUGJRDYQnKKMtp6uZkYVuKG8SPZ01cUkvIiiAkczb4bK6IVvPbZOnsWqHkD6EvK3TrpIhgFfGLlcG+zIwgdDZ3O++uhpJkIX1WJlkXZYqxQ=";

var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parseErrors);
var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dkimHeaderDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
//Assert.IsNull(parsingResults, "ParsingResults is not null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public void TryParse_ValidSelector_ReturnsTrueAndPopulatesDataFragment(string se
{
var dkimHeader = $"v=1; a=rsa-sha256; d=domain.com; s={selector}; h=message-id:from; bh=testbodyhash=; b=signaturedata";

var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parseErrors);
var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dkimHeaderDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}

[DataRow("verylongandinvalidselectorverylongandinvalidselectorverylongandinvalidselector")]
Expand All @@ -24,11 +24,11 @@ public void TryParse_InvalidSelector_ReturnsTrueAndPopulatesDataFragment(string
{
var dkimHeader = $"v=1; a=rsa-sha256; d=domain.com; s={selector}; h=message-id:from; bh=testbodyhash=; b=signaturedata";

var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parseErrors);
var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dkimHeaderDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsNotNull(parsingResults, "ParsingResults is null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public void TryParse_ValidVersion_ReturnsTrueAndPopulatesDataFragment(string ver
{
var dkimHeader = $"v={version}; a=rsa-sha256; d=domain.com; s=myselector; h=message-id:from; bh=testbodyhash=; b=signaturedata";

var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parseErrors);
var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dkimHeaderDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}

[DataRow("a")]
Expand All @@ -25,11 +25,11 @@ public void TryParse_InvalidVersion_ReturnsTrueAndPopulatesDataFragment(string v
{
var dkimHeader = $"v={version}; a=rsa-sha256; d=domain.com; s=myselector; h=message-id:from; bh=testbodyhash=; b=signaturedata";

var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parseErrors);
var isSuccessful = DkimHeaderParser.TryParse(dkimHeader, out var dkimHeaderDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dkimHeaderDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsNotNull(parsingResults, "ParsingResults is null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,42 @@ public sealed class BasicTest
[TestMethod]
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC", out var dmarcDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsNotNull(parsingResults, "ParsingResults is null");
}

[TestMethod]
public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1", out var dmarcDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}

[TestMethod]
public void TryParse_CorruptDmarcString1_ReturnsFalseAndParseErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("verification=123456789", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("verification=123456789", out var dmarcDataFragment, out var parsingResults);

Assert.IsFalse(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 1);
Assert.IsNotNull(parsingResults, "ParsingResults is null");
Assert.IsTrue(parsingResults.Length == 1);
}

[TestMethod]
public void TryParse_CorruptDmarcString2_ReturnsFalse()
{
var isSuccessful = DmarcRecordParser.TryParse(" ", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse(" ", out var dmarcDataFragment, out var parsingResults);

Assert.IsFalse(isSuccessful);
Assert.IsNull(dmarcDataFragment);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed class ComplexTest
[TestMethod]
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);
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 parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Expand All @@ -15,7 +15,7 @@ public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
Assert.AreEqual("100", dmarcDataFragment.PolicyPercentage);
Assert.AreEqual("s", dmarcDataFragment.DkimAlignmentMode);
Assert.AreEqual("s", dmarcDataFragment.SpfAlignmentMode);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public sealed class FailureReportingOptionsTest
[DataTestMethod]
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord(string failureReportingOptions)
{
var isSuccessful = DmarcRecordParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parsingResults);

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.IsNotNull(parsingResults, "ParsingResults is null");
Assert.IsTrue(parsingResults.Length == 1);
}

[DataRow("a", 2)]
Expand All @@ -29,16 +29,16 @@ public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord(string
[DataRow("wrong", 2)]
[DataRow("s:x:dd", 3)]
[DataTestMethod]
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors(string failureReportingOptions, int parseErrorCount)
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors(string failureReportingOptions, int parsingResultsCount)
{
var isSuccessful = DmarcRecordParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parsingResults);

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 == parseErrorCount);
Assert.IsNotNull(parsingResults, "ParsingResults is null");
Assert.IsTrue(parsingResults.Length == parsingResultsCount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ public sealed class PolicyPercentageTest
[TestMethod]
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; pct=60;", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; pct=60;", out var dmarcDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("60", dmarcDataFragment.PolicyPercentage);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}

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

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);
Assert.IsNotNull(parsingResults, "ParsingResults is null");
Assert.IsTrue(parsingResults.Length == 1);
}

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

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);
Assert.IsNotNull(parsingResults, "ParsingResults is null");
Assert.IsTrue(parsingResults.Length == 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ public sealed class PolicyTest
[TestMethod]
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=Test", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=Test", out var dmarcDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("Test", dmarcDataFragment.DomainPolicy);
Assert.IsNotNull(parseErrors);
Assert.IsTrue(parseErrors.Length == 1);
Assert.IsNotNull(parsingResults);
Assert.IsTrue(parsingResults.Length == 1);
}

[TestMethod]
public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=Test;", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=Test;", out var dmarcDataFragment, out var parsingResults);

Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("Test", dmarcDataFragment.DomainPolicy);
Assert.IsNotNull(parseErrors);
Assert.IsTrue(parseErrors.Length == 1);
Assert.IsNotNull(parsingResults);
Assert.IsTrue(parsingResults.Length == 1);
}

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

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

[TestMethod]
public void TryParse_ValidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; sp=none;", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; sp=none;", out var dmarcDataFragment, out var parsingResults);
Assert.IsTrue(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
Assert.AreEqual("none", dmarcDataFragment.SubdomainPolicy);
Assert.IsNull(parseErrors, "ParseErrors is not null");
Assert.IsNull(parsingResults, "ParsingResults is not null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ public sealed class ReportFormatTest
[TestMethod]
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
{
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; rf=afrf", out var dmarcDataFragment, out var parseErrors);
var isSuccessful = DmarcRecordParser.TryParse("v=DMARC1; p=reject; rf=afrf", out var dmarcDataFragment, out var parsingResults);

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

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

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

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

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

0 comments on commit c0276a2

Please sign in to comment.