Exemplo n.º 1
0
        public void ServerCanBeSecuredAndAuthenticated()
        {
            var userAuthenticator = new DelegatingUserAuthenticator((user, password) => true);

            ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationFailureForTestingOnly;

            using (var disposable = CreateServer(
                       options =>
                       options
                       .UserAuthenticator(userAuthenticator)
                       .AllowUnsecureAuthentication(true)
                       .Certificate(CreateCertificate())
                       .SupportedSslProtocols(SslProtocols.Default)))
            {
                ISessionContext sessionContext        = null;
                var             sessionCreatedHandler = new EventHandler <SessionEventArgs>(
                    delegate(object sender, SessionEventArgs args)
                {
                    sessionContext = args.Context;
                });

                disposable.Server.SessionCreated += sessionCreatedHandler;

                MailClient.Send(user: "******", password: "******");

                disposable.Server.SessionCreated -= sessionCreatedHandler;

                Assert.True(sessionContext.IsSecure);
                Assert.True(sessionContext.IsAuthenticated);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
Exemplo n.º 2
0
        public void SecuresTheSessionWhenCertificateIsSupplied()
        {
            ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationFailureForTestingOnly;

            using (var disposable = CreateServer(options => options.Certificate(CreateCertificate())))
            {
                var isSecure = false;
                var sessionCreatedHandler = new EventHandler <SessionEventArgs>(
                    delegate(object sender, SessionEventArgs args)
                {
                    args.Context.CommandExecuted += (_, commandArgs) =>
                    {
                        isSecure = commandArgs.Context.Pipe.IsSecure;
                    };
                });

                disposable.Server.SessionCreated += sessionCreatedHandler;

                MailClient.Send();

                disposable.Server.SessionCreated -= sessionCreatedHandler;

                Assert.True(isSecure);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
Exemplo n.º 3
0
        public void ServerCanBeSecuredAndAuthenticated()
        {
            var userAuthenticator = new DelegatingUserAuthenticator((user, password) => true);

            ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationFailureForTestingOnly;

            using (var disposable = CreateServer(
                       server => server.Certificate(CreateCertificate()).SupportedSslProtocols(SslProtocols.Tls12),
                       endpoint => endpoint.AllowUnsecureAuthentication(true),
                       services => services.Add(userAuthenticator)))
            {
                var             isSecure              = false;
                ISessionContext sessionContext        = null;
                var             sessionCreatedHandler = new EventHandler <SessionEventArgs>(
                    delegate(object sender, SessionEventArgs args)
                {
                    sessionContext = args.Context;
                    sessionContext.CommandExecuted += (_, commandArgs) =>
                    {
                        isSecure = commandArgs.Context.Pipe.IsSecure;
                    };
                });

                disposable.Server.SessionCreated += sessionCreatedHandler;

                MailClient.Send(user: "******", password: "******");

                disposable.Server.SessionCreated -= sessionCreatedHandler;

                Assert.True(isSecure);
                Assert.True(sessionContext.Authentication.IsAuthenticated);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
Exemplo n.º 4
0
        public void SecuresTheSessionByDefault()
        {
            ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationFailureForTestingOnly;

            using (var disposable = CreateServer(endpoint => endpoint.IsSecure(true).Certificate(CreateCertificate())))
            {
                var isSecure = false;
                var sessionCreatedHandler = new EventHandler <SessionEventArgs>(
                    delegate(object sender, SessionEventArgs args)
                {
                    args.Context.CommandExecuted += (_, commandArgs) =>
                    {
                        isSecure = commandArgs.Context.Pipe.IsSecure;
                    };
                });

                disposable.Server.SessionCreated += sessionCreatedHandler;

                MailClient.NoOp(MailKit.Security.SecureSocketOptions.SslOnConnect);

                disposable.Server.SessionCreated -= sessionCreatedHandler;

                Assert.True(isSecure);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
Exemplo n.º 5
0
        public void CanForceUserAuthentication_ThrowsIfLoginIsNotSent()
        {
            var userAuthenticator = new DelegatingUserAuthenticator((user, password) => true);

            using (CreateServer(options => options.AllowUnsecureAuthentication().AuthenticationRequired().UserAuthenticator(userAuthenticator)))
            {
                Assert.Throws <ServiceNotAuthenticatedException>(() => MailClient.Send());
            }
        }
Exemplo n.º 6
0
        public void CanForceUserAuthentication_DoesNotThrowIfLoginIsSent()
        {
            var userAuthenticator = new DelegatingUserAuthenticator((user, password) => true);

            using (CreateServer(options => options.AllowUnsecureAuthentication().AuthenticationRequired().UserAuthenticator(userAuthenticator)))
            {
                MailClient.Send(user: "******", password: "******");
            }
        }
Exemplo n.º 7
0
        public void CanReceiveUnicodeMimeMessage(string text, string charset)
        {
            using (CreateServer())
            {
                // act
                MailClient.Send(MailClient.Message(subject: text, text: text, charset: charset));

                // assert
                Assert.Equal(1, MessageStore.Messages.Count);
                Assert.Equal(text, MessageStore.Messages[0].Subject());
                Assert.Equal(text, MessageStore.Messages[0].Text(charset));
            }
        }
Exemplo n.º 8
0
        public void CanReceiveMessage()
        {
            using (CreateServer())
            {
                // act
                MailClient.Send(MailClient.Message(from: "*****@*****.**", to: "*****@*****.**"));

                // assert
                Assert.Equal(1, MessageStore.Messages.Count);
                Assert.Equal("*****@*****.**", MessageStore.Messages[0].From.AsAddress());
                Assert.Equal(1, MessageStore.Messages[0].To.Count);
                Assert.Equal("*****@*****.**", MessageStore.Messages[0].To[0].AsAddress());
            }
        }
Exemplo n.º 9
0
        public void CanReturnSmtpResponseException_SessionWillQuit()
        {
            // arrange
            var mailboxFilter = new DelegatingMailboxFilter(@from => throw new SmtpResponseException(SmtpResponse.AuthenticationRequired, true));

            using (CreateServer(services => services.Add(mailboxFilter)))
            {
                using var client = MailClient.Client();

                Assert.Throws <ServiceNotAuthenticatedException>(() => client.Send(MailClient.Message()));

                // no longer connected to this is invalid
                Assert.ThrowsAny <Exception>(() => client.NoOp());
            }
        }
Exemplo n.º 10
0
        public void CanReceiveBccInMessageTransaction()
        {
            using (CreateServer())
            {
                // act
                MailClient.Send(MailClient.Message(from: "*****@*****.**", to: "*****@*****.**", cc: "*****@*****.**", bcc: "*****@*****.**"));

                // assert
                Assert.Equal(1, MessageStore.Messages.Count);
                Assert.Equal("*****@*****.**", MessageStore.Messages[0].From.AsAddress());
                Assert.Equal(3, MessageStore.Messages[0].To.Count);
                Assert.Equal("*****@*****.**", MessageStore.Messages[0].To[0].AsAddress());
                Assert.Equal("*****@*****.**", MessageStore.Messages[0].To[1].AsAddress());
                Assert.Equal("*****@*****.**", MessageStore.Messages[0].To[2].AsAddress());
            }
        }
Exemplo n.º 11
0
        public void WillTimeoutWaitingForCommand()
        {
            using (CreateServer(c => c.CommandWaitTimeout(TimeSpan.FromSeconds(1))))
            {
                var client = MailClient.Client();
                client.NoOp();

                for (var i = 0; i < 5; i++)
                {
                    Task.Delay(TimeSpan.FromMilliseconds(250)).Wait();
                    client.NoOp();
                }

                Task.Delay(TimeSpan.FromSeconds(5)).Wait();

                Assert.Throws <IOException>(() => client.NoOp());
            }
        }
Exemplo n.º 12
0
        public void EndpointListenerWillRaiseEndPointEvents()
        {
            var endpointListenerFactory = new EndpointListenerFactory();

            var started = false;
            var stopped = false;

            endpointListenerFactory.EndpointStarted += (sender, e) => { started = true; };
            endpointListenerFactory.EndpointStopped += (sender, e) => { stopped = true; };

            using (CreateServer(services => services.Add(endpointListenerFactory)))
            {
                MailClient.Send();
            }

            Assert.True(started);
            Assert.True(stopped);
        }
Exemplo n.º 13
0
        public void CanReturnSmtpResponseException_DoesNotQuit()
        {
            // arrange
            var mailboxFilter = new DelegatingMailboxFilter(@from =>
            {
                throw new SmtpResponseException(SmtpResponse.AuthenticationRequired);

#pragma warning disable 162
                return(MailboxFilterResult.Yes);

#pragma warning restore 162
            });

            using (CreateServer(services => services.Add(mailboxFilter)))
            {
                using var client = MailClient.Client();

                Assert.Throws <ServiceNotAuthenticatedException>(() => client.Send(MailClient.Message()));

                client.NoOp();
            }
        }
Exemplo n.º 14
0
        public void DoesNotSecureTheSessionWhenCertificateIsEmpty()
        {
            using (var disposable = CreateServer())
            {
                ISessionContext sessionContext        = null;
                var             sessionCreatedHandler = new EventHandler <SessionEventArgs>(
                    delegate(object sender, SessionEventArgs args)
                {
                    sessionContext = args.Context;
                });

                disposable.Server.SessionCreated += sessionCreatedHandler;

                MailClient.Send();

                disposable.Server.SessionCreated -= sessionCreatedHandler;

                Assert.False(sessionContext.IsSecure);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
Exemplo n.º 15
0
        public void CanReturnSmtpResponseException_SessionWillQuit()
        {
            // arrange
            var mailboxFilter = new DelegatingMailboxFilter(@from =>
            {
                throw new SmtpResponseException(SmtpResponse.AuthenticationRequired, true);

#pragma warning disable 162
                return(MailboxFilterResult.Yes);

#pragma warning restore 162
            });

            using (CreateServer(options => options.MailboxFilter(mailboxFilter)))
            {
                using (var client = MailClient.Client())
                {
                    Assert.Throws <ServiceNotAuthenticatedException>(() => client.Send(MailClient.Message()));

                    // no longer connected to this is invalid
                    Assert.ThrowsAny <Exception>(() => client.NoOp());
                }
            }
        }
Exemplo n.º 16
0
        public void CanAuthenticateUser()
        {
            // arrange
            string user              = null;
            string password          = null;
            var    userAuthenticator = new DelegatingUserAuthenticator((u, p) =>
            {
                user     = u;
                password = p;

                return(true);
            });

            using (CreateServer(options => options.AllowUnsecureAuthentication().UserAuthenticator(userAuthenticator)))
            {
                // act
                MailClient.Send(user: "******", password: "******");

                // assert
                Assert.Equal(1, MessageStore.Messages.Count);
                Assert.Equal("user", user);
                Assert.Equal("password", password);
            }
        }
Exemplo n.º 17
0
        public void CanFailAuthenticationEmptyUserOrPassword(string user, string password)
        {
            // arrange
            string actualUser        = null;
            string actualPassword    = null;
            var    userAuthenticator = new DelegatingUserAuthenticator((u, p) =>
            {
                actualUser     = u;
                actualPassword = p;

                return(false);
            });

            using (CreateServer(endpoint => endpoint.AllowUnsecureAuthentication(), services => services.Add(userAuthenticator)))
            {
                // act and assert
                Assert.Throws <MailKit.Security.AuthenticationException>(() => MailClient.Send(user: user, password: password));

                // assert
                Assert.Empty(MessageStore.Messages);
                Assert.Equal(user, actualUser);
                Assert.Equal(password, actualPassword);
            }
        }
Exemplo n.º 18
0
        public void SecuresTheSessionWhenCertificateIsSupplied()
        {
            ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationFailureForTestingOnly;

            using (var disposable = CreateServer(options => options.Certificate(CreateCertificate())))
            {
                ISessionContext sessionContext        = null;
                var             sessionCreatedHandler = new EventHandler <SessionEventArgs>(
                    delegate(object sender, SessionEventArgs args)
                {
                    sessionContext = args.Context;
                });

                disposable.Server.SessionCreated += sessionCreatedHandler;

                MailClient.Send();

                disposable.Server.SessionCreated -= sessionCreatedHandler;

                Assert.True(sessionContext.NetworkClient.IsSecure);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }