-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more unit tests for *MimeParser and added inline comments
Refactored MimeReader StepHeaders/Async a bit just to make it easier to understand what is happening in some "invalid header" cases.
- Loading branch information
Showing
4 changed files
with
266 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1406,7 +1406,7 @@ This is the message body. | |
} | ||
|
||
[Test] | ||
public async Task TestMultipartSubpartHeadersLineStartsWithDashDashyAsync () | ||
public async Task TestMultipartSubpartHeadersLineStartsWithDashDashAsync () | ||
{ | ||
string text = @"From: [email protected] | ||
To: [email protected] | ||
|
@@ -1467,6 +1467,120 @@ This is the message body. | |
} | ||
} | ||
|
||
[Test] | ||
public void TestMultipartSubpartHeadersLineStartsWithDashDashEOF () | ||
{ | ||
string text = @"From: [email protected] | ||
To: [email protected] | ||
Subject: test of multipart subpart headers ending with a boundary | ||
Date: Tue, 12 Nov 2013 09:12:42 -0500 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
X-Mailer: Microsoft Office Outlook 12.0 | ||
Content-Type: multipart/mixed; | ||
boundary=""----=_NextPart_000_003F_01CE98CE.6E826F90"" | ||
------=_NextPart_000_003F_01CE98CE.6E826F90 | ||
Content-Type: text/plain; charset=utf-8 | ||
--not-the-boundary-muhahaha".Replace ("\r\n", "\n"); | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) { | ||
var parser = new ExperimentalMimeParser (stream, MimeFormat.Entity); | ||
var message = parser.ParseMessage (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text.Replace ("\n", "\r\n")), false)) { | ||
var parser = new ExperimentalMimeParser (stream, MimeFormat.Entity); | ||
var message = parser.ParseMessage (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task TestMultipartSubpartHeadersLineStartsWithDashDashEOFAsync () | ||
{ | ||
string text = @"From: [email protected] | ||
To: [email protected] | ||
Subject: test of multipart subpart headers ending with a boundary | ||
Date: Tue, 12 Nov 2013 09:12:42 -0500 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
X-Mailer: Microsoft Office Outlook 12.0 | ||
Content-Type: multipart/mixed; | ||
boundary=""----=_NextPart_000_003F_01CE98CE.6E826F90"" | ||
------=_NextPart_000_003F_01CE98CE.6E826F90 | ||
Content-Type: text/plain; charset=utf-8 | ||
--not-the-boundary-muhahaha".Replace ("\r\n", "\n"); | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) { | ||
var parser = new ExperimentalMimeParser (stream, MimeFormat.Entity); | ||
var message = await parser.ParseMessageAsync (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text.Replace ("\n", "\r\n")), false)) { | ||
var parser = new ExperimentalMimeParser (stream, MimeFormat.Entity); | ||
var message = await parser.ParseMessageAsync (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestMultipartBoundaryLineWithTrailingSpacesAndThenMoreCharacters () | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1398,7 +1398,7 @@ This is the message body. | |
} | ||
|
||
[Test] | ||
public async Task TestMultipartSubpartHeadersLineStartsWithDashDashyAsync () | ||
public async Task TestMultipartSubpartHeadersLineStartsWithDashDashAsync () | ||
{ | ||
string text = @"From: [email protected] | ||
To: [email protected] | ||
|
@@ -1459,6 +1459,120 @@ This is the message body. | |
} | ||
} | ||
|
||
[Test] | ||
public void TestMultipartSubpartHeadersLineStartsWithDashDashEOF () | ||
{ | ||
string text = @"From: [email protected] | ||
To: [email protected] | ||
Subject: test of multipart subpart headers ending with a boundary | ||
Date: Tue, 12 Nov 2013 09:12:42 -0500 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
X-Mailer: Microsoft Office Outlook 12.0 | ||
Content-Type: multipart/mixed; | ||
boundary=""----=_NextPart_000_003F_01CE98CE.6E826F90"" | ||
------=_NextPart_000_003F_01CE98CE.6E826F90 | ||
Content-Type: text/plain; charset=utf-8 | ||
--not-the-boundary-muhahaha".Replace ("\r\n", "\n"); | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) { | ||
var parser = new MimeParser (stream, MimeFormat.Entity); | ||
var message = parser.ParseMessage (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text.Replace ("\n", "\r\n")), false)) { | ||
var parser = new MimeParser (stream, MimeFormat.Entity); | ||
var message = parser.ParseMessage (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task TestMultipartSubpartHeadersLineStartsWithDashDashEOFAsync () | ||
{ | ||
string text = @"From: [email protected] | ||
To: [email protected] | ||
Subject: test of multipart subpart headers ending with a boundary | ||
Date: Tue, 12 Nov 2013 09:12:42 -0500 | ||
MIME-Version: 1.0 | ||
Message-ID: <[email protected]> | ||
X-Mailer: Microsoft Office Outlook 12.0 | ||
Content-Type: multipart/mixed; | ||
boundary=""----=_NextPart_000_003F_01CE98CE.6E826F90"" | ||
------=_NextPart_000_003F_01CE98CE.6E826F90 | ||
Content-Type: text/plain; charset=utf-8 | ||
--not-the-boundary-muhahaha".Replace ("\r\n", "\n"); | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text), false)) { | ||
var parser = new MimeParser (stream, MimeFormat.Entity); | ||
var message = await parser.ParseMessageAsync (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
|
||
using (var stream = new MemoryStream (Encoding.ASCII.GetBytes (text.Replace ("\n", "\r\n")), false)) { | ||
var parser = new MimeParser (stream, MimeFormat.Entity); | ||
var message = await parser.ParseMessageAsync (); | ||
|
||
Assert.That (message.Body, Is.InstanceOf<Multipart> (), "Expected top-level to be a multipart"); | ||
var multipart = (Multipart) message.Body; | ||
Assert.That (multipart.Count, Is.EqualTo (1), "Expected 1 child"); | ||
Assert.That (multipart[0], Is.InstanceOf<TextPart> (), "Expected first child of the multipart to be text/plain"); | ||
var body = (TextPart) multipart[0]; | ||
|
||
Assert.That (body.Headers[HeaderId.ContentType], Is.EqualTo ("text/plain; charset=utf-8")); | ||
Assert.That (body.ContentType.Charset, Is.EqualTo ("utf-8")); | ||
Assert.That (body.Headers.Count, Is.EqualTo (2)); | ||
Assert.That (body.Headers[1].IsInvalid, Is.True, "IsInvalid"); | ||
Assert.That (body.Headers[1].Field, Is.EqualTo ("--not-the-boundary-muhahaha")); | ||
|
||
Assert.That (body.Text, Is.EqualTo (string.Empty)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestMultipartBoundaryLineWithTrailingSpacesAndThenMoreCharacters () | ||
{ | ||
|