Skip to content

Commit 492fe2a

Browse files
committed
Future-proof (wip)
1 parent 8dcef95 commit 492fe2a

22 files changed

+456
-194
lines changed

src/Nager.EmailAuthentication.UnitTest/DmarcRecordParserTests/BasicTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void TryParse_CorruptDmarcString1_ReturnsFalseAndParseErrors()
2929
var isSuccessful = DmarcRecordDataFragmentParser.TryParse("verification=123456789", out var dmarcDataFragment, out var parsingResults);
3030

3131
Assert.IsFalse(isSuccessful);
32-
Assert.IsNotNull(dmarcDataFragment);
32+
Assert.IsNull(dmarcDataFragment);
3333
Assert.IsNotNull(parsingResults, "ParsingResults is null");
3434
Assert.IsTrue(parsingResults.Length == 1);
3535
}

src/Nager.EmailAuthentication.UnitTest/DmarcRecordParserTests/ComplexTest.cs

+69-38
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,55 @@ public sealed class ComplexTest
88
[TestMethod]
99
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
1010
{
11-
var dmarcRecord = "v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=100; adkim=s; aspf=s";
11+
var recordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=100; adkim=s; aspf=s";
1212

13-
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(dmarcRecord, out var dmarcDataFragment, out var parsingResults);
13+
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
1414

1515
Assert.IsTrue(isSuccessful);
16-
Assert.IsNotNull(dmarcDataFragment);
17-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
18-
Assert.AreEqual("mailto:[email protected], mailto:[email protected]", dmarcDataFragment.AggregateReportUri);
19-
Assert.AreEqual("100", dmarcDataFragment.PolicyPercentage);
20-
Assert.AreEqual("s", dmarcDataFragment.DkimAlignmentMode);
21-
Assert.AreEqual("s", dmarcDataFragment.SpfAlignmentMode);
16+
Assert.IsNotNull(dataFragment);
2217
Assert.IsNull(parsingResults, "ParsingResults is not null");
18+
19+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
20+
{
21+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
22+
return;
23+
}
24+
25+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
26+
Assert.AreEqual("mailto:[email protected], mailto:[email protected]", dataFragmentV1.AggregateReportUri);
27+
Assert.AreEqual("100", dataFragmentV1.PolicyPercentage);
28+
Assert.AreEqual("s", dataFragmentV1.DkimAlignmentMode);
29+
Assert.AreEqual("s", dataFragmentV1.SpfAlignmentMode);
2330
}
2431

2532
[TestMethod]
2633
public void TryParse_ValidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
2734
{
28-
var dmarcRecordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected]!10m, mailto:[email protected]; pct=100; adkim=s; aspf=s";
35+
var recordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected]!10m, mailto:[email protected]; pct=100; adkim=s; aspf=s";
2936

30-
var isDataFragmentParserSuccessful = DmarcRecordDataFragmentParser.TryParse(dmarcRecordRaw, out var dmarcDataFragment, out var parsingResults);
31-
Assert.IsNotNull(dmarcDataFragment);
37+
var isDataFragmentParserSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
3238

3339
Assert.IsTrue(isDataFragmentParserSuccessful);
34-
Assert.IsNotNull(dmarcDataFragment);
35-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
36-
Assert.AreEqual("mailto:[email protected]!10m, mailto:[email protected]", dmarcDataFragment.AggregateReportUri);
37-
Assert.AreEqual("100", dmarcDataFragment.PolicyPercentage);
38-
Assert.AreEqual("s", dmarcDataFragment.DkimAlignmentMode);
39-
Assert.AreEqual("s", dmarcDataFragment.SpfAlignmentMode);
40+
Assert.IsNotNull(dataFragment);
4041
Assert.IsNull(parsingResults, "ParsingResults is not null");
4142

42-
var isParserSuccessful = DmarcRecordParser.TryParse(dmarcDataFragment, out var dmarcRecord);
43+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
44+
{
45+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
46+
return;
47+
}
48+
49+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
50+
Assert.AreEqual("mailto:[email protected]!10m, mailto:[email protected]", dataFragmentV1.AggregateReportUri);
51+
Assert.AreEqual("100", dataFragmentV1.PolicyPercentage);
52+
Assert.AreEqual("s", dataFragmentV1.DkimAlignmentMode);
53+
Assert.AreEqual("s", dataFragmentV1.SpfAlignmentMode);
54+
55+
var isParserSuccessful = DmarcRecordParser.TryParseV1(dataFragmentV1, out var dmarcRecord);
56+
4357
Assert.IsTrue(isParserSuccessful);
4458
Assert.IsNotNull(dmarcRecord);
59+
4560
Assert.AreEqual(DmarcPolicy.Reject, dmarcRecord.DomainPolicy);
4661
Assert.AreEqual(DmarcPolicy.Reject, dmarcRecord.SubdomainPolicy);
4762
Assert.AreEqual(AlignmentMode.Strict, dmarcRecord.DkimAlignmentMode);
@@ -52,23 +67,32 @@ public void TryParse_ValidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
5267
[TestMethod]
5368
public void TryParse_ValidDmarcString3_ReturnsTrueAndPopulatesDmarcRecord()
5469
{
55-
var dmarcRecordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=50; adkim=r; aspf=r";
70+
var recordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=50; adkim=r; aspf=r";
5671

57-
var isDataFragmentParserSuccessful = DmarcRecordDataFragmentParser.TryParse(dmarcRecordRaw, out var dmarcDataFragment, out var parsingResults);
58-
Assert.IsNotNull(dmarcDataFragment);
72+
var isDataFragmentParserSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
5973

6074
Assert.IsTrue(isDataFragmentParserSuccessful);
61-
Assert.IsNotNull(dmarcDataFragment);
62-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
63-
Assert.AreEqual("mailto:[email protected], mailto:[email protected]", dmarcDataFragment.AggregateReportUri);
64-
Assert.AreEqual("50", dmarcDataFragment.PolicyPercentage);
65-
Assert.AreEqual("r", dmarcDataFragment.DkimAlignmentMode);
66-
Assert.AreEqual("r", dmarcDataFragment.SpfAlignmentMode);
75+
Assert.IsNotNull(dataFragment);
6776
Assert.IsNull(parsingResults, "ParsingResults is not null");
6877

69-
var isParserSuccessful = DmarcRecordParser.TryParse(dmarcDataFragment, out var dmarcRecord);
78+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
79+
{
80+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
81+
return;
82+
}
83+
84+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
85+
Assert.AreEqual("mailto:[email protected], mailto:[email protected]", dataFragmentV1.AggregateReportUri);
86+
Assert.AreEqual("50", dataFragmentV1.PolicyPercentage);
87+
Assert.AreEqual("r", dataFragmentV1.DkimAlignmentMode);
88+
Assert.AreEqual("r", dataFragmentV1.SpfAlignmentMode);
89+
90+
91+
var isParserSuccessful = DmarcRecordParser.TryParseV1(dataFragmentV1, out var dmarcRecord);
92+
7093
Assert.IsTrue(isParserSuccessful);
7194
Assert.IsNotNull(dmarcRecord);
95+
7296
Assert.AreEqual(DmarcPolicy.Reject, dmarcRecord.DomainPolicy);
7397
Assert.AreEqual(DmarcPolicy.Reject, dmarcRecord.SubdomainPolicy);
7498
Assert.AreEqual(AlignmentMode.Relaxed, dmarcRecord.DkimAlignmentMode);
@@ -79,21 +103,28 @@ public void TryParse_ValidDmarcString3_ReturnsTrueAndPopulatesDmarcRecord()
79103
[TestMethod]
80104
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
81105
{
82-
var dmarcRecordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=50; adkim=t; aspf=t";
106+
var recordRaw = "v=DMARC1; p=reject; rua=mailto:[email protected], mailto:[email protected]; pct=50; adkim=t; aspf=t";
83107

84-
var isDataFragmentParserSuccessful = DmarcRecordDataFragmentParser.TryParse(dmarcRecordRaw, out var dmarcDataFragment, out var parsingResults);
85-
Assert.IsNotNull(dmarcDataFragment);
108+
var isDataFragmentParserSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
86109

87110
Assert.IsTrue(isDataFragmentParserSuccessful);
88-
Assert.IsNotNull(dmarcDataFragment);
89-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
90-
Assert.AreEqual("mailto:[email protected], mailto:[email protected]", dmarcDataFragment.AggregateReportUri);
91-
Assert.AreEqual("50", dmarcDataFragment.PolicyPercentage);
92-
Assert.AreEqual("t", dmarcDataFragment.DkimAlignmentMode);
93-
Assert.AreEqual("t", dmarcDataFragment.SpfAlignmentMode);
111+
Assert.IsNotNull(dataFragment);
94112
Assert.IsNotNull(parsingResults, "ParsingResults is null");
95113

96-
var isParserSuccessful = DmarcRecordParser.TryParse(dmarcDataFragment, out var dmarcRecord);
114+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
115+
{
116+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
117+
return;
118+
}
119+
120+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
121+
Assert.AreEqual("mailto:[email protected], mailto:[email protected]", dataFragmentV1.AggregateReportUri);
122+
Assert.AreEqual("50", dataFragmentV1.PolicyPercentage);
123+
Assert.AreEqual("t", dataFragmentV1.DkimAlignmentMode);
124+
Assert.AreEqual("t", dataFragmentV1.SpfAlignmentMode);
125+
126+
var isParserSuccessful = DmarcRecordParser.TryParseV1(dataFragmentV1, out var dmarcRecord);
127+
97128
Assert.IsFalse(isParserSuccessful);
98129
Assert.IsNull(dmarcRecord);
99130
}

src/Nager.EmailAuthentication.UnitTest/DmarcRecordParserTests/FailureReportingOptionsTest.cs

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Nager.EmailAuthentication.UnitTest.DmarcRecordParserTests
1+
using Nager.EmailAuthentication.Models;
2+
3+
namespace Nager.EmailAuthentication.UnitTest.DmarcRecordParserTests
24
{
35
[TestClass]
46
public sealed class FailureReportingOptionsTest
@@ -12,14 +14,23 @@ public sealed class FailureReportingOptionsTest
1214
[DataTestMethod]
1315
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord(string failureReportingOptions)
1416
{
15-
var isSuccessful = DmarcRecordDataFragmentParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parsingResults);
17+
var recordRaw = $"v=DMARC1; p=reject; fo={failureReportingOptions}";
18+
19+
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
1620

1721
Assert.IsTrue(isSuccessful);
18-
Assert.IsNotNull(dmarcDataFragment);
19-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
20-
Assert.AreEqual(failureReportingOptions, dmarcDataFragment.FailureReportingOptions);
22+
Assert.IsNotNull(dataFragment);
2123
Assert.IsNotNull(parsingResults, "ParsingResults is null");
2224
Assert.IsTrue(parsingResults.Length == 1);
25+
26+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
27+
{
28+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
29+
return;
30+
}
31+
32+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
33+
Assert.AreEqual(failureReportingOptions, dataFragmentV1.FailureReportingOptions);
2334
}
2435

2536
[DataRow("a", 2)]
@@ -31,14 +42,23 @@ public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord(string
3142
[DataTestMethod]
3243
public void TryParse_InvalidDmarcString1_ReturnsTrueAndPopulatesDmarcRecordWithParseErrors(string failureReportingOptions, int parsingResultsCount)
3344
{
34-
var isSuccessful = DmarcRecordDataFragmentParser.TryParse($"v=DMARC1; p=reject; fo={failureReportingOptions}", out var dmarcDataFragment, out var parsingResults);
45+
var recordRaw = $"v=DMARC1; p=reject; fo={failureReportingOptions}";
46+
47+
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
3548

3649
Assert.IsTrue(isSuccessful);
37-
Assert.IsNotNull(dmarcDataFragment);
38-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
39-
Assert.AreEqual(failureReportingOptions, dmarcDataFragment.FailureReportingOptions);
50+
Assert.IsNotNull(dataFragment);
4051
Assert.IsNotNull(parsingResults, "ParsingResults is null");
4152
Assert.IsTrue(parsingResults.Length == parsingResultsCount);
53+
54+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
55+
{
56+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
57+
return;
58+
}
59+
60+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
61+
Assert.AreEqual(failureReportingOptions, dataFragmentV1.FailureReportingOptions);
4262
}
4363
}
4464
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,70 @@
1-
namespace Nager.EmailAuthentication.UnitTest.DmarcRecordParserTests
1+
using Nager.EmailAuthentication.Models;
2+
3+
namespace Nager.EmailAuthentication.UnitTest.DmarcRecordParserTests
24
{
35
[TestClass]
46
public sealed class PolicyPercentageTest
57
{
68
[TestMethod]
79
public void TryParse_ValidDmarcString1_ReturnsTrueAndPopulatesDmarcRecord()
810
{
9-
var isSuccessful = DmarcRecordDataFragmentParser.TryParse("v=DMARC1; p=reject; pct=60;", out var dmarcDataFragment, out var parsingResults);
11+
var recordRaw = "v=DMARC1; p=reject; pct=60;";
12+
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
1013

1114
Assert.IsTrue(isSuccessful);
12-
Assert.IsNotNull(dmarcDataFragment);
13-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
14-
Assert.AreEqual("60", dmarcDataFragment.PolicyPercentage);
15+
Assert.IsNotNull(dataFragment);
1516
Assert.IsNull(parsingResults, "ParsingResults is not null");
17+
18+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
19+
{
20+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
21+
return;
22+
}
23+
24+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
25+
Assert.AreEqual("60", dataFragmentV1.PolicyPercentage);
1626
}
1727

1828
[TestMethod]
1929
public void TryParse_InvalidDmarcString1_ReturnsTrueWithErrors()
2030
{
21-
var isSuccessful = DmarcRecordDataFragmentParser.TryParse("v=DMARC1; p=reject; pct=;", out var dmarcDataFragment, out var parsingResults);
31+
var recordRaw = "v=DMARC1; p=reject; pct=;";
32+
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
2233

2334
Assert.IsTrue(isSuccessful);
24-
Assert.IsNotNull(dmarcDataFragment);
25-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
26-
Assert.AreEqual("", dmarcDataFragment.PolicyPercentage);
35+
Assert.IsNotNull(dataFragment);
2736
Assert.IsNotNull(parsingResults, "ParsingResults is null");
2837
Assert.IsTrue(parsingResults.Length == 1);
38+
39+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
40+
{
41+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
42+
return;
43+
}
44+
45+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
46+
Assert.AreEqual("", dataFragmentV1.PolicyPercentage);
2947
}
3048

3149
[TestMethod]
3250
public void TryParse_InvalidDmarcString2_ReturnsTrueWithErrors()
3351
{
34-
var isSuccessful = DmarcRecordDataFragmentParser.TryParse("v=DMARC1; p=reject; pct=200;", out var dmarcDataFragment, out var parsingResults);
52+
var recordRaw = "v=DMARC1; p=reject; pct=200;";
53+
var isSuccessful = DmarcRecordDataFragmentParser.TryParse(recordRaw, out var dataFragment, out var parsingResults);
3554

3655
Assert.IsTrue(isSuccessful);
37-
Assert.IsNotNull(dmarcDataFragment);
38-
Assert.AreEqual("reject", dmarcDataFragment.DomainPolicy);
39-
Assert.AreEqual("200", dmarcDataFragment.PolicyPercentage);
56+
Assert.IsNotNull(dataFragment);
4057
Assert.IsNotNull(parsingResults, "ParsingResults is null");
4158
Assert.IsTrue(parsingResults.Length == 1);
59+
60+
if (dataFragment is not DmarcRecordDataFragmentV1 dataFragmentV1)
61+
{
62+
Assert.Fail("Wrong DmarcRecordDataFragmentV1 class");
63+
return;
64+
}
65+
66+
Assert.AreEqual("reject", dataFragmentV1.DomainPolicy);
67+
Assert.AreEqual("200", dataFragmentV1.PolicyPercentage);
4268
}
4369
}
4470
}

0 commit comments

Comments
 (0)