Beispiel #1
0
        public async Task NoMailCommand()
        {
            (_, _, MockSmtpChannel channel, DataCommand command) = Prepare();
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.BadSequence);
        }
Beispiel #2
0
        public async Task AuthenticatedLargeMessageAccepted()
        {
            var expectedBody = "From:[email protected]\r\n\r\nFirst Line\r\nSecond Line\r\n";

            (MockMailQueue queue, MockMailBuilder builder, MockSmtpChannel channel, DataCommand command) = Prepare(
                expectedBody + ".\r\n",
                TestHelpers.MakeSettings(unauthenticatedMessageSizeLimit: 1));

            channel.AuthenticatedUser = new UserData("*****@*****.**");

            builder.PendingMail = new SmtpMailMessage(new SmtpPath("*****@*****.**"))
            {
                Recipents = { "*****@*****.**" }
            };
            await command.ExecuteAsync(CancellationToken.None);

            Assert.Equal(2, channel.Entries.Count);
            SmtpTestHelper.AssertResponse(channel.Entries[0], SmtpReplyCode.StartMail);
            SmtpTestHelper.AssertResponse(channel.Entries[1], SmtpReplyCode.Okay);
            Assert.Equal(1, queue.References.Count);
            MockMailReference mailReference = queue.References[0];

            Assert.True(mailReference.IsSaved);
            Assert.Equal("*****@*****.**", mailReference.Sender);
            SequenceAssert.SameSet(new[] { "*****@*****.**" }, mailReference.Recipients);
            Assert.Throws <ObjectDisposedException>(() => mailReference.BodyStream.WriteByte(1));
            string mailBody = Encoding.UTF8.GetString(mailReference.BackupBodyStream.ToArray());

            Assert.EndsWith(expectedBody, mailBody);
            Assert.StartsWith("Received:", mailBody);
            Assert.Null(builder.PendingMail);
        }
Beispiel #3
0
        public async Task NoRecipients()
        {
            (_, MockMailBuilder builder, MockSmtpChannel channel, DataCommand command) = Prepare();
            builder.PendingMail = new SmtpMailMessage(new SmtpPath("*****@*****.**"));
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.BadSequence);
        }
Beispiel #4
0
        public async Task NotSupported()
        {
            var channel = new MockSmtpChannel();
            var command = new VerifyCommand(channel);
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.CannotVerify);
        }
Beispiel #5
0
        public async Task NoopSendsMessage()
        {
            var channel = new MockSmtpChannel();
            var noop    = new NoopCommand(channel);

            noop.Initialize("");
            await noop.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);
        }
Beispiel #6
0
        public async Task QuitClosesConnectionAfterReply()
        {
            var channel = new MockSmtpChannel();
            var command = new QuitCommand(channel);

            command.Initialize("");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Closing);
            Assert.True(channel.IsClosed);
        }
Beispiel #7
0
        public async Task NullStateReset()
        {
            var channel = new MockSmtpChannel();
            var builder = new MockMailBuilder();
            var command = new ResetCommand(channel, builder);

            command.Initialize("");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);
            Assert.Null(builder.PendingMail);
        }
Beispiel #8
0
        public async Task ResetAfterMailCommand()
        {
            var channel = new MockSmtpChannel();
            var builder = new MockMailBuilder {
                PendingMail = new SmtpMailMessage(new SmtpPath("*****@*****.**"))
            };
            var command = new ResetCommand(channel, builder);

            command.Initialize("");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);
            Assert.Null(builder.PendingMail);
        }
Beispiel #9
0
        public async Task HeloResponds()
        {
            var channel = new MockSmtpChannel();
            var command = new HelloCommand(channel, TestHelpers.MakeSettings("Testexample.com"), new MockLogger());

            command.Initialize("Sender.net");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);

            MockSmtpChannel.Entry entry = channel.Entries[0];

            Assert.Contains("Testexample.com", entry.Message);
            Assert.Contains("Sender.net", entry.Message);
        }
Beispiel #10
0
        public async Task TooManyArguments()
        {
            var auth = new MockIndex <string, IAuthenticationSession>
            {
                { "PLAIN", new MockPlainTextAuth(MockPlainTextAuth.Action.Null) }
            };
            var channel         = new MockSmtpChannel();
            var mockMailBuilder = new MockMailBuilder();
            var command         = new AuthenticateCommand(auth, channel, mockMailBuilder);

            command.Initialize("MECH INITIAL");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.InvalidArguments);
            Assert.Null(channel.AuthenticatedUser);
        }
Beispiel #11
0
        public async Task Roundtrip()
        {
            var channel = new MockSmtpChannel();
            var trans   = new SmtpAuthenticationTransport(
                channel,
                TestHelpers.GetReader("AQID\r\n")
                );

            await trans.SendAuthenticationFragmentAsync(new byte[] { 4, 5, 6 }, CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.AuthenticationFragment);
            Assert.Equal("BAUG", channel.Entries[0].Message);

            byte[] bytes = await trans.ReadAuthenticationFragmentAsync(CancellationToken.None);

            Assert.Equal(new byte[] { 1, 2, 3 }, bytes);
        }
Beispiel #12
0
        public async Task SuccessfulAuth()
        {
            var auth = new MockIndex <string, IAuthenticationSession>
            {
                { "PLAIN", new MockPlainTextAuth(MockPlainTextAuth.Action.Return) }
            };

            var channel         = new MockSmtpChannel();
            var mockMailBuilder = new MockMailBuilder();
            var command         = new AuthenticateCommand(auth, channel, mockMailBuilder);

            command.Initialize("PLAIN");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.AuthenticationComplete);
            Assert.NotNull(channel.AuthenticatedUser);
            Assert.Equal(MockPlainTextAuth.UserMailbox, channel.AuthenticatedUser.Mailbox);
        }
Beispiel #13
0
        public async Task BadSequenceNoMail()
        {
            var channel = new MockSmtpChannel();
            var builder = new MockMailBuilder();
            var command = new RecipientCommand(
                builder,
                channel,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") })
                );

            command.Initialize("TO:<*****@*****.**>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.BadSequence);
            Assert.Null(builder.PendingMail);
        }
Beispiel #14
0
        public async Task DuringMailSession()
        {
            var auth = new MockIndex <string, IAuthenticationSession>
            {
                { "PLAIN", new MockPlainTextAuth(MockPlainTextAuth.Action.Null) }
            };

            var channel         = new MockSmtpChannel();
            var mockMailBuilder = new MockMailBuilder {
                PendingMail = new SmtpMailMessage(new SmtpPath("*****@*****.**"))
            };
            var command = new AuthenticateCommand(auth, channel, mockMailBuilder);

            command.Initialize("PLAIN");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.BadSequence);
            Assert.Null(channel.AuthenticatedUser);
        }
Beispiel #15
0
        public async Task InvalidDomainRejected()
        {
            var channel = new MockSmtpChannel();
            var builder = new MockMailBuilder();
            var user    = new MockUserStore(false);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:!!!!");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.InvalidArguments);
            Assert.Null(builder.PendingMail);
        }
Beispiel #16
0
        public async Task VailToInternal()
        {
            var channel = new MockSmtpChannel();
            var builder = new MockMailBuilder();
            var user    = new MockUserStore(true);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:<>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);
            Assert.NotNull(builder.PendingMail);
            Assert.Equal("", builder.PendingMail.FromPath.Mailbox);
        }
Beispiel #17
0
        public async Task AlreadyAuthed()
        {
            var auth = new MockIndex <string, IAuthenticationSession>
            {
                { "PLAIN", new MockPlainTextAuth(MockPlainTextAuth.Action.Null) }
            };

            var user    = new UserData("*****@*****.**");
            var channel = new MockSmtpChannel {
                AuthenticatedUser = user
            };
            var mockMailBuilder = new MockMailBuilder();
            var command         = new AuthenticateCommand(auth, channel, mockMailBuilder);

            command.Initialize("PLAIN");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.BadSequence);
            Assert.Same(user, channel.AuthenticatedUser);
        }
Beispiel #18
0
        public async Task RejectInvalidMailbox()
        {
            var channel = new MockSmtpChannel();
            var mail    = new SmtpMailMessage(new SmtpPath("*****@*****.**"));
            var builder = new MockMailBuilder {
                PendingMail = mail
            };
            var command = new RecipientCommand(
                builder,
                channel,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") })
                );

            command.Initialize("TO:<no-at>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.InvalidArguments);
            Assert.Same(mail, builder.PendingMail);
        }
Beispiel #19
0
        public async Task VailMailAcceptedWithReturnPathRejected()
        {
            var channel = new MockSmtpChannel {
                AuthenticatedUser = new UserData("*****@*****.**")
            };
            var builder = new MockMailBuilder();
            var user    = new MockUserStore(true);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:<@other:[email protected]>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.InvalidArguments);
            Assert.Null(builder.PendingMail);
        }
Beispiel #20
0
        public async Task VailMailWithUnknownRejected()
        {
            var channel = new MockSmtpChannel {
                AuthenticatedUser = new UserData("*****@*****.**")
            };
            var builder = new MockMailBuilder();
            var user    = new MockUserStore(true);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:<*****@*****.**> EVIL=FAIL");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.ParameterNotImplemented);
            Assert.Null(builder.PendingMail);
        }
Beispiel #21
0
        public async Task FromWrongMailboxRejected()
        {
            var channel = new MockSmtpChannel {
                AuthenticatedUser = new UserData("*****@*****.**")
            };
            var builder = new MockMailBuilder();
            var user    = new MockUserStore(false);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:<*****@*****.**>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.MailboxUnavailable);
            Assert.Null(builder.PendingMail);
        }
Beispiel #22
0
        public async Task VailMailWithParametersAccepted()
        {
            var channel = new MockSmtpChannel {
                AuthenticatedUser = new UserData("*****@*****.**")
            };
            var builder = new MockMailBuilder();
            var user    = new MockUserStore(true);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:<*****@*****.**> BODY=7BIT");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);
            Assert.NotNull(builder.PendingMail);
            Assert.Equal("*****@*****.**", builder.PendingMail.FromPath.Mailbox);
        }
Beispiel #23
0
        public async Task AcceptRelayDomain()
        {
            var channel = new MockSmtpChannel();
            var mail    = new SmtpMailMessage(new SmtpPath("*****@*****.**"));
            var builder = new MockMailBuilder {
                PendingMail = mail
            };
            var command = new RecipientCommand(
                builder,
                channel,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    relayDomains: new[] { new SmtpRelayDomain("test.vaettir.net", "elsewhere.vaettir.net") })
                );

            command.Initialize("TO:<*****@*****.**>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.Okay);
            Assert.Same(mail, builder.PendingMail);
            Assert.Single(mail.Recipents);
            Assert.Equal("*****@*****.**", mail.Recipents[0]);
        }
Beispiel #24
0
        public async Task MailInProgressRejected()
        {
            var channel = new MockSmtpChannel();
            var message = new SmtpMailMessage(new SmtpPath(null));
            var builder = new MockMailBuilder
            {
                PendingMail = message
            };
            var user    = new MockUserStore(false);
            var command = new MailCommand(
                channel,
                builder,
                TestHelpers.MakeSettings(
                    "test.vaettir.net",
                    new[] { new SmtpAcceptDomain("test.vaettir.net") }),
                user);

            command.Initialize("FROM:<*****@*****.**>");
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.BadSequence);
            Assert.Same(message, builder.PendingMail);
        }
Beispiel #25
0
        public async Task TooLargeMailRejected()
        {
            var expectedBody = "From:[email protected]\r\n\r\nFirst Line\r\nSecond Line\r\n";

            (MockMailQueue queue, MockMailBuilder builder, MockSmtpChannel channel, DataCommand command) = Prepare(
                expectedBody + ".\r\n",
                TestHelpers.MakeSettings(unauthenticatedMessageSizeLimit: 1));

            builder.PendingMail = new SmtpMailMessage(new SmtpPath("*****@*****.**"))
            {
                Recipents = { "*****@*****.**" }
            };
            await command.ExecuteAsync(CancellationToken.None);

            Assert.Equal(2, channel.Entries.Count);
            SmtpTestHelper.AssertResponse(channel.Entries[0], SmtpReplyCode.StartMail);
            SmtpTestHelper.AssertResponse(channel.Entries[1], SmtpReplyCode.ExceededQuota);
            Assert.Equal(1, queue.References.Count);
            MockMailReference mailReference = queue.References[0];

            Assert.False(mailReference.IsSaved);
            Assert.Throws <ObjectDisposedException>(() => mailReference.BodyStream.WriteByte(1));
            Assert.Null(builder.PendingMail);
        }