static void AssertParse(string text, RfcComplianceMode mode) { var buffer = Encoding.UTF8.GetBytes(text); var options = ParserOptions.Default.Clone(); MailboxAddress mailbox; options.AddressParserComplianceMode = mode; try { Assert.IsTrue(MailboxAddress.TryParse(options, text, out mailbox), "MailboxAddress.TryParse(ParserOptions, string) should succeed."); } catch (Exception ex) { Assert.Fail("MailboxAddress.TryParse(ParserOptions, string) should not throw an exception: {0}", ex); } try { Assert.IsTrue(MailboxAddress.TryParse(options, buffer, out mailbox), "MailboxAddress.TryParse(ParserOptions, byte[]) should succeed."); } catch (Exception ex) { Assert.Fail("MailboxAddress.TryParse(ParserOptions, byte[]) should not throw an exception: {0}", ex); } try { Assert.IsTrue(MailboxAddress.TryParse(options, buffer, 0, out mailbox), "MailboxAddress.TryParse(ParserOptions, byte[], int) should succeed."); } catch (Exception ex) { Assert.Fail("MailboxAddress.TryParse(ParserOptions, byte[], int) should not throw an exception: {0}", ex); } try { Assert.IsTrue(MailboxAddress.TryParse(options, buffer, 0, buffer.Length, out mailbox), "MailboxAddress.TryParse(ParserOptions, byte[], int, int) should succeed."); } catch (Exception ex) { Assert.Fail("MailboxAddress.TryParse(ParserOptions, byte[], int, int) should not throw an exception: {0}", ex); } try { mailbox = MailboxAddress.Parse(options, text); } catch (Exception ex) { Assert.Fail("MailboxAddress.Parse(ParserOptions, string) should not throw an exception: {0}", ex); } try { mailbox = MailboxAddress.Parse(options, buffer); } catch (Exception ex) { Assert.Fail("MailboxAddress.Parse(ParserOptions, string) should not throw an exception: {0}", ex); } try { mailbox = MailboxAddress.Parse(options, buffer, 0); } catch (Exception ex) { Assert.Fail("MailboxAddress.Parse(ParserOptions, string) should not throw an exception: {0}", ex); } try { mailbox = MailboxAddress.Parse(options, buffer, 0, buffer.Length); } catch (Exception ex) { Assert.Fail("MailboxAddress.Parse(ParserOptions, string) should not throw an exception: {0}", ex); } }
static void AssertParseFailure(string text, bool result, int tokenIndex, int errorIndex, RfcComplianceMode mode = RfcComplianceMode.Loose) { var buffer = text.Length > 0 ? Encoding.ASCII.GetBytes(text) : new byte[1]; var options = ParserOptions.Default.Clone(); MailboxAddress mailbox; options.AddressParserComplianceMode = mode; Assert.AreEqual(result, MailboxAddress.TryParse(options, text, out mailbox), "MailboxAddress.TryParse(string)"); Assert.AreEqual(result, MailboxAddress.TryParse(options, buffer, out mailbox), "MailboxAddress.TryParse(byte[])"); Assert.AreEqual(result, MailboxAddress.TryParse(options, buffer, 0, out mailbox), "MailboxAddress.TryParse(byte[], int)"); Assert.AreEqual(result, MailboxAddress.TryParse(options, buffer, 0, buffer.Length, out mailbox), "MailboxAddress.TryParse(byte[], int, int)"); try { MailboxAddress.Parse(options, text); Assert.Fail("MailboxAddress.Parse(string) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("MailboxAddress.Parse(string) should throw ParseException."); } try { MailboxAddress.Parse(options, buffer); Assert.Fail("MailboxAddress.Parse(byte[]) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("MailboxAddress.Parse(new byte[]) should throw ParseException."); } try { MailboxAddress.Parse(options, buffer, 0); Assert.Fail("MailboxAddress.Parse(byte[], int) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("MailboxAddress.Parse(new byte[], int) should throw ParseException."); } try { MailboxAddress.Parse(options, buffer, 0, buffer.Length); Assert.Fail("MailboxAddress.Parse(byte[], int, int) should fail."); } catch (ParseException ex) { Assert.AreEqual(tokenIndex, ex.TokenIndex, "ParseException did not have the correct token index."); Assert.AreEqual(errorIndex, ex.ErrorIndex, "ParseException did not have the error index."); } catch { Assert.Fail("MailboxAddress.Parse(new byte[], int, int) should throw ParseException."); } }
// Note: this .ctor is used only by the MimeParser internal MimeMessage (ParserOptions options, IEnumerable<Header> headers) { addresses = new Dictionary<string, InternetAddressList> (MimeUtils.OrdinalIgnoreCase); Headers = new HeaderList (options); compliance = RfcComplianceMode.Loose; // initialize our address lists foreach (var name in StandardAddressHeaders) { var list = new InternetAddressList (); list.Changed += InternetAddressListChanged; addresses.Add (name, list); } references = new MessageIdList (); references.Changed += ReferencesChanged; inreplyto = null; Headers.Changed += HeadersChanged; // add all of our message headers... foreach (var header in headers) { if (header.Field.StartsWith ("Content-", StringComparison.OrdinalIgnoreCase)) continue; Headers.Add (header); } }
internal MimeMessage (ParserOptions options) { addresses = new Dictionary<string, InternetAddressList> (MimeUtils.OrdinalIgnoreCase); Headers = new HeaderList (options); compliance = RfcComplianceMode.Strict; // initialize our address lists foreach (var name in StandardAddressHeaders) { var list = new InternetAddressList (); list.Changed += InternetAddressListChanged; addresses.Add (name, list); } references = new MessageIdList (); references.Changed += ReferencesChanged; inreplyto = null; Headers.Changed += HeadersChanged; }