/// <summary> /// The Process_AddressAsync /// </summary> /// <param name="address">The address<see cref="string"/></param> /// <param name="expectedParsedAddress">The expectedParsedAddress<see cref="string"/></param> /// <param name="expectedResponse">The expectedResponse<see cref="StandardSmtpResponseCode"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task Process_AddressAsync(string address, string expectedParsedAddress, StandardSmtpResponseCode expectedResponse, bool asException = false, bool eightBitMessage = false) { TestMocks mocks = new TestMocks(); Mock <IMessageBuilder> message = new Mock <IMessageBuilder>(); message.SetupGet(m => m.EightBitTransport).Returns(eightBitMessage); IMessageBuilder currentMessage = null; mocks.Connection.Setup(c => c.NewMessage()).ReturnsAsync(() => { currentMessage = message.Object; return(currentMessage); }); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(() => currentMessage); MailFromVerb mailFromVerb = new MailFromVerb(); if (!asException) { await mailFromVerb.Process(mocks.Connection.Object, new SmtpCommand("FROM " + address)).ConfigureAwait(false); mocks.VerifyWriteResponse(expectedResponse); } else { SmtpServerException e = await Assert.ThrowsAsync <SmtpServerException>(() => mailFromVerb.Process(mocks.Connection.Object, new SmtpCommand("FROM " + address))); Assert.Equal((int)expectedResponse, e.SmtpResponse.Code); } if (expectedParsedAddress != null) { message.VerifySet(m => m.From = expectedParsedAddress); } }
public async Task Noop() { TestMocks mocks = new TestMocks(); NoopVerb verb = new NoopVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("NOOP")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); }
public async Task Quit_RespondsWithClosingChannel() { TestMocks mocks = new TestMocks(); QuitVerb quitVerb = new QuitVerb(); await quitVerb.Process(mocks.Connection.Object, new SmtpCommand("QUIT")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.ClosingTransmissionChannel); }
public async Task Process_MissingAddress_ErrorResponse() { TestMocks mocks = new TestMocks(); MailFromVerb mailFromVerb = new MailFromVerb(); await mailFromVerb.Process(mocks.Connection.Object, new SmtpCommand("FROM")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments); }
public async Task Data_NoCurrentMessage_ReturnsError() { TestMocks mocks = new TestMocks(); DataVerb verb = new DataVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("DATA")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.BadSequenceOfCommands); }
public async Task SayHelo_NoName() { TestMocks mocks = new TestMocks(); HeloVerb verb = new HeloVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("HELO")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); mocks.Session.VerifySet(s => s.ClientName = ""); }
public async Task Process_NoArguments_Accepted() { TestMocks mocks = new TestMocks(); EhloVerb ehloVerb = new EhloVerb(); await ehloVerb.Process(mocks.Connection.Object, new SmtpCommand("EHLO")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); mocks.Session.VerifySet(s => s.ClientName = ""); }
public async Task NoCertificateAvailable_ReturnsErrorResponse() { TestMocks mocks = new TestMocks(); mocks.ServerBehaviour.Setup(b => b.GetSSLCertificate(It.IsAny <IConnection>())).ReturnsAsync((X509Certificate)null); StartTlsVerb verb = new StartTlsVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("STARTTLS")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.CommandNotImplemented); }
public async Task SayHeloTwice_ReturnsError() { TestMocks mocks = new TestMocks(); mocks.Session.SetupGet(s => s.ClientName).Returns("already.said.helo"); HeloVerb verb = new HeloVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("HELO foo.blah")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.BadSequenceOfCommands); }
public async Task Data_ExactlySizeLimit_Accepted() { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); mocks.ServerBehaviour.Setup(b => b.GetMaximumMessageSize(It.IsAny <IConnection>())).ReturnsAsync(10); string[] messageData = new string[] { new string('x', 10), "." }; int messageLine = 0; mocks.Connection.Setup(c => c.ReadLine()).Returns(() => Task.FromResult(messageData[messageLine++])); DataVerb verb = new DataVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("DATA")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.StartMailInputEndWithDot); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); }
public async Task Process_AlreadyGivenFrom_ErrorResponse() { TestMocks mocks = new TestMocks(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(new Mock <IMessageBuilder>().Object); MailFromVerb mailFromVerb = new MailFromVerb(); await mailFromVerb.Process(mocks.Connection.Object, new SmtpCommand("FROM <*****@*****.**>")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.BadSequenceOfCommands); }
public async Task Process_SaidHeloAlready_Allowed() { TestMocks mocks = new TestMocks(); EhloVerb verb = new EhloVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("EHLO foo.blah")).ConfigureAwait(false); await verb.Process(mocks.Connection.Object, new SmtpCommand("EHLO foo.blah")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK, Times.Exactly(2)); }
public async Task ProcessAsync_UnregisteredSubCommand_ErrorResponse() { TestMocks mocks = new TestMocks(); Mock <VerbWithSubCommands> verbWithSubCommands = new Mock <VerbWithSubCommands>() { CallBase = true }; await verbWithSubCommands.Object.Process(mocks.Connection.Object, new SmtpCommand("VERB SUBCOMMAND1")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.CommandParameterNotImplemented); }
public async Task ProcessAsync() { TestMocks mocks = new TestMocks(); mocks.Connection.Setup(c => c.AbortMessage()).Returns(Task.CompletedTask).Verifiable(); RsetVerb verb = new RsetVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("RSET")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); mocks.Connection.Verify(); }
/// <summary> /// /// </summary> /// <param name="address">The address<see cref="string"/></param> /// <param name="expectedAddress">The expectedAddress<see cref="string"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task TestGoodAddressAsync(string address, string expectedAddress, bool eightBit = false) { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); messageBuilder.EightBitTransport = eightBit; mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); RcptToVerb verb = new RcptToVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("TO " + address)).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); Assert.Equal(expectedAddress, messageBuilder.Recipients.First()); }
/// <summary> /// /// </summary> /// <param name="messageData">The messageData<see cref="string"/></param> /// <param name="expectedData">The expectedData<see cref="string"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task TestGoodDataAsync(string[] messageData, string expectedData) { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); mocks.ServerBehaviour.Setup(b => b.GetMaximumMessageSize(It.IsAny <IConnection>())).ReturnsAsync((long?)null); int messageLine = 0; mocks.Connection.Setup(c => c.ReadLine()).Returns(() => Task.FromResult(messageData[messageLine++])); DataVerb verb = new DataVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("DATA")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.StartMailInputEndWithDot); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); using (StreamReader dataReader = new StreamReader(await messageBuilder.GetData().ConfigureAwait(false), Encoding.UTF8)) { Assert.Equal(expectedData, dataReader.ReadToEnd()); } }
public async Task Process_RespondsWith250() { TestMocks mocks = new TestMocks(); Mock <IExtensionProcessor> mockExtensionProcessor1 = new Mock <IExtensionProcessor>(); mockExtensionProcessor1.Setup(ep => ep.GetEHLOKeywords()).ReturnsAsync(new[] { "EXTN1" }); Mock <IExtensionProcessor> mockExtensionProcessor2 = new Mock <IExtensionProcessor>(); mockExtensionProcessor2.Setup(ep => ep.GetEHLOKeywords()).ReturnsAsync(new[] { "EXTN2A", "EXTN2B" }); mocks.Connection.SetupGet(c => c.ExtensionProcessors).Returns(new[] { mockExtensionProcessor1.Object, mockExtensionProcessor2.Object }); EhloVerb ehloVerb = new EhloVerb(); await ehloVerb.Process(mocks.Connection.Object, new SmtpCommand("EHLO foobar")).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.OK); }
/// <summary> /// /// </summary> /// <param name="address">The address<see cref="string"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task TestBadAddressAsync(string address, bool asException = false) { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); RcptToVerb verb = new RcptToVerb(); if (!asException) { await verb.Process(mocks.Connection.Object, new SmtpCommand("TO " + address)).ConfigureAwait(false); mocks.VerifyWriteResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments); } else { SmtpServerException e = await Assert.ThrowsAsync <SmtpServerException>(() => verb.Process(mocks.Connection.Object, new SmtpCommand("TO " + address))).ConfigureAwait(false); Assert.Equal((int)StandardSmtpResponseCode.SyntaxErrorInCommandArguments, e.SmtpResponse.Code); } Assert.Equal(0, messageBuilder.Recipients.Count); }