Пример #1
0
        public async Task Unsecured_Certificate_ExtendedHelloResponds()
        {
            var channel = new MockSmtpChannel();
            var conn    = new MockConnectionSecurity();

            conn.Certificate = TestHelpers.GetSelfSigned();

            var command = new ExtendedHelloCommand(
                TestHelpers.GetAuths(),
                conn,
                channel,
                TestHelpers.MakeSettings("test.vaettir.net"),
                new MockLogger());

            command.Initialize("Sender.net");

            await command.ExecuteAsync(CancellationToken.None);

            Assert.True(channel.Entries.All(c => c.Code == SmtpReplyCode.Okay));
            Assert.True(channel.Entries.Take(channel.Entries.Count - 1).All(e => e.More));
            Assert.False(channel.Entries.Last().More);

            Assert.Contains(channel.Entries, e => e.Message == "STARTTLS");
            List <MockSmtpChannel.Entry> authReplies = channel.Entries.Where(e => e.Message.StartsWith("AUTH")).ToList();

            Assert.Single(authReplies);
            List <string> authParts = authReplies[0].Message.Split(' ').Skip(1).ToList();

            SequenceAssert.SameSet(new[] { "PLN" }, authParts);

            MockSmtpChannel.Entry signoff = channel.Entries.First();
            Assert.Contains("test.vaettir.net", signoff.Message);
            Assert.Contains("Sender.net", signoff.Message);
        }
Пример #2
0
        public async Task Unsecured_NoCertificate_ExtendedHelloResponds()
        {
            var channel = new MockSmtpChannel();
            var conn    = new MockConnectionSecurity();

            var command = new ExtendedHelloCommand(
                TestHelpers.GetAuths(),
                conn,
                channel,
                TestHelpers.MakeSettings("test.vaettir.net"),
                new MockLogger());

            command.Initialize("Sender.net");

            await command.ExecuteAsync(CancellationToken.None);

            Assert.True(channel.Entries.All(c => c.Code == SmtpReplyCode.Okay));
            Assert.True(channel.Entries.Take(channel.Entries.Count - 1).All(e => e.More));
            Assert.False(channel.Entries.Last().More);

            Assert.DoesNotContain(channel.Entries, e => e.Message == "STARTTLS");
            Assert.DoesNotContain(channel.Entries, e => e.Message == "AUTH ENC");
            Assert.Contains(channel.Entries, e => e.Message == "AUTH PLN");

            MockSmtpChannel.Entry signoff = channel.Entries.First();
            Assert.Contains("test.vaettir.net", signoff.Message);
            Assert.Contains("Sender.net", signoff.Message);
        }
Пример #3
0
        public async Task NotSupported()
        {
            var channel = new MockSmtpChannel();
            var command = new VerifyCommand(channel);
            await command.ExecuteAsync(CancellationToken.None);

            SmtpTestHelper.AssertResponse(channel, SmtpReplyCode.CannotVerify);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
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);
        }
Пример #11
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);
        }
Пример #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);
        }
Пример #13
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);
        }
Пример #14
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);
        }
Пример #15
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);
        }
Пример #16
0
        private static (MockMailQueue, MockMailBuilder, MockSmtpChannel, DataCommand) Prepare(
            string content         = "",
            AgentSettings settings = null)
        {
            var queue = new MockMailQueue();

            settings = settings ?? TestHelpers.MakeSettings();
            var builder = new MockMailBuilder();
            var channel = new MockSmtpChannel();
            var command = new DataCommand(
                queue,
                settings,
                TestHelpers.GetReader(content),
                new ConnectionInformation("local", "remote"),
                builder,
                channel
                );

            return(queue, builder, channel, command);
        }
Пример #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);
        }
Пример #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);
        }
Пример #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);
        }
Пример #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);
        }
Пример #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);
        }
Пример #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);
        }
Пример #23
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);
        }
Пример #24
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]);
        }
Пример #25
0
 public static void AssertResponse(MockSmtpChannel channel, SmtpReplyCode reply)
 {
     Assert.Equal(1, channel.Entries.Count);
     AssertResponse(channel.Entries[0], reply);
 }