Skip to content

Commit

Permalink
Fix for #74 -- Issue #81 -- parser now supports UTF-8 encoding. (#82)
Browse files Browse the repository at this point in the history
* Fix for #74 -- parser now supports UTF-8 encoding.

* Was too quick on that commit -- fixed now.
  • Loading branch information
Jaben authored and cosullivan committed Jul 23, 2018
1 parent 44c88b3 commit 44a5e19
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
59 changes: 31 additions & 28 deletions Src/SmtpServer.Tests/SmtpParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SmtpParserTests
{
static SmtpParser CreateParser(string text)
{
var segment = new ArraySegment<byte>(Encoding.ASCII.GetBytes(text));
var segment = new ArraySegment<byte>(Encoding.UTF8.GetBytes(text));

var options = new SmtpServerOptionsBuilder().Logger(new NullLogger()).Build();

Expand Down Expand Up @@ -129,38 +129,38 @@ public void CanMakeAuthLogin()
Assert.Equal("Y2Fpbi5vc3VsbGl2YW5AZ21haWwuY29t", ((AuthCommand)command).Parameter);
}

[Fact]
public void CanMakeMail()
[Theory]
[InlineData("cain.osullivan@gmail.com", "cain.osullivan", "gmail.com")]
[InlineData(@"""Abc@def""@example.com", "Abc@def", "example.com")]
[InlineData("pelé@example.com", "pelé", "example.com", "SMTPUTF8")]
public void CanMakeMail(string email, string user, string host, string extension = null)
{
// arrange
var parser = CreateParser("MAIL FROM:<cain.osullivan@gmail.com>");
var mailTo = $"MAIL FROM:<{email}>";

if (!string.IsNullOrWhiteSpace(extension))
{
mailTo += $" {extension}";
}

var parser = CreateParser(mailTo);

// act
var result = parser.TryMakeMail(out SmtpCommand command, out SmtpResponse errorResponse);

// assert
Assert.True(result);
Assert.True(command is MailCommand);
Assert.Equal("cain.osullivan", ((MailCommand)command).Address.User);
Assert.Equal("gmail.com", ((MailCommand)command).Address.Host);
Assert.Equal(user, ((MailCommand)command).Address.User);
Assert.Equal(host, ((MailCommand)command).Address.Host);

if (!string.IsNullOrWhiteSpace(extension))
{
// verify the extension was put in the parameters
Assert.True(((MailCommand)command).Parameters.ContainsKey(extension));
}
}

//[Fact]
//public void CanMakeUtf8Mail()
//{
// // arrange
// var parser = CreateParser("MAIL FROM:<pelé@example.com> SMTPUTF8");

// // act
// var result = parser.TryMakeMail(out SmtpCommand command, out SmtpResponse errorResponse);

// // assert
// Assert.True(result);
// Assert.True(command is MailCommand);
// Assert.Equal("pelé", ((MailCommand)command).Address.User);
// Assert.Equal("example.com", ((MailCommand)command).Address.Host);
//}

[Fact]
public void CanMakeMailWithNoAddress()
{
Expand Down Expand Up @@ -210,22 +210,25 @@ public void CanNotMakeMail(string input)
Assert.NotNull(errorResponse);
}

[Fact]
public void CanMakeRcpt()
[Theory]
[InlineData("cain.osullivan@gmail.com", "cain.osullivan", "gmail.com")]
[InlineData(@"""Abc@def""@example.com", "Abc@def", "example.com")]
[InlineData("pelé@example.com", "pelé", "example.com")]
public void CanMakeRcpt(string email, string user, string host)
{
// arrange
var parser = CreateParser("RCPT TO:<cain.osullivan@gmail.com>");
var parser = CreateParser($"RCPT TO:<{email}>");

// act
var result = parser.TryMakeRcpt(out SmtpCommand command, out SmtpResponse errorResponse);

// assert
Assert.True(result);
Assert.True(command is RcptCommand);
Assert.Equal("cain.osullivan", ((RcptCommand)command).Address.User);
Assert.Equal("gmail.com", ((RcptCommand)command).Address.Host);
Assert.Equal(user, ((RcptCommand)command).Address.User);
Assert.Equal(host, ((RcptCommand)command).Address.Host);
}

[Fact]
public void CanMakeAtom()
{
Expand Down
12 changes: 11 additions & 1 deletion Src/SmtpServer/Text/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,17 @@ public static bool IsTextOrNumber(byte value)
/// <returns>true if the value is considered a text character, false if not.</returns>
public static bool IsText(byte value)
{
return IsBetween(value, 65, 90) || IsBetween(value, 97, 122);
return IsBetween(value, 65, 90) || IsBetween(value, 97, 122) || IsUtf8(value);
}

/// <summary>
/// Returns a value indicating whether or not the given byte is a UTF-8 encoded character.
/// </summary>
/// <param name="value">The value to test.</param>
/// <returns>true if the value is considered a UTF-8 character, false if not.</returns>
public static bool IsUtf8(byte value)
{
return value >= 0x80;
}

/// <summary>
Expand Down

0 comments on commit 44a5e19

Please sign in to comment.