Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cosullivan committed May 28, 2020
1 parent a75f6e2 commit 81f34d4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 85 deletions.
102 changes: 42 additions & 60 deletions Src/SmtpServer.Tests/SmtpParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using SmtpServer.Mail;
using SmtpServer.Protocol;
using SmtpServer.Text;
using Xunit;
Expand Down Expand Up @@ -41,7 +39,7 @@ public void CanMakeNoop()
var parser = CreateParser("NOOP");

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

// assert
Assert.True(result);
Expand All @@ -55,7 +53,7 @@ public void CanMakeHelo()
var parser = CreateParser("HELO abc-1-def.mail.com");

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

// assert
Assert.True(result);
Expand All @@ -73,7 +71,7 @@ public void CanNotMakeHelo(string input)
var parser = CreateParser(input);

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

// assert
Assert.False(result);
Expand All @@ -92,7 +90,7 @@ public void CanMakeEhlo(string input)
var parser = CreateParser(input);

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

var ipOrDomainPart = input.Substring(5);

Expand Down Expand Up @@ -121,7 +119,7 @@ public void CanMakeAuthPlain()
var parser = CreateParser("AUTH PLAIN Y2Fpbi5vc3VsbGl2YW5AZ21haWwuY29t");

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

// assert
Assert.True(result);
Expand All @@ -137,7 +135,7 @@ public void CanMakeAuthLogin()
var parser = CreateParser("AUTH LOGIN Y2Fpbi5vc3VsbGl2YW5AZ21haWwuY29t");

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

// assert
Assert.True(result);
Expand All @@ -163,7 +161,7 @@ public void CanMakeMail(string email, string user, string host, string extension
var parser = CreateParser(mailTo);

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

// assert
Assert.True(result);
Expand All @@ -185,7 +183,7 @@ public void CanMakeMailWithNoAddress()
var parser = CreateParser("MAIL FROM:<>");

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

// assert
Assert.True(result);
Expand All @@ -202,7 +200,7 @@ public void CanMakeMailWithBlankAddress()
var parser = CreateParser("MAIL FROM:< >");

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

// assert
Assert.True(result);
Expand All @@ -220,7 +218,7 @@ public void CanNotMakeMail(string input)
var parser = CreateParser(input);

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

// assert
Assert.False(result);
Expand All @@ -237,7 +235,7 @@ public void CanMakeRcpt(string email, string user, string host)
var parser = CreateParser($"RCPT TO:<{email}>");

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

// assert
Assert.True(result);
Expand All @@ -253,15 +251,15 @@ public void CanMakeProxyIpV4()
var parser = CreateParser("PROXY TCP4 192.168.1.1 192.168.1.2 1234 16789");

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

// assert
Assert.True(result);
Assert.True(command is ProxyProtocolCommand);
Assert.Equal("192.168.1.1", ((ProxyProtocolCommand)command).SourceEndpoint.Address.ToString());
Assert.Equal("192.168.1.2", ((ProxyProtocolCommand)command).DestinationEndpoint.Address.ToString());
Assert.Equal(1234, ((ProxyProtocolCommand)command).SourceEndpoint.Port);
Assert.Equal(16789, ((ProxyProtocolCommand)command).DestinationEndpoint.Port);
Assert.True(command is ProxyCommand);
Assert.Equal("192.168.1.1", ((ProxyCommand)command).SourceEndpoint.Address.ToString());
Assert.Equal("192.168.1.2", ((ProxyCommand)command).DestinationEndpoint.Address.ToString());
Assert.Equal(1234, ((ProxyCommand)command).SourceEndpoint.Port);
Assert.Equal(16789, ((ProxyCommand)command).DestinationEndpoint.Port);
}

[Fact]
Expand All @@ -271,26 +269,25 @@ public void CanMakeProxyIpV6()
var parser = CreateParser("PROXY TCP6 2001:1234:abcd::0001 3456:2e76:66d8:f84:abcd:abef:ffff:1234 1234 16789");

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

// assert
Assert.True(result);
Assert.True(command is ProxyProtocolCommand);
Assert.Equal(IPAddress.Parse("2001:1234:abcd::0001").ToString(), ((ProxyProtocolCommand)command).SourceEndpoint.Address.ToString());
Assert.Equal(IPAddress.Parse("3456:2e76:66d8:f84:abcd:abef:ffff:1234").ToString(), ((ProxyProtocolCommand)command).DestinationEndpoint.Address.ToString());
Assert.Equal(1234, ((ProxyProtocolCommand)command).SourceEndpoint.Port);
Assert.Equal(16789, ((ProxyProtocolCommand)command).DestinationEndpoint.Port);
Assert.True(command is ProxyCommand);
Assert.Equal(IPAddress.Parse("2001:1234:abcd::0001").ToString(), ((ProxyCommand)command).SourceEndpoint.Address.ToString());
Assert.Equal(IPAddress.Parse("3456:2e76:66d8:f84:abcd:abef:ffff:1234").ToString(), ((ProxyCommand)command).DestinationEndpoint.Address.ToString());
Assert.Equal(1234, ((ProxyCommand)command).SourceEndpoint.Port);
Assert.Equal(16789, ((ProxyCommand)command).DestinationEndpoint.Port);
}

[Fact]
public void CanMakeAtom()
{
// arrange
var parser = CreateParser("hello");
string atom;

// act
var made = parser.TryMakeAtom(out atom);
var made = parser.TryMakeAtom(out var atom);

// assert
Assert.True(made);
Expand All @@ -302,10 +299,9 @@ public void CanMakeDotString()
{
// arrange
var parser = CreateParser("abc.def.hij");
string dotString;

// act
var made = parser.TryMakeDotString(out dotString);
var made = parser.TryMakeDotString(out var dotString);

// assert
Assert.True(made);
Expand All @@ -317,10 +313,9 @@ public void CanMakeLocalPart()
{
// arrange
var parser = CreateParser("abc");
string localPart;

// act
var made = parser.TryMakeLocalPart(out localPart);
var made = parser.TryMakeLocalPart(out var localPart);

// assert
Assert.True(made);
Expand All @@ -331,12 +326,10 @@ public void CanMakeLocalPart()
public void CanMakeTextOrNumber()
{
// arrange
string textOrNumber1;
string textOrNumber2;

// act
var made1 = CreateParser("abc").TryMakeTextOrNumber(out textOrNumber1);
var made2 = CreateParser("123").TryMakeTextOrNumber(out textOrNumber2);
var made1 = CreateParser("abc").TryMakeTextOrNumber(out var textOrNumber1);
var made2 = CreateParser("123").TryMakeTextOrNumber(out var textOrNumber2);

// assert
Assert.True(made1);
Expand All @@ -350,10 +343,9 @@ public void CanMakeTextOrNumberOrHyphenString()
{
// arrange
var parser = CreateParser("a1-b2");
string textOrNumberOrHyphen1;

// act
var made1 = parser.TryMakeTextOrNumberOrHyphenString(out textOrNumberOrHyphen1);
var made1 = parser.TryMakeTextOrNumberOrHyphenString(out var textOrNumberOrHyphen1);

// assert
Assert.True(made1);
Expand All @@ -365,10 +357,9 @@ public void CanMakeSubdomain()
{
// arrange
var parser = CreateParser("a-1-b-2");
string subdomain;

// act
var made = parser.TryMakeSubdomain(out subdomain);
var made = parser.TryMakeSubdomain(out var subdomain);

// assert
Assert.True(made);
Expand All @@ -380,10 +371,9 @@ public void CanMakeDomain()
{
// arrange
var parser = CreateParser("123.abc.com");
string domain;

// act
var made = parser.TryMakeDomain(out domain);
var made = parser.TryMakeDomain(out var domain);

// assert
Assert.True(made);
Expand All @@ -404,10 +394,9 @@ public void CanMakeMailbox(string email, string user, string host)
{
// arrange
var parser = CreateParser(email);
IMailbox mailbox;

// act
var made = parser.TryMakeMailbox(out mailbox);
var made = parser.TryMakeMailbox(out var mailbox);

// assert
Assert.True(made);
Expand All @@ -420,10 +409,9 @@ public void CanMakePlusAddressMailBox()
{
// arrange
var parser = CreateParser("[email protected]");
IMailbox mailbox;

// act
var made = parser.TryMakeMailbox(out mailbox);
var made = parser.TryMakeMailbox(out var mailbox);

// assert
Assert.True(made);
Expand All @@ -436,10 +424,9 @@ public void CanMakeAtDomain()
{
// arrange
var parser = CreateParser("@gmail.com");
string atDomain;

// act
var made = parser.TryMakeAtDomain(out atDomain);
var made = parser.TryMakeAtDomain(out var atDomain);

// assert
Assert.True(made);
Expand All @@ -451,10 +438,9 @@ public void CanMakeAtDomainList()
{
// arrange
var parser = CreateParser("@gmail.com,@hotmail.com");
string atDomainList;

// act
var made = parser.TryMakeAtDomainList(out atDomainList);
var made = parser.TryMakeAtDomainList(out var atDomainList);

// assert
Assert.True(made);
Expand All @@ -466,10 +452,9 @@ public void CanMakePath()
{
// path
var parser = CreateParser("<@gmail.com,@hotmail.com:[email protected]>");
IMailbox mailbox;

// act
var made = parser.TryMakePath(out mailbox);
var made = parser.TryMakePath(out var mailbox);

// assert
Assert.True(made);
Expand All @@ -482,10 +467,9 @@ public void CanMakeReversePath()
{
// path
var parser = CreateParser("<@gmail.com,@hotmail.com:[email protected]>");
IMailbox mailbox;

// act
var made = parser.TryMakePath(out mailbox);
var made = parser.TryMakePath(out var mailbox);

// assert
Assert.True(made);
Expand All @@ -498,10 +482,9 @@ public void CanMakeAddressLiteral()
{
// arrange
var parser = CreateParser("[ 127.0.0.1 ]");
string address;

// act
var made = parser.TryMakeAddressLiteral(out address);
var made = parser.TryMakeAddressLiteral(out var address);

// assert
Assert.True(made);
Expand All @@ -513,20 +496,19 @@ public void CanMakeMailParameters()
{
// arrange
var parser = CreateParser("SIZE=123 ABC=DEF ABCDE ZZZ=123");
IReadOnlyDictionary<string, string> parameters;

// act
var made = parser.TryMakeMailParameters(out parameters);
var made = parser.TryMakeMailParameters(out var parameters);

// assert
Assert.True(made);
Assert.Equal(4, parameters.Count);
Assert.True(parameters.ContainsKey("SIZE"));
Assert.Equal(parameters["SIZE"], "123");
Assert.Equal("123", parameters["SIZE"]);
Assert.True(parameters.ContainsKey("ABC"));
Assert.Equal(parameters["ABC"], "DEF");
Assert.Equal("DEF", parameters["ABC"]);
Assert.True(parameters.ContainsKey("ZZZ"));
Assert.Equal(parameters["ZZZ"], "123");
Assert.Equal("123", parameters["ZZZ"]);
Assert.True(parameters.ContainsKey("ABCDE"));
}

Expand Down
1 change: 0 additions & 1 deletion Src/SmtpServer/ISessionContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using SmtpServer.IO;

namespace SmtpServer
Expand Down
Loading

0 comments on commit 81f34d4

Please sign in to comment.