public void SmtpConversationShouldBeLogged()
        {
            var inMemoryLog = new InMemoryLog();

            var commandHandlerMock = new Mock <ISmtpServerCommandHandler>();

            commandHandlerMock.Setup(f => f.HandleHelo("example.com")).Returns(SmtpCommandReply.CreateDefault250SuccessTask);
            commandHandlerMock.Setup(f => f.HandleMailFrom("*****@*****.**")).Returns(SmtpCommandReply.CreateDefault250SuccessTask);
            commandHandlerMock.Setup(f => f.HandleRcptTo("*****@*****.**")).Returns(SmtpCommandReply.CreateDefault250SuccessTask);
            commandHandlerMock.Setup(f => f.HandleData(It.IsAny <MemoryStreamWithFileBacking>())).Returns(SmtpCommandReply.CreateDefault250SuccessTask);

            var data   = "Hello World\r\n.\r\n";
            var memory = new MemoryStream(Encoding.UTF8.GetBytes(data));

            var connectionMock = ConnectionMockFactory.Create(new string[]
            {
                "HELO example.com",
                "MAIL FROM: [email protected]",
                "RCPT TO: [email protected]",
                "DATA",
                "QUIT"
            }, new[]
            {
                memory
            });

            SmtpServerSession session = new SmtpServerSession(commandHandlerMock.Object, inMemoryLog, new SmtpServerSessionConfiguration());

            var task = session.HandleConnection(connectionMock);

            task.Wait();

            var expectedLogEntries = new List <string>()
            {
                "HELO example.com",
                "MAIL FROM: [email protected]",
                "RCPT TO: [email protected]",
                "DATA",
                "QUIT",

                "250 Ok"
            };


            foreach (var expectedLogEntry in expectedLogEntries)
            {
                Assert.IsTrue(inMemoryLog.LogEntries.Any(logEntry => logEntry.Item1.Message.Contains(expectedLogEntry)), expectedLogEntry);
            }
        }
        public void TestImmediateQuit()
        {
            var commandHandlerMock = new Mock <IPop3ServerCommandHandler>();

            commandHandlerMock.Setup(f => f.HandleQuit()).Returns(Task.Run(() => new Pop3CommandReply(true, "Ok")));

            var connectionMock = ConnectionMockFactory.Create(new[]
            {
                "QUIT"
            }, new List <MemoryStream>());

            Pop3ServerSession session = new Pop3ServerSession(commandHandlerMock.Object, new NullLog(), new Pop3ServerSessionConfiguration());

            var task = session.HandleConnection(connectionMock);

            task.Wait();

            commandHandlerMock.Verify(f => f.HandleQuit());
        }
        public void TestCompleteSmtpServerSession()
        {
            var commandHandlerMock = new Mock <ISmtpServerCommandHandler>();

            commandHandlerMock.Setup(f => f.HandleHelo("example.com")).Returns(SmtpCommandReply.CreateDefault250SuccessTask);
            commandHandlerMock.Setup(f => f.HandleMailFrom("*****@*****.**")).Returns(SmtpCommandReply.CreateDefault250SuccessTask);
            commandHandlerMock.Setup(f => f.HandleRcptTo("*****@*****.**")).Returns(SmtpCommandReply.CreateDefault250SuccessTask);

            commandHandlerMock.Setup(f => f.HandleData(It.IsAny <MemoryStreamWithFileBacking>()))
            .Callback((Stream stream) =>
                      VerifyMemoryStreamContents(stream, "Hello World\r\n"))
            .Returns(SmtpCommandReply.CreateDefault250SuccessTask());

            var data   = "Hello World\r\n.\r\n";
            var memory = new MemoryStream(Encoding.UTF8.GetBytes(data));

            var connectionMock = ConnectionMockFactory.Create(new string[]
            {
                "HELO example.com",
                "MAIL FROM: [email protected]",
                "RCPT TO: [email protected]",
                "DATA",
                "QUIT"
            }, new []
            {
                memory
            });

            SmtpServerSession session = new SmtpServerSession(commandHandlerMock.Object, new NullLog(), new SmtpServerSessionConfiguration());

            var task = session.HandleConnection(connectionMock);

            task.Wait();

            commandHandlerMock.Verify(f => f.HandleHelo("example.com"));
        }