예제 #1
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.NetworkClient.IsSecure);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
예제 #2
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);
            }
        }
예제 #3
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.IsSecure);
            }

            ServicePointManager.ServerCertificateValidationCallback = null;
        }
예제 #4
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);
            }
        }