private void CheckTnefComplianceStatus() { TnefComplianceStatus complianceStatus = this.reader.ComplianceStatus; if ((complianceStatus & ~(TnefComplianceStatus.InvalidAttributeChecksum | TnefComplianceStatus.InvalidMessageCodepage | TnefComplianceStatus.InvalidDate)) != TnefComplianceStatus.Compliant) { throw new ConversionFailedException(ConversionFailureReason.CorruptContent, ServerStrings.ConversionCorruptTnef((int)complianceStatus), null); } }
static MimeMessage ParseTnefMessage(string path, TnefComplianceStatus expected) { using (var reader = new TnefReader(File.OpenRead(path), 0, TnefComplianceMode.Loose)) { var message = ExtractTnefMessage(reader); Assert.AreEqual(expected, reader.ComplianceStatus, "Unexpected compliance status."); return(message); } }
internal void SetComplianceError(TnefComplianceStatus error, Exception innerException = null) { ComplianceStatus |= error; if (ComplianceMode != TnefComplianceMode.Strict) { return; } string message = null; switch (error) { case TnefComplianceStatus.AttributeOverflow: message = "Too many attributes."; break; case TnefComplianceStatus.InvalidAttribute: message = "Invalid attribute."; break; case TnefComplianceStatus.InvalidAttributeChecksum: message = "Invalid attribute checksum."; break; case TnefComplianceStatus.InvalidAttributeLength: message = "Invalid attribute length."; break; case TnefComplianceStatus.InvalidAttributeLevel: message = "Invalid attribute level."; break; case TnefComplianceStatus.InvalidAttributeValue: message = "Invalid attribute value."; break; case TnefComplianceStatus.InvalidDate: message = "Invalid date."; break; case TnefComplianceStatus.InvalidMessageClass: message = "Invalid message class."; break; case TnefComplianceStatus.InvalidMessageCodepage: message = "Invalid message codepage."; break; case TnefComplianceStatus.InvalidPropertyLength: message = "Invalid property length."; break; case TnefComplianceStatus.InvalidRowCount: message = "Invalid row count."; break; case TnefComplianceStatus.InvalidTnefSignature: message = "Invalid TNEF signature."; break; case TnefComplianceStatus.InvalidTnefVersion: message = "Invalid TNEF version."; break; case TnefComplianceStatus.NestingTooDeep: message = "Nesting too deep."; break; case TnefComplianceStatus.StreamTruncated: message = "Truncated TNEF stream."; break; case TnefComplianceStatus.UnsupportedPropertyType: message = "Unsupported property type."; break; case TnefComplianceStatus.Compliant: return; } if (innerException != null) { throw new TnefException(error, message, innerException); } throw new TnefException(error, message); }
static void TestTnefParser(string path, TnefComplianceStatus expected = TnefComplianceStatus.Compliant) { var message = ParseTnefMessage(path + ".tnef", expected); var names = File.ReadAllLines(path + ".list"); foreach (var name in names) { bool found = false; foreach (var part in message.BodyParts.OfType <MimePart> ()) { if (part.FileName == name) { found = true; break; } } if (!found) { Assert.Fail("Failed to locate attachment: {0}", name); } } // now use TnefPart to do the same thing using (var content = File.OpenRead(path + ".tnef")) { var tnef = new TnefPart { Content = new MimeContent(content) }; var attachments = tnef.ExtractAttachments().ToList(); foreach (var name in names) { bool found = false; foreach (var part in attachments.OfType <MimePart> ()) { if (part.FileName == name) { found = true; break; } } if (!found) { Assert.Fail("Failed to locate attachment in TnefPart: {0}", name); } } } }
static void TestTnefParser(string path, TnefComplianceStatus expected = TnefComplianceStatus.Compliant) { var message = ParseTnefMessage(path + ".tnef", expected); var names = File.ReadAllLines(path + ".list"); foreach (var name in names) { bool found = false; foreach (var part in message.BodyParts) { if (part.FileName == name) { found = true; break; } } if (!found) { Assert.Fail("Failed to locate attachment: {0}", name); } } }
public void TestGarbageAtEnd() { const TnefComplianceStatus errors = TnefComplianceStatus.InvalidAttributeLevel | TnefComplianceStatus.StreamTruncated; TestTnefParser("../../TestData/tnef/garbage-at-end", errors); }
/// <summary> /// Initializes a new instance of the <see cref="MimeKit.Tnef.TnefException"/> class. /// </summary> /// <remarks> /// Creates a new <see cref="TnefException"/>. /// </remarks> /// <param name="error">The compliance status error.</param> /// <param name="message">The error message.</param> public TnefException(TnefComplianceStatus error, string message) : base(message) { Error = error; }
/// <summary> /// Initializes a new instance of the <see cref="MimeKit.Tnef.TnefException"/> class. /// </summary> /// <remarks> /// Creates a new <see cref="TnefException"/>. /// </remarks> /// <param name="error">The compliance status error.</param> /// <param name="message">The error message.</param> /// <param name="innerException">The inner exception.</param> public TnefException(TnefComplianceStatus error, string message, Exception innerException) : base(message, innerException) { Error = error; }
internal void SetComplianceError (TnefComplianceStatus error, Exception innerException = null) { ComplianceStatus |= error; if (ComplianceMode != TnefComplianceMode.Strict) return; string message = null; switch (error) { case TnefComplianceStatus.AttributeOverflow: message = "Too many attributes."; break; case TnefComplianceStatus.InvalidAttribute: message = "Invalid attribute."; break; case TnefComplianceStatus.InvalidAttributeChecksum: message = "Invalid attribute checksum."; break; case TnefComplianceStatus.InvalidAttributeLength: message = "Invalid attribute length."; break; case TnefComplianceStatus.InvalidAttributeLevel: message = "Invalid attribute level."; break; case TnefComplianceStatus.InvalidAttributeValue: message = "Invalid attribute value."; break; case TnefComplianceStatus.InvalidDate: message = "Invalid date."; break; case TnefComplianceStatus.InvalidMessageClass: message = "Invalid message class."; break; case TnefComplianceStatus.InvalidMessageCodepage: message = "Invalid message codepage."; break; case TnefComplianceStatus.InvalidPropertyLength: message = "Invalid property length."; break; case TnefComplianceStatus.InvalidRowCount: message = "Invalid row count."; break; case TnefComplianceStatus.InvalidTnefSignature: message = "Invalid TNEF signature."; break; case TnefComplianceStatus.InvalidTnefVersion: message = "Invalid TNEF version."; break; case TnefComplianceStatus.NestingTooDeep: message = "Nesting too deep."; break; case TnefComplianceStatus.StreamTruncated: message = "Truncated TNEF stream."; break; case TnefComplianceStatus.UnsupportedPropertyType: message = "Unsupported property type."; break; } if (innerException != null) throw new TnefException (message, innerException); throw new TnefException (message); }
static void TestTnefParser(string path, TnefComplianceStatus expected = TnefComplianceStatus.Compliant) { var message = ParseTnefMessage (path + ".tnef", expected); var names = File.ReadAllLines (path + ".list"); foreach (var name in names) { bool found = false; foreach (var part in message.BodyParts) { if (part.FileName == name) { found = true; break; } } if (!found) Assert.Fail ("Failed to locate attachment: {0}", name); } }
static MimeMessage ParseTnefMessage(string path, TnefComplianceStatus expected) { using (var reader = new TnefReader (File.OpenRead (path), 0, TnefComplianceMode.Loose)) { var message = ExtractTnefMessage (reader); Assert.AreEqual (expected, reader.ComplianceStatus, "Unexpected compliance status."); return message; } }
static void TestTnefParser (string path, TnefComplianceStatus expected = TnefComplianceStatus.Compliant) { var message = ParseTnefMessage (path + ".tnef", expected); var names = File.ReadAllLines (path + ".list"); foreach (var name in names) { bool found = false; foreach (var part in message.BodyParts.OfType<MimePart> ()) { if (part.FileName == name) { found = true; break; } } if (!found) Assert.Fail ("Failed to locate attachment: {0}", name); } // now use TnefPart to do the same thing using (var content = File.OpenRead (path + ".tnef")) { var tnef = new TnefPart { ContentObject = new ContentObject (content) }; var attachments = tnef.ExtractAttachments ().ToList (); foreach (var name in names) { bool found = false; foreach (var part in attachments.OfType<MimePart> ()) { if (part.FileName == name) { found = true; break; } } if (!found) Assert.Fail ("Failed to locate attachment in TnefPart: {0}", name); } } }
static void TestTnefParser(string path, TnefComplianceStatus expected = TnefComplianceStatus.Compliant) { var message = ParseTnefMessage(path + ".tnef", expected); var tnefName = Path.GetFileName(path + ".tnef"); var names = File.ReadAllLines(path + ".list"); foreach (var name in names) { bool found = false; foreach (var part in message.BodyParts.OfType <MimePart> ()) { if (part.FileName == name) { found = true; break; } } if (!found) { Assert.Fail("Failed to locate attachment: {0}", name); } } // now use TnefPart to do the same thing using (var content = File.OpenRead(path + ".tnef")) { var tnef = new TnefPart { Content = new MimeContent(content) }; var attachments = tnef.ExtractAttachments().ToList(); // Step 1: make sure we've extracted the body and all of the attachments foreach (var name in names) { bool found = false; foreach (var part in attachments.OfType <MimePart> ()) { if (part is TextPart && string.IsNullOrEmpty(part.FileName)) { var basename = Path.GetFileNameWithoutExtension(name); var extension = Path.GetExtension(name); string subtype; switch (extension) { case ".html": subtype = "html"; break; case ".rtf": subtype = "rtf"; break; default: subtype = "plain"; break; } if (basename == "body" && part.ContentType.IsMimeType("text", subtype)) { found = true; break; } } else if (part.FileName == name) { found = true; break; } } if (!found) { Assert.Fail("Failed to locate attachment in TnefPart: {0}", name); } } // Step 2: verify that the content of the extracted attachments matches up with the expected content int untitled = 1; foreach (var part in attachments.OfType <MimePart> ()) { var isText = false; string fileName; if (part is TextPart text && string.IsNullOrEmpty(part.FileName)) { if (text.IsHtml) { fileName = "message.html"; } else if (text.IsRichText) { fileName = "message.rtf"; } else { fileName = "message.txt"; } isText = true; }